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

Update: fix false negative of no-extra-parens with NewExpression #13930

Merged
merged 1 commit into from Dec 18, 2020
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
36 changes: 25 additions & 11 deletions lib/rules/no-extra-parens.js
Expand Up @@ -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)
)
Comment on lines +484 to +487
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new, the rest of the diff is refactoring.

) ||

// 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);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -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]",
Expand Down Expand Up @@ -844,6 +846,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"),
Comment on lines +851 to +852
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests are new behavior, others are regression tests.

invalid("(a().b).d;", "a().b.d;", "MemberExpression"),
invalid("(a.b()).d;", "a.b().d;", "CallExpression"),
invalid("(a.b).d;", "a.b.d;", "MemberExpression"),
Expand Down