flow-api-translator 0.18.2 → 0.19.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.
@@ -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);
@@ -513,6 +519,16 @@ function convertExportDeclaration(decl, opts, context) {
513
519
  }), deps];
514
520
  }
515
521
 
522
+ case 'HookDeclaration':
523
+ {
524
+ const [declDecl, deps] = convertHookDeclaration(decl, context);
525
+ return [opts.default ? _hermesTransform.t.DeclareExportDefaultDeclaration({
526
+ declaration: declDecl
527
+ }) : _hermesTransform.t.DeclareExportDeclarationNamedWithDeclaration({
528
+ declaration: declDecl
529
+ }), deps];
530
+ }
531
+
516
532
  case 'FunctionDeclaration':
517
533
  {
518
534
  const [declDecl, deps] = convertFunctionDeclaration(decl, context);
@@ -906,6 +922,32 @@ function convertComponentParameters(params, context) {
906
922
  }, [[], null, []]);
907
923
  }
908
924
 
925
+ function convertHookDeclaration(hook, context) {
926
+ var _hook$returnType;
927
+
928
+ const id = hook.id;
929
+ const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType : // $FlowFixMe[incompatible-type]
930
+ _hermesTransform.t.TypeAnnotation({
931
+ typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
932
+ });
933
+ const [resultReturnType, returnDeps] = convertTypeAnnotation(returnType, hook, context);
934
+ const [resultParams, restParam, paramsDeps] = convertFunctionParameters(hook.params, context);
935
+ const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(hook.typeParameters, context);
936
+
937
+ const resultFunc = _hermesTransform.t.FunctionTypeAnnotation({
938
+ params: resultParams,
939
+ returnType: resultReturnType,
940
+ rest: restParam,
941
+ typeParameters: resultTypeParams
942
+ });
943
+
944
+ const funcDeps = [...paramsDeps, ...returnDeps, ...typeParamsDeps];
945
+ return [_hermesTransform.t.DeclareHook({
946
+ name: id.name,
947
+ functionType: resultFunc
948
+ }), [...funcDeps]];
949
+ }
950
+
909
951
  function convertFunctionDeclaration(func, context) {
910
952
  const id = func.id;
911
953
 
@@ -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];
@@ -642,6 +648,19 @@ function convertExportDeclaration(
642
648
  deps,
643
649
  ];
644
650
  }
651
+ case 'HookDeclaration': {
652
+ const [declDecl, deps] = convertHookDeclaration(decl, context);
653
+ return [
654
+ opts.default
655
+ ? t.DeclareExportDefaultDeclaration({
656
+ declaration: declDecl,
657
+ })
658
+ : t.DeclareExportDeclarationNamedWithDeclaration({
659
+ declaration: declDecl,
660
+ }),
661
+ deps,
662
+ ];
663
+ }
645
664
  case 'FunctionDeclaration': {
646
665
  const [declDecl, deps] = convertFunctionDeclaration(decl, context);
647
666
  return [
@@ -1224,6 +1243,48 @@ function convertComponentParameters(
1224
1243
  );
1225
1244
  }
1226
1245
 
1246
+ function convertHookDeclaration(
1247
+ hook: HookDeclaration,
1248
+ context: TranslationContext,
1249
+ ): TranslatedResult<DeclareHook> {
1250
+ const id = hook.id;
1251
+ const returnType: TypeAnnotation =
1252
+ hook.returnType ??
1253
+ // $FlowFixMe[incompatible-type]
1254
+ t.TypeAnnotation({typeAnnotation: t.VoidTypeAnnotation()});
1255
+
1256
+ const [resultReturnType, returnDeps] = convertTypeAnnotation(
1257
+ returnType,
1258
+ hook,
1259
+ context,
1260
+ );
1261
+
1262
+ const [resultParams, restParam, paramsDeps] = convertFunctionParameters(
1263
+ hook.params,
1264
+ context,
1265
+ );
1266
+
1267
+ const [resultTypeParams, typeParamsDeps] =
1268
+ convertTypeParameterDeclarationOrNull(hook.typeParameters, context);
1269
+
1270
+ const resultFunc = t.FunctionTypeAnnotation({
1271
+ params: resultParams,
1272
+ returnType: resultReturnType,
1273
+ rest: restParam,
1274
+ typeParameters: resultTypeParams,
1275
+ });
1276
+
1277
+ const funcDeps = [...paramsDeps, ...returnDeps, ...typeParamsDeps];
1278
+
1279
+ return [
1280
+ t.DeclareHook({
1281
+ name: id.name,
1282
+ functionType: resultFunc,
1283
+ }),
1284
+ [...funcDeps],
1285
+ ];
1286
+ }
1287
+
1227
1288
  function convertFunctionDeclaration(
1228
1289
  func: FunctionDeclaration,
1229
1290
  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.0",
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.0",
16
+ "hermes-estree": "0.19.0",
17
+ "hermes-parser": "0.19.0",
18
+ "hermes-transform": "0.19.0"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "prettier": "^3.0.0 || ^2.7.1"