🖨️Code Generation
The generator module in go-fAST is responsible for converting an Abstract Syntax Tree (AST) back into human-readable, syntactically correct JavaScript source code. It traverses the AST and emits a source string, preserving formatting, indentation, and code structure.
✨ Overview
The generator is a pretty-printer and code emitter. It uses a GenVisitor to walk the AST and stream source code into a string builder.
What It Does
Converts AST back to source code
Adds parentheses, indentation, and line breaks where required
Handles literal formatting (e.g.
true,false,1.23,"abc")Reconstructs statements, declarations, expressions, blocks, and more
🧰 API
import "github.com/t14raptor/go-fast/generator"
output := generator.Generate(program)program: Anast.VisitableNode(typically an*ast.Program), but can be any node type.Returns: A
stringcontaining valid, printable JavaScript code
🧪 Example
Input JavaScript (example.js):
Output:
Last updated