flow-api-translator 0.20.1 → 0.21.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.
package/dist/index.js CHANGED
@@ -17,9 +17,12 @@ exports.translateFlowImportsTo = translateFlowImportsTo;
17
17
  exports.translateFlowToFlowDef = translateFlowToFlowDef;
18
18
  exports.translateFlowToJS = translateFlowToJS;
19
19
  exports.translateFlowToTSDef = translateFlowToTSDef;
20
+ exports.unstable_translateTSDefToFlowDef = unstable_translateTSDefToFlowDef;
20
21
 
21
22
  var _hermesTransform = require("hermes-transform");
22
23
 
24
+ var _parser = require("@typescript-eslint/parser");
25
+
23
26
  var _visitorKeys = require("@typescript-eslint/visitor-keys");
24
27
 
25
28
  var _flowToFlowDef = _interopRequireDefault(require("./flowToFlowDef"));
@@ -30,6 +33,8 @@ var _flowToJS = require("./flowToJS");
30
33
 
31
34
  var _flowImportTo = require("./flowImportTo");
32
35
 
36
+ var _TSDefToFlowDef = require("./TSDefToFlowDef");
37
+
33
38
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
39
 
35
40
  async function translateFlowToFlowDef(code, prettierOptions = {}) {
@@ -69,6 +74,35 @@ async function translateFlowToJS(code, prettierOptions = {}) {
69
74
  const jsAST = (0, _flowToJS.flowToJS)(ast, code, scopeManager);
70
75
  return (0, _hermesTransform.print)(jsAST, code, prettierOptions);
71
76
  }
77
+ /**
78
+ * This translator is very experimental and unstable.
79
+ *
80
+ * It is not written with productionizing it in mind, but instead used to evaluate how close Flow
81
+ * is to TypeScript.
82
+ *
83
+ * If you are going to use it anyways, you agree that you are calling a potentially broken function
84
+ * without any guarantee.
85
+ *
86
+ * @deprecated
87
+ */
88
+
89
+
90
+ async function unstable_translateTSDefToFlowDef(code, prettierOptions = {}) {
91
+ const ast = (0, _parser.parse)(code, {
92
+ loc: true,
93
+ range: true,
94
+ sourceType: 'module'
95
+ });
96
+
97
+ if (ast == null) {
98
+ throw `Failed to parse ${code} with @typescript-eslint/parser`;
99
+ }
100
+
101
+ const [flowAST, mutatedCode] = (0, _TSDefToFlowDef.TSDefToFlowDef)(code, ast, {
102
+ recoverFromErrors: false
103
+ });
104
+ return (0, _hermesTransform.print)(flowAST, mutatedCode, prettierOptions);
105
+ }
72
106
 
73
107
  async function translateFlowImportsTo(code, prettierOptions = {}, opts) {
74
108
  const {
@@ -13,11 +13,13 @@
13
13
  import type {MapperOptions} from './flowImportTo';
14
14
 
15
15
  import {parse, print} from 'hermes-transform';
16
+ import {parse as parseTS} from '@typescript-eslint/parser';
16
17
  import {visitorKeys as tsVisitorKeys} from '@typescript-eslint/visitor-keys';
17
18
  import flowToFlowDef from './flowToFlowDef';
18
19
  import {flowDefToTSDef} from './flowDefToTSDef';
19
20
  import {flowToJS} from './flowToJS';
20
21
  import {flowImportTo} from './flowImportTo';
22
+ import {TSDefToFlowDef} from './TSDefToFlowDef';
21
23
 
22
24
  export async function translateFlowToFlowDef(
23
25
  code: string,
@@ -72,6 +74,32 @@ export async function translateFlowToJS(
72
74
  return print(jsAST, code, prettierOptions);
73
75
  }
74
76
 
77
+ /**
78
+ * This translator is very experimental and unstable.
79
+ *
80
+ * It is not written with productionizing it in mind, but instead used to evaluate how close Flow
81
+ * is to TypeScript.
82
+ *
83
+ * If you are going to use it anyways, you agree that you are calling a potentially broken function
84
+ * without any guarantee.
85
+ *
86
+ * @deprecated
87
+ */
88
+ export async function unstable_translateTSDefToFlowDef(
89
+ code: string,
90
+ prettierOptions: {...} = {},
91
+ ): Promise<string> {
92
+ const ast = parseTS(code, {loc: true, range: true, sourceType: 'module'});
93
+ if (ast == null) {
94
+ throw `Failed to parse ${code} with @typescript-eslint/parser`;
95
+ }
96
+ const [flowAST, mutatedCode] = TSDefToFlowDef(code, ast, {
97
+ recoverFromErrors: false,
98
+ });
99
+
100
+ return print(flowAST, mutatedCode, prettierOptions);
101
+ }
102
+
75
103
  export type {MapperOptions as FlowImportsMapperOptions};
76
104
  export async function translateFlowImportsTo(
77
105
  code: string,
@@ -30,7 +30,9 @@
30
30
 
31
31
  'use strict';
32
32
 
33
- interface NodeOrTokenData {}
33
+ interface NodeOrTokenData {
34
+ loc: SourceLocation;
35
+ }
34
36
  interface BaseNode extends NodeOrTokenData {}
35
37
  interface BaseToken extends NodeOrTokenData {
36
38
  +value: string;
@@ -839,6 +841,7 @@ export interface MethodDefinitionAmbiguous extends MethodDefinitionBase {
839
841
  export interface MethodDefinitionComputedName
840
842
  extends MethodDefinitionComputedNameBase {
841
843
  +type: 'MethodDefinition';
844
+ +computed: true;
842
845
  }
843
846
  interface MethodDefinitionComputedNameBase extends MethodDefinitionBase {
844
847
  +key: PropertyNameComputed;
@@ -847,6 +850,7 @@ interface MethodDefinitionComputedNameBase extends MethodDefinitionBase {
847
850
  export interface MethodDefinitionNonComputedName
848
851
  extends ClassMethodDefinitionNonComputedNameBase {
849
852
  +type: 'MethodDefinition';
853
+ +computed: false;
850
854
  }
851
855
  interface MethodDefinitionNonComputedNameBase extends MethodDefinitionBase {
852
856
  +key: PropertyNameNonComputed;
@@ -1168,6 +1172,7 @@ export interface PropertyDefinitionAmbiguous extends PropertyDefinitionBase {
1168
1172
  export interface PropertyDefinitionComputedName
1169
1173
  extends PropertyDefinitionComputedNameBase {
1170
1174
  +type: 'PropertyDefinition';
1175
+ +computed: true;
1171
1176
  }
1172
1177
  interface PropertyDefinitionComputedNameBase extends PropertyDefinitionBase {
1173
1178
  +key: PropertyNameComputed;
@@ -1176,6 +1181,7 @@ interface PropertyDefinitionComputedNameBase extends PropertyDefinitionBase {
1176
1181
  export interface PropertyDefinitionNonComputedName
1177
1182
  extends ClassPropertyDefinitionNonComputedNameBase {
1178
1183
  +type: 'PropertyDefinition';
1184
+ +computed: false;
1179
1185
  }
1180
1186
  interface PropertyDefinitionNonComputedNameBase extends PropertyDefinitionBase {
1181
1187
  +key: PropertyNameNonComputed;
@@ -1409,22 +1415,26 @@ export type TSAbstractMethodDefinition =
1409
1415
  export interface TSAbstractMethodDefinitionComputedName
1410
1416
  extends MethodDefinitionComputedNameBase {
1411
1417
  +type: 'TSAbstractMethodDefinition';
1418
+ +computed: true;
1412
1419
  }
1413
1420
  export interface TSAbstractMethodDefinitionNonComputedName
1414
1421
  extends MethodDefinitionNonComputedNameBase {
1415
1422
  +type: 'TSAbstractMethodDefinition';
1423
+ +computed: false;
1416
1424
  }
1417
1425
  export type TSAbstractPropertyDefinition =
1418
1426
  | TSAbstractPropertyDefinitionComputedName
1419
1427
  | TSAbstractPropertyDefinitionNonComputedName;
1420
1428
  export interface TSAbstractPropertyDefinitionComputedName
1421
1429
  extends PropertyDefinitionComputedNameBase {
1430
+ +computed: true;
1422
1431
  +type: 'TSAbstractPropertyDefinition';
1423
1432
  +value: null;
1424
1433
  }
1425
1434
  export interface TSAbstractPropertyDefinitionNonComputedName
1426
1435
  extends PropertyDefinitionNonComputedNameBase {
1427
1436
  +type: 'TSAbstractPropertyDefinition';
1437
+ +computed: false;
1428
1438
  +value: null;
1429
1439
  }
1430
1440
  export interface TSAnyKeyword extends BaseNode {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-api-translator",
3
- "version": "0.20.1",
3
+ "version": "0.21.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",
@@ -10,12 +10,14 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@babel/code-frame": "^7.16.0",
13
- "@typescript-eslint/visitor-keys": "^5.42.0",
13
+ "@typescript-eslint/parser": "7.2.0",
14
+ "@typescript-eslint/visitor-keys": "7.2.0",
14
15
  "flow-enums-runtime": "^0.0.6",
15
- "hermes-eslint": "0.20.1",
16
- "hermes-estree": "0.20.1",
17
- "hermes-parser": "0.20.1",
18
- "hermes-transform": "0.20.1"
16
+ "hermes-eslint": "0.21.1",
17
+ "hermes-estree": "0.21.1",
18
+ "hermes-parser": "0.21.1",
19
+ "hermes-transform": "0.21.1",
20
+ "typescript": "5.3.2"
19
21
  },
20
22
  "peerDependencies": {
21
23
  "prettier": "^3.0.0 || ^2.7.1"