flow-api-translator 0.36.1 → 0.37.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 +38 -55
- package/dist/TSDefToFlowDef.js.flow +54 -31
- package/dist/flowDefToTSDef.js +88 -453
- package/dist/flowDefToTSDef.js.flow +104 -89
- package/dist/flowImportTo.js +0 -9
- package/dist/flowImportTo.js.flow +1 -1
- package/dist/flowToFlowDef.js +83 -47
- package/dist/flowToFlowDef.js.flow +91 -15
- package/dist/flowToJS.js +0 -9
- package/dist/index.js +1 -23
- package/dist/src/TSDefToFlowDef.js +38 -55
- package/dist/src/flowDefToTSDef.js +88 -453
- package/dist/src/flowImportTo.js +0 -9
- package/dist/src/flowToFlowDef.js +83 -47
- package/dist/src/flowToJS.js +0 -9
- package/dist/src/index.js +1 -23
- package/dist/src/utils/DocblockUtils.js +0 -10
- package/dist/src/utils/ErrorUtils.js +1 -22
- package/dist/src/utils/FlowAnalyze.js +3 -12
- package/dist/src/utils/TSNodeUtils.js +68 -0
- package/dist/src/utils/TranslationUtils.js +0 -9
- package/dist/src/utils/ts-estree-ast-types.js +0 -29
- package/dist/utils/DocblockUtils.js +0 -10
- package/dist/utils/ErrorUtils.js +1 -22
- package/dist/utils/ErrorUtils.js.flow +3 -3
- package/dist/utils/FlowAnalyze.js +3 -12
- package/dist/utils/FlowAnalyze.js.flow +6 -1
- package/dist/utils/TSNodeUtils.js +68 -0
- package/dist/utils/TSNodeUtils.js.flow +74 -0
- package/dist/utils/TranslationUtils.js +0 -9
- package/dist/utils/ts-estree-ast-types.js +0 -29
- package/dist/utils/ts-estree-ast-types.js.flow +697 -740
- package/package.json +8 -7
package/dist/src/flowImportTo.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", {
|
|
@@ -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", {
|
|
@@ -49,7 +40,6 @@ function getTopLevelStatement(node, context) {
|
|
|
49
40
|
var _currentNode$parent;
|
|
50
41
|
|
|
51
42
|
if (((_currentNode$parent = currentNode.parent) == null ? void 0 : _currentNode$parent.type) === 'Program') {
|
|
52
|
-
// $FlowFixMe[incompatible-type]
|
|
53
43
|
return currentNode;
|
|
54
44
|
}
|
|
55
45
|
|
|
@@ -60,20 +50,10 @@ function getTopLevelStatement(node, context) {
|
|
|
60
50
|
}
|
|
61
51
|
|
|
62
52
|
function transferProgramStatementProperties(stmt, orgStmt) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
stmt.comments = orgStmt.comments; // $FlowExpectedError[incompatible-use]
|
|
66
|
-
|
|
67
|
-
stmt.range = orgStmt.range; // $FlowExpectedError[incompatible-use]
|
|
68
|
-
|
|
53
|
+
stmt.comments = orgStmt.comments;
|
|
54
|
+
stmt.range = orgStmt.range;
|
|
69
55
|
stmt.loc = orgStmt.loc;
|
|
70
56
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Consume an arbitrary Flow AST and convert it into a type definition file.
|
|
73
|
-
*
|
|
74
|
-
* To do this all runtime logic will be stripped and only types that describe the module boundary will remain.
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
57
|
|
|
78
58
|
function flowToFlowDef(ast, code, scopeManager, opts) {
|
|
79
59
|
var _ast$interpreter$valu, _ast$interpreter;
|
|
@@ -123,8 +103,7 @@ function flowToFlowDef(ast, code, scopeManager, opts) {
|
|
|
123
103
|
function updateProcessedStatement(stmt) {
|
|
124
104
|
processedStatements.add(stmt);
|
|
125
105
|
pendingStatements.delete(stmt);
|
|
126
|
-
}
|
|
127
|
-
|
|
106
|
+
}
|
|
128
107
|
|
|
129
108
|
for (const stmt of ast.body) {
|
|
130
109
|
const resultExport = convertExport(stmt, context);
|
|
@@ -203,14 +182,11 @@ function convertExport(stmt, context) {
|
|
|
203
182
|
if (expr.type === 'AssignmentExpression' && expr.left.type === 'MemberExpression') {
|
|
204
183
|
const member = expr.left;
|
|
205
184
|
|
|
206
|
-
if (
|
|
207
|
-
member.object.type === 'Identifier' && member.object.name === 'exports' || // module.exports.A = 1;
|
|
208
|
-
member.object.type === 'MemberExpression' && member.object.object.type === 'Identifier' && member.object.object.name === 'module' && member.object.property.type === 'Identifier' && member.object.property.name === 'exports') {
|
|
185
|
+
if (member.object.type === 'Identifier' && member.object.name === 'exports' || member.object.type === 'MemberExpression' && member.object.object.type === 'Identifier' && member.object.object.name === 'module' && member.object.property.type === 'Identifier' && member.object.property.name === 'exports') {
|
|
209
186
|
throw (0, _ErrorUtils.translationError)(stmt, `convertExport: Named CommonJS exports not supported. Use either \`module.exports = {...}\` or ES6 exports.`, context);
|
|
210
187
|
}
|
|
211
188
|
|
|
212
|
-
if (
|
|
213
|
-
member.object.type === 'Identifier' && member.object.name === 'module' && member.property.type === 'Identifier' && member.property.name === 'exports') {
|
|
189
|
+
if (member.object.type === 'Identifier' && member.object.name === 'module' && member.property.type === 'Identifier' && member.property.name === 'exports') {
|
|
214
190
|
const [typeAnnotation, deps] = convertExpressionToTypeAnnotation(expr.right, context);
|
|
215
191
|
return [_hermesTransform.t.DeclareModuleExports({
|
|
216
192
|
typeAnnotation: _hermesTransform.t.TypeAnnotation({
|
|
@@ -225,14 +201,12 @@ function convertExport(stmt, context) {
|
|
|
225
201
|
|
|
226
202
|
default:
|
|
227
203
|
{
|
|
228
|
-
// Skip non exported functions
|
|
229
204
|
return null;
|
|
230
205
|
}
|
|
231
206
|
}
|
|
232
207
|
}
|
|
233
208
|
|
|
234
209
|
function stripUnusedDefs(detachedStmt, usedDeps, context) {
|
|
235
|
-
// $FlowExpectedError[incompatible-type]
|
|
236
210
|
const stmt = detachedStmt;
|
|
237
211
|
|
|
238
212
|
switch (stmt.type) {
|
|
@@ -315,6 +289,12 @@ function convertStatement(stmt, context) {
|
|
|
315
289
|
|
|
316
290
|
case 'VariableDeclaration':
|
|
317
291
|
{
|
|
292
|
+
const requireImport = convertRequireToImport(stmt);
|
|
293
|
+
|
|
294
|
+
if (requireImport != null) {
|
|
295
|
+
return requireImport;
|
|
296
|
+
}
|
|
297
|
+
|
|
318
298
|
const [result, deps] = convertVariableDeclaration(stmt, context);
|
|
319
299
|
return [result, deps];
|
|
320
300
|
}
|
|
@@ -396,7 +376,6 @@ function convertExpressionToTypeAnnotation(expr, context) {
|
|
|
396
376
|
}
|
|
397
377
|
|
|
398
378
|
function inheritComments(fromNode, toNode) {
|
|
399
|
-
// $FlowFixMe[unclear-type]
|
|
400
379
|
toNode.comments = fromNode.comments;
|
|
401
380
|
return toNode;
|
|
402
381
|
}
|
|
@@ -425,7 +404,6 @@ function convertObjectExpression(expr, context) {
|
|
|
425
404
|
|
|
426
405
|
const [resultExpr, deps] = convertAFunction(prop.value, context);
|
|
427
406
|
return [inheritComments(prop, _hermesTransform.t.ObjectTypeMethodSignature({
|
|
428
|
-
// $FlowFixMe[incompatible-type]
|
|
429
407
|
key: (0, _hermesTransform.asDetachedNode)(prop.key),
|
|
430
408
|
value: resultExpr
|
|
431
409
|
})), deps];
|
|
@@ -439,7 +417,6 @@ function convertObjectExpression(expr, context) {
|
|
|
439
417
|
const kind = prop.kind;
|
|
440
418
|
const [resultExpr, deps] = convertAFunction(prop.value, context);
|
|
441
419
|
return [inheritComments(prop, _hermesTransform.t.ObjectTypeAccessorSignature({
|
|
442
|
-
// $FlowFixMe[incompatible-type]
|
|
443
420
|
key: (0, _hermesTransform.asDetachedNode)(prop.key),
|
|
444
421
|
kind,
|
|
445
422
|
value: resultExpr
|
|
@@ -448,7 +425,6 @@ function convertObjectExpression(expr, context) {
|
|
|
448
425
|
|
|
449
426
|
const [resultExpr, deps] = convertExpressionToTypeAnnotation(prop.value, context);
|
|
450
427
|
return [inheritComments(prop, _hermesTransform.t.ObjectTypePropertySignature({
|
|
451
|
-
// $FlowFixMe[incompatible-type]
|
|
452
428
|
key: (0, _hermesTransform.asDetachedNode)(prop.key),
|
|
453
429
|
value: resultExpr,
|
|
454
430
|
optional: false,
|
|
@@ -726,6 +702,76 @@ function convertVariableDeclaration(stmt, context) {
|
|
|
726
702
|
}), annotDeps];
|
|
727
703
|
}
|
|
728
704
|
|
|
705
|
+
function convertRequireToImport(stmt) {
|
|
706
|
+
if (stmt.declarations.length !== 1) {
|
|
707
|
+
return null;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const decl = stmt.declarations[0];
|
|
711
|
+
const init = decl.init;
|
|
712
|
+
|
|
713
|
+
if (init == null || init.type !== 'CallExpression' || init.callee.type !== 'Identifier' || init.callee.name !== 'require' || init.arguments.length !== 1) {
|
|
714
|
+
return null;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
const sourceArg = init.arguments[0];
|
|
718
|
+
|
|
719
|
+
if (!(0, _hermesEstree.isStringLiteral)(sourceArg)) {
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const id = decl.id;
|
|
724
|
+
|
|
725
|
+
if (id.type === 'Identifier') {
|
|
726
|
+
return [_hermesTransform.t.ImportDeclaration({
|
|
727
|
+
importKind: 'value',
|
|
728
|
+
source: (0, _hermesTransform.asDetachedNode)(sourceArg),
|
|
729
|
+
specifiers: [_hermesTransform.t.ImportDefaultSpecifier({
|
|
730
|
+
local: _hermesTransform.t.Identifier({
|
|
731
|
+
name: id.name
|
|
732
|
+
})
|
|
733
|
+
})],
|
|
734
|
+
attributes: []
|
|
735
|
+
}), []];
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
if (id.type === 'ObjectPattern') {
|
|
739
|
+
const specifiers = [];
|
|
740
|
+
|
|
741
|
+
for (const prop of id.properties) {
|
|
742
|
+
if (prop.type === 'RestElement') {
|
|
743
|
+
return null;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const key = prop.key;
|
|
747
|
+
const value = prop.value;
|
|
748
|
+
|
|
749
|
+
if (key.type !== 'Identifier' || value.type !== 'Identifier') {
|
|
750
|
+
return null;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
specifiers.push(_hermesTransform.t.ImportSpecifier({
|
|
754
|
+
imported: _hermesTransform.t.Identifier({
|
|
755
|
+
name: key.name
|
|
756
|
+
}),
|
|
757
|
+
local: _hermesTransform.t.Identifier({
|
|
758
|
+
name: value.name
|
|
759
|
+
}),
|
|
760
|
+
importKind: null
|
|
761
|
+
}));
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
return [_hermesTransform.t.ImportDeclaration({
|
|
765
|
+
importKind: 'value',
|
|
766
|
+
source: (0, _hermesTransform.asDetachedNode)(sourceArg),
|
|
767
|
+
specifiers,
|
|
768
|
+
attributes: []
|
|
769
|
+
}), []];
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
|
|
729
775
|
function convertImportDeclaration(stmt, context) {
|
|
730
776
|
if (stmt.attributes.length > 0) {
|
|
731
777
|
throw (0, _ErrorUtils.translationError)(stmt, 'ImportDeclaration: attributes not supported', context);
|
|
@@ -754,7 +800,6 @@ function convertClassDeclaration(class_, context) {
|
|
|
754
800
|
}
|
|
755
801
|
|
|
756
802
|
return [_hermesTransform.t.DeclareClass({
|
|
757
|
-
// $FlowFixMe[incompatible-type]
|
|
758
803
|
id: (0, _hermesTransform.asDetachedNode)(class_.id),
|
|
759
804
|
typeParameters: resultTypeParams,
|
|
760
805
|
implements: class_.implements.map(impl => (0, _hermesTransform.asDetachedNode)(impl)),
|
|
@@ -886,7 +931,6 @@ function convertClassMember(member, context) {
|
|
|
886
931
|
{
|
|
887
932
|
var _member$value;
|
|
888
933
|
|
|
889
|
-
// PrivateIdentifier's are not exposed so can be stripped.
|
|
890
934
|
if (member.key.type === 'PrivateIdentifier') {
|
|
891
935
|
return EMPTY_TRANSLATION_RESULT;
|
|
892
936
|
}
|
|
@@ -902,7 +946,6 @@ function convertClassMember(member, context) {
|
|
|
902
946
|
if (((_member$value = member.value) == null ? void 0 : _member$value.type) === 'ArrowFunctionExpression' && member.typeAnnotation == null) {
|
|
903
947
|
const [resultTypeAnnotation, deps] = convertAFunction(member.value, context);
|
|
904
948
|
return [inheritComments(member, _hermesTransform.t.ObjectTypePropertySignature({
|
|
905
|
-
// $FlowFixMe[incompatible-type]
|
|
906
949
|
key: (0, _hermesTransform.asDetachedNode)(member.key),
|
|
907
950
|
value: resultTypeAnnotation,
|
|
908
951
|
optional: member.optional,
|
|
@@ -913,7 +956,6 @@ function convertClassMember(member, context) {
|
|
|
913
956
|
|
|
914
957
|
const [resultTypeAnnotation, deps] = convertTypeAnnotation(member.typeAnnotation, member, context);
|
|
915
958
|
return [inheritComments(member, _hermesTransform.t.ObjectTypePropertySignature({
|
|
916
|
-
// $FlowFixMe[incompatible-type]
|
|
917
959
|
key: (0, _hermesTransform.asDetachedNode)(member.key),
|
|
918
960
|
value: resultTypeAnnotation,
|
|
919
961
|
optional: member.optional,
|
|
@@ -924,7 +966,6 @@ function convertClassMember(member, context) {
|
|
|
924
966
|
|
|
925
967
|
case 'MethodDefinition':
|
|
926
968
|
{
|
|
927
|
-
// PrivateIdentifier's are not exposed so can be stripped.
|
|
928
969
|
if (member.key.type === 'PrivateIdentifier') {
|
|
929
970
|
return EMPTY_TRANSLATION_RESULT;
|
|
930
971
|
}
|
|
@@ -943,10 +984,8 @@ function convertClassMember(member, context) {
|
|
|
943
984
|
}) : member.key;
|
|
944
985
|
|
|
945
986
|
if (member.kind === 'get' || member.kind === 'set') {
|
|
946
|
-
// accessors are methods - but flow accessor signatures are properties
|
|
947
987
|
const kind = member.kind;
|
|
948
988
|
return [inheritComments(member, _hermesTransform.t.ObjectTypeAccessorSignature({
|
|
949
|
-
// $FlowFixMe[incompatible-type]
|
|
950
989
|
key: (0, _hermesTransform.asDetachedNode)(newKey),
|
|
951
990
|
value: resultValue,
|
|
952
991
|
static: member.static,
|
|
@@ -955,7 +994,6 @@ function convertClassMember(member, context) {
|
|
|
955
994
|
}
|
|
956
995
|
|
|
957
996
|
return [inheritComments(member, _hermesTransform.t.ObjectTypeMethodSignature({
|
|
958
|
-
// $FlowFixMe[incompatible-type]
|
|
959
997
|
key: (0, _hermesTransform.asDetachedNode)(newKey),
|
|
960
998
|
value: resultValue,
|
|
961
999
|
static: member.static
|
|
@@ -1115,8 +1153,7 @@ function convertHookDeclaration(hook, context) {
|
|
|
1115
1153
|
var _hook$returnType;
|
|
1116
1154
|
|
|
1117
1155
|
const id = hook.id;
|
|
1118
|
-
const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType :
|
|
1119
|
-
_hermesTransform.t.TypeAnnotation({
|
|
1156
|
+
const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType : _hermesTransform.t.TypeAnnotation({
|
|
1120
1157
|
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
1121
1158
|
});
|
|
1122
1159
|
const [resultReturnType, returnDeps] = convertTypeAnnotation(returnType, hook, context);
|
|
@@ -1206,8 +1243,7 @@ function convertFunctionParameters(params, context) {
|
|
|
1206
1243
|
throw (0, _ErrorUtils.translationError)(param, `FunctionParameter: Multiple rest elements found`, context);
|
|
1207
1244
|
}
|
|
1208
1245
|
|
|
1209
|
-
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(
|
|
1210
|
-
param.argument, context, index, false);
|
|
1246
|
+
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param.argument, context, index, false);
|
|
1211
1247
|
return [resultParams, resultParam, [...paramsDeps, ...deps]];
|
|
1212
1248
|
}
|
|
1213
1249
|
}
|
package/dist/src/flowToJS.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", {
|
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", {
|
|
@@ -62,8 +53,7 @@ async function translateFlowDefToTSDef(code, prettierOptions = {}) {
|
|
|
62
53
|
const [tsAST, mutatedCode] = (0, _flowDefToTSDef.flowDefToTSDef)(code, ast, scopeManager, {
|
|
63
54
|
recoverFromErrors: true
|
|
64
55
|
});
|
|
65
|
-
return (0, _hermesTransform.print)(
|
|
66
|
-
tsAST, mutatedCode, { ...prettierOptions
|
|
56
|
+
return (0, _hermesTransform.print)(tsAST, mutatedCode, { ...prettierOptions
|
|
67
57
|
}, _visitorKeys.visitorKeys);
|
|
68
58
|
}
|
|
69
59
|
|
|
@@ -75,18 +65,6 @@ async function translateFlowToJS(code, prettierOptions = {}) {
|
|
|
75
65
|
const jsAST = (0, _flowToJS.flowToJS)(ast, code, scopeManager);
|
|
76
66
|
return (0, _hermesTransform.print)(jsAST, code, prettierOptions);
|
|
77
67
|
}
|
|
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
68
|
|
|
91
69
|
async function unstable_translateTSDefToFlowDef(code, prettierOptions = {}) {
|
|
92
70
|
const ast = (0, _parser.parse)(code, {
|
|
@@ -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", {
|
|
@@ -21,7 +12,6 @@ function removeAtFlowFromDocblock(docblock) {
|
|
|
21
12
|
}
|
|
22
13
|
|
|
23
14
|
return {
|
|
24
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
25
15
|
comment: { ...docblock.comment,
|
|
26
16
|
value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
|
|
27
17
|
},
|
|
@@ -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", {
|
|
@@ -37,18 +28,7 @@ function flowFixMeOrError(container, message, context) {
|
|
|
37
28
|
class TranslationErrorBase extends Error {
|
|
38
29
|
constructor(node, message, context) {
|
|
39
30
|
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}`);
|
|
31
|
+
super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
52
32
|
this.name = 'TranslationError';
|
|
53
33
|
}
|
|
54
34
|
|
|
@@ -83,7 +63,6 @@ function unexpectedTranslationError(node, message, context) {
|
|
|
83
63
|
}
|
|
84
64
|
|
|
85
65
|
function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
|
|
86
|
-
// babel uses 1-indexed columns
|
|
87
66
|
const locForBabel = {
|
|
88
67
|
start: {
|
|
89
68
|
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", {
|
|
@@ -24,9 +15,7 @@ function analyzeFunctionReturn(func) {
|
|
|
24
15
|
|
|
25
16
|
if (returnType != null) {
|
|
26
17
|
return returnType;
|
|
27
|
-
}
|
|
28
|
-
// $FlowFixMe[incompatible-type]
|
|
29
|
-
|
|
18
|
+
}
|
|
30
19
|
|
|
31
20
|
return _hermesTransform.t.TypeAnnotation({
|
|
32
21
|
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
@@ -43,6 +32,8 @@ function analyzeTypeDependencies(rootNode, context) {
|
|
|
43
32
|
|
|
44
33
|
if (variable != null) {
|
|
45
34
|
deps.push(variable.name);
|
|
35
|
+
} else if (context.variableMap.has(node.name)) {
|
|
36
|
+
deps.push(node.name);
|
|
46
37
|
}
|
|
47
38
|
}
|
|
48
39
|
},
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.extractPropertyKeyLiterals = extractPropertyKeyLiterals;
|
|
7
|
+
|
|
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
|
+
|
|
14
|
+
const DUMMY_LOC = {
|
|
15
|
+
start: {
|
|
16
|
+
line: 1,
|
|
17
|
+
column: 0
|
|
18
|
+
},
|
|
19
|
+
end: {
|
|
20
|
+
line: 1,
|
|
21
|
+
column: 0
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function extractPropertyKeyLiterals(members) {
|
|
26
|
+
if (members.length === 0) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const literals = [];
|
|
31
|
+
|
|
32
|
+
for (const member of members) {
|
|
33
|
+
if (member.type !== 'TSPropertySignature' && member.type !== 'TSMethodSignature') {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (member.computed === true) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const {
|
|
42
|
+
key
|
|
43
|
+
} = member;
|
|
44
|
+
|
|
45
|
+
if (key.type === 'Identifier') {
|
|
46
|
+
literals.push({
|
|
47
|
+
type: 'TSLiteralType',
|
|
48
|
+
loc: DUMMY_LOC,
|
|
49
|
+
literal: {
|
|
50
|
+
type: 'Literal',
|
|
51
|
+
loc: DUMMY_LOC,
|
|
52
|
+
value: key.name,
|
|
53
|
+
raw: `'${key.name}'`
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
} else if (key.type === 'Literal') {
|
|
57
|
+
literals.push({
|
|
58
|
+
type: 'TSLiteralType',
|
|
59
|
+
loc: DUMMY_LOC,
|
|
60
|
+
literal: key
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return literals;
|
|
68
|
+
}
|
|
@@ -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", {
|
|
@@ -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", {
|
|
@@ -21,7 +12,6 @@ function removeAtFlowFromDocblock(docblock) {
|
|
|
21
12
|
}
|
|
22
13
|
|
|
23
14
|
return {
|
|
24
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
25
15
|
comment: { ...docblock.comment,
|
|
26
16
|
value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
|
|
27
17
|
},
|
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", {
|
|
@@ -37,18 +28,7 @@ function flowFixMeOrError(container, message, context) {
|
|
|
37
28
|
class TranslationErrorBase extends Error {
|
|
38
29
|
constructor(node, message, context) {
|
|
39
30
|
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}`);
|
|
31
|
+
super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
|
|
52
32
|
this.name = 'TranslationError';
|
|
53
33
|
}
|
|
54
34
|
|
|
@@ -83,7 +63,6 @@ function unexpectedTranslationError(node, message, context) {
|
|
|
83
63
|
}
|
|
84
64
|
|
|
85
65
|
function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
|
|
86
|
-
// babel uses 1-indexed columns
|
|
87
66
|
const locForBabel = {
|
|
88
67
|
start: {
|
|
89
68
|
line: node.loc.start.line,
|
|
@@ -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
|
}
|
|
@@ -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", {
|
|
@@ -24,9 +15,7 @@ function analyzeFunctionReturn(func) {
|
|
|
24
15
|
|
|
25
16
|
if (returnType != null) {
|
|
26
17
|
return returnType;
|
|
27
|
-
}
|
|
28
|
-
// $FlowFixMe[incompatible-type]
|
|
29
|
-
|
|
18
|
+
}
|
|
30
19
|
|
|
31
20
|
return _hermesTransform.t.TypeAnnotation({
|
|
32
21
|
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
@@ -43,6 +32,8 @@ function analyzeTypeDependencies(rootNode, context) {
|
|
|
43
32
|
|
|
44
33
|
if (variable != null) {
|
|
45
34
|
deps.push(variable.name);
|
|
35
|
+
} else if (context.variableMap.has(node.name)) {
|
|
36
|
+
deps.push(node.name);
|
|
46
37
|
}
|
|
47
38
|
}
|
|
48
39
|
},
|