JavaScript Object to AST Code Generator
Parse JavaScript source code into ESTree-compliant Abstract Syntax Trees. Generate, inspect, query, and convert AST nodes back to source code.
About
Incorrect AST manipulation leads to silent code generation bugs: malformed output, lost scope bindings, or invalid syntax that only surfaces at runtime. This tool implements a full recursive-descent parser conforming to the ESTree specification, producing the same node types (Program, FunctionExpression, MemberExpression, etc.) used by Esprima, Acorn, and Babel. It parses raw JavaScript source into a complete AST, renders the tree as inspectable JSON, and regenerates valid source code via a recursive walker. The parser handles ES6+ constructs including arrow functions, template literals, destructuring, spread operators, classes, and computed properties. A CSS-like selector engine lets you query nodes (e.g., function > id or call[callee = "require"]) for targeted inspection. Note: this parser approximates a subset of ECMAScript 2020. Edge cases involving obscure automatic semicolon insertion rules or exotic Unicode escapes may produce divergent results compared to V8's internal parser.
Formulas
The parser operates as a two-phase pipeline. Phase 1 is lexical analysis (tokenization). The source string S of length n is scanned character-by-character producing a token stream T.
Each token ti carries a type (keyword, identifier, literal, punctuator) and value. Phase 2 applies a Pratt parser (top-down operator precedence). Each token type has a binding power bp and optional null-denotation (nud) and left-denotation (led) functions.
Source regeneration walks the AST recursively. For each node type, a visitor function emits the corresponding JavaScript syntax with proper indentation depth d.
Where S = source string, n = source length, T = token array, m = token count, ti = individual token, bp = binding power (integer precedence), rbp = right binding power threshold, nud = null denotation (prefix handler), led = left denotation (infix handler), d = indentation depth.
Reference Data
| Short Name | ESTree Node Type | Description | Example |
|---|---|---|---|
| program | Program | Root node of any parsed source | var x = 1; |
| var | VariableDeclaration | Variable declaration with kind | let a = 5 |
| decl | VariableDeclarator | Single declarator within a declaration | a = 5 (inside var) |
| function | FunctionExpression | Anonymous or named function expression | function(x){} |
| functiondecl | FunctionDeclaration | Named function declaration (hoisted) | function foo(){} |
| arrow | ArrowFunctionExpression | ES6 arrow function | x => x+1 |
| call | CallExpression | Function invocation | foo(1, 2) |
| member | MemberExpression | Property access (dot or bracket) | obj.prop |
| object | ObjectExpression | Object literal | {a: 1} |
| array | ArrayExpression | Array literal | [1, 2, 3] |
| binary | BinaryExpression | Binary operator expression | a + b |
| logical | LogicalExpression | Logical operator expression | a && b |
| unary | UnaryExpression | Unary prefix operator | !x |
| update | UpdateExpression | Increment or decrement | i++ |
| assign | AssignmentExpression | Assignment with operator | x = 10 |
| conditional | ConditionalExpression | Ternary operator | a ? b : c |
| if | IfStatement | Conditional branching | if(x){} |
| for | ForStatement | C-style for loop | for(;;){} |
| forin | ForInStatement | For-in enumeration | for(k in o){} |
| while | WhileStatement | While loop | while(x){} |
| dowhile | DoWhileStatement | Do-while loop | do{}while(x) |
| return | ReturnStatement | Return from function | return x; |
| throw | ThrowStatement | Throw an exception | throw e; |
| try | TryStatement | Try-catch-finally | try{}catch(e){} |
| switch | SwitchStatement | Switch statement | switch(x){} |
| case | SwitchCase | Case clause in switch | case 1: |
| this | ThisExpression | Reference to this | this |
| new | NewExpression | Constructor invocation | new Foo() |
| sequence | SequenceExpression | Comma-separated expressions | a, b, c |
| property | Property | Key-value pair in object | key: val |
| literal | Literal | Primitive value | 42, "hello" |
| identifier | Identifier | Named reference | myVar |
| template | TemplateLiteral | Template string | `hello ${x}` |
| spread | SpreadElement | Spread operator | ...args |
| class | ClassDeclaration | ES6 class | class Foo{} |