Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

🖨️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: An ast.VisitableNode (typically an *ast.Program), but can be any node type.

  • Returns: A string containing valid, printable JavaScript code


🧪 Example

Input JavaScript (example.js):

Output:


Last updated