diff --git a/changelog_unreleased/typescript/19442.md b/changelog_unreleased/typescript/19442.md new file mode 100644 index 000000000..5c3a0024f --- /dev/null +++ b/changelog_unreleased/typescript/19442.md @@ -0,0 +1,25 @@ +#### Fix missing parentheses around instantiation expression (#19442 by @fisker) + + +```tsx +// Input +async function* f() { + makeRecord = (yield* makeRecordFactory); + makeRecord = (yield makeRecordFactory); + makeRecord = (await makeRecordFactory); +} + +// Prettier stable +async function* f() { + makeRecord = yield* makeRecordFactory; + makeRecord = yield makeRecordFactory; + makeRecord = await makeRecordFactory; +} + +// Prettier main +async function* f() { + makeRecord = (yield* makeRecordFactory); + makeRecord = (yield makeRecordFactory); + makeRecord = (await makeRecordFactory); +} +``` diff --git a/src/language-js/parentheses/parent-needs-parentheses.js b/src/language-js/parentheses/parent-needs-parentheses.js index ca84213bd..fb42c5ca3 100644 --- a/src/language-js/parentheses/parent-needs-parentheses.js +++ b/src/language-js/parentheses/parent-needs-parentheses.js @@ -115,6 +115,16 @@ function parentNeedsParentheses(path, options, needsParentheses) { return true; } break; + + case "TSInstantiationExpression": + if ( + key === "expression" && + (node.type === "AwaitExpression" || node.type === "YieldExpression") + ) { + return true; + } + + break; } } diff --git a/tests/format/typescript/parentheses/__snapshots__/format.test.js.snap b/tests/format/typescript/parentheses/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..6019aea29 --- /dev/null +++ b/tests/format/typescript/parentheses/__snapshots__/format.test.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`await.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] + printWidth: 80 (default) | +=====================================input====================================== +async function* f() { + makeRecord = ( await makeRecordFactory) +} + +=====================================output===================================== +async function* f() { + makeRecord = (await makeRecordFactory); +} + +================================================================================ +`; + +exports[`yield.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] + printWidth: 80 (default) | +=====================================input====================================== +async function* f() { + makeRecord = ( yield* makeRecordFactory) + makeRecord = ( yield makeRecordFactory) +} + +=====================================output===================================== +async function* f() { + makeRecord = (yield* makeRecordFactory); + makeRecord = (yield makeRecordFactory); +} + +================================================================================ +`; diff --git a/tests/format/typescript/parentheses/await.ts b/tests/format/typescript/parentheses/await.ts new file mode 100644 index 000000000..f1cab7f2d --- /dev/null +++ b/tests/format/typescript/parentheses/await.ts @@ -0,0 +1,3 @@ +async function* f() { + makeRecord = ( await makeRecordFactory) +} diff --git a/tests/format/typescript/parentheses/format.test.js b/tests/format/typescript/parentheses/format.test.js new file mode 100644 index 000000000..116dcbd78 --- /dev/null +++ b/tests/format/typescript/parentheses/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["typescript"]); diff --git a/tests/format/typescript/parentheses/yield.ts b/tests/format/typescript/parentheses/yield.ts new file mode 100644 index 000000000..5affb93ed --- /dev/null +++ b/tests/format/typescript/parentheses/yield.ts @@ -0,0 +1,4 @@ +async function* f() { + makeRecord = ( yield* makeRecordFactory) + makeRecord = ( yield makeRecordFactory) +}