Navigation Menu

Skip to content

Commit

Permalink
Update: Fix handling of chained new expressions in new-parens (#12303)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 29, 2019
1 parent 160b7c4 commit 41bfe91
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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]
}
]
});

0 comments on commit 41bfe91

Please sign in to comment.