Skip to content

Commit 6bb24b2

Browse files
authored
Fix leading comments in mapped types with readonly (Cherry-pick #13427) (#14310)
1 parent 399f2fa commit 6bb24b2

5 files changed

Lines changed: 193 additions & 7 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#### Fix leading comments in mapped types with `readonly` (#13427 by @thorn0, @sosukesuzuki)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
type Type = {
7+
// comment
8+
readonly [key in Foo];
9+
};
10+
11+
// Prettier stable
12+
type Type = {
13+
readonly // comment
14+
[key in Foo];
15+
};
16+
17+
// Prettier main
18+
type Type = {
19+
// comment
20+
readonly [key in Foo];
21+
};
22+
```

src/language-js/print/type-parameters.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
shouldPrintComma,
1313
getFunctionParameters,
1414
isObjectType,
15+
getTypeScriptMappedTypeModifier,
1516
} = require("../utils/index.js");
1617
const { createGroupIdMapper } = require("../../common/util.js");
1718
const { shouldHugType } = require("./type-annotation.js");
@@ -109,6 +110,12 @@ function printTypeParameter(path, options, print) {
109110
const parts = [];
110111
const parent = path.getParentNode();
111112
if (parent.type === "TSMappedType") {
113+
if (parent.readonly) {
114+
parts.push(
115+
getTypeScriptMappedTypeModifier(parent.readonly, "readonly"),
116+
" "
117+
);
118+
}
112119
parts.push("[", print("name"));
113120
if (node.constraint) {
114121
parts.push(" in ", print("constraint"));

src/language-js/print/typescript.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,6 @@ function printTypescript(path, options, print) {
307307
"{",
308308
indent([
309309
options.bracketSpacing ? line : softline,
310-
node.readonly
311-
? [
312-
getTypeScriptMappedTypeModifier(node.readonly, "readonly"),
313-
" ",
314-
]
315-
: "",
316-
printTypeScriptModifiers(path, options, print),
317310
print("typeParameter"),
318311
node.optional
319312
? getTypeScriptMappedTypeModifier(node.optional, "?")

tests/format/typescript/mapped-type/__snapshots__/jsfmt.spec.js.snap

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,119 @@ type Example = {
2222
================================================================================
2323
`;
2424

25+
exports[`issue-11098.ts format 1`] = `
26+
====================================options=====================================
27+
parsers: ["typescript"]
28+
printWidth: 80
29+
| printWidth
30+
=====================================input======================================
31+
type Type = {
32+
// comment
33+
readonly [T in number];
34+
};
35+
36+
type Type = {
37+
// comment1
38+
// comment2
39+
readonly [T in number];
40+
};
41+
42+
type Type = {
43+
// comment
44+
+readonly [T in number];
45+
};
46+
47+
type Type = {
48+
// comment
49+
-readonly [T in number];
50+
};
51+
52+
type Type = {
53+
// comment
54+
+ readonly [T in number];
55+
};
56+
57+
type Type = {
58+
// comment
59+
+readonly [T in number];
60+
};
61+
62+
type Type = {
63+
// comment
64+
readonly [T in number];
65+
};
66+
67+
type Type = {
68+
// comment
69+
[T in number];
70+
};
71+
72+
type Type = {
73+
readonly
74+
// comment
75+
[T in number];
76+
};
77+
78+
type Type = {
79+
readonly // foo
80+
/* bar */ [T in number];
81+
};
82+
83+
=====================================output=====================================
84+
type Type = {
85+
// comment
86+
readonly [T in number];
87+
};
88+
89+
type Type = {
90+
// comment1
91+
// comment2
92+
readonly [T in number];
93+
};
94+
95+
type Type = {
96+
// comment
97+
+readonly [T in number];
98+
};
99+
100+
type Type = {
101+
// comment
102+
-readonly [T in number];
103+
};
104+
105+
type Type = {
106+
// comment
107+
+readonly [T in number];
108+
};
109+
110+
type Type = {
111+
// comment
112+
+readonly [T in number];
113+
};
114+
115+
type Type = {
116+
// comment
117+
readonly [T in number];
118+
};
119+
120+
type Type = {
121+
// comment
122+
[T in number];
123+
};
124+
125+
type Type = {
126+
// comment
127+
readonly [T in number];
128+
};
129+
130+
type Type = {
131+
// foo
132+
/* bar */ readonly [T in number];
133+
};
134+
135+
================================================================================
136+
`;
137+
25138
exports[`mapped-type.ts format 1`] = `
26139
====================================options=====================================
27140
parsers: ["typescript"]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
type Type = {
2+
// comment
3+
readonly [T in number];
4+
};
5+
6+
type Type = {
7+
// comment1
8+
// comment2
9+
readonly [T in number];
10+
};
11+
12+
type Type = {
13+
// comment
14+
+readonly [T in number];
15+
};
16+
17+
type Type = {
18+
// comment
19+
-readonly [T in number];
20+
};
21+
22+
type Type = {
23+
// comment
24+
+ readonly [T in number];
25+
};
26+
27+
type Type = {
28+
// comment
29+
+readonly [T in number];
30+
};
31+
32+
type Type = {
33+
// comment
34+
readonly [T in number];
35+
};
36+
37+
type Type = {
38+
// comment
39+
[T in number];
40+
};
41+
42+
type Type = {
43+
readonly
44+
// comment
45+
[T in number];
46+
};
47+
48+
type Type = {
49+
readonly // foo
50+
/* bar */ [T in number];
51+
};

0 commit comments

Comments
 (0)