From 2eaad40bb27902374a490f8cd7e6286708814c6b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 10 Jun 2026 09:22:01 +0800 Subject: [PATCH] Revert "Print `;` for module declaration and declare function" (#18852) (#19333) --- changelog_unreleased/typescript/18852.md | 35 -------- src/language-js/print/function.js | 8 +- src/language-js/print/module-declaration.js | 8 +- .../asi/__snapshots__/format.test.js.snap | 35 -------- tests/format/flow/no-semi/asi/format.test.js | 1 - tests/format/flow/no-semi/asi/no-semi.js | 11 --- .../nosemi/__snapshots__/format.test.js.snap | 81 ------------------- tests/format/typescript/nosemi/functions.ts | 14 ---- .../typescript/nosemi/module-declaration.ts | 15 ---- 9 files changed, 5 insertions(+), 203 deletions(-) delete mode 100644 changelog_unreleased/typescript/18852.md delete mode 100644 tests/format/flow/no-semi/asi/__snapshots__/format.test.js.snap delete mode 100644 tests/format/flow/no-semi/asi/format.test.js delete mode 100644 tests/format/flow/no-semi/asi/no-semi.js delete mode 100644 tests/format/typescript/nosemi/functions.ts delete mode 100644 tests/format/typescript/nosemi/module-declaration.ts diff --git a/changelog_unreleased/typescript/18852.md b/changelog_unreleased/typescript/18852.md deleted file mode 100644 index fea605600..000000000 --- a/changelog_unreleased/typescript/18852.md +++ /dev/null @@ -1,35 +0,0 @@ -#### Fix required semicolon in `no-semi` mode (#18852 by @fisker) - - -```tsx -// Input -declare module "autoprefixer"; -{ - alert() -} -declare function a(); -{ - alert() -} - -// Prettier stable (--no-semi) -declare module "autoprefixer" -{ - alert() -} -declare function a() -{ - alert() -} - - -// Prettier main (--no-semi) -declare module "autoprefixer"; -{ - alert(); -} -declare function a(); -{ - alert(); -} -``` diff --git a/src/language-js/print/function.js b/src/language-js/print/function.js index 03241c8be..42fc7e89e 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -84,11 +84,9 @@ function printFunction(path, options, print, args) { shouldGroupParameters ? group(parametersDoc) : parametersDoc, returnTypeDoc, ]), - node.body - ? [" ", print("body")] - : // The semicolon not always needed, - // but prevent block statement after been parsed as body - ";", + node.body ? " " : "", + print("body"), + node.declare || !node.body ? printSemicolon(options) : "", ]; } diff --git a/src/language-js/print/module-declaration.js b/src/language-js/print/module-declaration.js index e93927915..162c1e357 100644 --- a/src/language-js/print/module-declaration.js +++ b/src/language-js/print/module-declaration.js @@ -1,5 +1,5 @@ import { group } from "../../document/index.js"; -import { printDeclareToken } from "./miscellaneous.js"; +import { printDeclareToken, printSemicolon } from "./miscellaneous.js"; /* - `TSModuleDeclaration` (TypeScript) @@ -11,11 +11,7 @@ function printModuleDeclaration(path, options, print) { printDeclareToken(path), node.kind === "global" ? "" : `${node.kind} `, print("id"), - node.body - ? [" ", group(print("body"))] - : // The semicolon not always needed, - // but prevent block statement after been parsed as body - ";", + node.body ? [" ", group(print("body"))] : printSemicolon(options), ]; } diff --git a/tests/format/flow/no-semi/asi/__snapshots__/format.test.js.snap b/tests/format/flow/no-semi/asi/__snapshots__/format.test.js.snap deleted file mode 100644 index c1441442e..000000000 --- a/tests/format/flow/no-semi/asi/__snapshots__/format.test.js.snap +++ /dev/null @@ -1,35 +0,0 @@ -// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing - -exports[`no-semi.js - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["flow"] -semi: false - printWidth: 80 (default) | -=====================================input====================================== -declare function foo():void; - -{ - foo -} - -declare hook foo(): Foo; - -{ - foo -} - -=====================================output===================================== -declare function foo(): void - -{ - foo -} - -declare hook foo(): Foo - -{ - foo -} - -================================================================================ -`; diff --git a/tests/format/flow/no-semi/asi/format.test.js b/tests/format/flow/no-semi/asi/format.test.js deleted file mode 100644 index 54c60c57c..000000000 --- a/tests/format/flow/no-semi/asi/format.test.js +++ /dev/null @@ -1 +0,0 @@ -runFormatTest(import.meta, ["flow"], { semi: false }); diff --git a/tests/format/flow/no-semi/asi/no-semi.js b/tests/format/flow/no-semi/asi/no-semi.js deleted file mode 100644 index 53dc8479b..000000000 --- a/tests/format/flow/no-semi/asi/no-semi.js +++ /dev/null @@ -1,11 +0,0 @@ -declare function foo():void; - -{ - foo -} - -declare hook foo(): Foo; - -{ - foo -} diff --git a/tests/format/typescript/nosemi/__snapshots__/format.test.js.snap b/tests/format/typescript/nosemi/__snapshots__/format.test.js.snap index d69d486de..8f1770d2a 100644 --- a/tests/format/typescript/nosemi/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/nosemi/__snapshots__/format.test.js.snap @@ -1,44 +1,5 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing -exports[`functions.ts - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -semi: false - printWidth: 80 (default) | -=====================================input====================================== -declare function a():void; - -{ - foo() -} - - -declare function b():void; - -[foo()] - - -for(;;) function c(); -{} - -=====================================output===================================== -declare function a(): void; - -{ - foo() -} - -declare function b(): void; - -;[foo()] - -for (;;) function c(); -{ -} - -================================================================================ -`; - exports[`index-signature.ts - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -92,48 +53,6 @@ interface InlineMultiple { ================================================================================ `; -exports[`module-declaration.ts - {"semi":false} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -semi: false - printWidth: 80 (default) | -=====================================input====================================== -declare module "foo"; - -{ - foo() -} - -declare module "bar" {}; - -{ - foo() -} - -declare module "baz"; - -[foo()] - -=====================================output===================================== -declare module "foo"; - -{ - foo() -} - -declare module "bar" {} - -{ - foo() -} - -declare module "baz"; - -;[foo()] - -================================================================================ -`; - exports[`type.ts - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/nosemi/functions.ts b/tests/format/typescript/nosemi/functions.ts deleted file mode 100644 index b15c792ff..000000000 --- a/tests/format/typescript/nosemi/functions.ts +++ /dev/null @@ -1,14 +0,0 @@ -declare function a():void; - -{ - foo() -} - - -declare function b():void; - -[foo()] - - -for(;;) function c(); -{} diff --git a/tests/format/typescript/nosemi/module-declaration.ts b/tests/format/typescript/nosemi/module-declaration.ts deleted file mode 100644 index 29f92b9c1..000000000 --- a/tests/format/typescript/nosemi/module-declaration.ts +++ /dev/null @@ -1,15 +0,0 @@ -declare module "foo"; - -{ - foo() -} - -declare module "bar" {}; - -{ - foo() -} - -declare module "baz"; - -[foo()]