oxc-parser
TypeScript icon, indicating that this package has built-in type declarations

0.61.2 • Public • Published

Oxc Parser

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.

It is fully aligned with Acorn's AST, and any deviation would be considered a bug.

The returned TypeScript AST will conform to @typescript-eslint/typescript-estree in the near future.

If you need all ASTs in the same with-TS-properties format, use the astType: 'ts' option.

AST Types

@oxc-project/types can be used. For example:

import { Statement } from '@oxc-project/types';

Visitor

oxc-walker or estree-walker can be used.

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

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);

Readme

Keywords

Package Sidebar

Install

npm i oxc-parser

Homepage

oxc.rs

Weekly Downloads

272,928

Version

0.61.2

License

MIT

Unpacked Size

342 kB

Total Files

11

Last publish

Collaborators

  • boshen