flow-api-translator 0.10.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.
@@ -0,0 +1,2325 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ * @format
9
+ */
10
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.flowDefToTSDef = flowDefToTSDef;
16
+
17
+ var FlowESTree = _interopRequireWildcard(require("hermes-estree"));
18
+
19
+ var _hermesTransform = require("hermes-transform");
20
+
21
+ var TSESTree = _interopRequireWildcard(require("./utils/ts-estree-ast-types"));
22
+
23
+ var _ErrorUtils = require("./utils/ErrorUtils");
24
+
25
+ var _DocblockUtils = require("./utils/DocblockUtils");
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
+
31
+ const cloneJSDocCommentsToNewNode = // $FlowExpectedError[incompatible-cast] - trust me this re-type is 100% safe
32
+ _hermesTransform.cloneJSDocCommentsToNewNode;
33
+ const VALID_REACT_IMPORTS = new Set(['React', 'react']);
34
+
35
+ function flowDefToTSDef(originalCode, ast, scopeManager) {
36
+ const tsBody = [];
37
+ const tsProgram = {
38
+ type: 'Program',
39
+ body: tsBody,
40
+ sourceType: ast.sourceType,
41
+ docblock: ast.docblock == null ? null : (0, _DocblockUtils.removeAtFlowFromDocblock)(ast.docblock)
42
+ };
43
+ const transform = getTransforms(originalCode, scopeManager);
44
+
45
+ for (const node of ast.body) {
46
+ if (node.type in transform) {
47
+ const result = transform[// $FlowExpectedError[prop-missing]
48
+ node.type]( // $FlowExpectedError[incompatible-type]
49
+ // $FlowExpectedError[prop-missing]
50
+ node);
51
+ tsBody.push(...(Array.isArray(result) ? result : [result]));
52
+ } else {
53
+ throw (0, _ErrorUtils.unexpectedTranslationError)(node, `Unexpected node type ${node.type}`, {
54
+ code: originalCode
55
+ });
56
+ }
57
+ }
58
+
59
+ return tsProgram;
60
+ }
61
+
62
+ const getTransforms = (code, scopeManager) => {
63
+ function translationError(node, message) {
64
+ return (0, _ErrorUtils.translationError)(node, message, {
65
+ code
66
+ });
67
+ }
68
+
69
+ function unexpectedTranslationError(node, message) {
70
+ return (0, _ErrorUtils.unexpectedTranslationError)(node, message, {
71
+ code
72
+ });
73
+ }
74
+
75
+ function unsupportedTranslationError(node, thing) {
76
+ return translationError(node, `Unsupported feature: Translating "${thing}" is currently not supported.`);
77
+ }
78
+
79
+ const topScope = (() => {
80
+ const globalScope = scopeManager.globalScope;
81
+
82
+ if (globalScope.childScopes.length > 0 && globalScope.childScopes[0].type === 'module') {
83
+ return globalScope.childScopes[0];
84
+ }
85
+
86
+ return globalScope;
87
+ })();
88
+
89
+ function isReactImport(id) {
90
+ let currentScope = scopeManager.acquire(id);
91
+
92
+ const variableDef = (() => {
93
+ while (currentScope != null) {
94
+ for (const variable of currentScope.variables) {
95
+ if (variable.defs.length && variable.name === id.name) {
96
+ return variable;
97
+ }
98
+ }
99
+
100
+ currentScope = currentScope.upper;
101
+ }
102
+ })(); // No variable found, it must be global. Using the `React` variable is enough in this case.
103
+
104
+
105
+ if (variableDef == null) {
106
+ return VALID_REACT_IMPORTS.has(id.name);
107
+ }
108
+
109
+ const def = variableDef.defs[0]; // Detect:
110
+
111
+ switch (def.type) {
112
+ // import React from 'react';
113
+ // import * as React from 'react';
114
+ case 'ImportBinding':
115
+ {
116
+ if (def.node.type === 'ImportDefaultSpecifier' || def.node.type === 'ImportNamespaceSpecifier') {
117
+ return VALID_REACT_IMPORTS.has(def.parent.source.value);
118
+ }
119
+
120
+ return false;
121
+ }
122
+ // Globals
123
+
124
+ case 'ImplicitGlobalVariable':
125
+ {
126
+ return VALID_REACT_IMPORTS.has(id.name);
127
+ }
128
+ // TODO Handle:
129
+ // const React = require('react');
130
+ // const Something = React;
131
+ }
132
+
133
+ return false;
134
+ }
135
+
136
+ const transform = {
137
+ AnyTypeAnnotation(_node) {
138
+ return {
139
+ type: 'TSAnyKeyword'
140
+ };
141
+ },
142
+
143
+ ArrayTypeAnnotation(node) {
144
+ return {
145
+ type: 'TSArrayType',
146
+ elementType: transform.TypeAnnotationType(node.elementType)
147
+ };
148
+ },
149
+
150
+ BigIntLiteral(node) {
151
+ return {
152
+ type: 'Literal',
153
+ bigint: node.bigint,
154
+ raw: node.raw,
155
+ value: node.value
156
+ };
157
+ },
158
+
159
+ BigIntLiteralTypeAnnotation(node) {
160
+ var _node$bigint;
161
+
162
+ // technically hermes doesn't support this yet
163
+ // but future proofing amirite
164
+ const bigint = // $FlowExpectedError[prop-missing]
165
+ (_node$bigint = node.bigint) != null ? _node$bigint : node.raw // estree spec is to not have a trailing `n` on this property
166
+ // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
167
+ .replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
168
+ .replace(/_/, '');
169
+ return {
170
+ type: 'TSLiteralType',
171
+ literal: {
172
+ type: 'Literal',
173
+ value: node.value,
174
+ raw: node.raw,
175
+ bigint
176
+ }
177
+ };
178
+ },
179
+
180
+ BigIntTypeAnnotation(_node) {
181
+ return {
182
+ type: 'TSBigIntKeyword'
183
+ };
184
+ },
185
+
186
+ BooleanLiteral(node) {
187
+ return {
188
+ type: 'Literal',
189
+ raw: node.raw,
190
+ value: node.value
191
+ };
192
+ },
193
+
194
+ BooleanLiteralTypeAnnotation(node) {
195
+ return {
196
+ type: 'TSLiteralType',
197
+ literal: {
198
+ type: 'Literal',
199
+ value: node.value,
200
+ raw: node.raw
201
+ }
202
+ };
203
+ },
204
+
205
+ BooleanTypeAnnotation(_node) {
206
+ return {
207
+ type: 'TSBooleanKeyword'
208
+ };
209
+ },
210
+
211
+ ClassImplements(node) {
212
+ return {
213
+ type: 'TSClassImplements',
214
+ expression: transform.Identifier(node.id, false),
215
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
216
+ };
217
+ },
218
+
219
+ DeclareClass(node) {
220
+ const classMembers = [];
221
+ const transformedBody = transform.ObjectTypeAnnotation(node.body);
222
+
223
+ if (transformedBody.type !== 'TSTypeLiteral') {
224
+ throw translationError(node.body, 'Spreads in declare class are not allowed');
225
+ }
226
+
227
+ for (const member of transformedBody.members) {
228
+ // TS uses the real ClassDeclaration AST so we need to
229
+ // make the signatures real methods/properties
230
+ switch (member.type) {
231
+ case 'TSIndexSignature':
232
+ {
233
+ classMembers.push(member);
234
+ break;
235
+ }
236
+
237
+ case 'TSMethodSignature':
238
+ {
239
+ // flow just creates a method signature like any other for a constructor
240
+ // but the proper AST has `kind = 'constructor'`
241
+ const isConstructor = (() => {
242
+ if (member.computed === true) {
243
+ return false;
244
+ }
245
+
246
+ return member.key.type === 'Identifier' && member.key.name === 'constructor' || member.key.type === 'Literal' && member.key.value === 'constructor';
247
+ })();
248
+
249
+ if (isConstructor) {
250
+ const newNode = {
251
+ type: 'MethodDefinition',
252
+ accessibility: undefined,
253
+ computed: false,
254
+ key: {
255
+ type: 'Identifier',
256
+ name: 'constructor'
257
+ },
258
+ kind: 'constructor',
259
+ optional: false,
260
+ override: false,
261
+ static: false,
262
+ value: {
263
+ type: 'TSEmptyBodyFunctionExpression',
264
+ async: false,
265
+ body: null,
266
+ declare: false,
267
+ expression: false,
268
+ generator: false,
269
+ id: null,
270
+ params: member.params,
271
+ // constructors explicitly have no return type
272
+ returnType: undefined,
273
+ typeParameters: member.typeParameters
274
+ }
275
+ };
276
+ cloneJSDocCommentsToNewNode(member, newNode);
277
+ classMembers.push(newNode);
278
+ } else {
279
+ var _member$computed, _member$static;
280
+
281
+ const newNode = {
282
+ type: 'MethodDefinition',
283
+ accessibility: member.accessibility,
284
+ computed: (_member$computed = member.computed) != null ? _member$computed : false,
285
+ key: member.key,
286
+ kind: member.kind,
287
+ optional: member.optional,
288
+ override: false,
289
+ static: (_member$static = member.static) != null ? _member$static : false,
290
+ value: {
291
+ type: 'TSEmptyBodyFunctionExpression',
292
+ async: false,
293
+ body: null,
294
+ declare: false,
295
+ expression: false,
296
+ generator: false,
297
+ id: null,
298
+ params: member.params,
299
+ returnType: member.returnType,
300
+ typeParameters: member.typeParameters
301
+ }
302
+ };
303
+ cloneJSDocCommentsToNewNode(member, newNode);
304
+ classMembers.push(newNode);
305
+ }
306
+
307
+ break;
308
+ }
309
+
310
+ case 'TSPropertySignature':
311
+ {
312
+ var _member$computed2, _member$static2;
313
+
314
+ const newNode = {
315
+ type: 'PropertyDefinition',
316
+ accessibility: member.accessibility,
317
+ computed: (_member$computed2 = member.computed) != null ? _member$computed2 : false,
318
+ declare: false,
319
+ key: member.key,
320
+ optional: member.optional,
321
+ readonly: member.readonly,
322
+ static: (_member$static2 = member.static) != null ? _member$static2 : false,
323
+ typeAnnotation: member.typeAnnotation,
324
+ value: null
325
+ };
326
+ cloneJSDocCommentsToNewNode(member, newNode);
327
+ classMembers.push(newNode);
328
+ break;
329
+ }
330
+
331
+ case 'TSCallSignatureDeclaration':
332
+ {
333
+ var _node$body$callProper;
334
+
335
+ /*
336
+ TODO - callProperties
337
+ It's not valid to directly declare a call property on a class in TS
338
+ You can do it, but it's a big complication in the AST:
339
+ ```ts
340
+ declare Class {
341
+ // ...
342
+ }
343
+ interface ClassConstructor {
344
+ new (): Class;
345
+ // call sigs
346
+ (): Type;
347
+ }
348
+ ```
349
+ Let's put a pin in it for now and deal with it later if the need arises.
350
+ */
351
+ throw unsupportedTranslationError((_node$body$callProper = node.body.callProperties[0]) != null ? _node$body$callProper : node.body, 'call signatures on classes');
352
+ }
353
+
354
+ default:
355
+ throw unexpectedTranslationError(node.body, `Unexpected member type ${member.type}`);
356
+ }
357
+ }
358
+
359
+ const superClass = node.extends.length > 0 ? node.extends[0] : undefined;
360
+ return {
361
+ type: 'ClassDeclaration',
362
+ body: {
363
+ type: 'ClassBody',
364
+ body: classMembers
365
+ },
366
+ declare: true,
367
+ id: transform.Identifier(node.id, false),
368
+ implements: node.implements == null ? undefined : node.implements.map(transform.ClassImplements),
369
+ superClass: superClass == null ? null : transform.Identifier(superClass.id, false),
370
+ superTypeParameters: (superClass == null ? void 0 : superClass.typeParameters) == null ? undefined : transform.TypeParameterInstantiation(superClass.typeParameters),
371
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters) // TODO - mixins??
372
+
373
+ };
374
+ },
375
+
376
+ DeclareExportDeclaration(node) {
377
+ if (node.default === true) {
378
+ const declaration = node.declaration;
379
+
380
+ switch (declaration.type) {
381
+ // TS doesn't support direct default export for declare'd classes
382
+ case 'DeclareClass':
383
+ {
384
+ const classDecl = transform.DeclareClass(declaration);
385
+ const name = declaration.id.name;
386
+ return [classDecl, {
387
+ type: 'ExportDefaultDeclaration',
388
+ declaration: {
389
+ type: 'Identifier',
390
+ name
391
+ },
392
+ exportKind: 'value'
393
+ }];
394
+ }
395
+ // TS doesn't support direct default export for declare'd functions
396
+
397
+ case 'DeclareFunction':
398
+ {
399
+ const functionDecl = transform.DeclareFunction(declaration);
400
+ const name = declaration.id.name;
401
+ return [functionDecl, {
402
+ type: 'ExportDefaultDeclaration',
403
+ declaration: {
404
+ type: 'Identifier',
405
+ name
406
+ },
407
+ exportKind: 'value'
408
+ }];
409
+ }
410
+ // Flow's declare export default Identifier is ambiguous.
411
+ // the Identifier might reference a type, or it might reference a value
412
+ // - If it's a value, then that's all good, TS supports that.
413
+ // - If it's a type, that's a problem - TS only allows value variables to be exported
414
+ // so we need to create an intermediate variable to hold the type.
415
+
416
+ case 'GenericTypeAnnotation':
417
+ {
418
+ const referencedId = declaration.id; // QualifiedTypeIdentifiers are types so cannot be handled without the intermediate variable so
419
+ // only Identifiers can be handled here.
420
+
421
+ if (referencedId.type === 'Identifier') {
422
+ const exportedVar = topScope.set.get(referencedId.name);
423
+
424
+ if (exportedVar == null || exportedVar.defs.length !== 1) {
425
+ throw unexpectedTranslationError(referencedId, `Unable to find exported variable ${referencedId.name}`);
426
+ }
427
+
428
+ const def = exportedVar.defs[0];
429
+
430
+ switch (def.type) {
431
+ case 'ImportBinding':
432
+ {
433
+ // `import type { Wut } from 'mod'; declare export default Wut;`
434
+ // `import { type Wut } from 'mod'; declare export default Wut;`
435
+ // these cases should be wrapped in a variable because they're exporting a type, not a value
436
+ const specifier = def.node;
437
+
438
+ if (specifier.importKind === 'type' || specifier.parent.importKind === 'type') {
439
+ // fallthrough to the "default" handling
440
+ break;
441
+ } // intentional fallthrough to the "value" handling
442
+
443
+ }
444
+
445
+ case 'ClassName':
446
+ case 'Enum':
447
+ case 'FunctionName':
448
+ case 'ImplicitGlobalVariable':
449
+ case 'Variable':
450
+ // there's already a variable defined to hold the type
451
+ return {
452
+ type: 'ExportDefaultDeclaration',
453
+ declaration: {
454
+ type: 'Identifier',
455
+ name: referencedId.name
456
+ },
457
+ exportKind: 'value'
458
+ };
459
+
460
+ case 'CatchClause':
461
+ case 'Parameter':
462
+ case 'TypeParameter':
463
+ throw translationError(def.node, `Unexpected variable def type: ${def.type}`);
464
+
465
+ case 'Type':
466
+ // fallthrough to the "default" handling
467
+ break;
468
+ }
469
+ } // intentional fallthrough to the "default" handling
470
+
471
+ }
472
+
473
+ default:
474
+ {
475
+ /*
476
+ flow allows syntax like
477
+ ```
478
+ declare export default TypeName;
479
+ ```
480
+ but TS does not, so we have to declare a temporary variable to
481
+ reference in the export declaration:
482
+ ```
483
+ declare const $$EXPORT_DEFAULT_DECLARATION$$: TypeName;
484
+ export default $$EXPORT_DEFAULT_DECLARATION$$;
485
+ ```
486
+ */
487
+ const SPECIFIER = '$$EXPORT_DEFAULT_DECLARATION$$';
488
+ return [{
489
+ type: 'VariableDeclaration',
490
+ declarations: [{
491
+ type: 'VariableDeclarator',
492
+ id: {
493
+ type: 'Identifier',
494
+ name: SPECIFIER,
495
+ typeAnnotation: {
496
+ type: 'TSTypeAnnotation',
497
+ typeAnnotation: transform.TypeAnnotationType(declaration)
498
+ }
499
+ },
500
+ init: null
501
+ }],
502
+ declare: true,
503
+ kind: 'const'
504
+ }, {
505
+ type: 'ExportDefaultDeclaration',
506
+ declaration: {
507
+ type: 'Identifier',
508
+ name: SPECIFIER
509
+ },
510
+ exportKind: 'value'
511
+ }];
512
+ }
513
+ }
514
+ } else {
515
+ // eslint-disable-next-line eqeqeq
516
+ if (node.source === null) {
517
+ // eslint-disable-next-line eqeqeq
518
+ if (node.declaration === null) {
519
+ return {
520
+ type: 'ExportNamedDeclaration',
521
+ // flow does not currently support assertions
522
+ assertions: [],
523
+ declaration: null,
524
+ // flow does not support declared type exports with specifiers
525
+ exportKind: 'value',
526
+ source: null,
527
+ specifiers: node.specifiers.map(transform.ExportSpecifier)
528
+ };
529
+ }
530
+
531
+ const {
532
+ declaration,
533
+ exportKind
534
+ } = (() => {
535
+ switch (node.declaration.type) {
536
+ case 'DeclareClass':
537
+ return {
538
+ declaration: transform.DeclareClass(node.declaration),
539
+ exportKind: 'value'
540
+ };
541
+
542
+ case 'DeclareFunction':
543
+ return {
544
+ declaration: transform.DeclareFunction(node.declaration),
545
+ exportKind: 'value'
546
+ };
547
+
548
+ case 'DeclareInterface':
549
+ return {
550
+ declaration: transform.DeclareInterface(node.declaration),
551
+ exportKind: 'type'
552
+ };
553
+
554
+ case 'DeclareOpaqueType':
555
+ return {
556
+ declaration: transform.DeclareOpaqueType(node.declaration),
557
+ exportKind: 'type'
558
+ };
559
+
560
+ case 'DeclareVariable':
561
+ return {
562
+ declaration: transform.DeclareVariable(node.declaration),
563
+ exportKind: 'value'
564
+ };
565
+ }
566
+ })();
567
+
568
+ return {
569
+ type: 'ExportNamedDeclaration',
570
+ // flow does not currently support assertions
571
+ assertions: [],
572
+ declaration,
573
+ exportKind,
574
+ source: null,
575
+ specifiers: []
576
+ };
577
+ } else {
578
+ return {
579
+ type: 'ExportNamedDeclaration',
580
+ // flow does not currently support assertions
581
+ assertions: [],
582
+ declaration: null,
583
+ // flow does not support declared type exports with a source
584
+ exportKind: 'value',
585
+ source: transform.StringLiteral(node.source),
586
+ specifiers: node.specifiers.map(transform.ExportSpecifier)
587
+ };
588
+ }
589
+ }
590
+ },
591
+
592
+ DeclareFunction(node) {
593
+ // the function information is stored as an annotation on the ID...
594
+ const id = transform.Identifier(node.id, false);
595
+ const functionInfo = transform.FunctionTypeAnnotation(node.id.typeAnnotation.typeAnnotation);
596
+ return {
597
+ type: 'TSDeclareFunction',
598
+ async: false,
599
+ body: undefined,
600
+ declare: true,
601
+ expression: false,
602
+ generator: false,
603
+ id: {
604
+ type: 'Identifier',
605
+ name: id.name
606
+ },
607
+ params: functionInfo.params,
608
+ returnType: functionInfo.returnType,
609
+ typeParameters: functionInfo.typeParameters
610
+ };
611
+ },
612
+
613
+ DeclareInterface(node) {
614
+ const transformedBody = transform.ObjectTypeAnnotation(node.body);
615
+
616
+ if (transformedBody.type !== 'TSTypeLiteral') {
617
+ throw translationError(node.body, 'Spreads in interfaces are not allowed');
618
+ }
619
+
620
+ return {
621
+ type: 'TSInterfaceDeclaration',
622
+ body: {
623
+ type: 'TSInterfaceBody',
624
+ body: transformedBody.members
625
+ },
626
+ declare: node.type !== 'InterfaceDeclaration',
627
+ extends: node.extends.map(transform.InterfaceExtends),
628
+ id: transform.Identifier(node.id, false),
629
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
630
+ };
631
+ },
632
+
633
+ DeclareTypeAlias(node) {
634
+ return {
635
+ type: 'TSTypeAliasDeclaration',
636
+ declare: node.type === 'DeclareTypeAlias',
637
+ id: transform.Identifier(node.id, false),
638
+ typeAnnotation: transform.TypeAnnotationType(node.right),
639
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
640
+ };
641
+ },
642
+
643
+ DeclareOpaqueType(node) {
644
+ // TS doesn't currently have nominal types - https://github.com/Microsoft/Typescript/issues/202
645
+ // TODO - we could simulate this in a variety of ways
646
+ // Examples - https://basarat.gitbook.io/typescript/main-1/nominaltyping
647
+ return {
648
+ type: 'TSTypeAliasDeclaration',
649
+ declare: true,
650
+ id: transform.Identifier(node.id, false),
651
+ typeAnnotation: node.supertype == null ? {
652
+ type: 'TSUnknownKeyword'
653
+ } : transform.TypeAnnotationType(node.supertype),
654
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
655
+ };
656
+ },
657
+
658
+ DeclareVariable(node) {
659
+ return {
660
+ type: 'VariableDeclaration',
661
+ declare: true,
662
+ declarations: [{
663
+ type: 'VariableDeclarator',
664
+ declare: true,
665
+ id: transform.Identifier(node.id, true),
666
+ init: null
667
+ }],
668
+ kind: 'var'
669
+ };
670
+ },
671
+
672
+ EmptyTypeAnnotation(node) {
673
+ // Flow's `empty` type doesn't map well to any types in TS.
674
+ // The closest is `never`, but `never` has a number of different semantics
675
+ // In reality no human code should ever directly use the `empty` type in flow
676
+ // So let's put a pin in it for now
677
+ throw unsupportedTranslationError(node, 'empty type');
678
+ },
679
+
680
+ EnumDeclaration(node) {
681
+ const body = node.body;
682
+
683
+ if (body.type === 'EnumSymbolBody') {
684
+ /*
685
+ There's unfortunately no way for us to support this in a clean way.
686
+ We can get really close using this code:
687
+ ```
688
+ declare namespace SymbolEnum {
689
+ export const member1: unique symbol;
690
+ export type member1 = typeof member1;
691
+ export const member2: unique symbol;
692
+ export type member2 = typeof member2;
693
+ }
694
+ type SymbolEnum = typeof SymbolEnum[keyof typeof SymbolEnum];
695
+ ```
696
+ However as explained in https://github.com/microsoft/TypeScript/issues/43657:
697
+ "A unique symbol type is never transferred from one declaration to another through inference."
698
+ This intended behaviour in TS means that the usage of the fake-enum would look like this:
699
+ ```
700
+ const value: SymbolEnum.member1 = SymbolEnum.member1;
701
+ // ^^^^^^^^^^^^^^^^^^ required to force TS to retain the information
702
+ ```
703
+ Which is really clunky and shitty. It definitely works, but ofc it's not good.
704
+ We can go with this design if users are okay with it!
705
+ Considering how rarely used symbol enums are ATM, let's just put a pin in it for now.
706
+ */
707
+ throw unsupportedTranslationError(node, 'symbol enums');
708
+ }
709
+
710
+ if (body.type === 'EnumBooleanBody') {
711
+ /*
712
+ TODO - TS enums only allow strings or numbers as their values - not booleans.
713
+ This means we need a non-ts-enum representation of the enum.
714
+ We can support boolean enums using a construct like this:
715
+ ```ts
716
+ declare namespace BooleanEnum {
717
+ export const member1: true;
718
+ export type member1 = typeof member1;
719
+ export const member2: false;
720
+ export type member2 = typeof member1;
721
+ }
722
+ declare type BooleanEnum = boolean;
723
+ ```
724
+ But it's pretty clunky and ugly.
725
+ Considering how rarely used boolean enums are ATM, let's just put a pin in it for now.
726
+ */
727
+ throw unsupportedTranslationError(node, 'boolean enums');
728
+ }
729
+
730
+ const members = [];
731
+
732
+ for (const member of body.members) {
733
+ switch (member.type) {
734
+ case 'EnumDefaultedMember':
735
+ {
736
+ if (body.type === 'EnumNumberBody') {
737
+ // this should be impossible!
738
+ throw unexpectedTranslationError(member, 'Unexpected defaulted number enum member');
739
+ }
740
+
741
+ members.push({
742
+ type: 'TSEnumMember',
743
+ computed: false,
744
+ id: transform.Identifier(member.id, false),
745
+ initializer: {
746
+ type: 'Literal',
747
+ raw: `"${member.id.name}"`,
748
+ value: member.id.name
749
+ }
750
+ });
751
+ break;
752
+ }
753
+
754
+ case 'EnumNumberMember':
755
+ case 'EnumStringMember':
756
+ members.push({
757
+ type: 'TSEnumMember',
758
+ computed: false,
759
+ id: transform.Identifier(member.id, false),
760
+ initializer: member.init.literalType === 'string' ? transform.StringLiteral(member.init) : transform.NumericLiteral(member.init)
761
+ });
762
+ }
763
+ }
764
+
765
+ const bodyRepresentationType = body.type === 'EnumNumberBody' ? {
766
+ type: 'TSNumberKeyword'
767
+ } : {
768
+ type: 'TSStringKeyword'
769
+ };
770
+ const enumName = transform.Identifier(node.id, false);
771
+ return [{
772
+ type: 'TSEnumDeclaration',
773
+ const: false,
774
+ declare: true,
775
+ id: enumName,
776
+ members
777
+ }, // flow also exports `.cast`, `.isValid`, `.members` and `.getName` for enums
778
+ // we can use declaration merging to declare these functions on the enum:
779
+
780
+ /*
781
+ declare enum Foo {
782
+ A = 1,
783
+ B = 2,
784
+ }
785
+ declare namespace Foo {
786
+ export function cast(value: number | null | undefined): Foo;
787
+ export function isValid(value: number | null | undefined): value is Foo;
788
+ export function members(): IterableIterator<Foo>;
789
+ export function getName(value: Foo): string;
790
+ }
791
+ */
792
+ {
793
+ type: 'TSModuleDeclaration',
794
+ declare: true,
795
+ id: enumName,
796
+ body: {
797
+ type: 'TSModuleBlock',
798
+ body: [// export function cast(value: number | null | undefined): Foo
799
+ {
800
+ type: 'ExportNamedDeclaration',
801
+ declaration: {
802
+ type: 'TSDeclareFunction',
803
+ id: {
804
+ type: 'Identifier',
805
+ name: 'cast'
806
+ },
807
+ generator: false,
808
+ expression: false,
809
+ async: false,
810
+ params: [{
811
+ type: 'Identifier',
812
+ name: 'value',
813
+ typeAnnotation: {
814
+ type: 'TSTypeAnnotation',
815
+ typeAnnotation: {
816
+ type: 'TSUnionType',
817
+ types: [bodyRepresentationType, {
818
+ type: 'TSNullKeyword'
819
+ }, {
820
+ type: 'TSUndefinedKeyword'
821
+ }]
822
+ }
823
+ }
824
+ }],
825
+ returnType: {
826
+ type: 'TSTypeAnnotation',
827
+ typeAnnotation: {
828
+ type: 'TSTypeReference',
829
+ typeName: enumName
830
+ }
831
+ }
832
+ },
833
+ specifiers: [],
834
+ source: null,
835
+ exportKind: 'value',
836
+ assertions: []
837
+ }, // export function isValid(value: number | null | undefined): value is Foo;
838
+ {
839
+ type: 'ExportNamedDeclaration',
840
+ declaration: {
841
+ type: 'TSDeclareFunction',
842
+ id: {
843
+ type: 'Identifier',
844
+ name: 'isValid'
845
+ },
846
+ generator: false,
847
+ expression: false,
848
+ async: false,
849
+ params: [{
850
+ type: 'Identifier',
851
+ name: 'value',
852
+ typeAnnotation: {
853
+ type: 'TSTypeAnnotation',
854
+ typeAnnotation: {
855
+ type: 'TSUnionType',
856
+ types: [bodyRepresentationType, {
857
+ type: 'TSNullKeyword'
858
+ }, {
859
+ type: 'TSUndefinedKeyword'
860
+ }]
861
+ }
862
+ }
863
+ }],
864
+ returnType: {
865
+ type: 'TSTypeAnnotation',
866
+ typeAnnotation: {
867
+ type: 'TSTypePredicate',
868
+ asserts: false,
869
+ parameterName: {
870
+ type: 'Identifier',
871
+ name: 'value'
872
+ },
873
+ typeAnnotation: {
874
+ type: 'TSTypeAnnotation',
875
+ typeAnnotation: {
876
+ type: 'TSTypeReference',
877
+ typeName: enumName
878
+ }
879
+ }
880
+ }
881
+ }
882
+ },
883
+ specifiers: [],
884
+ source: null,
885
+ exportKind: 'value',
886
+ assertions: []
887
+ }, // export function members(): IterableIterator<Foo>;
888
+ {
889
+ type: 'ExportNamedDeclaration',
890
+ declaration: {
891
+ type: 'TSDeclareFunction',
892
+ id: {
893
+ type: 'Identifier',
894
+ name: 'members'
895
+ },
896
+ generator: false,
897
+ expression: false,
898
+ async: false,
899
+ params: [],
900
+ returnType: {
901
+ type: 'TSTypeAnnotation',
902
+ typeAnnotation: {
903
+ type: 'TSTypeReference',
904
+ typeName: {
905
+ type: 'Identifier',
906
+ name: 'IterableIterator'
907
+ },
908
+ typeParameters: {
909
+ type: 'TSTypeParameterInstantiation',
910
+ params: [{
911
+ type: 'TSTypeReference',
912
+ typeName: enumName
913
+ }]
914
+ }
915
+ }
916
+ }
917
+ },
918
+ specifiers: [],
919
+ source: null,
920
+ exportKind: 'value',
921
+ assertions: []
922
+ }, // export function getName(value: Foo): string;
923
+ {
924
+ type: 'ExportNamedDeclaration',
925
+ declaration: {
926
+ type: 'TSDeclareFunction',
927
+ id: {
928
+ type: 'Identifier',
929
+ name: 'getName'
930
+ },
931
+ generator: false,
932
+ expression: false,
933
+ async: false,
934
+ params: [{
935
+ type: 'Identifier',
936
+ name: 'value',
937
+ typeAnnotation: {
938
+ type: 'TSTypeAnnotation',
939
+ typeAnnotation: {
940
+ type: 'TSTypeReference',
941
+ typeName: enumName
942
+ }
943
+ }
944
+ }],
945
+ returnType: {
946
+ type: 'TSTypeAnnotation',
947
+ typeAnnotation: {
948
+ type: 'TSStringKeyword'
949
+ }
950
+ }
951
+ },
952
+ specifiers: [],
953
+ source: null,
954
+ exportKind: 'value',
955
+ assertions: []
956
+ }]
957
+ }
958
+ }];
959
+ },
960
+
961
+ DeclareModuleExports(node) {
962
+ throw translationError(node, 'CommonJS exports are not supported.');
963
+ },
964
+
965
+ ExistsTypeAnnotation(node) {
966
+ // The existential type does not map to any types in TS
967
+ // It's also super deprecated - so let's not ever worry
968
+ throw unsupportedTranslationError(node, 'exestential type');
969
+ },
970
+
971
+ ExportNamedDeclaration(node) {
972
+ if (node.source != null || node.specifiers.length > 0) {
973
+ // can never have a declaration with a source
974
+ return {
975
+ type: 'ExportNamedDeclaration',
976
+ // flow does not currently support import/export assertions
977
+ assertions: [],
978
+ declaration: null,
979
+ exportKind: node.exportKind,
980
+ source: node.source == null ? null : transform.StringLiteral(node.source),
981
+ specifiers: node.specifiers.map(transform.ExportSpecifier)
982
+ };
983
+ }
984
+
985
+ const [exportedDeclaration, mergedDeclaration] = (() => {
986
+ if (node.declaration == null) {
987
+ return [null];
988
+ }
989
+
990
+ switch (node.declaration.type) {
991
+ case 'ClassDeclaration':
992
+ case 'FunctionDeclaration':
993
+ case 'VariableDeclaration':
994
+ // These cases shouldn't happen in flow defs because they have their own special
995
+ // AST node (DeclareClass, DeclareFunction, DeclareVariable)
996
+ throw unexpectedTranslationError(node.declaration, `Unexpected named declaration found ${node.declaration.type}`);
997
+
998
+ case 'EnumDeclaration':
999
+ return transform.EnumDeclaration(node.declaration);
1000
+
1001
+ case 'InterfaceDeclaration':
1002
+ return [transform.InterfaceDeclaration(node.declaration), null];
1003
+
1004
+ case 'OpaqueType':
1005
+ return [transform.OpaqueType(node.declaration), null];
1006
+
1007
+ case 'TypeAlias':
1008
+ return [transform.TypeAlias(node.declaration), null];
1009
+ }
1010
+ })();
1011
+
1012
+ const mainExport = {
1013
+ type: 'ExportNamedDeclaration',
1014
+ assertions: [],
1015
+ declaration: exportedDeclaration,
1016
+ exportKind: node.exportKind,
1017
+ source: null,
1018
+ specifiers: []
1019
+ };
1020
+
1021
+ if (mergedDeclaration != null) {
1022
+ // for cases where there is a merged declaration, TS enforces BOTH are exported
1023
+ return [mainExport, {
1024
+ type: 'ExportNamedDeclaration',
1025
+ assertions: [],
1026
+ declaration: mergedDeclaration,
1027
+ exportKind: node.exportKind,
1028
+ source: null,
1029
+ specifiers: []
1030
+ }];
1031
+ }
1032
+
1033
+ return mainExport;
1034
+ },
1035
+
1036
+ ExportSpecifier(node) {
1037
+ return {
1038
+ type: 'ExportSpecifier',
1039
+ exported: transform.Identifier(node.exported, false),
1040
+ local: transform.Identifier(node.local, false),
1041
+ // flow does not support inline exportKind for named exports
1042
+ exportKind: 'value'
1043
+ };
1044
+ },
1045
+
1046
+ FunctionTypeAnnotation(node) {
1047
+ const params = node.params.map(transform.FunctionTypeParam);
1048
+
1049
+ if (node.this != null) {
1050
+ params.unshift({
1051
+ type: 'Identifier',
1052
+ name: 'this',
1053
+ typeAnnotation: {
1054
+ type: 'TSTypeAnnotation',
1055
+ typeAnnotation: transform.TypeAnnotationType(node.this.typeAnnotation)
1056
+ }
1057
+ });
1058
+ }
1059
+
1060
+ if (node.rest != null) {
1061
+ const rest = node.rest;
1062
+ params.push({
1063
+ type: 'RestElement',
1064
+ argument: rest.name == null ? {
1065
+ type: 'Identifier',
1066
+ name: '$$REST$$'
1067
+ } : transform.Identifier(rest.name, false),
1068
+ typeAnnotation: {
1069
+ type: 'TSTypeAnnotation',
1070
+ typeAnnotation: transform.TypeAnnotationType(rest.typeAnnotation)
1071
+ }
1072
+ });
1073
+ }
1074
+
1075
+ return {
1076
+ type: 'TSFunctionType',
1077
+ params,
1078
+ returnType: {
1079
+ type: 'TSTypeAnnotation',
1080
+ typeAnnotation: transform.TypeAnnotationType(node.returnType)
1081
+ },
1082
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
1083
+ };
1084
+ },
1085
+
1086
+ FunctionTypeParam(node, idx = 0) {
1087
+ return {
1088
+ type: 'Identifier',
1089
+ name: node.name == null ? `$$PARAM_${idx}$$` : node.name.name,
1090
+ typeAnnotation: {
1091
+ type: 'TSTypeAnnotation',
1092
+ typeAnnotation: transform.TypeAnnotationType(node.typeAnnotation)
1093
+ },
1094
+ optional: node.optional
1095
+ };
1096
+ },
1097
+
1098
+ GenericTypeAnnotation(node) {
1099
+ const [fullTypeName, baseId] = (() => {
1100
+ let names = [];
1101
+ let currentNode = node.id;
1102
+
1103
+ while (currentNode != null) {
1104
+ switch (currentNode.type) {
1105
+ case 'Identifier':
1106
+ {
1107
+ names.unshift(currentNode.name);
1108
+ return [names.join('.'), currentNode];
1109
+ }
1110
+
1111
+ case 'QualifiedTypeIdentifier':
1112
+ {
1113
+ names.unshift(currentNode.id.name);
1114
+ currentNode = currentNode.qualification;
1115
+ break;
1116
+ }
1117
+ }
1118
+ }
1119
+
1120
+ throw translationError(node, `Invalid program state, types should only contain 'Identifier' and 'QualifiedTypeIdentifier' nodes.`);
1121
+ })();
1122
+
1123
+ const assertHasExactlyNTypeParameters = count => {
1124
+ if (node.typeParameters != null) {
1125
+ if (node.typeParameters.params.length !== count) {
1126
+ throw translationError(node, `Expected exactly ${count} type parameter${count > 1 ? 's' : ''} with \`${fullTypeName}\``);
1127
+ }
1128
+
1129
+ const res = [];
1130
+
1131
+ for (const param of node.typeParameters.params) {
1132
+ res.push(transform.TypeAnnotationType(param));
1133
+ }
1134
+
1135
+ return res;
1136
+ }
1137
+
1138
+ if (count !== 0) {
1139
+ throw translationError(node, `Expected no type parameters with \`${fullTypeName}\``);
1140
+ }
1141
+
1142
+ return [];
1143
+ };
1144
+
1145
+ switch (fullTypeName) {
1146
+ case '$Call':
1147
+ case '$ObjMap':
1148
+ case '$ObjMapConst':
1149
+ case '$ObjMapi':
1150
+ case '$TupleMap':
1151
+ {
1152
+ /*
1153
+ TODO - I don't think it's possible to make these types work in the generic case.
1154
+ TS has no utility types that allow you to generically mimic this functionality.
1155
+ You really need intimiate knowledge of the user's intent in order to correctly
1156
+ transform the code.
1157
+ For example the simple example for $Call from the flow docs:
1158
+ ```
1159
+ type ExtractPropType = <T>({prop: T}) => T;
1160
+ type Obj = {prop: number};
1161
+ type PropType = $Call<ExtractPropType, Obj>;
1162
+ // expected -- typeof PropType === number
1163
+ ```
1164
+ The equivalent in TS would be:
1165
+ ```
1166
+ type ExtractPropType<T extends { prop: any }> = (arg: T) => T['prop'];
1167
+ type Obj = { prop: number };
1168
+ type PropType = ReturnType<ExtractPropType<Obj>>; // number
1169
+ ```
1170
+ */
1171
+ throw unsupportedTranslationError(node, fullTypeName);
1172
+ }
1173
+
1174
+ case '$Diff':
1175
+ case '$Rest':
1176
+ {
1177
+ // `$Diff<A, B>` => `Pick<A, Exclude<keyof A, keyof B>>`
1178
+ const params = assertHasExactlyNTypeParameters(2);
1179
+ return {
1180
+ type: 'TSTypeReference',
1181
+ typeName: {
1182
+ type: 'Identifier',
1183
+ name: 'Pick'
1184
+ },
1185
+ typeParameters: {
1186
+ type: 'TSTypeParameterInstantiation',
1187
+ params: [params[0], {
1188
+ type: 'TSTypeReference',
1189
+ typeName: {
1190
+ type: 'Identifier',
1191
+ name: 'Exclude'
1192
+ },
1193
+ typeParameters: {
1194
+ type: 'TSTypeParameterInstantiation',
1195
+ params: [{
1196
+ type: 'TSTypeOperator',
1197
+ operator: 'keyof',
1198
+ typeAnnotation: params[0]
1199
+ }, {
1200
+ type: 'TSTypeOperator',
1201
+ operator: 'keyof',
1202
+ typeAnnotation: params[1]
1203
+ }]
1204
+ }
1205
+ }]
1206
+ }
1207
+ };
1208
+ }
1209
+
1210
+ case '$ElementType':
1211
+ case '$PropertyType':
1212
+ {
1213
+ // `$ElementType<T, K>` => `T[K]`
1214
+ const params = assertHasExactlyNTypeParameters(2);
1215
+ return {
1216
+ type: 'TSIndexedAccessType',
1217
+ objectType: params[0],
1218
+ indexType: params[1]
1219
+ };
1220
+ }
1221
+
1222
+ case '$Exact':
1223
+ {
1224
+ // `$Exact<T>` => `T`
1225
+ // TS has no concept of exact vs inexact types
1226
+ return assertHasExactlyNTypeParameters(1)[0];
1227
+ }
1228
+
1229
+ case '$Exports':
1230
+ {
1231
+ // `$Exports<'module'>` => `typeof import('module')`
1232
+ const moduleName = assertHasExactlyNTypeParameters(1)[0];
1233
+
1234
+ if (moduleName.type !== 'TSLiteralType' || moduleName.literal.type !== 'Literal' || typeof moduleName.literal.value !== 'string') {
1235
+ throw translationError(node, '$Exports must have a string literal argument');
1236
+ }
1237
+
1238
+ return {
1239
+ type: 'TSImportType',
1240
+ isTypeOf: true,
1241
+ parameter: moduleName,
1242
+ qualifier: null,
1243
+ typeParameters: null
1244
+ };
1245
+ }
1246
+
1247
+ case '$FlowFixMe':
1248
+ {
1249
+ // `$FlowFixMe` => `any`
1250
+ return {
1251
+ type: 'TSAnyKeyword'
1252
+ };
1253
+ }
1254
+
1255
+ case '$KeyMirror':
1256
+ {
1257
+ // `$KeyMirror<T>` => `{[K in keyof T]: K}`
1258
+ return {
1259
+ type: 'TSMappedType',
1260
+ typeParameter: {
1261
+ type: 'TSTypeParameter',
1262
+ name: {
1263
+ type: 'Identifier',
1264
+ name: 'K'
1265
+ },
1266
+ constraint: {
1267
+ type: 'TSTypeOperator',
1268
+ operator: 'keyof',
1269
+ typeAnnotation: assertHasExactlyNTypeParameters(1)[0]
1270
+ },
1271
+ in: false,
1272
+ out: false
1273
+ },
1274
+ nameType: null,
1275
+ typeAnnotation: {
1276
+ type: 'TSTypeReference',
1277
+ typeName: {
1278
+ type: 'Identifier',
1279
+ name: 'K'
1280
+ }
1281
+ }
1282
+ };
1283
+ }
1284
+
1285
+ case '$Keys':
1286
+ {
1287
+ // `$Keys<T>` => `keyof T`
1288
+ return {
1289
+ type: 'TSTypeOperator',
1290
+ operator: 'keyof',
1291
+ typeAnnotation: assertHasExactlyNTypeParameters(1)[0]
1292
+ };
1293
+ }
1294
+
1295
+ case '$NonMaybeType':
1296
+ {
1297
+ // `$NonMaybeType<T>` => `NonNullable<T>`
1298
+ // Not a great name because `NonNullable` also excludes `undefined`
1299
+ return {
1300
+ type: 'TSTypeReference',
1301
+ typeName: {
1302
+ type: 'Identifier',
1303
+ name: 'NonNullable'
1304
+ },
1305
+ typeParameters: {
1306
+ type: 'TSTypeParameterInstantiation',
1307
+ params: assertHasExactlyNTypeParameters(1)
1308
+ }
1309
+ };
1310
+ }
1311
+
1312
+ case '$ReadOnly':
1313
+ {
1314
+ // `$ReadOnly<T>` => `Readonly<T>`
1315
+ return {
1316
+ type: 'TSTypeReference',
1317
+ typeName: {
1318
+ type: 'Identifier',
1319
+ name: 'Readonly'
1320
+ },
1321
+ typeParameters: {
1322
+ type: 'TSTypeParameterInstantiation',
1323
+ params: assertHasExactlyNTypeParameters(1)
1324
+ }
1325
+ };
1326
+ }
1327
+
1328
+ case '$ReadOnlyArray':
1329
+ {
1330
+ // `$ReadOnlyArray<T>` => `ReadonlyArray<T>`
1331
+ //
1332
+ // we could also do => `readonly T[]`
1333
+ // TODO - maybe a config option?
1334
+ return {
1335
+ type: 'TSTypeReference',
1336
+ typeName: {
1337
+ type: 'Identifier',
1338
+ name: 'ReadonlyArray'
1339
+ },
1340
+ typeParameters: {
1341
+ type: 'TSTypeParameterInstantiation',
1342
+ params: assertHasExactlyNTypeParameters(1)
1343
+ }
1344
+ };
1345
+ }
1346
+
1347
+ case '$Shape':
1348
+ case '$Partial':
1349
+ {
1350
+ // `$Partial<T>` => `Partial<T>`
1351
+ return {
1352
+ type: 'TSTypeReference',
1353
+ typeName: {
1354
+ type: 'Identifier',
1355
+ name: 'Partial'
1356
+ },
1357
+ typeParameters: {
1358
+ type: 'TSTypeParameterInstantiation',
1359
+ params: assertHasExactlyNTypeParameters(1)
1360
+ }
1361
+ };
1362
+ }
1363
+
1364
+ case '$Subtype':
1365
+ case '$Supertype':
1366
+ {
1367
+ // These types are deprecated and shouldn't be used in any modern code
1368
+ // so let's not even bother trying to figure it out
1369
+ throw unsupportedTranslationError(node, fullTypeName);
1370
+ }
1371
+
1372
+ case '$Values':
1373
+ {
1374
+ // `$Values<T>` => `T[keyof T]`
1375
+ const transformedType = assertHasExactlyNTypeParameters(1)[0];
1376
+ return {
1377
+ type: 'TSIndexedAccessType',
1378
+ objectType: transformedType,
1379
+ indexType: {
1380
+ type: 'TSTypeOperator',
1381
+ operator: 'keyof',
1382
+ typeAnnotation: transformedType
1383
+ }
1384
+ };
1385
+ }
1386
+
1387
+ case 'Class':
1388
+ {
1389
+ // `Class<T>` => `new (...args: any[]) => T`
1390
+ const param = assertHasExactlyNTypeParameters(1)[0];
1391
+
1392
+ if (param.type !== 'TSTypeReference') {
1393
+ throw translationError(node, 'Expected a type reference within Class<T>');
1394
+ }
1395
+
1396
+ return {
1397
+ type: 'TSConstructorType',
1398
+ abstract: false,
1399
+ params: [{
1400
+ type: 'RestElement',
1401
+ argument: {
1402
+ type: 'Identifier',
1403
+ name: 'args'
1404
+ },
1405
+ typeAnnotation: {
1406
+ type: 'TSTypeAnnotation',
1407
+ typeAnnotation: {
1408
+ type: 'TSArrayType',
1409
+ elementType: {
1410
+ type: 'TSAnyKeyword'
1411
+ }
1412
+ }
1413
+ }
1414
+ }],
1415
+ returnType: {
1416
+ type: 'TSTypeAnnotation',
1417
+ typeAnnotation: param
1418
+ }
1419
+ };
1420
+ }
1421
+ } // React special conversion:
1422
+
1423
+
1424
+ if (isReactImport(baseId)) {
1425
+ switch (fullTypeName) {
1426
+ // React.Node -> React.ReactNode
1427
+ case 'React.Node':
1428
+ {
1429
+ assertHasExactlyNTypeParameters(0);
1430
+ return {
1431
+ type: 'TSTypeReference',
1432
+ typeName: {
1433
+ type: 'TSQualifiedName',
1434
+ left: transform.Identifier(baseId, false),
1435
+ right: {
1436
+ type: 'Identifier',
1437
+ name: `ReactNode`
1438
+ }
1439
+ },
1440
+ typeParameters: undefined
1441
+ };
1442
+ }
1443
+ // React.Element<typeof Component> -> React.ReactElement<typeof Component>
1444
+
1445
+ case 'React.Element':
1446
+ {
1447
+ return {
1448
+ type: 'TSTypeReference',
1449
+ typeName: {
1450
+ type: 'TSQualifiedName',
1451
+ left: transform.Identifier(baseId, false),
1452
+ right: {
1453
+ type: 'Identifier',
1454
+ name: `ReactElement`
1455
+ }
1456
+ },
1457
+ typeParameters: {
1458
+ type: 'TSTypeParameterInstantiation',
1459
+ params: assertHasExactlyNTypeParameters(1)
1460
+ }
1461
+ };
1462
+ }
1463
+ // React.MixedElement -> JSX.Element
1464
+
1465
+ case 'React.MixedElement':
1466
+ {
1467
+ assertHasExactlyNTypeParameters(0);
1468
+ return {
1469
+ type: 'TSTypeReference',
1470
+ typeName: {
1471
+ type: 'TSQualifiedName',
1472
+ left: {
1473
+ type: 'Identifier',
1474
+ name: 'JSX'
1475
+ },
1476
+ right: {
1477
+ type: 'Identifier',
1478
+ name: 'Element'
1479
+ }
1480
+ },
1481
+ typeParameters: undefined
1482
+ };
1483
+ }
1484
+ // React.AbstractComponent<Config> -> React.ForwardRefExoticComponent<Config>
1485
+ // React.AbstractComponent<Config, Instance> -> React.ForwardRefExoticComponent<Config & React.RefAttributes<Instance>>
1486
+
1487
+ case 'React.AbstractComponent':
1488
+ {
1489
+ const typeParameters = node.typeParameters;
1490
+
1491
+ if (typeParameters == null || typeParameters.params.length === 0) {
1492
+ throw translationError(node, `Expected at least 1 type parameter with \`${fullTypeName}\``);
1493
+ }
1494
+
1495
+ const params = typeParameters.params;
1496
+
1497
+ if (params.length > 2) {
1498
+ throw translationError(node, `Expected at no more than 2 type parameters with \`${fullTypeName}\``);
1499
+ }
1500
+
1501
+ let newTypeParam = transform.TypeAnnotationType(params[0]);
1502
+
1503
+ if (params[1] != null) {
1504
+ newTypeParam = {
1505
+ type: 'TSIntersectionType',
1506
+ types: [newTypeParam, {
1507
+ type: 'TSTypeReference',
1508
+ typeName: {
1509
+ type: 'TSQualifiedName',
1510
+ left: {
1511
+ type: 'Identifier',
1512
+ name: 'React'
1513
+ },
1514
+ right: {
1515
+ type: 'Identifier',
1516
+ name: 'RefAttributes'
1517
+ }
1518
+ },
1519
+ typeParameters: {
1520
+ type: 'TSTypeParameterInstantiation',
1521
+ params: [transform.TypeAnnotationType(params[1])]
1522
+ }
1523
+ }]
1524
+ };
1525
+ }
1526
+
1527
+ return {
1528
+ type: 'TSTypeReference',
1529
+ typeName: {
1530
+ type: 'TSQualifiedName',
1531
+ left: transform.Identifier(baseId, false),
1532
+ right: {
1533
+ type: 'Identifier',
1534
+ name: `ForwardRefExoticComponent`
1535
+ }
1536
+ },
1537
+ typeParameters: {
1538
+ type: 'TSTypeParameterInstantiation',
1539
+ params: [newTypeParam]
1540
+ }
1541
+ };
1542
+ }
1543
+ }
1544
+ }
1545
+
1546
+ return {
1547
+ type: 'TSTypeReference',
1548
+ typeName: node.id.type === 'Identifier' ? transform.Identifier(node.id, false) : transform.QualifiedTypeIdentifier(node.id),
1549
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
1550
+ };
1551
+ },
1552
+
1553
+ Identifier(node, includeTypeAnnotation = true) {
1554
+ return {
1555
+ type: 'Identifier',
1556
+ name: node.name,
1557
+ ...(includeTypeAnnotation && node.typeAnnotation != null ? {
1558
+ typeAnnotation: transform.TypeAnnotation(node.typeAnnotation)
1559
+ } : {})
1560
+ };
1561
+ },
1562
+
1563
+ IndexedAccessType(node) {
1564
+ return {
1565
+ type: 'TSIndexedAccessType',
1566
+ objectType: transform.TypeAnnotationType(node.objectType),
1567
+ indexType: transform.TypeAnnotationType(node.indexType)
1568
+ };
1569
+ },
1570
+
1571
+ InterfaceDeclaration(node) {
1572
+ return transform.DeclareInterface(node);
1573
+ },
1574
+
1575
+ ImportAttribute(node) {
1576
+ return {
1577
+ type: 'ImportAttribute',
1578
+ key: node.key.type === 'Identifier' ? transform.Identifier(node.key) : transform.Literal(node.key),
1579
+ value: transform.Literal(node.value)
1580
+ };
1581
+ },
1582
+
1583
+ ImportDeclaration(node) {
1584
+ if (node.importKind === 'typeof') {
1585
+ /*
1586
+ TODO - this is a complicated change to support because TS
1587
+ does not have typeof imports.
1588
+ Making it a `type` import would change the meaning!
1589
+ The only way to truly support this is to prefix all **usages** with `typeof T`.
1590
+ eg:
1591
+ ```
1592
+ import typeof Foo from 'Foo';
1593
+ type T = Foo;
1594
+ ```
1595
+ would become:
1596
+ ```
1597
+ import type Foo from 'Foo';
1598
+ type T = typeof Foo;
1599
+ ```
1600
+ This seems simple, but will actually be super complicated for us to do with
1601
+ our current translation architecture
1602
+ */
1603
+ throw unsupportedTranslationError(node, 'typeof imports');
1604
+ }
1605
+
1606
+ const importKind = node.importKind;
1607
+ return {
1608
+ type: 'ImportDeclaration',
1609
+ assertions: node.assertions.map(transform.ImportAttribute),
1610
+ importKind: importKind != null ? importKind : 'value',
1611
+ source: transform.StringLiteral(node.source),
1612
+ specifiers: node.specifiers.map(spec => {
1613
+ var _spec$importKind;
1614
+
1615
+ switch (spec.type) {
1616
+ case 'ImportDefaultSpecifier':
1617
+ return {
1618
+ type: 'ImportDefaultSpecifier',
1619
+ local: transform.Identifier(spec.local, false)
1620
+ };
1621
+
1622
+ case 'ImportNamespaceSpecifier':
1623
+ return {
1624
+ type: 'ImportNamespaceSpecifier',
1625
+ local: transform.Identifier(spec.local, false)
1626
+ };
1627
+
1628
+ case 'ImportSpecifier':
1629
+ if (spec.importKind === 'typeof') {
1630
+ // see above
1631
+ throw unsupportedTranslationError(node, 'typeof imports');
1632
+ }
1633
+
1634
+ return {
1635
+ type: 'ImportSpecifier',
1636
+ importKind: (_spec$importKind = spec.importKind) != null ? _spec$importKind : 'value',
1637
+ imported: transform.Identifier(spec.imported, false),
1638
+ local: transform.Identifier(spec.local, false)
1639
+ };
1640
+ }
1641
+ })
1642
+ };
1643
+ },
1644
+
1645
+ InterfaceExtends(node) {
1646
+ return {
1647
+ type: 'TSInterfaceHeritage',
1648
+ expression: transform.Identifier(node.id, false),
1649
+ typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
1650
+ };
1651
+ },
1652
+
1653
+ InterfaceTypeAnnotation(node) {
1654
+ if (node.extends) {
1655
+ // type T = interface extends U, V { ... }
1656
+ // to
1657
+ // type T = U & V & { ... }
1658
+ return {
1659
+ type: 'TSIntersectionType',
1660
+ types: [...node.extends.map(ex => ({
1661
+ type: 'TSTypeReference',
1662
+ typeName: transform.Identifier(ex.id, false),
1663
+ typeParameters: ex.typeParameters == null ? undefined : transform.TypeParameterInstantiation(ex.typeParameters)
1664
+ })), transform.ObjectTypeAnnotation(node.body)]
1665
+ };
1666
+ }
1667
+
1668
+ return transform.ObjectTypeAnnotation(node.body);
1669
+ },
1670
+
1671
+ IntersectionTypeAnnotation(node) {
1672
+ return {
1673
+ type: 'TSIntersectionType',
1674
+ types: node.types.map(transform.TypeAnnotationType)
1675
+ };
1676
+ },
1677
+
1678
+ Literal(node) {
1679
+ switch (node.literalType) {
1680
+ case 'bigint':
1681
+ return transform.BigIntLiteral(node);
1682
+
1683
+ case 'boolean':
1684
+ return transform.BooleanLiteral(node);
1685
+
1686
+ case 'null':
1687
+ return transform.NullLiteral(node);
1688
+
1689
+ case 'numeric':
1690
+ return transform.NumericLiteral(node);
1691
+
1692
+ case 'regexp':
1693
+ return transform.RegExpLiteral(node);
1694
+
1695
+ case 'string':
1696
+ return transform.StringLiteral(node);
1697
+ }
1698
+ },
1699
+
1700
+ MixedTypeAnnotation(_node) {
1701
+ return {
1702
+ type: 'TSUnknownKeyword'
1703
+ };
1704
+ },
1705
+
1706
+ NullLiteral(_node) {
1707
+ return {
1708
+ type: 'Literal',
1709
+ raw: 'null',
1710
+ value: null
1711
+ };
1712
+ },
1713
+
1714
+ NullLiteralTypeAnnotation(_node) {
1715
+ return {
1716
+ type: 'TSNullKeyword'
1717
+ };
1718
+ },
1719
+
1720
+ NullableTypeAnnotation(node) {
1721
+ // TS doesn't support the maybe type, so have to explicitly union in `null | undefined`
1722
+ // `?T` becomes `null | undefined | T`
1723
+ return {
1724
+ type: 'TSUnionType',
1725
+ types: [{
1726
+ type: 'TSNullKeyword'
1727
+ }, {
1728
+ type: 'TSUndefinedKeyword'
1729
+ }, transform.TypeAnnotationType(node.typeAnnotation)]
1730
+ };
1731
+ },
1732
+
1733
+ NumberLiteralTypeAnnotation(node) {
1734
+ return {
1735
+ type: 'TSLiteralType',
1736
+ literal: {
1737
+ type: 'Literal',
1738
+ value: node.value,
1739
+ raw: node.raw
1740
+ }
1741
+ };
1742
+ },
1743
+
1744
+ NumberTypeAnnotation(_node) {
1745
+ return {
1746
+ type: 'TSNumberKeyword'
1747
+ };
1748
+ },
1749
+
1750
+ NumericLiteral(node) {
1751
+ return {
1752
+ type: 'Literal',
1753
+ raw: node.raw,
1754
+ value: node.value
1755
+ };
1756
+ },
1757
+
1758
+ ObjectTypeAnnotation(node) {
1759
+ // we want to preserve the source order of the members
1760
+ // unfortunately flow has unordered properties storing things
1761
+ // so store all elements with their start index and sort the
1762
+ // list afterward
1763
+ const members = [];
1764
+
1765
+ for (const callProp of node.callProperties) {
1766
+ members.push({
1767
+ start: callProp.range[0],
1768
+ node: transform.ObjectTypeCallProperty(callProp)
1769
+ });
1770
+ }
1771
+
1772
+ for (const indexer of node.indexers) {
1773
+ members.push({
1774
+ start: indexer.range[0],
1775
+ node: transform.ObjectTypeIndexer(indexer)
1776
+ });
1777
+ }
1778
+ /*
1779
+ TODO - internalSlots
1780
+ I don't think there's anything analogous in TS.
1781
+ They're really rarely used (if ever) - so let's just ignore them for now
1782
+ */
1783
+
1784
+
1785
+ if (node.internalSlots.length > 0) {
1786
+ throw unsupportedTranslationError(node.internalSlots[0], 'internal slots');
1787
+ }
1788
+
1789
+ if (!node.properties.find(FlowESTree.isObjectTypeSpreadProperty)) {
1790
+ for (const property of node.properties) {
1791
+ if (property.type === 'ObjectTypeSpreadProperty') {
1792
+ // this is imposible due to the above find condition
1793
+ // this check is purely to satisfy flow
1794
+ throw unexpectedTranslationError(property, 'Impossible state');
1795
+ }
1796
+
1797
+ members.push({
1798
+ start: property.range[0],
1799
+ node: transform.ObjectTypeProperty(property)
1800
+ });
1801
+ }
1802
+
1803
+ const tsBody = members.sort((a, b) => a.start - b.start).map(({
1804
+ node
1805
+ }) => node);
1806
+ return {
1807
+ type: 'TSTypeLiteral',
1808
+ members: tsBody
1809
+ };
1810
+ } else {
1811
+ /*
1812
+ spreads are a complicate thing for us to handle, sadly.
1813
+ in flow type spreads are modelled after object spreads; meaning that for
1814
+ { ...A, ...B } - all properties in B will explicitly replace any properties
1815
+ in A of the same name.
1816
+ ```
1817
+ type T1 = { a: string };
1818
+ type T2 = { a: number };
1819
+ type T3 = { ...T1, ...T2 };
1820
+ type A = T3['a'] // === number
1821
+ ```
1822
+ however in TS there are no object type spreads - you can only merge
1823
+ objects either via the intersection operator or via interface extends.
1824
+ For an interface extends - `interface B extends A { ... }` - TS enforces
1825
+ that the properties of B are all covariantly related to the same named
1826
+ properties in A.
1827
+ So we can't use an interface extends.
1828
+ For a type intersection - `type T = A & B;` - TS will (essentially) merge
1829
+ the types by intersecting each same named property in each type to calculate
1830
+ the resulting type. This has the effect of enforcing the same constraint
1831
+ as the interface case, however instead of an error it causes properties to
1832
+ become `never`:
1833
+ ```
1834
+ type T1 = { a: string };
1835
+ type T2 = { a: number };
1836
+ type T3 = T1 & T2;
1837
+ type A = T3['a'] // === string & number === never
1838
+ ```
1839
+ So in order for us to model flow's spreads we have to explicitly omit the keys
1840
+ from the proceeding type that might clash. We can do this pretty easily using
1841
+ TS's utility types:
1842
+ ```
1843
+ type T1 = { a: string };
1844
+ type T2 = { a: number };
1845
+ type T3 = Omit<T1, keyof T2> & T2;
1846
+ type A = T3['a'] // === number
1847
+ ```
1848
+ Unfortunately because we need to solve for the general case object type usage,
1849
+ it's going to be a bit ugly and complicated, sadly.
1850
+ If we had access to type information we would be able to skip some ugliness by
1851
+ checking to see if there is any overlap and skipping the omit step if there isn't.
1852
+ But alas - we're working purely syntactically.
1853
+ ```
1854
+ type T = { ...T1, b: string };
1855
+ // becomes
1856
+ type T = Omit<T1, keyof { b: string }> & { b: string };
1857
+ ```
1858
+ ```
1859
+ type T = { ...T1, ...T2, ...T3, b: string };
1860
+ // becomes
1861
+ type T =
1862
+ & Omit<T1, keyof (T2 | T3 | { b: string })>
1863
+ & Omit<T2, keyof (T3 | { b: string })>
1864
+ & Omit<T3, keyof { b: string }>
1865
+ & { b: string };
1866
+ ```
1867
+ Note that because it's going to be super ugly and complicated - for now we're going to disallow:
1868
+ - spreads in the middle
1869
+ - spreads at the end
1870
+ - spreads of things that aren't "Identifiers"
1871
+ */
1872
+ if (members.length > 0) {
1873
+ throw unsupportedTranslationError(node, 'object types with spreads, indexers and/or call properties at the same time');
1874
+ }
1875
+
1876
+ const typesToIntersect = [];
1877
+
1878
+ for (const property of node.properties) {
1879
+ if (property.type === 'ObjectTypeSpreadProperty') {
1880
+ if (members.length > 0) {
1881
+ throw unsupportedTranslationError(property, 'object types with spreads in the middle or at the end');
1882
+ }
1883
+
1884
+ const spreadType = transform.TypeAnnotationType(property.argument);
1885
+
1886
+ if (spreadType.type !== 'TSTypeReference') {
1887
+ throw unsupportedTranslationError(property, 'object types with complex spreads');
1888
+ }
1889
+
1890
+ typesToIntersect.push(spreadType);
1891
+ } else {
1892
+ members.push({
1893
+ start: property.range[0],
1894
+ node: transform.ObjectTypeProperty(property)
1895
+ });
1896
+ }
1897
+ }
1898
+
1899
+ const tsBody = members.sort((a, b) => a.start - b.start).map(({
1900
+ node
1901
+ }) => node);
1902
+ const objectType = {
1903
+ type: 'TSTypeLiteral',
1904
+ members: tsBody
1905
+ };
1906
+ const intersectionMembers = [];
1907
+
1908
+ for (let i = 0; i < typesToIntersect.length; i += 1) {
1909
+ const currentType = typesToIntersect[i];
1910
+ const remainingTypes = typesToIntersect.slice(i + 1);
1911
+ intersectionMembers.push({
1912
+ type: 'TSTypeReference',
1913
+ typeName: {
1914
+ type: 'Identifier',
1915
+ name: 'Omit'
1916
+ },
1917
+ typeParameters: {
1918
+ type: 'TSTypeParameterInstantiation',
1919
+ params: [currentType, {
1920
+ type: 'TSTypeOperator',
1921
+ operator: 'keyof',
1922
+ typeAnnotation: {
1923
+ type: 'TSUnionType',
1924
+ types: [...remainingTypes, objectType]
1925
+ }
1926
+ }]
1927
+ }
1928
+ });
1929
+ }
1930
+
1931
+ intersectionMembers.push(objectType);
1932
+ return {
1933
+ type: 'TSIntersectionType',
1934
+ types: intersectionMembers
1935
+ };
1936
+ }
1937
+ },
1938
+
1939
+ ObjectTypeCallProperty(node) {
1940
+ // the info is stored on the "value"
1941
+ const func = transform.FunctionTypeAnnotation(node.value);
1942
+ return {
1943
+ type: 'TSCallSignatureDeclaration',
1944
+ params: func.params,
1945
+ returnType: func.returnType,
1946
+ typeParameters: func.typeParameters
1947
+ };
1948
+ },
1949
+
1950
+ ObjectTypeIndexer(node) {
1951
+ var _node$variance;
1952
+
1953
+ return {
1954
+ type: 'TSIndexSignature',
1955
+ parameters: [{
1956
+ type: 'Identifier',
1957
+ name: node.id == null ? '$$Key$$' : node.id.name,
1958
+ typeAnnotation: {
1959
+ type: 'TSTypeAnnotation',
1960
+ typeAnnotation: transform.TypeAnnotationType(node.key)
1961
+ }
1962
+ }],
1963
+ readonly: ((_node$variance = node.variance) == null ? void 0 : _node$variance.kind) === 'plus',
1964
+ static: node.static,
1965
+ typeAnnotation: {
1966
+ type: 'TSTypeAnnotation',
1967
+ typeAnnotation: transform.TypeAnnotationType(node.value)
1968
+ }
1969
+ };
1970
+ },
1971
+
1972
+ ObjectTypeProperty(node) {
1973
+ var _node$variance2;
1974
+
1975
+ const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : transform.StringLiteral(node.key);
1976
+
1977
+ if (node.method === true) {
1978
+ // flow has just one node for all object properties and relies upon the method flag
1979
+ // TS has separate nodes for methods and properties
1980
+ const func = transform.FunctionTypeAnnotation(node.value);
1981
+ return {
1982
+ type: 'TSMethodSignature',
1983
+ computed: false,
1984
+ key,
1985
+ kind: node.kind === 'init' ? 'method' : node.kind,
1986
+ optional: node.optional,
1987
+ params: func.params,
1988
+ returnType: func.returnType,
1989
+ static: node.static,
1990
+ typeParameters: func.typeParameters
1991
+ };
1992
+ }
1993
+
1994
+ if (node.kind === 'get' || node.kind === 'set') {
1995
+ // flow treats getters/setters as true property signatures (method === false)
1996
+ // TS treats them as method signatures
1997
+ const func = transform.FunctionTypeAnnotation(node.value);
1998
+ return {
1999
+ type: 'TSMethodSignature',
2000
+ computed: false,
2001
+ key,
2002
+ kind: node.kind,
2003
+ optional: false,
2004
+ params: func.params,
2005
+ // TS setters must not have a return type
2006
+ returnType: node.kind === 'set' ? undefined : func.returnType,
2007
+ static: node.static,
2008
+ // TS accessors cannot have type parameters
2009
+ typeParameters: undefined
2010
+ };
2011
+ }
2012
+
2013
+ return {
2014
+ type: 'TSPropertySignature',
2015
+ computed: false,
2016
+ key,
2017
+ optional: node.optional,
2018
+ readonly: ((_node$variance2 = node.variance) == null ? void 0 : _node$variance2.kind) === 'plus',
2019
+ static: node.static,
2020
+ typeAnnotation: {
2021
+ type: 'TSTypeAnnotation',
2022
+ typeAnnotation: transform.TypeAnnotationType(node.value)
2023
+ }
2024
+ };
2025
+ },
2026
+
2027
+ OpaqueType(node) {
2028
+ return transform.DeclareOpaqueType(node);
2029
+ },
2030
+
2031
+ OptionalIndexedAccessType(node) {
2032
+ // Foo?.[A][B]
2033
+ // ^^^^^^^^ optional = true
2034
+ // ^^^^^^^^^^^ optional = false
2035
+ if (node.optional === false) {
2036
+ return transform.IndexedAccessType(node);
2037
+ } // TS doesn't support optional index access so we have to wrap the object:
2038
+ // `T?.[K]` becomes `NonNullable<T>[K]`
2039
+
2040
+
2041
+ return {
2042
+ type: 'TSIndexedAccessType',
2043
+ objectType: {
2044
+ type: 'TSTypeReference',
2045
+ typeName: {
2046
+ type: 'Identifier',
2047
+ name: 'NonNullable'
2048
+ },
2049
+ typeParameters: {
2050
+ type: 'TSTypeParameterInstantiation',
2051
+ params: [transform.TypeAnnotationType(node.objectType)]
2052
+ }
2053
+ },
2054
+ indexType: transform.TypeAnnotationType(node.indexType)
2055
+ };
2056
+ },
2057
+
2058
+ QualifiedTypeIdentifier(node) {
2059
+ const qual = node.qualification;
2060
+ return {
2061
+ type: 'TSQualifiedName',
2062
+ left: qual.type === 'Identifier' ? transform.Identifier(qual, false) : transform.QualifiedTypeIdentifier(qual),
2063
+ right: transform.Identifier(node.id, false)
2064
+ };
2065
+ },
2066
+
2067
+ RegExpLiteral(node) {
2068
+ return {
2069
+ type: 'Literal',
2070
+ raw: node.raw,
2071
+ regex: {
2072
+ pattern: node.regex.pattern,
2073
+ flags: node.regex.pattern
2074
+ },
2075
+ value: node.value
2076
+ };
2077
+ },
2078
+
2079
+ StringLiteral(node) {
2080
+ return {
2081
+ type: 'Literal',
2082
+ raw: node.raw,
2083
+ value: node.value
2084
+ };
2085
+ },
2086
+
2087
+ StringLiteralTypeAnnotation(node) {
2088
+ return {
2089
+ type: 'TSLiteralType',
2090
+ literal: {
2091
+ type: 'Literal',
2092
+ value: node.value,
2093
+ raw: node.raw
2094
+ }
2095
+ };
2096
+ },
2097
+
2098
+ StringTypeAnnotation(_node) {
2099
+ return {
2100
+ type: 'TSStringKeyword'
2101
+ };
2102
+ },
2103
+
2104
+ SymbolTypeAnnotation(_node) {
2105
+ return {
2106
+ type: 'TSSymbolKeyword'
2107
+ };
2108
+ },
2109
+
2110
+ ThisTypeAnnotation(_node) {
2111
+ return {
2112
+ type: 'TSThisType'
2113
+ };
2114
+ },
2115
+
2116
+ TupleTypeAnnotation(node) {
2117
+ return {
2118
+ type: 'TSTupleType',
2119
+ elementTypes: node.types.map(transform.TypeAnnotationType)
2120
+ };
2121
+ },
2122
+
2123
+ TypeAlias(node) {
2124
+ return transform.DeclareTypeAlias(node);
2125
+ },
2126
+
2127
+ TypeAnnotation(node) {
2128
+ return {
2129
+ type: 'TSTypeAnnotation',
2130
+ typeAnnotation: transform.TypeAnnotationType(node.typeAnnotation)
2131
+ };
2132
+ },
2133
+
2134
+ TypeAnnotationType(node) {
2135
+ switch (node.type) {
2136
+ case 'AnyTypeAnnotation':
2137
+ return transform.AnyTypeAnnotation(node);
2138
+
2139
+ case 'ArrayTypeAnnotation':
2140
+ return transform.ArrayTypeAnnotation(node);
2141
+
2142
+ case 'BigIntLiteralTypeAnnotation':
2143
+ return transform.BigIntLiteralTypeAnnotation(node);
2144
+
2145
+ case 'BigIntTypeAnnotation':
2146
+ return transform.BigIntTypeAnnotation(node);
2147
+
2148
+ case 'BooleanLiteralTypeAnnotation':
2149
+ return transform.BooleanLiteralTypeAnnotation(node);
2150
+
2151
+ case 'BooleanTypeAnnotation':
2152
+ return transform.BooleanTypeAnnotation(node);
2153
+
2154
+ case 'EmptyTypeAnnotation':
2155
+ return transform.EmptyTypeAnnotation(node);
2156
+
2157
+ case 'ExistsTypeAnnotation':
2158
+ return transform.ExistsTypeAnnotation(node);
2159
+
2160
+ case 'FunctionTypeAnnotation':
2161
+ return transform.FunctionTypeAnnotation(node);
2162
+
2163
+ case 'GenericTypeAnnotation':
2164
+ return transform.GenericTypeAnnotation(node);
2165
+
2166
+ case 'IndexedAccessType':
2167
+ return transform.IndexedAccessType(node);
2168
+
2169
+ case 'InterfaceTypeAnnotation':
2170
+ return transform.InterfaceTypeAnnotation(node);
2171
+
2172
+ case 'IntersectionTypeAnnotation':
2173
+ return transform.IntersectionTypeAnnotation(node);
2174
+
2175
+ case 'MixedTypeAnnotation':
2176
+ return transform.MixedTypeAnnotation(node);
2177
+
2178
+ case 'NullLiteralTypeAnnotation':
2179
+ return transform.NullLiteralTypeAnnotation(node);
2180
+
2181
+ case 'NullableTypeAnnotation':
2182
+ return transform.NullableTypeAnnotation(node);
2183
+
2184
+ case 'NumberLiteralTypeAnnotation':
2185
+ return transform.NumberLiteralTypeAnnotation(node);
2186
+
2187
+ case 'NumberTypeAnnotation':
2188
+ return transform.NumberTypeAnnotation(node);
2189
+
2190
+ case 'ObjectTypeAnnotation':
2191
+ return transform.ObjectTypeAnnotation(node);
2192
+
2193
+ case 'OptionalIndexedAccessType':
2194
+ return transform.OptionalIndexedAccessType(node);
2195
+
2196
+ case 'QualifiedTypeIdentifier':
2197
+ return transform.QualifiedTypeIdentifier(node);
2198
+
2199
+ case 'StringLiteralTypeAnnotation':
2200
+ return transform.StringLiteralTypeAnnotation(node);
2201
+
2202
+ case 'StringTypeAnnotation':
2203
+ return transform.StringTypeAnnotation(node);
2204
+
2205
+ case 'SymbolTypeAnnotation':
2206
+ return transform.SymbolTypeAnnotation(node);
2207
+
2208
+ case 'ThisTypeAnnotation':
2209
+ return transform.ThisTypeAnnotation(node);
2210
+
2211
+ case 'TupleTypeAnnotation':
2212
+ return transform.TupleTypeAnnotation(node);
2213
+
2214
+ case 'TypeofTypeAnnotation':
2215
+ return transform.TypeofTypeAnnotation(node);
2216
+
2217
+ case 'UnionTypeAnnotation':
2218
+ return transform.UnionTypeAnnotation(node);
2219
+
2220
+ case 'VoidTypeAnnotation':
2221
+ return transform.VoidTypeAnnotation(node);
2222
+
2223
+ default:
2224
+ throw unexpectedTranslationError(node, `Unhandled type ${node.type}`);
2225
+ }
2226
+ },
2227
+
2228
+ TypeofTypeAnnotation(node) {
2229
+ const argument = transform.TypeAnnotationType(node.argument);
2230
+
2231
+ if (argument.type !== 'TSTypeReference') {
2232
+ throw unexpectedTranslationError(node, `Expected to find a type reference as the argument to the TypeofTypeAnnotation, but got ${node.argument.type}`);
2233
+ }
2234
+
2235
+ return {
2236
+ type: 'TSTypeQuery',
2237
+ exprName: argument.typeName,
2238
+ typeParameters: argument.typeParameters
2239
+ };
2240
+ },
2241
+
2242
+ TypeParameter(node) {
2243
+ /*
2244
+ TODO - flow models variance as explicit syntax, but but TS resolves it automatically
2245
+ TS does have syntax for explicit variance, but you can introduce a TS error if the
2246
+ marked parameter isn't used in the location that TS expects them to be in.
2247
+ To make it easier for now let's just rely on TS's auto-resolution.
2248
+ ```
2249
+ const variance =
2250
+ new Set(
2251
+ node.variance == null
2252
+ ? // by default flow generics act invariantly
2253
+ ['in', 'out']
2254
+ : node.variance.kind === 'plus'
2255
+ ? // covariant
2256
+ ['out']
2257
+ : // contravariant
2258
+ ['in'],
2259
+ );
2260
+ ```
2261
+ */
2262
+ return {
2263
+ type: 'TSTypeParameter',
2264
+ name: {
2265
+ type: 'Identifier',
2266
+ name: node.name
2267
+ },
2268
+ constraint: node.bound == null ? undefined : transform.TypeAnnotationType(node.bound.typeAnnotation),
2269
+ default: node.default == null ? undefined : transform.TypeAnnotationType(node.default),
2270
+ in: false,
2271
+ out: false // in: variance.has('in'),
2272
+ // out: variance.has('out'),
2273
+
2274
+ };
2275
+ },
2276
+
2277
+ TypeParameterDeclaration(node) {
2278
+ return {
2279
+ type: 'TSTypeParameterDeclaration',
2280
+ params: node.params.map(transform.TypeParameter)
2281
+ };
2282
+ },
2283
+
2284
+ TypeParameterInstantiation(node) {
2285
+ return {
2286
+ type: 'TSTypeParameterInstantiation',
2287
+ params: node.params.map(transform.TypeAnnotationType)
2288
+ };
2289
+ },
2290
+
2291
+ UnionTypeAnnotation(node) {
2292
+ return {
2293
+ type: 'TSUnionType',
2294
+ types: node.types.map(transform.TypeAnnotationType)
2295
+ };
2296
+ },
2297
+
2298
+ VoidTypeAnnotation(_node) {
2299
+ return {
2300
+ type: 'TSVoidKeyword'
2301
+ };
2302
+ }
2303
+
2304
+ }; // wrap each transform so that we automatically preserve jsdoc comments
2305
+ // this just saves us manually wiring up every single case
2306
+
2307
+ for (const key of Object.keys(transform)) {
2308
+ const originalFn = transform[key]; // $FlowExpectedError[cannot-write]
2309
+ // $FlowExpectedError[missing-local-annot]
2310
+
2311
+ transform[key] = (node, ...args) => {
2312
+ const result = originalFn(node, ...args);
2313
+
2314
+ if (Array.isArray(result)) {
2315
+ cloneJSDocCommentsToNewNode(node, result[0]);
2316
+ } else {
2317
+ cloneJSDocCommentsToNewNode(node, result);
2318
+ }
2319
+
2320
+ return result;
2321
+ };
2322
+ }
2323
+
2324
+ return transform;
2325
+ };