"매 source code 의 tree-shaped semantic skeleton". 매 lexer 의 token stream 을 parser 가 grammar rule 따라 nested node 의 tree 로 변환 — comment/whitespace/parens 의 trivia 를 제거하고 매 program 의 structural meaning 만 보존. 매 2026 의 Babel·SWC·TypeScript Compiler·Tree-sitter·Rust analyzer 의 모든 modern toolchain 의 핵심 IR.
매 핵심
매 AST vs CST
CST (Concrete Syntax Tree): 매 source 의 every char 보존 — formatter, linter용.
AST: 매 semantic structure 만 — compiler, transpiler, type checker용.
importtraversefrom"@babel/traverse";traverse(ast,{BinaryExpression(path){if(path.node.operator==="+"){// 매 constant folding
const{left,right}=path.node;if(left.type==="NumericLiteral"&&right.type==="NumericLiteral"){path.replaceWith({type:"NumericLiteral",value: left.value+right.value});}}},});
TypeScript Compiler API
import*astsfrom"typescript";constsource=ts.createSourceFile("x.ts","const x: number = 1;",ts.ScriptTarget.Latest);functionvisit(node: ts.Node){if(ts.isVariableDeclaration(node)){console.log(node.name.getText(source));}ts.forEachChild(node,visit);}visit(source);
Tree-sitter (incremental)
constParser=require("tree-sitter");constTS=require("tree-sitter-typescript").typescript;constparser=newParser();parser.setLanguage(TS);consttree=parser.parse("const x = 1;");// 매 incremental: tree.edit(...) 로 partial reparse
기본값: 매 TS 의 type-aware 작업 → ts-morph. 매 perf-critical 의 transpile → SWC. 매 editor → Tree-sitter.
🔗 Graph
부모: Parser
응용: ESLint · Prettier
🤖 LLM 활용
언제: 매 codemod 의 generation, AST node type 의 explanation, visitor pattern 의 boilerplate.
언제 X: 매 large-scale 의 type-aware refactor — ts-morph 의 deterministic 가 더 안전.
❌ 안티패턴
Regex 의 code 의 transform: 매 nested syntax 의 break — AST 의 use.
Mutate AST 의 children array 의 직접: 매 visitor 의 path API 의 use.
Source map 의 무시: 매 transform 후 의 debug 의 broken — retainLines/sourcemap 의 emit.