prettier/prettier

Extra parentheses added for nullish coalescing operator in ternary expression

Open

#19.533 aberto em 3 de jul. de 2026

Ver no GitHub
 (0 comments) (0 reactions) (0 assignees)JavaScript (4.716 forks)batch import
help wantedlang:javascript

Métricas do repositório

Stars
 (51.868 stars)
Métricas de merge de PR
 (Mesclagem média 6d) (186 fundiu PRs em 30d)

Description

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.)

Guia do colaborador