Parser
parseSync parses JavaScript, TypeScript, and JSX into an ESTree-based program. It is exported by @swc-next/core, @swc-next/parser, and the initialized @swc-next/core-browser package.
Signature
source is the complete source text. Omitting options or passing null uses the defaults below. Parsing is synchronous and can return a recovered AST with diagnostics.
ParserOptions
Omitted fields use their defaults. Invalid option values, such as lang: "javascript" or preserveParens: 1, throw a TypeError.
lang
Type: "js" | "ts" | "jsx" | "tsx" | "dts"
Default: "js"
Choose the syntax accepted by the parser:
TypeScript annotations and JSX remain in the AST. Selecting a language does not strip types, transform JSX, or type-check the source. Select sourceType separately; for file-based input, use langFromPath to choose the language from the extension.
sourceType
Type: "module" | "script" | "commonjs" | "unambiguous"
Default: "module"
Choose the rules for the file's top level:
Detection is based on syntax, rather than calls to require or assignments to module.exports. A dynamic import() alone also does not select module mode.
This option controls parsing only; it does not convert a module to CommonJS or resolve imports. Use sourceTypeFromPath when a file extension specifies the source type.
preserveParens
Type: boolean
Default: true
Keep parentheses as ParenthesizedExpression nodes and, for TypeScript types, TSParenthesizedType nodes. With false, keep only the inner expression or type, matching typescript-eslint's representation.
This changes the tree's shape, not expression precedence. The printer inserts parentheses required to preserve the AST's meaning even when explicit parenthesized nodes were omitted.
comments
Type: "none" | "flat"
Default: "flat"
With "flat", return every collected comment in source order in result.comments. With "none", skip collection and return an empty array. Comments are separate from the program and are not attached to AST nodes.
value excludes the comment delimiters, while start and end include them. Offsets use UTF-16 code units and the end is exclusive. A hashbang such as #!/usr/bin/env node is stored separately in program.hashbang.
Collecting comments does not make printSync emit them: its JavaScript interface takes the program and metadata, without the flat comment list.
semanticErrors
Type: boolean
Default: false
Run an additional AST pass and append semantic early errors to result.diagnostics. These include conflicting declarations in the same scope and exports of undeclared local names. Syntax diagnostics are reported regardless of this setting, and semantic checks also run on a recovered AST after syntax errors.
Enabling this option adds diagnostics without changing the AST. It does not expose the full semantic model or perform TypeScript type checking, such as checking whether a value matches its annotated type.
ParseResult
The program, comments, and diagnostics are decoded lazily when accessed. See AST types for node declarations.
Diagnostics and positions
A Diagnostic contains:
AST nodes and comments also use UTF-16 offsets. End offsets are exclusive, so source.slice(start, end) selects the corresponding span.
Path helpers
These helpers are exported alongside parseSync; they do not read the file. Pass the returned values as parser options:
Language and source type options accept strings; their TypeScript names are not runtime enums.
langFromPath checks the following suffixes in order:
sourceTypeFromPath returns "module" for .mjs and .mts, "commonjs" for .cjs and .cts, and "unambiguous" for all other paths. Both helpers use case-sensitive suffix matching and do not inspect package.json.