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 handling of chained new expressions in new-parens #12303

Merged
merged 1 commit into from Sep 29, 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
6 changes: 5 additions & 1 deletion lib/rules/new-parens.js
Expand Up @@ -65,7 +65,11 @@ module.exports = {

const lastToken = sourceCode.getLastToken(node);
const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
const hasParens = hasLastParen && astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken));

// `hasParens` is true only if the new expression ends with its own parens, e.g., new new foo() does not end with its own parens
const hasParens = hasLastParen &&
astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken)) &&
node.callee.range[1] < node.range[1];

if (always) {
if (!hasParens) {
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/new-parens.js
Expand Up @@ -119,6 +119,12 @@ ruleTester.run("new-parens", rule, {
options: ["always"],
errors: [error]
},
{
code: "var a = new new Foo()",
output: "var a = new new Foo()()",
options: ["always"],
errors: [error]
},

// Never
{
Expand Down Expand Up @@ -168,6 +174,12 @@ ruleTester.run("new-parens", rule, {
output: "var a = ((new Foo)).bar;",
options: ["never"],
errors: [neverError]
},
{
code: "var a = new new Foo()",
output: "var a = new (new Foo)",
options: ["never"],
errors: [neverError]
}
]
});