flow-api-translator 0.37.0 → 0.328.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/TSDefToFlowDef.js +35 -279
- package/dist/TSDefToFlowDef.js.flow +16 -4
- package/dist/flowDefToTSDef.js +51 -377
- package/dist/flowDefToTSDef.js.flow +93 -12
- package/dist/flowImportTo.js +5 -16
- package/dist/flowImportTo.js.flow +3 -3
- package/dist/flowToFlowDef.js +134 -359
- package/dist/flowToFlowDef.js.flow +18 -12
- package/dist/flowToJS.js +3 -10
- package/dist/flowToJS.js.flow +5 -5
- package/dist/index.js +12 -28
- package/dist/index.js.flow +1 -1
- package/dist/src/TSDefToFlowDef.js +35 -279
- package/dist/src/flowDefToTSDef.js +51 -377
- package/dist/src/flowImportTo.js +5 -16
- package/dist/src/flowToFlowDef.js +134 -359
- package/dist/src/flowToJS.js +3 -10
- package/dist/src/index.js +12 -28
- package/dist/src/utils/DocblockUtils.js +4 -4
- package/dist/src/utils/ErrorUtils.js +3 -18
- package/dist/src/utils/FlowAnalyze.js +5 -16
- package/dist/src/utils/TSNodeUtils.js +28 -13
- package/dist/src/utils/TranslationUtils.js +0 -4
- package/dist/utils/DocblockUtils.js +4 -4
- package/dist/utils/DocblockUtils.js.flow +1 -1
- package/dist/utils/ErrorUtils.js +3 -18
- package/dist/utils/ErrorUtils.js.flow +3 -3
- package/dist/utils/FlowAnalyze.js +5 -16
- package/dist/utils/FlowAnalyze.js.flow +3 -3
- package/dist/utils/TSNodeUtils.js +28 -13
- package/dist/utils/TSNodeUtils.js.flow +35 -1
- package/dist/utils/TranslationUtils.js +0 -4
- package/dist/utils/TranslationUtils.js.flow +2 -2
- package/package.json +9 -9
package/dist/flowToFlowDef.js
CHANGED
|
@@ -4,110 +4,79 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = flowToFlowDef;
|
|
7
|
-
|
|
8
|
-
var _hermesTransform = require("hermes-transform");
|
|
9
|
-
|
|
7
|
+
var _flowTransform = require("flow-transform");
|
|
10
8
|
var _FlowAnalyze = require("./utils/FlowAnalyze");
|
|
11
|
-
|
|
12
9
|
var _TranslationUtils = require("./utils/TranslationUtils");
|
|
13
|
-
|
|
14
10
|
var _ErrorUtils = require("./utils/ErrorUtils");
|
|
15
|
-
|
|
16
|
-
var _hermesEstree = require("hermes-estree");
|
|
17
|
-
|
|
11
|
+
var _flowEstree = require("flow-estree");
|
|
18
12
|
const EMPTY_TRANSLATION_RESULT = [null, []];
|
|
19
|
-
|
|
20
13
|
function convertArray(items, convert) {
|
|
21
14
|
const resultItems = [];
|
|
22
15
|
const deps = [];
|
|
23
|
-
|
|
24
16
|
for (const item of items) {
|
|
25
17
|
const [resultItem, itemDeps] = convert(item);
|
|
26
|
-
|
|
27
18
|
if (resultItem != null) {
|
|
28
19
|
resultItems.push(resultItem);
|
|
29
20
|
deps.push(...itemDeps);
|
|
30
21
|
}
|
|
31
22
|
}
|
|
32
|
-
|
|
33
23
|
return [resultItems, deps];
|
|
34
24
|
}
|
|
35
|
-
|
|
36
25
|
function getTopLevelStatement(node, context) {
|
|
37
26
|
let currentNode = node;
|
|
38
|
-
|
|
39
27
|
while (currentNode != null) {
|
|
40
28
|
var _currentNode$parent;
|
|
41
|
-
|
|
42
29
|
if (((_currentNode$parent = currentNode.parent) == null ? void 0 : _currentNode$parent.type) === 'Program') {
|
|
43
30
|
return currentNode;
|
|
44
31
|
}
|
|
45
|
-
|
|
46
32
|
currentNode = currentNode.parent;
|
|
47
33
|
}
|
|
48
|
-
|
|
49
34
|
throw (0, _ErrorUtils.translationError)(node, `getTopLevelStatement: Detached node of type "${node.type}" passed`, context);
|
|
50
35
|
}
|
|
51
|
-
|
|
52
36
|
function transferProgramStatementProperties(stmt, orgStmt) {
|
|
53
37
|
stmt.comments = orgStmt.comments;
|
|
54
38
|
stmt.range = orgStmt.range;
|
|
55
39
|
stmt.loc = orgStmt.loc;
|
|
56
40
|
}
|
|
57
|
-
|
|
58
41
|
function flowToFlowDef(ast, code, scopeManager, opts) {
|
|
59
42
|
var _ast$interpreter$valu, _ast$interpreter;
|
|
60
|
-
|
|
61
43
|
const context = (0, _TranslationUtils.createTranslationContext)(code, scopeManager, opts);
|
|
62
44
|
const translatedStatements = new Map();
|
|
63
|
-
|
|
64
45
|
function storeTranslatedStatement(stmt, orgStmt) {
|
|
65
46
|
translatedStatements.set(orgStmt, stmt);
|
|
66
47
|
}
|
|
67
|
-
|
|
68
48
|
const seenDeps = new Set();
|
|
69
49
|
const processedStatements = new Set();
|
|
70
50
|
const pendingStatements = new Set();
|
|
71
|
-
|
|
72
51
|
function updatePendingStatements(deps) {
|
|
73
52
|
for (const dep of deps) {
|
|
74
53
|
if (seenDeps.has(dep)) {
|
|
75
54
|
continue;
|
|
76
55
|
}
|
|
77
|
-
|
|
78
56
|
seenDeps.add(dep);
|
|
79
57
|
const variable = context.variableMap.get(dep);
|
|
80
|
-
|
|
81
58
|
if (variable == null) {
|
|
82
59
|
throw new Error(`updatePendingStatements: Variable for dependency "${dep}" not found`);
|
|
83
60
|
}
|
|
84
|
-
|
|
85
61
|
for (const def of variable.defs) {
|
|
86
62
|
const stmt = def.node;
|
|
87
|
-
|
|
88
63
|
if (stmt == null) {
|
|
89
64
|
throw new Error(`updatePendingStatements: Variable parent of "${dep}" not found`);
|
|
90
65
|
}
|
|
91
|
-
|
|
92
66
|
const topLevelStmt = getTopLevelStatement(stmt, context);
|
|
93
|
-
|
|
94
67
|
if (processedStatements.has(topLevelStmt)) {
|
|
95
68
|
continue;
|
|
96
69
|
}
|
|
97
|
-
|
|
98
70
|
pendingStatements.add(topLevelStmt);
|
|
99
71
|
}
|
|
100
72
|
}
|
|
101
73
|
}
|
|
102
|
-
|
|
103
74
|
function updateProcessedStatement(stmt) {
|
|
104
75
|
processedStatements.add(stmt);
|
|
105
76
|
pendingStatements.delete(stmt);
|
|
106
77
|
}
|
|
107
|
-
|
|
108
78
|
for (const stmt of ast.body) {
|
|
109
79
|
const resultExport = convertExport(stmt, context);
|
|
110
|
-
|
|
111
80
|
if (resultExport != null) {
|
|
112
81
|
updateProcessedStatement(stmt);
|
|
113
82
|
const [resultExportedStmt, deps] = resultExport;
|
|
@@ -115,33 +84,26 @@ function flowToFlowDef(ast, code, scopeManager, opts) {
|
|
|
115
84
|
updatePendingStatements(deps);
|
|
116
85
|
}
|
|
117
86
|
}
|
|
118
|
-
|
|
119
87
|
while (pendingStatements.size > 0) {
|
|
120
88
|
const stmt = pendingStatements.values().next().value;
|
|
121
|
-
|
|
122
89
|
if (stmt == null) {
|
|
123
90
|
throw new Error(`flowToFlowDef: Invalid state, "pendingStatements" cannot be empty`);
|
|
124
91
|
}
|
|
125
|
-
|
|
126
92
|
updateProcessedStatement(stmt);
|
|
127
93
|
const [resultStmt, deps] = convertStatement(stmt, context);
|
|
128
94
|
storeTranslatedStatement(resultStmt, stmt);
|
|
129
95
|
updatePendingStatements(deps);
|
|
130
96
|
}
|
|
131
|
-
|
|
132
97
|
const translatedBody = [];
|
|
133
|
-
|
|
134
98
|
for (const stmt of ast.body) {
|
|
135
99
|
const translatedStatement = translatedStatements.get(stmt);
|
|
136
|
-
|
|
137
100
|
if (translatedStatement != null) {
|
|
138
101
|
const optimizedStatement = stripUnusedDefs(translatedStatement, seenDeps, context);
|
|
139
102
|
transferProgramStatementProperties(optimizedStatement, stmt);
|
|
140
103
|
translatedBody.push(optimizedStatement);
|
|
141
104
|
}
|
|
142
105
|
}
|
|
143
|
-
|
|
144
|
-
return [_hermesTransform.t.Program({
|
|
106
|
+
return [_flowTransform.t.Program({
|
|
145
107
|
body: translatedBody,
|
|
146
108
|
sourceType: ast.sourceType,
|
|
147
109
|
interpreter: (_ast$interpreter$valu = (_ast$interpreter = ast.interpreter) == null ? void 0 : _ast$interpreter.value) != null ? _ast$interpreter$valu : null,
|
|
@@ -150,93 +112,76 @@ function flowToFlowDef(ast, code, scopeManager, opts) {
|
|
|
150
112
|
docblock: ast.docblock
|
|
151
113
|
}), code];
|
|
152
114
|
}
|
|
153
|
-
|
|
154
115
|
function convertExport(stmt, context) {
|
|
155
116
|
switch (stmt.type) {
|
|
156
117
|
case 'ExportNamedDeclaration':
|
|
157
118
|
{
|
|
158
119
|
return convertExportNamedDeclaration(stmt, context);
|
|
159
120
|
}
|
|
160
|
-
|
|
161
121
|
case 'ExportDefaultDeclaration':
|
|
162
122
|
{
|
|
163
123
|
return convertExportDefaultDeclaration(stmt, context);
|
|
164
124
|
}
|
|
165
|
-
|
|
166
125
|
case 'ExportAllDeclaration':
|
|
167
126
|
case 'DeclareExportAllDeclaration':
|
|
168
127
|
{
|
|
169
|
-
return [(0,
|
|
128
|
+
return [(0, _flowTransform.asDetachedNode)(stmt), []];
|
|
170
129
|
}
|
|
171
|
-
|
|
172
130
|
case 'DeclareExportDeclaration':
|
|
173
131
|
case 'DeclareModuleExports':
|
|
174
132
|
{
|
|
175
|
-
return [(0,
|
|
133
|
+
return [(0, _flowTransform.asDetachedNode)(stmt), (0, _FlowAnalyze.analyzeTypeDependencies)(stmt, context)];
|
|
176
134
|
}
|
|
177
|
-
|
|
178
135
|
case 'ExpressionStatement':
|
|
179
136
|
{
|
|
180
137
|
const expr = stmt.expression;
|
|
181
|
-
|
|
182
138
|
if (expr.type === 'AssignmentExpression' && expr.left.type === 'MemberExpression') {
|
|
183
139
|
const member = expr.left;
|
|
184
|
-
|
|
185
140
|
if (member.object.type === 'Identifier' && member.object.name === 'exports' || member.object.type === 'MemberExpression' && member.object.object.type === 'Identifier' && member.object.object.name === 'module' && member.object.property.type === 'Identifier' && member.object.property.name === 'exports') {
|
|
186
141
|
throw (0, _ErrorUtils.translationError)(stmt, `convertExport: Named CommonJS exports not supported. Use either \`module.exports = {...}\` or ES6 exports.`, context);
|
|
187
142
|
}
|
|
188
|
-
|
|
189
143
|
if (member.object.type === 'Identifier' && member.object.name === 'module' && member.property.type === 'Identifier' && member.property.name === 'exports') {
|
|
190
144
|
const [typeAnnotation, deps] = convertExpressionToTypeAnnotation(expr.right, context);
|
|
191
|
-
return [
|
|
192
|
-
typeAnnotation:
|
|
145
|
+
return [_flowTransform.t.DeclareModuleExports({
|
|
146
|
+
typeAnnotation: _flowTransform.t.TypeAnnotation({
|
|
193
147
|
typeAnnotation
|
|
194
148
|
})
|
|
195
149
|
}), deps];
|
|
196
150
|
}
|
|
197
151
|
}
|
|
198
|
-
|
|
199
152
|
return null;
|
|
200
153
|
}
|
|
201
|
-
|
|
202
154
|
default:
|
|
203
155
|
{
|
|
204
156
|
return null;
|
|
205
157
|
}
|
|
206
158
|
}
|
|
207
159
|
}
|
|
208
|
-
|
|
209
160
|
function stripUnusedDefs(detachedStmt, usedDeps, context) {
|
|
210
161
|
const stmt = detachedStmt;
|
|
211
|
-
|
|
212
162
|
switch (stmt.type) {
|
|
213
163
|
case 'ImportDeclaration':
|
|
214
164
|
{
|
|
215
165
|
const resultSpecfiers = stmt.specifiers.filter(spec => usedDeps.has(spec.local.name));
|
|
216
|
-
|
|
217
166
|
if (resultSpecfiers.length === 0) {
|
|
218
167
|
throw (0, _ErrorUtils.translationError)(stmt, `stripUnusedDefs ImportDeclaration: No specifiers remaining`, context);
|
|
219
168
|
}
|
|
220
|
-
|
|
221
169
|
if (resultSpecfiers.length !== stmt.specifiers.length) {
|
|
222
|
-
return
|
|
170
|
+
return _flowTransform.t.ImportDeclaration({
|
|
223
171
|
specifiers: resultSpecfiers,
|
|
224
172
|
importKind: stmt.importKind,
|
|
225
173
|
source: stmt.source,
|
|
226
174
|
attributes: stmt.attributes
|
|
227
175
|
});
|
|
228
176
|
}
|
|
229
|
-
|
|
230
177
|
return detachedStmt;
|
|
231
178
|
}
|
|
232
|
-
|
|
233
179
|
default:
|
|
234
180
|
{
|
|
235
181
|
return detachedStmt;
|
|
236
182
|
}
|
|
237
183
|
}
|
|
238
184
|
}
|
|
239
|
-
|
|
240
185
|
function convertStatement(stmt, context) {
|
|
241
186
|
switch (stmt.type) {
|
|
242
187
|
case 'ComponentDeclaration':
|
|
@@ -244,61 +189,50 @@ function convertStatement(stmt, context) {
|
|
|
244
189
|
const [result, deps] = convertComponentDeclaration(stmt, context);
|
|
245
190
|
return [result, deps];
|
|
246
191
|
}
|
|
247
|
-
|
|
248
192
|
case 'HookDeclaration':
|
|
249
193
|
{
|
|
250
194
|
const [result, deps] = convertHookDeclaration(stmt, context);
|
|
251
195
|
return [result, deps];
|
|
252
196
|
}
|
|
253
|
-
|
|
254
197
|
case 'FunctionDeclaration':
|
|
255
198
|
{
|
|
256
199
|
const [result, deps] = convertFunctionDeclaration(stmt, context);
|
|
257
200
|
return [result, deps];
|
|
258
201
|
}
|
|
259
|
-
|
|
260
202
|
case 'ClassDeclaration':
|
|
261
203
|
{
|
|
262
204
|
const [result, deps] = convertClassDeclaration(stmt, context);
|
|
263
205
|
return [result, deps];
|
|
264
206
|
}
|
|
265
|
-
|
|
266
207
|
case 'InterfaceDeclaration':
|
|
267
208
|
{
|
|
268
209
|
const [result, deps] = convertInterfaceDeclaration(stmt, context);
|
|
269
210
|
return [result, deps];
|
|
270
211
|
}
|
|
271
|
-
|
|
272
212
|
case 'TypeAlias':
|
|
273
213
|
{
|
|
274
214
|
const [result, deps] = convertTypeAlias(stmt, context);
|
|
275
215
|
return [result, deps];
|
|
276
216
|
}
|
|
277
|
-
|
|
278
217
|
case 'OpaqueType':
|
|
279
218
|
{
|
|
280
219
|
const [result, deps] = convertOpaqueType(stmt, context);
|
|
281
220
|
return [result, deps];
|
|
282
221
|
}
|
|
283
|
-
|
|
284
222
|
case 'ImportDeclaration':
|
|
285
223
|
{
|
|
286
224
|
const [result, deps] = convertImportDeclaration(stmt, context);
|
|
287
225
|
return [result, deps];
|
|
288
226
|
}
|
|
289
|
-
|
|
290
227
|
case 'VariableDeclaration':
|
|
291
228
|
{
|
|
292
229
|
const requireImport = convertRequireToImport(stmt);
|
|
293
|
-
|
|
294
230
|
if (requireImport != null) {
|
|
295
231
|
return requireImport;
|
|
296
232
|
}
|
|
297
|
-
|
|
298
233
|
const [result, deps] = convertVariableDeclaration(stmt, context);
|
|
299
234
|
return [result, deps];
|
|
300
235
|
}
|
|
301
|
-
|
|
302
236
|
case 'DeclareClass':
|
|
303
237
|
case 'DeclareVariable':
|
|
304
238
|
case 'DeclareFunction':
|
|
@@ -309,16 +243,14 @@ function convertStatement(stmt, context) {
|
|
|
309
243
|
case 'DeclareOpaqueType':
|
|
310
244
|
case 'EnumDeclaration':
|
|
311
245
|
{
|
|
312
|
-
return [(0,
|
|
246
|
+
return [(0, _flowTransform.asDetachedNode)(stmt), (0, _FlowAnalyze.analyzeTypeDependencies)(stmt, context)];
|
|
313
247
|
}
|
|
314
|
-
|
|
315
248
|
default:
|
|
316
249
|
{
|
|
317
250
|
throw (0, _ErrorUtils.translationError)(stmt, `Statement: Unsupported statement type of "${stmt.type}"`, context);
|
|
318
251
|
}
|
|
319
252
|
}
|
|
320
253
|
}
|
|
321
|
-
|
|
322
254
|
function convertExpressionToTypeAnnotation(expr, context) {
|
|
323
255
|
switch (expr.type) {
|
|
324
256
|
case 'AsExpression':
|
|
@@ -326,106 +258,91 @@ function convertExpressionToTypeAnnotation(expr, context) {
|
|
|
326
258
|
const [resultExpr, deps] = convertAsExpression(expr, context);
|
|
327
259
|
return [resultExpr, deps];
|
|
328
260
|
}
|
|
329
|
-
|
|
330
261
|
case 'TypeCastExpression':
|
|
331
262
|
{
|
|
332
263
|
const [resultExpr, deps] = convertTypeCastExpression(expr, context);
|
|
333
264
|
return [resultExpr, deps];
|
|
334
265
|
}
|
|
335
|
-
|
|
336
266
|
case 'Identifier':
|
|
337
267
|
{
|
|
338
|
-
return [
|
|
339
|
-
argument:
|
|
268
|
+
return [_flowTransform.t.TypeofTypeAnnotation({
|
|
269
|
+
argument: _flowTransform.t.Identifier({
|
|
340
270
|
name: expr.name
|
|
341
271
|
})
|
|
342
272
|
}), (0, _FlowAnalyze.analyzeTypeDependencies)(expr, context)];
|
|
343
273
|
}
|
|
344
|
-
|
|
345
274
|
case 'Literal':
|
|
346
275
|
{
|
|
347
276
|
const [resultExpr, deps] = convertLiteral(expr, context);
|
|
348
277
|
return [resultExpr, deps];
|
|
349
278
|
}
|
|
350
|
-
|
|
351
279
|
case 'ObjectExpression':
|
|
352
280
|
{
|
|
353
281
|
const [resultExpr, deps] = convertObjectExpression(expr, context);
|
|
354
282
|
return [resultExpr, deps];
|
|
355
283
|
}
|
|
356
|
-
|
|
357
284
|
case 'ArrowFunctionExpression':
|
|
358
285
|
case 'FunctionExpression':
|
|
359
286
|
{
|
|
360
287
|
const [resultExpr, deps] = convertAFunction(expr, context);
|
|
361
288
|
return [resultExpr, deps];
|
|
362
289
|
}
|
|
363
|
-
|
|
364
290
|
case 'MemberExpression':
|
|
365
291
|
{
|
|
366
|
-
return [
|
|
292
|
+
return [_flowTransform.t.TypeofTypeAnnotation({
|
|
367
293
|
argument: convertExpressionToTypeofIdentifier(expr, context)
|
|
368
294
|
}), (0, _FlowAnalyze.analyzeTypeDependencies)(expr, context)];
|
|
369
295
|
}
|
|
370
|
-
|
|
371
296
|
default:
|
|
372
297
|
{
|
|
373
298
|
return [(0, _ErrorUtils.flowFixMeOrError)(expr, `convertExpressionToTypeAnnotation: Unsupported expression of type "${expr.type}", a type annotation is required.`, context), []];
|
|
374
299
|
}
|
|
375
300
|
}
|
|
376
301
|
}
|
|
377
|
-
|
|
378
302
|
function inheritComments(fromNode, toNode) {
|
|
379
303
|
toNode.comments = fromNode.comments;
|
|
380
304
|
return toNode;
|
|
381
305
|
}
|
|
382
|
-
|
|
383
306
|
function convertObjectExpression(expr, context) {
|
|
384
307
|
const [resultProperties, deps] = convertArray(expr.properties, prop => {
|
|
385
308
|
switch (prop.type) {
|
|
386
309
|
case 'SpreadElement':
|
|
387
310
|
{
|
|
388
311
|
const [resultExpr, deps] = convertExpressionToTypeAnnotation(prop.argument, context);
|
|
389
|
-
return [
|
|
312
|
+
return [_flowTransform.t.ObjectTypeSpreadProperty({
|
|
390
313
|
argument: resultExpr
|
|
391
314
|
}), deps];
|
|
392
315
|
}
|
|
393
|
-
|
|
394
316
|
case 'Property':
|
|
395
317
|
{
|
|
396
|
-
if (!(0,
|
|
318
|
+
if (!(0, _flowEstree.isIdentifier)(prop.key) && !(0, _flowEstree.isStringLiteral)(prop.key) && !(0, _flowEstree.isNumericLiteral)(prop.key)) {
|
|
397
319
|
throw (0, _ErrorUtils.translationError)(prop.key, `ObjectExpression Property: Unsupported key type of "${prop.key.type}"`, context);
|
|
398
320
|
}
|
|
399
|
-
|
|
400
321
|
if (prop.method === true) {
|
|
401
322
|
if (prop.value.type !== 'ArrowFunctionExpression' && prop.value.type !== 'FunctionExpression') {
|
|
402
323
|
throw (0, _ErrorUtils.translationError)(prop.key, `ObjectExpression Property: Expected method to have a function value, but got ${prop.value.type}`, context);
|
|
403
324
|
}
|
|
404
|
-
|
|
405
325
|
const [resultExpr, deps] = convertAFunction(prop.value, context);
|
|
406
|
-
return [inheritComments(prop,
|
|
407
|
-
key: (0,
|
|
326
|
+
return [inheritComments(prop, _flowTransform.t.ObjectTypeMethodSignature({
|
|
327
|
+
key: (0, _flowTransform.asDetachedNode)(prop.key),
|
|
408
328
|
value: resultExpr
|
|
409
329
|
})), deps];
|
|
410
330
|
}
|
|
411
|
-
|
|
412
331
|
if (prop.kind === 'get' || prop.kind === 'set') {
|
|
413
332
|
if (prop.value.type !== 'ArrowFunctionExpression' && prop.value.type !== 'FunctionExpression') {
|
|
414
333
|
throw (0, _ErrorUtils.translationError)(prop.key, `ObjectExpression Property: Expected accessor to have a function value, but got ${prop.value.type}`, context);
|
|
415
334
|
}
|
|
416
|
-
|
|
417
335
|
const kind = prop.kind;
|
|
418
336
|
const [resultExpr, deps] = convertAFunction(prop.value, context);
|
|
419
|
-
return [inheritComments(prop,
|
|
420
|
-
key: (0,
|
|
337
|
+
return [inheritComments(prop, _flowTransform.t.ObjectTypeAccessorSignature({
|
|
338
|
+
key: (0, _flowTransform.asDetachedNode)(prop.key),
|
|
421
339
|
kind,
|
|
422
340
|
value: resultExpr
|
|
423
341
|
})), deps];
|
|
424
342
|
}
|
|
425
|
-
|
|
426
343
|
const [resultExpr, deps] = convertExpressionToTypeAnnotation(prop.value, context);
|
|
427
|
-
return [inheritComments(prop,
|
|
428
|
-
key: (0,
|
|
344
|
+
return [inheritComments(prop, _flowTransform.t.ObjectTypePropertySignature({
|
|
345
|
+
key: (0, _flowTransform.asDetachedNode)(prop.key),
|
|
429
346
|
value: resultExpr,
|
|
430
347
|
optional: false,
|
|
431
348
|
variance: null
|
|
@@ -433,7 +350,7 @@ function convertObjectExpression(expr, context) {
|
|
|
433
350
|
}
|
|
434
351
|
}
|
|
435
352
|
});
|
|
436
|
-
return [
|
|
353
|
+
return [_flowTransform.t.ObjectTypeAnnotation({
|
|
437
354
|
inexact: false,
|
|
438
355
|
exact: false,
|
|
439
356
|
properties: resultProperties,
|
|
@@ -442,480 +359,406 @@ function convertObjectExpression(expr, context) {
|
|
|
442
359
|
internalSlots: []
|
|
443
360
|
}), deps];
|
|
444
361
|
}
|
|
445
|
-
|
|
446
362
|
function convertLiteral(expr, context) {
|
|
447
363
|
switch (expr.literalType) {
|
|
448
364
|
case 'bigint':
|
|
449
365
|
{
|
|
450
|
-
return [
|
|
366
|
+
return [_flowTransform.t.BigIntLiteralTypeAnnotation({
|
|
451
367
|
raw: expr.raw
|
|
452
368
|
}), []];
|
|
453
369
|
}
|
|
454
|
-
|
|
455
370
|
case 'boolean':
|
|
456
371
|
{
|
|
457
|
-
return [
|
|
372
|
+
return [_flowTransform.t.BooleanLiteralTypeAnnotation({
|
|
458
373
|
raw: expr.raw,
|
|
459
374
|
value: expr.value
|
|
460
375
|
}), []];
|
|
461
376
|
}
|
|
462
|
-
|
|
463
377
|
case 'null':
|
|
464
378
|
{
|
|
465
|
-
return [
|
|
379
|
+
return [_flowTransform.t.NullLiteralTypeAnnotation({}), []];
|
|
466
380
|
}
|
|
467
|
-
|
|
468
381
|
case 'numeric':
|
|
469
382
|
{
|
|
470
|
-
return [
|
|
383
|
+
return [_flowTransform.t.NumberLiteralTypeAnnotation({
|
|
471
384
|
raw: expr.raw,
|
|
472
385
|
value: expr.value
|
|
473
386
|
}), []];
|
|
474
387
|
}
|
|
475
|
-
|
|
476
388
|
case 'string':
|
|
477
389
|
{
|
|
478
|
-
return [
|
|
390
|
+
return [_flowTransform.t.StringLiteralTypeAnnotation({
|
|
479
391
|
raw: expr.raw,
|
|
480
392
|
value: expr.value
|
|
481
393
|
}), []];
|
|
482
394
|
}
|
|
483
|
-
|
|
484
395
|
case 'regexp':
|
|
485
396
|
{
|
|
486
|
-
return [
|
|
487
|
-
id:
|
|
397
|
+
return [_flowTransform.t.GenericTypeAnnotation({
|
|
398
|
+
id: _flowTransform.t.Identifier({
|
|
488
399
|
name: 'RegExp'
|
|
489
400
|
})
|
|
490
401
|
}), []];
|
|
491
402
|
}
|
|
492
|
-
|
|
493
403
|
default:
|
|
494
404
|
{
|
|
495
405
|
throw (0, _ErrorUtils.translationError)(expr, 'convertLiteral: Unsupported literal type.', context);
|
|
496
406
|
}
|
|
497
407
|
}
|
|
498
408
|
}
|
|
499
|
-
|
|
500
409
|
function convertExportDeclaration(decl, opts, context) {
|
|
501
410
|
switch (decl.type) {
|
|
502
411
|
case 'ComponentDeclaration':
|
|
503
412
|
{
|
|
504
413
|
const [declDecl, deps] = convertComponentDeclaration(decl, context);
|
|
505
|
-
return [opts.default ?
|
|
414
|
+
return [opts.default ? _flowTransform.t.DeclareExportDefaultDeclaration({
|
|
506
415
|
declaration: declDecl
|
|
507
|
-
}) :
|
|
416
|
+
}) : _flowTransform.t.DeclareExportDeclarationNamedWithDeclaration({
|
|
508
417
|
declaration: declDecl
|
|
509
418
|
}), deps];
|
|
510
419
|
}
|
|
511
|
-
|
|
512
420
|
case 'HookDeclaration':
|
|
513
421
|
{
|
|
514
422
|
const [declDecl, deps] = convertHookDeclaration(decl, context);
|
|
515
|
-
return [opts.default ?
|
|
423
|
+
return [opts.default ? _flowTransform.t.DeclareExportDefaultDeclaration({
|
|
516
424
|
declaration: declDecl
|
|
517
|
-
}) :
|
|
425
|
+
}) : _flowTransform.t.DeclareExportDeclarationNamedWithDeclaration({
|
|
518
426
|
declaration: declDecl
|
|
519
427
|
}), deps];
|
|
520
428
|
}
|
|
521
|
-
|
|
522
429
|
case 'FunctionDeclaration':
|
|
523
430
|
{
|
|
524
431
|
const [declDecl, deps] = convertFunctionDeclaration(decl, context);
|
|
525
|
-
return [opts.default ?
|
|
432
|
+
return [opts.default ? _flowTransform.t.DeclareExportDefaultDeclaration({
|
|
526
433
|
declaration: declDecl
|
|
527
|
-
}) :
|
|
434
|
+
}) : _flowTransform.t.DeclareExportDeclarationNamedWithDeclaration({
|
|
528
435
|
declaration: declDecl
|
|
529
436
|
}), deps];
|
|
530
437
|
}
|
|
531
|
-
|
|
532
438
|
case 'ClassDeclaration':
|
|
533
439
|
{
|
|
534
440
|
const [declDecl, deps] = convertClassDeclaration(decl, context);
|
|
535
|
-
return [opts.default ?
|
|
441
|
+
return [opts.default ? _flowTransform.t.DeclareExportDefaultDeclaration({
|
|
536
442
|
declaration: declDecl
|
|
537
|
-
}) :
|
|
443
|
+
}) : _flowTransform.t.DeclareExportDeclarationNamedWithDeclaration({
|
|
538
444
|
declaration: declDecl
|
|
539
445
|
}), deps];
|
|
540
446
|
}
|
|
541
|
-
|
|
542
447
|
case 'InterfaceDeclaration':
|
|
543
448
|
{
|
|
544
449
|
if (opts.default) {
|
|
545
450
|
throw (0, _ErrorUtils.translationError)(decl, 'ExportDeclaration: Default interface found, invalid AST.', context);
|
|
546
451
|
}
|
|
547
|
-
|
|
548
452
|
const [declDecl, deps] = convertInterfaceDeclaration(decl, context);
|
|
549
|
-
return [
|
|
453
|
+
return [_flowTransform.t.ExportNamedDeclarationWithDeclaration({
|
|
550
454
|
exportKind: 'type',
|
|
551
455
|
declaration: declDecl
|
|
552
456
|
}), deps];
|
|
553
457
|
}
|
|
554
|
-
|
|
555
458
|
case 'TypeAlias':
|
|
556
459
|
{
|
|
557
460
|
if (opts.default) {
|
|
558
461
|
throw (0, _ErrorUtils.translationError)(decl, 'ExportDeclaration: Default type alias found, invalid AST.', context);
|
|
559
462
|
}
|
|
560
|
-
|
|
561
463
|
const [declDecl, deps] = convertTypeAlias(decl, context);
|
|
562
|
-
return [
|
|
464
|
+
return [_flowTransform.t.ExportNamedDeclarationWithDeclaration({
|
|
563
465
|
exportKind: 'type',
|
|
564
466
|
declaration: declDecl
|
|
565
467
|
}), deps];
|
|
566
468
|
}
|
|
567
|
-
|
|
568
469
|
case 'OpaqueType':
|
|
569
470
|
{
|
|
570
471
|
if (opts.default) {
|
|
571
472
|
throw (0, _ErrorUtils.translationError)(decl, 'ExportDeclaration: Default opaque type found, invalid AST.', context);
|
|
572
473
|
}
|
|
573
|
-
|
|
574
474
|
const [declDecl, deps] = convertOpaqueType(decl, context);
|
|
575
|
-
return [
|
|
475
|
+
return [_flowTransform.t.DeclareExportDeclarationNamedWithDeclaration({
|
|
576
476
|
declaration: declDecl
|
|
577
477
|
}), deps];
|
|
578
478
|
}
|
|
579
|
-
|
|
580
479
|
case 'VariableDeclaration':
|
|
581
480
|
{
|
|
582
481
|
if (opts.default) {
|
|
583
482
|
throw (0, _ErrorUtils.translationError)(decl, 'ExportDeclaration: Default VariableDeclaration found, invalid AST.', context);
|
|
584
483
|
}
|
|
585
|
-
|
|
586
484
|
const [declDecl, deps] = convertVariableDeclaration(decl, context);
|
|
587
|
-
return [
|
|
485
|
+
return [_flowTransform.t.DeclareExportDeclarationNamedWithDeclaration({
|
|
588
486
|
declaration: declDecl
|
|
589
487
|
}), deps];
|
|
590
488
|
}
|
|
591
|
-
|
|
592
489
|
case 'EnumDeclaration':
|
|
593
490
|
{
|
|
594
|
-
return [
|
|
491
|
+
return [_flowTransform.t.ExportNamedDeclarationWithDeclaration({
|
|
595
492
|
exportKind: 'value',
|
|
596
|
-
declaration: (0,
|
|
493
|
+
declaration: (0, _flowTransform.asDetachedNode)(decl)
|
|
597
494
|
}), []];
|
|
598
495
|
}
|
|
599
|
-
|
|
600
496
|
default:
|
|
601
497
|
{
|
|
602
|
-
if ((0,
|
|
498
|
+
if ((0, _flowEstree.isExpression)(decl)) {
|
|
603
499
|
if (!opts.default) {
|
|
604
500
|
throw (0, _ErrorUtils.translationError)(decl, 'ExportDeclaration: Non default expression found, invalid AST.', context);
|
|
605
501
|
}
|
|
606
|
-
|
|
607
502
|
const [declDecl, deps] = convertExpressionToTypeAnnotation(decl, context);
|
|
608
|
-
return [
|
|
503
|
+
return [_flowTransform.t.DeclareExportDefaultDeclaration({
|
|
609
504
|
declaration: declDecl
|
|
610
505
|
}), deps];
|
|
611
506
|
}
|
|
612
|
-
|
|
613
507
|
throw (0, _ErrorUtils.translationError)(decl, `ExportDeclaration: Unsupported declaration of type "${decl.type}".`, context);
|
|
614
508
|
}
|
|
615
509
|
}
|
|
616
510
|
}
|
|
617
|
-
|
|
618
511
|
function convertExportDefaultDeclaration(stmt, context) {
|
|
619
512
|
const expr = stmt.declaration;
|
|
620
|
-
|
|
621
|
-
if ((0, _hermesEstree.isExpression)(expr) && expr.type === 'Identifier') {
|
|
513
|
+
if ((0, _flowEstree.isExpression)(expr) && expr.type === 'Identifier') {
|
|
622
514
|
const name = expr.name;
|
|
623
|
-
const [declDecl, deps] = [
|
|
624
|
-
argument:
|
|
515
|
+
const [declDecl, deps] = [_flowTransform.t.TypeofTypeAnnotation({
|
|
516
|
+
argument: _flowTransform.t.Identifier({
|
|
625
517
|
name
|
|
626
518
|
})
|
|
627
519
|
}), (0, _FlowAnalyze.analyzeTypeDependencies)(expr, context)];
|
|
628
|
-
return [
|
|
520
|
+
return [_flowTransform.t.DeclareExportDefaultDeclaration({
|
|
629
521
|
declaration: declDecl
|
|
630
522
|
}), deps];
|
|
631
523
|
}
|
|
632
|
-
|
|
633
524
|
return convertExportDeclaration(stmt.declaration, {
|
|
634
525
|
default: true
|
|
635
526
|
}, context);
|
|
636
527
|
}
|
|
637
|
-
|
|
638
528
|
function convertExportNamedDeclaration(stmt, context) {
|
|
639
529
|
const decl = stmt.declaration;
|
|
640
|
-
|
|
641
530
|
if (decl != null) {
|
|
642
531
|
return convertExportDeclaration(decl, {
|
|
643
532
|
default: false
|
|
644
533
|
}, context);
|
|
645
534
|
}
|
|
646
|
-
|
|
647
|
-
const resultSpecfiers = stmt.specifiers.map(spec => (0, _hermesTransform.asDetachedNode)(spec));
|
|
535
|
+
const resultSpecfiers = stmt.specifiers.map(spec => (0, _flowTransform.asDetachedNode)(spec));
|
|
648
536
|
const specifiersDeps = stmt.source != null ? [] : stmt.specifiers.flatMap(({
|
|
649
537
|
local
|
|
650
538
|
}) => (0, _FlowAnalyze.analyzeTypeDependencies)(local, context));
|
|
651
|
-
return [
|
|
539
|
+
return [_flowTransform.t.ExportNamedDeclarationWithSpecifiers({
|
|
652
540
|
exportKind: stmt.exportKind,
|
|
653
|
-
source: (0,
|
|
541
|
+
source: (0, _flowTransform.asDetachedNode)(stmt.source),
|
|
654
542
|
specifiers: resultSpecfiers
|
|
655
543
|
}), specifiersDeps];
|
|
656
544
|
}
|
|
657
|
-
|
|
658
545
|
function convertVariableDeclaration(stmt, context) {
|
|
659
546
|
const [first, ...rest] = stmt.declarations;
|
|
660
|
-
|
|
661
547
|
if (rest.length > 0) {
|
|
662
548
|
throw (0, _ErrorUtils.translationError)(stmt, `VariableDeclaration: more than one VariableDeclarators found`, context);
|
|
663
549
|
}
|
|
664
|
-
|
|
665
550
|
const id = first.id;
|
|
666
|
-
|
|
667
551
|
if (id.type !== 'Identifier') {
|
|
668
552
|
throw (0, _ErrorUtils.translationError)(id, `VariableDeclaration: unsupported destructing`, context);
|
|
669
553
|
}
|
|
670
|
-
|
|
671
554
|
const [resultTypeAnnotation, annotDeps] = (() => {
|
|
672
555
|
if (id.typeAnnotation != null) {
|
|
673
556
|
return convertTypeAnnotation(id.typeAnnotation, id, context);
|
|
674
557
|
}
|
|
675
|
-
|
|
676
558
|
const init = first.init;
|
|
677
|
-
|
|
678
559
|
if (init == null) {
|
|
679
560
|
return [(0, _ErrorUtils.flowFixMeOrError)(first, `VariableDeclaration: Type annotation missing`, context), []];
|
|
680
561
|
}
|
|
681
|
-
|
|
682
562
|
if (init.type === 'Identifier') {
|
|
683
|
-
return [
|
|
684
|
-
argument:
|
|
563
|
+
return [_flowTransform.t.TypeofTypeAnnotation({
|
|
564
|
+
argument: _flowTransform.t.Identifier({
|
|
685
565
|
name: init.name
|
|
686
566
|
})
|
|
687
567
|
}), (0, _FlowAnalyze.analyzeTypeDependencies)(init, context)];
|
|
688
568
|
}
|
|
689
|
-
|
|
690
569
|
return convertExpressionToTypeAnnotation(init, context);
|
|
691
570
|
})();
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
571
|
+
return [_flowTransform.t.DeclareVariable({
|
|
572
|
+
declarations: [_flowTransform.t.VariableDeclarator({
|
|
573
|
+
id: _flowTransform.t.Identifier({
|
|
574
|
+
name: id.name,
|
|
575
|
+
typeAnnotation: _flowTransform.t.TypeAnnotation({
|
|
576
|
+
typeAnnotation: resultTypeAnnotation
|
|
577
|
+
}),
|
|
578
|
+
optional: false
|
|
698
579
|
}),
|
|
699
|
-
|
|
700
|
-
}),
|
|
701
|
-
kind: stmt.kind
|
|
580
|
+
init: null
|
|
581
|
+
})],
|
|
582
|
+
kind: stmt.kind,
|
|
583
|
+
implicitDeclare: false
|
|
702
584
|
}), annotDeps];
|
|
703
585
|
}
|
|
704
|
-
|
|
705
586
|
function convertRequireToImport(stmt) {
|
|
706
587
|
if (stmt.declarations.length !== 1) {
|
|
707
588
|
return null;
|
|
708
589
|
}
|
|
709
|
-
|
|
710
590
|
const decl = stmt.declarations[0];
|
|
711
591
|
const init = decl.init;
|
|
712
|
-
|
|
713
592
|
if (init == null || init.type !== 'CallExpression' || init.callee.type !== 'Identifier' || init.callee.name !== 'require' || init.arguments.length !== 1) {
|
|
714
593
|
return null;
|
|
715
594
|
}
|
|
716
|
-
|
|
717
595
|
const sourceArg = init.arguments[0];
|
|
718
|
-
|
|
719
|
-
if (!(0, _hermesEstree.isStringLiteral)(sourceArg)) {
|
|
596
|
+
if (!(0, _flowEstree.isStringLiteral)(sourceArg)) {
|
|
720
597
|
return null;
|
|
721
598
|
}
|
|
722
|
-
|
|
723
599
|
const id = decl.id;
|
|
724
|
-
|
|
725
600
|
if (id.type === 'Identifier') {
|
|
726
|
-
return [
|
|
601
|
+
return [_flowTransform.t.ImportDeclaration({
|
|
727
602
|
importKind: 'value',
|
|
728
|
-
source: (0,
|
|
729
|
-
specifiers: [
|
|
730
|
-
local:
|
|
603
|
+
source: (0, _flowTransform.asDetachedNode)(sourceArg),
|
|
604
|
+
specifiers: [_flowTransform.t.ImportDefaultSpecifier({
|
|
605
|
+
local: _flowTransform.t.Identifier({
|
|
731
606
|
name: id.name
|
|
732
607
|
})
|
|
733
608
|
})],
|
|
734
609
|
attributes: []
|
|
735
610
|
}), []];
|
|
736
611
|
}
|
|
737
|
-
|
|
738
612
|
if (id.type === 'ObjectPattern') {
|
|
739
613
|
const specifiers = [];
|
|
740
|
-
|
|
741
614
|
for (const prop of id.properties) {
|
|
742
615
|
if (prop.type === 'RestElement') {
|
|
743
616
|
return null;
|
|
744
617
|
}
|
|
745
|
-
|
|
746
618
|
const key = prop.key;
|
|
747
619
|
const value = prop.value;
|
|
748
|
-
|
|
749
620
|
if (key.type !== 'Identifier' || value.type !== 'Identifier') {
|
|
750
621
|
return null;
|
|
751
622
|
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
imported: _hermesTransform.t.Identifier({
|
|
623
|
+
specifiers.push(_flowTransform.t.ImportSpecifier({
|
|
624
|
+
imported: _flowTransform.t.Identifier({
|
|
755
625
|
name: key.name
|
|
756
626
|
}),
|
|
757
|
-
local:
|
|
627
|
+
local: _flowTransform.t.Identifier({
|
|
758
628
|
name: value.name
|
|
759
629
|
}),
|
|
760
630
|
importKind: null
|
|
761
631
|
}));
|
|
762
632
|
}
|
|
763
|
-
|
|
764
|
-
return [_hermesTransform.t.ImportDeclaration({
|
|
633
|
+
return [_flowTransform.t.ImportDeclaration({
|
|
765
634
|
importKind: 'value',
|
|
766
|
-
source: (0,
|
|
635
|
+
source: (0, _flowTransform.asDetachedNode)(sourceArg),
|
|
767
636
|
specifiers,
|
|
768
637
|
attributes: []
|
|
769
638
|
}), []];
|
|
770
639
|
}
|
|
771
|
-
|
|
772
640
|
return null;
|
|
773
641
|
}
|
|
774
|
-
|
|
775
642
|
function convertImportDeclaration(stmt, context) {
|
|
776
643
|
if (stmt.attributes.length > 0) {
|
|
777
644
|
throw (0, _ErrorUtils.translationError)(stmt, 'ImportDeclaration: attributes not supported', context);
|
|
778
645
|
}
|
|
779
|
-
|
|
780
|
-
return [_hermesTransform.t.ImportDeclaration({
|
|
646
|
+
return [_flowTransform.t.ImportDeclaration({
|
|
781
647
|
specifiers: stmt.specifiers,
|
|
782
648
|
importKind: stmt.importKind,
|
|
783
649
|
source: stmt.source,
|
|
784
650
|
attributes: []
|
|
785
651
|
}), []];
|
|
786
652
|
}
|
|
787
|
-
|
|
788
653
|
function convertInterfaceDeclaration(interface_, context) {
|
|
789
|
-
return [(0,
|
|
654
|
+
return [(0, _flowTransform.asDetachedNode)(interface_), (0, _FlowAnalyze.analyzeTypeDependencies)(interface_, context)];
|
|
790
655
|
}
|
|
791
|
-
|
|
792
656
|
function convertClassDeclaration(class_, context) {
|
|
793
657
|
const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(class_.typeParameters, context);
|
|
794
658
|
const implementsDeps = class_.implements.flatMap(impl => (0, _FlowAnalyze.analyzeTypeDependencies)(impl, context));
|
|
795
659
|
const [resultSuperClass, superClassDeps] = convertSuperClass(class_.superClass, class_.superTypeArguments, context);
|
|
796
660
|
const [resultClassBody, bodyDeps] = convertClassBody(class_.body, context);
|
|
797
|
-
|
|
798
661
|
if (class_.decorators.length > 0) {
|
|
799
662
|
throw (0, _ErrorUtils.translationError)(class_, 'ClassDeclaration: decorators not supported', context);
|
|
800
663
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
id: (0, _hermesTransform.asDetachedNode)(class_.id),
|
|
664
|
+
return [_flowTransform.t.DeclareClass({
|
|
665
|
+
id: (0, _flowTransform.asDetachedNode)(class_.id),
|
|
804
666
|
typeParameters: resultTypeParams,
|
|
805
|
-
implements: class_.implements.map(impl => (0,
|
|
667
|
+
implements: class_.implements.map(impl => (0, _flowTransform.asDetachedNode)(impl)),
|
|
806
668
|
extends: resultSuperClass == null ? [] : [resultSuperClass],
|
|
807
669
|
mixins: [],
|
|
808
670
|
body: resultClassBody
|
|
809
671
|
}), [...typeParamsDeps, ...implementsDeps, ...superClassDeps, ...bodyDeps]];
|
|
810
672
|
}
|
|
811
|
-
|
|
812
673
|
function convertExpressionToIdentifier(node, context) {
|
|
813
674
|
if (node.type === 'Identifier') {
|
|
814
|
-
return
|
|
675
|
+
return _flowTransform.t.Identifier({
|
|
815
676
|
name: node.name
|
|
816
677
|
});
|
|
817
678
|
}
|
|
818
|
-
|
|
819
679
|
if (node.type === 'MemberExpression') {
|
|
820
680
|
const {
|
|
821
681
|
property,
|
|
822
682
|
object
|
|
823
683
|
} = node;
|
|
824
|
-
|
|
825
684
|
if (property.type === 'Identifier' && object.type !== 'Super') {
|
|
826
|
-
return
|
|
685
|
+
return _flowTransform.t.QualifiedTypeIdentifier({
|
|
827
686
|
qualification: convertExpressionToIdentifier(object, context),
|
|
828
|
-
id:
|
|
687
|
+
id: _flowTransform.t.Identifier({
|
|
829
688
|
name: property.name
|
|
830
689
|
})
|
|
831
690
|
});
|
|
832
691
|
}
|
|
833
692
|
}
|
|
834
|
-
|
|
835
693
|
throw (0, _ErrorUtils.translationError)(node, `Expected ${node.type} to be an Identifier or Member with Identifier property, non-Super object.`, context);
|
|
836
694
|
}
|
|
837
|
-
|
|
838
695
|
function convertExpressionToTypeofIdentifier(node, context) {
|
|
839
696
|
if (node.type === 'Identifier') {
|
|
840
|
-
return
|
|
697
|
+
return _flowTransform.t.Identifier({
|
|
841
698
|
name: node.name
|
|
842
699
|
});
|
|
843
700
|
}
|
|
844
|
-
|
|
845
701
|
if (node.type === 'MemberExpression') {
|
|
846
702
|
const {
|
|
847
703
|
property,
|
|
848
704
|
object
|
|
849
705
|
} = node;
|
|
850
|
-
|
|
851
706
|
if (property.type === 'Identifier' && object.type !== 'Super') {
|
|
852
|
-
return
|
|
707
|
+
return _flowTransform.t.QualifiedTypeofIdentifier({
|
|
853
708
|
qualification: convertExpressionToTypeofIdentifier(object, context),
|
|
854
|
-
id:
|
|
709
|
+
id: _flowTransform.t.Identifier({
|
|
855
710
|
name: property.name
|
|
856
711
|
})
|
|
857
712
|
});
|
|
858
713
|
}
|
|
859
714
|
}
|
|
860
|
-
|
|
861
715
|
throw (0, _ErrorUtils.translationError)(node, `Expected ${node.type} to be an Identifier or Member with Identifier property, non-Super object.`, context);
|
|
862
716
|
}
|
|
863
|
-
|
|
864
717
|
function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeArguments, context) {
|
|
865
718
|
const [resultTypeParams, typeParamsDeps] = convertTypeParameterInstantiationOrNull(superTypeArguments, context);
|
|
866
719
|
const superDeps = (0, _FlowAnalyze.analyzeTypeDependencies)(nodeForDependencies, context);
|
|
867
|
-
return [
|
|
720
|
+
return [_flowTransform.t.InterfaceExtends({
|
|
868
721
|
id: detachedId,
|
|
869
722
|
typeParameters: resultTypeParams
|
|
870
723
|
}), [...typeParamsDeps, ...superDeps]];
|
|
871
724
|
}
|
|
872
|
-
|
|
873
725
|
function convertSuperClass(superClass, superTypeArguments, context) {
|
|
874
726
|
if (superClass == null) {
|
|
875
727
|
return EMPTY_TRANSLATION_RESULT;
|
|
876
728
|
}
|
|
877
|
-
|
|
878
729
|
switch (superClass.type) {
|
|
879
730
|
case 'Identifier':
|
|
880
731
|
{
|
|
881
|
-
return convertSuperClassHelper((0,
|
|
732
|
+
return convertSuperClassHelper((0, _flowTransform.asDetachedNode)(superClass), superClass, superTypeArguments, context);
|
|
882
733
|
}
|
|
883
|
-
|
|
884
734
|
case 'MemberExpression':
|
|
885
735
|
{
|
|
886
736
|
return convertSuperClassHelper(convertExpressionToIdentifier(superClass, context), superClass, superTypeArguments, context);
|
|
887
737
|
}
|
|
888
|
-
|
|
889
738
|
case 'TypeCastExpression':
|
|
890
739
|
case 'AsExpression':
|
|
891
740
|
{
|
|
892
741
|
const typeAnnotation = superClass.type === 'TypeCastExpression' ? superClass.typeAnnotation.typeAnnotation : superClass.typeAnnotation;
|
|
893
|
-
|
|
894
742
|
if (typeAnnotation.type === 'GenericTypeAnnotation') {
|
|
895
|
-
return convertSuperClassHelper((0,
|
|
743
|
+
return convertSuperClassHelper((0, _flowTransform.asDetachedNode)(typeAnnotation.id), typeAnnotation, superTypeArguments, context);
|
|
896
744
|
}
|
|
897
|
-
|
|
898
745
|
if (typeAnnotation.type === 'TypeofTypeAnnotation') {
|
|
899
746
|
const typeofArg = typeAnnotation.argument;
|
|
900
|
-
|
|
901
747
|
if (typeofArg.type === 'Identifier') {
|
|
902
|
-
return convertSuperClassHelper((0,
|
|
748
|
+
return convertSuperClassHelper((0, _flowTransform.asDetachedNode)(typeofArg), typeofArg, typeAnnotation.typeArguments, context);
|
|
903
749
|
}
|
|
904
750
|
}
|
|
905
|
-
|
|
906
751
|
throw (0, _ErrorUtils.translationError)(superClass, `SuperClass: Typecast super type of "${typeAnnotation.type}" not supported`, context);
|
|
907
752
|
}
|
|
908
|
-
|
|
909
753
|
default:
|
|
910
754
|
{
|
|
911
755
|
throw (0, _ErrorUtils.translationError)(superClass, `SuperClass: Non identifier super type of "${superClass.type}" not supported`, context);
|
|
912
756
|
}
|
|
913
757
|
}
|
|
914
758
|
}
|
|
915
|
-
|
|
916
759
|
function convertClassBody(body, context) {
|
|
917
760
|
const [resultProperties, deps] = convertArray(body.body, member => convertClassMember(member, context));
|
|
918
|
-
return [
|
|
761
|
+
return [_flowTransform.t.ObjectTypeAnnotation({
|
|
919
762
|
inexact: false,
|
|
920
763
|
exact: false,
|
|
921
764
|
properties: resultProperties,
|
|
@@ -924,104 +767,86 @@ function convertClassBody(body, context) {
|
|
|
924
767
|
internalSlots: []
|
|
925
768
|
}), deps];
|
|
926
769
|
}
|
|
927
|
-
|
|
928
770
|
function convertClassMember(member, context) {
|
|
929
771
|
switch (member.type) {
|
|
930
772
|
case 'PropertyDefinition':
|
|
931
773
|
{
|
|
932
774
|
var _member$value;
|
|
933
|
-
|
|
934
775
|
if (member.key.type === 'PrivateIdentifier') {
|
|
935
776
|
return EMPTY_TRANSLATION_RESULT;
|
|
936
777
|
}
|
|
937
|
-
|
|
938
778
|
if (context.mungeUnderscores && member.key.type === 'Identifier' && member.key.name.length >= 2 && member.key.name[0] === '_' && member.key.name[1] !== '_') {
|
|
939
779
|
return EMPTY_TRANSLATION_RESULT;
|
|
940
780
|
}
|
|
941
|
-
|
|
942
|
-
if (!(0, _hermesEstree.isIdentifier)(member.key) && !(0, _hermesEstree.isStringLiteral)(member.key) && !(0, _hermesEstree.isNumericLiteral)(member.key)) {
|
|
781
|
+
if (!(0, _flowEstree.isIdentifier)(member.key) && !(0, _flowEstree.isStringLiteral)(member.key) && !(0, _flowEstree.isNumericLiteral)(member.key)) {
|
|
943
782
|
throw (0, _ErrorUtils.translationError)(member.key, `ClassMember PropertyDefinition: Unsupported key type of "${member.key.type}"`, context);
|
|
944
783
|
}
|
|
945
|
-
|
|
946
784
|
if (((_member$value = member.value) == null ? void 0 : _member$value.type) === 'ArrowFunctionExpression' && member.typeAnnotation == null) {
|
|
947
785
|
const [resultTypeAnnotation, deps] = convertAFunction(member.value, context);
|
|
948
|
-
return [inheritComments(member,
|
|
949
|
-
key: (0,
|
|
786
|
+
return [inheritComments(member, _flowTransform.t.ObjectTypePropertySignature({
|
|
787
|
+
key: (0, _flowTransform.asDetachedNode)(member.key),
|
|
950
788
|
value: resultTypeAnnotation,
|
|
951
789
|
optional: member.optional,
|
|
952
790
|
static: member.static,
|
|
953
791
|
variance: member.variance
|
|
954
792
|
})), deps];
|
|
955
793
|
}
|
|
956
|
-
|
|
957
794
|
const [resultTypeAnnotation, deps] = convertTypeAnnotation(member.typeAnnotation, member, context);
|
|
958
|
-
return [inheritComments(member,
|
|
959
|
-
key: (0,
|
|
795
|
+
return [inheritComments(member, _flowTransform.t.ObjectTypePropertySignature({
|
|
796
|
+
key: (0, _flowTransform.asDetachedNode)(member.key),
|
|
960
797
|
value: resultTypeAnnotation,
|
|
961
798
|
optional: member.optional,
|
|
962
799
|
static: member.static,
|
|
963
800
|
variance: member.variance
|
|
964
801
|
})), deps];
|
|
965
802
|
}
|
|
966
|
-
|
|
967
803
|
case 'MethodDefinition':
|
|
968
804
|
{
|
|
969
805
|
if (member.key.type === 'PrivateIdentifier') {
|
|
970
806
|
return EMPTY_TRANSLATION_RESULT;
|
|
971
807
|
}
|
|
972
|
-
|
|
973
808
|
if (context.mungeUnderscores && member.key.type === 'Identifier' && member.key.name.length >= 2 && member.key.name[0] === '_' && member.key.name[1] !== '_') {
|
|
974
809
|
return EMPTY_TRANSLATION_RESULT;
|
|
975
810
|
}
|
|
976
|
-
|
|
977
|
-
if (!(0, _hermesEstree.isIdentifier)(member.key) && !(0, _hermesEstree.isStringLiteral)(member.key) && !(0, _hermesEstree.isNumericLiteral)(member.key) && !((0, _hermesEstree.isMemberExpressionWithNonComputedProperty)(member.key) && member.key.object.type === 'Identifier' && member.key.object.name === 'Symbol' && ['iterator', 'asyncIterator'].includes(member.key.property.name))) {
|
|
811
|
+
if (!(0, _flowEstree.isIdentifier)(member.key) && !(0, _flowEstree.isStringLiteral)(member.key) && !(0, _flowEstree.isNumericLiteral)(member.key) && !((0, _flowEstree.isMemberExpressionWithNonComputedProperty)(member.key) && member.key.object.type === 'Identifier' && member.key.object.name === 'Symbol' && ['iterator', 'asyncIterator'].includes(member.key.property.name))) {
|
|
978
812
|
throw (0, _ErrorUtils.translationError)(member.key, `ClassMember PropertyDefinition: Unsupported key type of "${member.key.type}"`, context);
|
|
979
813
|
}
|
|
980
|
-
|
|
981
814
|
const [resultValue, deps] = convertAFunction(member.value, context);
|
|
982
|
-
const newKey = (0,
|
|
815
|
+
const newKey = (0, _flowEstree.isMemberExpressionWithNonComputedProperty)(member.key) && member.key.object.type === 'Identifier' && member.key.object.name === 'Symbol' ? _flowTransform.t.Identifier({
|
|
983
816
|
name: `@@${member.key.property.name}`
|
|
984
817
|
}) : member.key;
|
|
985
|
-
|
|
986
818
|
if (member.kind === 'get' || member.kind === 'set') {
|
|
987
819
|
const kind = member.kind;
|
|
988
|
-
return [inheritComments(member,
|
|
989
|
-
key: (0,
|
|
820
|
+
return [inheritComments(member, _flowTransform.t.ObjectTypeAccessorSignature({
|
|
821
|
+
key: (0, _flowTransform.asDetachedNode)(newKey),
|
|
990
822
|
value: resultValue,
|
|
991
823
|
static: member.static,
|
|
992
824
|
kind
|
|
993
825
|
})), deps];
|
|
994
826
|
}
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
key: (0, _hermesTransform.asDetachedNode)(newKey),
|
|
827
|
+
return [inheritComments(member, _flowTransform.t.ObjectTypeMethodSignature({
|
|
828
|
+
key: (0, _flowTransform.asDetachedNode)(newKey),
|
|
998
829
|
value: resultValue,
|
|
999
830
|
static: member.static
|
|
1000
831
|
})), deps];
|
|
1001
832
|
}
|
|
1002
|
-
|
|
1003
833
|
default:
|
|
1004
834
|
{
|
|
1005
835
|
throw (0, _ErrorUtils.translationError)(member, `ClassMember: Unsupported member type of "${member.type}"`, context);
|
|
1006
836
|
}
|
|
1007
837
|
}
|
|
1008
838
|
}
|
|
1009
|
-
|
|
1010
839
|
function convertComponentDeclaration(comp, context) {
|
|
1011
840
|
const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(comp.typeParameters, context);
|
|
1012
841
|
const [resultParams, resultRestParam, paramsAndRestDeps] = convertComponentParameters(comp.params, context);
|
|
1013
|
-
|
|
1014
842
|
const [resultRendersType, rendersTypeDeps] = (() => {
|
|
1015
843
|
const rendersType = comp.rendersType;
|
|
1016
|
-
|
|
1017
844
|
if (rendersType == null) {
|
|
1018
845
|
return EMPTY_TRANSLATION_RESULT;
|
|
1019
846
|
}
|
|
1020
|
-
|
|
1021
|
-
return [(0, _hermesTransform.asDetachedNode)(rendersType), (0, _FlowAnalyze.analyzeTypeDependencies)(rendersType, context)];
|
|
847
|
+
return [(0, _flowTransform.asDetachedNode)(rendersType), (0, _FlowAnalyze.analyzeTypeDependencies)(rendersType, context)];
|
|
1022
848
|
})();
|
|
1023
|
-
|
|
1024
|
-
return [_hermesTransform.t.DeclareComponent({
|
|
849
|
+
return [_flowTransform.t.DeclareComponent({
|
|
1025
850
|
id: comp.id,
|
|
1026
851
|
params: resultParams,
|
|
1027
852
|
rest: resultRestParam,
|
|
@@ -1029,65 +854,54 @@ function convertComponentDeclaration(comp, context) {
|
|
|
1029
854
|
rendersType: resultRendersType
|
|
1030
855
|
}), [...typeParamsDeps, ...paramsAndRestDeps, ...rendersTypeDeps]];
|
|
1031
856
|
}
|
|
1032
|
-
|
|
1033
857
|
function hasNonIdentifierStringLiteralParam(params) {
|
|
1034
|
-
return params.some(param => param.type === 'ComponentParameter' && (0,
|
|
858
|
+
return params.some(param => param.type === 'ComponentParameter' && (0, _flowEstree.isStringLiteral)(param.name) && !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(param.name.value));
|
|
1035
859
|
}
|
|
1036
|
-
|
|
1037
860
|
function extractParamTypeInfo(param, context) {
|
|
1038
861
|
let optional = false;
|
|
1039
862
|
let local = param.local;
|
|
1040
|
-
|
|
1041
863
|
if (local.type === 'AssignmentPattern') {
|
|
1042
864
|
local = local.left;
|
|
1043
865
|
optional = true;
|
|
1044
866
|
}
|
|
1045
|
-
|
|
1046
867
|
if (!optional && local.type === 'Identifier') {
|
|
1047
868
|
optional = local.optional;
|
|
1048
869
|
}
|
|
1049
|
-
|
|
1050
870
|
return [optional, local, convertTypeAnnotation(local.typeAnnotation, param, context)];
|
|
1051
871
|
}
|
|
1052
|
-
|
|
1053
872
|
function convertComponentParameters(params, context) {
|
|
1054
873
|
if (hasNonIdentifierStringLiteralParam(params)) {
|
|
1055
874
|
return convertComponentParametersToPropsObject(params, context);
|
|
1056
875
|
}
|
|
1057
|
-
|
|
1058
876
|
return params.reduce(([resultParams, restParam, paramsDeps], param) => {
|
|
1059
877
|
switch (param.type) {
|
|
1060
878
|
case 'ComponentParameter':
|
|
1061
879
|
{
|
|
1062
880
|
const [optional, _local, [typeAnnotationType, typeDeps]] = extractParamTypeInfo(param, context);
|
|
1063
|
-
const name = (0,
|
|
881
|
+
const name = (0, _flowEstree.isStringLiteral)(param.name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(param.name.value) ? _flowTransform.t.Identifier({
|
|
1064
882
|
name: param.name.value
|
|
1065
|
-
}) : (0,
|
|
1066
|
-
const resultParam = inheritComments(param,
|
|
883
|
+
}) : (0, _flowTransform.asDetachedNode)(param.name);
|
|
884
|
+
const resultParam = inheritComments(param, _flowTransform.t.ComponentTypeParameter({
|
|
1067
885
|
name,
|
|
1068
886
|
typeAnnotation: typeAnnotationType,
|
|
1069
887
|
optional
|
|
1070
888
|
}));
|
|
1071
889
|
return [[...resultParams, resultParam], restParam, [...paramsDeps, ...typeDeps]];
|
|
1072
890
|
}
|
|
1073
|
-
|
|
1074
891
|
case 'RestElement':
|
|
1075
892
|
{
|
|
1076
893
|
if (restParam != null) {
|
|
1077
894
|
throw (0, _ErrorUtils.translationError)(param, `ComponentParameter: Multiple rest elements found`, context);
|
|
1078
895
|
}
|
|
1079
|
-
|
|
1080
896
|
const argument = param.argument;
|
|
1081
|
-
|
|
1082
897
|
if (argument.type === 'AssignmentPattern' || argument.type === 'ArrayPattern' || argument.type === 'RestElement') {
|
|
1083
898
|
throw (0, _ErrorUtils.translationError)(param, `ComponentParameter: Invalid RestElement usage`, context);
|
|
1084
899
|
}
|
|
1085
|
-
|
|
1086
900
|
const [typeAnnotationType, typeDeps] = convertTypeAnnotation(argument.typeAnnotation, argument, context);
|
|
1087
901
|
const restName = argument.type === 'Identifier' ? argument.name : 'rest';
|
|
1088
902
|
const restOptional = argument.type === 'Identifier' ? argument.optional : false;
|
|
1089
|
-
const resultRestParam = inheritComments(param,
|
|
1090
|
-
name:
|
|
903
|
+
const resultRestParam = inheritComments(param, _flowTransform.t.ComponentTypeParameter({
|
|
904
|
+
name: _flowTransform.t.Identifier({
|
|
1091
905
|
name: restName
|
|
1092
906
|
}),
|
|
1093
907
|
typeAnnotation: typeAnnotationType,
|
|
@@ -1098,38 +912,32 @@ function convertComponentParameters(params, context) {
|
|
|
1098
912
|
}
|
|
1099
913
|
}, [[], null, []]);
|
|
1100
914
|
}
|
|
1101
|
-
|
|
1102
915
|
function convertComponentParametersToPropsObject(params, context) {
|
|
1103
916
|
const properties = [];
|
|
1104
917
|
const allDeps = [];
|
|
1105
|
-
|
|
1106
918
|
for (const param of params) {
|
|
1107
919
|
if (param.type === 'RestElement') {
|
|
1108
920
|
const argument = param.argument;
|
|
1109
|
-
|
|
1110
921
|
if (argument.type !== 'AssignmentPattern' && argument.type !== 'ArrayPattern' && argument.type !== 'RestElement') {
|
|
1111
922
|
const [typeAnnotationType, typeDeps] = convertTypeAnnotation(argument.typeAnnotation, argument, context);
|
|
1112
923
|
allDeps.push(...typeDeps);
|
|
1113
|
-
properties.push(
|
|
924
|
+
properties.push(_flowTransform.t.ObjectTypeSpreadProperty({
|
|
1114
925
|
argument: typeAnnotationType
|
|
1115
926
|
}));
|
|
1116
927
|
}
|
|
1117
|
-
|
|
1118
928
|
continue;
|
|
1119
929
|
}
|
|
1120
|
-
|
|
1121
930
|
const [optional, _local, [typeAnnotationType, typeDeps]] = extractParamTypeInfo(param, context);
|
|
1122
931
|
allDeps.push(...typeDeps);
|
|
1123
|
-
properties.push(inheritComments(param,
|
|
1124
|
-
key: (0,
|
|
932
|
+
properties.push(inheritComments(param, _flowTransform.t.ObjectTypePropertySignature({
|
|
933
|
+
key: (0, _flowTransform.asDetachedNode)(param.name),
|
|
1125
934
|
value: typeAnnotationType,
|
|
1126
935
|
optional,
|
|
1127
936
|
static: false,
|
|
1128
937
|
variance: null
|
|
1129
938
|
})));
|
|
1130
939
|
}
|
|
1131
|
-
|
|
1132
|
-
const propsType = _hermesTransform.t.ObjectTypeAnnotation({
|
|
940
|
+
const propsType = _flowTransform.t.ObjectTypeAnnotation({
|
|
1133
941
|
inexact: false,
|
|
1134
942
|
exact: false,
|
|
1135
943
|
properties,
|
|
@@ -1137,89 +945,73 @@ function convertComponentParametersToPropsObject(params, context) {
|
|
|
1137
945
|
callProperties: [],
|
|
1138
946
|
internalSlots: []
|
|
1139
947
|
});
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
name: _hermesTransform.t.Identifier({
|
|
948
|
+
const restParam = _flowTransform.t.ComponentTypeParameter({
|
|
949
|
+
name: _flowTransform.t.Identifier({
|
|
1143
950
|
name: 'props'
|
|
1144
951
|
}),
|
|
1145
952
|
typeAnnotation: propsType,
|
|
1146
953
|
optional: false
|
|
1147
954
|
});
|
|
1148
|
-
|
|
1149
955
|
return [[], restParam, allDeps];
|
|
1150
956
|
}
|
|
1151
|
-
|
|
1152
957
|
function convertHookDeclaration(hook, context) {
|
|
1153
958
|
var _hook$returnType;
|
|
1154
|
-
|
|
1155
959
|
const id = hook.id;
|
|
1156
|
-
const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType :
|
|
1157
|
-
typeAnnotation:
|
|
960
|
+
const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType : _flowTransform.t.TypeAnnotation({
|
|
961
|
+
typeAnnotation: _flowTransform.t.VoidTypeAnnotation()
|
|
1158
962
|
});
|
|
1159
963
|
const [resultReturnType, returnDeps] = convertTypeAnnotation(returnType, hook, context);
|
|
1160
964
|
const [resultParams, restParam, paramsDeps] = convertFunctionParameters(hook.params, context);
|
|
1161
965
|
const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(hook.typeParameters, context);
|
|
1162
|
-
|
|
1163
|
-
const resultFunc = _hermesTransform.t.HookTypeAnnotation({
|
|
966
|
+
const resultFunc = _flowTransform.t.HookTypeAnnotation({
|
|
1164
967
|
params: resultParams,
|
|
1165
968
|
returnType: resultReturnType,
|
|
1166
969
|
rest: restParam,
|
|
1167
970
|
typeParameters: resultTypeParams
|
|
1168
971
|
});
|
|
1169
|
-
|
|
1170
972
|
const funcDeps = [...paramsDeps, ...returnDeps, ...typeParamsDeps];
|
|
1171
|
-
return [
|
|
973
|
+
return [_flowTransform.t.DeclareHook({
|
|
1172
974
|
name: id.name,
|
|
1173
975
|
functionType: resultFunc
|
|
1174
976
|
}), [...funcDeps]];
|
|
1175
977
|
}
|
|
1176
|
-
|
|
1177
978
|
function convertFunctionDeclaration(func, context) {
|
|
1178
979
|
const id = func.id;
|
|
1179
|
-
|
|
1180
980
|
if (id == null) {
|
|
1181
981
|
throw (0, _ErrorUtils.translationError)(func, `FunctionDeclaration: Missing name`, context);
|
|
1182
982
|
}
|
|
1183
|
-
|
|
1184
983
|
const [resultFunc, funcDeps] = convertAFunction(func, context);
|
|
1185
|
-
|
|
1186
984
|
const [resultPredicate, predicateDeps] = (() => {
|
|
1187
985
|
if (func.predicate == null) {
|
|
1188
986
|
return EMPTY_TRANSLATION_RESULT;
|
|
1189
987
|
}
|
|
1190
|
-
|
|
1191
988
|
const body = func.body.body;
|
|
1192
989
|
const predicateExpr = body.length === 1 && body[0].type === 'ReturnStatement' && body[0].argument != null ? body[0].argument : null;
|
|
1193
|
-
|
|
1194
990
|
if (predicateExpr == null) {
|
|
1195
991
|
throw (0, _ErrorUtils.translationError)(func, 'FunctionDeclation: Invalid predicate function.', context);
|
|
1196
992
|
}
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
value: (0, _hermesTransform.asDetachedNode)(predicateExpr)
|
|
993
|
+
return [_flowTransform.t.DeclaredPredicate({
|
|
994
|
+
value: (0, _flowTransform.asDetachedNode)(predicateExpr)
|
|
1200
995
|
}), (0, _FlowAnalyze.analyzeTypeDependencies)(predicateExpr, context)];
|
|
1201
996
|
})();
|
|
1202
|
-
|
|
1203
|
-
return [_hermesTransform.t.DeclareFunction({
|
|
997
|
+
return [_flowTransform.t.DeclareFunction({
|
|
1204
998
|
name: id.name,
|
|
1205
999
|
functionType: resultFunc,
|
|
1206
1000
|
predicate: resultPredicate
|
|
1207
1001
|
}), [...funcDeps, ...predicateDeps]];
|
|
1208
1002
|
}
|
|
1209
|
-
|
|
1210
1003
|
function convertAFunction(func, context) {
|
|
1211
1004
|
const returnType = (0, _FlowAnalyze.analyzeFunctionReturn)(func);
|
|
1212
1005
|
const [resultReturnType, returnDeps] = convertTypeAnnotation(returnType, func, context);
|
|
1213
1006
|
const [resultParams, restParam, paramsDeps] = convertFunctionParameters(func.params, context);
|
|
1214
1007
|
const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(func.typeParameters, context);
|
|
1215
|
-
return [
|
|
1008
|
+
return [_flowTransform.t.FunctionTypeAnnotation({
|
|
1216
1009
|
params: resultParams,
|
|
1217
1010
|
returnType: resultReturnType,
|
|
1218
1011
|
rest: restParam,
|
|
1219
1012
|
typeParameters: resultTypeParams
|
|
1220
1013
|
}), [...paramsDeps, ...returnDeps, ...typeParamsDeps]];
|
|
1221
1014
|
}
|
|
1222
|
-
|
|
1223
1015
|
function convertFunctionParameters(params, context) {
|
|
1224
1016
|
return params.reduce(([resultParams, restParam, paramsDeps], param, index) => {
|
|
1225
1017
|
switch (param.type) {
|
|
@@ -1230,98 +1022,81 @@ function convertFunctionParameters(params, context) {
|
|
|
1230
1022
|
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param, context, index, false);
|
|
1231
1023
|
return [[...resultParams, resultParam], restParam, [...paramsDeps, ...deps]];
|
|
1232
1024
|
}
|
|
1233
|
-
|
|
1234
1025
|
case 'AssignmentPattern':
|
|
1235
1026
|
{
|
|
1236
1027
|
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param.left, context, index, true);
|
|
1237
1028
|
return [[...resultParams, resultParam], restParam, [...paramsDeps, ...deps]];
|
|
1238
1029
|
}
|
|
1239
|
-
|
|
1240
1030
|
case 'RestElement':
|
|
1241
1031
|
{
|
|
1242
1032
|
if (restParam != null) {
|
|
1243
1033
|
throw (0, _ErrorUtils.translationError)(param, `FunctionParameter: Multiple rest elements found`, context);
|
|
1244
1034
|
}
|
|
1245
|
-
|
|
1246
1035
|
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param.argument, context, index, false);
|
|
1247
1036
|
return [resultParams, resultParam, [...paramsDeps, ...deps]];
|
|
1248
1037
|
}
|
|
1249
1038
|
}
|
|
1250
1039
|
}, [[], null, []]);
|
|
1251
1040
|
}
|
|
1252
|
-
|
|
1253
1041
|
function convertBindingNameToFunctionTypeParam(pat, context, index, isAssignment) {
|
|
1254
1042
|
const name = pat.type === 'Identifier' && pat.name != null ? pat.name : `$$PARAM_${index}$$`;
|
|
1255
1043
|
const [resultParamTypeAnnotation, paramDeps] = convertTypeAnnotation(pat.typeAnnotation, pat, context);
|
|
1256
|
-
return [
|
|
1257
|
-
name:
|
|
1044
|
+
return [_flowTransform.t.FunctionTypeParam({
|
|
1045
|
+
name: _flowTransform.t.Identifier({
|
|
1258
1046
|
name
|
|
1259
1047
|
}),
|
|
1260
1048
|
typeAnnotation: resultParamTypeAnnotation,
|
|
1261
1049
|
optional: isAssignment || (pat.type === 'Identifier' ? pat.optional : false)
|
|
1262
1050
|
}), paramDeps];
|
|
1263
1051
|
}
|
|
1264
|
-
|
|
1265
1052
|
function convertTypeAlias(typeAlias, context) {
|
|
1266
1053
|
const [typeAnnotationType, rightDeps] = convertTypeAnnotationType(typeAlias.right, typeAlias, context);
|
|
1267
1054
|
const [typeParameters, typeParamsDeps] = convertTypeParameterDeclarationOrNull(typeAlias.typeParameters, context);
|
|
1268
|
-
return [
|
|
1055
|
+
return [_flowTransform.t.TypeAlias({
|
|
1269
1056
|
right: typeAnnotationType,
|
|
1270
|
-
id: (0,
|
|
1057
|
+
id: (0, _flowTransform.asDetachedNode)(typeAlias.id),
|
|
1271
1058
|
typeParameters
|
|
1272
1059
|
}), [...rightDeps, ...typeParamsDeps]];
|
|
1273
1060
|
}
|
|
1274
|
-
|
|
1275
1061
|
function convertOpaqueType(opaqueType, context) {
|
|
1276
1062
|
const [resultSupertype, supertypeDeps] = convertTypeAnnotationTypeOrNull(opaqueType.supertype, context);
|
|
1277
1063
|
const [typeParameters, typeParamsDeps] = convertTypeParameterDeclarationOrNull(opaqueType.typeParameters, context);
|
|
1278
|
-
return [
|
|
1279
|
-
id: (0,
|
|
1064
|
+
return [_flowTransform.t.DeclareOpaqueType({
|
|
1065
|
+
id: (0, _flowTransform.asDetachedNode)(opaqueType.id),
|
|
1280
1066
|
typeParameters,
|
|
1281
1067
|
supertype: resultSupertype
|
|
1282
1068
|
}), [...typeParamsDeps, ...supertypeDeps]];
|
|
1283
1069
|
}
|
|
1284
|
-
|
|
1285
1070
|
function convertAsExpression(asExpression, context) {
|
|
1286
1071
|
return convertTypeAnnotationType(asExpression.typeAnnotation, asExpression, context);
|
|
1287
1072
|
}
|
|
1288
|
-
|
|
1289
1073
|
function convertTypeCastExpression(typeCast, context) {
|
|
1290
1074
|
return convertTypeAnnotation(typeCast.typeAnnotation, typeCast, context);
|
|
1291
1075
|
}
|
|
1292
|
-
|
|
1293
1076
|
function convertTypeAnnotation(annot, container, context) {
|
|
1294
1077
|
return convertTypeAnnotationType(annot == null ? void 0 : annot.typeAnnotation, container, context);
|
|
1295
1078
|
}
|
|
1296
|
-
|
|
1297
1079
|
function convertTypeAnnotationType(annot, container, context) {
|
|
1298
1080
|
if (annot == null) {
|
|
1299
1081
|
return [(0, _ErrorUtils.flowFixMeOrError)(container, `TypeAnnotationType: Type annotation missing`, context), []];
|
|
1300
1082
|
}
|
|
1301
|
-
|
|
1302
|
-
return [(0, _hermesTransform.asDetachedNode)(annot), (0, _FlowAnalyze.analyzeTypeDependencies)(annot, context)];
|
|
1083
|
+
return [(0, _flowTransform.asDetachedNode)(annot), (0, _FlowAnalyze.analyzeTypeDependencies)(annot, context)];
|
|
1303
1084
|
}
|
|
1304
|
-
|
|
1305
1085
|
function convertTypeAnnotationTypeOrNull(annot, context) {
|
|
1306
1086
|
if (annot == null) {
|
|
1307
1087
|
return EMPTY_TRANSLATION_RESULT;
|
|
1308
1088
|
}
|
|
1309
|
-
|
|
1310
|
-
return [(0, _hermesTransform.asDetachedNode)(annot), (0, _FlowAnalyze.analyzeTypeDependencies)(annot, context)];
|
|
1089
|
+
return [(0, _flowTransform.asDetachedNode)(annot), (0, _FlowAnalyze.analyzeTypeDependencies)(annot, context)];
|
|
1311
1090
|
}
|
|
1312
|
-
|
|
1313
1091
|
function convertTypeParameterDeclarationOrNull(decl, context) {
|
|
1314
1092
|
if (decl == null) {
|
|
1315
1093
|
return EMPTY_TRANSLATION_RESULT;
|
|
1316
1094
|
}
|
|
1317
|
-
|
|
1318
|
-
return [(0, _hermesTransform.asDetachedNode)(decl), (0, _FlowAnalyze.analyzeTypeDependencies)(decl, context)];
|
|
1095
|
+
return [(0, _flowTransform.asDetachedNode)(decl), (0, _FlowAnalyze.analyzeTypeDependencies)(decl, context)];
|
|
1319
1096
|
}
|
|
1320
|
-
|
|
1321
1097
|
function convertTypeParameterInstantiationOrNull(inst, context) {
|
|
1322
1098
|
if (inst == null) {
|
|
1323
1099
|
return EMPTY_TRANSLATION_RESULT;
|
|
1324
1100
|
}
|
|
1325
|
-
|
|
1326
|
-
return [(0, _hermesTransform.asDetachedNode)(inst), (0, _FlowAnalyze.analyzeTypeDependencies)(inst, context)];
|
|
1101
|
+
return [(0, _flowTransform.asDetachedNode)(inst), (0, _FlowAnalyze.analyzeTypeDependencies)(inst, context)];
|
|
1327
1102
|
}
|