What is meriyah?
Meriyah is a fast and lightweight JavaScript parser that supports the latest ECMAScript standards. It is designed to be highly performant and can be used for various tasks such as syntax analysis, code transformation, and static code analysis.
What are meriyah's main functionalities?
Parsing JavaScript Code
This feature allows you to parse JavaScript code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple JavaScript statement and log the resulting AST.
const meriyah = require('meriyah');
const ast = meriyah.parseScript('const x = 10;');
console.log(ast);
Parsing with Options
Meriyah supports various parsing options such as module parsing and JSX syntax. The code sample shows how to parse a script with these options enabled.
const meriyah = require('meriyah');
const ast = meriyah.parseScript('const x = 10;', { module: true, jsx: true });
console.log(ast);
Error Handling
Meriyah provides error handling capabilities to catch and handle syntax errors during parsing. The code sample demonstrates how to catch a parsing error and log the error message.
const meriyah = require('meriyah');
try {
const ast = meriyah.parseScript('const x = ;');
} catch (e) {
console.error('Parsing error:', e.message);
}
Other packages similar to meriyah
acorn
Acorn is a small, fast, JavaScript-based JavaScript parser. It is known for its modularity and flexibility, allowing users to extend its functionality with plugins. Compared to Meriyah, Acorn is more extensible but may be slightly slower in performance.
esprima
Esprima is a high-performance, standard-compliant ECMAScript parser. It is widely used in various JavaScript tools and frameworks. Esprima is known for its accuracy and reliability, but Meriyah is generally faster and more lightweight.
Meriyah
100% compliant, self-hosted javascript parser with high focus on both performance and stability
Features
- Conforms to the standard ECMAScript® 2020 (ECMA-262 10th Edition) language specification
- Support TC39 proposals via option
- Support for additional ECMAScript features for Web Browsers
- JSX support via option
- Optionally track syntactic node locations
- Emits an ESTree-compatible abstract syntax tree.
- No backtracking
- Reduced memory usage
- Very well tested (~81 000 unit tests with full code coverage)
- Lightweight - ~82 KB minified
ESNext features
Note: These features need to be enabled with the next
option.
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;
preserveParens: false;
lexical: false;
source: false;
identifierPattern: false;
jsx: false
}
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
}
}
}
]
}
ECMAScript compability
Meriyah is 100% ECMA spec compatible, but you have to enable several options to make sure your code parses with 100% ECMA spec compability. This is done because Meriyah's main focus is on performance, and each option you enable will have impact on it's performance.
Also note that support for additional ECMAScript features for Web Browsers (annexB) isn't enabled by default as in other parsers, but you can instead parse with and without web compability .
This is done because AnnexB is an extension of the language, and also beaucse all the Test262 suite
tests has no web compability.
Lexical binding and scope tracking has to be enabled with the lexical
option.