prettier/prettier

Extra parentheses added for nullish coalescing operator in ternary expression

Aberta

#19.533 aberto em 3 de jul. de 2026

 (0 comentário) (0 reação) (1 responsável)JavaScript (4.987 forks)batch import
help wantedlang:javascript

Métricas do repositório

Stars
 (52.204 estrelas)
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