See https://stackblitz.com/edit/oxc-parser for usage example.
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:
import defer
and import source
.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
.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.
@oxc-project/types can be used. For example:
import { Statement } from '@oxc-project/types';
oxc-walker or estree-walker can be used.
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,
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.
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>; }
import oxc 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 = oxc.parseSync(filename, code); // or `await oxc.parseAsync(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);
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
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4