estools / esvalid Goto Github PK
View Code? Open in Web Editor NEWconfirm that a SpiderMonkey format AST represents an ECMAScript program
confirm that a SpiderMonkey format AST represents an ECMAScript program
See #13 for reasoning.
Hi, first thank for your handy module!!
I get a error on the code: a: try { break a; } finally { }
which is legal I believe.
Below is a minimal bug reproduction:
var Esprima = require('esprima');
var Esvalid = require('esvalid');
var code = 'a: try { break a; } finally { }';
var ast = Esprima.parse(code);
var errors = Esvalid.errors(ast);
if (errors.length > 0) { throw errors[0]; }
TL;DR: try { esvalid.isValid(esprima.parse(text)) } catch(e) { true }
should, intuitively, always evaluate to true
. This is about some cases where it doesn't. This is not necessarily a bug report.
I wrote a quick tool to slurp up all the javascript files on my harddrive, run it through esprima and esvalid, select the files that parsed fine but failed the esvalid checks, and automatically minify them. Oh yeah, and unique them by the particular error I get from esvalid. Sorry, I may have had a little too much fun with this. ;)
Here's what I found:
function(){}
is placed where expressions/statements are expected.That last case is the most interesting to me. For example: `for(;;)function f(){}", "switch(n){case 1:function f(){}}", etc. Here, I think either:
What are your thoughts?
You can see the full results here: https://gist.github.com/joshuawarner32/994d2756687551d30d1d
This is them as of right now:
name
member must be a valid IdentifierName"name
member must not be a ReservedWord"alternate
must not be the consequent
of an IfStatement with a non-null alternate
"property
member must have a valid IdentifierName name
member"value
member must have zero parameters"value
member must have exactly one parameter"expressions
member length must be >= 2"cases
member length must be >= 1"cases
member must contain no more than one SwitchCase with a null test
member"handler
member or a non-null finalizer
member"delete
with bare identifier not allowed in strict mode"declarations
member must be non-empty"We'll have to add the errors for #23, #24, and #25 when they are done.
These six (copied from above) can be fixed by changing the AST structure:
value
member must have zero parameters"value
member must have exactly one parameter"expressions
member length must be >= 2"cases
member length must be >= 1"cases
member must contain no more than one SwitchCase with a null test
member"declarations
member must be non-empty"I'm writing a program that only needs to check for validity of an AST subtree, so I'm using esvalid.errors(partOfAst)
and stripping all errors with the message "given AST node should be of type Program". I've pinned esvalid as a specific version, because I'm unsure whether that's part of the public API.
Is it? Might that human-readable message change in the future?
If I've correctly understood, #33 plans to introduce machine-readable identifiers for the specific error types, which might be a better way to solve this.
{
type: "IfStatement",
test: (...),
consequent: {
type: "IfStatement",
test: (...),
consequent: (...),
alternate: null
},
alternate: (...)
}
Just ES5 support for now, no destructuring.
Lines 445 to 446 in a57c8c4
This is not actually a restriction of SpiderMonkey Literal nodes.
For example:
Working on this.
We should be sure to normalise numbers/strings/identifiers in order to compare the keys properly.
In 3 different types for loops in listed in http://es5.github.io/#A.4. This may need to introduce a new state.
Should be "Identifier name
member must be a valid IdentifierName"
Labels need to exist as ancestors of continue
/break
.
Hopefully this should be 1 for most of them or at least we can reduce them to 1.
https://github.com/Constellation/esutils
isReservedWord
(estools/esutils#3)isExpression
, isStatement
, isIterationStatement
, isSourceElement
(estools/esutils#8)isAssignmentOperator
, isBinaryOperator
, isLogicalOperator
, isUnaryOperator
, isUpdateOperator
Bug introduced in a19be8d.
errors.push(new E(node, "SwitchStatement cases
member length must be >= 2"));
It shall be ">= 1".
Apparently esvalidate is already taken (https://www.npmjs.org/package/esvalidate) grumble...
return;
// cannot contain more than one default
switch(0) {
default: 0
default: 0
}
For "get", expect zero parameters. For "set", expect one.
The identifiers "implements", "interface", "let", "package", "private", "protected", "public", "static", and "yield" are classified as FutureReservedWord tokens within strict mode code. (7.6.1.2).
continue;
Apparently esprima is still generating ASTs like this.
{
type: "TryStatement",
block: (...),
handler: null,
guardedHandlers: [],
finalizer: null
}
Test case: https://github.com/michaelficarra/esvalid/blob/master/test/unit.js#L147
Spec: http://es5.github.io/#x12.1
An empty SwitchStatement act as if it is a expression statement with only the discriminant.
LeftHandSideExpressions are:
Literal
Identifier
ArrayExpression
ObjectExpression
FunctionExpression
MemberExpression
CallExpression
... and unfortunately all the "GroupExpression".
Note that only MemberExpression and Identifier can be parsed correctly by esprima.
In ES5, they were restricted in strict mode, but ES6 allows them again. Rationale had to do with dynamic property names.
We could also add an isValidExpression
, but isValidStatement
is a non-starter because it relies on the current label set and whether we're in a function or IterationStatement, which is context we don't have.
Only for nodes types that also exist in JavaScript.
0, // needs to sequence at least 2 expressions
var // needs at least one declarator
switch(0) { } // needs at least one case/default
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.