Skip to content
Permalink
Browse files

Update: Fix handling of chained new expressions in new-parens (#12303)

  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 29, 2019
1 parent 160b7c4 commit 41bfe919c06932b7e58cd9ead20157e06656160a
Showing with 17 additions and 1 deletion.
  1. +5 −1 lib/rules/new-parens.js
  2. +12 −0 tests/lib/rules/new-parens.js
@@ -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) {
@@ -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
{
@@ -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.
You can’t perform that action at this time.