From 48c9bc4aea6061db7dfd7983c6f7ac3512176b36 Mon Sep 17 00:00:00 2001 From: Kirk Waiblinger <53019676+kirkwaiblinger@users.noreply.github.com> Date: Thu, 4 Apr 2024 01:39:31 -0600 Subject: [PATCH] Yield expression parens (#16194) Co-authored-by: fisker Cheung --- changelog_unreleased/typescript/16194.md | 21 +++++++++++++++++++ src/language-js/needs-parens.js | 5 ++++- .../cast/__snapshots__/jsfmt.spec.js.snap | 8 +++++++ tests/format/typescript/cast/parenthesis.ts | 4 ++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/16194.md 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); +}