flow-api-translator 0.36.0 → 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 +179 -78
- package/dist/flowToFlowDef.js.flow +238 -47
- package/dist/flowToJS.js +0 -9
- package/dist/index.js +4 -25
- package/dist/index.js.flow +2 -0
- 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 +179 -78
- package/dist/src/flowToJS.js +0 -9
- package/dist/src/index.js +4 -25
- 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 +3 -10
- 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 +3 -10
- package/dist/utils/TranslationUtils.js.flow +14 -3
- 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)),
|
|
@@ -827,7 +872,6 @@ function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeArgum
|
|
|
827
872
|
|
|
828
873
|
function convertSuperClass(superClass, superTypeArguments, context) {
|
|
829
874
|
if (superClass == null) {
|
|
830
|
-
// $FlowFixMe[incompatible-type]
|
|
831
875
|
return EMPTY_TRANSLATION_RESULT;
|
|
832
876
|
}
|
|
833
877
|
|
|
@@ -887,9 +931,11 @@ function convertClassMember(member, context) {
|
|
|
887
931
|
{
|
|
888
932
|
var _member$value;
|
|
889
933
|
|
|
890
|
-
// PrivateIdentifier's are not exposed so can be stripped.
|
|
891
934
|
if (member.key.type === 'PrivateIdentifier') {
|
|
892
|
-
|
|
935
|
+
return EMPTY_TRANSLATION_RESULT;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
if (context.mungeUnderscores && member.key.type === 'Identifier' && member.key.name.length >= 2 && member.key.name[0] === '_' && member.key.name[1] !== '_') {
|
|
893
939
|
return EMPTY_TRANSLATION_RESULT;
|
|
894
940
|
}
|
|
895
941
|
|
|
@@ -900,7 +946,6 @@ function convertClassMember(member, context) {
|
|
|
900
946
|
if (((_member$value = member.value) == null ? void 0 : _member$value.type) === 'ArrowFunctionExpression' && member.typeAnnotation == null) {
|
|
901
947
|
const [resultTypeAnnotation, deps] = convertAFunction(member.value, context);
|
|
902
948
|
return [inheritComments(member, _hermesTransform.t.ObjectTypePropertySignature({
|
|
903
|
-
// $FlowFixMe[incompatible-type]
|
|
904
949
|
key: (0, _hermesTransform.asDetachedNode)(member.key),
|
|
905
950
|
value: resultTypeAnnotation,
|
|
906
951
|
optional: member.optional,
|
|
@@ -911,7 +956,6 @@ function convertClassMember(member, context) {
|
|
|
911
956
|
|
|
912
957
|
const [resultTypeAnnotation, deps] = convertTypeAnnotation(member.typeAnnotation, member, context);
|
|
913
958
|
return [inheritComments(member, _hermesTransform.t.ObjectTypePropertySignature({
|
|
914
|
-
// $FlowFixMe[incompatible-type]
|
|
915
959
|
key: (0, _hermesTransform.asDetachedNode)(member.key),
|
|
916
960
|
value: resultTypeAnnotation,
|
|
917
961
|
optional: member.optional,
|
|
@@ -922,9 +966,11 @@ function convertClassMember(member, context) {
|
|
|
922
966
|
|
|
923
967
|
case 'MethodDefinition':
|
|
924
968
|
{
|
|
925
|
-
// PrivateIdentifier's are not exposed so can be stripped.
|
|
926
969
|
if (member.key.type === 'PrivateIdentifier') {
|
|
927
|
-
|
|
970
|
+
return EMPTY_TRANSLATION_RESULT;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
if (context.mungeUnderscores && member.key.type === 'Identifier' && member.key.name.length >= 2 && member.key.name[0] === '_' && member.key.name[1] !== '_') {
|
|
928
974
|
return EMPTY_TRANSLATION_RESULT;
|
|
929
975
|
}
|
|
930
976
|
|
|
@@ -938,10 +984,8 @@ function convertClassMember(member, context) {
|
|
|
938
984
|
}) : member.key;
|
|
939
985
|
|
|
940
986
|
if (member.kind === 'get' || member.kind === 'set') {
|
|
941
|
-
// accessors are methods - but flow accessor signatures are properties
|
|
942
987
|
const kind = member.kind;
|
|
943
988
|
return [inheritComments(member, _hermesTransform.t.ObjectTypeAccessorSignature({
|
|
944
|
-
// $FlowFixMe[incompatible-type]
|
|
945
989
|
key: (0, _hermesTransform.asDetachedNode)(newKey),
|
|
946
990
|
value: resultValue,
|
|
947
991
|
static: member.static,
|
|
@@ -950,7 +994,6 @@ function convertClassMember(member, context) {
|
|
|
950
994
|
}
|
|
951
995
|
|
|
952
996
|
return [inheritComments(member, _hermesTransform.t.ObjectTypeMethodSignature({
|
|
953
|
-
// $FlowFixMe[incompatible-type]
|
|
954
997
|
key: (0, _hermesTransform.asDetachedNode)(newKey),
|
|
955
998
|
value: resultValue,
|
|
956
999
|
static: member.static
|
|
@@ -987,31 +1030,44 @@ function convertComponentDeclaration(comp, context) {
|
|
|
987
1030
|
}), [...typeParamsDeps, ...paramsAndRestDeps, ...rendersTypeDeps]];
|
|
988
1031
|
}
|
|
989
1032
|
|
|
1033
|
+
function hasNonIdentifierStringLiteralParam(params) {
|
|
1034
|
+
return params.some(param => param.type === 'ComponentParameter' && (0, _hermesEstree.isStringLiteral)(param.name) && !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(param.name.value));
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
function extractParamTypeInfo(param, context) {
|
|
1038
|
+
let optional = false;
|
|
1039
|
+
let local = param.local;
|
|
1040
|
+
|
|
1041
|
+
if (local.type === 'AssignmentPattern') {
|
|
1042
|
+
local = local.left;
|
|
1043
|
+
optional = true;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
if (!optional && local.type === 'Identifier') {
|
|
1047
|
+
optional = local.optional;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
return [optional, local, convertTypeAnnotation(local.typeAnnotation, param, context)];
|
|
1051
|
+
}
|
|
1052
|
+
|
|
990
1053
|
function convertComponentParameters(params, context) {
|
|
1054
|
+
if (hasNonIdentifierStringLiteralParam(params)) {
|
|
1055
|
+
return convertComponentParametersToPropsObject(params, context);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
991
1058
|
return params.reduce(([resultParams, restParam, paramsDeps], param) => {
|
|
992
1059
|
switch (param.type) {
|
|
993
1060
|
case 'ComponentParameter':
|
|
994
1061
|
{
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
if (!optional && local.type === 'Identifier') {
|
|
1004
|
-
optional = local.optional;
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
const [typeAnnotationType, typeDeps] = convertTypeAnnotation(local.typeAnnotation, param, context);
|
|
1008
|
-
|
|
1009
|
-
const resultParam = _hermesTransform.t.ComponentTypeParameter({
|
|
1010
|
-
name: (0, _hermesTransform.asDetachedNode)(param.name),
|
|
1062
|
+
const [optional, _local, [typeAnnotationType, typeDeps]] = extractParamTypeInfo(param, context);
|
|
1063
|
+
const name = (0, _hermesEstree.isStringLiteral)(param.name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(param.name.value) ? _hermesTransform.t.Identifier({
|
|
1064
|
+
name: param.name.value
|
|
1065
|
+
}) : (0, _hermesTransform.asDetachedNode)(param.name);
|
|
1066
|
+
const resultParam = inheritComments(param, _hermesTransform.t.ComponentTypeParameter({
|
|
1067
|
+
name,
|
|
1011
1068
|
typeAnnotation: typeAnnotationType,
|
|
1012
1069
|
optional
|
|
1013
|
-
});
|
|
1014
|
-
|
|
1070
|
+
}));
|
|
1015
1071
|
return [[...resultParams, resultParam], restParam, [...paramsDeps, ...typeDeps]];
|
|
1016
1072
|
}
|
|
1017
1073
|
|
|
@@ -1028,34 +1084,83 @@ function convertComponentParameters(params, context) {
|
|
|
1028
1084
|
}
|
|
1029
1085
|
|
|
1030
1086
|
const [typeAnnotationType, typeDeps] = convertTypeAnnotation(argument.typeAnnotation, argument, context);
|
|
1031
|
-
|
|
1032
|
-
const
|
|
1087
|
+
const restName = argument.type === 'Identifier' ? argument.name : 'rest';
|
|
1088
|
+
const restOptional = argument.type === 'Identifier' ? argument.optional : false;
|
|
1089
|
+
const resultRestParam = inheritComments(param, _hermesTransform.t.ComponentTypeParameter({
|
|
1033
1090
|
name: _hermesTransform.t.Identifier({
|
|
1034
|
-
name:
|
|
1091
|
+
name: restName
|
|
1035
1092
|
}),
|
|
1036
1093
|
typeAnnotation: typeAnnotationType,
|
|
1037
|
-
optional:
|
|
1038
|
-
});
|
|
1039
|
-
|
|
1094
|
+
optional: restOptional
|
|
1095
|
+
}));
|
|
1040
1096
|
return [resultParams, resultRestParam, [...paramsDeps, ...typeDeps]];
|
|
1041
1097
|
}
|
|
1042
1098
|
}
|
|
1043
1099
|
}, [[], null, []]);
|
|
1044
1100
|
}
|
|
1045
1101
|
|
|
1102
|
+
function convertComponentParametersToPropsObject(params, context) {
|
|
1103
|
+
const properties = [];
|
|
1104
|
+
const allDeps = [];
|
|
1105
|
+
|
|
1106
|
+
for (const param of params) {
|
|
1107
|
+
if (param.type === 'RestElement') {
|
|
1108
|
+
const argument = param.argument;
|
|
1109
|
+
|
|
1110
|
+
if (argument.type !== 'AssignmentPattern' && argument.type !== 'ArrayPattern' && argument.type !== 'RestElement') {
|
|
1111
|
+
const [typeAnnotationType, typeDeps] = convertTypeAnnotation(argument.typeAnnotation, argument, context);
|
|
1112
|
+
allDeps.push(...typeDeps);
|
|
1113
|
+
properties.push(_hermesTransform.t.ObjectTypeSpreadProperty({
|
|
1114
|
+
argument: typeAnnotationType
|
|
1115
|
+
}));
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
const [optional, _local, [typeAnnotationType, typeDeps]] = extractParamTypeInfo(param, context);
|
|
1122
|
+
allDeps.push(...typeDeps);
|
|
1123
|
+
properties.push(inheritComments(param, _hermesTransform.t.ObjectTypePropertySignature({
|
|
1124
|
+
key: (0, _hermesTransform.asDetachedNode)(param.name),
|
|
1125
|
+
value: typeAnnotationType,
|
|
1126
|
+
optional,
|
|
1127
|
+
static: false,
|
|
1128
|
+
variance: null
|
|
1129
|
+
})));
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const propsType = _hermesTransform.t.ObjectTypeAnnotation({
|
|
1133
|
+
inexact: false,
|
|
1134
|
+
exact: false,
|
|
1135
|
+
properties,
|
|
1136
|
+
indexers: [],
|
|
1137
|
+
callProperties: [],
|
|
1138
|
+
internalSlots: []
|
|
1139
|
+
});
|
|
1140
|
+
|
|
1141
|
+
const restParam = _hermesTransform.t.ComponentTypeParameter({
|
|
1142
|
+
name: _hermesTransform.t.Identifier({
|
|
1143
|
+
name: 'props'
|
|
1144
|
+
}),
|
|
1145
|
+
typeAnnotation: propsType,
|
|
1146
|
+
optional: false
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
return [[], restParam, allDeps];
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1046
1152
|
function convertHookDeclaration(hook, context) {
|
|
1047
1153
|
var _hook$returnType;
|
|
1048
1154
|
|
|
1049
1155
|
const id = hook.id;
|
|
1050
|
-
const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType :
|
|
1051
|
-
_hermesTransform.t.TypeAnnotation({
|
|
1156
|
+
const returnType = (_hook$returnType = hook.returnType) != null ? _hook$returnType : _hermesTransform.t.TypeAnnotation({
|
|
1052
1157
|
typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
|
|
1053
1158
|
});
|
|
1054
1159
|
const [resultReturnType, returnDeps] = convertTypeAnnotation(returnType, hook, context);
|
|
1055
1160
|
const [resultParams, restParam, paramsDeps] = convertFunctionParameters(hook.params, context);
|
|
1056
1161
|
const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(hook.typeParameters, context);
|
|
1057
1162
|
|
|
1058
|
-
const resultFunc = _hermesTransform.t.
|
|
1163
|
+
const resultFunc = _hermesTransform.t.HookTypeAnnotation({
|
|
1059
1164
|
params: resultParams,
|
|
1060
1165
|
returnType: resultReturnType,
|
|
1061
1166
|
rest: restParam,
|
|
@@ -1138,8 +1243,7 @@ function convertFunctionParameters(params, context) {
|
|
|
1138
1243
|
throw (0, _ErrorUtils.translationError)(param, `FunctionParameter: Multiple rest elements found`, context);
|
|
1139
1244
|
}
|
|
1140
1245
|
|
|
1141
|
-
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(
|
|
1142
|
-
param.argument, context, index, false);
|
|
1246
|
+
const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param.argument, context, index, false);
|
|
1143
1247
|
return [resultParams, resultParam, [...paramsDeps, ...deps]];
|
|
1144
1248
|
}
|
|
1145
1249
|
}
|
|
@@ -1200,7 +1304,6 @@ function convertTypeAnnotationType(annot, container, context) {
|
|
|
1200
1304
|
|
|
1201
1305
|
function convertTypeAnnotationTypeOrNull(annot, context) {
|
|
1202
1306
|
if (annot == null) {
|
|
1203
|
-
// $FlowFixMe[incompatible-type]
|
|
1204
1307
|
return EMPTY_TRANSLATION_RESULT;
|
|
1205
1308
|
}
|
|
1206
1309
|
|
|
@@ -1209,7 +1312,6 @@ function convertTypeAnnotationTypeOrNull(annot, context) {
|
|
|
1209
1312
|
|
|
1210
1313
|
function convertTypeParameterDeclarationOrNull(decl, context) {
|
|
1211
1314
|
if (decl == null) {
|
|
1212
|
-
// $FlowFixMe[incompatible-type]
|
|
1213
1315
|
return EMPTY_TRANSLATION_RESULT;
|
|
1214
1316
|
}
|
|
1215
1317
|
|
|
@@ -1218,7 +1320,6 @@ function convertTypeParameterDeclarationOrNull(decl, context) {
|
|
|
1218
1320
|
|
|
1219
1321
|
function convertTypeParameterInstantiationOrNull(inst, context) {
|
|
1220
1322
|
if (inst == null) {
|
|
1221
|
-
// $FlowFixMe[incompatible-type]
|
|
1222
1323
|
return EMPTY_TRANSLATION_RESULT;
|
|
1223
1324
|
}
|
|
1224
1325
|
|
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", {
|
|
@@ -37,13 +28,14 @@ var _TSDefToFlowDef = require("./TSDefToFlowDef");
|
|
|
37
28
|
|
|
38
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
39
30
|
|
|
40
|
-
async function translateFlowToFlowDef(code, prettierOptions = {}) {
|
|
31
|
+
async function translateFlowToFlowDef(code, prettierOptions = {}, opts) {
|
|
41
32
|
const {
|
|
42
33
|
ast,
|
|
43
34
|
scopeManager
|
|
44
35
|
} = await (0, _hermesTransform.parse)(code);
|
|
45
36
|
const [flowDefAst, mutatedCode] = (0, _flowToFlowDef.default)(ast, code, scopeManager, {
|
|
46
|
-
recoverFromErrors: true
|
|
37
|
+
recoverFromErrors: true,
|
|
38
|
+
mungeUnderscores: opts == null ? void 0 : opts.mungeUnderscores
|
|
47
39
|
});
|
|
48
40
|
return (0, _hermesTransform.print)(flowDefAst, mutatedCode, prettierOptions);
|
|
49
41
|
}
|
|
@@ -61,8 +53,7 @@ async function translateFlowDefToTSDef(code, prettierOptions = {}) {
|
|
|
61
53
|
const [tsAST, mutatedCode] = (0, _flowDefToTSDef.flowDefToTSDef)(code, ast, scopeManager, {
|
|
62
54
|
recoverFromErrors: true
|
|
63
55
|
});
|
|
64
|
-
return (0, _hermesTransform.print)(
|
|
65
|
-
tsAST, mutatedCode, { ...prettierOptions
|
|
56
|
+
return (0, _hermesTransform.print)(tsAST, mutatedCode, { ...prettierOptions
|
|
66
57
|
}, _visitorKeys.visitorKeys);
|
|
67
58
|
}
|
|
68
59
|
|
|
@@ -74,18 +65,6 @@ async function translateFlowToJS(code, prettierOptions = {}) {
|
|
|
74
65
|
const jsAST = (0, _flowToJS.flowToJS)(ast, code, scopeManager);
|
|
75
66
|
return (0, _hermesTransform.print)(jsAST, code, prettierOptions);
|
|
76
67
|
}
|
|
77
|
-
/**
|
|
78
|
-
* This translator is very experimental and unstable.
|
|
79
|
-
*
|
|
80
|
-
* It is not written with productionizing it in mind, but instead used to evaluate how close Flow
|
|
81
|
-
* is to TypeScript.
|
|
82
|
-
*
|
|
83
|
-
* If you are going to use it anyways, you agree that you are calling a potentially broken function
|
|
84
|
-
* without any guarantee.
|
|
85
|
-
*
|
|
86
|
-
* @deprecated
|
|
87
|
-
*/
|
|
88
|
-
|
|
89
68
|
|
|
90
69
|
async function unstable_translateTSDefToFlowDef(code, prettierOptions = {}) {
|
|
91
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
|
},
|