From 479a54de663bb29b32cfe22ef52576b2672e263a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 12 Sep 2025 01:53:02 +0800 Subject: [PATCH] Add missing parentheses to arrow function in `TSInstantiationExpression` (#17724) --- changelog_unreleased/typescript/17724.md | 13 +++++++++++++ src/language-js/needs-parens.js | 3 +++ .../typescript/instantiation-expression/17714.ts | 1 + .../__snapshots__/format.test.js.snap | 14 ++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 changelog_unreleased/typescript/17724.md create mode 100644 tests/format/typescript/instantiation-expression/17714.ts diff --git a/changelog_unreleased/typescript/17724.md b/changelog_unreleased/typescript/17724.md new file mode 100644 index 000000000..6e4951d0b --- /dev/null +++ b/changelog_unreleased/typescript/17724.md @@ -0,0 +1,13 @@ +#### Add missing parentheses to arrow function in instantiation expression (#17724 by @fisker) + + +```tsx +// Input +void (<_T extends never>() => {}); + +// Prettier stable +void <_T extends never>() => {}; + +// Prettier main +void (<_T extends never>() => {}); +``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index b6aca54a2..9779ae14d 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -881,6 +881,9 @@ function needsParens(path, options) { case "MatchExpressionCase": return true; + case "TSInstantiationExpression": + return key === "expression"; + case "ConditionalExpression": return key === "test"; diff --git a/tests/format/typescript/instantiation-expression/17714.ts b/tests/format/typescript/instantiation-expression/17714.ts new file mode 100644 index 000000000..a5738b907 --- /dev/null +++ b/tests/format/typescript/instantiation-expression/17714.ts @@ -0,0 +1 @@ +void (<_T extends never>() => {}); diff --git a/tests/format/typescript/instantiation-expression/__snapshots__/format.test.js.snap b/tests/format/typescript/instantiation-expression/__snapshots__/format.test.js.snap index dcfe21b6e..d3adf97ad 100644 --- a/tests/format/typescript/instantiation-expression/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/instantiation-expression/__snapshots__/format.test.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`17714.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +void (<_T extends never>() => {}); + +=====================================output===================================== +void (<_T extends never>() => {}); + +================================================================================ +`; + exports[`basic.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"]