Skip to content

Commit

Permalink
Fix: no-extra-parens false positive with objects following arrows (#8339
Browse files Browse the repository at this point in the history
)

Previously, the no-extra-parens rule correctly did not report an error when an arrow function returned a parenthesized object expression. However, it would still report an error if an arrow function had a parenthesized object curly as its first token. This commit updates the rule to not report a paren between an arrow token and an opening curly token.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Mar 28, 2017
1 parent 3146167 commit 41b9786
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/rules/no-extra-parens.js
Expand Up @@ -345,6 +345,11 @@ module.exports = {
function report(node) {
const leftParenToken = sourceCode.getTokenBefore(node);
const rightParenToken = sourceCode.getTokenAfter(node);
const tokenBeforeLeftParen = sourceCode.getTokenBefore(leftParenToken);

if (tokenBeforeLeftParen && astUtils.isArrowToken(tokenBeforeLeftParen) && astUtils.isOpeningBraceToken(sourceCode.getFirstToken(node))) {
return;
}

context.report({
node,
Expand Down Expand Up @@ -481,7 +486,7 @@ module.exports = {
}

if (node.body.type !== "BlockStatement") {
if (sourceCode.getFirstToken(node.body).value !== "{" && hasExcessParens(node.body) && precedence(node.body) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
if (hasExcessParens(node.body) && precedence(node.body) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
report(node.body);
return;
}
Expand Down
30 changes: 29 additions & 1 deletion tests/lib/rules/no-extra-parens.js
Expand Up @@ -410,6 +410,22 @@ ruleTester.run("no-extra-parens", rule, {
{
code: "const A = class extends (B=C) {}",
parserOptions: { ecmaVersion: 2015 }
},
{
code: "() => ({ foo: 1 })",
parserOptions: { ecmaVersion: 2015 }
},
{
code: "() => ({ foo: 1 }).foo",
parserOptions: { ecmaVersion: 2015 }
},
{
code: "() => ({ foo: 1 }.foo().bar).baz.qux()",
parserOptions: { ecmaVersion: 2015 }
},
{
code: "() => ({ foo: 1 }.foo().bar + baz)",
parserOptions: { ecmaVersion: 2015 }
}
],

Expand Down Expand Up @@ -959,6 +975,18 @@ ruleTester.run("no-extra-parens", rule, {
"Identifier",
1,
{ parserOptions: { ecmaVersion: 2015 } }
)
),
{
code: "() => (({ foo: 1 }).foo)",
output: "() => ({ foo: 1 }).foo",
parserOptions: { ecmaVersion: 2015 },
errors: [

// 2 errors are reported, but fixing one gets rid of the other
{ message: "Gratuitous parentheses around expression.", type: "MemberExpression" },
{ message: "Gratuitous parentheses around expression.", type: "ObjectExpression" }
]

}
]
});

0 comments on commit 41b9786

Please sign in to comment.