Skip to content

Commit

Permalink
Update: Fix call, new and member expressions in no-extra-parens (#12302)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Sep 25, 2019
1 parent a7894eb commit 11ae6fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/rules/no-extra-parens.js
Expand Up @@ -205,7 +205,14 @@ module.exports = {
const lastToken = sourceCode.getLastToken(newExpression);
const penultimateToken = sourceCode.getTokenBefore(lastToken);

return newExpression.arguments.length > 0 || astUtils.isOpeningParenToken(penultimateToken) && astUtils.isClosingParenToken(lastToken);
return newExpression.arguments.length > 0 ||
(

// The expression should end with its own parens, e.g., new new foo() is not a new expression with parens
astUtils.isOpeningParenToken(penultimateToken) &&
astUtils.isClosingParenToken(lastToken) &&
newExpression.callee.range[1] < newExpression.range[1]
);
}

/**
Expand Down Expand Up @@ -338,7 +345,7 @@ module.exports = {
function finishReport() {
context.report({
node,
loc: leftParenToken.loc.start,
loc: leftParenToken.loc,
messageId: "unexpected",
fix(fixer) {
const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]);
Expand Down Expand Up @@ -887,6 +894,12 @@ module.exports = {
report(node.object);
}

if (nodeObjHasExcessParens &&
node.object.type === "NewExpression" &&
isNewExpressionWithParens(node.object)) {
report(node.object);
}

if (node.computed && hasExcessParens(node.property)) {
report(node.property);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -95,6 +95,17 @@ ruleTester.run("no-extra-parens", rule, {
"new A()()",
"(new A)()",
"(new (Foo || Bar))()",
"(new new foo())()",
"new (new new foo())(bar)",
"(new foo).bar",
"(new foo)[bar]",
"(new foo).bar.baz",
"(new foo.bar).baz",
"(new foo).bar()",
"(new foo.bar).baz()",
"new (new foo).bar",
"new (new foo.bar).baz",
"(new new foo()).baz",
"(2 + 3) ** 4",
"2 ** (2 + 3)",
"new (import(source))",
Expand Down Expand Up @@ -647,6 +658,11 @@ ruleTester.run("no-extra-parens", rule, {
invalid("(foo()).bar", "foo().bar", "CallExpression"),
invalid("(foo.bar()).baz", "foo.bar().baz", "CallExpression"),
invalid("(foo\n.bar())\n.baz", "foo\n.bar()\n.baz", "CallExpression"),
invalid("(new foo()).bar", "new foo().bar", "NewExpression"),
invalid("(new foo()).bar()", "new foo().bar()", "NewExpression"),
invalid("(new foo(bar)).baz", "new foo(bar).baz", "NewExpression"),
invalid("(new foo.bar()).baz", "new foo.bar().baz", "NewExpression"),
invalid("(new foo.bar()).baz()", "new foo.bar().baz()", "NewExpression"),

invalid("new (A)", "new A", "Identifier"),
invalid("(new A())()", "new A()()", "NewExpression"),
Expand Down

0 comments on commit 11ae6fc

Please sign in to comment.