Skip to content

Extra parentheses added for nullish coalescing operator in ternary expression #19533

Description

@isti115

Prettier v3.9.4
Playground link

--parser babel

Input:

const value = condition ? (
  <Example /> // comment
) : (
  "alpha" ?? "bravo"
);

const value = condition ? (
  <Example /> // comment
) : (
  "alpha" + "bravo"
);

Output:

const value = condition ? (
  <Example /> // comment
) : (
  ("alpha" ?? "bravo")
);

const value = condition ? (
  <Example /> // comment
) : (
  "alpha" + "bravo"
);

Expected output:

const value = condition ? (
  <Example /> // comment
) : (
  "alpha" ?? "bravo"
);

const value = condition ? (
  <Example /> // comment
) : (
  "alpha" + "bravo"
);

(Same as input, unchanged.)

Why?

The nullish coalescing operator ends up in double parentheses even though the second (inner) pair is completely unnecessary. The second example shows the inconsistency with other operators. (The comment isn't actually necessary for the reproduction, it's just an easy way to force breaking, but a long line works just as well.)

Metadata

Metadata

Assignees

Labels

help wantedWe're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue!lang:javascriptIssues affecting JS

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions