flow-api-translator 0.26.0 → 0.28.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.
@@ -928,12 +928,44 @@ const getTransforms = (
928
928
  cloneJSDocCommentsToNewNode(member, newNode);
929
929
  classMembers.push(newNode);
930
930
  } else {
931
+ const [key, computed] = (() => {
932
+ const _key = member.key;
933
+ if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
934
+ const name = _key.name.slice(2);
935
+ if (['iterator', 'asyncIterator'].includes(name)) {
936
+ return [
937
+ {
938
+ type: 'MemberExpression',
939
+ computed: false,
940
+ object: {
941
+ type: 'Identifier',
942
+ name: 'Symbol',
943
+ optional: false,
944
+ loc: DUMMY_LOC,
945
+ },
946
+ optional: false,
947
+ property: {
948
+ type: 'Identifier',
949
+ name,
950
+ optional: false,
951
+ loc: DUMMY_LOC,
952
+ },
953
+ loc: DUMMY_LOC,
954
+ },
955
+ true,
956
+ ];
957
+ }
958
+ }
959
+
960
+ return [member.key, member.computed];
961
+ })();
962
+
931
963
  const newNode: TSESTree.MethodDefinitionAmbiguous = {
932
964
  type: 'MethodDefinition',
933
965
  loc: DUMMY_LOC,
934
966
  accessibility: member.accessibility,
935
- computed: member.computed ?? false,
936
- key: member.key,
967
+ computed: computed ?? false,
968
+ key,
937
969
  kind: member.kind,
938
970
  optional: member.optional,
939
971
  override: false,
@@ -1030,8 +1062,9 @@ const getTransforms = (
1030
1062
  superClass:
1031
1063
  superClass == null
1032
1064
  ? null
1033
- : // Bug: superclass.id can be qualified
1034
- transform.Identifier((superClass.id: $FlowFixMe), false),
1065
+ : superClass.id.type === 'QualifiedTypeIdentifier'
1066
+ ? transform.QualifiedTypeIdentifier(superClass.id)
1067
+ : transform.Identifier((superClass.id: $FlowFixMe), false),
1035
1068
  superTypeParameters:
1036
1069
  superClass?.typeParameters == null
1037
1070
  ? undefined
@@ -1057,6 +1090,16 @@ const getTransforms = (
1057
1090
  | TSESTree.TSTypeAliasDeclaration
1058
1091
  ),
1059
1092
  TSESTree.ExportDefaultDeclaration,
1093
+ ]
1094
+ | [
1095
+ (
1096
+ | TSESTree.VariableDeclaration
1097
+ | TSESTree.ClassDeclaration
1098
+ | TSESTree.TSDeclareFunction
1099
+ | TSESTree.TSTypeAliasDeclaration
1100
+ ),
1101
+ TSESTree.TSTypeAliasDeclaration,
1102
+ TSESTree.ExportDefaultDeclaration,
1060
1103
  ] {
1061
1104
  if (node.default === true) {
1062
1105
  const declaration = node.declaration;
@@ -1204,6 +1247,38 @@ const getTransforms = (
1204
1247
  // intentional fallthrough to the "default" handling
1205
1248
  }
1206
1249
 
1250
+ case 'TypeofTypeAnnotation': {
1251
+ if (
1252
+ declaration.type === 'TypeofTypeAnnotation' &&
1253
+ declaration.argument.type === 'Identifier'
1254
+ ) {
1255
+ const name = declaration.argument.name;
1256
+ const exportedVar = topScope.set.get(name);
1257
+ if (exportedVar != null && exportedVar.defs.length === 1) {
1258
+ const def = exportedVar.defs[0];
1259
+
1260
+ switch (def.type) {
1261
+ case 'ClassName': {
1262
+ return {
1263
+ type: 'ExportDefaultDeclaration',
1264
+ declaration: {
1265
+ type: 'Identifier',
1266
+ decorators: [],
1267
+ name,
1268
+ optional: false,
1269
+ loc: DUMMY_LOC,
1270
+ },
1271
+ exportKind: 'value',
1272
+ loc: DUMMY_LOC,
1273
+ };
1274
+ }
1275
+ }
1276
+ }
1277
+ }
1278
+
1279
+ // intentional fallthrough to the "default" handling
1280
+ }
1281
+
1207
1282
  default: {
1208
1283
  /*
1209
1284
  flow allows syntax like
@@ -1243,6 +1318,29 @@ const getTransforms = (
1243
1318
  declare: true,
1244
1319
  kind: 'const',
1245
1320
  },
1321
+ {
1322
+ type: 'TSTypeAliasDeclaration',
1323
+ declare: true,
1324
+ id: {
1325
+ type: 'Identifier',
1326
+ decorators: [],
1327
+ name: SPECIFIER,
1328
+ optional: false,
1329
+ loc: DUMMY_LOC,
1330
+ },
1331
+ typeAnnotation: {
1332
+ type: 'TSTypeQuery',
1333
+ exprName: {
1334
+ type: 'Identifier',
1335
+ decorators: [],
1336
+ name: SPECIFIER,
1337
+ optional: false,
1338
+ loc: DUMMY_LOC,
1339
+ },
1340
+ loc: DUMMY_LOC,
1341
+ },
1342
+ loc: DUMMY_LOC,
1343
+ },
1246
1344
  {
1247
1345
  type: 'ExportDefaultDeclaration',
1248
1346
  loc: DUMMY_LOC,
@@ -1343,9 +1441,66 @@ const getTransforms = (
1343
1441
  }
1344
1442
  })();
1345
1443
 
1346
- return declarations.map(
1347
- ({declaration, exportKind}) =>
1348
- ({
1444
+ const mappedDeclarations: Array<
1445
+ | TSESTree.ExportNamedDeclaration
1446
+ | Array<TSESTree.ExportNamedDeclaration>,
1447
+ > = declarations.map(({declaration, exportKind}) => {
1448
+ if (
1449
+ declaration.type === 'VariableDeclaration' &&
1450
+ declaration.declarations.length === 1
1451
+ ) {
1452
+ const ident = declaration.declarations[0].id;
1453
+ if (ident.type === 'Identifier') {
1454
+ const name = ident.name;
1455
+ return [
1456
+ {
1457
+ type: 'ExportNamedDeclaration',
1458
+ loc: DUMMY_LOC,
1459
+ // flow does not currently support assertions
1460
+ assertions: [],
1461
+ declaration,
1462
+ exportKind,
1463
+ source: null,
1464
+ specifiers: [],
1465
+ },
1466
+ {
1467
+ type: 'ExportNamedDeclaration',
1468
+ declaration: {
1469
+ type: 'TSTypeAliasDeclaration',
1470
+ declare: true,
1471
+ id: {
1472
+ type: 'Identifier',
1473
+ decorators: [],
1474
+ name,
1475
+ optional: false,
1476
+ loc: DUMMY_LOC,
1477
+ },
1478
+ typeAnnotation: {
1479
+ type: 'TSTypeQuery',
1480
+ exprName: {
1481
+ type: 'Identifier',
1482
+ decorators: [],
1483
+ name,
1484
+ optional: false,
1485
+ loc: DUMMY_LOC,
1486
+ },
1487
+ loc: DUMMY_LOC,
1488
+ },
1489
+ loc: DUMMY_LOC,
1490
+ },
1491
+ source: null,
1492
+ loc: DUMMY_LOC,
1493
+ specifiers: [],
1494
+ exportKind: 'type',
1495
+ // flow does not currently support assertions
1496
+ assertions: [],
1497
+ },
1498
+ ];
1499
+ }
1500
+ }
1501
+
1502
+ const exportNamedDeclaration: TSESTree.ExportNamedDeclarationWithoutSourceWithSingle =
1503
+ {
1349
1504
  type: 'ExportNamedDeclaration',
1350
1505
  loc: DUMMY_LOC,
1351
1506
  // flow does not currently support assertions
@@ -1354,8 +1509,11 @@ const getTransforms = (
1354
1509
  exportKind,
1355
1510
  source: null,
1356
1511
  specifiers: [],
1357
- }: TSESTree.ExportNamedDeclarationWithoutSourceWithSingle),
1358
- );
1512
+ };
1513
+ return exportNamedDeclaration;
1514
+ });
1515
+
1516
+ return mappedDeclarations.flat();
1359
1517
  } else {
1360
1518
  return ({
1361
1519
  type: 'ExportNamedDeclaration',
@@ -1611,6 +1769,50 @@ const getTransforms = (
1611
1769
  // TODO - we could simulate this in a variety of ways
1612
1770
  // Examples - https://basarat.gitbook.io/typescript/main-1/nominaltyping
1613
1771
 
1772
+ if (node.supertype == null && node.typeParameters == null) {
1773
+ const name = `__${node.id.name}__`;
1774
+ return {
1775
+ type: 'TSTypeAliasDeclaration',
1776
+ loc: DUMMY_LOC,
1777
+ declare: true,
1778
+ id: transform.Identifier(node.id, false),
1779
+ typeAnnotation: {
1780
+ type: 'TSIntersectionType',
1781
+ types: [
1782
+ {
1783
+ type: 'TSSymbolKeyword',
1784
+ loc: DUMMY_LOC,
1785
+ },
1786
+ {
1787
+ type: 'TSTypeLiteral',
1788
+ loc: DUMMY_LOC,
1789
+ members: [
1790
+ {
1791
+ type: 'TSPropertySignature',
1792
+ computed: false,
1793
+ loc: DUMMY_LOC,
1794
+ key: {
1795
+ type: 'Identifier',
1796
+ name: name,
1797
+ loc: DUMMY_LOC,
1798
+ },
1799
+ typeAnnotation: {
1800
+ type: 'TSTypeAnnotation',
1801
+ loc: DUMMY_LOC,
1802
+ typeAnnotation: {
1803
+ type: 'TSStringKeyword',
1804
+ loc: DUMMY_LOC,
1805
+ },
1806
+ },
1807
+ },
1808
+ ],
1809
+ },
1810
+ ],
1811
+ loc: DUMMY_LOC,
1812
+ },
1813
+ };
1814
+ }
1815
+
1614
1816
  return {
1615
1817
  type: 'TSTypeAliasDeclaration',
1616
1818
  loc: DUMMY_LOC,
@@ -1971,6 +2173,37 @@ const getTransforms = (
1971
2173
  return unsupportedAnnotation(node, fullTypeName);
1972
2174
  }
1973
2175
 
2176
+ case '$ArrayBufferView': {
2177
+ // `$ArrayBufferView` => `ArrayBufferView`
2178
+ return {
2179
+ type: 'TSTypeReference',
2180
+ loc: DUMMY_LOC,
2181
+ typeName: {
2182
+ type: 'Identifier',
2183
+ loc: DUMMY_LOC,
2184
+ name: 'ArrayBufferView',
2185
+ },
2186
+ };
2187
+ }
2188
+
2189
+ case '$ArrayLike': {
2190
+ // `$ArrayLike<T>` => `ArrayLike<T>`
2191
+ return {
2192
+ type: 'TSTypeReference',
2193
+ loc: DUMMY_LOC,
2194
+ typeName: {
2195
+ type: 'Identifier',
2196
+ loc: DUMMY_LOC,
2197
+ name: 'ArrayLike',
2198
+ },
2199
+ typeParameters: {
2200
+ type: 'TSTypeParameterInstantiation',
2201
+ loc: DUMMY_LOC,
2202
+ params: assertHasExactlyNTypeParameters(1),
2203
+ },
2204
+ };
2205
+ }
2206
+
1974
2207
  case '$Diff':
1975
2208
  case '$Rest': {
1976
2209
  // `$Diff<A, B>` => `Pick<A, Exclude<keyof A, keyof B>>`
@@ -2545,8 +2778,8 @@ const getTransforms = (
2545
2778
  },
2546
2779
  };
2547
2780
  }
2548
- // React.ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2549
- // React$ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2781
+ // React.ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2782
+ // React$ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2550
2783
  case 'React$ElementRef':
2551
2784
  case 'React.ElementRef':
2552
2785
  return {
@@ -2559,7 +2792,7 @@ const getTransforms = (
2559
2792
  right: {
2560
2793
  type: 'Identifier',
2561
2794
  loc: DUMMY_LOC,
2562
- name: `ElementRef`,
2795
+ name: `ComponentRef`,
2563
2796
  },
2564
2797
  },
2565
2798
  typeParameters: {
@@ -2587,7 +2820,7 @@ const getTransforms = (
2587
2820
  },
2588
2821
  },
2589
2822
  };
2590
- // React.MixedElement -> JSX.Element
2823
+ // React.MixedElement -> React.JSX.Element
2591
2824
  case 'React$MixedElement':
2592
2825
  case 'React.MixedElement': {
2593
2826
  assertHasExactlyNTypeParameters(0);
@@ -2598,9 +2831,18 @@ const getTransforms = (
2598
2831
  type: 'TSQualifiedName',
2599
2832
  loc: DUMMY_LOC,
2600
2833
  left: {
2601
- type: 'Identifier',
2834
+ type: 'TSQualifiedName',
2602
2835
  loc: DUMMY_LOC,
2603
- name: 'JSX',
2836
+ left: {
2837
+ type: 'Identifier',
2838
+ loc: DUMMY_LOC,
2839
+ name: 'React',
2840
+ },
2841
+ right: {
2842
+ type: 'Identifier',
2843
+ loc: DUMMY_LOC,
2844
+ name: 'JSX',
2845
+ },
2604
2846
  },
2605
2847
  right: {
2606
2848
  type: 'Identifier',
@@ -2742,8 +2984,8 @@ const getTransforms = (
2742
2984
  },
2743
2985
  };
2744
2986
  }
2745
- // React.ElementConfig<A> -> JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2746
- // React$ElementConfig<A> -> JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2987
+ // React.ElementConfig<A> -> React.JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2988
+ // React$ElementConfig<A> -> React.JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2747
2989
  case 'React.ElementConfig':
2748
2990
  case 'React$ElementConfig': {
2749
2991
  const [param] = assertHasExactlyNTypeParameters(1);
@@ -2754,9 +2996,18 @@ const getTransforms = (
2754
2996
  type: 'TSQualifiedName',
2755
2997
  loc: DUMMY_LOC,
2756
2998
  left: {
2757
- type: 'Identifier',
2999
+ type: 'TSQualifiedName',
2758
3000
  loc: DUMMY_LOC,
2759
- name: 'JSX',
3001
+ left: {
3002
+ type: 'Identifier',
3003
+ loc: DUMMY_LOC,
3004
+ name: 'React',
3005
+ },
3006
+ right: {
3007
+ type: 'Identifier',
3008
+ loc: DUMMY_LOC,
3009
+ name: 'JSX',
3010
+ },
2760
3011
  },
2761
3012
  right: {
2762
3013
  type: 'Identifier',
@@ -2963,8 +3214,10 @@ const getTransforms = (
2963
3214
  return {
2964
3215
  type: 'TSInterfaceHeritage',
2965
3216
  loc: DUMMY_LOC,
2966
- // Bug: node.id can be qualified
2967
- expression: transform.Identifier((node.id: $FlowFixMe), false),
3217
+ expression:
3218
+ node.id.type === 'QualifiedTypeIdentifier'
3219
+ ? transform.QualifiedTypeIdentifier(node.id)
3220
+ : transform.Identifier(node.id, false),
2968
3221
  typeParameters:
2969
3222
  node.typeParameters == null
2970
3223
  ? undefined
@@ -3255,8 +3508,8 @@ const getTransforms = (
3255
3508
  type T = { ...T1, ...T2, ...T3, b: string };
3256
3509
  // becomes
3257
3510
  type T =
3258
- & Omit<T1, keyof (T2 | T3 | { b: string })>
3259
- & Omit<T2, keyof (T3 | { b: string })>
3511
+ & Omit<T1, keyof T2 | keyof T3 | keyof { b: string }>
3512
+ & Omit<T2, keyof T3 | keyof { b: string }>
3260
3513
  & Omit<T3, keyof { b: string }>
3261
3514
  & { b: string };
3262
3515
  ```
@@ -3285,7 +3538,10 @@ const getTransforms = (
3285
3538
  }
3286
3539
 
3287
3540
  const spreadType = transformTypeAnnotationType(property.argument);
3288
- if (spreadType.type !== 'TSTypeReference') {
3541
+ if (
3542
+ spreadType.type !== 'TSTypeReference' &&
3543
+ spreadType.type !== 'TSTypeQuery'
3544
+ ) {
3289
3545
  return unsupportedAnnotation(
3290
3546
  property,
3291
3547
  'object types with complex spreads',
@@ -3334,14 +3590,22 @@ const getTransforms = (
3334
3590
  params: [
3335
3591
  currentType,
3336
3592
  {
3337
- type: 'TSTypeOperator',
3593
+ type: 'TSUnionType',
3338
3594
  loc: DUMMY_LOC,
3339
- operator: 'keyof',
3340
- typeAnnotation: {
3341
- type: 'TSUnionType',
3342
- loc: DUMMY_LOC,
3343
- types: [...remainingTypes, objectType],
3344
- },
3595
+ types: [
3596
+ ...remainingTypes.map(t => ({
3597
+ type: 'TSTypeOperator',
3598
+ loc: DUMMY_LOC,
3599
+ operator: 'keyof',
3600
+ typeAnnotation: t,
3601
+ })),
3602
+ {
3603
+ type: 'TSTypeOperator',
3604
+ loc: DUMMY_LOC,
3605
+ operator: 'keyof',
3606
+ typeAnnotation: objectType,
3607
+ },
3608
+ ],
3345
3609
  },
3346
3610
  ],
3347
3611
  },
@@ -3371,7 +3635,38 @@ const getTransforms = (
3371
3635
  },
3372
3636
  ObjectTypeIndexer(
3373
3637
  node: FlowESTree.ObjectTypeIndexer,
3374
- ): TSESTree.TSIndexSignature {
3638
+ ): TSESTree.TSIndexSignature | TSESTree.TSPropertySignatureComputedName {
3639
+ if (node.key.type === 'GenericTypeAnnotation') {
3640
+ const ident =
3641
+ node.key.id.type === 'Identifier' ? node.key.id : node.key.id.id;
3642
+ return {
3643
+ type: 'TSPropertySignature',
3644
+ computed: true,
3645
+ loc: DUMMY_LOC,
3646
+ key: {
3647
+ type: 'BinaryExpression',
3648
+ operator: 'in',
3649
+ loc: DUMMY_LOC,
3650
+ left: {
3651
+ type: 'Identifier',
3652
+ name: node.id == null ? '$$Key$$' : node.id.name,
3653
+ loc: DUMMY_LOC,
3654
+ },
3655
+ right: {
3656
+ type: 'Identifier',
3657
+ name: ident.name,
3658
+ loc: DUMMY_LOC,
3659
+ },
3660
+ },
3661
+ readonly: node.variance?.kind === 'plus',
3662
+ static: node.static,
3663
+ typeAnnotation: {
3664
+ type: 'TSTypeAnnotation',
3665
+ loc: DUMMY_LOC,
3666
+ typeAnnotation: transformTypeAnnotationType(node.value),
3667
+ },
3668
+ };
3669
+ }
3375
3670
  return {
3376
3671
  type: 'TSIndexSignature',
3377
3672
  loc: DUMMY_LOC,