Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: false positive new with member in no-extra-parens (fixes #12740) (
…#13375)

* Fix: false positive new with member in no-extra-parens (fixes #12740)

* rename func

* remove duplicate function

* check whole node is wrapped or not

* remove checking whole node

* check member object
  • Loading branch information
yeonjuan committed Jul 3, 2020
1 parent 825a5b9 commit 3f51930
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/rules/no-extra-parens.js
Expand Up @@ -710,6 +710,20 @@ module.exports = {
reportsBuffer.reports = reportsBuffer.reports.filter(r => r.node !== node);
}

/**
* Checks whether a node is a MemberExpression at NewExpression's callee.
* @param {ASTNode} node node to check.
* @returns {boolean} True if the node is a MemberExpression at NewExpression's callee. false otherwise.
*/
function isMemberExpInNewCallee(node) {
if (node.type === "MemberExpression") {
return node.parent.type === "NewExpression" && node.parent.callee === node
? true
: node.parent.object === node && isMemberExpInNewCallee(node.parent);
}
return false;
}

return {
ArrayExpression(node) {
node.elements
Expand Down Expand Up @@ -950,7 +964,11 @@ module.exports = {
LogicalExpression: checkBinaryLogical,

MemberExpression(node) {
const nodeObjHasExcessParens = hasExcessParens(node.object) &&
const shouldAllowWrapOnce = isMemberExpInNewCallee(node) &&
doesMemberExpressionContainCallExpression(node);
const nodeObjHasExcessParens = shouldAllowWrapOnce
? hasDoubleExcessParens(node.object)
: hasExcessParens(node.object) &&
!(
isImmediateFunctionPrototypeMethodCall(node.parent) &&
node.parent.callee === node &&
Expand All @@ -974,8 +992,8 @@ module.exports = {
}

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

Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -612,6 +612,14 @@ ruleTester.run("no-extra-parens", rule, {
"for (; a; a); a; a;",
"for (let a = (b && c) === d; ;);",

"new (a()).b.c;",
"new (a().b).c;",
"new (a().b.c);",
"new (a().b().d);",
"new a().b().d;",
"new (a(b()).c)",
"new (a.b()).c",

// Nullish coalescing
{ code: "var v = (a ?? b) || c", parserOptions: { ecmaVersion: 2020 } },
{ code: "var v = a ?? (b || c)", parserOptions: { ecmaVersion: 2020 } },
Expand Down Expand Up @@ -759,6 +767,7 @@ ruleTester.run("no-extra-parens", rule, {
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[(b()).c]", "new a[b().c]", "CallExpression"),

invalid("(a)()", "a()", "Identifier"),
invalid("(a.b)()", "a.b()", "MemberExpression"),
Expand All @@ -772,6 +781,14 @@ ruleTester.run("no-extra-parens", rule, {
invalid("((new A))()", "(new A)()", "NewExpression"),
invalid("new (foo\n.baz\n.bar\n.foo.baz)", "new foo\n.baz\n.bar\n.foo.baz", "MemberExpression"),
invalid("new (foo.baz.bar.baz)", "new foo.baz.bar.baz", "MemberExpression"),
invalid("new ((a.b())).c", "new (a.b()).c", "CallExpression"),
invalid("new ((a().b)).c", "new (a().b).c", "MemberExpression"),
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("(a().b).d;", "a().b.d;", "MemberExpression"),
invalid("(a.b()).d;", "a.b().d;", "CallExpression"),
invalid("(a.b).d;", "a.b.d;", "MemberExpression"),

invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1),
invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1),
Expand Down

0 comments on commit 3f51930

Please sign in to comment.