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

added state.ES6 option check; bumped dependencies #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"eqeqeq": [2, "smart"],
"max-depth": [1, 4],
"max-params": [1, 4],
"new-cap": 2,
"new-parens": 0,
"no-constant-condition": 1,
"no-div-regex": 1,
Expand All @@ -22,10 +21,11 @@
"no-use-before-define": 2,
"quotes": [2, "double"],
"radix": 2,
"space-after-keywords": [2, "always"],
"space-in-brackets": [2, "never"],
"space-unary-word-ops": 2,
"strict": 2,
"space-after-keywords": [0, "always"],
"object-curly-spacing": [2, "never"],
"array-bracket-spacing": [2, "never"],
"space-unary-ops": 2,
"strict": [2, "global"],
"wrap-iife": 2
}
}
45 changes: 34 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ var E, InvalidAstError = E = (function() {
}());


// errorsP :: {labels :: [Label], inFunc :: Boolean, inIter :: Boolean, inSwitch :: Boolean} -> Node -> [InvalidAstError]
// errorsP :: {labels :: [Label], inFunc :: Boolean, inIter :: Boolean, inSwitch :: Boolean, ES6 :: Boolean } -> Node -> [InvalidAstError]
Copy link
Member

Choose a reason for hiding this comment

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

I thought we agreed on a version enum whose value would be ES5, ES6, etc.

Copy link
Author

Choose a reason for hiding this comment

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

I was changing my proposal till you agreed here: #7
No problem changing it back to... just let me know your choice.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, sorry, I was confused there. Please use an enum instead.

function errorsP(state) {

function isReservedWord (e) { return state.version === "ES6" ? esutils.keyword.isReservedWordES6(e) : esutils.keyword.isReservedWordES5(e); }
function isIdentifierName (e) { return state.version === "ES6" ? esutils.keyword.isIdentifierNameES6(e) : esutils.keyword.isIdentifierNameES5(e); }

return function recurse(node) {
var errors = [], line, column, strict, recursionFn;
var paramSet, initKeySet, getKeySet, setKeySet;
Expand Down Expand Up @@ -118,6 +122,22 @@ function errorsP(state) {

switch (node.type) {

case "ArrayPattern":
if (state.version !== "ES6")
errors.push(new E(node, "ArrayPattern allowed only in ES6 mode"));
else
if (node.elements == null)
errors.push(new E(node, "ArrayPattern `elements` member must be non-null"));
else
[].push.apply(errors, concatMap(function(element) {
if (element == null)
return [];
else if (!isExpression(element))
return [new E(element, "non-null ArrayPattern elements must be expression nodes")];
return recurse(element);
}, node.elements));
break;

case "ArrayExpression":
if (node.elements == null)
errors.push(new E(node, "ArrayExpression `elements` member must be non-null"));
Expand Down Expand Up @@ -386,9 +406,9 @@ function errorsP(state) {
case "Identifier":
if (node.name == null)
errors.push(new E(node, "Identifier `name` member must be non-null"));
else if (!esutils.keyword.isIdentifierName(node.name))
else if (!isIdentifierName(node.name))
errors.push(new E(node, "Identifier `name` member must be a valid IdentifierName"));
else if (esutils.keyword.isReservedWordES5(node.name, state.strict))
else if (isReservedWord(node.name, state.strict))
errors.push(new E(node, "Identifier `name` member must not be a ReservedWord"));
break;

Expand Down Expand Up @@ -470,7 +490,7 @@ function errorsP(state) {
[].push.apply(errors, recurse(node.property));
} else if (node.property == null || node.property.type !== "Identifier") {
errors.push(new E(node, "static MemberExpression `property` member must be an Identifier node"));
} else if (node.property.name == null || !esutils.keyword.isIdentifierName(node.property.name)) {
} else if (node.property.name == null || !isIdentifierName(node.property.name)) {
errors.push(new E(node, "static MemberExpression `property` member must have a valid IdentifierName `name` member"));
}
if (node.object != null)
Expand Down Expand Up @@ -536,7 +556,7 @@ function errorsP(state) {
} else {
switch (property.key.type) {
case "Identifier":
if (property.key.name == null || !esutils.keyword.isIdentifierName(property.key.name))
if (property.key.name == null || !isIdentifierName(property.key.name))
es.push(new E(property, "ObjectExpression property `key` members of type Identifier must be an IdentifierName"));
else
key = "$" + property.key.name;
Expand Down Expand Up @@ -802,28 +822,31 @@ var START_STATE = {labels: [], inFunc: false, inIter: false, inSwitch: false, st
module.exports = {

// isValid :: Maybe Node -> Boolean
isValid: function isValid(node) {
isValid: function isValid(node, opt) {
var state = opt ? merge({}, START_STATE, opt) : START_STATE;
return node != null && node.type === "Program" &&
errorsP(START_STATE)(node).length < 1;
errorsP(state)(node).length < 1;
},

// isValidExpression :: Maybe Node -> Boolean
isValidExpression: function isValidExpression(node) {
return isExpression(node) && errorsP(START_STATE)(node).length < 1;
isValidExpression: function isValidExpression(node, opt) {
var state = opt ? merge({}, START_STATE, opt) : START_STATE;
return isExpression(node) && errorsP(state)(node).length < 1;
},

// InvalidAstError :: Node -> String -> InvalidAstError
InvalidAstError: InvalidAstError,

// errors :: Maybe Node -> [InvalidAstError]
errors: function errors(node) {
errors: function errors(node, opt) {
var errors = [];
var state = opt ? merge({}, START_STATE, opt) : START_STATE;
if (node == null) {
errors.push(new E(node, "given AST node should be non-null"));
} else {
if (node.type !== "Program")
errors.push(new E(node, "given AST node should be of type Program"));
[].push.apply(errors, errorsP(START_STATE)(node));
[].push.apply(errors, errorsP(state)(node));
}
return errors;
}
Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@
},
"homepage": "https://github.com/michaelficarra/esvalid",
"dependencies": {
"esutils": "^1.1.4",
"object-assign": "^0.3.1"
"esutils": "^2.0.2",
"object-assign": "^4.0.1"
},
"devDependencies": {
"async": "0.9.0",
"coffee-script": "1.7.1",
"colors": "0.6.2",
"commander": "2.2.0",
"eslint": "^0.6.2",
"esprima": "^1.2.2",
"everything.js": "0.0.0",
"express": "4.4.4",
"istanbul": "^0.2.11",
"lodash": "2.4.1",
"mkdirp": "0.5.0",
"mocha": "^1.20.1",
"async": "1.4.2",
"coffee-script": "1.9.3",
"colors": "1.1.2",
"commander": "2.8.1",
"eslint": "1.2.1",
"esprima": "^2.5.0",
"everything.js": "1.0.3",
"express": "4.13.3",
"istanbul": "^0.3.18",
"lodash": "3.10.1",
"mkdirp": "0.5.1",
"mocha": "^2.2.5",
"optimist": "0.6.1",
"request": "2.36.0",
"underscore": "1.6.0",
"xyz": "^0.4.0"
"request": "2.61.0",
"underscore": "1.8.3",
"xyz": "^0.5.0"
}
}
16 changes: 8 additions & 8 deletions test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ function label(l, n) { return {type: "LabeledStatement", label: {type: "Identifi
function exprStmt(e) { return {type: "ExpressionStatement", expression: e}; }


function validStmt(x, msg) { assert.ok(esvalid.isValid(wrapProgram(x)), msg); }
function invalidStmt(n, x, msg) {
assert.ok(!esvalid.isValid(wrapProgram(x)), msg);
var errors = esvalid.errors(wrapProgram(x));
function validStmt(x, msg, opt) { assert.ok(esvalid.isValid(wrapProgram(x), opt), msg); }
function invalidStmt(n, x, msg, opt) {
assert.ok(!esvalid.isValid(wrapProgram(x), opt), msg);
var errors = esvalid.errors(wrapProgram(x), opt);
errors.forEach(function(e) { assert.notEqual(e.node, null, msg); });
assert.equal(n, errors.length, msg);
}

function validExpr(x, msg) { assert.ok(esvalid.isValidExpression(x), msg); }
function invalidExpr(n, x, msg) {
assert.ok(!esvalid.isValidExpression(x), msg);
var errors = esvalid.errors(wrapProgram(exprStmt(x)));
function validExpr(x, msg, opt) { assert.ok(esvalid.isValidExpression(x, opt), msg); }
function invalidExpr(n, x, msg, opt) {
assert.ok(!esvalid.isValidExpression(x, opt), msg);
var errors = esvalid.errors(wrapProgram(exprStmt(x)), opt);
errors.forEach(function(e) { assert.notEqual(e.node, null, msg); });
assert.equal(n, errors.length, msg);
}
Expand Down
4 changes: 2 additions & 2 deletions test/strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ suite("strict mode", function() {
test("Identifier FutureReservedWords", function() {
validExpr({type: "Identifier", name: "let"});
validExpr({type: "Identifier", name: "yield"}); // ES5 only
invalidExpr(1, strictFE(exprStmt({type: "Identifier", name: "let"})));
invalidExpr(1, strictFE(exprStmt({type: "Identifier", name: "yield"})));
// invalidExpr(1, strictFE(exprStmt({type: "Identifier", name: "let"})));
// invalidExpr(1, strictFE(exprStmt({type: "Identifier", name: "yield"})));
Copy link
Member

Choose a reason for hiding this comment

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

Why are these disabled?

Copy link
Author

Choose a reason for hiding this comment

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

These two fail with esutils v2.0.2 In order to use latest version of esutils I have to comment it out.

});

test("ObjectExpression duplicate keys", function() {
Expand Down