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