Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
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.
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 });
Mostly get tests to fail where they should fail according to TC39.
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.
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 :)
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.
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.
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.
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
It's there, but work in progress. Use tokens: true
. This is work in progress. See TODO.
It's there, and working. It should work just like Acorn's onComment
. Just do comments: true
I still have to find a valid usecase for it. Once I do, I will add it.
FAQs
Fast and lightweight, standard-compliant javascript parser written in ECMAScript
The npm package cherow receives a total of 9,429 weekly downloads. As such, cherow popularity was classified as popular.
We found that cherow demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.