### 💥 BREAKING CHANGES -a31567aallocator: [**BREAKING**] Make `Allocator::cursor_ptr` and `data_end_ptr` private (#26081) (overlookmotel) ### 🚀 Features -784e9faminifier: Invert `!0` and `!1` in place for boolean context to `1` and `0` (#26050) (Armano) -1e902ccminifier: Expand fold leading assignments into the var decl (#26142) (Armano) -3ed4f6aminifier: Expand de morgan's optimization to allow move of `!` (#25930) (Armano) -5672585parser: Attach all comments to nodes (#25944) (camc314) ### 🐛 Bug Fixes -4adfb4csemantic: Validate chained continue labels (#26157) (camc314) -b874f48ecmascript: `Math.round` only exact half ties (#26150) (camc314) -a625378minifier: Coerce omitted `indexOf` search argument (#26149) (camc314) -dc7398becmascript: Trim trailing whitespace in string to number (#26148) (camc314) -243b685transformer/object-rest: Lower multiple declarators correctly (#26147) (camc314) -dc09a3aparser: Avoid panic on escaped string export names (#26146) (camc314) -e412cf2linter: Clamp invalid JS plugin locations (#26144) (camc314) -c3dedc9semantic: Skip function body bindings in parameters (#26099) (Dunqing) -e74de61transform-react: Match Babel diagnostic reporting (#26128) (Boshen) -d5163d0parser: Correctly classify unapplied pure annotations (#26084) (camc314) -8531b9blinter/plugins: Alter method for obtaining mutable `Program` when sending AST to JS plugins (#26077) (overlookmotel) -9b51658regular_expression: Allow oversized decimal escape for Annex B (#26070) (leaysgur) -c8de4dfregular_expression: Reject oversized backreferences (#26055) (camc314) ### ⚡ Performance -ac4785adiagnostics: Use fixed ANSI styles (#26130) (Boshen) -07a0793packages/codegen: Flatten output in chunks (#26109) (overlookmotel) -487427apackages/codegen: Faster string flattening (#26108) (overlookmotel) -7785583packages/codegen: Ensure indent strings are flattened (#26107) (overlookmotel) -0dd4db3packages/codegen: Store mapping positions in an `Int32Array` (#26085) (overlookmotel)
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 deferandimport source. - In TS-ESTree AST,
import.defer(...)andimport.source(...)are represented as anImportExpressionwith'defer'or'source'inphasefield (as in ESTree spec), where TS-ESLint represents these as aCallExpressionwithMetaPropertyas itscallee. - Addition of a non-standard
hashbangfield toProgram.
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. Iftrue, AST nodes contain arangefield. Defaults tofalse.preserveParens:true|false. Iftrue, parenthesized expressions are represented by (non-standard)ParenthesizedExpressionandTSParenthesizedTypeAST nodes. Defaults totrue.showSemanticErrors:true|false. Iftrue, check file for semantic errors which parser does not otherwise emit e.g.let x; let x;. Has a small performance cost. Defaults tofalse.