JavaScript API

parse

Parse source text into an AST.

function parse(code: string, options?: ParseOptions): Promise<AstResult>;

Parses code with plugins, in memory.

The builtin parser is the terminal fallback after every parse plugin hook defers, and it resolves any requested input source map.

Parameters

NameTypeDescription
codestringThe source text to parse.
optionsParseOptionsOptional parse options.

ParseOptions

FieldTypeDefaultDescription
cwdstringprocess.cwd()Current working directory.
filestringindex.tsThe file being parsed. The language is inferred from its extension.
languageLanguageinferredThe grammar of the code. Overrides the extension inference.
sourceTypeSourceTypeinferredThe module system of the code. Overrides the grammar's module kind.
inputSourceMapInputSourceMapoffThe input source-map request for the step.
pluginsPluginOption[][]The plugins to run.

Language is one of "js", "ts", "dts", "jsx", or "tsx".

SourceType is one of "script", "commonjs", "module", or "unambiguous".

InputSourceMap is a SourceMap value, a literal input mode ("inline", "external", or "auto"), or { mode, path? }; in the options form, mode accepts a SourceMap value or a literal input mode.

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

Returns

Promise<AstResult>, where AstResult is { ast: Ast }. ast.language is the resolved grammar: an explicit language option, or file-extension inference otherwise.

Example

import type { AstResult } from "telarel";

import { parse } from "telarel";

const result: AstResult = await parse("const x: number = 1;");

Last updated on

Copyright © 2026, Alpheus

On this page