Rust API

rewrite

Rewrite an AST with plugins.

pub async fn rewrite(
    ast: Ast,
    options: RewriteOptions,
    plugins: Vec<SharedPluginable>,
) -> Result<AstResult, CompileError>

Rewrites ast through the plugin chain. An empty plugin list runs no transform hooks, so the AST is returned without modification.

The AST's own input map wins; otherwise the resolved input source map is attached before the stages run.

Parameters

NameTypeDescription
astAstThe AST to rewrite.
optionsRewriteOptionsThe rewrite options.
pluginsVec<SharedPluginable>The plugins to run.

RewriteOptions

Every field is optional and resolved against defaults before use.

FieldTypeDefaultDescription
cwdOption<PathBuf>process working dirFilesystem root used to resolve relative paths.
fileOption<String>index.tsThe file being rewritten; used for option resolution. Does not change the AST grammar.
languageOption<Language>AST language, then fileThe grammar recorded in the resolved step options and visible to plugins. Does not change the input AST grammar.
source_typeOption<SourceType>inferredThe module kind recorded in the resolved step options and visible to plugins. Does not change the input AST module kind.
input_source_mapOption<InputSourceMap>offThe input source-map request for the step.

The explicit values are visible to the options hook; after resolution the values (explicit or inferred) are visible to start and later hooks through the resolved options and PluginContext.module. When language is omitted, the resolved language falls back to the input AST grammar, then the file extension. rewrite does not reparse the input AST or change its grammar or module kind. The returned AST is the input AST or a plugin replacement, with the grammar and module kind that AST was built with.

rewrite produces an AST, so it has no output source map. The shared plugin options bag presents this request as source_map.input; a plugin-set source_map.output or top-level output is ignored by rewrite.

Returns

Result<AstResult, CompileError>, where AstResult is { ast: Ast }. See Ast.

Example

use telarel::{
    parse,
    rewrite,
    ParseOptions,
    RewriteOptions,
    AstResult,
};

let parsed: AstResult = parse(
    String::from("const x: number = 1;"),
    ParseOptions::default(),
    vec![],
).await?;

let rewritten: AstResult = rewrite(parsed.ast, RewriteOptions::default(), vec![]).await?;

Last updated on

Copyright © 2026, Alpheus

On this page