Skip to content

Commit

Permalink
Fix: no-extra-parens crash when code is "((let))" (#11444)
Browse files Browse the repository at this point in the history
This issue was detected by the fuzzer in https://travis-ci.org/eslint/eslint/jobs/498031437.
  • Loading branch information
not-an-aardvark authored Feb 28, 2019
1 parent 9d20de2 commit 6489518
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ module.exports = {
const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node) : sourceCode.getFirstToken(node);
const secondToken = sourceCode.getTokenAfter(firstToken, astUtils.isNotOpeningParenToken);
const thirdToken = secondToken ? sourceCode.getTokenAfter(secondToken) : null;
const tokenAfterClosingParens = secondToken ? sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken) : null;

if (
astUtils.isOpeningParenToken(firstToken) &&
Expand All @@ -479,7 +480,12 @@ module.exports = {
secondToken.type === "Keyword" && (
secondToken.value === "function" ||
secondToken.value === "class" ||
secondToken.value === "let" && astUtils.isOpeningBracketToken(sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken))
secondToken.value === "let" &&
tokenAfterClosingParens &&
(
astUtils.isOpeningBracketToken(tokenAfterClosingParens) ||
tokenAfterClosingParens.type === "Identifier"
)
) ||
secondToken && secondToken.type === "Identifier" && secondToken.value === "async" && thirdToken && thirdToken.type === "Keyword" && thirdToken.value === "function"
)
Expand Down
15 changes: 14 additions & 1 deletion tests/lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ ruleTester.run("no-extra-parens", rule, {
"({}) ? foo() : bar()",
"({}) + foo",
"(function(){}) + foo",
"(let)\nfoo",
"(let[foo]) = 1", // setting the 'foo' property of the 'let' variable to 1
{
code: "((function(){}).foo.bar)();",
Expand Down Expand Up @@ -1092,6 +1093,18 @@ ruleTester.run("no-extra-parens", rule, {
"Identifier",
1
),
invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null)
invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null),
invalid(
"(let)",
"let",
"Identifier",
1
),
invalid(
"((let))",
"(let)",
"Identifier",
1
)
]
});

0 comments on commit 6489518

Please sign in to comment.