Traversing the AST with Visitors
Walk syntax tree nodes to analyze code structure.
What Is the AST?
TypeScript parses source code into an Abstract Syntax Tree (AST) — a tree of nodes representing language constructs like variables, functions, and expressions.
import ts from "typescript";
const sf = ts.createSourceFile(
"example.ts",
"const x = 1;",
ts.ScriptTarget.Latest
);Node Kinds
Every AST node has a kind property (a ts.SyntaxKind enum value) indicating what it represents.
import ts from "typescript";
// Common kinds:
// ts.SyntaxKind.VariableDeclaration
// ts.SyntaxKind.FunctionDeclaration
// ts.SyntaxKind.IdentifierAll lessons in this course
- Creating a TypeScript Program with the API
- Traversing the AST with Visitors
- Custom Transformers and Code Generation
- Building a Simple Linting Tool