Fix the union type that contains an extra empty line (#18197)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
authored and GitHub committed 2025-11-17 20:13:03 +08:00
1 parent 9bde6074f0
commit 48b4ff97ad
3 files changed
+32 -6

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
#### Remove extra empty line in union type (#18197 by @Dunqing)
<!-- prettier-ignore -->
```tsx
// Input
type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla =
Fooo1000 | Baz2000 | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong;
// Prettier stable
type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla =
| Fooo1000
| Baz2000
| BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong;
// Prettier main
type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla =
| Fooo1000
| Baz2000
| BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong;
```
+8 -2
View File
@@ -62,6 +62,7 @@ function fits(
} }
let restCommandsIndex = restCommands.length; let restCommandsIndex = restCommands.length;
let hasPendingSpace = false;
/** @type {Array<Omit<Command, 'indent'>>} */ /** @type {Array<Omit<Command, 'indent'>>} */
const commands = [next]; const commands = [next];
// `output` is only used for width counting because `trim` requires to look // `output` is only used for width counting because `trim` requires to look
@@ -82,6 +83,12 @@ function fits(
switch (docType) { switch (docType) {
case DOC_TYPE_STRING: case DOC_TYPE_STRING:
if (doc) { if (doc) {
if (hasPendingSpace) {
output += " ";
remainingWidth -= 1;
hasPendingSpace = false;
}
output += doc; output += doc;
remainingWidth -= getStringWidth(doc); remainingWidth -= getStringWidth(doc);
} }
@@ -142,8 +149,7 @@ function fits(
return true; return true;
} }
if (!doc.soft) { if (!doc.soft) {
output += " "; hasPendingSpace = true;
remainingWidth--;
} }
break; break;
@@ -142,10 +142,9 @@ type A3 = // dir, exp, arg, modifiers
[string, ExpressionNode, ExpressionNode, ObjectExpression]; [string, ExpressionNode, ExpressionNode, ObjectExpression];
type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla =
| Fooo1000
| Fooo1000 | Baz2000
| Baz2000 | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong;
| BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong;
type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBl2 = type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBl2 =
Fooo1000 & Fooo1000 &