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 call, new and member expressions in no-extra-parens #12302

Merged
merged 1 commit into from Sep 25, 2019
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
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