Skip to content
Permalink
Browse files
Update: fix false negative of no-extra-parens with NewExpression (#13930
)
  • Loading branch information
mdjermanovic committed Dec 18, 2020
1 parent f85b4c7 commit a62ad6f03151358b93b5fede022a30d67310705c
Showing with 31 additions and 11 deletions.
  1. +25 −11 lib/rules/no-extra-parens.js
  2. +6 −0 tests/lib/rules/no-extra-parens.js
@@ -472,20 +472,34 @@ module.exports = {
const callee = node.callee;

if (hasExcessParensWithPrecedence(callee, precedence(node))) {
const hasNewParensException = callee.type === "NewExpression" && !isNewExpressionWithParens(callee);

if (
hasDoubleExcessParens(callee) ||
!isIIFE(node) &&
!hasNewParensException &&
!(

// Allow extra parens around a new expression if they are intervening parentheses.
node.type === "NewExpression" &&
callee.type === "MemberExpression" &&
doesMemberExpressionContainCallExpression(callee)
) &&
!(!node.optional && callee.type === "ChainExpression")
isIIFE(node) ||

// (new A)(); new (new A)();
(
callee.type === "NewExpression" &&
!isNewExpressionWithParens(callee) &&
!(
node.type === "NewExpression" &&
!isNewExpressionWithParens(node)
)
) ||

// new (a().b)(); new (a.b().c);
(
node.type === "NewExpression" &&
callee.type === "MemberExpression" &&
doesMemberExpressionContainCallExpression(callee)
) ||

// (a?.b)(); (a?.())();
(
!node.optional &&
callee.type === "ChainExpression"
)
)
) {
report(node.callee);
}
@@ -96,6 +96,8 @@ ruleTester.run("no-extra-parens", rule, {
"(new A)()",
"(new (Foo || Bar))()",
"(new new foo())()",
"new (new A)()",
"new (new a.b)()",
"new (new new foo())(bar)",
"(new foo).bar",
"(new foo)[bar]",
@@ -847,6 +849,10 @@ ruleTester.run("no-extra-parens", rule, {
invalid("new ((a().b().d))", "new (a().b().d)", "MemberExpression"),
invalid("new ((a())).b.d", "new (a()).b.d", "CallExpression"),
invalid("new (a.b).d;", "new a.b.d;", "MemberExpression"),
invalid("new (new A())();", "new new A()();", "NewExpression"),
invalid("new (new A());", "new new A();", "NewExpression"),
invalid("new (new A);", "new new A;", "NewExpression"),
invalid("new (new a.b);", "new new a.b;", "NewExpression"),
invalid("(a().b).d;", "a().b.d;", "MemberExpression"),
invalid("(a.b()).d;", "a.b().d;", "CallExpression"),
invalid("(a.b).d;", "a.b.d;", "MemberExpression"),

0 comments on commit a62ad6f

Please sign in to comment.