diff --git a/changelog_unreleased/typescript/18852.md b/changelog_unreleased/typescript/18852.md new file mode 100644 index 000000000..fea605600 --- /dev/null +++ b/changelog_unreleased/typescript/18852.md @@ -0,0 +1,35 @@ +#### 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 42fc7e89e..03241c8be 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -84,9 +84,11 @@ function printFunction(path, options, print, args) { shouldGroupParameters ? group(parametersDoc) : parametersDoc, returnTypeDoc, ]), - node.body ? " " : "", - print("body"), - node.declare || !node.body ? printSemicolon(options) : "", + node.body + ? [" ", print("body")] + : // The semicolon not always needed, + // but prevent block statement after been parsed as body + ";", ]; } diff --git a/src/language-js/print/module-declaration.js b/src/language-js/print/module-declaration.js index 162c1e357..e93927915 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, printSemicolon } from "./miscellaneous.js"; +import { printDeclareToken } from "./miscellaneous.js"; /* - `TSModuleDeclaration` (TypeScript) @@ -11,7 +11,11 @@ function printModuleDeclaration(path, options, print) { printDeclareToken(path), node.kind === "global" ? "" : `${node.kind} `, print("id"), - node.body ? [" ", group(print("body"))] : printSemicolon(options), + node.body + ? [" ", group(print("body"))] + : // The semicolon not always needed, + // but prevent block statement after been parsed as body + ";", ]; } 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 new file mode 100644 index 000000000..c1441442e --- /dev/null +++ b/tests/format/flow/no-semi/asi/__snapshots__/format.test.js.snap @@ -0,0 +1,35 @@ +// 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 new file mode 100644 index 000000000..54c60c57c --- /dev/null +++ b/tests/format/flow/no-semi/asi/format.test.js @@ -0,0 +1 @@ +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 new file mode 100644 index 000000000..53dc8479b --- /dev/null +++ b/tests/format/flow/no-semi/asi/no-semi.js @@ -0,0 +1,11 @@ +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 8f1770d2a..d69d486de 100644 --- a/tests/format/typescript/nosemi/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/nosemi/__snapshots__/format.test.js.snap @@ -1,5 +1,44 @@ // 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"] @@ -53,6 +92,48 @@ 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 new file mode 100644 index 000000000..b15c792ff --- /dev/null +++ b/tests/format/typescript/nosemi/functions.ts @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..29f92b9c1 --- /dev/null +++ b/tests/format/typescript/nosemi/module-declaration.ts @@ -0,0 +1,15 @@ +declare module "foo"; + +{ + foo() +} + +declare module "bar" {}; + +{ + foo() +} + +declare module "baz"; + +[foo()]