Remove space in empty object type (#19583)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-07-09 17:34:22 +08:00
1 parent 6394c73209
commit cbee7377a1
10 files changed
+237 -11

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
#### Remove space in comment-only object type (#19583 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
var foo = {
/* comment */
};
type Foo = {
/* comment */
};
// Prettier stable
var foo = {/* comment */};
type Foo = { /* comment */ };
// Prettier main
var foo = {/* comment */};
type Foo = {/* comment */};
```
+5 -1
View File
@@ -39,10 +39,12 @@ function printClassBody(path, options, print) {
const [openingBrace, closingBrace] =
isFlowTypeAnnotation && node.exact ? ["{|", "|}"] : "{}";
let isEmpty = true;
let firstMember;
iterateClassMembersPath(path, ({ node, next, isLast }) => {
firstMember ??= node;
isEmpty &&= false;
parts.push(print());
if (!isFlowRecordDeclaration && isObjectType && isFlowTypeAnnotation) {
@@ -83,6 +85,7 @@ function printClassBody(path, options, print) {
// TODO: this part can unify with the similar part in `printObject`
if (node.type === "ObjectTypeAnnotation" && node.inexact) {
isEmpty &&= false;
let printed;
if (hasComment(node, CommentCheckFlags.Dangling)) {
const hasLineComments = hasComment(node, CommentCheckFlags.Line);
@@ -114,7 +117,8 @@ function printClassBody(path, options, print) {
if (parts.length === 0) {
content = openingBrace + closingBrace;
} else {
const spacing = options.bracketSpacing ? line : softline;
const spacing =
!options.bracketSpacing || (isEmpty && !shouldBreak) ? softline : line;
content = [
openingBrace,
indent([spacing, ...parts]),
@@ -43,16 +43,16 @@ T6 = {/* comment */ // comment
};
=====================================output=====================================
type T1 = {| /* comment */ |};
type T12 = { /* comment */ };
type T1 = {|/* comment */|};
type T12 = {/* comment */};
T1 = {/* comment */};
type T2 = {| /* comment */ |};
type T22 = { /* comment */ };
type T2 = {|/* comment */|};
type T22 = {/* comment */};
T2 = {/* comment */};
type T3 = {| /* comment */ |};
type T32 = { /* comment */ };
type T3 = {|/* comment */|};
type T32 = {/* comment */};
T3 = {/* comment */};
type T4 = {|
@@ -0,0 +1,97 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`empty.js - {"bracketSpacing":false} format 1`] = `
====================================options=====================================
bracketSpacing: false
parsers: ["flow"]
printWidth: 80 (default) |
=====================================input======================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
type O23 = {...};
type O24 = {/*c*/ ...};
type O31 = {||};
type O32 = {|/*c*/|};
// type O33 = {|...|};
// type O34 = {|/*c*/ ...|};
interface I21 {}
interface I22 {/*c*/}
=====================================output=====================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
type O23 = {...};
type O24 = {/*c*/ ...};
type O31 = {||};
type O32 = {|/*c*/|};
// type O33 = {|...|};
// type O34 = {|/*c*/ ...|};
interface I21 {}
interface I22 {
/*c*/
}
================================================================================
`;
exports[`empty.js - {"bracketSpacing":true} format 1`] = `
====================================options=====================================
bracketSpacing: true
parsers: ["flow"]
printWidth: 80 (default) |
=====================================input======================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
type O23 = {...};
type O24 = {/*c*/ ...};
type O31 = {||};
type O32 = {|/*c*/|};
// type O33 = {|...|};
// type O34 = {|/*c*/ ...|};
interface I21 {}
interface I22 {/*c*/}
=====================================output=====================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
type O23 = { ... };
type O24 = { /*c*/ ... };
type O31 = {||};
type O32 = {|/*c*/|};
// type O33 = {|...|};
// type O34 = {|/*c*/ ...|};
interface I21 {}
interface I22 {
/*c*/
}
================================================================================
`;
@@ -0,0 +1,17 @@
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
type O23 = {...};
type O24 = {/*c*/ ...};
type O31 = {||};
type O32 = {|/*c*/|};
// type O33 = {|...|};
// type O34 = {|/*c*/ ...|};
interface I21 {}
interface I22 {/*c*/}
@@ -0,0 +1,2 @@
runFormatTest(import.meta, ["flow"], { bracketSpacing: true });
runFormatTest(import.meta, ["flow"], { bracketSpacing: false });
@@ -63,10 +63,10 @@ type T2 = {
type T3 = {
//comment
};
type T4 = { /*comment*/ };
type T5 = { /*comment*/ };
type T6 = { /*comment*/ };
type T7 = { /*comment*/ };
type T4 = {/*comment*/};
type T5 = {/*comment*/};
type T6 = {/*comment*/};
type T7 = {/*comment*/};
================================================================================
`;
@@ -0,0 +1,73 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`empty.ts - {"bracketSpacing":false} format 1`] = `
====================================options=====================================
bracketSpacing: false
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
interface I21 {}
interface I22 {/*c*/}
=====================================output=====================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
interface I21 {}
interface I22 {
/*c*/
}
================================================================================
`;
exports[`empty.ts - {"bracketSpacing":true} format 1`] = `
====================================options=====================================
bracketSpacing: true
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
interface I21 {}
interface I22 {/*c*/}
=====================================output=====================================
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
interface I21 {}
interface I22 {
/*c*/
}
================================================================================
`;
@@ -0,0 +1,11 @@
a = [];
a = [/*c*/];
type A11 = [];
type A12 = [/*c*/];
o = {};
o = {/*c*/};
type O21 = {};
type O22 = {/*c*/};
interface I21 {}
interface I22 {/*c*/}
@@ -0,0 +1,2 @@
runFormatTest(import.meta, ["typescript"], { bracketSpacing: true });
runFormatTest(import.meta, ["typescript"], { bracketSpacing: false });