Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the Meriyah parser #1105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

prantlf
Copy link

@prantlf prantlf commented May 7, 2022

Meriyah is a fast and modern implementation of ECMAScript parser.

grafik

Making the comment types SingleLine and MultiLine recognised by ast-types would make the parse wrapper shorter and on par with the recognition of other comment types returned by Esprima/Babel. I just converted the comment types, so that the tests succeeded and the first version of this submission was shorter.

// This module is suitable for passing as options.parser when calling
// recast.parse to process JavaScript code with Meriyah:
//
//   const ast = recast.parse(source, {
//     parser: require("recast/parsers/meriyah")
//   });
//
import { getOption } from "../lib/util";

function convertComment(comment: any): any {
  let { type } = comment;
  type = type === "SingleLine" ? "Line" : "Block";
  return { ...comment, type };
}

export function parse(source: string, options?: any) {
  const comments: any[] = [];
  const tokens: any[] = [];
  const ast = require("meriyah").parse(source, {
    module: getOption(options, "sourceType", "module") === "module",
    specDeviation: getOption(options, "tolerant", true),
    jsx: getOption(options, "jsx", false),
    ranges: getOption(options, "range", false),
    loc: true,
    raw: true,
    next: true,
    globalReturn: true,
    onComment: comments,
    onToken: tokens,
  });

  ast.comments = comments.map(convertComment);
  ast.tokens = tokens;

  return ast;
}

Meriyah is a fast and modern implementation of ECMAScript parser
Copy link
Contributor

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat that it's so much faster than Acorn or Esprima. How does the speed compare to Babel?

Small code comments below.

.github/workflows/main.yml Outdated Show resolved Hide resolved
parsers/meriyah.ts Outdated Show resolved Hide resolved
@prantlf
Copy link
Author

prantlf commented May 7, 2022

Neat that it's so much faster than Acorn or Esprima. How does the speed compare to Babel?

Babel is among the slowest, but my benchmark uses only jQuery and Marionette.CollectionView.

I replaced an old esprima in the RequireJS optimizer with meriyah to gain better performance and support of modern language. It works well so far.

Copy link
Contributor

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, thanks. These changes look good to me! The maintainers may have other comments.

@mrienstra
Copy link

Bump -- this looks like a good addition.

In case it's helpful: Meriyah is used by astring & nexe. And indirectly by react-scanner, by way of astray.

@eventualbuddha
Copy link
Collaborator

This isn't something I feel I can decide whether to incorporate. I'll leave it to @benjamn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants