Giter Club home page Giter Club logo

esvalid's People

Contributors

constellation avatar davidchambers avatar ikarienator avatar michaelficarra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

esvalid's Issues

Error on valid break statement

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]; }

Errors detected in syntax trees parsed with esprima

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:

  • 10 instances of "unrecognized node type", all of which are ES6-specific: ForOfStatement, ClassDeclaration, TemplateLiteral, TaggedTemplateExpression, Super, YieldExpression, ArrowFunctionExpression, ClassExpression, ObjectPattern, SpreadElement
  • 2 instances of ES6 features that aren't explicitly new node types
  • 17 instances of fallout from the unrecognized node types (i.e. "cascading failures")
  • 4 instances of "identical keys" that the esprima isn't catching, but could
  • an instance where esprima doesn't detect a break without a switch, where it could
  • 10 instances where 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:

  • Esprima is really generating invalid trees
  • The examples are really syntax errors, and esprima is being lenient, not catching some syntax errors it could
  • Esvalid is wrong, and that particular syntax tree is perfectly valid.

What are your thoughts?


You can see the full results here: https://gist.github.com/joshuawarner32/994d2756687551d30d1d

list errors that are unrelated to type checking in the README

This is them as of right now:

  • "BreakStatement must have an IterationStatement or SwitchStatement as an ancestor"
  • "labelled BreakStatement must have a matching LabeledStatement ancestor"
  • "ContinueStatement must have an IterationStatement as an ancestor"
  • "labelled ContinueStatement must have a matching LabeledStatement ancestor"
  • "Identifier name member must be a valid IdentifierName"
  • "Identifier name member must not be a ReservedWord"
  • "IfStatement with null alternate must not be the consequent of an IfStatement with a non-null alternate"
  • "LabeledStatement must not be nested within a LabeledStatement with the same label"
  • "numeric Literal nodes must not be NaN"
  • "numeric Literal nodes must not be negative"
  • "numeric Literal nodes must be finite"
  • "static MemberExpression property member must have a valid IdentifierName name member"
  • "ObjectExpression getter property value member must have zero parameters"
  • "ObjectExpression setter property value member must have exactly one parameter"
  • "duplicate data property in object literal not allowed in strict mode"
  • "ReturnStatement must be nested within a FunctionExpression or FunctionDeclaration node"
  • "SequenceExpression expressions member length must be >= 2"
  • "SwitchStatement cases member length must be >= 1"
  • "SwitchStatement cases member must contain no more than one SwitchCase with a null test member"
  • "TryStatement must have a non-null handler member or a non-null finalizer member"
  • "delete with bare identifier not allowed in strict mode"
  • "VariableDeclaration 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:

  • "ObjectExpression getter property value member must have zero parameters"
  • "ObjectExpression setter property value member must have exactly one parameter"
  • "SequenceExpression expressions member length must be >= 2"
  • "SwitchStatement cases member length must be >= 1"
  • "SwitchStatement cases member must contain no more than one SwitchCase with a null test member"
  • "VariableDeclaration declarations member must be non-empty"

Are the human-readable messages constant as part of the public API?

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.

Categorize reported errors.

For example:

  • Structural Context (SC)
  • Duplication Not Allowed (DNA)
  • Restricted Names (RN)
  • Incorrect Type (IT)
  • Nullability (N)

require input to isValid to be a Program

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.