Meriyah
100% compliant, self-hosted javascript parser with high focus on both performance and stability. Stable and already used in production.
Features
- Conforms to the standard ECMAScript® 2021 (ECMA-262 11th Edition) language specification
- Support TC39 proposals via option
- Support for additional ECMAScript features for Web Browsers
- JSX support via option
- Does not support TypeScript or Flow
- Optionally track syntactic node locations
- Emits an ESTree-compatible abstract syntax tree
- No backtracking
- Low memory usage
- Very well tested (~99 000 unit tests with full code coverage)
- Lightweight - ~90 KB minified
ESNext features
Note: These features need to be enabled with the next
option.
Installation
npm install meriyah --save-dev
API
Meriyah generates AST
according to ESTree AST format, and can be used to perform syntactic analysis (parsing) of a JavaScript program, and with ES2015
and later a JavaScript program can be either a script or a module.
The parse
method exposed by meriyah takes an optional options
object which allows you to specify whether to parse in script
mode (the default) or in module
mode.
This is the available options:
{
module: false;
next: false;
ranges: false;
webcompat: false;
loc: false;
raw: false;
directives: false;
globalReturn: false;
impliedStrict: false;
onComment: []
onInsertedSemicolon: (pos) => {}
onToken: []
preserveParens: false;
lexical: false;
source: false;
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
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
declare function onInsertedSemicolon(position: number): void;
Example usage
import { parseScript } from './meriyah';
parseScript('({x: [y] = 0} = 1)');
This will return when serialized in json:
{
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
}
}
}
]
}