Skip to content

Commit

Permalink
Test for punctuator tokens in lookupBlockType. Fixes jshintgh-1856
Browse files Browse the repository at this point in the history
  • Loading branch information
caitp committed Sep 23, 2014
1 parent 17d23c2 commit ddd02bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4412,23 +4412,23 @@ var JSHINT = (function () {
var i = -1;
var bracketStack = 0;
var ret = {};
if (_.contains(["[", "{"], state.tokens.curr.value))
if (checkPunctuators(state.tokens.curr, ["[", "{"]))
bracketStack += 1;
do {
pn = (i === -1) ? state.tokens.next : peek(i);
pn1 = peek(i + 1);
i = i + 1;
if (_.contains(["[", "{"], pn.value)) {
if (checkPunctuators(pn, ["[", "{"])) {
bracketStack += 1;
} else if (_.contains(["]", "}"], pn.value)) {
} else if (checkPunctuators(pn, ["]", "}"])) {
bracketStack -= 1;
}
if (pn.identifier && pn.value === "for" && bracketStack === 1) {
ret.isCompArray = true;
ret.notJson = true;
break;
}
if (_.contains(["}", "]"], pn.value) && bracketStack === 0) {
if (checkPunctuators(pn, ["}", "]"]) && bracketStack === 0) {
if (pn1.value === "=") {
ret.isDestAssign = true;
ret.notJson = true;
Expand All @@ -4446,6 +4446,11 @@ var JSHINT = (function () {
return ret;
};

// Test whether a given token is a punctuator matching one of the specified values
function checkPunctuators(token, values) {
return token.type === "(punctuator)" && _.contains(values, token.value);
}

// Check whether this function has been reached for a destructuring assign with undeclared values
function destructuringAssignOrJsonValue() {
// lookup for the assignment (esnext only)
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,25 @@ exports["test: array comprehension"] = function (test) {
test.done();
};

exports["gh-1856 mistakenly identified as array comprehension"] = function (test) {
var code = [
"function main(value) {",
" var result = ['{'],",
" key;",
" for (key in value) {",
" result.push(key);",
" }",
" return result;",
"}",
"main({abc:true});"
];

TestRun(test)
.test(code);

test.done();
};

exports["test: moz-style array comprehension"] = function (test) {
// example taken from https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7
var code = [
Expand Down

0 comments on commit ddd02bb

Please sign in to comment.