Fix comment in empty type literal (#18364)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-11-29 00:00:52 +08:00
1 parent 21a91fc2aa
commit 29a11ae1ae
11 files changed
+267 -41

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
#### Fix comment in empty type literal (#18364 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
export type XXX = {
// tbd
};
// Prettier stable
export type XXX = { // tbd };
// Prettier main
export type XXX = {
// tbd
};
```
+10 -8
View File
@@ -32,6 +32,7 @@ function printClassBody(path, options, print) {
const isFlowTypeAnnotation = node.type === "ObjectTypeAnnotation";
const isObjectType = !isClassBody(path);
const separator = isObjectType ? line : hardline;
const hasDanglingComments = hasComment(node, CommentCheckFlags.Dangling);
const [openingBrace, closingBrace] =
isFlowTypeAnnotation && node.exact ? ["{|", "|}"] : "{}";
@@ -68,7 +69,7 @@ function printClassBody(path, options, print) {
}
});
if (hasComment(node, CommentCheckFlags.Dangling)) {
if (hasDanglingComments) {
parts.push(printDanglingComments(path, options));
}
@@ -92,13 +93,14 @@ function printClassBody(path, options, print) {
if (isObjectType) {
const shouldBreak =
options.objectWrap === "preserve" &&
firstMember &&
hasNewlineInRange(
options.originalText,
locStart(node),
locStart(firstMember),
);
hasDanglingComments ||
(options.objectWrap === "preserve" &&
firstMember &&
hasNewlineInRange(
options.originalText,
locStart(node),
locStart(firstMember),
));
let content;
if (parts.length === 0) {
@@ -81,7 +81,10 @@ type Foo = {
...
};
type Foo = { /* comment */ ... };
type Foo = {
/* comment */
...
};
type Foo = {
/* comment */
@@ -216,7 +219,10 @@ type Foo = {
...
};
type Foo = { /* comment */ ... };
type Foo = {
/* comment */
...
};
type Foo = {
/* comment */
@@ -351,7 +357,10 @@ type Foo = {
...
};
type Foo = { /* comment */ ... };
type Foo = {
/* comment */
...
};
type Foo = {
/* comment */
@@ -239,31 +239,11 @@ parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class A1 {
// comment
}
class A2 { // comment
}
class A3 {
}
class A4 {
m() {}
}
=====================================output=====================================
class A1 {
// comment
}
class A2 {
// comment
}
class A3 {}
class A4 {
m() {}
}
-10
View File
@@ -1,13 +1,3 @@
class A1 {
// comment
}
class A2 { // comment
}
class A3 {
}
class A4 {
m() {}
}
@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`empty.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class C1 {}
class C2 {//comment
}
class C3 {
//comment
}
class C4 {/*comment*/}
class C5 {/*comment*/
}
class C6 {
/*comment*/}
class C7 {
/*comment*/
}
[
class C1 {},
class C2 {//comment
},
class C3 {
//comment
},
class C4 {/*comment*/},
class C5 {/*comment*/
},
class C6 {
/*comment*/},
class C7 {
/*comment*/
},
];
=====================================output=====================================
class C1 {}
class C2 {
//comment
}
class C3 {
//comment
}
class C4 {
/*comment*/
}
class C5 {
/*comment*/
}
class C6 {
/*comment*/
}
class C7 {
/*comment*/
}
[
class C1 {},
class C2 {
//comment
},
class C3 {
//comment
},
class C4 {
/*comment*/
},
class C5 {
/*comment*/
},
class C6 {
/*comment*/
},
class C7 {
/*comment*/
},
];
================================================================================
`;
+31
View File
@@ -0,0 +1,31 @@
class C1 {}
class C2 {//comment
}
class C3 {
//comment
}
class C4 {/*comment*/}
class C5 {/*comment*/
}
class C6 {
/*comment*/}
class C7 {
/*comment*/
}
[
class C1 {},
class C2 {//comment
},
class C3 {
//comment
},
class C4 {/*comment*/},
class C5 {/*comment*/
},
class C6 {
/*comment*/},
class C7 {
/*comment*/
},
];
@@ -0,0 +1 @@
runFormatTest(import.meta, ["babel", "flow", "typescript"]);
@@ -0,0 +1,81 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`empty.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
interface I1 {}
interface I2 {//comment
}
interface I3 {
//comment
}
interface I4 {/*comment*/}
interface I5 {/*comment*/
}
interface I6 {
/*comment*/}
interface I7 {
/*comment*/
}
type T1 = {}
type T2 = {//comment
}
type T3 = {
//comment
}
type T4 = {/*comment*/}
type T5 = {/*comment*/
}
type T6 = {
/*comment*/}
type T7 = {
/*comment*/
}
=====================================output=====================================
interface I1 {}
interface I2 {
//comment
}
interface I3 {
//comment
}
interface I4 {
/*comment*/
}
interface I5 {
/*comment*/
}
interface I6 {
/*comment*/
}
interface I7 {
/*comment*/
}
type T1 = {};
type T2 = {
//comment
};
type T3 = {
//comment
};
type T4 = {
/*comment*/
};
type T5 = {
/*comment*/
};
type T6 = {
/*comment*/
};
type T7 = {
/*comment*/
};
================================================================================
`;
@@ -0,0 +1,29 @@
interface I1 {}
interface I2 {//comment
}
interface I3 {
//comment
}
interface I4 {/*comment*/}
interface I5 {/*comment*/
}
interface I6 {
/*comment*/}
interface I7 {
/*comment*/
}
type T1 = {}
type T2 = {//comment
}
type T3 = {
//comment
}
type T4 = {/*comment*/}
type T5 = {/*comment*/
}
type T6 = {
/*comment*/}
type T7 = {
/*comment*/
}
@@ -0,0 +1 @@
runFormatTest(import.meta, ["typescript"]);