Skip to content

Commit

Permalink
Fix: no-extra-parens invalid autofix in for-of statements (#8337)
Browse files Browse the repository at this point in the history
This commit fixes a bug in the `no-extra-parens` autofixer where if the right side of a `for-of` predicate had extra parens, it would sometimes get combined with the `of` token.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Mar 27, 2017
1 parent 6c819d8 commit 6eda3b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/no-extra-parens.js
Expand Up @@ -318,7 +318,7 @@ module.exports = {
}

// If the parens are preceded by a keyword (e.g. `typeof(0)`), a space should be inserted (`typeof 0`)
const precededByKeyword = tokenBeforeLeftParen.type === "Keyword";
const precededByIdentiferPart = esUtils.code.isIdentifierPartES6(tokenBeforeLeftParen.value.slice(-1).charCodeAt(0));

// However, a space should not be inserted unless the first character of the token is an identifier part
// e.g. `typeof([])` should be fixed to `typeof[]`
Expand All @@ -331,7 +331,7 @@ module.exports = {
const startsWithUnaryPlus = firstToken.type === "Punctuator" && firstToken.value === "+";
const startsWithUnaryMinus = firstToken.type === "Punctuator" && firstToken.value === "-";

return (precededByKeyword && startsWithIdentifierPart) ||
return (precededByIdentiferPart && startsWithIdentifierPart) ||
(precededByUnaryPlus && startsWithUnaryPlus) ||
(precededByUnaryMinus && startsWithUnaryMinus);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -952,6 +952,13 @@ ruleTester.run("no-extra-parens", rule, {
"AssignmentExpression",
1,
{ parserOptions: { ecmaVersion: 2015 } }
),
invalid(
"for (foo of(bar));",
"for (foo of bar);",
"Identifier",
1,
{ parserOptions: { ecmaVersion: 2015 } }
)
]
});

0 comments on commit 6eda3b5

Please sign in to comment.