From d42e865bdc44955fd45b8d39d4cdf25eb032dce9 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 9 May 2022 12:10:44 +0300 Subject: [PATCH 1/2] fix: function-paren-newline: crash on "new new Foo();" --- lib/rules/function-paren-newline.js | 7 ++++--- tests/lib/rules/function-paren-newline.js | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/rules/function-paren-newline.js b/lib/rules/function-paren-newline.js index 898e113ee8e..302ac0a3e75 100644 --- a/lib/rules/function-paren-newline.js +++ b/lib/rules/function-paren-newline.js @@ -191,10 +191,11 @@ module.exports = { function getParenTokens(node) { switch (node.type) { case "NewExpression": - if (!node.arguments.length && !( - astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) && + if (node.callee.type === "NewExpression" || + !node.arguments.length && !( + astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) && astUtils.isClosingParenToken(sourceCode.getLastToken(node)) - )) { + )) { // If the NewExpression does not have parens (e.g. `new Foo`), return null. return null; diff --git a/tests/lib/rules/function-paren-newline.js b/tests/lib/rules/function-paren-newline.js index 048370f356a..183bef9553e 100644 --- a/tests/lib/rules/function-paren-newline.js +++ b/tests/lib/rules/function-paren-newline.js @@ -30,6 +30,7 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); ruleTester.run("function-paren-newline", rule, { valid: [ + "new new Foo();", // multiline option (default) "function baz(foo, bar) {}", From dc0854e3cd059248af9d01161469a403cd4dbfb5 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 9 May 2022 16:46:42 +0300 Subject: [PATCH 2/2] fix: function-paren-newline: false negative case support --- lib/rules/function-paren-newline.js | 10 ++++++---- tests/lib/rules/function-paren-newline.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/rules/function-paren-newline.js b/lib/rules/function-paren-newline.js index 302ac0a3e75..a5b8f0d70c6 100644 --- a/lib/rules/function-paren-newline.js +++ b/lib/rules/function-paren-newline.js @@ -191,11 +191,13 @@ module.exports = { function getParenTokens(node) { switch (node.type) { case "NewExpression": - if (node.callee.type === "NewExpression" || - !node.arguments.length && !( + if (!node.arguments.length && + !( astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) && - astUtils.isClosingParenToken(sourceCode.getLastToken(node)) - )) { + astUtils.isClosingParenToken(sourceCode.getLastToken(node)) && + node.callee.range[1] < node.range[1] + ) + ) { // If the NewExpression does not have parens (e.g. `new Foo`), return null. return null; diff --git a/tests/lib/rules/function-paren-newline.js b/tests/lib/rules/function-paren-newline.js index 183bef9553e..a8c7cc85827 100644 --- a/tests/lib/rules/function-paren-newline.js +++ b/tests/lib/rules/function-paren-newline.js @@ -1175,6 +1175,18 @@ ruleTester.run("function-paren-newline", rule, { options: ["never"], errors: [LEFT_UNEXPECTED_ERROR] }, + { + code: ` + new new C()( + ); + `, + output: ` + new new C()(); + `, + options: ["never"], + errors: [LEFT_UNEXPECTED_ERROR, RIGHT_UNEXPECTED_ERROR] + }, + { code: ` function baz(