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

fix: function-paren-newline crash on "new new Foo();" #15850

Merged
merged 2 commits into from May 9, 2022
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
11 changes: 7 additions & 4 deletions lib/rules/function-paren-newline.js
Expand Up @@ -191,10 +191,13 @@ module.exports = {
function getParenTokens(node) {
switch (node.type) {
case "NewExpression":
if (!node.arguments.length && !(
astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) &&
astUtils.isClosingParenToken(sourceCode.getLastToken(node))
)) {
if (!node.arguments.length &&
!(
astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) &&
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;
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/function-paren-newline.js
Expand Up @@ -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) {}",
Expand Down Expand Up @@ -1174,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(
Expand Down