Skip to content

Commit 6489518

Browse files
Fix: no-extra-parens crash when code is "((let))" (#11444)
This issue was detected by the fuzzer in https://travis-ci.org/eslint/eslint/jobs/498031437.
1 parent 9d20de2 commit 6489518

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/rules/no-extra-parens.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ module.exports = {
471471
const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node) : sourceCode.getFirstToken(node);
472472
const secondToken = sourceCode.getTokenAfter(firstToken, astUtils.isNotOpeningParenToken);
473473
const thirdToken = secondToken ? sourceCode.getTokenAfter(secondToken) : null;
474+
const tokenAfterClosingParens = secondToken ? sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken) : null;
474475

475476
if (
476477
astUtils.isOpeningParenToken(firstToken) &&
@@ -479,7 +480,12 @@ module.exports = {
479480
secondToken.type === "Keyword" && (
480481
secondToken.value === "function" ||
481482
secondToken.value === "class" ||
482-
secondToken.value === "let" && astUtils.isOpeningBracketToken(sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken))
483+
secondToken.value === "let" &&
484+
tokenAfterClosingParens &&
485+
(
486+
astUtils.isOpeningBracketToken(tokenAfterClosingParens) ||
487+
tokenAfterClosingParens.type === "Identifier"
488+
)
483489
) ||
484490
secondToken && secondToken.type === "Identifier" && secondToken.value === "async" && thirdToken && thirdToken.type === "Keyword" && thirdToken.value === "function"
485491
)

tests/lib/rules/no-extra-parens.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ ruleTester.run("no-extra-parens", rule, {
452452
"({}) ? foo() : bar()",
453453
"({}) + foo",
454454
"(function(){}) + foo",
455+
"(let)\nfoo",
455456
"(let[foo]) = 1", // setting the 'foo' property of the 'let' variable to 1
456457
{
457458
code: "((function(){}).foo.bar)();",
@@ -1092,6 +1093,18 @@ ruleTester.run("no-extra-parens", rule, {
10921093
"Identifier",
10931094
1
10941095
),
1095-
invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null)
1096+
invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null),
1097+
invalid(
1098+
"(let)",
1099+
"let",
1100+
"Identifier",
1101+
1
1102+
),
1103+
invalid(
1104+
"((let))",
1105+
"(let)",
1106+
"Identifier",
1107+
1
1108+
)
10961109
]
10971110
});

0 commit comments

Comments
 (0)