Files
test-repo/changelog_unreleased/javascript/17190.md
T

956 B

Add parentheses to numbers in optional member expression (#17190 by @fisker)

There is an inconsistency when formatting member expression where the object is a number before Prettier main.

When using babel parser(and other Babel based parsers), we print the number as unparenthesized, when using typescript parser(and other ESTree parsers), we print it as parenthesized.

Technically parentheses are not needed, but if we print it as 1?.toString() and later users realize it's unnecessary to use ?., users can't simply remove the question mark because 1.toString() will raise a SyntaxError, for this reason, we decide to always put parentheses around it.

// Input
(1)?.toString();
(1.5)?.toString();

// Prettier stable (--parser=babel)
1?.toString();
1.5?.toString();

// Prettier stable (--parser=typescript)
(1)?.toString();
(1.5)?.toString();

// Prettier main
(1)?.toString();
(1.5)?.toString();