generate
Generate code from an AST.
pub async fn generate(
ast: Ast,
options: GenerateOptions,
plugins: Vec<SharedPluginable>,
) -> Result<CodeResult, CompileError>Generates code from ast.
The builtin generator is the terminal fallback after every generate hook defers, and the resolved output source-map mode is applied to the winning result.
Parameters
| Name | Type | Description |
|---|---|---|
ast | Ast | The AST to generate from. |
options | GenerateOptions | The generate options. |
plugins | Vec<SharedPluginable> | The plugins to run. |
GenerateOptions
Every field is optional and resolved against defaults before use.
| Field | Type | Default | Description |
|---|---|---|---|
cwd | Option<PathBuf> | process working dir | Filesystem root used to resolve relative paths. |
file | Option<String> | index.ts | The file being generated; used for option resolution and source-map defaults. Does not change the AST grammar. |
language | Option<Language> | AST language, then file | The grammar recorded in the resolved step options and visible to plugins. Builtin generation uses the AST grammar. |
output | Option<OutputOptions> | file | Output code settings. output.file derives the default source-map URL. |
source_map | Option<SourceMapOptions> | off | The source-map inputs and outputs requested for the step. |
language is resolved into the step options: an explicit value wins, otherwise the AST grammar, then file. The explicit value is visible to the options hook, and the resolved value is visible to start and later hooks through the resolved options and PluginContext.module. Builtin code generation does not use it: it emits from the AST's own Program.source_type. Module kind remains owned by Program.source_type: there is no GenerateOptions.source_type.
Returns
Result<CodeResult, CompileError>, where CodeResult is { code: String, map: Option<SourceMap> }.
Example
use telarel::{generate, parse, GenerateOptions, ParseOptions, AstResult, CodeResult};
let parsed: AstResult = parse(
String::from("const x: number = 1;"),
ParseOptions::default(),
vec![],
).await?;
let result: CodeResult = generate(parsed.ast, GenerateOptions::default(), vec![]).await?;
println!("{}", result.code);Last updated on
rewrite
Rewrite an AST with plugins.
compile_from_ast
Compile an existing AST through rewrite and generation.
Copyright © 2026, Alpheus