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
|
@@ -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
|
}
|
|
@@ -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;
|
|
@@ -30,7 +30,7 @@ export function analyzeFunctionReturn(func: AFunction): TypeAnnotation {
|
|
|
30
30
|
export function analyzeTypeDependencies(
|
|
31
31
|
rootNode: ESNode,
|
|
32
32
|
context: TranslationContext,
|
|
33
|
-
):
|
|
33
|
+
): ReadonlyArray<Dep> {
|
|
34
34
|
const deps = [];
|
|
35
35
|
SimpleTraverser.traverse(rootNode, {
|
|
36
36
|
enter(node: ESNode) {
|
|
@@ -38,6 +38,11 @@ export function analyzeTypeDependencies(
|
|
|
38
38
|
const variable = context.referenceMap.get(node);
|
|
39
39
|
if (variable != null) {
|
|
40
40
|
deps.push(variable.name);
|
|
41
|
+
} else if (context.variableMap.has(node.name)) {
|
|
42
|
+
// The scope manager may not track type references to value
|
|
43
|
+
// variables (e.g. `const Foo = require('foo')` used as `Foo`
|
|
44
|
+
// in a GenericTypeAnnotation). Fall back to variableMap.
|
|
45
|
+
deps.push(node.name);
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
},
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
import type {SourceLocation} from 'flow-estree';
|
|
14
|
+
|
|
15
|
+
import * as TSESTree from './ts-estree-ast-types';
|
|
16
|
+
|
|
17
|
+
const DUMMY_LOC: SourceLocation = {
|
|
18
|
+
start: {line: 1, column: 0},
|
|
19
|
+
end: {line: 1, column: 0},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Extract statically known property key names from a list of TS type element
|
|
24
|
+
* nodes and return them as `TSLiteralType` (string literal) AST nodes.
|
|
25
|
+
*
|
|
26
|
+
* Returns `null` if any member has a computed or non-extractable key,
|
|
27
|
+
* or if the list is empty.
|
|
28
|
+
*/
|
|
29
|
+
export function extractPropertyKeyLiterals(
|
|
30
|
+
members: ReadonlyArray<TSESTree.TypeElement>,
|
|
31
|
+
): Array<TSESTree.TSLiteralType> | null {
|
|
32
|
+
// An empty member list has no keys to extract. Returning `null` here lets
|
|
33
|
+
// the caller fall back to `keyof {}`, preserving the original output for
|
|
34
|
+
// spread-only objects (e.g. `{...T1}`) where a literal union would be
|
|
35
|
+
// invalid TS.
|
|
36
|
+
if (members.length === 0) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const literals: Array<TSESTree.TSLiteralType> = [];
|
|
40
|
+
for (const member of members) {
|
|
41
|
+
if (
|
|
42
|
+
member.type !== 'TSPropertySignature' &&
|
|
43
|
+
member.type !== 'TSMethodSignature'
|
|
44
|
+
) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (member.computed === true) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const {key} = member;
|
|
51
|
+
if (key.type === 'Identifier') {
|
|
52
|
+
literals.push({
|
|
53
|
+
type: 'TSLiteralType',
|
|
54
|
+
loc: DUMMY_LOC,
|
|
55
|
+
literal: {
|
|
56
|
+
type: 'Literal',
|
|
57
|
+
loc: DUMMY_LOC,
|
|
58
|
+
value: key.name,
|
|
59
|
+
raw: `'${key.name}'`,
|
|
60
|
+
} as TSESTree.StringLiteral,
|
|
61
|
+
});
|
|
62
|
+
} else if (key.type === 'Literal') {
|
|
63
|
+
// A string or numeric literal key is already a valid literal type.
|
|
64
|
+
literals.push({
|
|
65
|
+
type: 'TSLiteralType',
|
|
66
|
+
loc: DUMMY_LOC,
|
|
67
|
+
literal: key,
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return literals;
|
|
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
|
+
}
|
|
@@ -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,
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {Identifier, JSXIdentifier} from '
|
|
14
|
-
import type {ScopeManager, Variable} from '
|
|
13
|
+
import type {Identifier, JSXIdentifier} from 'flow-estree';
|
|
14
|
+
import type {ScopeManager, Variable} from 'flow-eslint';
|
|
15
15
|
|
|
16
16
|
export type Dep = string;
|
|
17
17
|
export type TranslationOptions = {
|
|
@@ -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';
|