Files
oxc/napi/parser
oxc-guard f799f174b5 release(crates): oxc v0.157.0 (#28719)
### 🚀 Features

- 8dd67a6 transform-react: Expose TypeScript transform options (Tomas Särnhammar)
- deed7dc parser: More friendly error for duplicate import attribute key (#28262) (sapphi-red)

### 🐛 Bug Fixes

- eaa8e90 ecmascript: Avoid numeric `+` folding when an operand may be a string (#28544) (camc314)
- b03c7b8 regular_expression: Allow incomplete oversized legacy quantifiers (#28449) (Boshen)
- 128c67d semantic: Check strict eval and arguments writes by reference flags (#28445) (Boshen)
- c97c43c parser: Reject TypeScript default exports in JavaScript (#28440) (Boshen)
- fdd119f span: Count Unicode characters when filtering typo suggestions (#28452) (Boshen)
- cb64638 regular_expression: Reject incomplete Unicode atom escapes (#28448) (Boshen)
- fc0eed3 semantic: Stop let-binding scans at arrow functions (#28444) (Boshen)
- 3421516 parser: Preserve default import names in reexports (#28442) (Boshen)
- a1e7e5f parser: Reject optional class members in JavaScript (#28439) (Boshen)
- 5b9a7bc parser: Parse sloppy let expressions and labels (#28438) (Boshen)
- 8e61652 regex: Reject templates without cooked values (#28345) (Boshen)
- 3b36543 parser: Preserve in-context after TypeScript type arguments (#28300) (Boshen)
- 2e3126c regex: Decode constructor patterns and flags consistently (#28298) (Boshen)
- 8a90be2 regex: Track capture groups and disjunction identity (#28297) (Boshen)
- 3dc2cb0 regex: Preserve escape values and boundaries (#28296) (Boshen)
- fc8acbc parser: Handle leading-zero numeric exponents (#28295) (Boshen)
- 91a638d parser: Normalize raw template line endings with invalid escapes (#28285) (Boshen)
- 2ac9a42 parser: Allow parenthesized private-in expressions (#28284) (Boshen)
- 052bca9 ast: Prevent greyed out fields in AST types in IDEs (#28335) (overlookmotel)
- ed7df7d ast/estree: Include comments in TS Program span (#28326) (camc314)

### ⚡ Performance

- 9b6b7bf regular_expression: Advance validated Unicode escapes directly (#28514) (Boshen)
- 82e1982 regular_expression: Share Unicode property name prefixes (#28513) (Boshen)
- 5395a40 regular_expression: Parse class set character prefixes once (#28512) (Boshen)
- 3708934 regular_expression: Defer literal range dash allocation (#28511) (Boshen)
- d85ee3d semantic: Skip arguments ancestry checks outside classes (#28510) (Boshen)
- cf81d07 semantic: Reuse private reference resolution (#28509) (Boshen)
- a79eebc semantic: Avoid duplicate maps for public JavaScript members (#28508) (Boshen)
- 235ca3b semantic: Skip escape checks for unescaped strings (#28507) (Boshen)
- babf44c parser: Reuse shorthand assignment target identifiers (#28504) (Boshen)
- 444f24a ecmascript: Short-circuit simple parameter checks for rest (#28503) (Boshen)
- 04b20c0 semantic: Compute duplicate parameter policy once per list (#28446) (Boshen)
- dbf8482 parser: Use token lookahead for simple function types (#28489) (Boshen)
- bca1275 parser: Avoid unused destructuring rest capacity (#28499) (Boshen)
- 691be5c parser: Compact cover initialized name storage (#28495) (Boshen)
- b3b5444 parser: Build simple arrow bindings directly (#28493) (Boshen)
- e1c9baf parser: Disambiguate single-identifier arrow heads (#28491) (Boshen)
- 550e98a regular_expression: Avoid rescanning unique capture names (#28450) (Boshen)
- 10e9fc7 parser: Skip duplicate lookup for singleton import attributes (#28441) (Boshen)
- 5238839 parser: Use a stack buffer for float separators (#28435) (Boshen)
- 6e35e3c parser: Fast-path leaf expressions before delimiters (#28432) (Boshen)
- 3d7512c parser: Decode Unicode identifiers once (#28414) (Boshen)
- 30f59f4 parser: Factor object type opening brace (#28430) (Boshen)
- 028a267 parser: Always inline postfix update parsing (#28429) (Boshen)
- 1b5a171 parser: Reuse export kind classification (#28424) (Boshen)
- 179a432 parser: Factor return type identifier parsing (#28416) (Boshen)
- 553dd1d parser: Inline hot paths and fold function dispatch (#28404) (Boshen)
- de75d56 parser: Combine parenthesized arrow lookahead (#28412) (Boshen)
- a768a0c parser: Reuse comment annotations after rewind (#28408) (Boshen)
- 563d92a parser: Skip redundant source cursor updates (#28409) (Boshen)
- 7941ca9 parser: Factor tuple identifier parsing (#28398) (Boshen)
- 426c151 parser: Construct completed tokens without intermediate writes (#28291) (Boshen)
- df42953 minifier: Don't request another pass for block unwrapping (#28315) (Dunqing)
- c2d663d parser: Avoid vectors for single parenthesized expressions (#28374) (Boshen)
- 50bd0a9 parser: Size delimited lists after the first element (#28373) (Boshen)
- bd869b5 parser: Bypass redundant operand parsing (#28372) (Boshen)
- 455fc09 parser: Skip keyword checks for plain identifier expressions (#28371) (Boshen)
- 61303fb parser: Fast-path identifier operands (#28306) (Boshen)
- 6cf5b1a parser: Guard arrow and conditional parsing (#28305) (Boshen)
- 3666939 parser: Specialize identifier reference parsing (#28303) (Boshen)
- 0d6641a parser: Skip inapplicable expression parsing paths (#28294) (Boshen)
- 3be7997 parser: Reduce lexer search batch size (#28292) (Boshen)
- ab5655a regex: Remove redundant character-class and quantifier checks (#28288) (Boshen)
- ec8e8d4 parser: Remove redundant grammar and lexer checks (#28287) (Boshen)
- c903f5c parser: Specialize known-token parsing (#28286) (Boshen)
- 2683225 str: `static_ident!` and `static_str!` macros guarantee const evaluation (#28340) (overlookmotel)
- df90836 ast: Reduce compile time of `#[ast]` macro (#28336) (overlookmotel)
- 8239dac parser: Replace dragonbox_ecma with zmij_ecma (#28282) (Boshen)
2026-11-02 19:40:44 +00:00
..
…

Oxc Parser

See usage instructions.

Features

Supports WASM

See https://stackblitz.com/edit/oxc-parser for usage example.

ESTree

When parsing JS or JSX files, the AST returned is fully conformant with the ESTree standard, the same as produced by Acorn.

When parsing TypeScript, the AST conforms to @typescript-eslint/typescript-estree's TS-ESTree format.

If you need all ASTs in the same with-TS-properties format, use the astType: 'ts' option.

The only differences between Oxc's AST and ESTree / TS-ESTree are:

  • Support for Stage 3 decorators.
  • Support for Stage 3 ECMA features import defer and import source.
  • In TS-ESTree AST, import.defer(...) and import.source(...) are represented as an ImportExpression with 'defer' or 'source' in phase field (as in ESTree spec), where TS-ESLint represents these as a CallExpression with MetaProperty as its callee.
  • Addition of a non-standard hashbang field to Program.

That aside, the AST should completely align with Acorn's ESTree AST or TS-ESLint's TS-ESTree. Any deviation would be considered a bug.

Property order

ESTree and TS-ESTree do not define an order for node properties. oxc-parser emits them in this order:

  1. type.
  2. Node-specific fields in Oxc's defined serialization order. Properties containing child nodes have the same relative order as the entries in visitorKeys[node.type].
  3. start, then end.
  4. range, when the range option is enabled.

visitorKeys is an exported map from a node type to the properties containing that node's children, in traversal order. For example, visitorKeys.ConditionalExpression is ['test', 'consequent', 'alternate']. This is not alphabetical order: walking alternate before test would not match the syntax or evaluation order of a conditional expression.

Fields omitted by the selected astType do not change the relative order of the remaining fields. A generic walker which recursively inspects Object.keys(node) therefore visits standard ESTree children in the same relative order as Oxc's Visitor and the exported visitorKeys.

AST Types

@oxc-project/types can be used. For example:

import { Statement } from "@oxc-project/types";

Visitor

An AST visitor is provided. See example below.

This package also exports visitor keys which can be used with any other ESTree walker.

import { visitorKeys } from "oxc-parser";

Fast Mode

By default, Oxc parser does not produce semantic errors where symbols and scopes are needed.

To enable semantic errors, apply the option showSemanticErrors: true.

For example,

let foo;
let foo;

Does not produce any errors when showSemanticErrors is false, which is the default behavior.

Fast mode is best suited for parser plugins, where other parts of your build pipeline has already checked for errors.

Please note that turning off fast mode ​incurs​ a small performance overhead.

Returns ESM information.

It is likely that you are writing a parser plugin that requires ESM information.

To avoid walking the AST again, Oxc Parser returns ESM information directly.

This information can be used to rewrite import and exports with the help of magic-string, without any AST manipulations.

export interface EcmaScriptModule {
  /**
   * Has ESM syntax.
   *
   * i.e. `import` and `export` statements, and `import.meta`.
   *
   * Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
   */
  hasModuleSyntax: boolean;
  /** Import statements */
  staticImports: Array<StaticImport>;
  /** Export statements */
  staticExports: Array<StaticExport>;
  /** Dynamic import expressions */
  dynamicImports: Array<DynamicImport>;
  /** Span positions of `import.meta` */
  importMetas: Array<Span>;
}

API

Functions

// Synchronous parsing
parseSync(filename: string, sourceText: string, options?: ParserOptions): ParseResult

// Asynchronous parsing
parse(filename: string, sourceText: string, options?: ParserOptions): Promise<ParseResult>

Use parseSync for synchronous parsing. Use parse for asynchronous parsing, which can be beneficial in I/O-bound or concurrent scenarios, though it adds async overhead.

Example

import { parseSync, Visitor } from "oxc-parser";

const code = "const url: String = /* 🤨 */ import.meta.url;";

// File extension is used to determine which dialect to parse source as.
const filename = "test.tsx";

const result = parseSync(filename, code);
// Or use async version: const result = await parse(filename, code);

// An array of errors, if any.
console.log(result.errors);

// AST and comments.
console.log(result.program, result.comments);

// ESM information - imports, exports, `import.meta`s.
console.log(result.module);

// Visit the AST
const visitations = [];

const visitor = new Visitor({
  VariableDeclaration(decl) {
    visitations.push(`enter ${decl.kind}`);
  },
  "VariableDeclaration:exit"(decl) {
    visitations.push(`exit ${decl.kind}`);
  },
  Identifier(ident) {
    visitations.push(ident.name);
  },
});

visitor.visit(result.program);

// Logs: [ 'enter const', 'url', 'String', 'import', 'meta', 'url', 'exit const' ]
console.log(visitations);

Options

All options are optional.

  • lang: 'js' | 'jsx' | 'ts' | 'tsx'. Set language of source. If omitted, language is deduced from file extension.
  • sourceType: 'script' | 'module' | 'unambiguous'. Set source type. Defaults to 'module'.
  • astType: 'js' | 'ts'. Set to 'ts' if you want ASTs of plain JS/JSX files to contain TypeScript-specific properties.
  • range: true | false. If true, AST nodes contain a range field. Defaults to false.
  • preserveParens: true | false. If true, parenthesized expressions are represented by (non-standard) ParenthesizedExpression and TSParenthesizedType AST nodes. Defaults to true.
  • showSemanticErrors: true | false. If true, check file for semantic errors which parser does not otherwise emit e.g. let x; let x;. Has a small performance cost. Defaults to false.