diff --git a/changelog_unreleased/typescript/16194.md b/changelog_unreleased/typescript/16194.md new file mode 100644 index 000000000..f1dafbc17 --- /dev/null +++ b/changelog_unreleased/typescript/16194.md @@ -0,0 +1,21 @@ +#### Add necessary parentheses to yield expressions (#16194 by @kirkwaiblinger) + +Add parentheses around yield expressions if parent is an angle-bracket type assertion. + + +```ts +// Input +function* g() { + const y = (yield x); +} + +// Prettier stable +function* g() { + const y = yield x; +} + +// Prettier main +function* g() { + const y = (yield x); +} +``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index ba3a49efd..c0074632b 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -472,7 +472,10 @@ function needsParens(path, options) { } case "YieldExpression": - if (parent.type === "AwaitExpression") { + if ( + parent.type === "AwaitExpression" || + parent.type === "TSTypeAssertion" + ) { return true; } // else fallthrough diff --git a/tests/format/typescript/cast/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/cast/__snapshots__/jsfmt.spec.js.snap index 19b789bd9..b46abb429 100644 --- a/tests/format/typescript/cast/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/cast/__snapshots__/jsfmt.spec.js.snap @@ -355,6 +355,10 @@ true || a; a + b; (a) = 1; +function * g() { + const a = (yield b); +} + =====================================output===================================== (a ? b : c); (() => {}); @@ -365,6 +369,10 @@ true || a; a + b; (a) = 1; +function* g() { + const a = (yield b); +} + ================================================================================ `; diff --git a/tests/format/typescript/cast/parenthesis.ts b/tests/format/typescript/cast/parenthesis.ts index edada7854..db00a98be 100644 --- a/tests/format/typescript/cast/parenthesis.ts +++ b/tests/format/typescript/cast/parenthesis.ts @@ -6,3 +6,7 @@ true || a; a + b; (a) = 1; + +function * g() { + const a = (yield b); +}