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

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

Merged
merged 7 commits into from Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
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
: isMemberExpInNewCallee(node.parent);
}
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this should go up the tree only if it's object child of another MemberExpression, otherwise this would be a false negative:

"new a[(b()).c]"

Copy link
Member Author

Choose a reason for hiding this comment

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

@mdjermanovic
Thanks! 👍 I added a checking about that and test case.


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
16 changes: 16 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 @@ -772,6 +780,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