From 765aa10dfe006bf799803ef43363a124708dc597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sat, 9 Mar 2024 19:46:47 +0800 Subject: [PATCH] fix: no semi for readonly class field (#16133) Co-authored-by: Sosuke Suzuki --- changelog_unreleased/typescript/16133.md | 22 +++++++++++++++++++ src/language-js/print/class.js | 3 ++- .../no-semi/__snapshots__/jsfmt.spec.js.snap | 4 ++++ tests/format/typescript/no-semi/no-semi.ts | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/16133.md diff --git a/changelog_unreleased/typescript/16133.md b/changelog_unreleased/typescript/16133.md new file mode 100644 index 000000000..9c2b90f26 --- /dev/null +++ b/changelog_unreleased/typescript/16133.md @@ -0,0 +1,22 @@ +#### Respect `--no-semi` for readonly class field (#16133 by @sxzz) + + +```tsx +// Input +class A { + field + readonly [expr] = true +} + +// Prettier stable +class A { + field; + readonly [expr] = true +} + +// Prettier main +class A { + field + readonly [expr] = true +} +``` diff --git a/src/language-js/print/class.js b/src/language-js/print/class.js index 518340460..bbcb24670 100644 --- a/src/language-js/print/class.js +++ b/src/language-js/print/class.js @@ -323,7 +323,8 @@ function shouldPrintSemicolonAfterClassProperty(node, nextNode) { if ( nextNode.static || - nextNode.accessibility // TypeScript + nextNode.accessibility || // TypeScript + nextNode.readonly // TypeScript ) { return false; } diff --git a/tests/format/typescript/no-semi/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/no-semi/__snapshots__/jsfmt.spec.js.snap index 4e9ca3903..36ef55ac7 100644 --- a/tests/format/typescript/no-semi/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/no-semi/__snapshots__/jsfmt.spec.js.snap @@ -16,6 +16,7 @@ class A { bar: A; private [baz] + readonly [qux] } =====================================output===================================== @@ -28,6 +29,7 @@ class A { bar: A private [baz] + readonly [qux] } ================================================================================ @@ -48,6 +50,7 @@ class A { bar: A; private [baz] + readonly [qux] } =====================================output===================================== @@ -60,6 +63,7 @@ class A { bar: A; private [baz]; + readonly [qux]; } ================================================================================ diff --git a/tests/format/typescript/no-semi/no-semi.ts b/tests/format/typescript/no-semi/no-semi.ts index 19134b858..2ef89445a 100644 --- a/tests/format/typescript/no-semi/no-semi.ts +++ b/tests/format/typescript/no-semi/no-semi.ts @@ -7,4 +7,5 @@ class A { bar: A; private [baz] + readonly [qux] }