flow-api-translator 0.37.0 → 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 +35 -279
- package/dist/TSDefToFlowDef.js.flow +16 -4
- package/dist/flowDefToTSDef.js +51 -377
- package/dist/flowDefToTSDef.js.flow +93 -12
- package/dist/flowImportTo.js +5 -16
- package/dist/flowImportTo.js.flow +3 -3
- package/dist/flowToFlowDef.js +134 -359
- package/dist/flowToFlowDef.js.flow +18 -12
- package/dist/flowToJS.js +3 -10
- package/dist/flowToJS.js.flow +5 -5
- package/dist/index.js +12 -28
- package/dist/index.js.flow +1 -1
- package/dist/src/TSDefToFlowDef.js +35 -279
- package/dist/src/flowDefToTSDef.js +51 -377
- package/dist/src/flowImportTo.js +5 -16
- package/dist/src/flowToFlowDef.js +134 -359
- package/dist/src/flowToJS.js +3 -10
- package/dist/src/index.js +12 -28
- package/dist/src/utils/DocblockUtils.js +4 -4
- package/dist/src/utils/ErrorUtils.js +3 -18
- package/dist/src/utils/FlowAnalyze.js +5 -16
- package/dist/src/utils/TSNodeUtils.js +28 -13
- package/dist/src/utils/TranslationUtils.js +0 -4
- package/dist/utils/DocblockUtils.js +4 -4
- package/dist/utils/DocblockUtils.js.flow +1 -1
- package/dist/utils/ErrorUtils.js +3 -18
- package/dist/utils/ErrorUtils.js.flow +3 -3
- package/dist/utils/FlowAnalyze.js +5 -16
- package/dist/utils/FlowAnalyze.js.flow +3 -3
- package/dist/utils/TSNodeUtils.js +28 -13
- package/dist/utils/TSNodeUtils.js.flow +35 -1
- package/dist/utils/TranslationUtils.js +0 -4
- package/dist/utils/TranslationUtils.js.flow +2 -2
- package/package.json +9 -9
package/dist/src/flowToJS.js
CHANGED
|
@@ -4,27 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.flowToJS = flowToJS;
|
|
7
|
-
|
|
8
|
-
var _hermesParser = require("hermes-parser");
|
|
9
|
-
|
|
7
|
+
var _flowParser = require("flow-parser");
|
|
10
8
|
var _DocblockUtils = require("./utils/DocblockUtils");
|
|
11
|
-
|
|
12
9
|
const {
|
|
13
10
|
nodeWith
|
|
14
|
-
} =
|
|
15
|
-
|
|
11
|
+
} = _flowParser.astNodeMutationHelpers;
|
|
16
12
|
function stripAtFlow(ast, _options) {
|
|
17
13
|
const docblock = ast.docblock;
|
|
18
|
-
|
|
19
14
|
if (docblock == null) {
|
|
20
15
|
return ast;
|
|
21
16
|
}
|
|
22
|
-
|
|
23
17
|
return nodeWith(ast, {
|
|
24
18
|
docblock: (0, _DocblockUtils.removeAtFlowFromDocblock)(docblock)
|
|
25
19
|
});
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
function flowToJS(sourceAST, _code, _scopeManager) {
|
|
29
|
-
return [
|
|
22
|
+
return [_flowParser.Transforms.transformComponentSyntax, _flowParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
|
|
30
23
|
}
|
package/dist/src/index.js
CHANGED
|
@@ -9,85 +9,69 @@ exports.translateFlowToFlowDef = translateFlowToFlowDef;
|
|
|
9
9
|
exports.translateFlowToJS = translateFlowToJS;
|
|
10
10
|
exports.translateFlowToTSDef = translateFlowToTSDef;
|
|
11
11
|
exports.unstable_translateTSDefToFlowDef = unstable_translateTSDefToFlowDef;
|
|
12
|
-
|
|
13
|
-
var _hermesTransform = require("hermes-transform");
|
|
14
|
-
|
|
12
|
+
var _flowTransform = require("flow-transform");
|
|
15
13
|
var _parser = require("@typescript-eslint/parser");
|
|
16
|
-
|
|
17
14
|
var _visitorKeys = require("@typescript-eslint/visitor-keys");
|
|
18
|
-
|
|
19
15
|
var _flowToFlowDef = _interopRequireDefault(require("./flowToFlowDef"));
|
|
20
|
-
|
|
21
16
|
var _flowDefToTSDef = require("./flowDefToTSDef");
|
|
22
|
-
|
|
23
17
|
var _flowToJS = require("./flowToJS");
|
|
24
|
-
|
|
25
18
|
var _flowImportTo = require("./flowImportTo");
|
|
26
|
-
|
|
27
19
|
var _TSDefToFlowDef = require("./TSDefToFlowDef");
|
|
28
|
-
|
|
29
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
30
|
-
|
|
20
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
31
21
|
async function translateFlowToFlowDef(code, prettierOptions = {}, opts) {
|
|
32
22
|
const {
|
|
33
23
|
ast,
|
|
34
24
|
scopeManager
|
|
35
|
-
} = await (0,
|
|
25
|
+
} = await (0, _flowTransform.parse)(code);
|
|
36
26
|
const [flowDefAst, mutatedCode] = (0, _flowToFlowDef.default)(ast, code, scopeManager, {
|
|
37
27
|
recoverFromErrors: true,
|
|
38
28
|
mungeUnderscores: opts == null ? void 0 : opts.mungeUnderscores
|
|
39
29
|
});
|
|
40
|
-
return (0,
|
|
30
|
+
return (0, _flowTransform.print)(flowDefAst, mutatedCode, prettierOptions);
|
|
41
31
|
}
|
|
42
|
-
|
|
43
32
|
async function translateFlowToTSDef(code, prettierOptions = {}) {
|
|
44
33
|
const flowDefCode = await translateFlowToFlowDef(code, prettierOptions);
|
|
45
34
|
return translateFlowDefToTSDef(flowDefCode, prettierOptions);
|
|
46
35
|
}
|
|
47
|
-
|
|
48
36
|
async function translateFlowDefToTSDef(code, prettierOptions = {}) {
|
|
49
37
|
const {
|
|
50
38
|
ast,
|
|
51
39
|
scopeManager
|
|
52
|
-
} = await (0,
|
|
40
|
+
} = await (0, _flowTransform.parse)(code);
|
|
53
41
|
const [tsAST, mutatedCode] = (0, _flowDefToTSDef.flowDefToTSDef)(code, ast, scopeManager, {
|
|
54
42
|
recoverFromErrors: true
|
|
55
43
|
});
|
|
56
|
-
return (0,
|
|
44
|
+
return (0, _flowTransform.print)(tsAST, mutatedCode, {
|
|
45
|
+
...prettierOptions
|
|
57
46
|
}, _visitorKeys.visitorKeys);
|
|
58
47
|
}
|
|
59
|
-
|
|
60
48
|
async function translateFlowToJS(code, prettierOptions = {}) {
|
|
61
49
|
const {
|
|
62
50
|
ast,
|
|
63
51
|
scopeManager
|
|
64
|
-
} = await (0,
|
|
52
|
+
} = await (0, _flowTransform.parse)(code);
|
|
65
53
|
const jsAST = (0, _flowToJS.flowToJS)(ast, code, scopeManager);
|
|
66
|
-
return (0,
|
|
54
|
+
return (0, _flowTransform.print)(jsAST, code, prettierOptions);
|
|
67
55
|
}
|
|
68
|
-
|
|
69
56
|
async function unstable_translateTSDefToFlowDef(code, prettierOptions = {}) {
|
|
70
57
|
const ast = (0, _parser.parse)(code, {
|
|
71
58
|
loc: true,
|
|
72
59
|
range: true,
|
|
73
60
|
sourceType: 'module'
|
|
74
61
|
});
|
|
75
|
-
|
|
76
62
|
if (ast == null) {
|
|
77
63
|
throw `Failed to parse ${code} with @typescript-eslint/parser`;
|
|
78
64
|
}
|
|
79
|
-
|
|
80
65
|
const [flowAST, mutatedCode] = (0, _TSDefToFlowDef.TSDefToFlowDef)(code, ast, {
|
|
81
66
|
recoverFromErrors: false
|
|
82
67
|
});
|
|
83
|
-
return (0,
|
|
68
|
+
return (0, _flowTransform.print)(flowAST, mutatedCode, prettierOptions);
|
|
84
69
|
}
|
|
85
|
-
|
|
86
70
|
async function translateFlowImportsTo(code, prettierOptions = {}, opts) {
|
|
87
71
|
const {
|
|
88
72
|
ast,
|
|
89
73
|
scopeManager
|
|
90
|
-
} = await (0,
|
|
74
|
+
} = await (0, _flowTransform.parse)(code);
|
|
91
75
|
const jsAST = (0, _flowImportTo.flowImportTo)(ast, code, scopeManager, opts);
|
|
92
|
-
return (0,
|
|
76
|
+
return (0, _flowTransform.print)(jsAST, code, prettierOptions);
|
|
93
77
|
}
|
|
@@ -5,17 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.removeAtFlowFromDocblock = removeAtFlowFromDocblock;
|
|
7
7
|
const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;
|
|
8
|
-
|
|
9
8
|
function removeAtFlowFromDocblock(docblock) {
|
|
10
9
|
if (!FLOW_DIRECTIVE.test(docblock.comment.value)) {
|
|
11
10
|
return docblock;
|
|
12
11
|
}
|
|
13
|
-
|
|
14
12
|
return {
|
|
15
|
-
comment: {
|
|
13
|
+
comment: {
|
|
14
|
+
...docblock.comment,
|
|
16
15
|
value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
|
|
17
16
|
},
|
|
18
|
-
directives: {
|
|
17
|
+
directives: {
|
|
18
|
+
...docblock.directives,
|
|
19
19
|
flow: undefined,
|
|
20
20
|
noflow: undefined
|
|
21
21
|
}
|
|
@@ -8,60 +8,45 @@ exports.buildCodeFrame = buildCodeFrame;
|
|
|
8
8
|
exports.flowFixMeOrError = flowFixMeOrError;
|
|
9
9
|
exports.translationError = translationError;
|
|
10
10
|
exports.unexpectedTranslationError = unexpectedTranslationError;
|
|
11
|
-
|
|
12
|
-
var _hermesTransform = require("hermes-transform");
|
|
13
|
-
|
|
11
|
+
var _flowTransform = require("flow-transform");
|
|
14
12
|
var _codeFrame = require("@babel/code-frame");
|
|
15
|
-
|
|
16
13
|
function flowFixMeOrError(container, message, context) {
|
|
17
14
|
if (context.recoverFromErrors) {
|
|
18
|
-
return
|
|
19
|
-
id:
|
|
15
|
+
return _flowTransform.t.GenericTypeAnnotation({
|
|
16
|
+
id: _flowTransform.t.Identifier({
|
|
20
17
|
name: '$FlowFixMe'
|
|
21
18
|
})
|
|
22
19
|
});
|
|
23
20
|
}
|
|
24
|
-
|
|
25
21
|
throw translationError(container, message, context);
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
class TranslationErrorBase extends Error {
|
|
29
24
|
constructor(node, message, context) {
|
|
30
25
|
const framedMessage = buildCodeFrame(node, message, context.code);
|
|
31
26
|
super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
32
27
|
this.name = 'TranslationError';
|
|
33
28
|
}
|
|
34
|
-
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
class ExpectedTranslationError extends TranslationErrorBase {
|
|
38
31
|
constructor(...args) {
|
|
39
32
|
super(...args);
|
|
40
33
|
this.name = 'ExpectedTranslationError';
|
|
41
34
|
}
|
|
42
|
-
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
exports.ExpectedTranslationError = ExpectedTranslationError;
|
|
46
|
-
|
|
47
37
|
function translationError(node, message, context) {
|
|
48
38
|
return new ExpectedTranslationError(node, message, context);
|
|
49
39
|
}
|
|
50
|
-
|
|
51
40
|
class UnexpectedTranslationError extends TranslationErrorBase {
|
|
52
41
|
constructor(...args) {
|
|
53
42
|
super(...args);
|
|
54
43
|
this.name = 'UnexpectedTranslationError';
|
|
55
44
|
}
|
|
56
|
-
|
|
57
45
|
}
|
|
58
|
-
|
|
59
46
|
exports.UnexpectedTranslationError = UnexpectedTranslationError;
|
|
60
|
-
|
|
61
47
|
function unexpectedTranslationError(node, message, context) {
|
|
62
48
|
return new UnexpectedTranslationError(node, message, context);
|
|
63
49
|
}
|
|
64
|
-
|
|
65
50
|
function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
|
|
66
51
|
const locForBabel = {
|
|
67
52
|
start: {
|
|
@@ -5,31 +5,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.analyzeFunctionReturn = analyzeFunctionReturn;
|
|
7
7
|
exports.analyzeTypeDependencies = analyzeTypeDependencies;
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
var _hermesParser = require("hermes-parser");
|
|
12
|
-
|
|
8
|
+
var _flowTransform = require("flow-transform");
|
|
9
|
+
var _flowParser = require("flow-parser");
|
|
13
10
|
function analyzeFunctionReturn(func) {
|
|
14
11
|
const returnType = func.returnType;
|
|
15
|
-
|
|
16
12
|
if (returnType != null) {
|
|
17
13
|
return returnType;
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
15
|
+
return _flowTransform.t.TypeAnnotation({
|
|
16
|
+
typeAnnotation: _flowTransform.t.VoidTypeAnnotation()
|
|
22
17
|
});
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
function analyzeTypeDependencies(rootNode, context) {
|
|
26
20
|
const deps = [];
|
|
27
|
-
|
|
28
|
-
_hermesParser.SimpleTraverser.traverse(rootNode, {
|
|
21
|
+
_flowParser.SimpleTraverser.traverse(rootNode, {
|
|
29
22
|
enter(node) {
|
|
30
23
|
if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
|
|
31
24
|
const variable = context.referenceMap.get(node);
|
|
32
|
-
|
|
33
25
|
if (variable != null) {
|
|
34
26
|
deps.push(variable.name);
|
|
35
27
|
} else if (context.variableMap.has(node.name)) {
|
|
@@ -37,10 +29,7 @@ function analyzeTypeDependencies(rootNode, context) {
|
|
|
37
29
|
}
|
|
38
30
|
}
|
|
39
31
|
},
|
|
40
|
-
|
|
41
32
|
leave() {}
|
|
42
|
-
|
|
43
33
|
});
|
|
44
|
-
|
|
45
34
|
return deps;
|
|
46
35
|
}
|
|
@@ -3,14 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.ensureTypeIncludesUndefined = ensureTypeIncludesUndefined;
|
|
6
7
|
exports.extractPropertyKeyLiterals = extractPropertyKeyLiterals;
|
|
7
|
-
|
|
8
8
|
var TSESTree = _interopRequireWildcard(require("./ts-estree-ast-types"));
|
|
9
|
-
|
|
10
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
|
|
12
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
-
|
|
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); }
|
|
14
10
|
const DUMMY_LOC = {
|
|
15
11
|
start: {
|
|
16
12
|
line: 1,
|
|
@@ -21,27 +17,21 @@ const DUMMY_LOC = {
|
|
|
21
17
|
column: 0
|
|
22
18
|
}
|
|
23
19
|
};
|
|
24
|
-
|
|
25
20
|
function extractPropertyKeyLiterals(members) {
|
|
26
21
|
if (members.length === 0) {
|
|
27
22
|
return null;
|
|
28
23
|
}
|
|
29
|
-
|
|
30
24
|
const literals = [];
|
|
31
|
-
|
|
32
25
|
for (const member of members) {
|
|
33
26
|
if (member.type !== 'TSPropertySignature' && member.type !== 'TSMethodSignature') {
|
|
34
27
|
return null;
|
|
35
28
|
}
|
|
36
|
-
|
|
37
29
|
if (member.computed === true) {
|
|
38
30
|
return null;
|
|
39
31
|
}
|
|
40
|
-
|
|
41
32
|
const {
|
|
42
33
|
key
|
|
43
34
|
} = member;
|
|
44
|
-
|
|
45
35
|
if (key.type === 'Identifier') {
|
|
46
36
|
literals.push({
|
|
47
37
|
type: 'TSLiteralType',
|
|
@@ -63,6 +53,31 @@ function extractPropertyKeyLiterals(members) {
|
|
|
63
53
|
return null;
|
|
64
54
|
}
|
|
65
55
|
}
|
|
66
|
-
|
|
67
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
|
+
};
|
|
68
83
|
}
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.createTranslationContext = createTranslationContext;
|
|
7
|
-
|
|
8
7
|
function createTranslationContext(code, scopeManager, {
|
|
9
8
|
recoverFromErrors,
|
|
10
9
|
mungeUnderscores = true
|
|
@@ -12,18 +11,15 @@ function createTranslationContext(code, scopeManager, {
|
|
|
12
11
|
const referenceMap = new Map();
|
|
13
12
|
const variableMap = new Map();
|
|
14
13
|
const moduleScope = scopeManager.globalScope.childScopes[0];
|
|
15
|
-
|
|
16
14
|
if (moduleScope == null || moduleScope.type !== 'module') {
|
|
17
15
|
throw new Error('createTranslationContext: Module scope not found');
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
for (const variable of moduleScope.variables) {
|
|
21
18
|
for (const reference of variable.references) {
|
|
22
19
|
referenceMap.set(reference.identifier, variable);
|
|
23
20
|
variableMap.set(variable.name, variable);
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
|
-
|
|
27
23
|
return {
|
|
28
24
|
scopeManager,
|
|
29
25
|
referenceMap,
|
|
@@ -5,17 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.removeAtFlowFromDocblock = removeAtFlowFromDocblock;
|
|
7
7
|
const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;
|
|
8
|
-
|
|
9
8
|
function removeAtFlowFromDocblock(docblock) {
|
|
10
9
|
if (!FLOW_DIRECTIVE.test(docblock.comment.value)) {
|
|
11
10
|
return docblock;
|
|
12
11
|
}
|
|
13
|
-
|
|
14
12
|
return {
|
|
15
|
-
comment: {
|
|
13
|
+
comment: {
|
|
14
|
+
...docblock.comment,
|
|
16
15
|
value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
|
|
17
16
|
},
|
|
18
|
-
directives: {
|
|
17
|
+
directives: {
|
|
18
|
+
...docblock.directives,
|
|
19
19
|
flow: undefined,
|
|
20
20
|
noflow: undefined
|
|
21
21
|
}
|
package/dist/utils/ErrorUtils.js
CHANGED
|
@@ -8,60 +8,45 @@ exports.buildCodeFrame = buildCodeFrame;
|
|
|
8
8
|
exports.flowFixMeOrError = flowFixMeOrError;
|
|
9
9
|
exports.translationError = translationError;
|
|
10
10
|
exports.unexpectedTranslationError = unexpectedTranslationError;
|
|
11
|
-
|
|
12
|
-
var _hermesTransform = require("hermes-transform");
|
|
13
|
-
|
|
11
|
+
var _flowTransform = require("flow-transform");
|
|
14
12
|
var _codeFrame = require("@babel/code-frame");
|
|
15
|
-
|
|
16
13
|
function flowFixMeOrError(container, message, context) {
|
|
17
14
|
if (context.recoverFromErrors) {
|
|
18
|
-
return
|
|
19
|
-
id:
|
|
15
|
+
return _flowTransform.t.GenericTypeAnnotation({
|
|
16
|
+
id: _flowTransform.t.Identifier({
|
|
20
17
|
name: '$FlowFixMe'
|
|
21
18
|
})
|
|
22
19
|
});
|
|
23
20
|
}
|
|
24
|
-
|
|
25
21
|
throw translationError(container, message, context);
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
class TranslationErrorBase extends Error {
|
|
29
24
|
constructor(node, message, context) {
|
|
30
25
|
const framedMessage = buildCodeFrame(node, message, context.code);
|
|
31
26
|
super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
32
27
|
this.name = 'TranslationError';
|
|
33
28
|
}
|
|
34
|
-
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
class ExpectedTranslationError extends TranslationErrorBase {
|
|
38
31
|
constructor(...args) {
|
|
39
32
|
super(...args);
|
|
40
33
|
this.name = 'ExpectedTranslationError';
|
|
41
34
|
}
|
|
42
|
-
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
exports.ExpectedTranslationError = ExpectedTranslationError;
|
|
46
|
-
|
|
47
37
|
function translationError(node, message, context) {
|
|
48
38
|
return new ExpectedTranslationError(node, message, context);
|
|
49
39
|
}
|
|
50
|
-
|
|
51
40
|
class UnexpectedTranslationError extends TranslationErrorBase {
|
|
52
41
|
constructor(...args) {
|
|
53
42
|
super(...args);
|
|
54
43
|
this.name = 'UnexpectedTranslationError';
|
|
55
44
|
}
|
|
56
|
-
|
|
57
45
|
}
|
|
58
|
-
|
|
59
46
|
exports.UnexpectedTranslationError = UnexpectedTranslationError;
|
|
60
|
-
|
|
61
47
|
function unexpectedTranslationError(node, message, context) {
|
|
62
48
|
return new UnexpectedTranslationError(node, message, context);
|
|
63
49
|
}
|
|
64
|
-
|
|
65
50
|
function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
|
|
66
51
|
const locForBabel = {
|
|
67
52
|
start: {
|
|
@@ -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(
|
|
@@ -5,31 +5,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.analyzeFunctionReturn = analyzeFunctionReturn;
|
|
7
7
|
exports.analyzeTypeDependencies = analyzeTypeDependencies;
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
var _hermesParser = require("hermes-parser");
|
|
12
|
-
|
|
8
|
+
var _flowTransform = require("flow-transform");
|
|
9
|
+
var _flowParser = require("flow-parser");
|
|
13
10
|
function analyzeFunctionReturn(func) {
|
|
14
11
|
const returnType = func.returnType;
|
|
15
|
-
|
|
16
12
|
if (returnType != null) {
|
|
17
13
|
return returnType;
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
15
|
+
return _flowTransform.t.TypeAnnotation({
|
|
16
|
+
typeAnnotation: _flowTransform.t.VoidTypeAnnotation()
|
|
22
17
|
});
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
function analyzeTypeDependencies(rootNode, context) {
|
|
26
20
|
const deps = [];
|
|
27
|
-
|
|
28
|
-
_hermesParser.SimpleTraverser.traverse(rootNode, {
|
|
21
|
+
_flowParser.SimpleTraverser.traverse(rootNode, {
|
|
29
22
|
enter(node) {
|
|
30
23
|
if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
|
|
31
24
|
const variable = context.referenceMap.get(node);
|
|
32
|
-
|
|
33
25
|
if (variable != null) {
|
|
34
26
|
deps.push(variable.name);
|
|
35
27
|
} else if (context.variableMap.has(node.name)) {
|
|
@@ -37,10 +29,7 @@ function analyzeTypeDependencies(rootNode, context) {
|
|
|
37
29
|
}
|
|
38
30
|
}
|
|
39
31
|
},
|
|
40
|
-
|
|
41
32
|
leave() {}
|
|
42
|
-
|
|
43
33
|
});
|
|
44
|
-
|
|
45
34
|
return deps;
|
|
46
35
|
}
|
|
@@ -10,11 +10,11 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {AFunction, TypeAnnotation, ESNode} from '
|
|
13
|
+
import type {AFunction, TypeAnnotation, ESNode} from 'flow-estree';
|
|
14
14
|
import type {TranslationContext, Dep} from './TranslationUtils';
|
|
15
15
|
|
|
16
|
-
import {t} from '
|
|
17
|
-
import {SimpleTraverser} from '
|
|
16
|
+
import {t} from 'flow-transform';
|
|
17
|
+
import {SimpleTraverser} from 'flow-parser';
|
|
18
18
|
|
|
19
19
|
export function analyzeFunctionReturn(func: AFunction): TypeAnnotation {
|
|
20
20
|
const returnType = func.returnType;
|
|
@@ -3,14 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.ensureTypeIncludesUndefined = ensureTypeIncludesUndefined;
|
|
6
7
|
exports.extractPropertyKeyLiterals = extractPropertyKeyLiterals;
|
|
7
|
-
|
|
8
8
|
var TSESTree = _interopRequireWildcard(require("./ts-estree-ast-types"));
|
|
9
|
-
|
|
10
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
|
|
12
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
-
|
|
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); }
|
|
14
10
|
const DUMMY_LOC = {
|
|
15
11
|
start: {
|
|
16
12
|
line: 1,
|
|
@@ -21,27 +17,21 @@ const DUMMY_LOC = {
|
|
|
21
17
|
column: 0
|
|
22
18
|
}
|
|
23
19
|
};
|
|
24
|
-
|
|
25
20
|
function extractPropertyKeyLiterals(members) {
|
|
26
21
|
if (members.length === 0) {
|
|
27
22
|
return null;
|
|
28
23
|
}
|
|
29
|
-
|
|
30
24
|
const literals = [];
|
|
31
|
-
|
|
32
25
|
for (const member of members) {
|
|
33
26
|
if (member.type !== 'TSPropertySignature' && member.type !== 'TSMethodSignature') {
|
|
34
27
|
return null;
|
|
35
28
|
}
|
|
36
|
-
|
|
37
29
|
if (member.computed === true) {
|
|
38
30
|
return null;
|
|
39
31
|
}
|
|
40
|
-
|
|
41
32
|
const {
|
|
42
33
|
key
|
|
43
34
|
} = member;
|
|
44
|
-
|
|
45
35
|
if (key.type === 'Identifier') {
|
|
46
36
|
literals.push({
|
|
47
37
|
type: 'TSLiteralType',
|
|
@@ -63,6 +53,31 @@ function extractPropertyKeyLiterals(members) {
|
|
|
63
53
|
return null;
|
|
64
54
|
}
|
|
65
55
|
}
|
|
66
|
-
|
|
67
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
|
+
};
|
|
68
83
|
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {SourceLocation} from '
|
|
13
|
+
import type {SourceLocation} from 'flow-estree';
|
|
14
14
|
|
|
15
15
|
import * as TSESTree from './ts-estree-ast-types';
|
|
16
16
|
|
|
@@ -72,3 +72,37 @@ export function extractPropertyKeyLiterals(
|
|
|
72
72
|
}
|
|
73
73
|
return literals;
|
|
74
74
|
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns a type that is guaranteed to include `undefined`, without duplicating
|
|
78
|
+
* it when already present.
|
|
79
|
+
*
|
|
80
|
+
* Flow's optional members permit `undefined`, so the faithful TypeScript
|
|
81
|
+
* translation of `foo?: T` is `foo?: T | undefined` (the two are distinct under
|
|
82
|
+
* `exactOptionalPropertyTypes`).
|
|
83
|
+
*/
|
|
84
|
+
export function ensureTypeIncludesUndefined(
|
|
85
|
+
typeAnnotation: TSESTree.TypeNode,
|
|
86
|
+
): TSESTree.TypeNode {
|
|
87
|
+
if (typeAnnotation.type === 'TSUndefinedKeyword') {
|
|
88
|
+
return typeAnnotation;
|
|
89
|
+
}
|
|
90
|
+
if (typeAnnotation.type === 'TSUnionType') {
|
|
91
|
+
if (typeAnnotation.types.some(type => type.type === 'TSUndefinedKeyword')) {
|
|
92
|
+
return typeAnnotation;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
type: 'TSUnionType',
|
|
96
|
+
loc: DUMMY_LOC,
|
|
97
|
+
types: [
|
|
98
|
+
...typeAnnotation.types,
|
|
99
|
+
{type: 'TSUndefinedKeyword', loc: DUMMY_LOC},
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
type: 'TSUnionType',
|
|
105
|
+
loc: DUMMY_LOC,
|
|
106
|
+
types: [typeAnnotation, {type: 'TSUndefinedKeyword', loc: DUMMY_LOC}],
|
|
107
|
+
};
|
|
108
|
+
}
|