Skip to content

Commit

Permalink
Fix: check token before using in no-cond-assign (fixes #11611) (#11619)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and not-an-aardvark committed Apr 30, 2019
1 parent 7f290a9 commit b6d41cb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-cond-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {
nextToken = sourceCode.getTokenAfter(node, 1);

return astUtils.isParenthesised(sourceCode, node) &&
astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
previousToken && astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/no-cond-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ruleTester.run("no-cond-assign", rule, {
{ code: "while ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] },
{ code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] },
{ code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] },
{ code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] }
{ code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] },
{ code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] }
]
});

0 comments on commit b6d41cb

Please sign in to comment.