Print ; for module declaration and declare function without body (#18852)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-02-05 10:19:28 +08:00
1 parent c06314c988
commit 0eda012bcb
9 files changed
+203 -5

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
#### Fix required semicolon in `no-semi` mode (#18852 by @fisker)
<!-- prettier-ignore -->
```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();
}
```
+5 -3
View File
@@ -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
";",
];
}
+6 -2
View File
@@ -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
";",
];
}
@@ -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
}
================================================================================
`;
@@ -0,0 +1 @@
runFormatTest(import.meta, ["flow"], { semi: false });
+11
View File
@@ -0,0 +1,11 @@
declare function foo():void;
{
foo
}
declare hook foo(): Foo;
{
foo
}
@@ -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"]
@@ -0,0 +1,14 @@
declare function a():void;
{
foo()
}
declare function b():void;
[foo()]
for(;;) function c();
{}
@@ -0,0 +1,15 @@
declare module "foo";
{
foo()
}
declare module "bar" {};
{
foo()
}
declare module "baz";
[foo()]