flow-api-translator 0.33.2 → 0.34.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 +1 -1
- package/dist/TSDefToFlowDef.js.flow +1 -1
- package/dist/flowDefToTSDef.js +82 -19
- package/dist/flowDefToTSDef.js.flow +93 -22
- package/dist/flowToFlowDef.js +7 -7
- package/dist/flowToFlowDef.js.flow +9 -6
- package/dist/src/TSDefToFlowDef.js +1 -1
- package/dist/src/flowDefToTSDef.js +82 -19
- package/dist/src/flowToFlowDef.js +7 -7
- package/package.json +5 -5
package/dist/TSDefToFlowDef.js
CHANGED
|
@@ -525,7 +525,7 @@ const getTransforms = (originalCode, opts) => {
|
|
|
525
525
|
}),
|
|
526
526
|
importKind: // `import type React from 'react'` in TS means `import typeof React from react` in Flow
|
|
527
527
|
specifiers.some(s => s.type === 'ImportDefaultSpecifier') && node.importKind === 'type' ? 'typeof' : node.importKind,
|
|
528
|
-
|
|
528
|
+
attributes: [],
|
|
529
529
|
specifiers
|
|
530
530
|
});
|
|
531
531
|
}
|
package/dist/flowDefToTSDef.js
CHANGED
|
@@ -720,11 +720,21 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
720
720
|
case 'ComponentTypeAnnotation':
|
|
721
721
|
return transform.ComponentTypeAnnotation(node);
|
|
722
722
|
|
|
723
|
+
case 'NeverTypeAnnotation':
|
|
724
|
+
return transform.NeverTypeAnnotation(node);
|
|
725
|
+
|
|
726
|
+
case 'UndefinedTypeAnnotation':
|
|
727
|
+
return transform.UndefinedTypeAnnotation(node);
|
|
728
|
+
|
|
729
|
+
case 'UnknownTypeAnnotation':
|
|
730
|
+
return transform.UnknownTypeAnnotation(node);
|
|
731
|
+
|
|
723
732
|
default:
|
|
724
733
|
throw unexpectedTranslationError(node, `Unhandled type ${node.type}`);
|
|
725
734
|
}
|
|
726
735
|
};
|
|
727
736
|
|
|
737
|
+
const wellKnownSymbols = new Set(['iterator', 'asyncIterator', 'dispose', 'asyncDispose']);
|
|
728
738
|
const transform = {
|
|
729
739
|
AnyTypeAnnotation(_node) {
|
|
730
740
|
return {
|
|
@@ -890,7 +900,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
890
900
|
if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
|
|
891
901
|
const name = _key.name.slice(2);
|
|
892
902
|
|
|
893
|
-
if (
|
|
903
|
+
if (wellKnownSymbols.has(name)) {
|
|
894
904
|
return [{
|
|
895
905
|
type: 'MemberExpression',
|
|
896
906
|
computed: false,
|
|
@@ -1258,7 +1268,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1258
1268
|
return {
|
|
1259
1269
|
type: 'ExportNamedDeclaration',
|
|
1260
1270
|
loc: DUMMY_LOC,
|
|
1261
|
-
// flow does not currently support
|
|
1271
|
+
// flow does not currently support attributes
|
|
1262
1272
|
assertions: [],
|
|
1263
1273
|
declaration: null,
|
|
1264
1274
|
// flow does not support declared type exports with specifiers
|
|
@@ -1341,7 +1351,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1341
1351
|
return [{
|
|
1342
1352
|
type: 'ExportNamedDeclaration',
|
|
1343
1353
|
loc: DUMMY_LOC,
|
|
1344
|
-
// flow does not currently support
|
|
1354
|
+
// flow does not currently support attributes
|
|
1345
1355
|
assertions: [],
|
|
1346
1356
|
declaration,
|
|
1347
1357
|
exportKind,
|
|
@@ -1376,7 +1386,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1376
1386
|
loc: DUMMY_LOC,
|
|
1377
1387
|
specifiers: [],
|
|
1378
1388
|
exportKind: 'type',
|
|
1379
|
-
// flow does not currently support
|
|
1389
|
+
// flow does not currently support attributes
|
|
1380
1390
|
assertions: []
|
|
1381
1391
|
}];
|
|
1382
1392
|
}
|
|
@@ -1385,7 +1395,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1385
1395
|
const exportNamedDeclaration = {
|
|
1386
1396
|
type: 'ExportNamedDeclaration',
|
|
1387
1397
|
loc: DUMMY_LOC,
|
|
1388
|
-
// flow does not currently support
|
|
1398
|
+
// flow does not currently support attributes
|
|
1389
1399
|
assertions: [],
|
|
1390
1400
|
declaration,
|
|
1391
1401
|
exportKind,
|
|
@@ -1399,7 +1409,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1399
1409
|
return {
|
|
1400
1410
|
type: 'ExportNamedDeclaration',
|
|
1401
1411
|
loc: DUMMY_LOC,
|
|
1402
|
-
// flow does not currently support
|
|
1412
|
+
// flow does not currently support attributes
|
|
1403
1413
|
assertions: [],
|
|
1404
1414
|
declaration: null,
|
|
1405
1415
|
// flow does not support declared type exports with a source
|
|
@@ -1709,7 +1719,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1709
1719
|
return {
|
|
1710
1720
|
type: 'ExportAllDeclaration',
|
|
1711
1721
|
loc: DUMMY_LOC,
|
|
1712
|
-
// flow does not currently support import/export
|
|
1722
|
+
// flow does not currently support import/export attributes
|
|
1713
1723
|
assertions: [],
|
|
1714
1724
|
exportKind: node.exportKind,
|
|
1715
1725
|
source: transform.StringLiteral(node.source),
|
|
@@ -1723,7 +1733,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1723
1733
|
return {
|
|
1724
1734
|
type: 'ExportNamedDeclaration',
|
|
1725
1735
|
loc: DUMMY_LOC,
|
|
1726
|
-
// flow does not currently support import/export
|
|
1736
|
+
// flow does not currently support import/export attributes
|
|
1727
1737
|
assertions: [],
|
|
1728
1738
|
declaration: null,
|
|
1729
1739
|
exportKind: node.exportKind,
|
|
@@ -2972,7 +2982,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2972
2982
|
const out = specifiers.length ? [{
|
|
2973
2983
|
type: 'ImportDeclaration',
|
|
2974
2984
|
loc: DUMMY_LOC,
|
|
2975
|
-
assertions: node.
|
|
2985
|
+
assertions: node.attributes.map(transform.ImportAttribute),
|
|
2976
2986
|
importKind: importKind === 'typeof' ? 'type' : importKind != null ? importKind : 'value',
|
|
2977
2987
|
source: transform.StringLiteral(node.source),
|
|
2978
2988
|
specifiers
|
|
@@ -3417,20 +3427,50 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3417
3427
|
ObjectTypeProperty(node) {
|
|
3418
3428
|
var _node$variance3;
|
|
3419
3429
|
|
|
3420
|
-
const key =
|
|
3430
|
+
const [key, computed] = (() => {
|
|
3431
|
+
if (node.key.type === 'Identifier' && node.key.name.startsWith('@@')) {
|
|
3432
|
+
const name = node.key.name.slice(2);
|
|
3421
3433
|
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3434
|
+
if (node.method === true && wellKnownSymbols.has(name)) {
|
|
3435
|
+
return [{
|
|
3436
|
+
type: 'MemberExpression',
|
|
3437
|
+
computed: false,
|
|
3438
|
+
object: {
|
|
3439
|
+
type: 'Identifier',
|
|
3440
|
+
name: 'Symbol',
|
|
3441
|
+
optional: false,
|
|
3442
|
+
loc: DUMMY_LOC
|
|
3443
|
+
},
|
|
3444
|
+
optional: false,
|
|
3445
|
+
property: {
|
|
3446
|
+
type: 'Identifier',
|
|
3447
|
+
name,
|
|
3448
|
+
optional: false,
|
|
3449
|
+
loc: DUMMY_LOC
|
|
3450
|
+
},
|
|
3451
|
+
loc: DUMMY_LOC
|
|
3452
|
+
}, true];
|
|
3453
|
+
}
|
|
3454
|
+
}
|
|
3455
|
+
|
|
3456
|
+
const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : node.key.literalType === 'string' ? transform.StringLiteral(node.key) : null;
|
|
3457
|
+
|
|
3458
|
+
if (key == null) {
|
|
3459
|
+
throw unexpectedTranslationError(node, 'Unsupported key type');
|
|
3460
|
+
}
|
|
3461
|
+
|
|
3462
|
+
return [key, false];
|
|
3463
|
+
})();
|
|
3425
3464
|
|
|
3426
3465
|
if (node.method === true) {
|
|
3427
3466
|
// flow has just one node for all object properties and relies upon the method flag
|
|
3428
3467
|
// TS has separate nodes for methods and properties
|
|
3429
|
-
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3468
|
+
const func = transform.FunctionTypeAnnotation(node.value); // $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3469
|
+
|
|
3430
3470
|
return {
|
|
3431
3471
|
type: 'TSMethodSignature',
|
|
3432
3472
|
loc: DUMMY_LOC,
|
|
3433
|
-
computed
|
|
3473
|
+
computed,
|
|
3434
3474
|
key,
|
|
3435
3475
|
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
3436
3476
|
optional: node.optional,
|
|
@@ -3444,11 +3484,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3444
3484
|
if (node.kind === 'get' || node.kind === 'set') {
|
|
3445
3485
|
// flow treats getters/setters as true property signatures (method === false)
|
|
3446
3486
|
// TS treats them as method signatures
|
|
3447
|
-
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3487
|
+
const func = transform.FunctionTypeAnnotation(node.value); // $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3488
|
+
|
|
3448
3489
|
return {
|
|
3449
3490
|
type: 'TSMethodSignature',
|
|
3450
3491
|
loc: DUMMY_LOC,
|
|
3451
|
-
computed
|
|
3492
|
+
computed,
|
|
3452
3493
|
key,
|
|
3453
3494
|
kind: node.kind,
|
|
3454
3495
|
optional: false,
|
|
@@ -3459,12 +3500,13 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3459
3500
|
// TS accessors cannot have type parameters
|
|
3460
3501
|
typeParameters: undefined
|
|
3461
3502
|
};
|
|
3462
|
-
}
|
|
3503
|
+
} // $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3504
|
+
|
|
3463
3505
|
|
|
3464
3506
|
return {
|
|
3465
3507
|
type: 'TSPropertySignature',
|
|
3466
3508
|
loc: DUMMY_LOC,
|
|
3467
|
-
computed
|
|
3509
|
+
computed,
|
|
3468
3510
|
key,
|
|
3469
3511
|
optional: node.optional,
|
|
3470
3512
|
readonly: ((_node$variance3 = node.variance) == null ? void 0 : _node$variance3.kind) === 'plus',
|
|
@@ -3756,6 +3798,27 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3756
3798
|
};
|
|
3757
3799
|
},
|
|
3758
3800
|
|
|
3801
|
+
NeverTypeAnnotation(_node) {
|
|
3802
|
+
return {
|
|
3803
|
+
type: 'TSNeverKeyword',
|
|
3804
|
+
loc: DUMMY_LOC
|
|
3805
|
+
};
|
|
3806
|
+
},
|
|
3807
|
+
|
|
3808
|
+
UndefinedTypeAnnotation(_node) {
|
|
3809
|
+
return {
|
|
3810
|
+
type: 'TSUndefinedKeyword',
|
|
3811
|
+
loc: DUMMY_LOC
|
|
3812
|
+
};
|
|
3813
|
+
},
|
|
3814
|
+
|
|
3815
|
+
UnknownTypeAnnotation(_node) {
|
|
3816
|
+
return {
|
|
3817
|
+
type: 'TSUnknownKeyword',
|
|
3818
|
+
loc: DUMMY_LOC
|
|
3819
|
+
};
|
|
3820
|
+
},
|
|
3821
|
+
|
|
3759
3822
|
ConditionalTypeAnnotation(node) {
|
|
3760
3823
|
return {
|
|
3761
3824
|
type: 'TSConditionalType',
|
|
@@ -755,11 +755,24 @@ const getTransforms = (
|
|
|
755
755
|
return transform.TypeOperator(node);
|
|
756
756
|
case 'ComponentTypeAnnotation':
|
|
757
757
|
return transform.ComponentTypeAnnotation(node);
|
|
758
|
+
case 'NeverTypeAnnotation':
|
|
759
|
+
return transform.NeverTypeAnnotation(node);
|
|
760
|
+
case 'UndefinedTypeAnnotation':
|
|
761
|
+
return transform.UndefinedTypeAnnotation(node);
|
|
762
|
+
case 'UnknownTypeAnnotation':
|
|
763
|
+
return transform.UnknownTypeAnnotation(node);
|
|
758
764
|
default:
|
|
759
765
|
throw unexpectedTranslationError(node, `Unhandled type ${node.type}`);
|
|
760
766
|
}
|
|
761
767
|
};
|
|
762
768
|
|
|
769
|
+
const wellKnownSymbols = new Set([
|
|
770
|
+
'iterator',
|
|
771
|
+
'asyncIterator',
|
|
772
|
+
'dispose',
|
|
773
|
+
'asyncDispose',
|
|
774
|
+
]);
|
|
775
|
+
|
|
763
776
|
const transform = {
|
|
764
777
|
AnyTypeAnnotation(
|
|
765
778
|
_node: FlowESTree.AnyTypeAnnotation,
|
|
@@ -938,7 +951,7 @@ const getTransforms = (
|
|
|
938
951
|
const _key = member.key;
|
|
939
952
|
if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
|
|
940
953
|
const name = _key.name.slice(2);
|
|
941
|
-
if (
|
|
954
|
+
if (wellKnownSymbols.has(name)) {
|
|
942
955
|
return [
|
|
943
956
|
{
|
|
944
957
|
type: 'MemberExpression',
|
|
@@ -1367,7 +1380,7 @@ const getTransforms = (
|
|
|
1367
1380
|
return ({
|
|
1368
1381
|
type: 'ExportNamedDeclaration',
|
|
1369
1382
|
loc: DUMMY_LOC,
|
|
1370
|
-
// flow does not currently support
|
|
1383
|
+
// flow does not currently support attributes
|
|
1371
1384
|
assertions: [],
|
|
1372
1385
|
declaration: null,
|
|
1373
1386
|
// flow does not support declared type exports with specifiers
|
|
@@ -1464,7 +1477,7 @@ const getTransforms = (
|
|
|
1464
1477
|
{
|
|
1465
1478
|
type: 'ExportNamedDeclaration',
|
|
1466
1479
|
loc: DUMMY_LOC,
|
|
1467
|
-
// flow does not currently support
|
|
1480
|
+
// flow does not currently support attributes
|
|
1468
1481
|
assertions: [],
|
|
1469
1482
|
declaration,
|
|
1470
1483
|
exportKind,
|
|
@@ -1500,7 +1513,7 @@ const getTransforms = (
|
|
|
1500
1513
|
loc: DUMMY_LOC,
|
|
1501
1514
|
specifiers: [],
|
|
1502
1515
|
exportKind: 'type',
|
|
1503
|
-
// flow does not currently support
|
|
1516
|
+
// flow does not currently support attributes
|
|
1504
1517
|
assertions: [],
|
|
1505
1518
|
},
|
|
1506
1519
|
];
|
|
@@ -1511,7 +1524,7 @@ const getTransforms = (
|
|
|
1511
1524
|
{
|
|
1512
1525
|
type: 'ExportNamedDeclaration',
|
|
1513
1526
|
loc: DUMMY_LOC,
|
|
1514
|
-
// flow does not currently support
|
|
1527
|
+
// flow does not currently support attributes
|
|
1515
1528
|
assertions: [],
|
|
1516
1529
|
declaration,
|
|
1517
1530
|
exportKind,
|
|
@@ -1526,7 +1539,7 @@ const getTransforms = (
|
|
|
1526
1539
|
return ({
|
|
1527
1540
|
type: 'ExportNamedDeclaration',
|
|
1528
1541
|
loc: DUMMY_LOC,
|
|
1529
|
-
// flow does not currently support
|
|
1542
|
+
// flow does not currently support attributes
|
|
1530
1543
|
assertions: [],
|
|
1531
1544
|
declaration: null,
|
|
1532
1545
|
// flow does not support declared type exports with a source
|
|
@@ -1899,7 +1912,7 @@ const getTransforms = (
|
|
|
1899
1912
|
return {
|
|
1900
1913
|
type: 'ExportAllDeclaration',
|
|
1901
1914
|
loc: DUMMY_LOC,
|
|
1902
|
-
// flow does not currently support import/export
|
|
1915
|
+
// flow does not currently support import/export attributes
|
|
1903
1916
|
assertions: [],
|
|
1904
1917
|
exportKind: node.exportKind,
|
|
1905
1918
|
source: transform.StringLiteral(node.source),
|
|
@@ -1917,7 +1930,7 @@ const getTransforms = (
|
|
|
1917
1930
|
return {
|
|
1918
1931
|
type: 'ExportNamedDeclaration',
|
|
1919
1932
|
loc: DUMMY_LOC,
|
|
1920
|
-
// flow does not currently support import/export
|
|
1933
|
+
// flow does not currently support import/export attributes
|
|
1921
1934
|
assertions: [],
|
|
1922
1935
|
declaration: null,
|
|
1923
1936
|
exportKind: node.exportKind,
|
|
@@ -3221,7 +3234,7 @@ const getTransforms = (
|
|
|
3221
3234
|
{
|
|
3222
3235
|
type: 'ImportDeclaration',
|
|
3223
3236
|
loc: DUMMY_LOC,
|
|
3224
|
-
assertions: node.
|
|
3237
|
+
assertions: node.attributes.map(transform.ImportAttribute),
|
|
3225
3238
|
importKind:
|
|
3226
3239
|
importKind === 'typeof' ? 'type' : (importKind ?? 'value'),
|
|
3227
3240
|
source: transform.StringLiteral(node.source),
|
|
@@ -3722,25 +3735,57 @@ const getTransforms = (
|
|
|
3722
3735
|
ObjectTypeProperty(
|
|
3723
3736
|
node: FlowESTree.ObjectTypeProperty,
|
|
3724
3737
|
): TSESTree.TSPropertySignature | TSESTree.TSMethodSignature {
|
|
3725
|
-
const key =
|
|
3726
|
-
node.key.type === 'Identifier'
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3738
|
+
const [key, computed] = ((): [TSESTree.PropertyName, boolean] => {
|
|
3739
|
+
if (node.key.type === 'Identifier' && node.key.name.startsWith('@@')) {
|
|
3740
|
+
const name = node.key.name.slice(2);
|
|
3741
|
+
if (node.method === true && wellKnownSymbols.has(name)) {
|
|
3742
|
+
return [
|
|
3743
|
+
{
|
|
3744
|
+
type: 'MemberExpression',
|
|
3745
|
+
computed: false,
|
|
3746
|
+
object: {
|
|
3747
|
+
type: 'Identifier',
|
|
3748
|
+
name: 'Symbol',
|
|
3749
|
+
optional: false,
|
|
3750
|
+
loc: DUMMY_LOC,
|
|
3751
|
+
},
|
|
3752
|
+
optional: false,
|
|
3753
|
+
property: {
|
|
3754
|
+
type: 'Identifier',
|
|
3755
|
+
name,
|
|
3756
|
+
optional: false,
|
|
3757
|
+
loc: DUMMY_LOC,
|
|
3758
|
+
},
|
|
3759
|
+
loc: DUMMY_LOC,
|
|
3760
|
+
},
|
|
3761
|
+
true,
|
|
3762
|
+
];
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
|
|
3766
|
+
const key =
|
|
3767
|
+
node.key.type === 'Identifier'
|
|
3768
|
+
? transform.Identifier(node.key)
|
|
3769
|
+
: node.key.literalType === 'string'
|
|
3770
|
+
? transform.StringLiteral(node.key)
|
|
3771
|
+
: null;
|
|
3772
|
+
|
|
3773
|
+
if (key == null) {
|
|
3774
|
+
throw unexpectedTranslationError(node, 'Unsupported key type');
|
|
3775
|
+
}
|
|
3776
|
+
|
|
3777
|
+
return [key, false];
|
|
3778
|
+
})();
|
|
3735
3779
|
|
|
3736
3780
|
if (node.method === true) {
|
|
3737
3781
|
// flow has just one node for all object properties and relies upon the method flag
|
|
3738
3782
|
// TS has separate nodes for methods and properties
|
|
3739
3783
|
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3784
|
+
// $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3740
3785
|
return {
|
|
3741
3786
|
type: 'TSMethodSignature',
|
|
3742
3787
|
loc: DUMMY_LOC,
|
|
3743
|
-
computed
|
|
3788
|
+
computed,
|
|
3744
3789
|
key,
|
|
3745
3790
|
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
3746
3791
|
optional: node.optional,
|
|
@@ -3755,10 +3800,11 @@ const getTransforms = (
|
|
|
3755
3800
|
// flow treats getters/setters as true property signatures (method === false)
|
|
3756
3801
|
// TS treats them as method signatures
|
|
3757
3802
|
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3803
|
+
// $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3758
3804
|
return {
|
|
3759
3805
|
type: 'TSMethodSignature',
|
|
3760
3806
|
loc: DUMMY_LOC,
|
|
3761
|
-
computed
|
|
3807
|
+
computed,
|
|
3762
3808
|
key,
|
|
3763
3809
|
kind: node.kind,
|
|
3764
3810
|
optional: false,
|
|
@@ -3771,10 +3817,11 @@ const getTransforms = (
|
|
|
3771
3817
|
};
|
|
3772
3818
|
}
|
|
3773
3819
|
|
|
3820
|
+
// $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3774
3821
|
return {
|
|
3775
3822
|
type: 'TSPropertySignature',
|
|
3776
3823
|
loc: DUMMY_LOC,
|
|
3777
|
-
computed
|
|
3824
|
+
computed,
|
|
3778
3825
|
key,
|
|
3779
3826
|
optional: node.optional,
|
|
3780
3827
|
readonly: node.variance?.kind === 'plus',
|
|
@@ -4105,6 +4152,30 @@ const getTransforms = (
|
|
|
4105
4152
|
loc: DUMMY_LOC,
|
|
4106
4153
|
};
|
|
4107
4154
|
},
|
|
4155
|
+
NeverTypeAnnotation(
|
|
4156
|
+
_node: FlowESTree.NeverTypeAnnotation,
|
|
4157
|
+
): TSESTree.TSNeverKeyword {
|
|
4158
|
+
return {
|
|
4159
|
+
type: 'TSNeverKeyword',
|
|
4160
|
+
loc: DUMMY_LOC,
|
|
4161
|
+
};
|
|
4162
|
+
},
|
|
4163
|
+
UndefinedTypeAnnotation(
|
|
4164
|
+
_node: FlowESTree.UndefinedTypeAnnotation,
|
|
4165
|
+
): TSESTree.TSUndefinedKeyword {
|
|
4166
|
+
return {
|
|
4167
|
+
type: 'TSUndefinedKeyword',
|
|
4168
|
+
loc: DUMMY_LOC,
|
|
4169
|
+
};
|
|
4170
|
+
},
|
|
4171
|
+
UnknownTypeAnnotation(
|
|
4172
|
+
_node: FlowESTree.UnknownTypeAnnotation,
|
|
4173
|
+
): TSESTree.TSUnknownKeyword {
|
|
4174
|
+
return {
|
|
4175
|
+
type: 'TSUnknownKeyword',
|
|
4176
|
+
loc: DUMMY_LOC,
|
|
4177
|
+
};
|
|
4178
|
+
},
|
|
4108
4179
|
ConditionalTypeAnnotation(
|
|
4109
4180
|
node: FlowESTree.ConditionalTypeAnnotation,
|
|
4110
4181
|
): TSESTree.TSConditionalType {
|
package/dist/flowToFlowDef.js
CHANGED
|
@@ -249,7 +249,7 @@ function stripUnusedDefs(detachedStmt, usedDeps, context) {
|
|
|
249
249
|
specifiers: resultSpecfiers,
|
|
250
250
|
importKind: stmt.importKind,
|
|
251
251
|
source: stmt.source,
|
|
252
|
-
|
|
252
|
+
attributes: stmt.attributes
|
|
253
253
|
});
|
|
254
254
|
}
|
|
255
255
|
|
|
@@ -727,15 +727,15 @@ function convertVariableDeclaration(stmt, context) {
|
|
|
727
727
|
}
|
|
728
728
|
|
|
729
729
|
function convertImportDeclaration(stmt, context) {
|
|
730
|
-
if (stmt.
|
|
731
|
-
throw (0, _ErrorUtils.translationError)(stmt, 'ImportDeclaration:
|
|
730
|
+
if (stmt.attributes.length > 0) {
|
|
731
|
+
throw (0, _ErrorUtils.translationError)(stmt, 'ImportDeclaration: attributes not supported', context);
|
|
732
732
|
}
|
|
733
733
|
|
|
734
734
|
return [_hermesTransform.t.ImportDeclaration({
|
|
735
735
|
specifiers: stmt.specifiers,
|
|
736
736
|
importKind: stmt.importKind,
|
|
737
737
|
source: stmt.source,
|
|
738
|
-
|
|
738
|
+
attributes: []
|
|
739
739
|
}), []];
|
|
740
740
|
}
|
|
741
741
|
|
|
@@ -1147,12 +1147,12 @@ function convertFunctionParameters(params, context) {
|
|
|
1147
1147
|
}
|
|
1148
1148
|
|
|
1149
1149
|
function convertBindingNameToFunctionTypeParam(pat, context, index, isAssignment) {
|
|
1150
|
-
const name = pat.type === 'Identifier' ? pat.name : `$$PARAM_${index}$$`;
|
|
1150
|
+
const name = pat.type === 'Identifier' && pat.name != null ? pat.name : `$$PARAM_${index}$$`;
|
|
1151
1151
|
const [resultParamTypeAnnotation, paramDeps] = convertTypeAnnotation(pat.typeAnnotation, pat, context);
|
|
1152
1152
|
return [_hermesTransform.t.FunctionTypeParam({
|
|
1153
|
-
name:
|
|
1153
|
+
name: _hermesTransform.t.Identifier({
|
|
1154
1154
|
name
|
|
1155
|
-
})
|
|
1155
|
+
}),
|
|
1156
1156
|
typeAnnotation: resultParamTypeAnnotation,
|
|
1157
1157
|
optional: isAssignment || (pat.type === 'Identifier' ? pat.optional : false)
|
|
1158
1158
|
}), paramDeps];
|
|
@@ -357,7 +357,7 @@ function stripUnusedDefs(
|
|
|
357
357
|
specifiers: resultSpecfiers,
|
|
358
358
|
importKind: stmt.importKind,
|
|
359
359
|
source: stmt.source,
|
|
360
|
-
|
|
360
|
+
attributes: stmt.attributes,
|
|
361
361
|
});
|
|
362
362
|
}
|
|
363
363
|
return detachedStmt;
|
|
@@ -940,10 +940,10 @@ function convertImportDeclaration(
|
|
|
940
940
|
stmt: ImportDeclaration,
|
|
941
941
|
context: TranslationContext,
|
|
942
942
|
): TranslatedResult<ImportDeclaration> {
|
|
943
|
-
if (stmt.
|
|
943
|
+
if (stmt.attributes.length > 0) {
|
|
944
944
|
throw translationError(
|
|
945
945
|
stmt,
|
|
946
|
-
'ImportDeclaration:
|
|
946
|
+
'ImportDeclaration: attributes not supported',
|
|
947
947
|
context,
|
|
948
948
|
);
|
|
949
949
|
}
|
|
@@ -953,7 +953,7 @@ function convertImportDeclaration(
|
|
|
953
953
|
specifiers: stmt.specifiers,
|
|
954
954
|
importKind: stmt.importKind,
|
|
955
955
|
source: stmt.source,
|
|
956
|
-
|
|
956
|
+
attributes: [],
|
|
957
957
|
}),
|
|
958
958
|
[],
|
|
959
959
|
];
|
|
@@ -1623,7 +1623,10 @@ function convertBindingNameToFunctionTypeParam(
|
|
|
1623
1623
|
index: number,
|
|
1624
1624
|
isAssignment: boolean,
|
|
1625
1625
|
): TranslatedResult<FunctionTypeParam> {
|
|
1626
|
-
const name =
|
|
1626
|
+
const name =
|
|
1627
|
+
pat.type === 'Identifier' && pat.name != null
|
|
1628
|
+
? pat.name
|
|
1629
|
+
: `$$PARAM_${index}$$`;
|
|
1627
1630
|
const [resultParamTypeAnnotation, paramDeps] = convertTypeAnnotation(
|
|
1628
1631
|
pat.typeAnnotation,
|
|
1629
1632
|
pat,
|
|
@@ -1631,7 +1634,7 @@ function convertBindingNameToFunctionTypeParam(
|
|
|
1631
1634
|
);
|
|
1632
1635
|
return [
|
|
1633
1636
|
t.FunctionTypeParam({
|
|
1634
|
-
name:
|
|
1637
|
+
name: t.Identifier({name}),
|
|
1635
1638
|
typeAnnotation: resultParamTypeAnnotation,
|
|
1636
1639
|
optional:
|
|
1637
1640
|
isAssignment || (pat.type === 'Identifier' ? pat.optional : false),
|
|
@@ -525,7 +525,7 @@ const getTransforms = (originalCode, opts) => {
|
|
|
525
525
|
}),
|
|
526
526
|
importKind: // `import type React from 'react'` in TS means `import typeof React from react` in Flow
|
|
527
527
|
specifiers.some(s => s.type === 'ImportDefaultSpecifier') && node.importKind === 'type' ? 'typeof' : node.importKind,
|
|
528
|
-
|
|
528
|
+
attributes: [],
|
|
529
529
|
specifiers
|
|
530
530
|
});
|
|
531
531
|
}
|
|
@@ -720,11 +720,21 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
720
720
|
case 'ComponentTypeAnnotation':
|
|
721
721
|
return transform.ComponentTypeAnnotation(node);
|
|
722
722
|
|
|
723
|
+
case 'NeverTypeAnnotation':
|
|
724
|
+
return transform.NeverTypeAnnotation(node);
|
|
725
|
+
|
|
726
|
+
case 'UndefinedTypeAnnotation':
|
|
727
|
+
return transform.UndefinedTypeAnnotation(node);
|
|
728
|
+
|
|
729
|
+
case 'UnknownTypeAnnotation':
|
|
730
|
+
return transform.UnknownTypeAnnotation(node);
|
|
731
|
+
|
|
723
732
|
default:
|
|
724
733
|
throw unexpectedTranslationError(node, `Unhandled type ${node.type}`);
|
|
725
734
|
}
|
|
726
735
|
};
|
|
727
736
|
|
|
737
|
+
const wellKnownSymbols = new Set(['iterator', 'asyncIterator', 'dispose', 'asyncDispose']);
|
|
728
738
|
const transform = {
|
|
729
739
|
AnyTypeAnnotation(_node) {
|
|
730
740
|
return {
|
|
@@ -890,7 +900,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
890
900
|
if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
|
|
891
901
|
const name = _key.name.slice(2);
|
|
892
902
|
|
|
893
|
-
if (
|
|
903
|
+
if (wellKnownSymbols.has(name)) {
|
|
894
904
|
return [{
|
|
895
905
|
type: 'MemberExpression',
|
|
896
906
|
computed: false,
|
|
@@ -1258,7 +1268,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1258
1268
|
return {
|
|
1259
1269
|
type: 'ExportNamedDeclaration',
|
|
1260
1270
|
loc: DUMMY_LOC,
|
|
1261
|
-
// flow does not currently support
|
|
1271
|
+
// flow does not currently support attributes
|
|
1262
1272
|
assertions: [],
|
|
1263
1273
|
declaration: null,
|
|
1264
1274
|
// flow does not support declared type exports with specifiers
|
|
@@ -1341,7 +1351,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1341
1351
|
return [{
|
|
1342
1352
|
type: 'ExportNamedDeclaration',
|
|
1343
1353
|
loc: DUMMY_LOC,
|
|
1344
|
-
// flow does not currently support
|
|
1354
|
+
// flow does not currently support attributes
|
|
1345
1355
|
assertions: [],
|
|
1346
1356
|
declaration,
|
|
1347
1357
|
exportKind,
|
|
@@ -1376,7 +1386,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1376
1386
|
loc: DUMMY_LOC,
|
|
1377
1387
|
specifiers: [],
|
|
1378
1388
|
exportKind: 'type',
|
|
1379
|
-
// flow does not currently support
|
|
1389
|
+
// flow does not currently support attributes
|
|
1380
1390
|
assertions: []
|
|
1381
1391
|
}];
|
|
1382
1392
|
}
|
|
@@ -1385,7 +1395,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1385
1395
|
const exportNamedDeclaration = {
|
|
1386
1396
|
type: 'ExportNamedDeclaration',
|
|
1387
1397
|
loc: DUMMY_LOC,
|
|
1388
|
-
// flow does not currently support
|
|
1398
|
+
// flow does not currently support attributes
|
|
1389
1399
|
assertions: [],
|
|
1390
1400
|
declaration,
|
|
1391
1401
|
exportKind,
|
|
@@ -1399,7 +1409,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1399
1409
|
return {
|
|
1400
1410
|
type: 'ExportNamedDeclaration',
|
|
1401
1411
|
loc: DUMMY_LOC,
|
|
1402
|
-
// flow does not currently support
|
|
1412
|
+
// flow does not currently support attributes
|
|
1403
1413
|
assertions: [],
|
|
1404
1414
|
declaration: null,
|
|
1405
1415
|
// flow does not support declared type exports with a source
|
|
@@ -1709,7 +1719,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1709
1719
|
return {
|
|
1710
1720
|
type: 'ExportAllDeclaration',
|
|
1711
1721
|
loc: DUMMY_LOC,
|
|
1712
|
-
// flow does not currently support import/export
|
|
1722
|
+
// flow does not currently support import/export attributes
|
|
1713
1723
|
assertions: [],
|
|
1714
1724
|
exportKind: node.exportKind,
|
|
1715
1725
|
source: transform.StringLiteral(node.source),
|
|
@@ -1723,7 +1733,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1723
1733
|
return {
|
|
1724
1734
|
type: 'ExportNamedDeclaration',
|
|
1725
1735
|
loc: DUMMY_LOC,
|
|
1726
|
-
// flow does not currently support import/export
|
|
1736
|
+
// flow does not currently support import/export attributes
|
|
1727
1737
|
assertions: [],
|
|
1728
1738
|
declaration: null,
|
|
1729
1739
|
exportKind: node.exportKind,
|
|
@@ -2972,7 +2982,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2972
2982
|
const out = specifiers.length ? [{
|
|
2973
2983
|
type: 'ImportDeclaration',
|
|
2974
2984
|
loc: DUMMY_LOC,
|
|
2975
|
-
assertions: node.
|
|
2985
|
+
assertions: node.attributes.map(transform.ImportAttribute),
|
|
2976
2986
|
importKind: importKind === 'typeof' ? 'type' : importKind != null ? importKind : 'value',
|
|
2977
2987
|
source: transform.StringLiteral(node.source),
|
|
2978
2988
|
specifiers
|
|
@@ -3417,20 +3427,50 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3417
3427
|
ObjectTypeProperty(node) {
|
|
3418
3428
|
var _node$variance3;
|
|
3419
3429
|
|
|
3420
|
-
const key =
|
|
3430
|
+
const [key, computed] = (() => {
|
|
3431
|
+
if (node.key.type === 'Identifier' && node.key.name.startsWith('@@')) {
|
|
3432
|
+
const name = node.key.name.slice(2);
|
|
3421
3433
|
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3434
|
+
if (node.method === true && wellKnownSymbols.has(name)) {
|
|
3435
|
+
return [{
|
|
3436
|
+
type: 'MemberExpression',
|
|
3437
|
+
computed: false,
|
|
3438
|
+
object: {
|
|
3439
|
+
type: 'Identifier',
|
|
3440
|
+
name: 'Symbol',
|
|
3441
|
+
optional: false,
|
|
3442
|
+
loc: DUMMY_LOC
|
|
3443
|
+
},
|
|
3444
|
+
optional: false,
|
|
3445
|
+
property: {
|
|
3446
|
+
type: 'Identifier',
|
|
3447
|
+
name,
|
|
3448
|
+
optional: false,
|
|
3449
|
+
loc: DUMMY_LOC
|
|
3450
|
+
},
|
|
3451
|
+
loc: DUMMY_LOC
|
|
3452
|
+
}, true];
|
|
3453
|
+
}
|
|
3454
|
+
}
|
|
3455
|
+
|
|
3456
|
+
const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : node.key.literalType === 'string' ? transform.StringLiteral(node.key) : null;
|
|
3457
|
+
|
|
3458
|
+
if (key == null) {
|
|
3459
|
+
throw unexpectedTranslationError(node, 'Unsupported key type');
|
|
3460
|
+
}
|
|
3461
|
+
|
|
3462
|
+
return [key, false];
|
|
3463
|
+
})();
|
|
3425
3464
|
|
|
3426
3465
|
if (node.method === true) {
|
|
3427
3466
|
// flow has just one node for all object properties and relies upon the method flag
|
|
3428
3467
|
// TS has separate nodes for methods and properties
|
|
3429
|
-
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3468
|
+
const func = transform.FunctionTypeAnnotation(node.value); // $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3469
|
+
|
|
3430
3470
|
return {
|
|
3431
3471
|
type: 'TSMethodSignature',
|
|
3432
3472
|
loc: DUMMY_LOC,
|
|
3433
|
-
computed
|
|
3473
|
+
computed,
|
|
3434
3474
|
key,
|
|
3435
3475
|
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
3436
3476
|
optional: node.optional,
|
|
@@ -3444,11 +3484,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3444
3484
|
if (node.kind === 'get' || node.kind === 'set') {
|
|
3445
3485
|
// flow treats getters/setters as true property signatures (method === false)
|
|
3446
3486
|
// TS treats them as method signatures
|
|
3447
|
-
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3487
|
+
const func = transform.FunctionTypeAnnotation(node.value); // $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3488
|
+
|
|
3448
3489
|
return {
|
|
3449
3490
|
type: 'TSMethodSignature',
|
|
3450
3491
|
loc: DUMMY_LOC,
|
|
3451
|
-
computed
|
|
3492
|
+
computed,
|
|
3452
3493
|
key,
|
|
3453
3494
|
kind: node.kind,
|
|
3454
3495
|
optional: false,
|
|
@@ -3459,12 +3500,13 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3459
3500
|
// TS accessors cannot have type parameters
|
|
3460
3501
|
typeParameters: undefined
|
|
3461
3502
|
};
|
|
3462
|
-
}
|
|
3503
|
+
} // $FlowFixMe[incompatible-type] `computed` is set dynamically
|
|
3504
|
+
|
|
3463
3505
|
|
|
3464
3506
|
return {
|
|
3465
3507
|
type: 'TSPropertySignature',
|
|
3466
3508
|
loc: DUMMY_LOC,
|
|
3467
|
-
computed
|
|
3509
|
+
computed,
|
|
3468
3510
|
key,
|
|
3469
3511
|
optional: node.optional,
|
|
3470
3512
|
readonly: ((_node$variance3 = node.variance) == null ? void 0 : _node$variance3.kind) === 'plus',
|
|
@@ -3756,6 +3798,27 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3756
3798
|
};
|
|
3757
3799
|
},
|
|
3758
3800
|
|
|
3801
|
+
NeverTypeAnnotation(_node) {
|
|
3802
|
+
return {
|
|
3803
|
+
type: 'TSNeverKeyword',
|
|
3804
|
+
loc: DUMMY_LOC
|
|
3805
|
+
};
|
|
3806
|
+
},
|
|
3807
|
+
|
|
3808
|
+
UndefinedTypeAnnotation(_node) {
|
|
3809
|
+
return {
|
|
3810
|
+
type: 'TSUndefinedKeyword',
|
|
3811
|
+
loc: DUMMY_LOC
|
|
3812
|
+
};
|
|
3813
|
+
},
|
|
3814
|
+
|
|
3815
|
+
UnknownTypeAnnotation(_node) {
|
|
3816
|
+
return {
|
|
3817
|
+
type: 'TSUnknownKeyword',
|
|
3818
|
+
loc: DUMMY_LOC
|
|
3819
|
+
};
|
|
3820
|
+
},
|
|
3821
|
+
|
|
3759
3822
|
ConditionalTypeAnnotation(node) {
|
|
3760
3823
|
return {
|
|
3761
3824
|
type: 'TSConditionalType',
|
|
@@ -249,7 +249,7 @@ function stripUnusedDefs(detachedStmt, usedDeps, context) {
|
|
|
249
249
|
specifiers: resultSpecfiers,
|
|
250
250
|
importKind: stmt.importKind,
|
|
251
251
|
source: stmt.source,
|
|
252
|
-
|
|
252
|
+
attributes: stmt.attributes
|
|
253
253
|
});
|
|
254
254
|
}
|
|
255
255
|
|
|
@@ -727,15 +727,15 @@ function convertVariableDeclaration(stmt, context) {
|
|
|
727
727
|
}
|
|
728
728
|
|
|
729
729
|
function convertImportDeclaration(stmt, context) {
|
|
730
|
-
if (stmt.
|
|
731
|
-
throw (0, _ErrorUtils.translationError)(stmt, 'ImportDeclaration:
|
|
730
|
+
if (stmt.attributes.length > 0) {
|
|
731
|
+
throw (0, _ErrorUtils.translationError)(stmt, 'ImportDeclaration: attributes not supported', context);
|
|
732
732
|
}
|
|
733
733
|
|
|
734
734
|
return [_hermesTransform.t.ImportDeclaration({
|
|
735
735
|
specifiers: stmt.specifiers,
|
|
736
736
|
importKind: stmt.importKind,
|
|
737
737
|
source: stmt.source,
|
|
738
|
-
|
|
738
|
+
attributes: []
|
|
739
739
|
}), []];
|
|
740
740
|
}
|
|
741
741
|
|
|
@@ -1147,12 +1147,12 @@ function convertFunctionParameters(params, context) {
|
|
|
1147
1147
|
}
|
|
1148
1148
|
|
|
1149
1149
|
function convertBindingNameToFunctionTypeParam(pat, context, index, isAssignment) {
|
|
1150
|
-
const name = pat.type === 'Identifier' ? pat.name : `$$PARAM_${index}$$`;
|
|
1150
|
+
const name = pat.type === 'Identifier' && pat.name != null ? pat.name : `$$PARAM_${index}$$`;
|
|
1151
1151
|
const [resultParamTypeAnnotation, paramDeps] = convertTypeAnnotation(pat.typeAnnotation, pat, context);
|
|
1152
1152
|
return [_hermesTransform.t.FunctionTypeParam({
|
|
1153
|
-
name:
|
|
1153
|
+
name: _hermesTransform.t.Identifier({
|
|
1154
1154
|
name
|
|
1155
|
-
})
|
|
1155
|
+
}),
|
|
1156
1156
|
typeAnnotation: resultParamTypeAnnotation,
|
|
1157
1157
|
optional: isAssignment || (pat.type === 'Identifier' ? pat.optional : false)
|
|
1158
1158
|
}), paramDeps];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flow-api-translator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.34.0",
|
|
4
4
|
"description": "Toolkit for creating Flow and TypeScript compatible libraries from Flow source code.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"@typescript-eslint/parser": "8.38.0",
|
|
14
14
|
"@typescript-eslint/visitor-keys": "8.38.0",
|
|
15
15
|
"flow-enums-runtime": "^0.0.6",
|
|
16
|
-
"hermes-eslint": "0.
|
|
17
|
-
"hermes-estree": "0.
|
|
18
|
-
"hermes-parser": "0.
|
|
19
|
-
"hermes-transform": "0.
|
|
16
|
+
"hermes-eslint": "0.34.0",
|
|
17
|
+
"hermes-estree": "0.34.0",
|
|
18
|
+
"hermes-parser": "0.34.0",
|
|
19
|
+
"hermes-transform": "0.34.0",
|
|
20
20
|
"typescript": "5.3.2"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|