flow-api-translator 0.37.0 → 0.328.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/TSDefToFlowDef.js +35 -279
- package/dist/TSDefToFlowDef.js.flow +16 -4
- package/dist/flowDefToTSDef.js +51 -377
- package/dist/flowDefToTSDef.js.flow +93 -12
- package/dist/flowImportTo.js +5 -16
- package/dist/flowImportTo.js.flow +3 -3
- package/dist/flowToFlowDef.js +134 -359
- package/dist/flowToFlowDef.js.flow +18 -12
- package/dist/flowToJS.js +3 -10
- package/dist/flowToJS.js.flow +5 -5
- package/dist/index.js +12 -28
- package/dist/index.js.flow +1 -1
- package/dist/src/TSDefToFlowDef.js +35 -279
- package/dist/src/flowDefToTSDef.js +51 -377
- package/dist/src/flowImportTo.js +5 -16
- package/dist/src/flowToFlowDef.js +134 -359
- package/dist/src/flowToJS.js +3 -10
- package/dist/src/index.js +12 -28
- package/dist/src/utils/DocblockUtils.js +4 -4
- package/dist/src/utils/ErrorUtils.js +3 -18
- package/dist/src/utils/FlowAnalyze.js +5 -16
- package/dist/src/utils/TSNodeUtils.js +28 -13
- package/dist/src/utils/TranslationUtils.js +0 -4
- package/dist/utils/DocblockUtils.js +4 -4
- package/dist/utils/DocblockUtils.js.flow +1 -1
- package/dist/utils/ErrorUtils.js +3 -18
- package/dist/utils/ErrorUtils.js.flow +3 -3
- package/dist/utils/FlowAnalyze.js +5 -16
- package/dist/utils/FlowAnalyze.js.flow +3 -3
- package/dist/utils/TSNodeUtils.js +28 -13
- package/dist/utils/TSNodeUtils.js.flow +35 -1
- package/dist/utils/TranslationUtils.js +0 -4
- package/dist/utils/TranslationUtils.js.flow +2 -2
- package/package.json +9 -9
|
@@ -4,25 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.flowDefToTSDef = flowDefToTSDef;
|
|
7
|
-
|
|
8
|
-
var FlowESTree = _interopRequireWildcard(require("hermes-estree"));
|
|
9
|
-
|
|
7
|
+
var FlowESTree = _interopRequireWildcard(require("flow-estree"));
|
|
10
8
|
var TSESTree = _interopRequireWildcard(require("./utils/ts-estree-ast-types"));
|
|
11
|
-
|
|
12
|
-
var _hermesTransform = require("hermes-transform");
|
|
13
|
-
|
|
9
|
+
var _flowTransform = require("flow-transform");
|
|
14
10
|
var _ErrorUtils = require("./utils/ErrorUtils");
|
|
15
|
-
|
|
16
11
|
var _DocblockUtils = require("./utils/DocblockUtils");
|
|
17
|
-
|
|
18
12
|
var _TSNodeUtils = require("./utils/TSNodeUtils");
|
|
19
|
-
|
|
20
13
|
var _os = require("os");
|
|
21
|
-
|
|
22
|
-
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); }
|
|
23
|
-
|
|
24
|
-
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; }
|
|
25
|
-
|
|
14
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
26
15
|
const DUMMY_LOC = {
|
|
27
16
|
start: {
|
|
28
17
|
line: 1,
|
|
@@ -33,33 +22,57 @@ const DUMMY_LOC = {
|
|
|
33
22
|
column: 0
|
|
34
23
|
}
|
|
35
24
|
};
|
|
36
|
-
|
|
37
25
|
function constructFlowNode(node) {
|
|
38
26
|
return node;
|
|
39
27
|
}
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
28
|
+
const cloneJSDocCommentsToNewNode = _flowTransform.cloneJSDocCommentsToNewNode;
|
|
29
|
+
const makeCommentOwnLine = _flowTransform.makeCommentOwnLine;
|
|
30
|
+
function normalizeDeclareComponentParams(params, rest, onUnexpected) {
|
|
31
|
+
const normalizedParams = [];
|
|
32
|
+
let normalizedRest = rest;
|
|
33
|
+
const toComponentTypeParameter = (name, local, source) => {
|
|
34
|
+
if (local.type !== 'Identifier' || local.typeAnnotation == null) {
|
|
35
|
+
throw onUnexpected(source, `DeclareComponent: expected an annotated identifier parameter, got "${local.type}"`);
|
|
36
|
+
}
|
|
37
|
+
return constructFlowNode({
|
|
38
|
+
type: 'ComponentTypeParameter',
|
|
39
|
+
name,
|
|
40
|
+
typeAnnotation: local.typeAnnotation.typeAnnotation,
|
|
41
|
+
optional: local.optional,
|
|
42
|
+
loc: source.loc,
|
|
43
|
+
range: source.range
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
for (const param of params) {
|
|
47
|
+
switch (param.type) {
|
|
48
|
+
case 'ComponentTypeParameter':
|
|
49
|
+
normalizedParams.push(param);
|
|
50
|
+
break;
|
|
51
|
+
case 'ComponentParameter':
|
|
52
|
+
normalizedParams.push(toComponentTypeParameter(param.name, param.local, param));
|
|
53
|
+
break;
|
|
54
|
+
case 'RestElement':
|
|
55
|
+
normalizedRest = toComponentTypeParameter(null, param.argument, param);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return [normalizedParams, normalizedRest];
|
|
60
|
+
}
|
|
43
61
|
const VALID_REACT_IMPORTS = new Set(['React', 'react']);
|
|
44
|
-
|
|
45
62
|
function isValidReactImportOrGlobal(id) {
|
|
46
63
|
return VALID_REACT_IMPORTS.has(id.name) || id.name.startsWith('React$');
|
|
47
64
|
}
|
|
48
|
-
|
|
49
65
|
let shouldAddReactImport = null;
|
|
50
|
-
|
|
51
66
|
function getReactIdentifier(hasReactImport) {
|
|
52
67
|
if (shouldAddReactImport !== false) {
|
|
53
68
|
shouldAddReactImport = !hasReactImport;
|
|
54
69
|
}
|
|
55
|
-
|
|
56
70
|
return {
|
|
57
71
|
type: 'Identifier',
|
|
58
72
|
loc: DUMMY_LOC,
|
|
59
73
|
name: `React`
|
|
60
74
|
};
|
|
61
75
|
}
|
|
62
|
-
|
|
63
76
|
function flowDefToTSDef(originalCode, ast, scopeManager, opts) {
|
|
64
77
|
const tsBody = [];
|
|
65
78
|
const tsProgram = {
|
|
@@ -71,7 +84,6 @@ function flowDefToTSDef(originalCode, ast, scopeManager, opts) {
|
|
|
71
84
|
};
|
|
72
85
|
shouldAddReactImport = null;
|
|
73
86
|
const [transform, code] = getTransforms(originalCode, scopeManager, opts);
|
|
74
|
-
|
|
75
87
|
for (const node of ast.body) {
|
|
76
88
|
if (node.type in transform) {
|
|
77
89
|
const result = transform[node.type](node);
|
|
@@ -82,7 +94,6 @@ function flowDefToTSDef(originalCode, ast, scopeManager, opts) {
|
|
|
82
94
|
});
|
|
83
95
|
}
|
|
84
96
|
}
|
|
85
|
-
|
|
86
97
|
if (shouldAddReactImport === true) {
|
|
87
98
|
tsBody.unshift({
|
|
88
99
|
type: 'ImportDeclaration',
|
|
@@ -106,36 +117,28 @@ function flowDefToTSDef(originalCode, ast, scopeManager, opts) {
|
|
|
106
117
|
importKind: 'value'
|
|
107
118
|
});
|
|
108
119
|
}
|
|
109
|
-
|
|
110
120
|
return [tsProgram, code];
|
|
111
121
|
}
|
|
112
|
-
|
|
113
122
|
const getTransforms = (originalCode, scopeManager, opts) => {
|
|
114
123
|
let code = originalCode;
|
|
115
|
-
|
|
116
124
|
function translationError(node, message) {
|
|
117
125
|
return (0, _ErrorUtils.translationError)(node, message, {
|
|
118
126
|
code
|
|
119
127
|
});
|
|
120
128
|
}
|
|
121
|
-
|
|
122
129
|
function unexpectedTranslationError(node, message) {
|
|
123
130
|
return (0, _ErrorUtils.unexpectedTranslationError)(node, message, {
|
|
124
131
|
code
|
|
125
132
|
});
|
|
126
133
|
}
|
|
127
|
-
|
|
128
134
|
function unsupportedFeatureMessage(thing) {
|
|
129
135
|
return `Unsupported feature: Translating "${thing}" is currently not supported.`;
|
|
130
136
|
}
|
|
131
|
-
|
|
132
137
|
function buildCodeFrameForComment(node, message) {
|
|
133
138
|
return (0, _ErrorUtils.buildCodeFrame)(node, message, code, false);
|
|
134
139
|
}
|
|
135
|
-
|
|
136
140
|
function addErrorComment(node, message) {
|
|
137
141
|
var _node$comments;
|
|
138
|
-
|
|
139
142
|
const comment = {
|
|
140
143
|
type: 'Block',
|
|
141
144
|
loc: DUMMY_LOC,
|
|
@@ -147,10 +150,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
147
150
|
(_node$comments = node.comments) != null ? _node$comments : node.comments = [];
|
|
148
151
|
node.comments.push(comment);
|
|
149
152
|
}
|
|
150
|
-
|
|
151
153
|
function unsupportedAnnotation(node, thing) {
|
|
152
154
|
const message = unsupportedFeatureMessage(thing);
|
|
153
|
-
|
|
154
155
|
if (opts.recoverFromErrors) {
|
|
155
156
|
const codeFrame = buildCodeFrameForComment(node, message);
|
|
156
157
|
const newNode = {
|
|
@@ -160,13 +161,10 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
160
161
|
addErrorComment(newNode, codeFrame);
|
|
161
162
|
return newNode;
|
|
162
163
|
}
|
|
163
|
-
|
|
164
164
|
throw translationError(node, message);
|
|
165
165
|
}
|
|
166
|
-
|
|
167
166
|
function unsupportedDeclaration(node, thing, id, declare = false, typeParameters = null) {
|
|
168
167
|
const message = unsupportedFeatureMessage(thing);
|
|
169
|
-
|
|
170
168
|
if (opts.recoverFromErrors) {
|
|
171
169
|
const codeFrame = buildCodeFrameForComment(node, message);
|
|
172
170
|
const newNode = {
|
|
@@ -183,37 +181,28 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
183
181
|
addErrorComment(newNode, codeFrame);
|
|
184
182
|
return newNode;
|
|
185
183
|
}
|
|
186
|
-
|
|
187
184
|
throw translationError(node, message);
|
|
188
185
|
}
|
|
189
|
-
|
|
190
186
|
const topScope = (() => {
|
|
191
187
|
const globalScope = scopeManager.globalScope;
|
|
192
|
-
|
|
193
188
|
if (globalScope.childScopes.length > 0 && globalScope.childScopes[0].type === 'module') {
|
|
194
189
|
return globalScope.childScopes[0];
|
|
195
190
|
}
|
|
196
|
-
|
|
197
191
|
return globalScope;
|
|
198
192
|
})();
|
|
199
|
-
|
|
200
193
|
function isReactImport(scopeNode, name) {
|
|
201
194
|
let currentScope = (() => {
|
|
202
195
|
let scope = null;
|
|
203
196
|
let node = scopeNode;
|
|
204
|
-
|
|
205
197
|
while (!scope && node) {
|
|
206
198
|
scope = scopeManager.acquire(node, true);
|
|
207
199
|
node = node.parent;
|
|
208
200
|
}
|
|
209
|
-
|
|
210
201
|
return scope;
|
|
211
202
|
})();
|
|
212
|
-
|
|
213
203
|
if (currentScope == null) {
|
|
214
204
|
throw new Error('unable to resolve scope');
|
|
215
205
|
}
|
|
216
|
-
|
|
217
206
|
const variableDef = (() => {
|
|
218
207
|
while (currentScope != null) {
|
|
219
208
|
for (const variable of currentScope.variables) {
|
|
@@ -221,49 +210,37 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
221
210
|
return variable;
|
|
222
211
|
}
|
|
223
212
|
}
|
|
224
|
-
|
|
225
213
|
currentScope = currentScope.upper;
|
|
226
214
|
}
|
|
227
215
|
})();
|
|
228
|
-
|
|
229
216
|
if (variableDef == null) {
|
|
230
217
|
return false;
|
|
231
218
|
}
|
|
232
|
-
|
|
233
219
|
const def = variableDef.defs[0];
|
|
234
|
-
|
|
235
220
|
switch (def.type) {
|
|
236
221
|
case 'ImportBinding':
|
|
237
222
|
{
|
|
238
223
|
if (def.node.type === 'ImportDefaultSpecifier' || def.node.type === 'ImportNamespaceSpecifier') {
|
|
239
224
|
return VALID_REACT_IMPORTS.has(def.parent.source.value);
|
|
240
225
|
}
|
|
241
|
-
|
|
242
226
|
return false;
|
|
243
227
|
}
|
|
244
|
-
|
|
245
228
|
case 'ImplicitGlobalVariable':
|
|
246
229
|
{
|
|
247
230
|
return VALID_REACT_IMPORTS.has(name);
|
|
248
231
|
}
|
|
249
232
|
}
|
|
250
|
-
|
|
251
233
|
return false;
|
|
252
234
|
}
|
|
253
|
-
|
|
254
235
|
function EnumImpl(node) {
|
|
255
236
|
const body = node.body;
|
|
256
|
-
|
|
257
237
|
if (body.type === 'EnumSymbolBody') {
|
|
258
238
|
return unsupportedDeclaration(node, 'symbol enums', node.id, FlowESTree.isDeclareEnum(node));
|
|
259
239
|
}
|
|
260
|
-
|
|
261
240
|
if (body.type === 'EnumBooleanBody') {
|
|
262
241
|
return unsupportedDeclaration(node, 'boolean enums', node.id, FlowESTree.isDeclareEnum(node));
|
|
263
242
|
}
|
|
264
|
-
|
|
265
243
|
const members = [];
|
|
266
|
-
|
|
267
244
|
for (const member of body.members) {
|
|
268
245
|
switch (member.type) {
|
|
269
246
|
case 'EnumDefaultedMember':
|
|
@@ -271,7 +248,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
271
248
|
if (body.type === 'EnumNumberBody') {
|
|
272
249
|
throw unexpectedTranslationError(member, 'Unexpected defaulted number enum member');
|
|
273
250
|
}
|
|
274
|
-
|
|
275
251
|
members.push({
|
|
276
252
|
type: 'TSEnumMember',
|
|
277
253
|
loc: DUMMY_LOC,
|
|
@@ -286,7 +262,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
286
262
|
});
|
|
287
263
|
break;
|
|
288
264
|
}
|
|
289
|
-
|
|
290
265
|
case 'EnumNumberMember':
|
|
291
266
|
case 'EnumStringMember':
|
|
292
267
|
members.push({
|
|
@@ -298,7 +273,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
298
273
|
});
|
|
299
274
|
}
|
|
300
275
|
}
|
|
301
|
-
|
|
302
276
|
const bodyRepresentationType = body.type === 'EnumNumberBody' ? {
|
|
303
277
|
type: 'TSNumberKeyword',
|
|
304
278
|
loc: DUMMY_LOC
|
|
@@ -525,133 +499,92 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
525
499
|
}
|
|
526
500
|
}];
|
|
527
501
|
}
|
|
528
|
-
|
|
529
502
|
const getPlaceholderNameForTypeofImport = (() => {
|
|
530
503
|
let typeof_import_count = 0;
|
|
531
504
|
return () => `$$IMPORT_TYPEOF_${++typeof_import_count}$$`;
|
|
532
505
|
})();
|
|
533
|
-
|
|
534
506
|
const transformTypeAnnotationType = node => {
|
|
535
507
|
switch (node.type) {
|
|
536
508
|
case 'AnyTypeAnnotation':
|
|
537
509
|
return transform.AnyTypeAnnotation(node);
|
|
538
|
-
|
|
539
510
|
case 'ArrayTypeAnnotation':
|
|
540
511
|
return transform.ArrayTypeAnnotation(node);
|
|
541
|
-
|
|
542
512
|
case 'BigIntLiteralTypeAnnotation':
|
|
543
513
|
return transform.BigIntLiteralTypeAnnotation(node);
|
|
544
|
-
|
|
545
514
|
case 'BigIntTypeAnnotation':
|
|
546
515
|
return transform.BigIntTypeAnnotation(node);
|
|
547
|
-
|
|
548
516
|
case 'BooleanLiteralTypeAnnotation':
|
|
549
517
|
return transform.BooleanLiteralTypeAnnotation(node);
|
|
550
|
-
|
|
551
518
|
case 'BooleanTypeAnnotation':
|
|
552
519
|
return transform.BooleanTypeAnnotation(node);
|
|
553
|
-
|
|
554
520
|
case 'EmptyTypeAnnotation':
|
|
555
521
|
return transform.EmptyTypeAnnotation(node);
|
|
556
|
-
|
|
557
522
|
case 'ExistsTypeAnnotation':
|
|
558
523
|
return transform.ExistsTypeAnnotation(node);
|
|
559
|
-
|
|
560
524
|
case 'FunctionTypeAnnotation':
|
|
561
525
|
return transform.FunctionTypeAnnotation(node);
|
|
562
|
-
|
|
563
526
|
case 'GenericTypeAnnotation':
|
|
564
527
|
return transform.GenericTypeAnnotation(node);
|
|
565
|
-
|
|
566
528
|
case 'IndexedAccessType':
|
|
567
529
|
return transform.IndexedAccessType(node);
|
|
568
|
-
|
|
569
530
|
case 'InterfaceTypeAnnotation':
|
|
570
531
|
return transform.InterfaceTypeAnnotation(node);
|
|
571
|
-
|
|
572
532
|
case 'IntersectionTypeAnnotation':
|
|
573
533
|
return transform.IntersectionTypeAnnotation(node);
|
|
574
|
-
|
|
575
534
|
case 'MixedTypeAnnotation':
|
|
576
535
|
return transform.MixedTypeAnnotation(node);
|
|
577
|
-
|
|
578
536
|
case 'NullLiteralTypeAnnotation':
|
|
579
537
|
return transform.NullLiteralTypeAnnotation(node);
|
|
580
|
-
|
|
581
538
|
case 'NullableTypeAnnotation':
|
|
582
539
|
return transform.NullableTypeAnnotation(node);
|
|
583
|
-
|
|
584
540
|
case 'NumberLiteralTypeAnnotation':
|
|
585
541
|
return transform.NumberLiteralTypeAnnotation(node);
|
|
586
|
-
|
|
587
542
|
case 'NumberTypeAnnotation':
|
|
588
543
|
return transform.NumberTypeAnnotation(node);
|
|
589
|
-
|
|
590
544
|
case 'ObjectTypeAnnotation':
|
|
591
545
|
return transform.ObjectTypeAnnotation(node);
|
|
592
|
-
|
|
593
546
|
case 'OptionalIndexedAccessType':
|
|
594
547
|
return transform.OptionalIndexedAccessType(node);
|
|
595
|
-
|
|
596
548
|
case 'QualifiedTypeIdentifier':
|
|
597
549
|
return transform.QualifiedTypeIdentifier(node);
|
|
598
|
-
|
|
599
550
|
case 'StringLiteralTypeAnnotation':
|
|
600
551
|
return transform.StringLiteralTypeAnnotation(node);
|
|
601
|
-
|
|
602
552
|
case 'StringTypeAnnotation':
|
|
603
553
|
return transform.StringTypeAnnotation(node);
|
|
604
|
-
|
|
605
554
|
case 'SymbolTypeAnnotation':
|
|
606
555
|
return transform.SymbolTypeAnnotation(node);
|
|
607
|
-
|
|
608
556
|
case 'ThisTypeAnnotation':
|
|
609
557
|
return transform.ThisTypeAnnotation(node);
|
|
610
|
-
|
|
611
558
|
case 'TupleTypeAnnotation':
|
|
612
559
|
return transform.TupleTypeAnnotation(node);
|
|
613
|
-
|
|
614
560
|
case 'TypeofTypeAnnotation':
|
|
615
561
|
return transform.TypeofTypeAnnotation(node);
|
|
616
|
-
|
|
617
562
|
case 'UnionTypeAnnotation':
|
|
618
563
|
return transform.UnionTypeAnnotation(node);
|
|
619
|
-
|
|
620
564
|
case 'VoidTypeAnnotation':
|
|
621
565
|
return transform.VoidTypeAnnotation(node);
|
|
622
|
-
|
|
623
566
|
case 'TypePredicate':
|
|
624
567
|
return transform.TypePredicateAnnotation(node);
|
|
625
|
-
|
|
626
568
|
case 'ConditionalTypeAnnotation':
|
|
627
569
|
return transform.ConditionalTypeAnnotation(node);
|
|
628
|
-
|
|
629
570
|
case 'InferTypeAnnotation':
|
|
630
571
|
return transform.InferTypeAnnotation(node);
|
|
631
|
-
|
|
632
572
|
case 'KeyofTypeAnnotation':
|
|
633
573
|
return transform.KeyofTypeAnnotation(node);
|
|
634
|
-
|
|
635
574
|
case 'TypeOperator':
|
|
636
575
|
return transform.TypeOperator(node);
|
|
637
|
-
|
|
638
576
|
case 'ComponentTypeAnnotation':
|
|
639
577
|
return transform.ComponentTypeAnnotation(node);
|
|
640
|
-
|
|
641
578
|
case 'NeverTypeAnnotation':
|
|
642
579
|
return transform.NeverTypeAnnotation(node);
|
|
643
|
-
|
|
644
580
|
case 'UndefinedTypeAnnotation':
|
|
645
581
|
return transform.UndefinedTypeAnnotation(node);
|
|
646
|
-
|
|
647
582
|
case 'UnknownTypeAnnotation':
|
|
648
583
|
return transform.UnknownTypeAnnotation(node);
|
|
649
|
-
|
|
650
584
|
default:
|
|
651
585
|
throw unexpectedTranslationError(node, `Unhandled type ${node.type}`);
|
|
652
586
|
}
|
|
653
587
|
};
|
|
654
|
-
|
|
655
588
|
const wellKnownSymbols = new Set(['iterator', 'asyncIterator', 'dispose', 'asyncDispose']);
|
|
656
589
|
const transform = {
|
|
657
590
|
AnyTypeAnnotation(_node) {
|
|
@@ -660,7 +593,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
660
593
|
loc: DUMMY_LOC
|
|
661
594
|
};
|
|
662
595
|
},
|
|
663
|
-
|
|
664
596
|
ArrayTypeAnnotation(node) {
|
|
665
597
|
return {
|
|
666
598
|
type: 'TSArrayType',
|
|
@@ -668,7 +600,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
668
600
|
elementType: transformTypeAnnotationType(node.elementType)
|
|
669
601
|
};
|
|
670
602
|
},
|
|
671
|
-
|
|
672
603
|
BigIntLiteral(node) {
|
|
673
604
|
return {
|
|
674
605
|
type: 'Literal',
|
|
@@ -678,10 +609,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
678
609
|
value: node.value
|
|
679
610
|
};
|
|
680
611
|
},
|
|
681
|
-
|
|
682
612
|
BigIntLiteralTypeAnnotation(node) {
|
|
683
613
|
var _node$bigint;
|
|
684
|
-
|
|
685
614
|
const bigint = (_node$bigint = node.bigint) != null ? _node$bigint : node.raw.replace(/n$/, '').replace(/_/, '');
|
|
686
615
|
return {
|
|
687
616
|
type: 'TSLiteralType',
|
|
@@ -695,14 +624,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
695
624
|
}
|
|
696
625
|
};
|
|
697
626
|
},
|
|
698
|
-
|
|
699
627
|
BigIntTypeAnnotation(_node) {
|
|
700
628
|
return {
|
|
701
629
|
type: 'TSBigIntKeyword',
|
|
702
630
|
loc: DUMMY_LOC
|
|
703
631
|
};
|
|
704
632
|
},
|
|
705
|
-
|
|
706
633
|
BooleanLiteral(node) {
|
|
707
634
|
return {
|
|
708
635
|
type: 'Literal',
|
|
@@ -711,7 +638,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
711
638
|
value: node.value
|
|
712
639
|
};
|
|
713
640
|
},
|
|
714
|
-
|
|
715
641
|
BooleanLiteralTypeAnnotation(node) {
|
|
716
642
|
return {
|
|
717
643
|
type: 'TSLiteralType',
|
|
@@ -724,14 +650,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
724
650
|
}
|
|
725
651
|
};
|
|
726
652
|
},
|
|
727
|
-
|
|
728
653
|
BooleanTypeAnnotation(_node) {
|
|
729
654
|
return {
|
|
730
655
|
type: 'TSBooleanKeyword',
|
|
731
656
|
loc: DUMMY_LOC
|
|
732
657
|
};
|
|
733
658
|
},
|
|
734
|
-
|
|
735
659
|
ClassImplements(node) {
|
|
736
660
|
return {
|
|
737
661
|
type: 'TSClassImplements',
|
|
@@ -740,15 +664,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
740
664
|
typeArguments: transform.TypeParameterInstantiation(node.typeParameters)
|
|
741
665
|
};
|
|
742
666
|
},
|
|
743
|
-
|
|
744
667
|
DeclareClass(node) {
|
|
745
668
|
const classMembers = [];
|
|
746
669
|
const transformedBody = transform.ObjectTypeAnnotation(node.body);
|
|
747
|
-
|
|
748
670
|
if (transformedBody.type !== 'TSTypeLiteral') {
|
|
749
671
|
return unsupportedDeclaration(node.body, 'Spreads in declare class are not allowed', node.id, true, node.typeParameters);
|
|
750
672
|
}
|
|
751
|
-
|
|
752
673
|
for (const member of transformedBody.members) {
|
|
753
674
|
switch (member.type) {
|
|
754
675
|
case 'TSIndexSignature':
|
|
@@ -756,17 +677,14 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
756
677
|
classMembers.push(member);
|
|
757
678
|
break;
|
|
758
679
|
}
|
|
759
|
-
|
|
760
680
|
case 'TSMethodSignature':
|
|
761
681
|
{
|
|
762
682
|
const isConstructor = (() => {
|
|
763
683
|
if (member.computed === true) {
|
|
764
684
|
return false;
|
|
765
685
|
}
|
|
766
|
-
|
|
767
686
|
return member.key.type === 'Identifier' && member.key.name === 'constructor' || member.key.type === 'Literal' && member.key.value === 'constructor';
|
|
768
687
|
})();
|
|
769
|
-
|
|
770
688
|
if (isConstructor) {
|
|
771
689
|
const newNode = {
|
|
772
690
|
type: 'MethodDefinition',
|
|
@@ -800,13 +718,10 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
800
718
|
classMembers.push(newNode);
|
|
801
719
|
} else {
|
|
802
720
|
var _member$static;
|
|
803
|
-
|
|
804
721
|
const [key, computed] = (() => {
|
|
805
722
|
const _key = member.key;
|
|
806
|
-
|
|
807
723
|
if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
|
|
808
724
|
const name = _key.name.slice(2);
|
|
809
|
-
|
|
810
725
|
if (wellKnownSymbols.has(name)) {
|
|
811
726
|
return [{
|
|
812
727
|
type: 'MemberExpression',
|
|
@@ -828,10 +743,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
828
743
|
}, true];
|
|
829
744
|
}
|
|
830
745
|
}
|
|
831
|
-
|
|
832
746
|
return [member.key, member.computed];
|
|
833
747
|
})();
|
|
834
|
-
|
|
835
748
|
const newNode = {
|
|
836
749
|
type: 'MethodDefinition',
|
|
837
750
|
loc: DUMMY_LOC,
|
|
@@ -859,14 +772,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
859
772
|
cloneJSDocCommentsToNewNode(member, newNode);
|
|
860
773
|
classMembers.push(newNode);
|
|
861
774
|
}
|
|
862
|
-
|
|
863
775
|
break;
|
|
864
776
|
}
|
|
865
|
-
|
|
866
777
|
case 'TSPropertySignature':
|
|
867
778
|
{
|
|
868
779
|
var _member$computed, _member$static2;
|
|
869
|
-
|
|
870
780
|
const newNode = {
|
|
871
781
|
type: 'PropertyDefinition',
|
|
872
782
|
loc: DUMMY_LOC,
|
|
@@ -884,19 +794,15 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
884
794
|
classMembers.push(newNode);
|
|
885
795
|
break;
|
|
886
796
|
}
|
|
887
|
-
|
|
888
797
|
case 'TSCallSignatureDeclaration':
|
|
889
798
|
{
|
|
890
799
|
var _node$body$callProper;
|
|
891
|
-
|
|
892
800
|
return unsupportedDeclaration((_node$body$callProper = node.body.callProperties[0]) != null ? _node$body$callProper : node.body, 'call signatures on classes', node.id, true, node.typeParameters);
|
|
893
801
|
}
|
|
894
|
-
|
|
895
802
|
default:
|
|
896
803
|
throw unexpectedTranslationError(node.body, `Unexpected member type ${member.type}`);
|
|
897
804
|
}
|
|
898
805
|
}
|
|
899
|
-
|
|
900
806
|
const superClass = node.extends.length > 0 ? node.extends[0] : undefined;
|
|
901
807
|
return {
|
|
902
808
|
type: 'ClassDeclaration',
|
|
@@ -914,11 +820,9 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
914
820
|
typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
|
|
915
821
|
};
|
|
916
822
|
},
|
|
917
|
-
|
|
918
823
|
DeclareExportDeclaration(node) {
|
|
919
824
|
if (node.default === true) {
|
|
920
825
|
const declaration = node.declaration;
|
|
921
|
-
|
|
922
826
|
switch (declaration.type) {
|
|
923
827
|
case 'DeclareClass':
|
|
924
828
|
{
|
|
@@ -935,7 +839,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
935
839
|
exportKind: 'value'
|
|
936
840
|
}];
|
|
937
841
|
}
|
|
938
|
-
|
|
939
842
|
case 'DeclareFunction':
|
|
940
843
|
{
|
|
941
844
|
const functionDecl = transform.DeclareFunction(declaration);
|
|
@@ -951,7 +854,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
951
854
|
exportKind: 'value'
|
|
952
855
|
}];
|
|
953
856
|
}
|
|
954
|
-
|
|
955
857
|
case 'DeclareComponent':
|
|
956
858
|
{
|
|
957
859
|
const functionDecl = transform.DeclareComponent(declaration);
|
|
@@ -967,7 +869,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
967
869
|
exportKind: 'value'
|
|
968
870
|
}];
|
|
969
871
|
}
|
|
970
|
-
|
|
971
872
|
case 'DeclareHook':
|
|
972
873
|
{
|
|
973
874
|
const functionDecl = transform.DeclareHook(declaration);
|
|
@@ -983,27 +884,21 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
983
884
|
exportKind: 'value'
|
|
984
885
|
}];
|
|
985
886
|
}
|
|
986
|
-
|
|
987
887
|
case 'GenericTypeAnnotation':
|
|
988
888
|
{
|
|
989
889
|
const referencedId = declaration.id;
|
|
990
|
-
|
|
991
890
|
if (referencedId.type === 'Identifier') {
|
|
992
891
|
const exportedVar = topScope.set.get(referencedId.name);
|
|
993
|
-
|
|
994
892
|
if (exportedVar != null && exportedVar.defs.length === 1) {
|
|
995
893
|
const def = exportedVar.defs[0];
|
|
996
|
-
|
|
997
894
|
switch (def.type) {
|
|
998
895
|
case 'ImportBinding':
|
|
999
896
|
{
|
|
1000
897
|
const specifier = def.node;
|
|
1001
|
-
|
|
1002
898
|
if (specifier.importKind === 'type' || specifier.parent.importKind === 'type') {
|
|
1003
899
|
break;
|
|
1004
900
|
}
|
|
1005
901
|
}
|
|
1006
|
-
|
|
1007
902
|
case 'ClassName':
|
|
1008
903
|
case 'Enum':
|
|
1009
904
|
case 'FunctionName':
|
|
@@ -1019,28 +914,23 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1019
914
|
},
|
|
1020
915
|
exportKind: 'value'
|
|
1021
916
|
};
|
|
1022
|
-
|
|
1023
917
|
case 'CatchClause':
|
|
1024
918
|
case 'Parameter':
|
|
1025
919
|
case 'TypeParameter':
|
|
1026
920
|
throw translationError(def.node, `Unexpected variable def type: ${def.type}`);
|
|
1027
|
-
|
|
1028
921
|
case 'Type':
|
|
1029
922
|
break;
|
|
1030
923
|
}
|
|
1031
924
|
}
|
|
1032
925
|
}
|
|
1033
926
|
}
|
|
1034
|
-
|
|
1035
927
|
case 'TypeofTypeAnnotation':
|
|
1036
928
|
{
|
|
1037
929
|
if (declaration.type === 'TypeofTypeAnnotation' && declaration.argument.type === 'Identifier') {
|
|
1038
930
|
const name = declaration.argument.name;
|
|
1039
931
|
const exportedVar = topScope.set.get(name);
|
|
1040
|
-
|
|
1041
932
|
if (exportedVar != null && exportedVar.defs.length === 1) {
|
|
1042
933
|
const def = exportedVar.defs[0];
|
|
1043
|
-
|
|
1044
934
|
switch (def.type) {
|
|
1045
935
|
case 'ClassName':
|
|
1046
936
|
{
|
|
@@ -1061,7 +951,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1061
951
|
}
|
|
1062
952
|
}
|
|
1063
953
|
}
|
|
1064
|
-
|
|
1065
954
|
default:
|
|
1066
955
|
{
|
|
1067
956
|
const SPECIFIER = '$$EXPORT_DEFAULT_DECLARATION$$';
|
|
@@ -1132,7 +1021,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1132
1021
|
specifiers: node.specifiers.map(transform.ExportSpecifier)
|
|
1133
1022
|
};
|
|
1134
1023
|
}
|
|
1135
|
-
|
|
1136
1024
|
const declarations = (() => {
|
|
1137
1025
|
switch (node.declaration.type) {
|
|
1138
1026
|
case 'DeclareClass':
|
|
@@ -1140,43 +1028,36 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1140
1028
|
declaration: transform.DeclareClass(node.declaration),
|
|
1141
1029
|
exportKind: 'value'
|
|
1142
1030
|
}];
|
|
1143
|
-
|
|
1144
1031
|
case 'DeclareComponent':
|
|
1145
1032
|
return [{
|
|
1146
1033
|
declaration: transform.DeclareComponent(node.declaration),
|
|
1147
1034
|
exportKind: 'value'
|
|
1148
1035
|
}];
|
|
1149
|
-
|
|
1150
1036
|
case 'DeclareHook':
|
|
1151
1037
|
return [{
|
|
1152
1038
|
declaration: transform.DeclareHook(node.declaration),
|
|
1153
1039
|
exportKind: 'value'
|
|
1154
1040
|
}];
|
|
1155
|
-
|
|
1156
1041
|
case 'DeclareFunction':
|
|
1157
1042
|
return [{
|
|
1158
1043
|
declaration: transform.DeclareFunction(node.declaration),
|
|
1159
1044
|
exportKind: 'value'
|
|
1160
1045
|
}];
|
|
1161
|
-
|
|
1162
1046
|
case 'DeclareInterface':
|
|
1163
1047
|
return [{
|
|
1164
1048
|
declaration: transform.DeclareInterface(node.declaration),
|
|
1165
1049
|
exportKind: 'type'
|
|
1166
1050
|
}];
|
|
1167
|
-
|
|
1168
1051
|
case 'DeclareOpaqueType':
|
|
1169
1052
|
return [{
|
|
1170
1053
|
declaration: transform.DeclareOpaqueType(node.declaration),
|
|
1171
1054
|
exportKind: 'type'
|
|
1172
1055
|
}];
|
|
1173
|
-
|
|
1174
1056
|
case 'DeclareVariable':
|
|
1175
1057
|
return [{
|
|
1176
1058
|
declaration: transform.DeclareVariable(node.declaration),
|
|
1177
1059
|
exportKind: 'value'
|
|
1178
1060
|
}];
|
|
1179
|
-
|
|
1180
1061
|
case 'DeclareEnum':
|
|
1181
1062
|
{
|
|
1182
1063
|
const result = transform.DeclareEnum(node.declaration);
|
|
@@ -1193,14 +1074,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1193
1074
|
}
|
|
1194
1075
|
}
|
|
1195
1076
|
})();
|
|
1196
|
-
|
|
1197
1077
|
const mappedDeclarations = declarations.map(({
|
|
1198
1078
|
declaration,
|
|
1199
1079
|
exportKind
|
|
1200
1080
|
}) => {
|
|
1201
1081
|
if (declaration.type === 'VariableDeclaration' && declaration.declarations.length === 1) {
|
|
1202
1082
|
const ident = declaration.declarations[0].id;
|
|
1203
|
-
|
|
1204
1083
|
if (ident.type === 'Identifier') {
|
|
1205
1084
|
const name = ident.name;
|
|
1206
1085
|
return [{
|
|
@@ -1244,7 +1123,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1244
1123
|
}];
|
|
1245
1124
|
}
|
|
1246
1125
|
}
|
|
1247
|
-
|
|
1248
1126
|
const exportNamedDeclaration = {
|
|
1249
1127
|
type: 'ExportNamedDeclaration',
|
|
1250
1128
|
loc: DUMMY_LOC,
|
|
@@ -1270,11 +1148,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1270
1148
|
}
|
|
1271
1149
|
}
|
|
1272
1150
|
},
|
|
1273
|
-
|
|
1274
1151
|
DeclareComponent(node) {
|
|
1275
1152
|
const id = transform.Identifier(node.id, false);
|
|
1276
1153
|
const typeParameters = node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters);
|
|
1277
|
-
const
|
|
1154
|
+
const [declaredParams, declaredRest] = normalizeDeclareComponentParams(node.params, node.rest, unexpectedTranslationError);
|
|
1155
|
+
const params = transform.ComponentTypeParameters(declaredParams, declaredRest);
|
|
1278
1156
|
const hasReactImport = isReactImport(node, 'React');
|
|
1279
1157
|
const returnType = {
|
|
1280
1158
|
type: 'TSTypeAnnotation',
|
|
@@ -1313,7 +1191,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1313
1191
|
typeParameters: typeParameters
|
|
1314
1192
|
};
|
|
1315
1193
|
},
|
|
1316
|
-
|
|
1317
1194
|
ComponentTypeParameters(params, rest) {
|
|
1318
1195
|
if (params.length === 0 && rest != null) {
|
|
1319
1196
|
return [{
|
|
@@ -1328,9 +1205,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1328
1205
|
optional: false
|
|
1329
1206
|
}];
|
|
1330
1207
|
}
|
|
1331
|
-
|
|
1332
1208
|
const flowPropsType = [];
|
|
1333
|
-
|
|
1334
1209
|
if (rest != null) {
|
|
1335
1210
|
flowPropsType.push(constructFlowNode({
|
|
1336
1211
|
type: 'ObjectTypeSpreadProperty',
|
|
@@ -1339,10 +1214,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1339
1214
|
loc: rest.loc
|
|
1340
1215
|
}));
|
|
1341
1216
|
}
|
|
1342
|
-
|
|
1343
1217
|
for (let i = 0; i < params.length; i++) {
|
|
1344
1218
|
var _param$name;
|
|
1345
|
-
|
|
1346
1219
|
const param = params[i];
|
|
1347
1220
|
flowPropsType.push(constructFlowNode({
|
|
1348
1221
|
type: 'ObjectTypeProperty',
|
|
@@ -1363,7 +1236,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1363
1236
|
loc: param.loc
|
|
1364
1237
|
}));
|
|
1365
1238
|
}
|
|
1366
|
-
|
|
1367
1239
|
const tsPropsObjectType = transform.ObjectTypeAnnotation(constructFlowNode({
|
|
1368
1240
|
type: 'ObjectTypeAnnotation',
|
|
1369
1241
|
inexact: false,
|
|
@@ -1385,7 +1257,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1385
1257
|
optional: false
|
|
1386
1258
|
}];
|
|
1387
1259
|
},
|
|
1388
|
-
|
|
1389
1260
|
DeclareHook(node) {
|
|
1390
1261
|
const id = transform.Identifier(node.id, false);
|
|
1391
1262
|
const functionInfo = transform.FunctionTypeAnnotation(node.id.typeAnnotation.typeAnnotation);
|
|
@@ -1407,7 +1278,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1407
1278
|
typeParameters: functionInfo.typeParameters
|
|
1408
1279
|
};
|
|
1409
1280
|
},
|
|
1410
|
-
|
|
1411
1281
|
DeclareFunction(node) {
|
|
1412
1282
|
const id = transform.Identifier(node.id, false);
|
|
1413
1283
|
const functionInfo = transform.FunctionTypeAnnotation(node.id.typeAnnotation.typeAnnotation);
|
|
@@ -1429,14 +1299,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1429
1299
|
typeParameters: functionInfo.typeParameters
|
|
1430
1300
|
};
|
|
1431
1301
|
},
|
|
1432
|
-
|
|
1433
1302
|
DeclareInterface(node) {
|
|
1434
1303
|
const transformedBody = transform.ObjectTypeAnnotation(node.body);
|
|
1435
|
-
|
|
1436
1304
|
if (transformedBody.type !== 'TSTypeLiteral') {
|
|
1437
1305
|
throw translationError(node.body, 'Spreads in interfaces are not allowed');
|
|
1438
1306
|
}
|
|
1439
|
-
|
|
1440
1307
|
return {
|
|
1441
1308
|
type: 'TSInterfaceDeclaration',
|
|
1442
1309
|
loc: DUMMY_LOC,
|
|
@@ -1451,7 +1318,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1451
1318
|
typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
|
|
1452
1319
|
};
|
|
1453
1320
|
},
|
|
1454
|
-
|
|
1455
1321
|
DeclareTypeAlias(node) {
|
|
1456
1322
|
return {
|
|
1457
1323
|
type: 'TSTypeAliasDeclaration',
|
|
@@ -1462,7 +1328,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1462
1328
|
typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
|
|
1463
1329
|
};
|
|
1464
1330
|
},
|
|
1465
|
-
|
|
1466
1331
|
DeclareOpaqueType(node) {
|
|
1467
1332
|
if (node.supertype == null && node.typeParameters == null) {
|
|
1468
1333
|
const name = `__${node.id.name}__`;
|
|
@@ -1502,7 +1367,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1502
1367
|
}
|
|
1503
1368
|
};
|
|
1504
1369
|
}
|
|
1505
|
-
|
|
1506
1370
|
return {
|
|
1507
1371
|
type: 'TSTypeAliasDeclaration',
|
|
1508
1372
|
loc: DUMMY_LOC,
|
|
@@ -1515,43 +1379,41 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1515
1379
|
typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
|
|
1516
1380
|
};
|
|
1517
1381
|
},
|
|
1518
|
-
|
|
1519
1382
|
DeclareVariable(node) {
|
|
1520
1383
|
return {
|
|
1521
1384
|
type: 'VariableDeclaration',
|
|
1522
1385
|
loc: DUMMY_LOC,
|
|
1523
1386
|
declare: true,
|
|
1524
|
-
declarations:
|
|
1525
|
-
type
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1387
|
+
declarations: node.declarations.map(declaration => {
|
|
1388
|
+
if (declaration.id.type !== 'Identifier') {
|
|
1389
|
+
throw translationError(declaration.id, `DeclareVariable: unsupported declarator of type "${declaration.id.type}"`);
|
|
1390
|
+
}
|
|
1391
|
+
return {
|
|
1392
|
+
type: 'VariableDeclarator',
|
|
1393
|
+
loc: DUMMY_LOC,
|
|
1394
|
+
declare: true,
|
|
1395
|
+
id: transform.Identifier(declaration.id, true),
|
|
1396
|
+
init: null
|
|
1397
|
+
};
|
|
1398
|
+
}),
|
|
1531
1399
|
kind: node.kind
|
|
1532
1400
|
};
|
|
1533
1401
|
},
|
|
1534
|
-
|
|
1535
1402
|
DeclareEnum(node) {
|
|
1536
1403
|
return EnumImpl(node);
|
|
1537
1404
|
},
|
|
1538
|
-
|
|
1539
1405
|
EmptyTypeAnnotation(node) {
|
|
1540
1406
|
return unsupportedAnnotation(node, 'empty type');
|
|
1541
1407
|
},
|
|
1542
|
-
|
|
1543
1408
|
EnumDeclaration(node) {
|
|
1544
1409
|
return EnumImpl(node);
|
|
1545
1410
|
},
|
|
1546
|
-
|
|
1547
1411
|
DeclareModuleExports(node) {
|
|
1548
1412
|
throw translationError(node, 'CommonJS exports are not supported.');
|
|
1549
1413
|
},
|
|
1550
|
-
|
|
1551
1414
|
ExistsTypeAnnotation(node) {
|
|
1552
1415
|
return unsupportedAnnotation(node, 'existential type');
|
|
1553
1416
|
},
|
|
1554
|
-
|
|
1555
1417
|
ExportAllDeclaration(node) {
|
|
1556
1418
|
return {
|
|
1557
1419
|
type: 'ExportAllDeclaration',
|
|
@@ -1562,7 +1424,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1562
1424
|
exported: node.exported == null ? null : transform.Identifier(node.exported)
|
|
1563
1425
|
};
|
|
1564
1426
|
},
|
|
1565
|
-
|
|
1566
1427
|
ExportNamedDeclaration(node) {
|
|
1567
1428
|
if (node.source != null || node.specifiers.length > 0) {
|
|
1568
1429
|
return {
|
|
@@ -1575,12 +1436,10 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1575
1436
|
specifiers: node.specifiers.map(transform.ExportSpecifier)
|
|
1576
1437
|
};
|
|
1577
1438
|
}
|
|
1578
|
-
|
|
1579
1439
|
const [exportedDeclaration, mergedDeclaration] = (() => {
|
|
1580
1440
|
if (node.declaration == null) {
|
|
1581
1441
|
return [null, null];
|
|
1582
1442
|
}
|
|
1583
|
-
|
|
1584
1443
|
switch (node.declaration.type) {
|
|
1585
1444
|
case 'ClassDeclaration':
|
|
1586
1445
|
case 'ComponentDeclaration':
|
|
@@ -1589,24 +1448,19 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1589
1448
|
case 'FunctionDeclaration':
|
|
1590
1449
|
case 'VariableDeclaration':
|
|
1591
1450
|
throw unexpectedTranslationError(node.declaration, `Unexpected named declaration found ${node.declaration.type}`);
|
|
1592
|
-
|
|
1593
1451
|
case 'EnumDeclaration':
|
|
1594
1452
|
{
|
|
1595
1453
|
const result = transform.EnumDeclaration(node.declaration);
|
|
1596
1454
|
return Array.isArray(result) ? result : [result, null];
|
|
1597
1455
|
}
|
|
1598
|
-
|
|
1599
1456
|
case 'InterfaceDeclaration':
|
|
1600
1457
|
return [transform.InterfaceDeclaration(node.declaration), null];
|
|
1601
|
-
|
|
1602
1458
|
case 'OpaqueType':
|
|
1603
1459
|
return [transform.OpaqueType(node.declaration), null];
|
|
1604
|
-
|
|
1605
1460
|
case 'TypeAlias':
|
|
1606
1461
|
return [transform.TypeAlias(node.declaration), null];
|
|
1607
1462
|
}
|
|
1608
1463
|
})();
|
|
1609
|
-
|
|
1610
1464
|
const mainExport = {
|
|
1611
1465
|
type: 'ExportNamedDeclaration',
|
|
1612
1466
|
loc: DUMMY_LOC,
|
|
@@ -1616,7 +1470,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1616
1470
|
source: null,
|
|
1617
1471
|
specifiers: []
|
|
1618
1472
|
};
|
|
1619
|
-
|
|
1620
1473
|
if (mergedDeclaration != null) {
|
|
1621
1474
|
return [mainExport, {
|
|
1622
1475
|
type: 'ExportNamedDeclaration',
|
|
@@ -1628,10 +1481,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1628
1481
|
specifiers: []
|
|
1629
1482
|
}];
|
|
1630
1483
|
}
|
|
1631
|
-
|
|
1632
1484
|
return mainExport;
|
|
1633
1485
|
},
|
|
1634
|
-
|
|
1635
1486
|
ExportSpecifier(node) {
|
|
1636
1487
|
return {
|
|
1637
1488
|
type: 'ExportSpecifier',
|
|
@@ -1641,10 +1492,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1641
1492
|
exportKind: 'value'
|
|
1642
1493
|
};
|
|
1643
1494
|
},
|
|
1644
|
-
|
|
1645
1495
|
FunctionTypeAnnotation(node) {
|
|
1646
1496
|
const params = node.params.map(transform.FunctionTypeParam);
|
|
1647
|
-
|
|
1648
1497
|
if (node.type === 'FunctionTypeAnnotation' && node.this != null) {
|
|
1649
1498
|
params.unshift({
|
|
1650
1499
|
type: 'Identifier',
|
|
@@ -1657,7 +1506,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1657
1506
|
}
|
|
1658
1507
|
});
|
|
1659
1508
|
}
|
|
1660
|
-
|
|
1661
1509
|
if (node.rest != null) {
|
|
1662
1510
|
const rest = node.rest;
|
|
1663
1511
|
params.push({
|
|
@@ -1675,7 +1523,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1675
1523
|
}
|
|
1676
1524
|
});
|
|
1677
1525
|
}
|
|
1678
|
-
|
|
1679
1526
|
return {
|
|
1680
1527
|
type: 'TSFunctionType',
|
|
1681
1528
|
loc: DUMMY_LOC,
|
|
@@ -1688,7 +1535,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1688
1535
|
typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters)
|
|
1689
1536
|
};
|
|
1690
1537
|
},
|
|
1691
|
-
|
|
1692
1538
|
FunctionTypeParam(node, idx = 0) {
|
|
1693
1539
|
return {
|
|
1694
1540
|
type: 'Identifier',
|
|
@@ -1702,12 +1548,10 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1702
1548
|
optional: node.optional
|
|
1703
1549
|
};
|
|
1704
1550
|
},
|
|
1705
|
-
|
|
1706
1551
|
GenericTypeAnnotation(node) {
|
|
1707
1552
|
const [fullTypeName, baseId] = (() => {
|
|
1708
1553
|
let names = [];
|
|
1709
1554
|
let currentNode = node.id;
|
|
1710
|
-
|
|
1711
1555
|
while (currentNode != null) {
|
|
1712
1556
|
switch (currentNode.type) {
|
|
1713
1557
|
case 'Identifier':
|
|
@@ -1715,7 +1559,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1715
1559
|
names.unshift(currentNode.name);
|
|
1716
1560
|
return [names.join('.'), currentNode];
|
|
1717
1561
|
}
|
|
1718
|
-
|
|
1719
1562
|
case 'QualifiedTypeIdentifier':
|
|
1720
1563
|
{
|
|
1721
1564
|
names.unshift(currentNode.id.name);
|
|
@@ -1724,54 +1567,40 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1724
1567
|
}
|
|
1725
1568
|
}
|
|
1726
1569
|
}
|
|
1727
|
-
|
|
1728
1570
|
throw translationError(node, `Invalid program state, types should only contain 'Identifier' and 'QualifiedTypeIdentifier' nodes.`);
|
|
1729
1571
|
})();
|
|
1730
|
-
|
|
1731
1572
|
const assertHasExactlyNTypeParameters = count => {
|
|
1732
1573
|
if (node.typeParameters != null) {
|
|
1733
1574
|
if (node.typeParameters.params.length !== count) {
|
|
1734
1575
|
throw translationError(node, `Expected exactly ${count} type parameter${count > 1 ? 's' : ''} with \`${fullTypeName}\``);
|
|
1735
1576
|
}
|
|
1736
|
-
|
|
1737
1577
|
const res = [];
|
|
1738
|
-
|
|
1739
1578
|
for (const param of node.typeParameters.params) {
|
|
1740
1579
|
res.push(transformTypeAnnotationType(param));
|
|
1741
1580
|
}
|
|
1742
|
-
|
|
1743
1581
|
return res;
|
|
1744
1582
|
}
|
|
1745
|
-
|
|
1746
1583
|
if (count !== 0) {
|
|
1747
1584
|
throw translationError(node, `Expected no type parameters with \`${fullTypeName}\``);
|
|
1748
1585
|
}
|
|
1749
|
-
|
|
1750
1586
|
return [];
|
|
1751
1587
|
};
|
|
1752
|
-
|
|
1753
1588
|
function assertHasTypeParametersInRange(min, max) {
|
|
1754
1589
|
const {
|
|
1755
1590
|
typeParameters
|
|
1756
1591
|
} = node;
|
|
1757
|
-
|
|
1758
1592
|
if (typeParameters == null) {
|
|
1759
1593
|
if (min > 0) {
|
|
1760
1594
|
throw translationError(node, `Expected between ${min} and ${max} type parameters with \`${fullTypeName}\`, but got none.`);
|
|
1761
1595
|
}
|
|
1762
|
-
|
|
1763
1596
|
return [];
|
|
1764
1597
|
}
|
|
1765
|
-
|
|
1766
1598
|
const params = typeParameters.params;
|
|
1767
|
-
|
|
1768
1599
|
if (params.length < min || params.length > max) {
|
|
1769
1600
|
throw translationError(node, `Expected between ${min} and ${max} type parameters with \`${fullTypeName}\`, but got ${params.length}.`);
|
|
1770
1601
|
}
|
|
1771
|
-
|
|
1772
1602
|
return typeParameters.params.map(transformTypeAnnotationType);
|
|
1773
1603
|
}
|
|
1774
|
-
|
|
1775
1604
|
switch (fullTypeName) {
|
|
1776
1605
|
case '$Call':
|
|
1777
1606
|
case '$ObjMap':
|
|
@@ -1781,7 +1610,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1781
1610
|
{
|
|
1782
1611
|
return unsupportedAnnotation(node, fullTypeName);
|
|
1783
1612
|
}
|
|
1784
|
-
|
|
1785
1613
|
case '$ArrayBufferView':
|
|
1786
1614
|
{
|
|
1787
1615
|
return {
|
|
@@ -1794,7 +1622,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1794
1622
|
}
|
|
1795
1623
|
};
|
|
1796
1624
|
}
|
|
1797
|
-
|
|
1798
1625
|
case '$ArrayLike':
|
|
1799
1626
|
{
|
|
1800
1627
|
return {
|
|
@@ -1812,7 +1639,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1812
1639
|
}
|
|
1813
1640
|
};
|
|
1814
1641
|
}
|
|
1815
|
-
|
|
1816
1642
|
case '$Diff':
|
|
1817
1643
|
case '$Rest':
|
|
1818
1644
|
{
|
|
@@ -1855,7 +1681,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1855
1681
|
}
|
|
1856
1682
|
};
|
|
1857
1683
|
}
|
|
1858
|
-
|
|
1859
1684
|
case '$ElementType':
|
|
1860
1685
|
case '$PropertyType':
|
|
1861
1686
|
{
|
|
@@ -1867,20 +1692,16 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1867
1692
|
indexType: params[1]
|
|
1868
1693
|
};
|
|
1869
1694
|
}
|
|
1870
|
-
|
|
1871
1695
|
case '$Exact':
|
|
1872
1696
|
{
|
|
1873
1697
|
return assertHasExactlyNTypeParameters(1)[0];
|
|
1874
1698
|
}
|
|
1875
|
-
|
|
1876
1699
|
case '$Exports':
|
|
1877
1700
|
{
|
|
1878
1701
|
const moduleName = assertHasExactlyNTypeParameters(1)[0];
|
|
1879
|
-
|
|
1880
1702
|
if (moduleName.type !== 'TSLiteralType' || moduleName.literal.type !== 'Literal' || typeof moduleName.literal.value !== 'string') {
|
|
1881
1703
|
throw translationError(node, '$Exports must have a string literal argument');
|
|
1882
1704
|
}
|
|
1883
|
-
|
|
1884
1705
|
return {
|
|
1885
1706
|
type: 'TSTypeQuery',
|
|
1886
1707
|
loc: DUMMY_LOC,
|
|
@@ -1895,7 +1716,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1895
1716
|
}
|
|
1896
1717
|
};
|
|
1897
1718
|
}
|
|
1898
|
-
|
|
1899
1719
|
case '$FlowFixMe':
|
|
1900
1720
|
{
|
|
1901
1721
|
return {
|
|
@@ -1903,7 +1723,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1903
1723
|
loc: DUMMY_LOC
|
|
1904
1724
|
};
|
|
1905
1725
|
}
|
|
1906
|
-
|
|
1907
1726
|
case '$KeyMirror':
|
|
1908
1727
|
{
|
|
1909
1728
|
return {
|
|
@@ -1949,7 +1768,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1949
1768
|
}
|
|
1950
1769
|
};
|
|
1951
1770
|
}
|
|
1952
|
-
|
|
1953
1771
|
case '$Keys':
|
|
1954
1772
|
{
|
|
1955
1773
|
return {
|
|
@@ -1959,7 +1777,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1959
1777
|
typeAnnotation: assertHasExactlyNTypeParameters(1)[0]
|
|
1960
1778
|
};
|
|
1961
1779
|
}
|
|
1962
|
-
|
|
1963
1780
|
case '$NonMaybeType':
|
|
1964
1781
|
{
|
|
1965
1782
|
return {
|
|
@@ -1977,7 +1794,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1977
1794
|
}
|
|
1978
1795
|
};
|
|
1979
1796
|
}
|
|
1980
|
-
|
|
1981
1797
|
case '$ReadOnly':
|
|
1982
1798
|
{
|
|
1983
1799
|
return {
|
|
@@ -1995,7 +1811,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
1995
1811
|
}
|
|
1996
1812
|
};
|
|
1997
1813
|
}
|
|
1998
|
-
|
|
1999
1814
|
case '$ReadOnlyArray':
|
|
2000
1815
|
{
|
|
2001
1816
|
return {
|
|
@@ -2013,7 +1828,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2013
1828
|
}
|
|
2014
1829
|
};
|
|
2015
1830
|
}
|
|
2016
|
-
|
|
2017
1831
|
case '$ReadOnlyMap':
|
|
2018
1832
|
{
|
|
2019
1833
|
return {
|
|
@@ -2031,7 +1845,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2031
1845
|
}
|
|
2032
1846
|
};
|
|
2033
1847
|
}
|
|
2034
|
-
|
|
2035
1848
|
case '$ReadOnlySet':
|
|
2036
1849
|
{
|
|
2037
1850
|
return {
|
|
@@ -2049,7 +1862,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2049
1862
|
}
|
|
2050
1863
|
};
|
|
2051
1864
|
}
|
|
2052
|
-
|
|
2053
1865
|
case '$Values':
|
|
2054
1866
|
case 'Values':
|
|
2055
1867
|
{
|
|
@@ -2066,15 +1878,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2066
1878
|
}
|
|
2067
1879
|
};
|
|
2068
1880
|
}
|
|
2069
|
-
|
|
2070
1881
|
case 'Class':
|
|
2071
1882
|
{
|
|
2072
1883
|
const param = assertHasExactlyNTypeParameters(1)[0];
|
|
2073
|
-
|
|
2074
1884
|
if (param.type !== 'TSTypeReference') {
|
|
2075
1885
|
throw translationError(node, 'Expected a type reference within Class<T>');
|
|
2076
1886
|
}
|
|
2077
|
-
|
|
2078
1887
|
return {
|
|
2079
1888
|
type: 'TSConstructorType',
|
|
2080
1889
|
loc: DUMMY_LOC,
|
|
@@ -2107,18 +1916,14 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2107
1916
|
}
|
|
2108
1917
|
};
|
|
2109
1918
|
}
|
|
2110
|
-
|
|
2111
1919
|
case 'StringPrefix':
|
|
2112
1920
|
{
|
|
2113
1921
|
var _params$;
|
|
2114
|
-
|
|
2115
1922
|
const params = assertHasTypeParametersInRange(1, 2);
|
|
2116
1923
|
const prefix = params[0];
|
|
2117
|
-
|
|
2118
1924
|
if (prefix.type !== 'TSLiteralType' || typeof prefix.literal.value !== 'string') {
|
|
2119
1925
|
throw translationError(node, 'Expected a string literal for the first type parameter.');
|
|
2120
1926
|
}
|
|
2121
|
-
|
|
2122
1927
|
const prefixStr = prefix.literal.value;
|
|
2123
1928
|
const remainder = (_params$ = params[1]) != null ? _params$ : {
|
|
2124
1929
|
type: 'TSStringKeyword',
|
|
@@ -2147,18 +1952,14 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2147
1952
|
types: [remainder]
|
|
2148
1953
|
};
|
|
2149
1954
|
}
|
|
2150
|
-
|
|
2151
1955
|
case 'StringSuffix':
|
|
2152
1956
|
{
|
|
2153
1957
|
var _params$2;
|
|
2154
|
-
|
|
2155
1958
|
const params = assertHasTypeParametersInRange(1, 2);
|
|
2156
1959
|
const suffix = params[0];
|
|
2157
|
-
|
|
2158
1960
|
if (suffix.type !== 'TSLiteralType' || typeof suffix.literal.value !== 'string') {
|
|
2159
1961
|
throw translationError(node, 'Expected a string literal for the first type parameter.');
|
|
2160
1962
|
}
|
|
2161
|
-
|
|
2162
1963
|
const suffixStr = suffix.literal.value;
|
|
2163
1964
|
const remainder = (_params$2 = params[1]) != null ? _params$2 : {
|
|
2164
1965
|
type: 'TSStringKeyword',
|
|
@@ -2188,10 +1989,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2188
1989
|
};
|
|
2189
1990
|
}
|
|
2190
1991
|
}
|
|
2191
|
-
|
|
2192
1992
|
const validReactImportOrGlobal = isValidReactImportOrGlobal(baseId);
|
|
2193
1993
|
const hasReactImport = isReactImport(baseId, baseId.name);
|
|
2194
|
-
|
|
2195
1994
|
if (validReactImportOrGlobal || hasReactImport) {
|
|
2196
1995
|
switch (fullTypeName) {
|
|
2197
1996
|
case 'React.ChildrenArray':
|
|
@@ -2217,22 +2016,17 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2217
2016
|
}]
|
|
2218
2017
|
};
|
|
2219
2018
|
}
|
|
2220
|
-
|
|
2221
2019
|
case 'React.Component':
|
|
2222
2020
|
case 'React$Component':
|
|
2223
2021
|
{
|
|
2224
2022
|
const typeParameters = node.typeParameters;
|
|
2225
|
-
|
|
2226
2023
|
if (typeParameters == null || typeParameters.params.length === 0) {
|
|
2227
2024
|
throw translationError(node, `Expected at least 1 type parameter with \`${fullTypeName}\``);
|
|
2228
2025
|
}
|
|
2229
|
-
|
|
2230
2026
|
const params = typeParameters.params;
|
|
2231
|
-
|
|
2232
2027
|
if (params.length > 2) {
|
|
2233
2028
|
throw translationError(node, `Expected at no more than 2 type parameters with \`${fullTypeName}\``);
|
|
2234
2029
|
}
|
|
2235
|
-
|
|
2236
2030
|
return {
|
|
2237
2031
|
type: 'TSTypeReference',
|
|
2238
2032
|
loc: DUMMY_LOC,
|
|
@@ -2253,7 +2047,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2253
2047
|
}
|
|
2254
2048
|
};
|
|
2255
2049
|
}
|
|
2256
|
-
|
|
2257
2050
|
case 'React$Context':
|
|
2258
2051
|
case 'React.Context':
|
|
2259
2052
|
return {
|
|
@@ -2275,7 +2068,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2275
2068
|
params: assertHasExactlyNTypeParameters(1)
|
|
2276
2069
|
}
|
|
2277
2070
|
};
|
|
2278
|
-
|
|
2279
2071
|
case 'React.Key':
|
|
2280
2072
|
case 'React$Key':
|
|
2281
2073
|
assertHasExactlyNTypeParameters(0);
|
|
@@ -2293,7 +2085,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2293
2085
|
}
|
|
2294
2086
|
}
|
|
2295
2087
|
};
|
|
2296
|
-
|
|
2297
2088
|
case 'React$ElementType':
|
|
2298
2089
|
case 'React.ElementType':
|
|
2299
2090
|
{
|
|
@@ -2314,7 +2105,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2314
2105
|
typeArguments: undefined
|
|
2315
2106
|
};
|
|
2316
2107
|
}
|
|
2317
|
-
|
|
2318
2108
|
case 'React$Node':
|
|
2319
2109
|
case 'React.Node':
|
|
2320
2110
|
{
|
|
@@ -2335,7 +2125,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2335
2125
|
typeArguments: undefined
|
|
2336
2126
|
};
|
|
2337
2127
|
}
|
|
2338
|
-
|
|
2339
2128
|
case 'React$Element':
|
|
2340
2129
|
case 'React.Element':
|
|
2341
2130
|
{
|
|
@@ -2359,7 +2148,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2359
2148
|
}
|
|
2360
2149
|
};
|
|
2361
2150
|
}
|
|
2362
|
-
|
|
2363
2151
|
case 'React$ElementRef':
|
|
2364
2152
|
case 'React.ElementRef':
|
|
2365
2153
|
return {
|
|
@@ -2381,7 +2169,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2381
2169
|
params: assertHasExactlyNTypeParameters(1)
|
|
2382
2170
|
}
|
|
2383
2171
|
};
|
|
2384
|
-
|
|
2385
2172
|
case 'React$FragmentType':
|
|
2386
2173
|
case 'React.Fragment':
|
|
2387
2174
|
assertHasExactlyNTypeParameters(0);
|
|
@@ -2399,7 +2186,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2399
2186
|
}
|
|
2400
2187
|
}
|
|
2401
2188
|
};
|
|
2402
|
-
|
|
2403
2189
|
case 'React$MixedElement':
|
|
2404
2190
|
case 'React.MixedElement':
|
|
2405
2191
|
{
|
|
@@ -2433,7 +2219,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2433
2219
|
typeArguments: undefined
|
|
2434
2220
|
};
|
|
2435
2221
|
}
|
|
2436
|
-
|
|
2437
2222
|
case 'React.ComponentType':
|
|
2438
2223
|
case 'React$ComponentType':
|
|
2439
2224
|
{
|
|
@@ -2457,27 +2242,21 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2457
2242
|
}
|
|
2458
2243
|
};
|
|
2459
2244
|
}
|
|
2460
|
-
|
|
2461
2245
|
case 'React.AbstractComponent':
|
|
2462
2246
|
case 'React$AbstractComponent':
|
|
2463
2247
|
{
|
|
2464
2248
|
const typeParameters = node.typeParameters;
|
|
2465
|
-
|
|
2466
2249
|
if (typeParameters == null || typeParameters.params.length === 0) {
|
|
2467
2250
|
throw translationError(node, `Expected at least 1 type parameter with \`${fullTypeName}\``);
|
|
2468
2251
|
}
|
|
2469
|
-
|
|
2470
2252
|
const params = typeParameters.params;
|
|
2471
|
-
|
|
2472
2253
|
if (params.length > 3) {
|
|
2473
2254
|
throw translationError(node, `Expected at no more than 3 type parameters with \`${fullTypeName}\``);
|
|
2474
2255
|
}
|
|
2475
|
-
|
|
2476
2256
|
const newParams = (() => {
|
|
2477
2257
|
if (params.length === 1) {
|
|
2478
2258
|
return assertHasExactlyNTypeParameters(1);
|
|
2479
2259
|
}
|
|
2480
|
-
|
|
2481
2260
|
const props = transformTypeAnnotationType(params[0]);
|
|
2482
2261
|
const ref = transformTypeAnnotationType(params[1]);
|
|
2483
2262
|
return [{
|
|
@@ -2508,7 +2287,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2508
2287
|
}]
|
|
2509
2288
|
}];
|
|
2510
2289
|
})();
|
|
2511
|
-
|
|
2512
2290
|
return {
|
|
2513
2291
|
type: 'TSTypeReference',
|
|
2514
2292
|
loc: DUMMY_LOC,
|
|
@@ -2529,7 +2307,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2529
2307
|
}
|
|
2530
2308
|
};
|
|
2531
2309
|
}
|
|
2532
|
-
|
|
2533
2310
|
case 'React.ElementProps':
|
|
2534
2311
|
case 'React$ElementProps':
|
|
2535
2312
|
{
|
|
@@ -2553,7 +2330,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2553
2330
|
}
|
|
2554
2331
|
};
|
|
2555
2332
|
}
|
|
2556
|
-
|
|
2557
2333
|
case 'React.ElementConfig':
|
|
2558
2334
|
case 'React$ElementConfig':
|
|
2559
2335
|
{
|
|
@@ -2609,7 +2385,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2609
2385
|
}
|
|
2610
2386
|
};
|
|
2611
2387
|
}
|
|
2612
|
-
|
|
2613
2388
|
case 'React.RefSetter':
|
|
2614
2389
|
case 'React$RefSetter':
|
|
2615
2390
|
return {
|
|
@@ -2631,12 +2406,10 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2631
2406
|
params: assertHasExactlyNTypeParameters(1)
|
|
2632
2407
|
}
|
|
2633
2408
|
};
|
|
2634
|
-
|
|
2635
2409
|
default:
|
|
2636
2410
|
return unsupportedAnnotation(node, fullTypeName);
|
|
2637
2411
|
}
|
|
2638
2412
|
}
|
|
2639
|
-
|
|
2640
2413
|
return {
|
|
2641
2414
|
type: 'TSTypeReference',
|
|
2642
2415
|
loc: DUMMY_LOC,
|
|
@@ -2644,7 +2417,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2644
2417
|
typeArguments: transform.TypeParameterInstantiation(node.typeParameters)
|
|
2645
2418
|
};
|
|
2646
2419
|
},
|
|
2647
|
-
|
|
2648
2420
|
Identifier(node, includeTypeAnnotation = true) {
|
|
2649
2421
|
return {
|
|
2650
2422
|
type: 'Identifier',
|
|
@@ -2655,7 +2427,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2655
2427
|
} : {})
|
|
2656
2428
|
};
|
|
2657
2429
|
},
|
|
2658
|
-
|
|
2659
2430
|
IndexedAccessType(node) {
|
|
2660
2431
|
return {
|
|
2661
2432
|
type: 'TSIndexedAccessType',
|
|
@@ -2664,11 +2435,9 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2664
2435
|
indexType: transformTypeAnnotationType(node.indexType)
|
|
2665
2436
|
};
|
|
2666
2437
|
},
|
|
2667
|
-
|
|
2668
2438
|
InterfaceDeclaration(node) {
|
|
2669
2439
|
return transform.DeclareInterface(node);
|
|
2670
2440
|
},
|
|
2671
|
-
|
|
2672
2441
|
ImportAttribute(node) {
|
|
2673
2442
|
return {
|
|
2674
2443
|
type: 'ImportAttribute',
|
|
@@ -2677,7 +2446,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2677
2446
|
value: transform.Literal(node.value)
|
|
2678
2447
|
};
|
|
2679
2448
|
},
|
|
2680
|
-
|
|
2681
2449
|
ImportDeclaration(node) {
|
|
2682
2450
|
const importKind = node.importKind;
|
|
2683
2451
|
const specifiers = [];
|
|
@@ -2702,10 +2470,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2702
2470
|
});
|
|
2703
2471
|
return id;
|
|
2704
2472
|
}
|
|
2705
|
-
|
|
2706
2473
|
return transform.Identifier(spec.local, false);
|
|
2707
2474
|
})();
|
|
2708
|
-
|
|
2709
2475
|
switch (spec.type) {
|
|
2710
2476
|
case 'ImportDefaultSpecifier':
|
|
2711
2477
|
specifiers.push({
|
|
@@ -2714,7 +2480,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2714
2480
|
local: id
|
|
2715
2481
|
});
|
|
2716
2482
|
return;
|
|
2717
|
-
|
|
2718
2483
|
case 'ImportNamespaceSpecifier':
|
|
2719
2484
|
specifiers.push({
|
|
2720
2485
|
type: 'ImportNamespaceSpecifier',
|
|
@@ -2722,7 +2487,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2722
2487
|
local: id
|
|
2723
2488
|
});
|
|
2724
2489
|
return;
|
|
2725
|
-
|
|
2726
2490
|
case 'ImportSpecifier':
|
|
2727
2491
|
specifiers.push({
|
|
2728
2492
|
type: 'ImportSpecifier',
|
|
@@ -2744,7 +2508,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2744
2508
|
}] : [];
|
|
2745
2509
|
return [...out, ...unsupportedSpecifiers];
|
|
2746
2510
|
},
|
|
2747
|
-
|
|
2748
2511
|
InterfaceExtends(node) {
|
|
2749
2512
|
return {
|
|
2750
2513
|
type: 'TSInterfaceHeritage',
|
|
@@ -2753,7 +2516,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2753
2516
|
typeArguments: transform.TypeParameterInstantiation(node.typeParameters)
|
|
2754
2517
|
};
|
|
2755
2518
|
},
|
|
2756
|
-
|
|
2757
2519
|
InterfaceTypeAnnotation(node) {
|
|
2758
2520
|
if (node.extends) {
|
|
2759
2521
|
return {
|
|
@@ -2767,10 +2529,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2767
2529
|
})), transform.ObjectTypeAnnotation(node.body)]
|
|
2768
2530
|
};
|
|
2769
2531
|
}
|
|
2770
|
-
|
|
2771
2532
|
return transform.ObjectTypeAnnotation(node.body);
|
|
2772
2533
|
},
|
|
2773
|
-
|
|
2774
2534
|
IntersectionTypeAnnotation(node) {
|
|
2775
2535
|
return {
|
|
2776
2536
|
type: 'TSIntersectionType',
|
|
@@ -2778,36 +2538,28 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2778
2538
|
types: node.types.map(transformTypeAnnotationType)
|
|
2779
2539
|
};
|
|
2780
2540
|
},
|
|
2781
|
-
|
|
2782
2541
|
Literal(node) {
|
|
2783
2542
|
switch (node.literalType) {
|
|
2784
2543
|
case 'bigint':
|
|
2785
2544
|
return transform.BigIntLiteral(node);
|
|
2786
|
-
|
|
2787
2545
|
case 'boolean':
|
|
2788
2546
|
return transform.BooleanLiteral(node);
|
|
2789
|
-
|
|
2790
2547
|
case 'null':
|
|
2791
2548
|
return transform.NullLiteral(node);
|
|
2792
|
-
|
|
2793
2549
|
case 'numeric':
|
|
2794
2550
|
return transform.NumericLiteral(node);
|
|
2795
|
-
|
|
2796
2551
|
case 'regexp':
|
|
2797
2552
|
return transform.RegExpLiteral(node);
|
|
2798
|
-
|
|
2799
2553
|
case 'string':
|
|
2800
2554
|
return transform.StringLiteral(node);
|
|
2801
2555
|
}
|
|
2802
2556
|
},
|
|
2803
|
-
|
|
2804
2557
|
MixedTypeAnnotation(_node) {
|
|
2805
2558
|
return {
|
|
2806
2559
|
type: 'TSUnknownKeyword',
|
|
2807
2560
|
loc: DUMMY_LOC
|
|
2808
2561
|
};
|
|
2809
2562
|
},
|
|
2810
|
-
|
|
2811
2563
|
NullLiteral(_node) {
|
|
2812
2564
|
return {
|
|
2813
2565
|
type: 'Literal',
|
|
@@ -2816,14 +2568,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2816
2568
|
value: null
|
|
2817
2569
|
};
|
|
2818
2570
|
},
|
|
2819
|
-
|
|
2820
2571
|
NullLiteralTypeAnnotation(_node) {
|
|
2821
2572
|
return {
|
|
2822
2573
|
type: 'TSNullKeyword',
|
|
2823
2574
|
loc: DUMMY_LOC
|
|
2824
2575
|
};
|
|
2825
2576
|
},
|
|
2826
|
-
|
|
2827
2577
|
NullableTypeAnnotation(node) {
|
|
2828
2578
|
return {
|
|
2829
2579
|
type: 'TSUnionType',
|
|
@@ -2837,7 +2587,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2837
2587
|
}, transformTypeAnnotationType(node.typeAnnotation)]
|
|
2838
2588
|
};
|
|
2839
2589
|
},
|
|
2840
|
-
|
|
2841
2590
|
NumberLiteralTypeAnnotation(node) {
|
|
2842
2591
|
return {
|
|
2843
2592
|
type: 'TSLiteralType',
|
|
@@ -2850,14 +2599,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2850
2599
|
}
|
|
2851
2600
|
};
|
|
2852
2601
|
},
|
|
2853
|
-
|
|
2854
2602
|
NumberTypeAnnotation(_node) {
|
|
2855
2603
|
return {
|
|
2856
2604
|
type: 'TSNumberKeyword',
|
|
2857
2605
|
loc: DUMMY_LOC
|
|
2858
2606
|
};
|
|
2859
2607
|
},
|
|
2860
|
-
|
|
2861
2608
|
NumericLiteral(node) {
|
|
2862
2609
|
return {
|
|
2863
2610
|
type: 'Literal',
|
|
@@ -2866,11 +2613,9 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2866
2613
|
value: node.value
|
|
2867
2614
|
};
|
|
2868
2615
|
},
|
|
2869
|
-
|
|
2870
2616
|
ObjectTypeAnnotation(node) {
|
|
2871
2617
|
if (node.properties.length === 1 && node.properties[0].type === 'ObjectTypeMappedTypeProperty') {
|
|
2872
2618
|
var _prop$variance, _prop$variance2;
|
|
2873
|
-
|
|
2874
2619
|
const prop = node.properties[0];
|
|
2875
2620
|
const tsProp = {
|
|
2876
2621
|
type: 'TSMappedType',
|
|
@@ -2900,43 +2645,35 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2900
2645
|
};
|
|
2901
2646
|
return tsProp;
|
|
2902
2647
|
}
|
|
2903
|
-
|
|
2904
2648
|
const members = [];
|
|
2905
|
-
|
|
2906
2649
|
for (const callProp of node.callProperties) {
|
|
2907
2650
|
members.push({
|
|
2908
2651
|
start: callProp.range[0],
|
|
2909
2652
|
node: transform.ObjectTypeCallProperty(callProp)
|
|
2910
2653
|
});
|
|
2911
2654
|
}
|
|
2912
|
-
|
|
2913
2655
|
for (const indexer of node.indexers) {
|
|
2914
2656
|
members.push({
|
|
2915
2657
|
start: indexer.range[0],
|
|
2916
2658
|
node: transform.ObjectTypeIndexer(indexer)
|
|
2917
2659
|
});
|
|
2918
2660
|
}
|
|
2919
|
-
|
|
2920
2661
|
if (node.internalSlots.length > 0) {
|
|
2921
2662
|
return unsupportedAnnotation(node.internalSlots[0], 'internal slots');
|
|
2922
2663
|
}
|
|
2923
|
-
|
|
2924
2664
|
if (!node.properties.find(FlowESTree.isObjectTypeSpreadProperty)) {
|
|
2925
2665
|
for (const property of node.properties) {
|
|
2926
2666
|
if (property.type === 'ObjectTypeSpreadProperty') {
|
|
2927
2667
|
throw unexpectedTranslationError(property, 'Impossible state');
|
|
2928
2668
|
}
|
|
2929
|
-
|
|
2930
2669
|
if (property.type === 'ObjectTypeMappedTypeProperty') {
|
|
2931
2670
|
return unsupportedAnnotation(property, 'object type with mapped type property along with other properties');
|
|
2932
2671
|
}
|
|
2933
|
-
|
|
2934
2672
|
members.push({
|
|
2935
2673
|
start: property.range[0],
|
|
2936
2674
|
node: transform.ObjectTypeProperty(property)
|
|
2937
2675
|
});
|
|
2938
2676
|
}
|
|
2939
|
-
|
|
2940
2677
|
const tsBody = members.sort((a, b) => a.start - b.start).map(({
|
|
2941
2678
|
node
|
|
2942
2679
|
}) => node);
|
|
@@ -2947,25 +2684,19 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2947
2684
|
};
|
|
2948
2685
|
} else {
|
|
2949
2686
|
var _extractPropertyKeyLi;
|
|
2950
|
-
|
|
2951
2687
|
if (members.length > 0) {
|
|
2952
2688
|
return unsupportedAnnotation(node, 'object types with spreads, indexers and/or call properties at the same time');
|
|
2953
2689
|
}
|
|
2954
|
-
|
|
2955
2690
|
const typesToIntersect = [];
|
|
2956
|
-
|
|
2957
2691
|
for (const property of node.properties) {
|
|
2958
2692
|
if (property.type === 'ObjectTypeSpreadProperty') {
|
|
2959
2693
|
if (members.length > 0) {
|
|
2960
2694
|
return unsupportedAnnotation(property, 'object types with spreads in the middle or at the end');
|
|
2961
2695
|
}
|
|
2962
|
-
|
|
2963
2696
|
const spreadType = transformTypeAnnotationType(property.argument);
|
|
2964
|
-
|
|
2965
2697
|
if (spreadType.type !== 'TSTypeReference' && spreadType.type !== 'TSTypeQuery') {
|
|
2966
2698
|
return unsupportedAnnotation(property, 'object types with complex spreads');
|
|
2967
2699
|
}
|
|
2968
|
-
|
|
2969
2700
|
typesToIntersect.push(spreadType);
|
|
2970
2701
|
} else if (property.type === 'ObjectTypeMappedTypeProperty') {
|
|
2971
2702
|
return unsupportedAnnotation(property, 'object type with mapped type property');
|
|
@@ -2976,7 +2707,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2976
2707
|
});
|
|
2977
2708
|
}
|
|
2978
2709
|
}
|
|
2979
|
-
|
|
2980
2710
|
const tsBody = members.sort((a, b) => a.start - b.start).map(({
|
|
2981
2711
|
node
|
|
2982
2712
|
}) => node);
|
|
@@ -2992,7 +2722,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
2992
2722
|
typeAnnotation: objectType
|
|
2993
2723
|
}];
|
|
2994
2724
|
const intersectionMembers = [];
|
|
2995
|
-
|
|
2996
2725
|
for (let i = 0; i < typesToIntersect.length; i += 1) {
|
|
2997
2726
|
const currentType = typesToIntersect[i];
|
|
2998
2727
|
const remainingTypes = typesToIntersect.slice(i + 1);
|
|
@@ -3021,7 +2750,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3021
2750
|
}
|
|
3022
2751
|
});
|
|
3023
2752
|
}
|
|
3024
|
-
|
|
3025
2753
|
intersectionMembers.push(objectType);
|
|
3026
2754
|
return {
|
|
3027
2755
|
type: 'TSIntersectionType',
|
|
@@ -3030,7 +2758,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3030
2758
|
};
|
|
3031
2759
|
}
|
|
3032
2760
|
},
|
|
3033
|
-
|
|
3034
2761
|
ObjectTypeCallProperty(node) {
|
|
3035
2762
|
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3036
2763
|
return {
|
|
@@ -3041,13 +2768,10 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3041
2768
|
typeParameters: func.typeParameters
|
|
3042
2769
|
};
|
|
3043
2770
|
},
|
|
3044
|
-
|
|
3045
2771
|
ObjectTypeIndexer(node) {
|
|
3046
2772
|
var _node$variance3, _node$variance4;
|
|
3047
|
-
|
|
3048
2773
|
if (node.key.type === 'GenericTypeAnnotation') {
|
|
3049
2774
|
var _node$variance, _node$variance2;
|
|
3050
|
-
|
|
3051
2775
|
const ident = node.key.id.type === 'Identifier' ? node.key.id : node.key.id.id;
|
|
3052
2776
|
return {
|
|
3053
2777
|
type: 'TSPropertySignature',
|
|
@@ -3077,7 +2801,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3077
2801
|
}
|
|
3078
2802
|
};
|
|
3079
2803
|
}
|
|
3080
|
-
|
|
3081
2804
|
return {
|
|
3082
2805
|
type: 'TSIndexSignature',
|
|
3083
2806
|
loc: DUMMY_LOC,
|
|
@@ -3100,14 +2823,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3100
2823
|
}
|
|
3101
2824
|
};
|
|
3102
2825
|
},
|
|
3103
|
-
|
|
3104
2826
|
ObjectTypeProperty(node) {
|
|
3105
2827
|
var _node$variance5, _node$variance6;
|
|
3106
|
-
|
|
3107
2828
|
const [key, computed] = (() => {
|
|
3108
2829
|
if (node.key.type === 'Identifier' && node.key.name.startsWith('@@')) {
|
|
3109
2830
|
const name = node.key.name.slice(2);
|
|
3110
|
-
|
|
3111
2831
|
if (node.method === true && wellKnownSymbols.has(name)) {
|
|
3112
2832
|
return [{
|
|
3113
2833
|
type: 'MemberExpression',
|
|
@@ -3129,16 +2849,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3129
2849
|
}, true];
|
|
3130
2850
|
}
|
|
3131
2851
|
}
|
|
3132
|
-
|
|
3133
2852
|
const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : node.key.literalType === 'string' ? transform.StringLiteral(node.key) : null;
|
|
3134
|
-
|
|
3135
2853
|
if (key == null) {
|
|
3136
2854
|
throw unexpectedTranslationError(node, 'Unsupported key type');
|
|
3137
2855
|
}
|
|
3138
|
-
|
|
3139
2856
|
return [key, false];
|
|
3140
2857
|
})();
|
|
3141
|
-
|
|
3142
2858
|
if (node.method === true) {
|
|
3143
2859
|
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3144
2860
|
return {
|
|
@@ -3154,7 +2870,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3154
2870
|
typeParameters: func.typeParameters
|
|
3155
2871
|
};
|
|
3156
2872
|
}
|
|
3157
|
-
|
|
3158
2873
|
if (node.kind === 'get' || node.kind === 'set') {
|
|
3159
2874
|
const func = transform.FunctionTypeAnnotation(node.value);
|
|
3160
2875
|
return {
|
|
@@ -3170,7 +2885,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3170
2885
|
typeParameters: undefined
|
|
3171
2886
|
};
|
|
3172
2887
|
}
|
|
3173
|
-
|
|
3174
2888
|
return {
|
|
3175
2889
|
type: 'TSPropertySignature',
|
|
3176
2890
|
loc: DUMMY_LOC,
|
|
@@ -3182,20 +2896,17 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3182
2896
|
typeAnnotation: {
|
|
3183
2897
|
type: 'TSTypeAnnotation',
|
|
3184
2898
|
loc: DUMMY_LOC,
|
|
3185
|
-
typeAnnotation: transformTypeAnnotationType(node.value)
|
|
2899
|
+
typeAnnotation: node.optional === true ? (0, _TSNodeUtils.ensureTypeIncludesUndefined)(transformTypeAnnotationType(node.value)) : transformTypeAnnotationType(node.value)
|
|
3186
2900
|
}
|
|
3187
2901
|
};
|
|
3188
2902
|
},
|
|
3189
|
-
|
|
3190
2903
|
OpaqueType(node) {
|
|
3191
2904
|
return transform.DeclareOpaqueType(node);
|
|
3192
2905
|
},
|
|
3193
|
-
|
|
3194
2906
|
OptionalIndexedAccessType(node) {
|
|
3195
2907
|
if (node.optional === false) {
|
|
3196
2908
|
return transform.IndexedAccessType(node);
|
|
3197
2909
|
}
|
|
3198
|
-
|
|
3199
2910
|
return {
|
|
3200
2911
|
type: 'TSIndexedAccessType',
|
|
3201
2912
|
loc: DUMMY_LOC,
|
|
@@ -3216,7 +2927,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3216
2927
|
indexType: transformTypeAnnotationType(node.indexType)
|
|
3217
2928
|
};
|
|
3218
2929
|
},
|
|
3219
|
-
|
|
3220
2930
|
QualifiedTypeIdentifier(node) {
|
|
3221
2931
|
const qual = node.qualification;
|
|
3222
2932
|
return {
|
|
@@ -3226,7 +2936,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3226
2936
|
right: transform.Identifier(node.id, false)
|
|
3227
2937
|
};
|
|
3228
2938
|
},
|
|
3229
|
-
|
|
3230
2939
|
QualifiedTypeofIdentifier(node) {
|
|
3231
2940
|
const qual = node.qualification;
|
|
3232
2941
|
return {
|
|
@@ -3236,7 +2945,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3236
2945
|
right: transform.Identifier(node.id, false)
|
|
3237
2946
|
};
|
|
3238
2947
|
},
|
|
3239
|
-
|
|
3240
2948
|
RegExpLiteral(node) {
|
|
3241
2949
|
return {
|
|
3242
2950
|
type: 'Literal',
|
|
@@ -3249,7 +2957,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3249
2957
|
value: node.value
|
|
3250
2958
|
};
|
|
3251
2959
|
},
|
|
3252
|
-
|
|
3253
2960
|
StringLiteral(node) {
|
|
3254
2961
|
return {
|
|
3255
2962
|
type: 'Literal',
|
|
@@ -3258,7 +2965,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3258
2965
|
value: node.value
|
|
3259
2966
|
};
|
|
3260
2967
|
},
|
|
3261
|
-
|
|
3262
2968
|
StringLiteralTypeAnnotation(node) {
|
|
3263
2969
|
return {
|
|
3264
2970
|
type: 'TSLiteralType',
|
|
@@ -3271,28 +2977,24 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3271
2977
|
}
|
|
3272
2978
|
};
|
|
3273
2979
|
},
|
|
3274
|
-
|
|
3275
2980
|
StringTypeAnnotation(_node) {
|
|
3276
2981
|
return {
|
|
3277
2982
|
type: 'TSStringKeyword',
|
|
3278
2983
|
loc: DUMMY_LOC
|
|
3279
2984
|
};
|
|
3280
2985
|
},
|
|
3281
|
-
|
|
3282
2986
|
SymbolTypeAnnotation(_node) {
|
|
3283
2987
|
return {
|
|
3284
2988
|
type: 'TSSymbolKeyword',
|
|
3285
2989
|
loc: DUMMY_LOC
|
|
3286
2990
|
};
|
|
3287
2991
|
},
|
|
3288
|
-
|
|
3289
2992
|
ThisTypeAnnotation(_node) {
|
|
3290
2993
|
return {
|
|
3291
2994
|
type: 'TSThisType',
|
|
3292
2995
|
loc: DUMMY_LOC
|
|
3293
2996
|
};
|
|
3294
2997
|
},
|
|
3295
|
-
|
|
3296
2998
|
TupleTypeAnnotation(node) {
|
|
3297
2999
|
const allReadOnly = node.elementTypes.length > 0 && node.elementTypes.every(element => element.type === 'TupleTypeLabeledElement' && element.variance != null && (element.variance.kind === 'plus' || element.variance.kind === 'readonly'));
|
|
3298
3000
|
const elems = node.elementTypes.map(element => {
|
|
@@ -3301,7 +3003,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3301
3003
|
if (!allReadOnly && element.variance != null) {
|
|
3302
3004
|
return unsupportedAnnotation(element, 'tuple type element variance annotations');
|
|
3303
3005
|
}
|
|
3304
|
-
|
|
3305
3006
|
return {
|
|
3306
3007
|
type: 'TSNamedTupleMember',
|
|
3307
3008
|
loc: DUMMY_LOC,
|
|
@@ -3309,7 +3010,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3309
3010
|
optional: element.optional,
|
|
3310
3011
|
elementType: transformTypeAnnotationType(element.elementType)
|
|
3311
3012
|
};
|
|
3312
|
-
|
|
3313
3013
|
case 'TupleTypeSpreadElement':
|
|
3314
3014
|
{
|
|
3315
3015
|
const annot = transformTypeAnnotationType(element.typeAnnotation);
|
|
@@ -3325,7 +3025,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3325
3025
|
} : annot
|
|
3326
3026
|
};
|
|
3327
3027
|
}
|
|
3328
|
-
|
|
3329
3028
|
default:
|
|
3330
3029
|
return transformTypeAnnotationType(element);
|
|
3331
3030
|
}
|
|
@@ -3354,11 +3053,9 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3354
3053
|
typeAnnotation: tupleAnnot
|
|
3355
3054
|
} : tupleAnnot;
|
|
3356
3055
|
},
|
|
3357
|
-
|
|
3358
3056
|
TypeAlias(node) {
|
|
3359
3057
|
return transform.DeclareTypeAlias(node);
|
|
3360
3058
|
},
|
|
3361
|
-
|
|
3362
3059
|
TypeAnnotation(node) {
|
|
3363
3060
|
return {
|
|
3364
3061
|
type: 'TSTypeAnnotation',
|
|
@@ -3366,7 +3063,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3366
3063
|
typeAnnotation: transformTypeAnnotationType(node.typeAnnotation)
|
|
3367
3064
|
};
|
|
3368
3065
|
},
|
|
3369
|
-
|
|
3370
3066
|
TypeofTypeAnnotation(node) {
|
|
3371
3067
|
switch (node.argument.type) {
|
|
3372
3068
|
case 'Identifier':
|
|
@@ -3376,7 +3072,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3376
3072
|
exprName: transform.Identifier(node.argument),
|
|
3377
3073
|
typeArguments: undefined
|
|
3378
3074
|
};
|
|
3379
|
-
|
|
3380
3075
|
case 'QualifiedTypeofIdentifier':
|
|
3381
3076
|
return {
|
|
3382
3077
|
type: 'TSTypeQuery',
|
|
@@ -3386,7 +3081,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3386
3081
|
};
|
|
3387
3082
|
}
|
|
3388
3083
|
},
|
|
3389
|
-
|
|
3390
3084
|
TypeParameter(node) {
|
|
3391
3085
|
return {
|
|
3392
3086
|
type: 'TSTypeParameter',
|
|
@@ -3402,7 +3096,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3402
3096
|
out: false
|
|
3403
3097
|
};
|
|
3404
3098
|
},
|
|
3405
|
-
|
|
3406
3099
|
TypeParameterDeclaration(node) {
|
|
3407
3100
|
return {
|
|
3408
3101
|
type: 'TSTypeParameterDeclaration',
|
|
@@ -3410,19 +3103,16 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3410
3103
|
params: node.params.map(transform.TypeParameter)
|
|
3411
3104
|
};
|
|
3412
3105
|
},
|
|
3413
|
-
|
|
3414
3106
|
TypeParameterInstantiation(node) {
|
|
3415
3107
|
if (node == null || node.params.length === 0) {
|
|
3416
3108
|
return undefined;
|
|
3417
3109
|
}
|
|
3418
|
-
|
|
3419
3110
|
return {
|
|
3420
3111
|
type: 'TSTypeParameterInstantiation',
|
|
3421
3112
|
loc: DUMMY_LOC,
|
|
3422
3113
|
params: node.params.map(transformTypeAnnotationType)
|
|
3423
3114
|
};
|
|
3424
3115
|
},
|
|
3425
|
-
|
|
3426
3116
|
UnionTypeAnnotation(node) {
|
|
3427
3117
|
return {
|
|
3428
3118
|
type: 'TSUnionType',
|
|
@@ -3430,35 +3120,30 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3430
3120
|
types: node.types.map(transformTypeAnnotationType)
|
|
3431
3121
|
};
|
|
3432
3122
|
},
|
|
3433
|
-
|
|
3434
3123
|
VoidTypeAnnotation(_node) {
|
|
3435
3124
|
return {
|
|
3436
3125
|
type: 'TSVoidKeyword',
|
|
3437
3126
|
loc: DUMMY_LOC
|
|
3438
3127
|
};
|
|
3439
3128
|
},
|
|
3440
|
-
|
|
3441
3129
|
NeverTypeAnnotation(_node) {
|
|
3442
3130
|
return {
|
|
3443
3131
|
type: 'TSNeverKeyword',
|
|
3444
3132
|
loc: DUMMY_LOC
|
|
3445
3133
|
};
|
|
3446
3134
|
},
|
|
3447
|
-
|
|
3448
3135
|
UndefinedTypeAnnotation(_node) {
|
|
3449
3136
|
return {
|
|
3450
3137
|
type: 'TSUndefinedKeyword',
|
|
3451
3138
|
loc: DUMMY_LOC
|
|
3452
3139
|
};
|
|
3453
3140
|
},
|
|
3454
|
-
|
|
3455
3141
|
UnknownTypeAnnotation(_node) {
|
|
3456
3142
|
return {
|
|
3457
3143
|
type: 'TSUnknownKeyword',
|
|
3458
3144
|
loc: DUMMY_LOC
|
|
3459
3145
|
};
|
|
3460
3146
|
},
|
|
3461
|
-
|
|
3462
3147
|
ConditionalTypeAnnotation(node) {
|
|
3463
3148
|
return {
|
|
3464
3149
|
type: 'TSConditionalType',
|
|
@@ -3469,7 +3154,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3469
3154
|
falseType: transformTypeAnnotationType(node.falseType)
|
|
3470
3155
|
};
|
|
3471
3156
|
},
|
|
3472
|
-
|
|
3473
3157
|
TypePredicateAnnotation(node) {
|
|
3474
3158
|
return {
|
|
3475
3159
|
type: 'TSTypePredicate',
|
|
@@ -3483,7 +3167,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3483
3167
|
}
|
|
3484
3168
|
};
|
|
3485
3169
|
},
|
|
3486
|
-
|
|
3487
3170
|
InferTypeAnnotation(node) {
|
|
3488
3171
|
return {
|
|
3489
3172
|
type: 'TSInferType',
|
|
@@ -3491,7 +3174,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3491
3174
|
typeParameter: transform.TypeParameter(node.typeParameter)
|
|
3492
3175
|
};
|
|
3493
3176
|
},
|
|
3494
|
-
|
|
3495
3177
|
KeyofTypeAnnotation(node) {
|
|
3496
3178
|
return {
|
|
3497
3179
|
type: 'TSTypeOperator',
|
|
@@ -3500,7 +3182,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3500
3182
|
typeAnnotation: transformTypeAnnotationType(node.argument)
|
|
3501
3183
|
};
|
|
3502
3184
|
},
|
|
3503
|
-
|
|
3504
3185
|
TypeOperator(node) {
|
|
3505
3186
|
switch (node.operator) {
|
|
3506
3187
|
case 'renders':
|
|
@@ -3526,7 +3207,6 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3526
3207
|
}
|
|
3527
3208
|
}
|
|
3528
3209
|
},
|
|
3529
|
-
|
|
3530
3210
|
ComponentTypeAnnotation(node) {
|
|
3531
3211
|
const typeParameters = node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters);
|
|
3532
3212
|
const params = transform.ComponentTypeParameters(node.params, node.rest);
|
|
@@ -3558,15 +3238,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3558
3238
|
returnType
|
|
3559
3239
|
};
|
|
3560
3240
|
}
|
|
3561
|
-
|
|
3562
3241
|
};
|
|
3563
|
-
|
|
3564
3242
|
for (const key of Object.keys(transform)) {
|
|
3565
3243
|
const originalFn = transform[key];
|
|
3566
|
-
|
|
3567
3244
|
transform[key] = (node, ...args) => {
|
|
3568
3245
|
const result = originalFn(node, ...args);
|
|
3569
|
-
|
|
3570
3246
|
if (node != null && result != null) {
|
|
3571
3247
|
if (Array.isArray(result)) {
|
|
3572
3248
|
cloneJSDocCommentsToNewNode(node, result[0]);
|
|
@@ -3574,10 +3250,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
|
|
|
3574
3250
|
cloneJSDocCommentsToNewNode(node, result);
|
|
3575
3251
|
}
|
|
3576
3252
|
}
|
|
3577
|
-
|
|
3578
3253
|
return result;
|
|
3579
3254
|
};
|
|
3580
3255
|
}
|
|
3581
|
-
|
|
3582
3256
|
return [transform, code];
|
|
3583
3257
|
};
|