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,33 +1,15 @@
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.TSDefToFlowDef = TSDefToFlowDef;
16
-
17
- var FlowESTree = _interopRequireWildcard(require("hermes-estree"));
18
-
7
+ var FlowESTree = _interopRequireWildcard(require("flow-estree"));
19
8
  var TSESTree = _interopRequireWildcard(require("./utils/ts-estree-ast-types"));
20
-
21
- var _hermesTransform = require("hermes-transform");
22
-
9
+ var _flowTransform = require("flow-transform");
23
10
  var _ErrorUtils = require("./utils/ErrorUtils");
24
-
25
11
  var _os = require("os");
26
-
27
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
28
-
29
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
30
-
12
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
31
13
  const DUMMY_LOC = null;
32
14
  const DUMMY_RANGE = [0, 0];
33
15
  const DUMMY_PARENT = null;
@@ -36,19 +18,17 @@ const DUMMY_COMMON = {
36
18
  range: DUMMY_RANGE,
37
19
  parent: DUMMY_PARENT
38
20
  };
39
-
40
21
  function constructFlowNode(node) {
41
- return { ...node,
22
+ return {
23
+ ...node,
42
24
  ...DUMMY_COMMON
43
25
  };
44
26
  }
45
-
46
- const makeCommentOwnLine = // $FlowExpectedError[incompatible-type] - trust me this re-type is 100% safe
47
- _hermesTransform.makeCommentOwnLine;
48
-
27
+ const makeCommentOwnLine = _flowTransform.makeCommentOwnLine;
49
28
  function TSDefToFlowDef(originalCode, ast, opts) {
50
29
  const flowBody = [];
51
- const flowProgram = { ...DUMMY_COMMON,
30
+ const flowProgram = {
31
+ ...DUMMY_COMMON,
52
32
  type: 'Program',
53
33
  body: flowBody,
54
34
  comments: [],
@@ -59,44 +39,32 @@ function TSDefToFlowDef(originalCode, ast, opts) {
59
39
  docblock: null
60
40
  };
61
41
  const [transform, code] = getTransforms(originalCode, opts);
62
-
63
42
  for (const node of ast.body) {
64
43
  const result = transform.AllStatement(node);
65
44
  flowBody.push(...(Array.isArray(result) ? result : [result]));
66
45
  }
67
-
68
46
  return [flowProgram, code];
69
- } // Note: The implementation here is still in early stage. If something is not supported, it doesn't
70
- // necessarily mean that it cannot be. It might just mean that it's not priortized yet. If something
71
- // is translated in way that is wrong, then it's likely wrong.
72
-
73
-
47
+ }
74
48
  const getTransforms = (originalCode, opts) => {
75
49
  let code = originalCode;
76
-
77
50
  function translationError(node, message) {
78
51
  return (0, _ErrorUtils.translationError)(node, message, {
79
52
  code
80
53
  });
81
54
  }
82
-
83
55
  function unexpectedTranslationError(node, message) {
84
56
  return (0, _ErrorUtils.unexpectedTranslationError)(node, message, {
85
57
  code
86
58
  });
87
59
  }
88
-
89
60
  function unsupportedFeatureMessage(thing) {
90
61
  return `Unsupported feature: Translating "${thing}" is currently not supported.`;
91
62
  }
92
-
93
63
  function buildCodeFrameForComment(node, message) {
94
64
  return (0, _ErrorUtils.buildCodeFrame)(node, message, code, false);
95
65
  }
96
-
97
66
  function addErrorComment(node, message) {
98
67
  var _node$comments;
99
-
100
68
  const comment = {
101
69
  type: 'Block',
102
70
  loc: DUMMY_LOC,
@@ -104,38 +72,33 @@ const getTransforms = (originalCode, opts) => {
104
72
  leading: true,
105
73
  printed: false
106
74
  };
107
- code = makeCommentOwnLine(code, comment); // $FlowExpectedError[prop-missing]
108
- // $FlowExpectedError[cannot-write]
109
-
110
- (_node$comments = node.comments) != null ? _node$comments : node.comments = []; // $FlowExpectedError[incompatible-type]
111
-
75
+ code = makeCommentOwnLine(code, comment);
76
+ (_node$comments = node.comments) != null ? _node$comments : node.comments = [];
112
77
  node.comments.push(comment);
113
78
  }
114
-
115
79
  function unsupportedAnnotation(node, thing) {
116
80
  const message = unsupportedFeatureMessage(thing);
117
-
118
81
  if (opts.recoverFromErrors) {
119
82
  const codeFrame = buildCodeFrameForComment(node, message);
120
- const newNode = { ...DUMMY_COMMON,
83
+ const newNode = {
84
+ ...DUMMY_COMMON,
121
85
  type: 'AnyTypeAnnotation'
122
86
  };
123
87
  addErrorComment(newNode, codeFrame);
124
88
  return newNode;
125
89
  }
126
-
127
90
  throw translationError(node, message);
128
91
  }
129
-
130
92
  function unsupportedDeclaration(node, thing, id, typeParameters = null) {
131
93
  const message = unsupportedFeatureMessage(thing);
132
-
133
94
  if (opts.recoverFromErrors) {
134
95
  const codeFrame = buildCodeFrameForComment(node, message);
135
- const newNode = { ...DUMMY_COMMON,
96
+ const newNode = {
97
+ ...DUMMY_COMMON,
136
98
  type: 'TypeAlias',
137
99
  id: Transform.Identifier(id, false),
138
- right: { ...DUMMY_COMMON,
100
+ right: {
101
+ ...DUMMY_COMMON,
139
102
  type: 'AnyTypeAnnotation'
140
103
  },
141
104
  typeParameters: Transform.TSTypeParameterDeclarationOpt(typeParameters)
@@ -143,10 +106,8 @@ const getTransforms = (originalCode, opts) => {
143
106
  addErrorComment(newNode, codeFrame);
144
107
  return newNode;
145
108
  }
146
-
147
109
  throw translationError(node, message);
148
110
  }
149
-
150
111
  class Transform {
151
112
  static BlockStatement(node) {
152
113
  return constructFlowNode({
@@ -154,7 +115,6 @@ const getTransforms = (originalCode, opts) => {
154
115
  body: node.body.flatMap(node => Transform.Statement(node))
155
116
  });
156
117
  }
157
-
158
118
  static ClassDeclarationWithName(node) {
159
119
  return constructFlowNode({
160
120
  type: 'DeclareClass',
@@ -166,37 +126,27 @@ const getTransforms = (originalCode, opts) => {
166
126
  body: Transform.ClassDeclarationBody(node.body)
167
127
  });
168
128
  }
169
-
170
129
  static ClassDeclarationBody({
171
130
  body
172
131
  }) {
173
132
  const properties = [];
174
133
  const indexers = [];
175
-
176
134
  for (const classItem of body) {
177
135
  switch (classItem.type) {
178
136
  case 'StaticBlock':
179
137
  break;
180
-
181
138
  case 'TSIndexSignature':
182
139
  break;
183
-
184
140
  case 'TSAbstractPropertyDefinition':
185
141
  case 'PropertyDefinition':
186
- // $FlowFixMe[incompatible-type] ambiguous node
187
142
  Transform._translateIntoObjectProp(classItem, properties, indexers);
188
-
189
143
  break;
190
-
191
144
  case 'MethodDefinition':
192
145
  case 'TSAbstractMethodDefinition':
193
- // $FlowFixMe[incompatible-type] ambiguous node
194
146
  Transform._translateIntoObjectMethod(classItem, properties);
195
-
196
147
  break;
197
148
  }
198
149
  }
199
-
200
150
  return constructFlowNode({
201
151
  type: 'ObjectTypeAnnotation',
202
152
  properties,
@@ -207,47 +157,38 @@ const getTransforms = (originalCode, opts) => {
207
157
  inexact: false
208
158
  });
209
159
  }
210
-
211
160
  static ClassDeclarationSuperClass(superClass, superTypeArguments) {
212
161
  if (superClass == null) {
213
162
  return [];
214
163
  }
215
-
216
164
  const id = Transform._expressionToIdOrQualifiedTypeId(superClass, 'superClass');
217
-
218
165
  return [constructFlowNode({
219
166
  type: 'InterfaceExtends',
220
167
  id,
221
168
  typeParameters: Transform.TSTypeParameterInstantiationOpt(superTypeArguments)
222
169
  })];
223
170
  }
224
-
225
171
  static ClassImplements(node) {
226
172
  if (node.expression.type !== 'Identifier') {
227
173
  throw unexpectedTranslationError(node, 'Expected expression to be an Identifier');
228
174
  }
229
-
230
175
  return constructFlowNode({
231
176
  type: 'ClassImplements',
232
177
  id: Transform.Identifier(node.expression),
233
178
  typeParameters: Transform.TSTypeParameterInstantiationOpt(node.typeArguments)
234
179
  });
235
180
  }
236
-
237
181
  static DebuggerStatement() {
238
182
  return constructFlowNode({
239
183
  type: 'DebuggerStatement'
240
184
  });
241
185
  }
242
-
243
186
  static EntityNameToTypeIdentifier(node) {
244
187
  switch (node.type) {
245
188
  case 'Identifier':
246
189
  return Transform.Identifier(node);
247
-
248
190
  case 'TSQualifiedName':
249
191
  return Transform.TSQualifiedNameToQualifiedTypeIdentifier(node);
250
-
251
192
  case 'ThisExpression':
252
193
  return constructFlowNode({
253
194
  type: 'Identifier',
@@ -257,15 +198,12 @@ const getTransforms = (originalCode, opts) => {
257
198
  });
258
199
  }
259
200
  }
260
-
261
201
  static EntityNameToTypeofIdentifier(node) {
262
202
  switch (node.type) {
263
203
  case 'Identifier':
264
204
  return Transform.Identifier(node);
265
-
266
205
  case 'TSQualifiedName':
267
206
  return Transform.TSQualifiedNameToQualifiedTypeofIdentifier(node);
268
-
269
207
  case 'ThisExpression':
270
208
  return constructFlowNode({
271
209
  type: 'Identifier',
@@ -275,7 +213,6 @@ const getTransforms = (originalCode, opts) => {
275
213
  });
276
214
  }
277
215
  }
278
-
279
216
  static ExportAllDeclaration(node) {
280
217
  return constructFlowNode({
281
218
  type: 'ExportAllDeclaration',
@@ -290,36 +227,27 @@ const getTransforms = (originalCode, opts) => {
290
227
  exported: node.exported != null ? Transform.Identifier(node.exported) : null
291
228
  });
292
229
  }
293
-
294
230
  static ExportDefaultDeclaration(node) {
295
231
  let declaration;
296
-
297
232
  switch (node.declaration.type) {
298
233
  case 'ClassDeclaration':
299
- declaration = Transform.ClassDeclarationWithName( // possibly missing id
300
- node.declaration);
234
+ declaration = Transform.ClassDeclarationWithName(node.declaration);
301
235
  break;
302
-
303
236
  case 'FunctionDeclaration':
304
- declaration = Transform.FunctionDeclarationWithName( // possibly missing id
305
- node.declaration);
237
+ declaration = Transform.FunctionDeclarationWithName(node.declaration);
306
238
  break;
307
-
308
239
  case 'TSDeclareFunction':
309
240
  declaration = Transform.TSDeclareFunction(node.declaration);
310
241
  break;
311
-
312
242
  case 'Identifier':
313
243
  declaration = constructFlowNode({
314
244
  type: 'TypeofTypeAnnotation',
315
245
  argument: Transform.Identifier(node.declaration)
316
246
  });
317
247
  break;
318
-
319
248
  default:
320
249
  throw translationError(node.declaration, `Unsupported export declaration: ${node.declaration.type}`);
321
250
  }
322
-
323
251
  return constructFlowNode({
324
252
  type: 'DeclareExportDeclaration',
325
253
  declaration,
@@ -328,7 +256,6 @@ const getTransforms = (originalCode, opts) => {
328
256
  specifiers: []
329
257
  });
330
258
  }
331
-
332
259
  static ExportNamedDeclaration(node) {
333
260
  if (node.declaration == null) {
334
261
  const source = node.source == null ? null : constructFlowNode({
@@ -350,28 +277,23 @@ const getTransforms = (originalCode, opts) => {
350
277
  specifiers
351
278
  });
352
279
  }
353
-
354
280
  switch (node.declaration.type) {
355
281
  case 'ClassDeclaration':
356
282
  return constructFlowNode({
357
283
  type: 'DeclareExportDeclaration',
358
- declaration: Transform.ClassDeclarationWithName( // possibly missing id
359
- node.declaration),
284
+ declaration: Transform.ClassDeclarationWithName(node.declaration),
360
285
  default: false,
361
286
  source: null,
362
287
  specifiers: []
363
288
  });
364
-
365
289
  case 'FunctionDeclaration':
366
290
  return constructFlowNode({
367
291
  type: 'DeclareExportDeclaration',
368
- declaration: Transform.FunctionDeclarationWithName( // possibly missing id
369
- node.declaration),
292
+ declaration: Transform.FunctionDeclarationWithName(node.declaration),
370
293
  default: false,
371
294
  source: null,
372
295
  specifiers: []
373
296
  });
374
-
375
297
  case 'TSDeclareFunction':
376
298
  return constructFlowNode({
377
299
  type: 'DeclareExportDeclaration',
@@ -380,18 +302,14 @@ const getTransforms = (originalCode, opts) => {
380
302
  source: null,
381
303
  specifiers: []
382
304
  });
383
-
384
305
  case 'TSEnumDeclaration':
385
306
  throw translationError(node.declaration, `Unsupported export declaration: ${node.declaration.type}`);
386
-
387
307
  case 'TSModuleDeclaration':
388
308
  {
389
309
  const decl = Transform.TSModuleDeclaration(node.declaration);
390
-
391
310
  if (decl.id.type !== 'Identifier') {
392
311
  throw translationError(decl.id, `Unsupported module declaration id`);
393
312
  }
394
-
395
313
  return [decl, constructFlowNode({
396
314
  type: 'ExportNamedDeclaration',
397
315
  declaration: null,
@@ -404,7 +322,6 @@ const getTransforms = (originalCode, opts) => {
404
322
  })]
405
323
  })];
406
324
  }
407
-
408
325
  case 'TSInterfaceDeclaration':
409
326
  {
410
327
  const decl = Transform.TSInterfaceDeclaration(node.declaration);
@@ -422,7 +339,6 @@ const getTransforms = (originalCode, opts) => {
422
339
  specifiers: []
423
340
  });
424
341
  }
425
-
426
342
  case 'TSTypeAliasDeclaration':
427
343
  {
428
344
  const decl = Transform.TSTypeAliasDeclaration(node.declaration);
@@ -434,7 +350,6 @@ const getTransforms = (originalCode, opts) => {
434
350
  specifiers: []
435
351
  });
436
352
  }
437
-
438
353
  case 'VariableDeclaration':
439
354
  {
440
355
  return Transform.VariableDeclaration(node.declaration).map(declaration => constructFlowNode({
@@ -447,16 +362,13 @@ const getTransforms = (originalCode, opts) => {
447
362
  }
448
363
  }
449
364
  }
450
-
451
365
  static FunctionDeclarationWithName(node) {
452
366
  var _node$returnType;
453
-
454
367
  const {
455
368
  thisParam,
456
369
  restParam,
457
370
  params
458
371
  } = Transform._partitionAndTranslateTSFunctionParams(node.params);
459
-
460
372
  const fnAnnot = constructFlowNode({
461
373
  type: 'FunctionTypeAnnotation',
462
374
  typeParameters: Transform.TSTypeParameterDeclarationOpt(node.typeParameters),
@@ -467,19 +379,21 @@ const getTransforms = (originalCode, opts) => {
467
379
  });
468
380
  return constructFlowNode({
469
381
  type: 'DeclareFunction',
470
- id: { ...DUMMY_COMMON,
382
+ id: {
383
+ ...DUMMY_COMMON,
471
384
  type: 'Identifier',
472
385
  name: node.id.name,
473
- typeAnnotation: { ...DUMMY_COMMON,
386
+ typeAnnotation: {
387
+ ...DUMMY_COMMON,
474
388
  type: 'TypeAnnotation',
475
389
  typeAnnotation: fnAnnot
476
390
  },
477
391
  optional: false
478
392
  },
393
+ implicitDeclare: false,
479
394
  predicate: null
480
395
  });
481
396
  }
482
-
483
397
  static Identifier(node, optional) {
484
398
  return constructFlowNode({
485
399
  type: 'Identifier',
@@ -488,24 +402,20 @@ const getTransforms = (originalCode, opts) => {
488
402
  optional: Boolean(optional != null ? optional : node.optional)
489
403
  });
490
404
  }
491
-
492
405
  static ImportDeclaration(node) {
493
406
  const specifiers = node.specifiers.map(specifier => {
494
407
  var _specifier$importKind;
495
-
496
408
  switch (specifier.type) {
497
409
  case 'ImportNamespaceSpecifier':
498
410
  return constructFlowNode({
499
411
  type: 'ImportNamespaceSpecifier',
500
412
  local: Transform.Identifier(specifier.local)
501
413
  });
502
-
503
414
  case 'ImportDefaultSpecifier':
504
415
  return constructFlowNode({
505
416
  type: 'ImportDefaultSpecifier',
506
417
  local: Transform.Identifier(specifier.local)
507
418
  });
508
-
509
419
  case 'ImportSpecifier':
510
420
  return constructFlowNode({
511
421
  type: 'ImportSpecifier',
@@ -523,27 +433,22 @@ const getTransforms = (originalCode, opts) => {
523
433
  value: node.source.value,
524
434
  raw: node.source.raw
525
435
  }),
526
- importKind: // `import type React from 'react'` in TS means `import typeof React from react` in Flow
527
- specifiers.some(s => s.type === 'ImportDefaultSpecifier') && node.importKind === 'type' ? 'typeof' : node.importKind,
436
+ importKind: specifiers.some(s => s.type === 'ImportDefaultSpecifier') && node.importKind === 'type' ? 'typeof' : node.importKind,
528
437
  attributes: [],
529
438
  specifiers
530
439
  });
531
440
  }
532
-
533
441
  static LabeledStatement(node) {
534
442
  const body = Transform.Statement(node.body);
535
-
536
443
  if (Array.isArray(body)) {
537
444
  throw translationError(node.body, 'Unexpected array of statements');
538
445
  }
539
-
540
446
  return constructFlowNode({
541
447
  type: 'LabeledStatement',
542
448
  label: Transform.Identifier(node.label),
543
449
  body
544
450
  });
545
451
  }
546
-
547
452
  static Literal(node) {
548
453
  if (typeof node.value === 'number') {
549
454
  return constructFlowNode({
@@ -593,10 +498,8 @@ const getTransforms = (originalCode, opts) => {
593
498
  throw translationError(node, `Unsupported literal type ${typeof node.value}`);
594
499
  }
595
500
  }
596
-
597
501
  static LiteralType(node) {
598
502
  const literal = Transform.Literal(node);
599
-
600
503
  switch (literal.literalType) {
601
504
  case 'boolean':
602
505
  return constructFlowNode({
@@ -604,21 +507,18 @@ const getTransforms = (originalCode, opts) => {
604
507
  value: literal.value,
605
508
  raw: literal.raw
606
509
  });
607
-
608
510
  case 'numeric':
609
511
  return constructFlowNode({
610
512
  type: 'NumberLiteralTypeAnnotation',
611
513
  value: literal.value,
612
514
  raw: literal.raw
613
515
  });
614
-
615
516
  case 'string':
616
517
  return constructFlowNode({
617
518
  type: 'StringLiteralTypeAnnotation',
618
519
  value: literal.value,
619
520
  raw: literal.raw
620
521
  });
621
-
622
522
  case 'bigint':
623
523
  return constructFlowNode({
624
524
  type: 'BigIntLiteralTypeAnnotation',
@@ -626,89 +526,61 @@ const getTransforms = (originalCode, opts) => {
626
526
  bigint: literal.bigint,
627
527
  raw: literal.raw
628
528
  });
629
-
630
529
  case 'null':
631
530
  return constructFlowNode({
632
531
  type: 'NullLiteralTypeAnnotation',
633
532
  value: literal.value,
634
533
  raw: literal.raw
635
534
  });
636
-
637
535
  case 'regexp':
638
536
  return unsupportedAnnotation(node, 'regexp literal type');
639
-
640
537
  default:
641
538
  literal;
642
539
  throw 'unreachable';
643
540
  }
644
541
  }
645
-
646
542
  static AllStatement(node) {
647
543
  switch (node.type) {
648
544
  case 'BlockStatement':
649
545
  return Transform.BlockStatement(node);
650
-
651
546
  case 'ClassDeclaration':
652
547
  return Transform.ClassDeclarationWithName(node);
653
-
654
548
  case 'DebuggerStatement':
655
549
  return Transform.DebuggerStatement();
656
-
657
550
  case 'ExportAllDeclaration':
658
551
  return Transform.ExportAllDeclaration(node);
659
-
660
552
  case 'ExportDefaultDeclaration':
661
553
  return Transform.ExportDefaultDeclaration(node);
662
-
663
554
  case 'ExportNamedDeclaration':
664
555
  return Transform.ExportNamedDeclaration(node);
665
-
666
556
  case 'FunctionDeclaration':
667
557
  return Transform.FunctionDeclarationWithName(node);
668
-
669
558
  case 'ImportDeclaration':
670
559
  return Transform.ImportDeclaration(node);
671
-
672
560
  case 'LabeledStatement':
673
561
  return Transform.LabeledStatement(node);
674
-
675
562
  case 'TSDeclareFunction':
676
563
  return Transform.TSDeclareFunction(node);
677
-
678
564
  case 'TSEnumDeclaration':
679
565
  return Transform.TSEnumDeclaration(node);
680
-
681
566
  case 'TSExportAssignment':
682
567
  return Transform.TSExportAssignment(node);
683
-
684
568
  case 'TSImportEqualsDeclaration':
685
569
  return Transform.TSImportEqualsDeclaration(node);
686
-
687
570
  case 'TSInterfaceDeclaration':
688
571
  return Transform.TSInterfaceDeclaration(node);
689
-
690
572
  case 'TSModuleDeclaration':
691
573
  return Transform.TSModuleDeclaration(node);
692
-
693
574
  case 'TSNamespaceExportDeclaration':
694
- // Flow will never support `export as namespace` since we can't allow a normal file to
695
- // introduce a global out of nowhere, and because it's only useful for legacy module
696
- // system However, it's very reasonable to completely ignore it under some mode, so that
697
- // people using these libdefs won't have a lot of pain.
698
575
  return [];
699
-
700
576
  case 'TSTypeAliasDeclaration':
701
577
  return Transform.TSTypeAliasDeclaration(node);
702
-
703
578
  case 'VariableDeclaration':
704
579
  return Transform.VariableDeclaration(node);
705
-
706
580
  case 'ExpressionStatement':
707
581
  throw translationError(node, 'Unsupported expression statement');
708
-
709
582
  case 'WithStatement':
710
583
  throw translationError(node, 'Unsupported with statement');
711
-
712
584
  case 'BreakStatement':
713
585
  case 'ContinueStatement':
714
586
  case 'DoWhileStatement':
@@ -724,36 +596,30 @@ const getTransforms = (originalCode, opts) => {
724
596
  throw translationError(node, 'Unsupported control flow statement');
725
597
  }
726
598
  }
727
-
728
599
  static Statement(node) {
729
600
  return Transform.AllStatement(node);
730
601
  }
731
-
732
602
  static TSAnyType() {
733
603
  return constructFlowNode({
734
604
  type: 'AnyTypeAnnotation'
735
605
  });
736
606
  }
737
-
738
607
  static TSArrayType(node) {
739
608
  return constructFlowNode({
740
609
  type: 'ArrayTypeAnnotation',
741
610
  elementType: Transform.TSTypeAnnotation(node.elementType)
742
611
  });
743
612
  }
744
-
745
613
  static TSBigIntType() {
746
614
  return constructFlowNode({
747
615
  type: 'BigIntTypeAnnotation'
748
616
  });
749
617
  }
750
-
751
618
  static TSBooleanType() {
752
619
  return constructFlowNode({
753
620
  type: 'BooleanTypeAnnotation'
754
621
  });
755
622
  }
756
-
757
623
  static TSConditionalType(node) {
758
624
  return constructFlowNode({
759
625
  type: 'ConditionalTypeAnnotation',
@@ -763,26 +629,20 @@ const getTransforms = (originalCode, opts) => {
763
629
  falseType: Transform.TSTypeAnnotation(node.falseType)
764
630
  });
765
631
  }
766
-
767
632
  static TSConstructorType(node) {
768
633
  return unsupportedAnnotation(node, 'constructor types');
769
634
  }
770
-
771
635
  static TSDeclareFunction(node) {
772
636
  var _node$returnType2;
773
-
774
637
  if (node.id == null) {
775
638
  throw translationError(node, 'Missing function name');
776
639
  }
777
-
778
640
  const name = node.id.name;
779
-
780
641
  const {
781
642
  thisParam,
782
643
  restParam,
783
644
  params
784
645
  } = Transform._partitionAndTranslateTSFunctionParams(node.params);
785
-
786
646
  const fnAnnot = constructFlowNode({
787
647
  type: 'FunctionTypeAnnotation',
788
648
  typeParameters: Transform.TSTypeParameterDeclarationOpt(node.typeParameters),
@@ -793,26 +653,26 @@ const getTransforms = (originalCode, opts) => {
793
653
  });
794
654
  return constructFlowNode({
795
655
  type: 'DeclareFunction',
796
- id: { ...DUMMY_COMMON,
656
+ id: {
657
+ ...DUMMY_COMMON,
797
658
  type: 'Identifier',
798
659
  name,
799
- typeAnnotation: { ...DUMMY_COMMON,
660
+ typeAnnotation: {
661
+ ...DUMMY_COMMON,
800
662
  type: 'TypeAnnotation',
801
663
  typeAnnotation: fnAnnot
802
664
  },
803
665
  optional: false
804
666
  },
667
+ implicitDeclare: false,
805
668
  predicate: null
806
669
  });
807
670
  }
808
-
809
671
  static TSEnumDeclaration(node) {
810
672
  return unsupportedDeclaration(node, 'enums', node.id);
811
673
  }
812
-
813
674
  static TSExportAssignment(node) {
814
675
  let typeAnnotation;
815
-
816
676
  if (node.expression.type === 'Identifier') {
817
677
  typeAnnotation = constructFlowNode({
818
678
  type: 'TypeofTypeAnnotation',
@@ -823,7 +683,6 @@ const getTransforms = (originalCode, opts) => {
823
683
  } else {
824
684
  throw translationError(node, `Unsupported export assignment expression ${node.expression.type}`);
825
685
  }
826
-
827
686
  return constructFlowNode({
828
687
  type: 'DeclareModuleExports',
829
688
  typeAnnotation: constructFlowNode({
@@ -832,16 +691,13 @@ const getTransforms = (originalCode, opts) => {
832
691
  })
833
692
  });
834
693
  }
835
-
836
694
  static TSFunctionType(node, allowMissingReturn = false) {
837
695
  var _node$returnType3;
838
-
839
696
  const {
840
697
  thisParam,
841
698
  restParam,
842
699
  params
843
700
  } = Transform._partitionAndTranslateTSFunctionParams(node.params);
844
-
845
701
  return constructFlowNode({
846
702
  type: 'FunctionTypeAnnotation',
847
703
  typeParameters: Transform.TSTypeParameterDeclarationOpt(node.typeParameters),
@@ -853,16 +709,13 @@ const getTransforms = (originalCode, opts) => {
853
709
  this: thisParam
854
710
  });
855
711
  }
856
-
857
712
  static _partitionAndTranslateTSFunctionParams(tsParams) {
858
713
  const params = [...tsParams];
859
714
  const firstParam = params[0];
860
715
  let thisParam = null;
861
716
  let restParam = null;
862
-
863
717
  if (firstParam != null && firstParam.type === 'Identifier' && firstParam.name === 'this') {
864
718
  var _firstParam$typeAnnot;
865
-
866
719
  thisParam = constructFlowNode({
867
720
  type: 'FunctionTypeParam',
868
721
  name: constructFlowNode({
@@ -876,12 +729,9 @@ const getTransforms = (originalCode, opts) => {
876
729
  });
877
730
  params.shift();
878
731
  }
879
-
880
732
  const lastParam = params[params.length - 1];
881
-
882
733
  if (lastParam != null && lastParam.type === 'RestElement') {
883
734
  var _lastParam$typeAnnota;
884
-
885
735
  restParam = constructFlowNode({
886
736
  type: 'FunctionTypeParam',
887
737
  name: constructFlowNode({
@@ -895,14 +745,12 @@ const getTransforms = (originalCode, opts) => {
895
745
  });
896
746
  params.pop();
897
747
  }
898
-
899
748
  return {
900
749
  thisParam,
901
750
  restParam,
902
751
  params: params.map((param, i) => {
903
752
  if (param.type === 'Identifier') {
904
753
  var _param$typeAnnotation;
905
-
906
754
  return constructFlowNode({
907
755
  type: 'FunctionTypeParam',
908
756
  name: constructFlowNode({
@@ -916,7 +764,6 @@ const getTransforms = (originalCode, opts) => {
916
764
  });
917
765
  } else if (param.type === 'ArrayPattern' || param.type === 'ObjectPattern') {
918
766
  var _param$typeAnnotation2;
919
-
920
767
  return constructFlowNode({
921
768
  type: 'FunctionTypeParam',
922
769
  name: constructFlowNode({
@@ -934,8 +781,16 @@ const getTransforms = (originalCode, opts) => {
934
781
  })
935
782
  };
936
783
  }
937
-
938
784
  static TSImportType(node) {
785
+ var _node$argument;
786
+ const source = (_node$argument = node.argument) != null ? _node$argument : node.source == null ? null : {
787
+ type: 'TSLiteralType',
788
+ loc: node.source.loc,
789
+ literal: node.source
790
+ };
791
+ if (source == null) {
792
+ return unsupportedAnnotation(node, 'import types without a source');
793
+ }
939
794
  let base = constructFlowNode({
940
795
  type: 'GenericTypeAnnotation',
941
796
  id: constructFlowNode({
@@ -946,28 +801,22 @@ const getTransforms = (originalCode, opts) => {
946
801
  }),
947
802
  typeParameters: constructFlowNode({
948
803
  type: 'TypeParameterInstantiation',
949
- params: [Transform.TSTypeAnnotation(node.argument)]
804
+ params: [Transform.TSTypeAnnotation(source)]
950
805
  })
951
806
  });
952
-
807
+ if (node.typeArguments != null) {
808
+ return unsupportedAnnotation(node, 'import types with type arguments');
809
+ }
953
810
  if (node.qualifier == null) {
954
811
  return base;
955
812
  }
956
-
957
- if (node.typeParameters != null) {
958
- return unsupportedAnnotation(node, 'import types with type parameters');
959
- }
960
-
961
813
  let qualifier = Transform.EntityNameToTypeIdentifier(node.qualifier);
962
814
  const namesRev = [];
963
-
964
815
  while (qualifier.type !== 'Identifier') {
965
816
  namesRev.push(qualifier.id.name);
966
817
  qualifier = qualifier.qualification;
967
818
  }
968
-
969
819
  namesRev.push(qualifier.name);
970
-
971
820
  while (namesRev.length > 0) {
972
821
  const name = namesRev.pop();
973
822
  base = constructFlowNode({
@@ -975,24 +824,18 @@ const getTransforms = (originalCode, opts) => {
975
824
  objectType: base,
976
825
  indexType: constructFlowNode({
977
826
  type: 'StringLiteralTypeAnnotation',
978
- // $FlowFixMe[incompatible-type]
979
827
  value: name,
980
- // $FlowFixMe[incompatible-type]
981
828
  raw: `'${name}'`
982
829
  })
983
830
  });
984
831
  }
985
-
986
832
  return base;
987
833
  }
988
-
989
834
  static TSImportEqualsDeclaration(node) {
990
835
  if (node.moduleReference.type === 'ThisExpression' || node.moduleReference.type === 'TSQualifiedName') {
991
836
  return unsupportedDeclaration(node, 'import equals declaration with weird module reference', node.id);
992
837
  }
993
-
994
838
  let moduleName;
995
-
996
839
  if (node.moduleReference.type === 'TSExternalModuleReference') {
997
840
  if (node.moduleReference.expression.type === 'Literal') {
998
841
  moduleName = String(node.moduleReference.expression.value);
@@ -1002,7 +845,6 @@ const getTransforms = (originalCode, opts) => {
1002
845
  } else {
1003
846
  moduleName = node.moduleReference.name;
1004
847
  }
1005
-
1006
848
  return constructFlowNode({
1007
849
  type: 'VariableDeclaration',
1008
850
  kind: 'const',
@@ -1034,7 +876,6 @@ const getTransforms = (originalCode, opts) => {
1034
876
  })]
1035
877
  });
1036
878
  }
1037
-
1038
879
  static TSIndexedAccessType(node) {
1039
880
  return constructFlowNode({
1040
881
  type: 'IndexedAccessType',
@@ -1042,17 +883,14 @@ const getTransforms = (originalCode, opts) => {
1042
883
  indexType: Transform.TSTypeAnnotation(node.indexType)
1043
884
  });
1044
885
  }
1045
-
1046
886
  static TSInferType(node) {
1047
887
  return constructFlowNode({
1048
888
  type: 'InferTypeAnnotation',
1049
889
  typeParameter: Transform.TSTypeParameter(node.typeParameter)
1050
890
  });
1051
891
  }
1052
-
1053
892
  static TSInterfaceDeclaration(node) {
1054
- const body = Transform.TSTypeLiteralOrInterfaceBody(node.body); // $FlowFixMe[cannot-write]
1055
-
893
+ const body = Transform.TSTypeLiteralOrInterfaceBody(node.body);
1056
894
  body.inexact = false;
1057
895
  return constructFlowNode({
1058
896
  type: 'InterfaceDeclaration',
@@ -1062,7 +900,6 @@ const getTransforms = (originalCode, opts) => {
1062
900
  extends: (node.extends || []).map(e => Transform.TSInterfaceHeritage(e))
1063
901
  });
1064
902
  }
1065
-
1066
903
  static TSInterfaceHeritage(node) {
1067
904
  return constructFlowNode({
1068
905
  type: 'InterfaceExtends',
@@ -1070,7 +907,6 @@ const getTransforms = (originalCode, opts) => {
1070
907
  typeParameters: Transform.TSTypeParameterInstantiationOpt(node.typeArguments)
1071
908
  });
1072
909
  }
1073
-
1074
910
  static _expressionToIdOrQualifiedTypeId(node, kind) {
1075
911
  if (node.type === 'Identifier') {
1076
912
  return Transform.Identifier(node);
@@ -1085,36 +921,28 @@ const getTransforms = (originalCode, opts) => {
1085
921
  throw unexpectedTranslationError(node, `Expected ${kind} to be an Identifier or Member`);
1086
922
  }
1087
923
  }
1088
-
1089
924
  static TSIntersectionType(node) {
1090
925
  return constructFlowNode({
1091
926
  type: 'IntersectionTypeAnnotation',
1092
927
  types: node.types.map(node => Transform.TSTypeAnnotation(node))
1093
928
  });
1094
929
  }
1095
-
1096
930
  static TSLiteralType(node) {
1097
931
  switch (node.literal.type) {
1098
932
  case 'TemplateLiteral':
1099
933
  return unsupportedAnnotation(node, 'template literals');
1100
-
1101
934
  case 'Literal':
1102
935
  return Transform.LiteralType(node.literal);
1103
-
1104
936
  case 'UnaryExpression':
1105
937
  return unsupportedAnnotation(node, 'UnaryExpression literal type');
1106
-
1107
938
  case 'UpdateExpression':
1108
939
  return unsupportedAnnotation(node, 'UpdateExpression literal type');
1109
940
  }
1110
941
  }
1111
-
1112
942
  static TSMappedType(node) {
1113
943
  var _keyTparam$bound;
1114
-
1115
944
  const keyTparam = Transform.TSTypeParameter(node.typeParameter);
1116
- const sourceType = (_keyTparam$bound = keyTparam.bound) == null ? void 0 : _keyTparam$bound.typeAnnotation; // $FlowFixMe[cannot-write]
1117
-
945
+ const sourceType = (_keyTparam$bound = keyTparam.bound) == null ? void 0 : _keyTparam$bound.typeAnnotation;
1118
946
  keyTparam.bound = null;
1119
947
  const prop = constructFlowNode({
1120
948
  type: 'ObjectTypeMappedTypeProperty',
@@ -1125,8 +953,7 @@ const getTransforms = (originalCode, opts) => {
1125
953
  type: 'Variance',
1126
954
  kind: 'plus'
1127
955
  }) : null,
1128
- optional: node.optional === '+' ? 'PlusOptional' : node.optional === '-' ? 'MinusOptional' : // eslint-disable-next-line no-extra-boolean-cast
1129
- Boolean(node.optional) ? 'Optional' : null
956
+ optional: node.optional === '+' ? 'PlusOptional' : node.optional === '-' ? 'MinusOptional' : Boolean(node.optional) ? 'Optional' : null
1130
957
  });
1131
958
  return constructFlowNode({
1132
959
  type: 'ObjectTypeAnnotation',
@@ -1138,10 +965,8 @@ const getTransforms = (originalCode, opts) => {
1138
965
  inexact: false
1139
966
  });
1140
967
  }
1141
-
1142
968
  static TSModuleDeclaration(node) {
1143
969
  var _node$global;
1144
-
1145
970
  const body = node.body == null ? constructFlowNode({
1146
971
  type: 'BlockStatement',
1147
972
  body: []
@@ -1151,7 +976,6 @@ const getTransforms = (originalCode, opts) => {
1151
976
  type: 'BlockStatement',
1152
977
  body: node.body.body.flatMap(s => Transform.Statement(s))
1153
978
  });
1154
-
1155
979
  if (node.id.type === 'Literal') {
1156
980
  return constructFlowNode({
1157
981
  type: 'DeclareModule',
@@ -1159,22 +983,21 @@ const getTransforms = (originalCode, opts) => {
1159
983
  body
1160
984
  });
1161
985
  }
1162
-
1163
986
  if ((_node$global = node.global) != null ? _node$global : false) {
1164
987
  return unsupportedDeclaration(node, 'global declaration', node.id);
1165
988
  }
1166
-
1167
989
  return constructFlowNode({
1168
990
  type: 'DeclareNamespace',
991
+ global: false,
1169
992
  id: Transform.Identifier(node.id),
1170
- body
993
+ body,
994
+ implicitDeclare: false,
995
+ keyword: 'namespace'
1171
996
  });
1172
997
  }
1173
-
1174
998
  static TSNamedTupleMember(node) {
1175
999
  let optional = false;
1176
1000
  let elementType;
1177
-
1178
1001
  if (node.elementType.type === 'TSRestType') {
1179
1002
  const child = node.elementType;
1180
1003
  return constructFlowNode({
@@ -1190,7 +1013,6 @@ const getTransforms = (originalCode, opts) => {
1190
1013
  } else {
1191
1014
  elementType = Transform.TSTypeAnnotation(node.elementType);
1192
1015
  }
1193
-
1194
1016
  return constructFlowNode({
1195
1017
  type: 'TupleTypeLabeledElement',
1196
1018
  label: Transform.Identifier(node.label),
@@ -1199,25 +1021,21 @@ const getTransforms = (originalCode, opts) => {
1199
1021
  variance: null
1200
1022
  });
1201
1023
  }
1202
-
1203
1024
  static TSNeverType() {
1204
1025
  return constructFlowNode({
1205
1026
  type: 'EmptyTypeAnnotation'
1206
1027
  });
1207
1028
  }
1208
-
1209
1029
  static TSNullType() {
1210
1030
  return constructFlowNode({
1211
1031
  type: 'NullLiteralTypeAnnotation'
1212
1032
  });
1213
1033
  }
1214
-
1215
1034
  static TSNumberType() {
1216
1035
  return constructFlowNode({
1217
1036
  type: 'NumberTypeAnnotation'
1218
1037
  });
1219
1038
  }
1220
-
1221
1039
  static TSObjectType() {
1222
1040
  return constructFlowNode({
1223
1041
  type: 'InterfaceTypeAnnotation',
@@ -1233,7 +1051,6 @@ const getTransforms = (originalCode, opts) => {
1233
1051
  extends: []
1234
1052
  });
1235
1053
  }
1236
-
1237
1054
  static TSQualifiedNameToQualifiedTypeIdentifier(node) {
1238
1055
  return constructFlowNode({
1239
1056
  type: 'QualifiedTypeIdentifier',
@@ -1241,7 +1058,6 @@ const getTransforms = (originalCode, opts) => {
1241
1058
  id: Transform.Identifier(node.right)
1242
1059
  });
1243
1060
  }
1244
-
1245
1061
  static TSQualifiedNameToQualifiedTypeofIdentifier(node) {
1246
1062
  return constructFlowNode({
1247
1063
  type: 'QualifiedTypeofIdentifier',
@@ -1249,23 +1065,19 @@ const getTransforms = (originalCode, opts) => {
1249
1065
  id: Transform.Identifier(node.right)
1250
1066
  });
1251
1067
  }
1252
-
1253
1068
  static TSStringType() {
1254
1069
  return constructFlowNode({
1255
1070
  type: 'StringTypeAnnotation'
1256
1071
  });
1257
1072
  }
1258
-
1259
1073
  static TSSymbolType() {
1260
1074
  return constructFlowNode({
1261
1075
  type: 'SymbolTypeAnnotation'
1262
1076
  });
1263
1077
  }
1264
-
1265
1078
  static TSTemplateLiteralType(node) {
1266
1079
  return unsupportedAnnotation(node, 'constructor types');
1267
1080
  }
1268
-
1269
1081
  static TSThisType(_node) {
1270
1082
  return constructFlowNode({
1271
1083
  type: 'GenericTypeAnnotation',
@@ -1278,7 +1090,6 @@ const getTransforms = (originalCode, opts) => {
1278
1090
  typeParameters: null
1279
1091
  });
1280
1092
  }
1281
-
1282
1093
  static TSTupleType(node) {
1283
1094
  return constructFlowNode({
1284
1095
  type: 'TupleTypeAnnotation',
@@ -1286,7 +1097,6 @@ const getTransforms = (originalCode, opts) => {
1286
1097
  inexact: false
1287
1098
  });
1288
1099
  }
1289
-
1290
1100
  static TSTypeAliasDeclaration(node) {
1291
1101
  return constructFlowNode({
1292
1102
  type: 'TypeAlias',
@@ -1295,14 +1105,12 @@ const getTransforms = (originalCode, opts) => {
1295
1105
  right: Transform.TSTypeAnnotation(node.typeAnnotation)
1296
1106
  });
1297
1107
  }
1298
-
1299
1108
  static TSTypeAnnotation(node) {
1300
1109
  switch (node.type) {
1301
1110
  case 'TSOptionalType':
1302
1111
  case 'TSQualifiedName':
1303
1112
  case 'TSRestType':
1304
1113
  return unsupportedAnnotation(node, 'unexpected toplevel ' + node.type);
1305
-
1306
1114
  case 'TSAbstractKeyword':
1307
1115
  case 'TSAsyncKeyword':
1308
1116
  case 'TSDeclareKeyword':
@@ -1313,142 +1121,100 @@ const getTransforms = (originalCode, opts) => {
1313
1121
  case 'TSReadonlyKeyword':
1314
1122
  case 'TSStaticKeyword':
1315
1123
  return unsupportedAnnotation(node, 'wat keyword ' + node.type);
1316
-
1317
1124
  case 'TSAnyKeyword':
1318
1125
  return Transform.TSAnyType();
1319
-
1320
1126
  case 'TSArrayType':
1321
1127
  return Transform.TSArrayType(node);
1322
-
1323
1128
  case 'TSBigIntKeyword':
1324
1129
  return Transform.TSBigIntType();
1325
-
1326
1130
  case 'TSBooleanKeyword':
1327
1131
  return Transform.TSBooleanType();
1328
-
1329
1132
  case 'TSConditionalType':
1330
1133
  return Transform.TSConditionalType(node);
1331
-
1332
1134
  case 'TSConstructorType':
1333
1135
  return Transform.TSConstructorType(node);
1334
-
1335
1136
  case 'TSFunctionType':
1336
1137
  return Transform.TSFunctionType(node);
1337
-
1338
1138
  case 'TSImportType':
1339
1139
  return Transform.TSImportType(node);
1340
-
1341
1140
  case 'TSIndexedAccessType':
1342
1141
  return Transform.TSIndexedAccessType(node);
1343
-
1344
1142
  case 'TSInferType':
1345
1143
  return Transform.TSInferType(node);
1346
-
1347
1144
  case 'TSIntersectionType':
1348
1145
  return Transform.TSIntersectionType(node);
1349
-
1350
1146
  case 'TSIntrinsicKeyword':
1351
1147
  return unsupportedAnnotation(node, 'intrinsic keyword');
1352
-
1353
1148
  case 'TSLiteralType':
1354
1149
  return Transform.TSLiteralType(node);
1355
-
1356
1150
  case 'TSMappedType':
1357
1151
  return Transform.TSMappedType(node);
1358
-
1359
1152
  case 'TSNamedTupleMember':
1360
1153
  return Transform.TSNamedTupleMember(node);
1361
-
1362
1154
  case 'TSNeverKeyword':
1363
1155
  return Transform.TSNeverType();
1364
-
1365
1156
  case 'TSNullKeyword':
1366
1157
  return Transform.TSNullType();
1367
-
1368
1158
  case 'TSNumberKeyword':
1369
1159
  return Transform.TSNumberType();
1370
-
1371
1160
  case 'TSObjectKeyword':
1372
1161
  return Transform.TSObjectType();
1373
-
1374
1162
  case 'TSStringKeyword':
1375
1163
  return Transform.TSStringType();
1376
-
1377
1164
  case 'TSSymbolKeyword':
1378
1165
  return Transform.TSSymbolType();
1379
-
1380
1166
  case 'TSTemplateLiteralType':
1381
1167
  return Transform.TSTemplateLiteralType(node);
1382
-
1383
1168
  case 'TSThisType':
1384
1169
  return Transform.TSThisType(node);
1385
-
1386
1170
  case 'TSTupleType':
1387
1171
  return Transform.TSTupleType(node);
1388
-
1389
1172
  case 'TSTypeLiteral':
1390
1173
  return Transform.TSTypeLiteralOrInterfaceBody(node);
1391
-
1392
1174
  case 'TSTypeOperator':
1393
1175
  return Transform.TSTypeOperator(node);
1394
-
1395
1176
  case 'TSTypePredicate':
1396
1177
  return Transform.TSTypePredicate(node);
1397
-
1398
1178
  case 'TSTypeQuery':
1399
1179
  return Transform.TSTypeQuery(node);
1400
-
1401
1180
  case 'TSTypeReference':
1402
1181
  return Transform.TSTypeReference(node);
1403
-
1404
1182
  case 'TSUndefinedKeyword':
1405
1183
  case 'TSVoidKeyword':
1406
1184
  return Transform.TSUndefinedOrVoidType();
1407
-
1408
1185
  case 'TSUnionType':
1409
1186
  return Transform.TSUnionType(node);
1410
-
1411
1187
  case 'TSUnknownKeyword':
1412
1188
  return Transform.TSUnknownType();
1413
1189
  }
1414
1190
  }
1415
-
1416
1191
  static TSTypeAnnotationOpt(node) {
1417
1192
  return node == null ? constructFlowNode({
1418
1193
  type: 'AnyTypeAnnotation'
1419
1194
  }) : Transform.TSTypeAnnotation(node);
1420
1195
  }
1421
-
1422
1196
  static TSTypeAnnotationNode(node) {
1423
1197
  return constructFlowNode({
1424
1198
  type: 'TypeAnnotation',
1425
1199
  typeAnnotation: Transform.TSTypeAnnotation(node.typeAnnotation)
1426
1200
  });
1427
1201
  }
1428
- /** A very confusingly named object type */
1429
-
1430
-
1431
1202
  static TSTypeLiteralOrInterfaceBody(node) {
1432
1203
  const properties = [];
1433
1204
  const indexers = [];
1434
1205
  const callProperties = [];
1435
-
1436
1206
  for (const prop of node.type === 'TSTypeLiteral' ? node.members : node.body) {
1437
1207
  switch (prop.type) {
1438
1208
  case 'TSPropertySignature':
1439
1209
  {
1440
1210
  Transform._translateIntoObjectProp(prop, properties, indexers);
1441
-
1442
1211
  break;
1443
1212
  }
1444
-
1445
1213
  case 'TSMethodSignature':
1446
1214
  {
1447
1215
  Transform._translateIntoObjectMethod(prop, properties);
1448
-
1449
1216
  break;
1450
1217
  }
1451
-
1452
1218
  case 'TSCallSignatureDeclaration':
1453
1219
  callProperties.push(constructFlowNode({
1454
1220
  type: 'ObjectTypeCallProperty',
@@ -1466,12 +1232,9 @@ const getTransforms = (originalCode, opts) => {
1466
1232
  })
1467
1233
  }));
1468
1234
  break;
1469
-
1470
1235
  case 'TSIndexSignature':
1471
1236
  {
1472
1237
  var _prop$typeAnnotation;
1473
-
1474
- // eslint-disable-next-line no-extra-boolean-cast
1475
1238
  const variance = Boolean(prop.readonly) ? constructFlowNode({
1476
1239
  type: 'Variance',
1477
1240
  kind: 'plus'
@@ -1492,7 +1255,6 @@ const getTransforms = (originalCode, opts) => {
1492
1255
  }));
1493
1256
  break;
1494
1257
  }
1495
-
1496
1258
  case 'TSConstructSignatureDeclaration':
1497
1259
  properties.push(constructFlowNode({
1498
1260
  type: 'ObjectTypeProperty',
@@ -1519,7 +1281,6 @@ const getTransforms = (originalCode, opts) => {
1519
1281
  break;
1520
1282
  }
1521
1283
  }
1522
-
1523
1284
  return constructFlowNode({
1524
1285
  type: 'ObjectTypeAnnotation',
1525
1286
  properties,
@@ -1530,17 +1291,13 @@ const getTransforms = (originalCode, opts) => {
1530
1291
  inexact: true
1531
1292
  });
1532
1293
  }
1533
-
1534
1294
  static _translateIntoObjectProp(prop, properties, indexers) {
1535
- // eslint-disable-next-line no-extra-boolean-cast
1536
1295
  const variance = Boolean(prop.readonly) ? constructFlowNode({
1537
1296
  type: 'Variance',
1538
1297
  kind: 'plus'
1539
1298
  }) : null;
1540
-
1541
1299
  if (prop.computed === false) {
1542
1300
  var _prop$typeAnnotation2;
1543
-
1544
1301
  const key = prop.key;
1545
1302
  properties.push(constructFlowNode({
1546
1303
  type: 'ObjectTypeProperty',
@@ -1563,7 +1320,6 @@ const getTransforms = (originalCode, opts) => {
1563
1320
  }));
1564
1321
  } else {
1565
1322
  var _prop$typeAnnotation3;
1566
-
1567
1323
  indexers.push(constructFlowNode({
1568
1324
  type: 'ObjectTypeIndexer',
1569
1325
  kind: 'init',
@@ -1580,12 +1336,10 @@ const getTransforms = (originalCode, opts) => {
1580
1336
  }));
1581
1337
  }
1582
1338
  }
1583
-
1584
1339
  static _translateIntoObjectMethod(prop, properties) {
1585
1340
  if (prop.computed === true) {
1586
1341
  throw translationError(prop, 'computed method signature');
1587
1342
  }
1588
-
1589
1343
  const originalKey = prop.key;
1590
1344
  const key = originalKey.type === 'Identifier' ? Transform.Identifier(originalKey, false) : originalKey.type === 'PrivateIdentifier' ? constructFlowNode({
1591
1345
  type: 'PrivateIdentifier',
@@ -1603,7 +1357,6 @@ const getTransforms = (originalCode, opts) => {
1603
1357
  returnType: prop.type === 'MethodDefinition' || prop.type === 'TSAbstractMethodDefinition' ? prop.value.returnType : prop.returnType,
1604
1358
  typeParameters: prop.typeParameters
1605
1359
  }, true);
1606
-
1607
1360
  if (prop.kind === 'method' || prop.kind === 'constructor') {
1608
1361
  properties.push(constructFlowNode({
1609
1362
  type: 'ObjectTypeProperty',
@@ -1630,22 +1383,18 @@ const getTransforms = (originalCode, opts) => {
1630
1383
  }));
1631
1384
  }
1632
1385
  }
1633
-
1634
1386
  static TSTypeOperator(node) {
1635
1387
  switch (node.operator) {
1636
1388
  case 'unique':
1637
1389
  return unsupportedAnnotation(node, 'unique operator');
1638
-
1639
1390
  case 'keyof':
1640
1391
  return constructFlowNode({
1641
1392
  type: 'KeyofTypeAnnotation',
1642
1393
  argument: Transform.TSTypeAnnotationOpt(node.typeAnnotation)
1643
1394
  });
1644
-
1645
1395
  case 'readonly':
1646
1396
  {
1647
1397
  const child = node.typeAnnotation;
1648
-
1649
1398
  switch (child == null ? void 0 : child.type) {
1650
1399
  case 'TSArrayType':
1651
1400
  return constructFlowNode({
@@ -1661,7 +1410,6 @@ const getTransforms = (originalCode, opts) => {
1661
1410
  params: [Transform.TSTypeAnnotation(child.elementType)]
1662
1411
  })
1663
1412
  });
1664
-
1665
1413
  case 'TSTupleType':
1666
1414
  return constructFlowNode({
1667
1415
  type: 'GenericTypeAnnotation',
@@ -1676,14 +1424,12 @@ const getTransforms = (originalCode, opts) => {
1676
1424
  params: [Transform.TSTypeAnnotation(child)]
1677
1425
  })
1678
1426
  });
1679
-
1680
1427
  default:
1681
1428
  return unsupportedAnnotation(node, 'readonly operator with inner type: ' + ((child == null ? void 0 : child.type) || 'null'));
1682
1429
  }
1683
1430
  }
1684
1431
  }
1685
1432
  }
1686
-
1687
1433
  static TSTypeParameter(node) {
1688
1434
  return constructFlowNode({
1689
1435
  type: 'TypeParameter',
@@ -1701,29 +1447,24 @@ const getTransforms = (originalCode, opts) => {
1701
1447
  })
1702
1448
  });
1703
1449
  }
1704
-
1705
1450
  static TSTypeParameterDeclaration(node) {
1706
1451
  return constructFlowNode({
1707
1452
  type: 'TypeParameterDeclaration',
1708
1453
  params: node.params.map(node => Transform.TSTypeParameter(node))
1709
1454
  });
1710
1455
  }
1711
-
1712
1456
  static TSTypeParameterDeclarationOpt(node) {
1713
1457
  return node != null ? Transform.TSTypeParameterDeclaration(node) : null;
1714
1458
  }
1715
-
1716
1459
  static TSTypeParameterInstantiation(node) {
1717
1460
  return constructFlowNode({
1718
1461
  type: 'TypeParameterInstantiation',
1719
1462
  params: node.params.map(node => Transform.TSTypeAnnotation(node))
1720
1463
  });
1721
1464
  }
1722
-
1723
1465
  static TSTypeParameterInstantiationOpt(node) {
1724
1466
  return node != null ? Transform.TSTypeParameterInstantiation(node) : null;
1725
1467
  }
1726
-
1727
1468
  static TSTypePredicate(node) {
1728
1469
  return constructFlowNode({
1729
1470
  type: 'TypePredicate',
@@ -1737,17 +1478,20 @@ const getTransforms = (originalCode, opts) => {
1737
1478
  typeAnnotation: node.typeAnnotation == null ? null : Transform.TSTypeAnnotation(node.typeAnnotation.typeAnnotation)
1738
1479
  });
1739
1480
  }
1740
-
1741
1481
  static TSTypeQuery(node) {
1742
1482
  var _Transform$TSTypePara;
1743
-
1483
+ if (node.exprName.type === 'TSImportType') {
1484
+ if (node.typeArguments != null) {
1485
+ return unsupportedAnnotation(node, 'type query imports with type arguments');
1486
+ }
1487
+ return Transform.TSImportType(node.exprName);
1488
+ }
1744
1489
  return constructFlowNode({
1745
1490
  type: 'TypeofTypeAnnotation',
1746
1491
  argument: Transform.EntityNameToTypeofIdentifier(node.exprName),
1747
- typeArguments: (_Transform$TSTypePara = Transform.TSTypeParameterInstantiationOpt(node.typeParameters)) != null ? _Transform$TSTypePara : undefined
1492
+ typeArguments: (_Transform$TSTypePara = Transform.TSTypeParameterInstantiationOpt(node.typeArguments)) != null ? _Transform$TSTypePara : undefined
1748
1493
  });
1749
1494
  }
1750
-
1751
1495
  static TSTypeReference(node) {
1752
1496
  return constructFlowNode({
1753
1497
  type: 'GenericTypeAnnotation',
@@ -1755,36 +1499,29 @@ const getTransforms = (originalCode, opts) => {
1755
1499
  typeParameters: Transform.TSTypeParameterInstantiationOpt(node.typeArguments)
1756
1500
  });
1757
1501
  }
1758
-
1759
1502
  static TSUndefinedOrVoidType() {
1760
1503
  return constructFlowNode({
1761
1504
  type: 'VoidTypeAnnotation'
1762
1505
  });
1763
1506
  }
1764
-
1765
1507
  static TSUnionType(node) {
1766
1508
  return constructFlowNode({
1767
1509
  type: 'UnionTypeAnnotation',
1768
1510
  types: node.types.map(node => Transform.TSTypeAnnotation(node))
1769
1511
  });
1770
1512
  }
1771
-
1772
1513
  static TSUnknownType() {
1773
1514
  return constructFlowNode({
1774
1515
  type: 'MixedTypeAnnotation'
1775
1516
  });
1776
1517
  }
1777
-
1778
1518
  static VariableDeclaration(node) {
1779
1519
  return node.declarations.map(decl => {
1780
1520
  if (decl.id.type !== 'Identifier') {
1781
1521
  throw translationError(decl.id, 'Non-identifier variable declaration');
1782
1522
  }
1783
-
1784
1523
  const id = Transform.Identifier(decl.id);
1785
-
1786
1524
  if (id.typeAnnotation == null) {
1787
- // $FlowExpectedError[cannot-write]
1788
1525
  id.typeAnnotation = constructFlowNode({
1789
1526
  type: 'TypeAnnotation',
1790
1527
  typeAnnotation: constructFlowNode({
@@ -1792,16 +1529,18 @@ const getTransforms = (originalCode, opts) => {
1792
1529
  })
1793
1530
  });
1794
1531
  }
1795
-
1796
1532
  return constructFlowNode({
1797
1533
  type: 'DeclareVariable',
1798
- id,
1799
- kind: node.kind
1534
+ declarations: [constructFlowNode({
1535
+ type: 'VariableDeclarator',
1536
+ id,
1537
+ init: null
1538
+ })],
1539
+ kind: node.kind,
1540
+ implicitDeclare: false
1800
1541
  });
1801
1542
  });
1802
1543
  }
1803
-
1804
1544
  }
1805
-
1806
1545
  return [Transform, code];
1807
1546
  };