flow-api-translator 0.18.2 → 0.19.1

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.
@@ -944,6 +944,21 @@ const getTransforms = (originalCode, scopeManager, opts) => {
944
944
  exportKind: 'value'
945
945
  }];
946
946
  }
947
+ // TS doesn't support direct default export for declare'd functions
948
+
949
+ case 'DeclareHook':
950
+ {
951
+ const functionDecl = transform.DeclareHook(declaration);
952
+ const name = declaration.id.name;
953
+ return [functionDecl, {
954
+ type: 'ExportDefaultDeclaration',
955
+ declaration: {
956
+ type: 'Identifier',
957
+ name
958
+ },
959
+ exportKind: 'value'
960
+ }];
961
+ }
947
962
  // Flow's declare export default Identifier is ambiguous.
948
963
  // the Identifier might reference a type, or it might reference a value
949
964
  // - If it's a value, then that's all good, TS supports that.
@@ -1077,6 +1092,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1077
1092
  exportKind: 'value'
1078
1093
  }];
1079
1094
 
1095
+ case 'DeclareHook':
1096
+ return [{
1097
+ declaration: transform.DeclareHook(node.declaration),
1098
+ exportKind: 'value'
1099
+ }];
1100
+
1080
1101
  case 'DeclareFunction':
1081
1102
  return [{
1082
1103
  declaration: transform.DeclareFunction(node.declaration),
@@ -1252,6 +1273,27 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1252
1273
  }];
1253
1274
  },
1254
1275
 
1276
+ DeclareHook(node) {
1277
+ // the hook params/returnType are stored as an annotation on the ID...
1278
+ const id = transform.Identifier(node.id, false);
1279
+ const functionInfo = transform.FunctionTypeAnnotation(node.id.typeAnnotation.typeAnnotation);
1280
+ return {
1281
+ type: 'TSDeclareFunction',
1282
+ async: false,
1283
+ body: undefined,
1284
+ declare: true,
1285
+ expression: false,
1286
+ generator: false,
1287
+ id: {
1288
+ type: 'Identifier',
1289
+ name: id.name
1290
+ },
1291
+ params: functionInfo.params,
1292
+ returnType: functionInfo.returnType,
1293
+ typeParameters: functionInfo.typeParameters
1294
+ };
1295
+ },
1296
+
1255
1297
  DeclareFunction(node) {
1256
1298
  // the function information is stored as an annotation on the ID...
1257
1299
  const id = transform.Identifier(node.id, false);
@@ -1391,6 +1433,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1391
1433
  switch (node.declaration.type) {
1392
1434
  case 'ClassDeclaration':
1393
1435
  case 'ComponentDeclaration':
1436
+ case 'HookDeclaration':
1394
1437
  case 'FunctionDeclaration':
1395
1438
  case 'VariableDeclaration':
1396
1439
  // These cases shouldn't happen in flow defs because they have their own special
@@ -1451,7 +1494,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1451
1494
  FunctionTypeAnnotation(node) {
1452
1495
  const params = node.params.map(transform.FunctionTypeParam);
1453
1496
 
1454
- if (node.this != null) {
1497
+ if (node.type === 'FunctionTypeAnnotation' && node.this != null) {
1455
1498
  params.unshift({
1456
1499
  type: 'Identifier',
1457
1500
  name: 'this',
@@ -1759,7 +1802,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1759
1802
  },
1760
1803
  typeParameters: {
1761
1804
  type: 'TSTypeParameterInstantiation',
1762
- params: assertHasExactlyNTypeParameters(1)
1805
+ params: assertHasExactlyNTypeParameters(2)
1763
1806
  }
1764
1807
  };
1765
1808
  }
@@ -1034,6 +1034,23 @@ const getTransforms = (
1034
1034
  ];
1035
1035
  }
1036
1036
 
1037
+ // TS doesn't support direct default export for declare'd functions
1038
+ case 'DeclareHook': {
1039
+ const functionDecl = transform.DeclareHook(declaration);
1040
+ const name = declaration.id.name;
1041
+ return [
1042
+ functionDecl,
1043
+ {
1044
+ type: 'ExportDefaultDeclaration',
1045
+ declaration: {
1046
+ type: 'Identifier',
1047
+ name,
1048
+ },
1049
+ exportKind: 'value',
1050
+ },
1051
+ ];
1052
+ }
1053
+
1037
1054
  // Flow's declare export default Identifier is ambiguous.
1038
1055
  // the Identifier might reference a type, or it might reference a value
1039
1056
  // - If it's a value, then that's all good, TS supports that.
@@ -1177,6 +1194,13 @@ const getTransforms = (
1177
1194
  exportKind: 'value',
1178
1195
  },
1179
1196
  ];
1197
+ case 'DeclareHook':
1198
+ return [
1199
+ {
1200
+ declaration: transform.DeclareHook(node.declaration),
1201
+ exportKind: 'value',
1202
+ },
1203
+ ];
1180
1204
  case 'DeclareFunction':
1181
1205
  return [
1182
1206
  {
@@ -1377,6 +1401,29 @@ const getTransforms = (
1377
1401
  },
1378
1402
  ];
1379
1403
  },
1404
+ DeclareHook(node: FlowESTree.DeclareHook): TSESTree.TSDeclareFunction {
1405
+ // the hook params/returnType are stored as an annotation on the ID...
1406
+ const id = transform.Identifier(node.id, false);
1407
+ const functionInfo = transform.FunctionTypeAnnotation(
1408
+ node.id.typeAnnotation.typeAnnotation,
1409
+ );
1410
+
1411
+ return {
1412
+ type: 'TSDeclareFunction',
1413
+ async: false,
1414
+ body: undefined,
1415
+ declare: true,
1416
+ expression: false,
1417
+ generator: false,
1418
+ id: {
1419
+ type: 'Identifier',
1420
+ name: id.name,
1421
+ },
1422
+ params: functionInfo.params,
1423
+ returnType: functionInfo.returnType,
1424
+ typeParameters: functionInfo.typeParameters,
1425
+ };
1426
+ },
1380
1427
  DeclareFunction(
1381
1428
  node: FlowESTree.DeclareFunction,
1382
1429
  ): TSESTree.TSDeclareFunction {
@@ -1557,6 +1604,7 @@ const getTransforms = (
1557
1604
  switch (node.declaration.type) {
1558
1605
  case 'ClassDeclaration':
1559
1606
  case 'ComponentDeclaration':
1607
+ case 'HookDeclaration':
1560
1608
  case 'FunctionDeclaration':
1561
1609
  case 'VariableDeclaration':
1562
1610
  // These cases shouldn't happen in flow defs because they have their own special
@@ -1617,10 +1665,10 @@ const getTransforms = (
1617
1665
  };
1618
1666
  },
1619
1667
  FunctionTypeAnnotation(
1620
- node: FlowESTree.FunctionTypeAnnotation,
1668
+ node: FlowESTree.FunctionTypeAnnotation | FlowESTree.HookTypeAnnotation,
1621
1669
  ): TSESTree.TSFunctionType {
1622
1670
  const params = node.params.map(transform.FunctionTypeParam);
1623
- if (node.this != null) {
1671
+ if (node.type === 'FunctionTypeAnnotation' && node.this != null) {
1624
1672
  params.unshift({
1625
1673
  type: 'Identifier',
1626
1674
  name: 'this',
@@ -1949,7 +1997,7 @@ const getTransforms = (
1949
1997
  },
1950
1998
  typeParameters: {
1951
1999
  type: 'TSTypeParameterInstantiation',
1952
- params: assertHasExactlyNTypeParameters(1),
2000
+ params: assertHasExactlyNTypeParameters(2),
1953
2001
  },
1954
2002
  };
1955
2003
  }
@@ -271,6 +271,12 @@ function convertStatement(stmt, context) {
271
271
  return [result, deps];
272
272
  }
273
273
 
274
+ case 'HookDeclaration':
275
+ {
276
+ const [result, deps] = convertHookDeclaration(stmt, context);
277
+ return [result, deps];
278
+ }
279
+
274
280
  case 'FunctionDeclaration':
275
281
  {
276
282
  const [result, deps] = convertFunctionDeclaration(stmt, context);
@@ -317,6 +323,7 @@ function convertStatement(stmt, context) {
317
323
  case 'DeclareVariable':
318
324
  case 'DeclareFunction':
319
325
  case 'DeclareModule':
326
+ case 'DeclareNamespace':
320
327
  case 'DeclareInterface':
321
328
  case 'DeclareTypeAlias':
322
329
  case 'DeclareOpaqueType':
@@ -513,6 +520,16 @@ function convertExportDeclaration(decl, opts, context) {
513
520
  }), deps];
514
521
  }
515
522
 
523
+ case 'HookDeclaration':
524
+ {
525
+ const [declDecl, deps] = convertHookDeclaration(decl, context);
526
+ return [opts.default ? _hermesTransform.t.DeclareExportDefaultDeclaration({
527
+ declaration: declDecl
528
+ }) : _hermesTransform.t.DeclareExportDeclarationNamedWithDeclaration({
529
+ declaration: declDecl
530
+ }), deps];
531
+ }
532
+
516
533
  case 'FunctionDeclaration':
517
534
  {
518
535
  const [declDecl, deps] = convertFunctionDeclaration(decl, context);
@@ -906,6 +923,32 @@ function convertComponentParameters(params, context) {
906
923
  }, [[], null, []]);
907
924
  }
908
925
 
926
+ function convertHookDeclaration(hook, context) {
927
+ var _hook$returnType;
928
+
929
+ const id = hook.id;
930
+ const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType : // $FlowFixMe[incompatible-type]
931
+ _hermesTransform.t.TypeAnnotation({
932
+ typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
933
+ });
934
+ const [resultReturnType, returnDeps] = convertTypeAnnotation(returnType, hook, context);
935
+ const [resultParams, restParam, paramsDeps] = convertFunctionParameters(hook.params, context);
936
+ const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(hook.typeParameters, context);
937
+
938
+ const resultFunc = _hermesTransform.t.FunctionTypeAnnotation({
939
+ params: resultParams,
940
+ returnType: resultReturnType,
941
+ rest: restParam,
942
+ typeParameters: resultTypeParams
943
+ });
944
+
945
+ const funcDeps = [...paramsDeps, ...returnDeps, ...typeParamsDeps];
946
+ return [_hermesTransform.t.DeclareHook({
947
+ name: id.name,
948
+ functionType: resultFunc
949
+ }), [...funcDeps]];
950
+ }
951
+
909
952
  function convertFunctionDeclaration(func, context) {
910
953
  const id = func.id;
911
954
 
@@ -24,6 +24,7 @@ import type {
24
24
  ComponentTypeParameter,
25
25
  DeclareClass,
26
26
  DeclareComponent,
27
+ DeclareHook,
27
28
  DeclareFunction,
28
29
  DeclareOpaqueType,
29
30
  DeclareVariable,
@@ -36,6 +37,7 @@ import type {
36
37
  FunctionParameter,
37
38
  FunctionTypeAnnotation,
38
39
  FunctionTypeParam,
40
+ HookDeclaration,
39
41
  Identifier,
40
42
  ImportDeclaration,
41
43
  InterfaceDeclaration,
@@ -372,6 +374,10 @@ function convertStatement(
372
374
  const [result, deps] = convertComponentDeclaration(stmt, context);
373
375
  return [result, deps];
374
376
  }
377
+ case 'HookDeclaration': {
378
+ const [result, deps] = convertHookDeclaration(stmt, context);
379
+ return [result, deps];
380
+ }
375
381
  case 'FunctionDeclaration': {
376
382
  const [result, deps] = convertFunctionDeclaration(stmt, context);
377
383
  return [result, deps];
@@ -404,6 +410,7 @@ function convertStatement(
404
410
  case 'DeclareVariable':
405
411
  case 'DeclareFunction':
406
412
  case 'DeclareModule':
413
+ case 'DeclareNamespace':
407
414
  case 'DeclareInterface':
408
415
  case 'DeclareTypeAlias':
409
416
  case 'DeclareOpaqueType':
@@ -642,6 +649,19 @@ function convertExportDeclaration(
642
649
  deps,
643
650
  ];
644
651
  }
652
+ case 'HookDeclaration': {
653
+ const [declDecl, deps] = convertHookDeclaration(decl, context);
654
+ return [
655
+ opts.default
656
+ ? t.DeclareExportDefaultDeclaration({
657
+ declaration: declDecl,
658
+ })
659
+ : t.DeclareExportDeclarationNamedWithDeclaration({
660
+ declaration: declDecl,
661
+ }),
662
+ deps,
663
+ ];
664
+ }
645
665
  case 'FunctionDeclaration': {
646
666
  const [declDecl, deps] = convertFunctionDeclaration(decl, context);
647
667
  return [
@@ -1224,6 +1244,48 @@ function convertComponentParameters(
1224
1244
  );
1225
1245
  }
1226
1246
 
1247
+ function convertHookDeclaration(
1248
+ hook: HookDeclaration,
1249
+ context: TranslationContext,
1250
+ ): TranslatedResult<DeclareHook> {
1251
+ const id = hook.id;
1252
+ const returnType: TypeAnnotation =
1253
+ hook.returnType ??
1254
+ // $FlowFixMe[incompatible-type]
1255
+ t.TypeAnnotation({typeAnnotation: t.VoidTypeAnnotation()});
1256
+
1257
+ const [resultReturnType, returnDeps] = convertTypeAnnotation(
1258
+ returnType,
1259
+ hook,
1260
+ context,
1261
+ );
1262
+
1263
+ const [resultParams, restParam, paramsDeps] = convertFunctionParameters(
1264
+ hook.params,
1265
+ context,
1266
+ );
1267
+
1268
+ const [resultTypeParams, typeParamsDeps] =
1269
+ convertTypeParameterDeclarationOrNull(hook.typeParameters, context);
1270
+
1271
+ const resultFunc = t.FunctionTypeAnnotation({
1272
+ params: resultParams,
1273
+ returnType: resultReturnType,
1274
+ rest: restParam,
1275
+ typeParameters: resultTypeParams,
1276
+ });
1277
+
1278
+ const funcDeps = [...paramsDeps, ...returnDeps, ...typeParamsDeps];
1279
+
1280
+ return [
1281
+ t.DeclareHook({
1282
+ name: id.name,
1283
+ functionType: resultFunc,
1284
+ }),
1285
+ [...funcDeps],
1286
+ ];
1287
+ }
1288
+
1227
1289
  function convertFunctionDeclaration(
1228
1290
  func: FunctionDeclaration,
1229
1291
  context: TranslationContext,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-api-translator",
3
- "version": "0.18.2",
3
+ "version": "0.19.1",
4
4
  "description": "Toolkit for creating Flow and TypeScript compatible libraries from Flow source code.",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -12,10 +12,10 @@
12
12
  "@babel/code-frame": "^7.16.0",
13
13
  "@typescript-eslint/visitor-keys": "^5.42.0",
14
14
  "flow-enums-runtime": "^0.0.6",
15
- "hermes-eslint": "0.18.2",
16
- "hermes-estree": "0.18.2",
17
- "hermes-parser": "0.18.2",
18
- "hermes-transform": "0.18.2"
15
+ "hermes-eslint": "0.19.1",
16
+ "hermes-estree": "0.19.1",
17
+ "hermes-parser": "0.19.1",
18
+ "hermes-transform": "0.19.1"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "prettier": "^3.0.0 || ^2.7.1"