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

Fix: no-extra-parens false positives for variables called "let" #8808

Merged
merged 1 commit into from Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 21 additions & 10 deletions lib/rules/no-extra-parens.js
Expand Up @@ -426,7 +426,7 @@ module.exports = {
secondToken.type === "Keyword" && (
secondToken.value === "function" ||
secondToken.value === "class" ||
secondToken.value === "let" && astUtils.isOpeningBracketToken(sourceCode.getTokenAfter(secondToken))
secondToken.value === "let" && astUtils.isOpeningBracketToken(sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken))
)
)
) {
Expand Down Expand Up @@ -512,16 +512,27 @@ module.exports = {
ExportDefaultDeclaration: node => checkExpressionOrExportStatement(node.declaration),
ExpressionStatement: node => checkExpressionOrExportStatement(node.expression),

ForInStatement(node) {
if (hasExcessParens(node.right)) {
report(node.right);
}
if (hasExcessParens(node.left)) {
report(node.left);
}
},
"ForInStatement, ForOfStatement"(node) {
if (node.left.type !== "VariableDeclarator") {
const firstLeftToken = sourceCode.getFirstToken(node.left, astUtils.isNotOpeningParenToken);

if (
firstLeftToken.value === "let" && (

// If `let` is the only thing on the left side of the loop, it's the loop variable: `for ((let) of foo);`
// Removing it will cause a syntax error, because it will be parsed as the start of a VariableDeclarator.
firstLeftToken.range[1] === node.left.range[1] ||

ForOfStatement(node) {
// If `let` is followed by a `[` token, it's a property access on the `let` value: `for ((let[foo]) of bar);`
// Removing it will cause the property access to be parsed as a destructuring declaration of `foo` instead.
astUtils.isOpeningBracketToken(
sourceCode.getTokenAfter(firstLeftToken, astUtils.isNotClosingParenToken)
)
)
) {
tokensToIgnore.add(firstLeftToken);
}
}
if (hasExcessParens(node.right)) {
report(node.right);
}
Expand Down
25 changes: 24 additions & 1 deletion tests/lib/rules/no-extra-parens.js
Expand Up @@ -450,7 +450,12 @@ ruleTester.run("no-extra-parens", rule, {
{
code: "((function(){}).foo)();",
options: ["functions"]
}
},
"(let)[foo]",
"for ((let) in foo);",
"for ((let[foo]) in bar);",
"for ((let)[foo] in bar);",
"for ((let[foo].bar) in baz);"
],

invalid: [
Expand Down Expand Up @@ -1052,6 +1057,24 @@ ruleTester.run("no-extra-parens", rule, {
"MemberExpression",
1,
{ parserOptions: { ecmaVersion: 2015 } }
),
invalid(
"(let).foo",
"let.foo",
"Identifier",
1
),
invalid(
"for ((let.foo) in bar);",
"for (let.foo in bar);",
"MemberExpression",
1
),
invalid(
"for ((let).foo.bar in baz);",
"for (let.foo.bar in baz);",
"Identifier",
1
)
]
});