Skip to content

Commit cbee737

Browse files
authored
Remove space in empty object type (#19583)
1 parent 6394c73 commit cbee737

10 files changed

Lines changed: 237 additions & 11 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#### Remove space in comment-only object type (#19583 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
var foo = {
7+
/* comment */
8+
};
9+
type Foo = {
10+
/* comment */
11+
};
12+
13+
// Prettier stable
14+
var foo = {/* comment */};
15+
type Foo = { /* comment */ };
16+
17+
// Prettier main
18+
var foo = {/* comment */};
19+
type Foo = {/* comment */};
20+
```

‎src/language-js/print/class-body.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ function printClassBody(path, options, print) {
3939

4040
const [openingBrace, closingBrace] =
4141
isFlowTypeAnnotation && node.exact ? ["{|", "|}"] : "{}";
42+
let isEmpty = true;
4243
let firstMember;
4344

4445
iterateClassMembersPath(path, ({ node, next, isLast }) => {
4546
firstMember ??= node;
47+
isEmpty &&= false;
4648
parts.push(print());
4749

4850
if (!isFlowRecordDeclaration && isObjectType && isFlowTypeAnnotation) {
@@ -83,6 +85,7 @@ function printClassBody(path, options, print) {
8385

8486
// TODO: this part can unify with the similar part in `printObject`
8587
if (node.type === "ObjectTypeAnnotation" && node.inexact) {
88+
isEmpty &&= false;
8689
let printed;
8790
if (hasComment(node, CommentCheckFlags.Dangling)) {
8891
const hasLineComments = hasComment(node, CommentCheckFlags.Line);
@@ -114,7 +117,8 @@ function printClassBody(path, options, print) {
114117
if (parts.length === 0) {
115118
content = openingBrace + closingBrace;
116119
} else {
117-
const spacing = options.bracketSpacing ? line : softline;
120+
const spacing =
121+
!options.bracketSpacing || (isEmpty && !shouldBreak) ? softline : line;
118122
content = [
119123
openingBrace,
120124
indent([spacing, ...parts]),

‎tests/format/flow/object-inexact/comments/__snapshots__/format.test.js.snap‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ T6 = {/* comment */ // comment
4343
};
4444
4545
=====================================output=====================================
46-
type T1 = {| /* comment */ |};
47-
type T12 = { /* comment */ };
46+
type T1 = {|/* comment */|};
47+
type T12 = {/* comment */};
4848
T1 = {/* comment */};
4949
50-
type T2 = {| /* comment */ |};
51-
type T22 = { /* comment */ };
50+
type T2 = {|/* comment */|};
51+
type T22 = {/* comment */};
5252
T2 = {/* comment */};
5353
54-
type T3 = {| /* comment */ |};
55-
type T32 = { /* comment */ };
54+
type T3 = {|/* comment */|};
55+
type T32 = {/* comment */};
5656
T3 = {/* comment */};
5757
5858
type T4 = {|
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`empty.js - {"bracketSpacing":false} format 1`] = `
4+
====================================options=====================================
5+
bracketSpacing: false
6+
parsers: ["flow"]
7+
printWidth: 80 (default) |
8+
=====================================input======================================
9+
a = [];
10+
a = [/*c*/];
11+
type A11 = [];
12+
type A12 = [/*c*/];
13+
14+
o = {};
15+
o = {/*c*/};
16+
type O21 = {};
17+
type O22 = {/*c*/};
18+
type O23 = {...};
19+
type O24 = {/*c*/ ...};
20+
type O31 = {||};
21+
type O32 = {|/*c*/|};
22+
// type O33 = {|...|};
23+
// type O34 = {|/*c*/ ...|};
24+
interface I21 {}
25+
interface I22 {/*c*/}
26+
27+
=====================================output=====================================
28+
a = [];
29+
a = [/*c*/];
30+
type A11 = [];
31+
type A12 = [/*c*/];
32+
33+
o = {};
34+
o = {/*c*/};
35+
type O21 = {};
36+
type O22 = {/*c*/};
37+
type O23 = {...};
38+
type O24 = {/*c*/ ...};
39+
type O31 = {||};
40+
type O32 = {|/*c*/|};
41+
// type O33 = {|...|};
42+
// type O34 = {|/*c*/ ...|};
43+
interface I21 {}
44+
interface I22 {
45+
/*c*/
46+
}
47+
48+
================================================================================
49+
`;
50+
51+
exports[`empty.js - {"bracketSpacing":true} format 1`] = `
52+
====================================options=====================================
53+
bracketSpacing: true
54+
parsers: ["flow"]
55+
printWidth: 80 (default) |
56+
=====================================input======================================
57+
a = [];
58+
a = [/*c*/];
59+
type A11 = [];
60+
type A12 = [/*c*/];
61+
62+
o = {};
63+
o = {/*c*/};
64+
type O21 = {};
65+
type O22 = {/*c*/};
66+
type O23 = {...};
67+
type O24 = {/*c*/ ...};
68+
type O31 = {||};
69+
type O32 = {|/*c*/|};
70+
// type O33 = {|...|};
71+
// type O34 = {|/*c*/ ...|};
72+
interface I21 {}
73+
interface I22 {/*c*/}
74+
75+
=====================================output=====================================
76+
a = [];
77+
a = [/*c*/];
78+
type A11 = [];
79+
type A12 = [/*c*/];
80+
81+
o = {};
82+
o = {/*c*/};
83+
type O21 = {};
84+
type O22 = {/*c*/};
85+
type O23 = { ... };
86+
type O24 = { /*c*/ ... };
87+
type O31 = {||};
88+
type O32 = {|/*c*/|};
89+
// type O33 = {|...|};
90+
// type O34 = {|/*c*/ ...|};
91+
interface I21 {}
92+
interface I22 {
93+
/*c*/
94+
}
95+
96+
================================================================================
97+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
a = [];
2+
a = [/*c*/];
3+
type A11 = [];
4+
type A12 = [/*c*/];
5+
6+
o = {};
7+
o = {/*c*/};
8+
type O21 = {};
9+
type O22 = {/*c*/};
10+
type O23 = {...};
11+
type O24 = {/*c*/ ...};
12+
type O31 = {||};
13+
type O32 = {|/*c*/|};
14+
// type O33 = {|...|};
15+
// type O34 = {|/*c*/ ...|};
16+
interface I21 {}
17+
interface I22 {/*c*/}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
runFormatTest(import.meta, ["flow"], { bracketSpacing: true });
2+
runFormatTest(import.meta, ["flow"], { bracketSpacing: false });

‎tests/format/typescript/class-and-interface/empty/__snapshots__/format.test.js.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ type T2 = {
6363
type T3 = {
6464
//comment
6565
};
66-
type T4 = { /*comment*/ };
67-
type T5 = { /*comment*/ };
68-
type T6 = { /*comment*/ };
69-
type T7 = { /*comment*/ };
66+
type T4 = {/*comment*/};
67+
type T5 = {/*comment*/};
68+
type T6 = {/*comment*/};
69+
type T7 = {/*comment*/};
7070
7171
================================================================================
7272
`;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`empty.ts - {"bracketSpacing":false} format 1`] = `
4+
====================================options=====================================
5+
bracketSpacing: false
6+
parsers: ["typescript"]
7+
printWidth: 80 (default) |
8+
=====================================input======================================
9+
a = [];
10+
a = [/*c*/];
11+
type A11 = [];
12+
type A12 = [/*c*/];
13+
14+
o = {};
15+
o = {/*c*/};
16+
type O21 = {};
17+
type O22 = {/*c*/};
18+
interface I21 {}
19+
interface I22 {/*c*/}
20+
21+
=====================================output=====================================
22+
a = [];
23+
a = [/*c*/];
24+
type A11 = [];
25+
type A12 = [/*c*/];
26+
27+
o = {};
28+
o = {/*c*/};
29+
type O21 = {};
30+
type O22 = {/*c*/};
31+
interface I21 {}
32+
interface I22 {
33+
/*c*/
34+
}
35+
36+
================================================================================
37+
`;
38+
39+
exports[`empty.ts - {"bracketSpacing":true} format 1`] = `
40+
====================================options=====================================
41+
bracketSpacing: true
42+
parsers: ["typescript"]
43+
printWidth: 80 (default) |
44+
=====================================input======================================
45+
a = [];
46+
a = [/*c*/];
47+
type A11 = [];
48+
type A12 = [/*c*/];
49+
50+
o = {};
51+
o = {/*c*/};
52+
type O21 = {};
53+
type O22 = {/*c*/};
54+
interface I21 {}
55+
interface I22 {/*c*/}
56+
57+
=====================================output=====================================
58+
a = [];
59+
a = [/*c*/];
60+
type A11 = [];
61+
type A12 = [/*c*/];
62+
63+
o = {};
64+
o = {/*c*/};
65+
type O21 = {};
66+
type O22 = {/*c*/};
67+
interface I21 {}
68+
interface I22 {
69+
/*c*/
70+
}
71+
72+
================================================================================
73+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
a = [];
2+
a = [/*c*/];
3+
type A11 = [];
4+
type A12 = [/*c*/];
5+
6+
o = {};
7+
o = {/*c*/};
8+
type O21 = {};
9+
type O22 = {/*c*/};
10+
interface I21 {}
11+
interface I22 {/*c*/}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
runFormatTest(import.meta, ["typescript"], { bracketSpacing: true });
2+
runFormatTest(import.meta, ["typescript"], { bracketSpacing: false });

0 commit comments

Comments
 (0)