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.
- package/dist/TSDefToFlowDef.js +66 -327
- package/dist/TSDefToFlowDef.js.flow +70 -35
- package/dist/flowDefToTSDef.js +134 -825
- package/dist/flowDefToTSDef.js.flow +196 -100
- package/dist/flowImportTo.js +5 -25
- package/dist/flowImportTo.js.flow +4 -4
- package/dist/flowToFlowDef.js +190 -379
- package/dist/flowToFlowDef.js.flow +109 -27
- package/dist/flowToJS.js +3 -19
- package/dist/flowToJS.js.flow +5 -5
- package/dist/index.js +12 -50
- package/dist/index.js.flow +1 -1
- package/dist/src/TSDefToFlowDef.js +66 -327
- package/dist/src/flowDefToTSDef.js +134 -825
- package/dist/src/flowImportTo.js +5 -25
- package/dist/src/flowToFlowDef.js +190 -379
- package/dist/src/flowToJS.js +3 -19
- package/dist/src/index.js +12 -50
- package/dist/src/utils/DocblockUtils.js +4 -14
- package/dist/src/utils/ErrorUtils.js +4 -40
- package/dist/src/utils/FlowAnalyze.js +8 -28
- package/dist/src/utils/TSNodeUtils.js +83 -0
- package/dist/src/utils/TranslationUtils.js +0 -13
- package/dist/src/utils/ts-estree-ast-types.js +0 -29
- package/dist/utils/DocblockUtils.js +4 -14
- package/dist/utils/DocblockUtils.js.flow +1 -1
- package/dist/utils/ErrorUtils.js +4 -40
- package/dist/utils/ErrorUtils.js.flow +6 -6
- package/dist/utils/FlowAnalyze.js +8 -28
- package/dist/utils/FlowAnalyze.js.flow +9 -4
- package/dist/utils/TSNodeUtils.js +83 -0
- package/dist/utils/TSNodeUtils.js.flow +108 -0
- package/dist/utils/TranslationUtils.js +0 -13
- package/dist/utils/TranslationUtils.js.flow +2 -2
- package/dist/utils/ts-estree-ast-types.js +0 -29
- package/dist/utils/ts-estree-ast-types.js.flow +697 -740
- package/package.json +10 -9
package/dist/src/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
|
-
} =
|
|
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 [
|
|
22
|
+
return [_flowParser.Transforms.transformComponentSyntax, _flowParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
|
|
39
23
|
}
|
package/dist/src/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,
|
|
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,
|
|
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,
|
|
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,
|
|
66
|
-
|
|
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,
|
|
52
|
+
} = await (0, _flowTransform.parse)(code);
|
|
75
53
|
const jsAST = (0, _flowToJS.flowToJS)(ast, code, scopeManager);
|
|
76
|
-
return (0,
|
|
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,
|
|
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,
|
|
74
|
+
} = await (0, _flowTransform.parse)(code);
|
|
113
75
|
const jsAST = (0, _flowImportTo.flowImportTo)(ast, code, scopeManager, opts);
|
|
114
|
-
return (0,
|
|
76
|
+
return (0, _flowTransform.print)(jsAST, code, prettierOptions);
|
|
115
77
|
}
|
|
@@ -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", {
|
|
@@ -14,18 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
14
5
|
});
|
|
15
6
|
exports.removeAtFlowFromDocblock = removeAtFlowFromDocblock;
|
|
16
7
|
const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;
|
|
17
|
-
|
|
18
8
|
function removeAtFlowFromDocblock(docblock) {
|
|
19
9
|
if (!FLOW_DIRECTIVE.test(docblock.comment.value)) {
|
|
20
10
|
return docblock;
|
|
21
11
|
}
|
|
22
|
-
|
|
23
12
|
return {
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
comment: {
|
|
14
|
+
...docblock.comment,
|
|
26
15
|
value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
|
|
27
16
|
},
|
|
28
|
-
directives: {
|
|
17
|
+
directives: {
|
|
18
|
+
...docblock.directives,
|
|
29
19
|
flow: undefined,
|
|
30
20
|
noflow: undefined
|
|
31
21
|
}
|
|
@@ -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", {
|
|
@@ -17,73 +8,46 @@ exports.buildCodeFrame = buildCodeFrame;
|
|
|
17
8
|
exports.flowFixMeOrError = flowFixMeOrError;
|
|
18
9
|
exports.translationError = translationError;
|
|
19
10
|
exports.unexpectedTranslationError = unexpectedTranslationError;
|
|
20
|
-
|
|
21
|
-
var _hermesTransform = require("hermes-transform");
|
|
22
|
-
|
|
11
|
+
var _flowTransform = require("flow-transform");
|
|
23
12
|
var _codeFrame = require("@babel/code-frame");
|
|
24
|
-
|
|
25
13
|
function flowFixMeOrError(container, message, context) {
|
|
26
14
|
if (context.recoverFromErrors) {
|
|
27
|
-
return
|
|
28
|
-
id:
|
|
15
|
+
return _flowTransform.t.GenericTypeAnnotation({
|
|
16
|
+
id: _flowTransform.t.Identifier({
|
|
29
17
|
name: '$FlowFixMe'
|
|
30
18
|
})
|
|
31
19
|
});
|
|
32
20
|
}
|
|
33
|
-
|
|
34
21
|
throw translationError(container, message, context);
|
|
35
22
|
}
|
|
36
|
-
|
|
37
23
|
class TranslationErrorBase extends Error {
|
|
38
24
|
constructor(node, message, context) {
|
|
39
25
|
const framedMessage = buildCodeFrame(node, message, context.code);
|
|
40
|
-
super(
|
|
41
|
-
// `instanceof Error` which makes the code frame look awful and hard to verify:
|
|
42
|
-
//
|
|
43
|
-
// [TranslationError: > 12 | code
|
|
44
|
-
// | ^^^^ error]
|
|
45
|
-
//
|
|
46
|
-
// this just adds a newline in jest tests so that it all lines up nicely
|
|
47
|
-
//
|
|
48
|
-
// [TranslationError:
|
|
49
|
-
// > 12 | code
|
|
50
|
-
// | ^^^^ error]
|
|
51
|
-
process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
26
|
+
super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
52
27
|
this.name = 'TranslationError';
|
|
53
28
|
}
|
|
54
|
-
|
|
55
29
|
}
|
|
56
|
-
|
|
57
30
|
class ExpectedTranslationError extends TranslationErrorBase {
|
|
58
31
|
constructor(...args) {
|
|
59
32
|
super(...args);
|
|
60
33
|
this.name = 'ExpectedTranslationError';
|
|
61
34
|
}
|
|
62
|
-
|
|
63
35
|
}
|
|
64
|
-
|
|
65
36
|
exports.ExpectedTranslationError = ExpectedTranslationError;
|
|
66
|
-
|
|
67
37
|
function translationError(node, message, context) {
|
|
68
38
|
return new ExpectedTranslationError(node, message, context);
|
|
69
39
|
}
|
|
70
|
-
|
|
71
40
|
class UnexpectedTranslationError extends TranslationErrorBase {
|
|
72
41
|
constructor(...args) {
|
|
73
42
|
super(...args);
|
|
74
43
|
this.name = 'UnexpectedTranslationError';
|
|
75
44
|
}
|
|
76
|
-
|
|
77
45
|
}
|
|
78
|
-
|
|
79
46
|
exports.UnexpectedTranslationError = UnexpectedTranslationError;
|
|
80
|
-
|
|
81
47
|
function unexpectedTranslationError(node, message, context) {
|
|
82
48
|
return new UnexpectedTranslationError(node, message, context);
|
|
83
49
|
}
|
|
84
|
-
|
|
85
50
|
function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
|
|
86
|
-
// babel uses 1-indexed columns
|
|
87
51
|
const locForBabel = {
|
|
88
52
|
start: {
|
|
89
53
|
line: node.loc.start.line,
|
|
@@ -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", {
|
|
@@ -14,42 +5,31 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
14
5
|
});
|
|
15
6
|
exports.analyzeFunctionReturn = analyzeFunctionReturn;
|
|
16
7
|
exports.analyzeTypeDependencies = analyzeTypeDependencies;
|
|
17
|
-
|
|
18
|
-
var
|
|
19
|
-
|
|
20
|
-
var _hermesParser = require("hermes-parser");
|
|
21
|
-
|
|
8
|
+
var _flowTransform = require("flow-transform");
|
|
9
|
+
var _flowParser = require("flow-parser");
|
|
22
10
|
function analyzeFunctionReturn(func) {
|
|
23
11
|
const returnType = func.returnType;
|
|
24
|
-
|
|
25
12
|
if (returnType != null) {
|
|
26
13
|
return returnType;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return _hermesTransform.t.TypeAnnotation({
|
|
32
|
-
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
14
|
+
}
|
|
15
|
+
return _flowTransform.t.TypeAnnotation({
|
|
16
|
+
typeAnnotation: _flowTransform.t.VoidTypeAnnotation()
|
|
33
17
|
});
|
|
34
18
|
}
|
|
35
|
-
|
|
36
19
|
function analyzeTypeDependencies(rootNode, context) {
|
|
37
20
|
const deps = [];
|
|
38
|
-
|
|
39
|
-
_hermesParser.SimpleTraverser.traverse(rootNode, {
|
|
21
|
+
_flowParser.SimpleTraverser.traverse(rootNode, {
|
|
40
22
|
enter(node) {
|
|
41
23
|
if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
|
|
42
24
|
const variable = context.referenceMap.get(node);
|
|
43
|
-
|
|
44
25
|
if (variable != null) {
|
|
45
26
|
deps.push(variable.name);
|
|
27
|
+
} else if (context.variableMap.has(node.name)) {
|
|
28
|
+
deps.push(node.name);
|
|
46
29
|
}
|
|
47
30
|
}
|
|
48
31
|
},
|
|
49
|
-
|
|
50
32
|
leave() {}
|
|
51
|
-
|
|
52
33
|
});
|
|
53
|
-
|
|
54
34
|
return deps;
|
|
55
35
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ensureTypeIncludesUndefined = ensureTypeIncludesUndefined;
|
|
7
|
+
exports.extractPropertyKeyLiterals = extractPropertyKeyLiterals;
|
|
8
|
+
var TSESTree = _interopRequireWildcard(require("./ts-estree-ast-types"));
|
|
9
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
10
|
+
const DUMMY_LOC = {
|
|
11
|
+
start: {
|
|
12
|
+
line: 1,
|
|
13
|
+
column: 0
|
|
14
|
+
},
|
|
15
|
+
end: {
|
|
16
|
+
line: 1,
|
|
17
|
+
column: 0
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function extractPropertyKeyLiterals(members) {
|
|
21
|
+
if (members.length === 0) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const literals = [];
|
|
25
|
+
for (const member of members) {
|
|
26
|
+
if (member.type !== 'TSPropertySignature' && member.type !== 'TSMethodSignature') {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
if (member.computed === true) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const {
|
|
33
|
+
key
|
|
34
|
+
} = member;
|
|
35
|
+
if (key.type === 'Identifier') {
|
|
36
|
+
literals.push({
|
|
37
|
+
type: 'TSLiteralType',
|
|
38
|
+
loc: DUMMY_LOC,
|
|
39
|
+
literal: {
|
|
40
|
+
type: 'Literal',
|
|
41
|
+
loc: DUMMY_LOC,
|
|
42
|
+
value: key.name,
|
|
43
|
+
raw: `'${key.name}'`
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
} else if (key.type === 'Literal') {
|
|
47
|
+
literals.push({
|
|
48
|
+
type: 'TSLiteralType',
|
|
49
|
+
loc: DUMMY_LOC,
|
|
50
|
+
literal: key
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return literals;
|
|
57
|
+
}
|
|
58
|
+
function ensureTypeIncludesUndefined(typeAnnotation) {
|
|
59
|
+
if (typeAnnotation.type === 'TSUndefinedKeyword') {
|
|
60
|
+
return typeAnnotation;
|
|
61
|
+
}
|
|
62
|
+
if (typeAnnotation.type === 'TSUnionType') {
|
|
63
|
+
if (typeAnnotation.types.some(type => type.type === 'TSUndefinedKeyword')) {
|
|
64
|
+
return typeAnnotation;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
type: 'TSUnionType',
|
|
68
|
+
loc: DUMMY_LOC,
|
|
69
|
+
types: [...typeAnnotation.types, {
|
|
70
|
+
type: 'TSUndefinedKeyword',
|
|
71
|
+
loc: DUMMY_LOC
|
|
72
|
+
}]
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
type: 'TSUnionType',
|
|
77
|
+
loc: DUMMY_LOC,
|
|
78
|
+
types: [typeAnnotation, {
|
|
79
|
+
type: 'TSUndefinedKeyword',
|
|
80
|
+
loc: DUMMY_LOC
|
|
81
|
+
}]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -1,19 +1,9 @@
|
|
|
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.createTranslationContext = createTranslationContext;
|
|
16
|
-
|
|
17
7
|
function createTranslationContext(code, scopeManager, {
|
|
18
8
|
recoverFromErrors,
|
|
19
9
|
mungeUnderscores = true
|
|
@@ -21,18 +11,15 @@ function createTranslationContext(code, scopeManager, {
|
|
|
21
11
|
const referenceMap = new Map();
|
|
22
12
|
const variableMap = new Map();
|
|
23
13
|
const moduleScope = scopeManager.globalScope.childScopes[0];
|
|
24
|
-
|
|
25
14
|
if (moduleScope == null || moduleScope.type !== 'module') {
|
|
26
15
|
throw new Error('createTranslationContext: Module scope not found');
|
|
27
16
|
}
|
|
28
|
-
|
|
29
17
|
for (const variable of moduleScope.variables) {
|
|
30
18
|
for (const reference of variable.references) {
|
|
31
19
|
referenceMap.set(reference.identifier, variable);
|
|
32
20
|
variableMap.set(variable.name, variable);
|
|
33
21
|
}
|
|
34
22
|
}
|
|
35
|
-
|
|
36
23
|
return {
|
|
37
24
|
scopeManager,
|
|
38
25
|
referenceMap,
|
|
@@ -1,30 +1 @@
|
|
|
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
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The following types have been adapted by hand from
|
|
13
|
-
* https://unpkg.com/browse/@typescript-eslint/types@5.41.0/dist/generated/ast-spec.d.ts
|
|
14
|
-
*
|
|
15
|
-
* Changes:
|
|
16
|
-
* - remove and inline `ValueOf` type
|
|
17
|
-
* - `undefined` -> `void`
|
|
18
|
-
* - remove all `declare` keywords
|
|
19
|
-
* - comment out `bigint` type
|
|
20
|
-
* -> flow doesn't support it yet
|
|
21
|
-
* - remove `range` and `loc` from `NodeOrTokenData`
|
|
22
|
-
* -> during conversion our locations will be all off, so we'll rely on prettier to print later
|
|
23
|
-
* - make all properties readonly and all arrays $ReadOnlyArray
|
|
24
|
-
* -> unlike TS - flow enforces subtype constraints strictly!
|
|
25
|
-
* - add `type` to interfaces that previously relied upon inheriting the `type`
|
|
26
|
-
* -> this is because flow sentinel refinement does not check inherited members
|
|
27
|
-
* - create "Ambiguous" versions for some nodes that have unions (like PropertyDefinition, MemberDefinition)
|
|
28
|
-
* -> makes it easier to construct them from other nodes that have unions
|
|
29
|
-
*/
|
|
30
1
|
'use strict';
|
|
@@ -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", {
|
|
@@ -14,18 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
14
5
|
});
|
|
15
6
|
exports.removeAtFlowFromDocblock = removeAtFlowFromDocblock;
|
|
16
7
|
const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;
|
|
17
|
-
|
|
18
8
|
function removeAtFlowFromDocblock(docblock) {
|
|
19
9
|
if (!FLOW_DIRECTIVE.test(docblock.comment.value)) {
|
|
20
10
|
return docblock;
|
|
21
11
|
}
|
|
22
|
-
|
|
23
12
|
return {
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
comment: {
|
|
14
|
+
...docblock.comment,
|
|
26
15
|
value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
|
|
27
16
|
},
|
|
28
|
-
directives: {
|
|
17
|
+
directives: {
|
|
18
|
+
...docblock.directives,
|
|
29
19
|
flow: undefined,
|
|
30
20
|
noflow: undefined
|
|
31
21
|
}
|
package/dist/utils/ErrorUtils.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", {
|
|
@@ -17,73 +8,46 @@ exports.buildCodeFrame = buildCodeFrame;
|
|
|
17
8
|
exports.flowFixMeOrError = flowFixMeOrError;
|
|
18
9
|
exports.translationError = translationError;
|
|
19
10
|
exports.unexpectedTranslationError = unexpectedTranslationError;
|
|
20
|
-
|
|
21
|
-
var _hermesTransform = require("hermes-transform");
|
|
22
|
-
|
|
11
|
+
var _flowTransform = require("flow-transform");
|
|
23
12
|
var _codeFrame = require("@babel/code-frame");
|
|
24
|
-
|
|
25
13
|
function flowFixMeOrError(container, message, context) {
|
|
26
14
|
if (context.recoverFromErrors) {
|
|
27
|
-
return
|
|
28
|
-
id:
|
|
15
|
+
return _flowTransform.t.GenericTypeAnnotation({
|
|
16
|
+
id: _flowTransform.t.Identifier({
|
|
29
17
|
name: '$FlowFixMe'
|
|
30
18
|
})
|
|
31
19
|
});
|
|
32
20
|
}
|
|
33
|
-
|
|
34
21
|
throw translationError(container, message, context);
|
|
35
22
|
}
|
|
36
|
-
|
|
37
23
|
class TranslationErrorBase extends Error {
|
|
38
24
|
constructor(node, message, context) {
|
|
39
25
|
const framedMessage = buildCodeFrame(node, message, context.code);
|
|
40
|
-
super(
|
|
41
|
-
// `instanceof Error` which makes the code frame look awful and hard to verify:
|
|
42
|
-
//
|
|
43
|
-
// [TranslationError: > 12 | code
|
|
44
|
-
// | ^^^^ error]
|
|
45
|
-
//
|
|
46
|
-
// this just adds a newline in jest tests so that it all lines up nicely
|
|
47
|
-
//
|
|
48
|
-
// [TranslationError:
|
|
49
|
-
// > 12 | code
|
|
50
|
-
// | ^^^^ error]
|
|
51
|
-
process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
26
|
+
super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
52
27
|
this.name = 'TranslationError';
|
|
53
28
|
}
|
|
54
|
-
|
|
55
29
|
}
|
|
56
|
-
|
|
57
30
|
class ExpectedTranslationError extends TranslationErrorBase {
|
|
58
31
|
constructor(...args) {
|
|
59
32
|
super(...args);
|
|
60
33
|
this.name = 'ExpectedTranslationError';
|
|
61
34
|
}
|
|
62
|
-
|
|
63
35
|
}
|
|
64
|
-
|
|
65
36
|
exports.ExpectedTranslationError = ExpectedTranslationError;
|
|
66
|
-
|
|
67
37
|
function translationError(node, message, context) {
|
|
68
38
|
return new ExpectedTranslationError(node, message, context);
|
|
69
39
|
}
|
|
70
|
-
|
|
71
40
|
class UnexpectedTranslationError extends TranslationErrorBase {
|
|
72
41
|
constructor(...args) {
|
|
73
42
|
super(...args);
|
|
74
43
|
this.name = 'UnexpectedTranslationError';
|
|
75
44
|
}
|
|
76
|
-
|
|
77
45
|
}
|
|
78
|
-
|
|
79
46
|
exports.UnexpectedTranslationError = UnexpectedTranslationError;
|
|
80
|
-
|
|
81
47
|
function unexpectedTranslationError(node, message, context) {
|
|
82
48
|
return new UnexpectedTranslationError(node, message, context);
|
|
83
49
|
}
|
|
84
|
-
|
|
85
50
|
function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
|
|
86
|
-
// babel uses 1-indexed columns
|
|
87
51
|
const locForBabel = {
|
|
88
52
|
start: {
|
|
89
53
|
line: node.loc.start.line,
|
|
@@ -10,11 +10,11 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {ObjectWithLoc, TypeAnnotationType} from '
|
|
13
|
+
import type {ObjectWithLoc, TypeAnnotationType} from 'flow-estree';
|
|
14
14
|
import type {TranslationContext} from './TranslationUtils';
|
|
15
|
-
import type {DetachedNode} from '
|
|
15
|
+
import type {DetachedNode} from 'flow-transform';
|
|
16
16
|
|
|
17
|
-
import {t} from '
|
|
17
|
+
import {t} from 'flow-transform';
|
|
18
18
|
import {codeFrameColumns} from '@babel/code-frame';
|
|
19
19
|
|
|
20
20
|
export function flowFixMeOrError(
|
|
@@ -33,7 +33,7 @@ class TranslationErrorBase extends Error {
|
|
|
33
33
|
constructor(
|
|
34
34
|
node: ObjectWithLoc,
|
|
35
35
|
message: string,
|
|
36
|
-
context:
|
|
36
|
+
context: Readonly<{code: string, ...}>,
|
|
37
37
|
) {
|
|
38
38
|
const framedMessage = buildCodeFrame(node, message, context.code);
|
|
39
39
|
super(
|
|
@@ -59,7 +59,7 @@ export class ExpectedTranslationError extends TranslationErrorBase {
|
|
|
59
59
|
export function translationError(
|
|
60
60
|
node: ObjectWithLoc,
|
|
61
61
|
message: string,
|
|
62
|
-
context:
|
|
62
|
+
context: Readonly<{code: string, ...}>,
|
|
63
63
|
): ExpectedTranslationError {
|
|
64
64
|
return new ExpectedTranslationError(node, message, context);
|
|
65
65
|
}
|
|
@@ -70,7 +70,7 @@ export class UnexpectedTranslationError extends TranslationErrorBase {
|
|
|
70
70
|
export function unexpectedTranslationError(
|
|
71
71
|
node: ObjectWithLoc,
|
|
72
72
|
message: string,
|
|
73
|
-
context:
|
|
73
|
+
context: Readonly<{code: string, ...}>,
|
|
74
74
|
): UnexpectedTranslationError {
|
|
75
75
|
return new UnexpectedTranslationError(node, message, context);
|
|
76
76
|
}
|