JavaScript API

createGenerator

Create a builtin generator over a fixed AST and options.

function createGenerator(ast: Ast, options: GeneratorOptions): Generator;

type Generator = {
    generate: () => Promise<CodeResult>;
};

Creates a builtin generator over ast and options.

The returned generator calls the builtin generator directly and never runs the plugin chain. Use generate when plugins should be able to replace the generated result.

Parameters

NameTypeDescription
astAstThe AST to generate from.
optionsGeneratorOptionsThe generator's fixed, resolved options.

GeneratorOptions

Every field is required. In a generate hook, the stage options are available as args.options.

FieldTypeDescription
cwdstringCurrent working directory. Reserved; codegen ignores it.
filestringThe file being generated.
sourceMapSourceMapOptionsThe source-map inputs and outputs requested for the step.

Generation uses ast.language when present, then the file extension. GeneratorOptions has no language field.

The builtin generator composes the AST's input map into the produced map and returns that map separately. Output delivery modes (external, inline, and both) are applied by the pipeline, not by the builtin generator.

Returns

A Generator whose generate() method returns Promise<CodeResult>:

FieldTypeDescription
codestringThe generated code.
mapSourceMap | nullThe source map for the generated code, when one was produced.

Generation failures reject with a CompileError.

Example

import type { AstResult, CodeResult, Generator } from "telarel";

import { createGenerator, parse } from "telarel";

const { ast }: AstResult = await parse("const x: number = 1;");

const generator: Generator = createGenerator(ast, {
    cwd: process.cwd(),
    file: "index.ts",
    sourceMap: {},
});

const result: CodeResult = await generator.generate();

Last updated on

Copyright © 2026, Alpheus

On this page