Files
oxc-guard 5a6e37e5cf release(crates): oxc v0.150.0 (#26632)
### 🚀 Features

- ca649e0 ecma: Define math constants as known globals and resolve their types (#26585) (Armano)
- 9ef028c codegen: Add `ascii_only` option (#25994) (Samuel Attard)
- 80a76a0 minifier: Negate binary comparison for `typeof x < 'u'` (#26367) (Armano)

### 🐛 Bug Fixes

- 8ca76da parser: Reject `accessor` modifiers on methods (#26617) (camc314)
- 1916f31 parser: Reject `readonly` modifier on constructors (#26612) (camc314)
- 1c42008 parser: Handle escaped let in for loops (#26583) (camc314)
- d21d5cf parser: Recognize annotated empty arrows in conditionals (#26537) (camc314)
- d7713ad parser: Classify Unicode line breaks in block comments (#26536) (camc314)
- 75cd919 transformer: Preserve receivers in private optional chains (#26535) (camc314)
- b32d25d parser: Reject escaped import-phase keywords (#26534) (camc314)
- 47b8311 parser: Recognize contextual binding names in type lookaheads (#26532) (camc314)
- d6b6705 parser: Require arrow separator in TypeScript function types (#26529) (camc314)
- e98beef parser: Disambiguate await using in for initializers (#26527) (camc314)
- 92afee6 parser: Allow parenthesized JSX comma expressions with preserve_parens=false (#26524) (camc314)
- 31508b1 parser: Reject return types on constructor overloads (#26523) (camc314)
- a091fc4 parser: Validate await context for await using declarations (#26495) (camc314)
- 5501e86 parser: Disallow in expressions in using for-loop initializers (#26490) (camc314)
- c8e5fa7 parser: Allow escaped type names in import and export specifiers (#26487) (camc314)
- 2dcee2f parser: Reject async modifiers on class fields (#26486) (camc314)
- 973d58e parser: Require comma after TypeScript this parameter (#26480) (camc314)
- 32d00c5 codegen: Preserve instantiation expression precedence (#26424) (camc314)
- cfa47ab parser: Allow `in` expressions in class static blocks (#26423) (camc314)
- 5986187 packages/codegen: Preserve private-in right operand precedence (#26420) (camc314)
- f8e6c6c packages/codegen: Preserve in restriction through yield arguments (#26421) (camc314)
- d61e3bf parser: Validate TS named tuple rest elements (#26419) (camc314)
- ae6c386 codegen: Preserve in restriction through yield arguments (#26413) (camc314)
- 42ac916 codegen: Preserve private-in right operand precedence (#26411) (camc314)
- 10521b2 parser: Allow escaped type default import bindings (#26409) (camc314)
- 72cb5e3 parser: Reject rest parameters in getters (#26400) (camc314)
- 6e15ad5 packages/codegen: Print matching quoted import names as identifiers (#26404) (camc314)
- bbbb4bc packages/codegen: Preserve private-in left operand precedence (#26403) (camc314)
- a111b5b packages/codegen: Print accessibility modifiers before abstract (#26402) (camc314)
- 4e76602 parser: Allow `in` in arrow block bodies within `for` initializers (#26395) (camc314)
- b20fc19 parser: Reject partially parenthesized mixed coalesce expressions (#26394) (camc314)

###  Performance

- 1f902a6 isolated_declarations: Key scope maps by `Ident` (#26380) (Dunqing)
- a242469 minfiier: Reduce allocs when creating indirect access (#26601) (Armano)
- 5b4787f minifier: Update chain expressions in place (#26544) (Armano)
- 0bc1661 minifier: Try merging before creating new expression statements (#26556) (Armano)
- c78d707 minifier: Process newly created stmt in handle_if_statement (#26541) (Armano)
- 029c84b minfier: Update expressions in place when substituting alternate syntax (#26460) (Armano)
- d198982 codegen: Outline postfix source mapping work (#26450) (camc314)
- 53f006e ecmascript: Format small integer literals with itoa (#26446) (camc314)
- 8bfb8c0 codegen: Avoid duplicate sourcemap name lookups (#26441) (camc314)

### 📚 Documentation

- 38533ac ast: Move type annotation span comment to span field (#26522) (camc314)
2026-09-14 11:25:40 +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.

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.