Practical Abstract Syntax Trees
4.7 101 students started
What You Will Learn
Abstract syntax tree fundamentals
How to parse, traverse, and generate abstract syntax trees
In this course, we'll start with the fundamentals of abstract syntax trees (ASTs) and learn the basic mental models. This general AST knowledge can be translated to almost any tool that works with ASTs.
Why this course?
Understanding and using ASTs unlocks the ability to make sweeping changes in a safe and reliable way in any size codebase.
Throughout this course, we'll have converted source code into ASTs, traversed, mutated, and generated ASTs. With these concepts we'll then explore several practical applications including things like code audits (static analysis), code transformations (codemods), and linting.
We'll learn the fundamentals of abstract syntax trees.
- What is an AST?
- How to explore an AST
We'll learn how to work with ASTs.
- How to turn code into an AST
- How to programmatically navigate any AST
- How to leverage TypeScript to prevent runtime errors
We'll learn how to statically analyze, or "audit" code to understand the state of the codebase using abstract syntax trees.
- An introduction to an example codebase and refactor
- Understanding the state of the current codebase
- When to use an AST-based tool versus doing something manually
We'll learn how to transform, or "codemod" code from one state to another using abstract syntax trees.
- How to make changes to an AST
- How to change ASTs with jscodeshift
- How to test a code transform
We'll learn how to write rules, or "lint" code using abstract syntax trees.
- How to create rules for code
- How to create custom rules with ESLint
- How to test a rule
Understanding Abstract Syntax Trees (AST)
What is an AST?
Understand the basics of an AST and how it relates to real-world code
Exploring an AST
Looking at an example code snippet and how it relates to the resulting AST
A quick overview of the basic environment setup used throughout this course
Working with Abstract Syntax Trees
Traversing an AST
Programmatically traverse an AST and visit arbitrary nodes
Preventing Runtime Errors (with Types)
See how types can surface runtime errors
Practical code audits
A practical example of needing to perform a code audit (static analysis) to understand the current state of a codebase
Performing a code audit
Create a custom script to audit a codebase
Adding the Button component
Introduce a shared Button component to replace all existing button elements
When to use AST-based tooling
A rough formula to determine when to use AST-based tooling
Mutating an AST
Transforming code in place by mutating an AST
Using jscodeshift to make the same code transformations with less boilerplate
Testing a transform
Create tests for jscodeshift transforms for a faster feedback loop
Creating rules for code
Implement a linting rule to prevent the code we just transformed from being reintroduced in the future
Use ESLint to create the same linting rule for the code with less boilerplate
Testing custom linting rules
Testing custom ESLint linting rules to verify they work as expected
Frequently Asked Questions
Who is this course for?
What if I need help?
You can ask us questions anytime through the community Discord channel or by sending us a message.
8 months ago
Recommend learning about AST's with Spencer. The course has hands-on examples and clearly explained topics.