Files
oxc-guard 60fa13878c release(crates): oxc v0.149.0 (#26397)
### 💥 BREAKING CHANGES

- 66744f0 parser: [**BREAKING**] Rename `panicked` to `fatal_error` in `ParserReturn` (#26382) (overlookmotel)
- 2c9a947 parser: [**BREAKING**] Reduce `MAX_LEN` to 256 bytes below `u32::MAX` (#26352) (overlookmotel)

### 🚀 Features

- 32bdc5b allocator: Construct `ArenaHashSet` with any `Default` hasher (#26372) (Dunqing)
- a17de58 minifier: Process of return/throw in non last position and fix async * inlining (#26214) (Armano)
- bfb4c57 parser: Distinguish unapplied no-side-effects comments (#26120) (碳苯 Carbon)
- 1d9b9d3 ast: Add `GetNodeId` trait (#26145) (camc314)

### 🐛 Bug Fixes

- b156333 codegen: Order accessibility before abstract on accessors (#26392) (camc314)
- 116c8b8 transformer/class-properties: Keep private methods in class-expression scope (#26308) (Changsu Seong)
- 1400a0f parser: Require a string source after `export ... from` (#26389) (camc314)
- 2da73b7 codegen: Print matching quoted import names as identifiers (#26386) (camc314)
- 0a81d29 codegen: Preserve private-in left operand precedence (#26383) (camc314)
- 9a02337 parser: Correctly round large nondecimal literals (#26379) (camc314)
- 3d95477 transformer: Gate async-only rewrites by owning transform (#26326) (Dunqing)
- f45a9ee transformer: Limit arguments capture to lowered async functions (#26317) (Dunqing)
- 789969f transformer: Gate this capture by owning async transform (#26227) (Dunqing)
- affeb14 minifier: Mangle class private members in Node API (#26118) (碳苯 Carbon)
- 8b35abd semantic: Detect duplicate private class elements (#26361) (camc314)
- 457ed57 semantic: Reject await and yield in rest parameter defaults (#26359) (camc314)
- 4b89c7b semantic: Reject jumps across arrow functions (#26357) (camc314)
- c966aca parser: Do not omit `<` token opening type argument list (#26328) (overlookmotel)
- f07dca5 ast_visit: Add the trimmed prefix length to translation table offsets (#26173) (Bharadwaj Pendyala)
- 3ab9bd1 parser: Do not re-lex template substitution tail after fatal error (#26230) (overlookmotel)
- 07851b9 parser: Fix debug assert failure when lexer error with tokens enabled (#26229) (overlookmotel)
- 00dea7a allocator: Gate `Allocator::data_end_ptr` behind fixed_size feature (#26248) (camc314)
- 853ffab ecmascript: Avoid `charAt` panic on 32-bit (#26244) (camc314)
- 34feccb runtime: Remove stale regenerator exports (#26243) (camc314)

### ⚡ Performance

- 34a242e minifier: Use `Ident` for property mangler (#26334) (sapphi-red)
- 5303f5c minifier: Unify merging of last expression into target sequence (#26264) (Armano)
- 2ca7d29 minifier: Consume nodes in minimize_statements in reverse order (#26258) (Armano)
- d2354b4 mangler: Use `Ident` for variable names to keep (#26333) (sapphi-red)
- 5b3e335 minifier: Use `Ident` instead of `Str` in `KeepVar` (#26332) (sapphi-red)
- 51366fb minifier: Use `IdentHashSet` in `PrivateMemberUsageStack` (#26331) (sapphi-red)
- d19c42a minifier: Merge nested if stmt in place instead of creating dummies (#26351) (Armano)
- 356def6 parser: Shrink annotation comment ranges (#26356) (overlookmotel)
- a5be474 parser: Shave instruction off `parse_jsx_element_name` (#26355) (overlookmotel)
- 9780663 parser: Remove fatal error guard from `parse_jsx_element_name` (#26354) (overlookmotel)
- 766e12f parser: Remove `token` field from `LexerCheckpoint` (#26350) (overlookmotel)

### 📚 Documentation

- d4b4e61 transformer: Document ES2015 target floor (#26378) (Dunqing)
- 67dd054 ast_visit: Correct out-of-date comments on `Utf8ToUtf16` converter (#26285) (overlookmotel)
- a8ed2a7 minifier: Fix stale validation instructions (#26251) (camc314)
2026-09-07 14:29:34 +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.