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