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.
@@ -879,14 +879,45 @@ const getTransforms = (originalCode, scopeManager, opts) => {
879
879
  cloneJSDocCommentsToNewNode(member, newNode);
880
880
  classMembers.push(newNode);
881
881
  } else {
882
- var _member$computed, _member$static;
882
+ var _member$static;
883
+
884
+ const [key, computed] = (() => {
885
+ const _key = member.key;
886
+
887
+ if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
888
+ const name = _key.name.slice(2);
889
+
890
+ if (['iterator', 'asyncIterator'].includes(name)) {
891
+ return [{
892
+ type: 'MemberExpression',
893
+ computed: false,
894
+ object: {
895
+ type: 'Identifier',
896
+ name: 'Symbol',
897
+ optional: false,
898
+ loc: DUMMY_LOC
899
+ },
900
+ optional: false,
901
+ property: {
902
+ type: 'Identifier',
903
+ name,
904
+ optional: false,
905
+ loc: DUMMY_LOC
906
+ },
907
+ loc: DUMMY_LOC
908
+ }, true];
909
+ }
910
+ }
911
+
912
+ return [member.key, member.computed];
913
+ })();
883
914
 
884
915
  const newNode = {
885
916
  type: 'MethodDefinition',
886
917
  loc: DUMMY_LOC,
887
918
  accessibility: member.accessibility,
888
- computed: (_member$computed = member.computed) != null ? _member$computed : false,
889
- key: member.key,
919
+ computed: computed != null ? computed : false,
920
+ key,
890
921
  kind: member.kind,
891
922
  optional: member.optional,
892
923
  override: false,
@@ -914,13 +945,13 @@ const getTransforms = (originalCode, scopeManager, opts) => {
914
945
 
915
946
  case 'TSPropertySignature':
916
947
  {
917
- var _member$computed2, _member$static2;
948
+ var _member$computed, _member$static2;
918
949
 
919
950
  const newNode = {
920
951
  type: 'PropertyDefinition',
921
952
  loc: DUMMY_LOC,
922
953
  accessibility: member.accessibility,
923
- computed: (_member$computed2 = member.computed) != null ? _member$computed2 : false,
954
+ computed: (_member$computed = member.computed) != null ? _member$computed : false,
924
955
  declare: false,
925
956
  key: member.key,
926
957
  optional: member.optional,
@@ -974,8 +1005,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
974
1005
  declare: true,
975
1006
  id: transform.Identifier(node.id, false),
976
1007
  implements: node.implements == null ? undefined : node.implements.map(transform.ClassImplements),
977
- superClass: superClass == null ? null : // Bug: superclass.id can be qualified
978
- transform.Identifier(superClass.id, false),
1008
+ superClass: superClass == null ? null : superClass.id.type === 'QualifiedTypeIdentifier' ? transform.QualifiedTypeIdentifier(superClass.id) : transform.Identifier(superClass.id, false),
979
1009
  superTypeParameters: (superClass == null ? void 0 : superClass.typeParameters) == null ? undefined : transform.TypeParameterInstantiation(superClass.typeParameters),
980
1010
  typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters) // TODO - mixins??
981
1011
 
@@ -1117,6 +1147,37 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1117
1147
 
1118
1148
  }
1119
1149
 
1150
+ case 'TypeofTypeAnnotation':
1151
+ {
1152
+ if (declaration.type === 'TypeofTypeAnnotation' && declaration.argument.type === 'Identifier') {
1153
+ const name = declaration.argument.name;
1154
+ const exportedVar = topScope.set.get(name);
1155
+
1156
+ if (exportedVar != null && exportedVar.defs.length === 1) {
1157
+ const def = exportedVar.defs[0];
1158
+
1159
+ switch (def.type) {
1160
+ case 'ClassName':
1161
+ {
1162
+ return {
1163
+ type: 'ExportDefaultDeclaration',
1164
+ declaration: {
1165
+ type: 'Identifier',
1166
+ decorators: [],
1167
+ name,
1168
+ optional: false,
1169
+ loc: DUMMY_LOC
1170
+ },
1171
+ exportKind: 'value',
1172
+ loc: DUMMY_LOC
1173
+ };
1174
+ }
1175
+ }
1176
+ }
1177
+ } // intentional fallthrough to the "default" handling
1178
+
1179
+ }
1180
+
1120
1181
  default:
1121
1182
  {
1122
1183
  /*
@@ -1152,6 +1213,28 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1152
1213
  }],
1153
1214
  declare: true,
1154
1215
  kind: 'const'
1216
+ }, {
1217
+ type: 'TSTypeAliasDeclaration',
1218
+ declare: true,
1219
+ id: {
1220
+ type: 'Identifier',
1221
+ decorators: [],
1222
+ name: SPECIFIER,
1223
+ optional: false,
1224
+ loc: DUMMY_LOC
1225
+ },
1226
+ typeAnnotation: {
1227
+ type: 'TSTypeQuery',
1228
+ exprName: {
1229
+ type: 'Identifier',
1230
+ decorators: [],
1231
+ name: SPECIFIER,
1232
+ optional: false,
1233
+ loc: DUMMY_LOC
1234
+ },
1235
+ loc: DUMMY_LOC
1236
+ },
1237
+ loc: DUMMY_LOC
1155
1238
  }, {
1156
1239
  type: 'ExportDefaultDeclaration',
1157
1240
  loc: DUMMY_LOC,
@@ -1243,19 +1326,72 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1243
1326
  }
1244
1327
  })();
1245
1328
 
1246
- return declarations.map(({
1329
+ const mappedDeclarations = declarations.map(({
1247
1330
  declaration,
1248
1331
  exportKind
1249
- }) => ({
1250
- type: 'ExportNamedDeclaration',
1251
- loc: DUMMY_LOC,
1252
- // flow does not currently support assertions
1253
- assertions: [],
1254
- declaration,
1255
- exportKind,
1256
- source: null,
1257
- specifiers: []
1258
- }));
1332
+ }) => {
1333
+ if (declaration.type === 'VariableDeclaration' && declaration.declarations.length === 1) {
1334
+ const ident = declaration.declarations[0].id;
1335
+
1336
+ if (ident.type === 'Identifier') {
1337
+ const name = ident.name;
1338
+ return [{
1339
+ type: 'ExportNamedDeclaration',
1340
+ loc: DUMMY_LOC,
1341
+ // flow does not currently support assertions
1342
+ assertions: [],
1343
+ declaration,
1344
+ exportKind,
1345
+ source: null,
1346
+ specifiers: []
1347
+ }, {
1348
+ type: 'ExportNamedDeclaration',
1349
+ declaration: {
1350
+ type: 'TSTypeAliasDeclaration',
1351
+ declare: true,
1352
+ id: {
1353
+ type: 'Identifier',
1354
+ decorators: [],
1355
+ name,
1356
+ optional: false,
1357
+ loc: DUMMY_LOC
1358
+ },
1359
+ typeAnnotation: {
1360
+ type: 'TSTypeQuery',
1361
+ exprName: {
1362
+ type: 'Identifier',
1363
+ decorators: [],
1364
+ name,
1365
+ optional: false,
1366
+ loc: DUMMY_LOC
1367
+ },
1368
+ loc: DUMMY_LOC
1369
+ },
1370
+ loc: DUMMY_LOC
1371
+ },
1372
+ source: null,
1373
+ loc: DUMMY_LOC,
1374
+ specifiers: [],
1375
+ exportKind: 'type',
1376
+ // flow does not currently support assertions
1377
+ assertions: []
1378
+ }];
1379
+ }
1380
+ }
1381
+
1382
+ const exportNamedDeclaration = {
1383
+ type: 'ExportNamedDeclaration',
1384
+ loc: DUMMY_LOC,
1385
+ // flow does not currently support assertions
1386
+ assertions: [],
1387
+ declaration,
1388
+ exportKind,
1389
+ source: null,
1390
+ specifiers: []
1391
+ };
1392
+ return exportNamedDeclaration;
1393
+ });
1394
+ return mappedDeclarations.flat();
1259
1395
  } else {
1260
1396
  return {
1261
1397
  type: 'ExportNamedDeclaration',
@@ -1472,6 +1608,45 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1472
1608
  // TS doesn't currently have nominal types - https://github.com/Microsoft/Typescript/issues/202
1473
1609
  // TODO - we could simulate this in a variety of ways
1474
1610
  // Examples - https://basarat.gitbook.io/typescript/main-1/nominaltyping
1611
+ if (node.supertype == null && node.typeParameters == null) {
1612
+ const name = `__${node.id.name}__`;
1613
+ return {
1614
+ type: 'TSTypeAliasDeclaration',
1615
+ loc: DUMMY_LOC,
1616
+ declare: true,
1617
+ id: transform.Identifier(node.id, false),
1618
+ typeAnnotation: {
1619
+ type: 'TSIntersectionType',
1620
+ types: [{
1621
+ type: 'TSSymbolKeyword',
1622
+ loc: DUMMY_LOC
1623
+ }, {
1624
+ type: 'TSTypeLiteral',
1625
+ loc: DUMMY_LOC,
1626
+ members: [{
1627
+ type: 'TSPropertySignature',
1628
+ computed: false,
1629
+ loc: DUMMY_LOC,
1630
+ key: {
1631
+ type: 'Identifier',
1632
+ name: name,
1633
+ loc: DUMMY_LOC
1634
+ },
1635
+ typeAnnotation: {
1636
+ type: 'TSTypeAnnotation',
1637
+ loc: DUMMY_LOC,
1638
+ typeAnnotation: {
1639
+ type: 'TSStringKeyword',
1640
+ loc: DUMMY_LOC
1641
+ }
1642
+ }
1643
+ }]
1644
+ }],
1645
+ loc: DUMMY_LOC
1646
+ }
1647
+ };
1648
+ }
1649
+
1475
1650
  return {
1476
1651
  type: 'TSTypeAliasDeclaration',
1477
1652
  loc: DUMMY_LOC,
@@ -1782,6 +1957,39 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1782
1957
  return unsupportedAnnotation(node, fullTypeName);
1783
1958
  }
1784
1959
 
1960
+ case '$ArrayBufferView':
1961
+ {
1962
+ // `$ArrayBufferView` => `ArrayBufferView`
1963
+ return {
1964
+ type: 'TSTypeReference',
1965
+ loc: DUMMY_LOC,
1966
+ typeName: {
1967
+ type: 'Identifier',
1968
+ loc: DUMMY_LOC,
1969
+ name: 'ArrayBufferView'
1970
+ }
1971
+ };
1972
+ }
1973
+
1974
+ case '$ArrayLike':
1975
+ {
1976
+ // `$ArrayLike<T>` => `ArrayLike<T>`
1977
+ return {
1978
+ type: 'TSTypeReference',
1979
+ loc: DUMMY_LOC,
1980
+ typeName: {
1981
+ type: 'Identifier',
1982
+ loc: DUMMY_LOC,
1983
+ name: 'ArrayLike'
1984
+ },
1985
+ typeParameters: {
1986
+ type: 'TSTypeParameterInstantiation',
1987
+ loc: DUMMY_LOC,
1988
+ params: assertHasExactlyNTypeParameters(1)
1989
+ }
1990
+ };
1991
+ }
1992
+
1785
1993
  case '$Diff':
1786
1994
  case '$Rest':
1787
1995
  {
@@ -2349,8 +2557,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2349
2557
  }
2350
2558
  };
2351
2559
  }
2352
- // React.ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2353
- // React$ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2560
+ // React.ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2561
+ // React$ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2354
2562
 
2355
2563
  case 'React$ElementRef':
2356
2564
  case 'React.ElementRef':
@@ -2364,7 +2572,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2364
2572
  right: {
2365
2573
  type: 'Identifier',
2366
2574
  loc: DUMMY_LOC,
2367
- name: `ElementRef`
2575
+ name: `ComponentRef`
2368
2576
  }
2369
2577
  },
2370
2578
  typeParameters: {
@@ -2393,7 +2601,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2393
2601
  }
2394
2602
  }
2395
2603
  };
2396
- // React.MixedElement -> JSX.Element
2604
+ // React.MixedElement -> React.JSX.Element
2397
2605
 
2398
2606
  case 'React$MixedElement':
2399
2607
  case 'React.MixedElement':
@@ -2406,9 +2614,18 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2406
2614
  type: 'TSQualifiedName',
2407
2615
  loc: DUMMY_LOC,
2408
2616
  left: {
2409
- type: 'Identifier',
2617
+ type: 'TSQualifiedName',
2410
2618
  loc: DUMMY_LOC,
2411
- name: 'JSX'
2619
+ left: {
2620
+ type: 'Identifier',
2621
+ loc: DUMMY_LOC,
2622
+ name: 'React'
2623
+ },
2624
+ right: {
2625
+ type: 'Identifier',
2626
+ loc: DUMMY_LOC,
2627
+ name: 'JSX'
2628
+ }
2412
2629
  },
2413
2630
  right: {
2414
2631
  type: 'Identifier',
@@ -2547,8 +2764,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2547
2764
  }
2548
2765
  };
2549
2766
  }
2550
- // React.ElementConfig<A> -> JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2551
- // React$ElementConfig<A> -> JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2767
+ // React.ElementConfig<A> -> React.JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2768
+ // React$ElementConfig<A> -> React.JSX.LibraryManagedAttributes<A, React.ComponentProps<A>>
2552
2769
 
2553
2770
  case 'React.ElementConfig':
2554
2771
  case 'React$ElementConfig':
@@ -2561,9 +2778,18 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2561
2778
  type: 'TSQualifiedName',
2562
2779
  loc: DUMMY_LOC,
2563
2780
  left: {
2564
- type: 'Identifier',
2781
+ type: 'TSQualifiedName',
2565
2782
  loc: DUMMY_LOC,
2566
- name: 'JSX'
2783
+ left: {
2784
+ type: 'Identifier',
2785
+ loc: DUMMY_LOC,
2786
+ name: 'React'
2787
+ },
2788
+ right: {
2789
+ type: 'Identifier',
2790
+ loc: DUMMY_LOC,
2791
+ name: 'JSX'
2792
+ }
2567
2793
  },
2568
2794
  right: {
2569
2795
  type: 'Identifier',
@@ -2738,8 +2964,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2738
2964
  return {
2739
2965
  type: 'TSInterfaceHeritage',
2740
2966
  loc: DUMMY_LOC,
2741
- // Bug: node.id can be qualified
2742
- expression: transform.Identifier(node.id, false),
2967
+ expression: node.id.type === 'QualifiedTypeIdentifier' ? transform.QualifiedTypeIdentifier(node.id) : transform.Identifier(node.id, false),
2743
2968
  typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
2744
2969
  };
2745
2970
  },
@@ -3000,8 +3225,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3000
3225
  type T = { ...T1, ...T2, ...T3, b: string };
3001
3226
  // becomes
3002
3227
  type T =
3003
- & Omit<T1, keyof (T2 | T3 | { b: string })>
3004
- & Omit<T2, keyof (T3 | { b: string })>
3228
+ & Omit<T1, keyof T2 | keyof T3 | keyof { b: string }>
3229
+ & Omit<T2, keyof T3 | keyof { b: string }>
3005
3230
  & Omit<T3, keyof { b: string }>
3006
3231
  & { b: string };
3007
3232
  ```
@@ -3024,7 +3249,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3024
3249
 
3025
3250
  const spreadType = transformTypeAnnotationType(property.argument);
3026
3251
 
3027
- if (spreadType.type !== 'TSTypeReference') {
3252
+ if (spreadType.type !== 'TSTypeReference' && spreadType.type !== 'TSTypeQuery') {
3028
3253
  return unsupportedAnnotation(property, 'object types with complex spreads');
3029
3254
  }
3030
3255
 
@@ -3065,14 +3290,19 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3065
3290
  type: 'TSTypeParameterInstantiation',
3066
3291
  loc: DUMMY_LOC,
3067
3292
  params: [currentType, {
3068
- type: 'TSTypeOperator',
3293
+ type: 'TSUnionType',
3069
3294
  loc: DUMMY_LOC,
3070
- operator: 'keyof',
3071
- typeAnnotation: {
3072
- type: 'TSUnionType',
3295
+ types: [...remainingTypes.map(t => ({
3296
+ type: 'TSTypeOperator',
3073
3297
  loc: DUMMY_LOC,
3074
- types: [...remainingTypes, objectType]
3075
- }
3298
+ operator: 'keyof',
3299
+ typeAnnotation: t
3300
+ })), {
3301
+ type: 'TSTypeOperator',
3302
+ loc: DUMMY_LOC,
3303
+ operator: 'keyof',
3304
+ typeAnnotation: objectType
3305
+ }]
3076
3306
  }]
3077
3307
  }
3078
3308
  });
@@ -3100,7 +3330,40 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3100
3330
  },
3101
3331
 
3102
3332
  ObjectTypeIndexer(node) {
3103
- var _node$variance;
3333
+ var _node$variance2;
3334
+
3335
+ if (node.key.type === 'GenericTypeAnnotation') {
3336
+ var _node$variance;
3337
+
3338
+ const ident = node.key.id.type === 'Identifier' ? node.key.id : node.key.id.id;
3339
+ return {
3340
+ type: 'TSPropertySignature',
3341
+ computed: true,
3342
+ loc: DUMMY_LOC,
3343
+ key: {
3344
+ type: 'BinaryExpression',
3345
+ operator: 'in',
3346
+ loc: DUMMY_LOC,
3347
+ left: {
3348
+ type: 'Identifier',
3349
+ name: node.id == null ? '$$Key$$' : node.id.name,
3350
+ loc: DUMMY_LOC
3351
+ },
3352
+ right: {
3353
+ type: 'Identifier',
3354
+ name: ident.name,
3355
+ loc: DUMMY_LOC
3356
+ }
3357
+ },
3358
+ readonly: ((_node$variance = node.variance) == null ? void 0 : _node$variance.kind) === 'plus',
3359
+ static: node.static,
3360
+ typeAnnotation: {
3361
+ type: 'TSTypeAnnotation',
3362
+ loc: DUMMY_LOC,
3363
+ typeAnnotation: transformTypeAnnotationType(node.value)
3364
+ }
3365
+ };
3366
+ }
3104
3367
 
3105
3368
  return {
3106
3369
  type: 'TSIndexSignature',
@@ -3115,7 +3378,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3115
3378
  typeAnnotation: transformTypeAnnotationType(node.key)
3116
3379
  }
3117
3380
  }],
3118
- readonly: ((_node$variance = node.variance) == null ? void 0 : _node$variance.kind) === 'plus',
3381
+ readonly: ((_node$variance2 = node.variance) == null ? void 0 : _node$variance2.kind) === 'plus',
3119
3382
  static: node.static,
3120
3383
  typeAnnotation: {
3121
3384
  type: 'TSTypeAnnotation',
@@ -3126,7 +3389,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3126
3389
  },
3127
3390
 
3128
3391
  ObjectTypeProperty(node) {
3129
- var _node$variance2;
3392
+ var _node$variance3;
3130
3393
 
3131
3394
  const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : transform.StringLiteral(node.key);
3132
3395
 
@@ -3174,7 +3437,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3174
3437
  computed: false,
3175
3438
  key,
3176
3439
  optional: node.optional,
3177
- readonly: ((_node$variance2 = node.variance) == null ? void 0 : _node$variance2.kind) === 'plus',
3440
+ readonly: ((_node$variance3 = node.variance) == null ? void 0 : _node$variance3.kind) === 'plus',
3178
3441
  static: node.static,
3179
3442
  typeAnnotation: {
3180
3443
  type: 'TSTypeAnnotation',