Socket
Socket
Sign inDemoInstall

cherow

Package Overview
Dependencies
Maintainers
1
Versions
536
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cherow

Fast, standard-compliant ECMAScript parser written in ECMAScript


Version published
Weekly downloads
11K
decreased by-16.04%
Maintainers
1
Weekly downloads
 
Created
Source

Cherow

Work in progress

ANNOUNCEMENT I have stopped playing around, and gone into a mode where I'm actually finishing this parser.

Everything parses now after the ECMA specifications.

It's now save to use Cherow in development, and it covers the same thing as Esprima and Acorn does. It even parse a lot of cases the mentioned parsers fails on.

In fact. Cherow now parses everything after the ECMAScript specs.

NOTE!! No point to open issue tickets. I know what I'm doing : )

It's safe to use Cherow now, but I'm still not willing to move the code to Master branch until the internals are fixed, and the my remaining TODO are fixed.

Over 4400 unit tests should tell you that Cherow works!

Be aware that the code will change rapidly as I do progress.

Current stage

At current stage Cherow are working 100%, but not optimized. It's tested against both Acorn and Esprima and I'm also parsing huge libraries with this parser - both ES5 and ES6 code.

To parse something, you can do:

 
 implement { parseMOdule, parseScript } from 'cherow';

 // parse in sloppy mode
 parseScript('function foo() { return "bar"; } ');

 // parse inmodule code
 parseModule('function foo() { return "bar"; } ');

 // parse and get a ESTree output like Esprima

 parseScript('function foo() { return "bar"; } ', { raw: true, directives: true });

 // parse and get a ESTree output like Acorn

 parseScript('function foo() { return "bar"; } ', { raw: true, ranges: true });

Roadmap

Mostly get tests to fail where they should fail according to TC39.

  1. Make sure things parses as they should
  2. General cleanup and reduce amount of bitmasks
  3. Reduce code size

Important

JSX are 99% completed and template are missing support for escaped sequences. This is done with purpose. TypeScript handle JSX and templates differently so this will be fixed after the TS parser code have been implemented.

Development process

That is the most complex things I'm doing. I'm using http://astexplorer.net/ to keep track on what fails and doesn't fail in various parsers. I'm also running SpiderMonkey, V8, and testing it against NodejS itself to validate if I'm doing the right things.

Beside that. For every change I do in the code base, I'm parsing around 30 different libraries every time just to validate that I didn't break anything!

On top of that I'm running various benchmarks.

So everyone can make sure that the thing I'm pushing to this repo actually are working :)

Location tracker

As it is now this parser have options you can use to get the same AST output as either Esprima or Acorn. By default Esprima doesnt have any location tracking on the node. Acorn has. For that use ranges: true as the option when you parse.

Column and lines are in progress still.

Note there exist a difference between how Esprima and Acorn calculating the ranges. You can see this in template string with an simple identifier. Acorn set the identifier start value to 2 (after template head). Esprima calculating it as 0.

Due to this differences I'm still thinking how to do this. Example  Espree uses Acorn, but uses Esprima location tracking layout.

However. Column and lines can be activated with ranges: true, but will output wrong values due to the fact this is far from completed.

I may end up adding a Esprima and Acorn mode option due to backward compability.

Error tracking

This is an complex process. Mostly all open source parsers report either wrong location or wrong token position. One example here is Esprima wich is failing on invalid computed property. ({[x]}). In this case Esprima will throw and report the last brace - } - as the wrong token. In fact this has nothing to do with the invalid computed shorthand property.

Cherow are designed from ground up to fix all this things and report errors correctly. In mentioned case, Cherow will report the first bracket - [ - as the wrong token. Wich is the start of the computed property.

Here is an example on how I do it.

It should fail on both function static() { "use strict"; } and "use strict"; function static() {} with correct error location. Note the "use strict"; directive in the functions body.

So the source code for it, will look like this:

  // if allready in strict mode code, thow
  this.error(Errors.UnexpectedStrictReserved);
  // ... else record current location and mark
  // that we found a reserved word
  this.errorLocation = this.trackErrorLocation();
  this.flags |= Flags.ReservedWord;

In the functions body I check if the parser state are in strict mode, and if that's the case, I check for the bitmask and throw the error at the recorded location.

No magic! Just simple coding.

Performance

Once again. Allmost all open source parsers have two or more deopts. Cherow are designed to avoid this. A good example is when you are accessing another object shape for checking values - obj.type === "AssignmentExpression". This can and will cause an deopt.

Performance is one out of many reasons why developing this parser take so long time.

TC39

At this stage 90% TC39 compatible. There exist a few tests that doesn't fail when they should. I'm working on it :)

And there can be bugs in the bleeding edge cases like Stage 3 (use next Option). One example here is object rest spread. A few weeks ago the specs changed again, and I haven't updated it yet

Other

Tokenizing.

It's there, but work in progress. Use tokens: true. This is work in progress. See TODO.

Comment collecting

It's there, and working. It should work just like Acorn's onComment. Just do comments: true

Tolerate mode

I still have to find a valid usecase for it. Once I do, I will add it.

FAQs

Package last updated on 24 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc