-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.ts
317 lines (291 loc) 路 9.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { declare } from "@babel/helper-plugin-utils";
import syntaxFlow from "@babel/plugin-syntax-flow";
import { types as t } from "@babel/core";
import generateCode from "@babel/generator";
import type { NodePath } from "@babel/traverse";
export default declare(api => {
api.assertVersion(7);
function commentFromString(comment: string | t.Comment): t.Comment {
return typeof comment === "string"
? { type: "CommentBlock", value: comment }
: comment;
}
function attachComment({
ofPath,
toPath,
where = "trailing",
optional = false,
comments = generateComment(ofPath, optional),
keepType = false,
}: {
ofPath?: NodePath;
toPath?: NodePath;
where?: t.CommentTypeShorthand;
optional?: boolean;
comments?: string | t.Comment | (string | t.Comment)[];
keepType?: boolean;
}) {
if (!toPath?.node) {
toPath = ofPath.getPrevSibling();
where = "trailing";
}
if (!toPath.node) {
toPath = ofPath.getNextSibling();
where = "leading";
}
if (!toPath.node) {
toPath = ofPath.parentPath;
where = "inner";
}
if (!Array.isArray(comments)) {
comments = [comments];
}
const newComments = comments.map(commentFromString);
if (!keepType && ofPath?.node) {
// Removes the node at `ofPath` while conserving the comments attached
// to it.
const node = ofPath.node;
const parent = ofPath.parentPath;
const prev = ofPath.getPrevSibling();
const next = ofPath.getNextSibling();
const isSingleChild = !(prev.node || next.node);
const leading = node.leadingComments;
const trailing = node.trailingComments;
if (isSingleChild && leading) {
parent.addComments("inner", leading);
}
toPath.addComments(where, newComments);
ofPath.remove();
if (isSingleChild && trailing) {
parent.addComments("inner", trailing);
}
} else {
toPath.addComments(where, newComments);
}
}
function wrapInFlowComment(path: NodePath) {
attachComment({
ofPath: path,
comments: generateComment(
path,
// @ts-ignore
path.parent.optional,
),
});
}
function generateComment(path: NodePath, optional?: boolean | void) {
let comment = path
.getSource()
.replace(/\*-\//g, "*-ESCAPED/")
.replace(/\*\//g, "*-/");
if (optional) comment = "?" + comment;
if (comment[0] !== ":") comment = ":: " + comment;
return comment;
}
function isTypeImport(importKind: "type" | "typeof" | "value") {
return importKind === "type" || importKind === "typeof";
}
return {
name: "transform-flow-comments",
inherits: syntaxFlow,
visitor: {
TypeCastExpression(path) {
const { node } = path;
attachComment({
ofPath: path.get("typeAnnotation"),
toPath: path.get("expression"),
keepType: true,
});
path.replaceWith(t.parenthesizedExpression(node.expression));
},
// support function a(b?) {}
Identifier(path) {
if (path.parentPath.isFlow()) return;
const { node } = path;
if (node.typeAnnotation) {
attachComment({
ofPath: path.get("typeAnnotation"),
toPath: path,
optional:
node.optional ||
// @ts-expect-error Fixme: optional is not in t.TypeAnnotation,
// maybe we can remove it
node.typeAnnotation.optional,
});
if (node.optional) {
node.optional = false;
}
} else if (node.optional) {
attachComment({
toPath: path,
comments: ":: ?",
});
node.optional = false;
}
},
AssignmentPattern: {
exit({ node }) {
const { left } = node;
// @ts-ignore optional is not in ObjectPattern
if (left.optional) {
// @ts-ignore optional is not in ObjectPattern
left.optional = false;
}
},
},
// strip optional property from function params - facebook/fbjs#17
Function(path) {
if (path.isDeclareFunction()) return;
const { node } = path;
if (node.typeParameters) {
attachComment({
ofPath: path.get("typeParameters"),
toPath: path.get("id"),
// @ts-ignore Fixme: optional is not in t.TypeParameterDeclaration
optional: node.typeParameters.optional,
});
}
if (node.returnType) {
attachComment({
ofPath: path.get("returnType"),
toPath: path.get("body"),
where: "leading",
// @ts-ignore Fixme: optional is not in t.TypeAnnotation
optional: node.returnType.typeAnnotation.optional,
});
}
},
// support for `class X { foo: string }` - #4622
ClassProperty(path) {
const { node } = path;
if (!node.value) {
wrapInFlowComment(path);
} else if (node.typeAnnotation) {
attachComment({
ofPath: path.get("typeAnnotation"),
toPath: path.get("key"),
// @ts-ignore Fixme: optional is not in t.TypeAnnotation
optional: node.typeAnnotation.optional,
});
}
},
// support `export type a = {}` - #8 Error: You passed path.replaceWith() a falsy node
ExportNamedDeclaration(path) {
const { node } = path;
if (node.exportKind !== "type" && !t.isFlow(node.declaration)) {
return;
}
wrapInFlowComment(path);
},
// support `import type A` and `import typeof A` #10
ImportDeclaration(path) {
const { node } = path;
if (isTypeImport(node.importKind)) {
wrapInFlowComment(path);
return;
}
const typeSpecifiers = node.specifiers.filter(
specifier =>
specifier.type === "ImportSpecifier" &&
isTypeImport(specifier.importKind),
);
const nonTypeSpecifiers = node.specifiers.filter(
specifier =>
specifier.type !== "ImportSpecifier" ||
!isTypeImport(specifier.importKind),
);
node.specifiers = nonTypeSpecifiers;
if (typeSpecifiers.length > 0) {
const typeImportNode = t.cloneNode(node);
typeImportNode.specifiers = typeSpecifiers;
const comment = `:: ${generateCode(typeImportNode).code}`;
if (nonTypeSpecifiers.length > 0) {
attachComment({ toPath: path, comments: comment });
} else {
attachComment({ ofPath: path, comments: comment });
}
}
},
ObjectPattern(path) {
const { node } = path;
if (node.typeAnnotation) {
attachComment({
ofPath: path.get("typeAnnotation"),
toPath: path,
optional:
// @ts-ignore optional is not in ObjectPattern
node.optional ||
// @ts-ignore Fixme: optional is not in t.TypeAnnotation
node.typeAnnotation.optional,
});
}
},
Flow(
path: NodePath<
t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier
>,
) {
wrapInFlowComment(path);
},
Class(path) {
const { node } = path;
let comments = [];
if (node.typeParameters) {
const typeParameters = path.get("typeParameters");
comments.push(
// @ts-ignore optional is not in TypeParameterDeclaration
generateComment(typeParameters, node.typeParameters.optional),
);
const trailingComments = node.typeParameters.trailingComments;
if (trailingComments) {
comments.push(...trailingComments);
}
typeParameters.remove();
}
if (node.superClass) {
if (comments.length > 0) {
attachComment({
toPath: path.get("id"),
comments: comments,
});
comments = [];
}
if (node.superTypeParameters) {
const superTypeParameters = path.get(
"superTypeParameters",
) as NodePath<t.TypeParameterInstantiation>;
comments.push(
generateComment(
superTypeParameters,
// @ts-ignore optional is not in TypeParameterInstantiation
superTypeParameters.node.optional,
),
);
superTypeParameters.remove();
}
}
if (node.implements) {
const impls = path.get("implements");
const comment =
"implements " +
impls
.map(impl => generateComment(impl).replace(/^:: /, ""))
.join(", ");
delete node["implements"];
if (comments.length === 1) {
comments[0] += ` ${comment}`;
} else {
comments.push(`:: ${comment}`);
}
}
if (comments.length > 0) {
attachComment({
toPath: path.get("body"),
where: "leading",
comments: comments,
});
}
},
},
};
});