Guide/Parser

Parser

Use parseSync to turn source text into an AST compatible with ESTree. TypeScript and JSX nodes follow typescript-eslint's AST format. Install the package for your runtime first.

Parse a program

import { parseSync } from "@swc-next/core";

const { program, diagnostics } = parseSync("const answer = 42;");
console.log(program.type); // Program
console.log(diagnostics); // []

Inspect diagnostics before transforming or printing recovered syntax.

Parse TypeScript or JSX

Choose the language explicitly:

import { parseSync } from "@swc-next/core";

const result = parseSync("const answer: number = 42;", { lang: "ts" });
console.log(result.program.body[0].type); // VariableDeclaration

Use jsx or tsx for JSX input, and dts for declaration files. Parsing TypeScript preserves its types; it does not compile them away.

Next steps

Walk the AST or generate code.

See the Parser API for input and output types, language and source type options, comments, diagnostics, and source positions.