flow-api-translator 0.36.0 → 0.37.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.
@@ -1,12 +1,3 @@
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", {
@@ -43,8 +34,7 @@ function constructFlowNode(node) {
43
34
  };
44
35
  }
45
36
 
46
- const makeCommentOwnLine = // $FlowExpectedError[incompatible-type] - trust me this re-type is 100% safe
47
- _hermesTransform.makeCommentOwnLine;
37
+ const makeCommentOwnLine = _hermesTransform.makeCommentOwnLine;
48
38
 
49
39
  function TSDefToFlowDef(originalCode, ast, opts) {
50
40
  const flowBody = [];
@@ -66,10 +56,7 @@ function TSDefToFlowDef(originalCode, ast, opts) {
66
56
  }
67
57
 
68
58
  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
-
59
+ }
73
60
 
74
61
  const getTransforms = (originalCode, opts) => {
75
62
  let code = originalCode;
@@ -104,11 +91,8 @@ const getTransforms = (originalCode, opts) => {
104
91
  leading: true,
105
92
  printed: false
106
93
  };
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
-
94
+ code = makeCommentOwnLine(code, comment);
95
+ (_node$comments = node.comments) != null ? _node$comments : node.comments = [];
112
96
  node.comments.push(comment);
113
97
  }
114
98
 
@@ -183,14 +167,12 @@ const getTransforms = (originalCode, opts) => {
183
167
 
184
168
  case 'TSAbstractPropertyDefinition':
185
169
  case 'PropertyDefinition':
186
- // $FlowFixMe[incompatible-type] ambiguous node
187
170
  Transform._translateIntoObjectProp(classItem, properties, indexers);
188
171
 
189
172
  break;
190
173
 
191
174
  case 'MethodDefinition':
192
175
  case 'TSAbstractMethodDefinition':
193
- // $FlowFixMe[incompatible-type] ambiguous node
194
176
  Transform._translateIntoObjectMethod(classItem, properties);
195
177
 
196
178
  break;
@@ -296,13 +278,11 @@ const getTransforms = (originalCode, opts) => {
296
278
 
297
279
  switch (node.declaration.type) {
298
280
  case 'ClassDeclaration':
299
- declaration = Transform.ClassDeclarationWithName( // possibly missing id
300
- node.declaration);
281
+ declaration = Transform.ClassDeclarationWithName(node.declaration);
301
282
  break;
302
283
 
303
284
  case 'FunctionDeclaration':
304
- declaration = Transform.FunctionDeclarationWithName( // possibly missing id
305
- node.declaration);
285
+ declaration = Transform.FunctionDeclarationWithName(node.declaration);
306
286
  break;
307
287
 
308
288
  case 'TSDeclareFunction':
@@ -355,8 +335,7 @@ const getTransforms = (originalCode, opts) => {
355
335
  case 'ClassDeclaration':
356
336
  return constructFlowNode({
357
337
  type: 'DeclareExportDeclaration',
358
- declaration: Transform.ClassDeclarationWithName( // possibly missing id
359
- node.declaration),
338
+ declaration: Transform.ClassDeclarationWithName(node.declaration),
360
339
  default: false,
361
340
  source: null,
362
341
  specifiers: []
@@ -365,8 +344,7 @@ const getTransforms = (originalCode, opts) => {
365
344
  case 'FunctionDeclaration':
366
345
  return constructFlowNode({
367
346
  type: 'DeclareExportDeclaration',
368
- declaration: Transform.FunctionDeclarationWithName( // possibly missing id
369
- node.declaration),
347
+ declaration: Transform.FunctionDeclarationWithName(node.declaration),
370
348
  default: false,
371
349
  source: null,
372
350
  specifiers: []
@@ -523,8 +501,7 @@ const getTransforms = (originalCode, opts) => {
523
501
  value: node.source.value,
524
502
  raw: node.source.raw
525
503
  }),
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,
504
+ importKind: specifiers.some(s => s.type === 'ImportDefaultSpecifier') && node.importKind === 'type' ? 'typeof' : node.importKind,
528
505
  attributes: [],
529
506
  specifiers
530
507
  });
@@ -691,10 +668,6 @@ const getTransforms = (originalCode, opts) => {
691
668
  return Transform.TSModuleDeclaration(node);
692
669
 
693
670
  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
671
  return [];
699
672
 
700
673
  case 'TSTypeAliasDeclaration':
@@ -936,6 +909,18 @@ const getTransforms = (originalCode, opts) => {
936
909
  }
937
910
 
938
911
  static TSImportType(node) {
912
+ var _node$argument;
913
+
914
+ const source = (_node$argument = node.argument) != null ? _node$argument : node.source == null ? null : {
915
+ type: 'TSLiteralType',
916
+ loc: node.source.loc,
917
+ literal: node.source
918
+ };
919
+
920
+ if (source == null) {
921
+ return unsupportedAnnotation(node, 'import types without a source');
922
+ }
923
+
939
924
  let base = constructFlowNode({
940
925
  type: 'GenericTypeAnnotation',
941
926
  id: constructFlowNode({
@@ -946,16 +931,16 @@ const getTransforms = (originalCode, opts) => {
946
931
  }),
947
932
  typeParameters: constructFlowNode({
948
933
  type: 'TypeParameterInstantiation',
949
- params: [Transform.TSTypeAnnotation(node.argument)]
934
+ params: [Transform.TSTypeAnnotation(source)]
950
935
  })
951
936
  });
952
937
 
953
- if (node.qualifier == null) {
954
- return base;
938
+ if (node.typeArguments != null) {
939
+ return unsupportedAnnotation(node, 'import types with type arguments');
955
940
  }
956
941
 
957
- if (node.typeParameters != null) {
958
- return unsupportedAnnotation(node, 'import types with type parameters');
942
+ if (node.qualifier == null) {
943
+ return base;
959
944
  }
960
945
 
961
946
  let qualifier = Transform.EntityNameToTypeIdentifier(node.qualifier);
@@ -975,9 +960,7 @@ const getTransforms = (originalCode, opts) => {
975
960
  objectType: base,
976
961
  indexType: constructFlowNode({
977
962
  type: 'StringLiteralTypeAnnotation',
978
- // $FlowFixMe[incompatible-type]
979
963
  value: name,
980
- // $FlowFixMe[incompatible-type]
981
964
  raw: `'${name}'`
982
965
  })
983
966
  });
@@ -1051,8 +1034,7 @@ const getTransforms = (originalCode, opts) => {
1051
1034
  }
1052
1035
 
1053
1036
  static TSInterfaceDeclaration(node) {
1054
- const body = Transform.TSTypeLiteralOrInterfaceBody(node.body); // $FlowFixMe[cannot-write]
1055
-
1037
+ const body = Transform.TSTypeLiteralOrInterfaceBody(node.body);
1056
1038
  body.inexact = false;
1057
1039
  return constructFlowNode({
1058
1040
  type: 'InterfaceDeclaration',
@@ -1113,8 +1095,7 @@ const getTransforms = (originalCode, opts) => {
1113
1095
  var _keyTparam$bound;
1114
1096
 
1115
1097
  const keyTparam = Transform.TSTypeParameter(node.typeParameter);
1116
- const sourceType = (_keyTparam$bound = keyTparam.bound) == null ? void 0 : _keyTparam$bound.typeAnnotation; // $FlowFixMe[cannot-write]
1117
-
1098
+ const sourceType = (_keyTparam$bound = keyTparam.bound) == null ? void 0 : _keyTparam$bound.typeAnnotation;
1118
1099
  keyTparam.bound = null;
1119
1100
  const prop = constructFlowNode({
1120
1101
  type: 'ObjectTypeMappedTypeProperty',
@@ -1125,8 +1106,7 @@ const getTransforms = (originalCode, opts) => {
1125
1106
  type: 'Variance',
1126
1107
  kind: 'plus'
1127
1108
  }) : null,
1128
- optional: node.optional === '+' ? 'PlusOptional' : node.optional === '-' ? 'MinusOptional' : // eslint-disable-next-line no-extra-boolean-cast
1129
- Boolean(node.optional) ? 'Optional' : null
1109
+ optional: node.optional === '+' ? 'PlusOptional' : node.optional === '-' ? 'MinusOptional' : Boolean(node.optional) ? 'Optional' : null
1130
1110
  });
1131
1111
  return constructFlowNode({
1132
1112
  type: 'ObjectTypeAnnotation',
@@ -1425,8 +1405,6 @@ const getTransforms = (originalCode, opts) => {
1425
1405
  typeAnnotation: Transform.TSTypeAnnotation(node.typeAnnotation)
1426
1406
  });
1427
1407
  }
1428
- /** A very confusingly named object type */
1429
-
1430
1408
 
1431
1409
  static TSTypeLiteralOrInterfaceBody(node) {
1432
1410
  const properties = [];
@@ -1471,7 +1449,6 @@ const getTransforms = (originalCode, opts) => {
1471
1449
  {
1472
1450
  var _prop$typeAnnotation;
1473
1451
 
1474
- // eslint-disable-next-line no-extra-boolean-cast
1475
1452
  const variance = Boolean(prop.readonly) ? constructFlowNode({
1476
1453
  type: 'Variance',
1477
1454
  kind: 'plus'
@@ -1532,7 +1509,6 @@ const getTransforms = (originalCode, opts) => {
1532
1509
  }
1533
1510
 
1534
1511
  static _translateIntoObjectProp(prop, properties, indexers) {
1535
- // eslint-disable-next-line no-extra-boolean-cast
1536
1512
  const variance = Boolean(prop.readonly) ? constructFlowNode({
1537
1513
  type: 'Variance',
1538
1514
  kind: 'plus'
@@ -1741,10 +1717,18 @@ const getTransforms = (originalCode, opts) => {
1741
1717
  static TSTypeQuery(node) {
1742
1718
  var _Transform$TSTypePara;
1743
1719
 
1720
+ if (node.exprName.type === 'TSImportType') {
1721
+ if (node.typeArguments != null) {
1722
+ return unsupportedAnnotation(node, 'type query imports with type arguments');
1723
+ }
1724
+
1725
+ return Transform.TSImportType(node.exprName);
1726
+ }
1727
+
1744
1728
  return constructFlowNode({
1745
1729
  type: 'TypeofTypeAnnotation',
1746
1730
  argument: Transform.EntityNameToTypeofIdentifier(node.exprName),
1747
- typeArguments: (_Transform$TSTypePara = Transform.TSTypeParameterInstantiationOpt(node.typeParameters)) != null ? _Transform$TSTypePara : undefined
1731
+ typeArguments: (_Transform$TSTypePara = Transform.TSTypeParameterInstantiationOpt(node.typeArguments)) != null ? _Transform$TSTypePara : undefined
1748
1732
  });
1749
1733
  }
1750
1734
 
@@ -1784,7 +1768,6 @@ const getTransforms = (originalCode, opts) => {
1784
1768
  const id = Transform.Identifier(decl.id);
1785
1769
 
1786
1770
  if (id.typeAnnotation == null) {
1787
- // $FlowExpectedError[cannot-write]
1788
1771
  id.typeAnnotation = constructFlowNode({
1789
1772
  type: 'TypeAnnotation',
1790
1773
  typeAnnotation: constructFlowNode({