Meriyah JavaScript Parser

7.1.0 · active · verified Tue Apr 21

Meriyah is a highly performant and stable JavaScript parser, currently at version 7.1.0, designed for 100% compliance with the ECMAScript® 2024 (ES-262 15th Edition) standard. It provides an ESTree-compatible Abstract Syntax Tree and operates without backtracking, contributing to its low memory footprint. While it actively supports Annex B (Web Browsers) features, JSX parsing, and select TC39 Stage 3 proposals like Decorators and JSON Modules via an opt-in `next` flag, it notably does *not* support TypeScript or Flow syntax. The project maintains a steady release cadence, frequently updating to reflect the latest ECMAScript specifications and address bug fixes, as evidenced by recent v6 and v7 patch and minor releases, ensuring it remains a cutting-edge tool for syntactic analysis.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a complex JavaScript module with various features, including ESNext proposals, web compatibility, location tracking, and error handling.

import { parse, isParseError } from 'meriyah';
import type { Program } from 'estree'; // Assuming ESTree types are needed for the result

const javascriptCode = `
  // This is a complex JavaScript module demonstrating various features.
  import { someUtil } from './utils.js';

  @debounce(300)
  class MyClass {
    #privateField = 42; // Private class field (ES2022)

    constructor(name) {
      this.name = name;
    }

    // A method with decorators (TC39 Stage 3, requires 'next' option)
    @log
    greet(param) {
      console.log(`Hello, ${this.name}! ${param}`);
      return someUtil(this.#privateField);
    }
  }

  export const instance = new MyClass('Meriyah User');

  // Example of Annex B feature (non-strict mode)
  // function foo() { 'use strict'; with (obj) {} } // This would error in strict mode
  
  // A regular expression with newer features (requires Node.js >= 24 for full validation)
  const regex = /(?<word>\w+)/d; 
  if ("test".match(regex)) {
    console.log("Matched!");
  }
`;

try {
  const ast: Program = parse(javascriptCode, {
    sourceType: 'module', // Parse as an ES module
    ranges: true,        // Include start/end offsets for each node
    loc: true,           // Include line/column location information
    next: true,          // Enable TC39 Stage 3 proposals (e.g., Decorators)
    jsx: false,          // Explicitly disable JSX (if not needed)
    webcompat: true,     // Enable web compatibility (Annex B)
    validateRegex: true, // Validate regular expressions with the current JS runtime
  });

  console.log('Successfully parsed AST:');
  // console.log(JSON.stringify(ast, null, 2)); // Uncomment to see the full AST
  console.log(`Program type: ${ast.type}`);
  console.log(`First statement type: ${ast.body[0].type}`);

} catch (e: any) {
  if (isParseError(e)) {
    console.error(`Meriyah Parse Error: ${e.message} at line ${e.loc?.line}, column ${e.loc?.column}`);
  } else {
    console.error('An unexpected error occurred:', e.message);
  }
}

view raw JSON →