Skip to content

@babel/generator missing parentheses in some edge case of NewExpression #18045

Description

@guyutongxue

💻

  • Would you like to work on a fix?

How are you using Babel?

Babel REPL

Input code

const foo = () => (args) => class A {};
new (foo()`bar`)();

const baz = () => ({ qux: class A {} })
new (baz()?.qux)();

REPL link

Configuration file name

No response

Configuration

{} (no config)

Current and expected behavior

Current behavior: babel prints

const foo = () => args => class A {};
new foo()`bar`();

const baz = () => ({ qux: class A {} });
new baz()?.qux();

the parentheses around foo()`bar` and baz()?.qux are wrongly removed, which changes semantic due to the first () immediately ends NewExpression.

Expected behavior: print the source AS IS

Environment

  • Babel version: v7.29.7
  • Reproduced on REPL, Chrome version: 147

Possible solution

The check of wrapping parentheses around callee of new should consider TaggedTemplateExpression and ChainExpression.

Additional context

The related printer bugs was first observed from SvelteJS' esrap and we'd discovered same issue for Babel.

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions