oxc-parser
A high-performance JavaScript and TypeScript parser for Node.js, powered by Rust. The parser generates an ESTree-compatible AST with support for the latest ECMAScript features.Installation
Quick Start
- TypeScript
- JavaScript
API Functions
parseSync
Synchronous parsing on the current thread. This is the recommended function for most use cases as it avoids the overhead of spawning a thread.The name of the file being parsed. Used for error messages and to infer language type from extension (
.js, .ts, .jsx, .tsx, .mjs, .cjs, .mts, .cts).The JavaScript or TypeScript source code to parse.
Configuration options for the parser. See ParserOptions for details.
Example
parse
Asynchronous parsing on a separate thread. The Rust-side parsing happens on a separate thread, but deserialization to JavaScript objects happens on the main thread.The majority of the workload (deserialization) cannot be parallelized. Generally
parseSync is preferable as it doesn’t have thread spawning overhead. Use worker threads with parseSync if you need to parallelize multiple files.The name of the file being parsed.
The source code to parse.
Configuration options for the parser.
Promise<ParseResult>
Example
ParserOptions
Configuration options for parsing behavior.Explicitly specify the language to parse. If not provided, inferred from the filename extension.
'js'- JavaScript'jsx'- JavaScript with JSX'ts'- TypeScript'tsx'- TypeScript with JSX'dts'- TypeScript declaration file
How to interpret the source code.
'script'- Classic script (no imports/exports)'module'- ES module (supports import/export)'commonjs'- CommonJS module (supports top-level return)'unambiguous'- Auto-detect based on imports/exports
Controls whether TypeScript-specific properties are included in the AST.
'js'- ESTree-compatible AST (default for JS/JSX files)'ts'- TS-ESTree-compatible AST with TypeScript properties (default for TS/TSX files)
astType: 'js' when parsing TypeScript if you want a cleaner AST without TypeScript-specific fields like decorators, typeAnnotation, etc.Include
range property on AST nodes. The range is a [number, number] tuple indicating start/end offsets.Emit
ParenthesizedExpression and TSParenthesizedType nodes in the AST.When true, parenthesized expressions are represented by non-standard ParenthesizedExpression nodes. When false, parentheses are omitted.Perform an additional semantic analysis pass to detect errors that depend on symbols and scopes.This adds a small performance overhead but catches errors like:
- Duplicate variable declarations
- Invalid break/continue statements
- Undeclared variables in strict mode
ParseResult
The result of parsing, containing the AST, module information, comments, and any errors.The root AST node conforming to the ESTree specification.
All comments found in the source code.
Parse and semantic errors. The parser recovers from common syntax errors, so transformed code may still be available even with errors.
Module Information
Themodule property provides detailed information about ES module syntax.
Static Imports
Static Exports
Dynamic Imports
Advanced Usage
TypeScript Parsing
JSX/TSX Parsing
Error Handling
Working with Comments
AST Visitor Pattern
Performance Tips
Use parseSync for Single Files
The synchronous API avoids thread spawning overhead and is faster for parsing individual files.
Disable Unused Features
Set
preserveParens: false and avoid showSemanticErrors unless needed to improve performance.Parallelize with Worker Threads
For parsing multiple files, use Node.js worker threads with
parseSync instead of the async parse function.Reuse AST
The parser is very fast. Caching the AST is often unnecessary unless you’re parsing the same file repeatedly.
Type Definitions
The package includes full TypeScript definitions. Import types as needed:Related
Transformer API
Transform JavaScript/TypeScript with Babel-like functionality
Minifier API
Minify JavaScript for production
Parser Tool
Learn more about the Oxc parser
AST Concepts
Understanding Abstract Syntax Trees