flow-api-translator 0.36.1 → 0.327.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.
Files changed (37) hide show
  1. package/dist/TSDefToFlowDef.js +66 -327
  2. package/dist/TSDefToFlowDef.js.flow +70 -35
  3. package/dist/flowDefToTSDef.js +134 -825
  4. package/dist/flowDefToTSDef.js.flow +196 -100
  5. package/dist/flowImportTo.js +5 -25
  6. package/dist/flowImportTo.js.flow +4 -4
  7. package/dist/flowToFlowDef.js +190 -379
  8. package/dist/flowToFlowDef.js.flow +109 -27
  9. package/dist/flowToJS.js +3 -19
  10. package/dist/flowToJS.js.flow +5 -5
  11. package/dist/index.js +12 -50
  12. package/dist/index.js.flow +1 -1
  13. package/dist/src/TSDefToFlowDef.js +66 -327
  14. package/dist/src/flowDefToTSDef.js +134 -825
  15. package/dist/src/flowImportTo.js +5 -25
  16. package/dist/src/flowToFlowDef.js +190 -379
  17. package/dist/src/flowToJS.js +3 -19
  18. package/dist/src/index.js +12 -50
  19. package/dist/src/utils/DocblockUtils.js +4 -14
  20. package/dist/src/utils/ErrorUtils.js +4 -40
  21. package/dist/src/utils/FlowAnalyze.js +8 -28
  22. package/dist/src/utils/TSNodeUtils.js +83 -0
  23. package/dist/src/utils/TranslationUtils.js +0 -13
  24. package/dist/src/utils/ts-estree-ast-types.js +0 -29
  25. package/dist/utils/DocblockUtils.js +4 -14
  26. package/dist/utils/DocblockUtils.js.flow +1 -1
  27. package/dist/utils/ErrorUtils.js +4 -40
  28. package/dist/utils/ErrorUtils.js.flow +6 -6
  29. package/dist/utils/FlowAnalyze.js +8 -28
  30. package/dist/utils/FlowAnalyze.js.flow +9 -4
  31. package/dist/utils/TSNodeUtils.js +83 -0
  32. package/dist/utils/TSNodeUtils.js.flow +108 -0
  33. package/dist/utils/TranslationUtils.js +0 -13
  34. package/dist/utils/TranslationUtils.js.flow +2 -2
  35. package/dist/utils/ts-estree-ast-types.js +0 -29
  36. package/dist/utils/ts-estree-ast-types.js.flow +697 -740
  37. package/package.json +10 -9
@@ -64,22 +64,22 @@ import type {
64
64
  TypeParameterDeclaration,
65
65
  TypeParameterInstantiation,
66
66
  VariableDeclaration,
67
- } from 'hermes-estree';
68
- import type {ScopeManager} from 'hermes-eslint';
69
- import type {DetachedNode} from 'hermes-transform';
67
+ } from 'flow-estree';
68
+ import type {ScopeManager} from 'flow-eslint';
69
+ import type {DetachedNode} from 'flow-transform';
70
70
  import type {
71
71
  Dep,
72
72
  TranslationContext,
73
73
  TranslationOptions,
74
74
  } from './utils/TranslationUtils';
75
75
 
76
- import {t} from 'hermes-transform';
76
+ import {t} from 'flow-transform';
77
77
  import {
78
78
  analyzeFunctionReturn,
79
79
  analyzeTypeDependencies,
80
80
  } from './utils/FlowAnalyze';
81
81
  import {createTranslationContext} from './utils/TranslationUtils';
82
- import {asDetachedNode} from 'hermes-transform';
82
+ import {asDetachedNode} from 'flow-transform';
83
83
  import {translationError, flowFixMeOrError} from './utils/ErrorUtils';
84
84
  import {
85
85
  isExpression,
@@ -87,16 +87,16 @@ import {
87
87
  isNumericLiteral,
88
88
  isIdentifier,
89
89
  isMemberExpressionWithNonComputedProperty,
90
- } from 'hermes-estree';
90
+ } from 'flow-estree';
91
91
 
92
92
  const EMPTY_TRANSLATION_RESULT: TranslatedResultOrNull<empty> = [null, []];
93
93
 
94
- type TranslatedDeps = $ReadOnlyArray<Dep>;
95
- type TranslatedResultOrNull<+T> = Readonly<
94
+ type TranslatedDeps = ReadonlyArray<Dep>;
95
+ type TranslatedResultOrNull<out T> = Readonly<
96
96
  [DetachedNode<T> | null, TranslatedDeps],
97
97
  >;
98
98
  type TranslatedResultArray<T> = [
99
- $ReadOnlyArray<DetachedNode<T>>,
99
+ ReadonlyArray<DetachedNode<T>>,
100
100
  TranslatedDeps,
101
101
  ];
102
102
  type TranslatedResult<T> = [DetachedNode<T>, TranslatedDeps];
@@ -104,7 +104,7 @@ type TranslatedResult<T> = [DetachedNode<T>, TranslatedDeps];
104
104
  type ProgramStatement = Statement | ModuleDeclaration;
105
105
 
106
106
  function convertArray<TIn, TOut>(
107
- items: $ReadOnlyArray<TIn>,
107
+ items: ReadonlyArray<TIn>,
108
108
  convert: TIn => TranslatedResultOrNull<TOut>,
109
109
  ): TranslatedResultArray<TOut> {
110
110
  const resultItems: Array<DetachedNode<TOut>> = [];
@@ -409,6 +409,10 @@ function convertStatement(
409
409
  return [result, deps];
410
410
  }
411
411
  case 'VariableDeclaration': {
412
+ const requireImport = convertRequireToImport(stmt);
413
+ if (requireImport != null) {
414
+ return requireImport;
415
+ }
412
416
  const [result, deps] = convertVariableDeclaration(stmt, context);
413
417
  return [result, deps];
414
418
  }
@@ -486,12 +490,12 @@ function convertExpressionToTypeAnnotation(
486
490
  }
487
491
  }
488
492
 
489
- function inheritComments<T: DetachedNode<ESNode>>(
493
+ function inheritComments<T extends DetachedNode<ESNode>>(
490
494
  fromNode: ESNode,
491
495
  toNode: T,
492
496
  ): T {
493
497
  // $FlowFixMe[unclear-type]
494
- (toNode: any).comments = (fromNode: any).comments;
498
+ (toNode as any).comments = (fromNode as any).comments;
495
499
  return toNode;
496
500
  }
497
501
 
@@ -663,7 +667,7 @@ function convertLiteral(
663
667
  function convertExportDeclaration(
664
668
  decl:
665
669
  | ExportDefaultDeclaration['declaration']
666
- | $NonMaybeType<ExportNamedDeclaration['declaration']>,
670
+ | NonNullable<ExportNamedDeclaration['declaration']>,
667
671
  opts: {default: boolean},
668
672
  context: TranslationContext,
669
673
  ): TranslatedResult<ProgramStatement> {
@@ -831,8 +835,8 @@ function convertExportDefaultDeclaration(
831
835
  context: TranslationContext,
832
836
  ): TranslatedResult<ProgramStatement> {
833
837
  const expr = stmt.declaration;
834
- if (isExpression(expr) && (expr: $FlowFixMe).type === 'Identifier') {
835
- const name = ((expr: $FlowFixMe): Identifier).name;
838
+ if (isExpression(expr) && (expr as $FlowFixMe).type === 'Identifier') {
839
+ const name = (expr as $FlowFixMe as Identifier).name;
836
840
  const [declDecl, deps] = [
837
841
  t.TypeofTypeAnnotation({argument: t.Identifier({name})}),
838
842
  analyzeTypeDependencies(expr, context),
@@ -926,19 +930,97 @@ function convertVariableDeclaration(
926
930
 
927
931
  return [
928
932
  t.DeclareVariable({
929
- id: t.Identifier({
930
- name: id.name,
931
- typeAnnotation: t.TypeAnnotation({
932
- typeAnnotation: resultTypeAnnotation,
933
+ declarations: [
934
+ t.VariableDeclarator({
935
+ id: t.Identifier({
936
+ name: id.name,
937
+ typeAnnotation: t.TypeAnnotation({
938
+ typeAnnotation: resultTypeAnnotation,
939
+ }),
940
+ optional: false,
941
+ }),
942
+ init: null,
933
943
  }),
934
- optional: false,
935
- }),
944
+ ],
936
945
  kind: stmt.kind,
946
+ implicitDeclare: false,
937
947
  }),
938
948
  annotDeps,
939
949
  ];
940
950
  }
941
951
 
952
+ function convertRequireToImport(
953
+ stmt: VariableDeclaration,
954
+ ): ?TranslatedResult<ProgramStatement> {
955
+ if (stmt.declarations.length !== 1) {
956
+ return null;
957
+ }
958
+ const decl = stmt.declarations[0];
959
+ const init = decl.init;
960
+ if (
961
+ init == null ||
962
+ init.type !== 'CallExpression' ||
963
+ init.callee.type !== 'Identifier' ||
964
+ init.callee.name !== 'require' ||
965
+ init.arguments.length !== 1
966
+ ) {
967
+ return null;
968
+ }
969
+ const sourceArg = init.arguments[0];
970
+ if (!isStringLiteral(sourceArg)) {
971
+ return null;
972
+ }
973
+ const id = decl.id;
974
+
975
+ if (id.type === 'Identifier') {
976
+ return [
977
+ t.ImportDeclaration({
978
+ importKind: 'value',
979
+ source: asDetachedNode(sourceArg),
980
+ specifiers: [
981
+ t.ImportDefaultSpecifier({
982
+ local: t.Identifier({name: id.name}),
983
+ }),
984
+ ],
985
+ attributes: [],
986
+ }),
987
+ [],
988
+ ];
989
+ }
990
+
991
+ if (id.type === 'ObjectPattern') {
992
+ const specifiers = [];
993
+ for (const prop of id.properties) {
994
+ if (prop.type === 'RestElement') {
995
+ return null;
996
+ }
997
+ const key = prop.key;
998
+ const value = prop.value;
999
+ if (key.type !== 'Identifier' || value.type !== 'Identifier') {
1000
+ return null;
1001
+ }
1002
+ specifiers.push(
1003
+ t.ImportSpecifier({
1004
+ imported: t.Identifier({name: key.name}),
1005
+ local: t.Identifier({name: value.name}),
1006
+ importKind: null,
1007
+ }),
1008
+ );
1009
+ }
1010
+ return [
1011
+ t.ImportDeclaration({
1012
+ importKind: 'value',
1013
+ source: asDetachedNode(sourceArg),
1014
+ specifiers,
1015
+ attributes: [],
1016
+ }),
1017
+ [],
1018
+ ];
1019
+ }
1020
+
1021
+ return null;
1022
+ }
1023
+
942
1024
  function convertImportDeclaration(
943
1025
  stmt: ImportDeclaration,
944
1026
  context: TranslationContext,
@@ -1371,13 +1453,13 @@ function convertComponentDeclaration(
1371
1453
  }
1372
1454
 
1373
1455
  type TranslatedComponentParametersResults = [
1374
- $ReadOnlyArray<DetachedNode<ComponentTypeParameter>>,
1456
+ ReadonlyArray<DetachedNode<ComponentTypeParameter>>,
1375
1457
  ?DetachedNode<ComponentTypeParameter>,
1376
1458
  TranslatedDeps,
1377
1459
  ];
1378
1460
 
1379
1461
  function hasNonIdentifierStringLiteralParam(
1380
- params: $ReadOnlyArray<ComponentParameter | RestElement>,
1462
+ params: ReadonlyArray<ComponentParameter | RestElement>,
1381
1463
  ): boolean {
1382
1464
  return params.some(
1383
1465
  param =>
@@ -1408,7 +1490,7 @@ function extractParamTypeInfo(
1408
1490
  }
1409
1491
 
1410
1492
  function convertComponentParameters(
1411
- params: $ReadOnlyArray<ComponentParameter | RestElement>,
1493
+ params: ReadonlyArray<ComponentParameter | RestElement>,
1412
1494
  context: TranslationContext,
1413
1495
  ): TranslatedComponentParametersResults {
1414
1496
  if (hasNonIdentifierStringLiteralParam(params)) {
@@ -1490,7 +1572,7 @@ function convertComponentParameters(
1490
1572
  }
1491
1573
 
1492
1574
  function convertComponentParametersToPropsObject(
1493
- params: $ReadOnlyArray<ComponentParameter | RestElement>,
1575
+ params: ReadonlyArray<ComponentParameter | RestElement>,
1494
1576
  context: TranslationContext,
1495
1577
  ): TranslatedComponentParametersResults {
1496
1578
  const properties: Array<
@@ -1673,12 +1755,12 @@ function convertAFunction(
1673
1755
  }
1674
1756
 
1675
1757
  type TranslatedFunctionParametersResults = [
1676
- $ReadOnlyArray<DetachedNode<FunctionTypeParam>>,
1758
+ ReadonlyArray<DetachedNode<FunctionTypeParam>>,
1677
1759
  ?DetachedNode<FunctionTypeParam>,
1678
1760
  TranslatedDeps,
1679
1761
  ];
1680
1762
  function convertFunctionParameters(
1681
- params: $ReadOnlyArray<FunctionParameter>,
1763
+ params: ReadonlyArray<FunctionParameter>,
1682
1764
  context: TranslationContext,
1683
1765
  ): TranslatedFunctionParametersResults {
1684
1766
  return params.reduce<TranslatedFunctionParametersResults>(
package/dist/flowToJS.js CHANGED
@@ -1,39 +1,23 @@
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", {
13
4
  value: true
14
5
  });
15
6
  exports.flowToJS = flowToJS;
16
-
17
- var _hermesParser = require("hermes-parser");
18
-
7
+ var _flowParser = require("flow-parser");
19
8
  var _DocblockUtils = require("./utils/DocblockUtils");
20
-
21
9
  const {
22
10
  nodeWith
23
- } = _hermesParser.astNodeMutationHelpers;
24
-
11
+ } = _flowParser.astNodeMutationHelpers;
25
12
  function stripAtFlow(ast, _options) {
26
13
  const docblock = ast.docblock;
27
-
28
14
  if (docblock == null) {
29
15
  return ast;
30
16
  }
31
-
32
17
  return nodeWith(ast, {
33
18
  docblock: (0, _DocblockUtils.removeAtFlowFromDocblock)(docblock)
34
19
  });
35
20
  }
36
-
37
21
  function flowToJS(sourceAST, _code, _scopeManager) {
38
- return [_hermesParser.Transforms.transformComponentSyntax, _hermesParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
22
+ return [_flowParser.Transforms.transformComponentSyntax, _flowParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
39
23
  }
@@ -10,12 +10,12 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {ScopeManager} from 'hermes-eslint';
14
- import type {Program} from 'hermes-estree';
15
- import type {ParserOptions} from 'hermes-parser';
13
+ import type {ScopeManager} from 'flow-eslint';
14
+ import type {Program} from 'flow-estree';
15
+ import type {ParserOptions} from 'flow-parser';
16
16
 
17
- import {Transforms} from 'hermes-parser';
18
- import {astNodeMutationHelpers} from 'hermes-parser';
17
+ import {Transforms} from 'flow-parser';
18
+ import {astNodeMutationHelpers} from 'flow-parser';
19
19
  import {removeAtFlowFromDocblock} from './utils/DocblockUtils';
20
20
 
21
21
  const {nodeWith} = astNodeMutationHelpers;
package/dist/index.js CHANGED
@@ -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", {
@@ -18,98 +9,69 @@ exports.translateFlowToFlowDef = translateFlowToFlowDef;
18
9
  exports.translateFlowToJS = translateFlowToJS;
19
10
  exports.translateFlowToTSDef = translateFlowToTSDef;
20
11
  exports.unstable_translateTSDefToFlowDef = unstable_translateTSDefToFlowDef;
21
-
22
- var _hermesTransform = require("hermes-transform");
23
-
12
+ var _flowTransform = require("flow-transform");
24
13
  var _parser = require("@typescript-eslint/parser");
25
-
26
14
  var _visitorKeys = require("@typescript-eslint/visitor-keys");
27
-
28
15
  var _flowToFlowDef = _interopRequireDefault(require("./flowToFlowDef"));
29
-
30
16
  var _flowDefToTSDef = require("./flowDefToTSDef");
31
-
32
17
  var _flowToJS = require("./flowToJS");
33
-
34
18
  var _flowImportTo = require("./flowImportTo");
35
-
36
19
  var _TSDefToFlowDef = require("./TSDefToFlowDef");
37
-
38
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
-
20
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
40
21
  async function translateFlowToFlowDef(code, prettierOptions = {}, opts) {
41
22
  const {
42
23
  ast,
43
24
  scopeManager
44
- } = await (0, _hermesTransform.parse)(code);
25
+ } = await (0, _flowTransform.parse)(code);
45
26
  const [flowDefAst, mutatedCode] = (0, _flowToFlowDef.default)(ast, code, scopeManager, {
46
27
  recoverFromErrors: true,
47
28
  mungeUnderscores: opts == null ? void 0 : opts.mungeUnderscores
48
29
  });
49
- return (0, _hermesTransform.print)(flowDefAst, mutatedCode, prettierOptions);
30
+ return (0, _flowTransform.print)(flowDefAst, mutatedCode, prettierOptions);
50
31
  }
51
-
52
32
  async function translateFlowToTSDef(code, prettierOptions = {}) {
53
33
  const flowDefCode = await translateFlowToFlowDef(code, prettierOptions);
54
34
  return translateFlowDefToTSDef(flowDefCode, prettierOptions);
55
35
  }
56
-
57
36
  async function translateFlowDefToTSDef(code, prettierOptions = {}) {
58
37
  const {
59
38
  ast,
60
39
  scopeManager
61
- } = await (0, _hermesTransform.parse)(code);
40
+ } = await (0, _flowTransform.parse)(code);
62
41
  const [tsAST, mutatedCode] = (0, _flowDefToTSDef.flowDefToTSDef)(code, ast, scopeManager, {
63
42
  recoverFromErrors: true
64
43
  });
65
- return (0, _hermesTransform.print)( // $FlowExpectedError[incompatible-type] - this is fine as we're providing the visitor keys
66
- tsAST, mutatedCode, { ...prettierOptions
44
+ return (0, _flowTransform.print)(tsAST, mutatedCode, {
45
+ ...prettierOptions
67
46
  }, _visitorKeys.visitorKeys);
68
47
  }
69
-
70
48
  async function translateFlowToJS(code, prettierOptions = {}) {
71
49
  const {
72
50
  ast,
73
51
  scopeManager
74
- } = await (0, _hermesTransform.parse)(code);
52
+ } = await (0, _flowTransform.parse)(code);
75
53
  const jsAST = (0, _flowToJS.flowToJS)(ast, code, scopeManager);
76
- return (0, _hermesTransform.print)(jsAST, code, prettierOptions);
54
+ return (0, _flowTransform.print)(jsAST, code, prettierOptions);
77
55
  }
78
- /**
79
- * This translator is very experimental and unstable.
80
- *
81
- * It is not written with productionizing it in mind, but instead used to evaluate how close Flow
82
- * is to TypeScript.
83
- *
84
- * If you are going to use it anyways, you agree that you are calling a potentially broken function
85
- * without any guarantee.
86
- *
87
- * @deprecated
88
- */
89
-
90
-
91
56
  async function unstable_translateTSDefToFlowDef(code, prettierOptions = {}) {
92
57
  const ast = (0, _parser.parse)(code, {
93
58
  loc: true,
94
59
  range: true,
95
60
  sourceType: 'module'
96
61
  });
97
-
98
62
  if (ast == null) {
99
63
  throw `Failed to parse ${code} with @typescript-eslint/parser`;
100
64
  }
101
-
102
65
  const [flowAST, mutatedCode] = (0, _TSDefToFlowDef.TSDefToFlowDef)(code, ast, {
103
66
  recoverFromErrors: false
104
67
  });
105
- return (0, _hermesTransform.print)(flowAST, mutatedCode, prettierOptions);
68
+ return (0, _flowTransform.print)(flowAST, mutatedCode, prettierOptions);
106
69
  }
107
-
108
70
  async function translateFlowImportsTo(code, prettierOptions = {}, opts) {
109
71
  const {
110
72
  ast,
111
73
  scopeManager
112
- } = await (0, _hermesTransform.parse)(code);
74
+ } = await (0, _flowTransform.parse)(code);
113
75
  const jsAST = (0, _flowImportTo.flowImportTo)(ast, code, scopeManager, opts);
114
- return (0, _hermesTransform.print)(jsAST, code, prettierOptions);
76
+ return (0, _flowTransform.print)(jsAST, code, prettierOptions);
115
77
  }
@@ -12,7 +12,7 @@
12
12
 
13
13
  import type {MapperOptions} from './flowImportTo';
14
14
 
15
- import {parse, print} from 'hermes-transform';
15
+ import {parse, print} from 'flow-transform';
16
16
  import {parse as parseTS} from '@typescript-eslint/parser';
17
17
  import {visitorKeys as tsVisitorKeys} from '@typescript-eslint/visitor-keys';
18
18
  import flowToFlowDef from './flowToFlowDef';