Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meriyah/meriyah
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
https://github.com/meriyah/meriyah
acorn ast ecmascript esnext estree javascript jsx parser parsing performance stability tc39 typescript
Last synced: 7 days ago
JSON representation
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
- Host: GitHub
- URL: https://github.com/meriyah/meriyah
- Owner: meriyah
- License: isc
- Created: 2019-05-07T05:32:23.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T23:49:43.000Z (3 months ago)
- Last Synced: 2024-10-29T15:20:44.880Z (3 months ago)
- Topics: acorn, ast, ecmascript, esnext, estree, javascript, jsx, parser, parsing, performance, stability, tc39, typescript
- Language: TypeScript
- Homepage:
- Size: 9.64 MB
- Stars: 1,040
- Watchers: 22
- Forks: 47
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-list - meriyah - hosted javascript parser - https://meriyah.github.io/meriyah | meriyah | 748 | (TypeScript)
README
Meriyah
100% compliant, self-hosted javascript parser with high focus on both performance and stability. Stable and already used in production.
[Interactive Playground](https://meriyah.github.io/meriyah)
[Benchmark](https://meriyah.github.io/meriyah/performance)## Features
- Conforms to the standard ECMAScript® 2024 (ECMA-262 15th Edition) language specification
- Except RegExp duplicate named groups (See [RegExp support](#regexp-support))
- Support some TC39 stage 3 proposals via option "next"
- Support for additional ECMAScript features for Web Browsers (Annex B)
- JSX support via option "jsx"
- Does **NOT** support TypeScript or Flow syntax
- Track syntactic node locations with option "ranges" or "loc"
- Emits an ESTree-compatible abstract syntax tree
- No backtracking
- Low memory usage## ESNext Stage 3 features
### Supported stage 3 features:
These features need to be enabled with the `next` option.
- [Decorators](https://github.com/tc39/proposal-decorators)
- [Import Attributes](https://github.com/tc39/proposal-import-attributes)
- [JSON Modules](https://github.com/tc39/proposal-json-modules)### Not yet supported stage 3 features:
- [Explicit resource management](https://github.com/tc39/proposal-explicit-resource-management)
- [Source phase import](https://github.com/tc39/proposal-source-phase-imports)
- [RegExp modifiers](https://github.com/tc39/proposal-regexp-modifiers) (See [RegExp support](#regexp-support))## RegExp support
Meriyah doesn't parse RegExp internal syntax, ESTree spec didn't require internal structure of RegExp. Meriyah
does use JavaScript runtime to validate the RegExp literal. That means Meriyah's RegExp support is only as good
as JavaScript runtime's RegExp support.As of Auguest 2024, some latest RegExp features are not supported due to missing implementation in general
JavaScript runtime.- [RegExp modifiers](https://github.com/tc39/proposal-regexp-modifiers) (stage 3) is not supported
- [RegExp duplicate named groups](https://github.com/tc39/proposal-duplicate-named-capturing-groups) is not supportedIn addition, RegExp v flag (unicodeSets) only works on Nodejs v20+ and latest browsers.
## Installation
```sh
npm install meriyah --save-dev
```## API
Meriyah generates `AST` according to [ESTree AST format](https://github.com/estree/estree), and can be used to perform [syntactic analysis](https://en.wikipedia.org/wiki/Parsing) (parsing) of a JavaScript program, and with `ES2015` and later a JavaScript program can be either [a script or a module](https://tc39.github.io/ecma262/index.html#sec-ecmascript-language-scripts-and-modules).
The `parse` method exposed by meriyah takes an optional `options` object which allows you to specify whether to parse in [`script`](https://tc39.github.io/ecma262/#sec-parse-script) mode (the default) or in [`module`](https://tc39.github.io/ecma262/#sec-parsemodule) mode.
```js
// There are also "parseScript" and "parseModule" exported.
import { parse } from 'meriyah';
const result = parse('let some = "code";', { ranges: true });
```The available options:
```js
{
// The flag to allow module code
module: false;// The flag to enable stage 3 support (ESNext)
next: false;// The flag to enable start, end offsets and range: [start, end] to each node
ranges: false;// Enable web compatibility
webcompat: false;// The flag to enable line/column location information to each node
loc: false;// The flag to attach raw property to each literal and identifier node
raw: false;// The flag to allow return in the global scope
globalReturn: false;// The flag to enable implied strict mode
impliedStrict: false;// Allows comment extraction. Accepts either a function or array
onComment: [];// Allows detection of automatic semicolon insertion. Accepts a callback function that will be passed the charater offset where the semicolon was inserted
onInsertedSemicolon: (pos) => {};// Allows token extraction. Accepts either a function or array
onToken: [];// Enable non-standard parenthesized expression node
preserveParens: false;// Enable lexical binding and scope tracking
lexical: false;// Adds a source attribute in every node’s loc object when the locations option is `true`
source: undefined; // Set to source: 'source-file.js'// Enable React JSX parsing
jsx: false;
}
```### onComment and onToken
If an array is supplied, comments/tokens will be pushed to the array, the item in the array contains `start/end/range` information when ranges flag is true, it will also contain `loc` information when loc flag is true.
If a function callback is supplied, the signature must be
```ts
declare function onComment(type: string, value: string, start: number, end: number, loc: SourceLocation): void;declare function onToken(token: string, start: number, end: number, loc: SourceLocation): void;
```Note the `start/end/loc` information are provided to the function callback regardless of the settings on ranges and loc flags. onComment callback has one extra argument `value: string` for the body string of the comment.
### onInsertedSemicolon
If a function callback is supplied, the signature must be
```ts
declare function onInsertedSemicolon(position: number): void;
```## Example usage
```js
import { parseScript } from './meriyah';parseScript('({x: [y] = 0} = 1)');
```This will return when serialized in json:
```js
{
type: "Program",
sourceType: "script",
body: [
{
type: "ExpressionStatement",
expression: {
type: "AssignmentExpression",
left: {
type: "ObjectPattern",
properties: [
{
type: "Property",
key: {
type: "Identifier",
name: "x"
},
value: {
type: "AssignmentPattern",
left: {
type: "ArrayPattern",
elements: [
{
"type": "Identifier",
"name": "y"
}
]
},
right: {
type: "Literal",
value: 0
}
},
kind: "init",
computed: false,
method: false,
shorthand: false
}
]
},
operator: "=",
right: {
type: "Literal",
value: 1
}
}
}
]
}
```