flow-parser 0.318.0 → 0.319.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/oxidized/FlowParser.js +249 -0
- package/oxidized/FlowParser.js.flow +457 -0
- package/oxidized/FlowParserDeserializer.js +209 -0
- package/oxidized/FlowParserDeserializer.js.flow +323 -0
- package/oxidized/FlowParserNodeDeserializers.js +1767 -0
- package/oxidized/FlowParserNodeDeserializers.js.flow +18 -0
- package/oxidized/FlowParserWASM.js +1 -0
- package/oxidized/FlowParserWASM.js.flow +119 -0
- package/oxidized/HermesAST.js.flow +57 -0
- package/oxidized/HermesParserDecodeUTF8String.js +45 -0
- package/oxidized/HermesParserDecodeUTF8String.js.flow +65 -0
- package/oxidized/ParserOptions.js +8 -0
- package/oxidized/ParserOptions.js.flow +55 -0
- package/oxidized/babel/TransformESTreeToBabel.js +1055 -0
- package/oxidized/babel/TransformESTreeToBabel.js.flow +1349 -0
- package/oxidized/estree/StripFlowTypes.js +158 -0
- package/oxidized/estree/StripFlowTypes.js.flow +158 -0
- package/oxidized/estree/StripFlowTypesForBabel.js +176 -0
- package/oxidized/estree/StripFlowTypesForBabel.js.flow +216 -0
- package/oxidized/estree/TransformComponentSyntax.js +751 -0
- package/oxidized/estree/TransformComponentSyntax.js.flow +864 -0
- package/oxidized/estree/TransformEnumSyntax.js +92 -0
- package/oxidized/estree/TransformEnumSyntax.js.flow +125 -0
- package/oxidized/estree/TransformMatchSyntax.js +990 -0
- package/oxidized/estree/TransformMatchSyntax.js.flow +991 -0
- package/oxidized/estree/TransformRecordSyntax.js +283 -0
- package/oxidized/estree/TransformRecordSyntax.js.flow +310 -0
- package/oxidized/generated/ESTreeVisitorKeys.js +234 -0
- package/oxidized/generated/ESTreeVisitorKeys.js.flow +22 -0
- package/oxidized/getModuleDocblock.js +87 -0
- package/oxidized/getModuleDocblock.js.flow +119 -0
- package/oxidized/index.js +220 -0
- package/oxidized/index.js.flow +183 -0
- package/oxidized/oxidized-src/FlowParser.js +249 -0
- package/oxidized/oxidized-src/FlowParserDeserializer.js +209 -0
- package/oxidized/oxidized-src/FlowParserNodeDeserializers.js +1767 -0
- package/oxidized/oxidized-src/HermesParserDecodeUTF8String.js +45 -0
- package/oxidized/oxidized-src/ParserOptions.js +8 -0
- package/oxidized/oxidized-src/babel/TransformESTreeToBabel.js +1055 -0
- package/oxidized/oxidized-src/estree/StripFlowTypes.js +158 -0
- package/oxidized/oxidized-src/estree/StripFlowTypesForBabel.js +176 -0
- package/oxidized/oxidized-src/estree/TransformComponentSyntax.js +751 -0
- package/oxidized/oxidized-src/estree/TransformEnumSyntax.js +92 -0
- package/oxidized/oxidized-src/estree/TransformMatchSyntax.js +990 -0
- package/oxidized/oxidized-src/estree/TransformRecordSyntax.js +283 -0
- package/oxidized/oxidized-src/generated/ESTreeVisitorKeys.js +234 -0
- package/oxidized/oxidized-src/getModuleDocblock.js +87 -0
- package/oxidized/oxidized-src/index.js +220 -0
- package/oxidized/oxidized-src/transform/SimpleTransform.js +95 -0
- package/oxidized/oxidized-src/transform/astArrayMutationHelpers.js +52 -0
- package/oxidized/oxidized-src/transform/astNodeMutationHelpers.js +142 -0
- package/oxidized/oxidized-src/transform/print/comments/comments.js +245 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/common/util.js +235 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/comments.js +663 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/loc.js +30 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/printer-estree.js +23 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/utils.js +89 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/main/comments.js +374 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/utils/get-last.js +5 -0
- package/oxidized/oxidized-src/transform/print/detachedNodeTypes.js +1 -0
- package/oxidized/oxidized-src/transform/print/print.js +68 -0
- package/oxidized/oxidized-src/traverse/SimpleTraverser.js +90 -0
- package/oxidized/oxidized-src/traverse/getVisitorKeys.js +29 -0
- package/oxidized/oxidized-src/utils/Builders.js +181 -0
- package/oxidized/oxidized-src/utils/GenID.js +37 -0
- package/oxidized/oxidized-src/utils/createSyntaxError.js +15 -0
- package/oxidized/oxidized-src/utils/isReservedWord.js +53 -0
- package/oxidized/oxidized-src/utils/mutateESTreeASTForPrettier.js +85 -0
- package/oxidized/transform/SimpleTransform.js +95 -0
- package/oxidized/transform/SimpleTransform.js.flow +168 -0
- package/oxidized/transform/astArrayMutationHelpers.js +52 -0
- package/oxidized/transform/astArrayMutationHelpers.js.flow +71 -0
- package/oxidized/transform/astNodeMutationHelpers.js +142 -0
- package/oxidized/transform/astNodeMutationHelpers.js.flow +252 -0
- package/oxidized/transform/print/comments/comments.js +245 -0
- package/oxidized/transform/print/comments/comments.js.flow +330 -0
- package/oxidized/transform/print/comments/prettier/common/util.js +235 -0
- package/oxidized/transform/print/comments/prettier/language-js/comments.js +663 -0
- package/oxidized/transform/print/comments/prettier/language-js/loc.js +30 -0
- package/oxidized/transform/print/comments/prettier/language-js/printer-estree.js +23 -0
- package/oxidized/transform/print/comments/prettier/language-js/utils.js +89 -0
- package/oxidized/transform/print/comments/prettier/main/comments.js +374 -0
- package/oxidized/transform/print/comments/prettier/utils/get-last.js +5 -0
- package/oxidized/transform/print/detachedNodeTypes.js +1 -0
- package/oxidized/transform/print/detachedNodeTypes.js.flow +24 -0
- package/oxidized/transform/print/print.js +68 -0
- package/oxidized/transform/print/print.js.flow +87 -0
- package/oxidized/traverse/SimpleTraverser.js +90 -0
- package/oxidized/traverse/SimpleTraverser.js.flow +133 -0
- package/oxidized/traverse/getVisitorKeys.js +29 -0
- package/oxidized/traverse/getVisitorKeys.js.flow +36 -0
- package/oxidized/utils/Builders.js +181 -0
- package/oxidized/utils/Builders.js.flow +218 -0
- package/oxidized/utils/GenID.js +37 -0
- package/oxidized/utils/GenID.js.flow +39 -0
- package/oxidized/utils/createSyntaxError.js +15 -0
- package/oxidized/utils/createSyntaxError.js.flow +24 -0
- package/oxidized/utils/isReservedWord.js +53 -0
- package/oxidized/utils/isReservedWord.js.flow +57 -0
- package/oxidized/utils/mutateESTreeASTForPrettier.js +85 -0
- package/oxidized/utils/mutateESTreeASTForPrettier.js.flow +130 -0
- package/package.json +6 -4
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transformProgram = transformProgram;
|
|
7
|
+
|
|
8
|
+
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
9
|
+
|
|
10
|
+
var _astNodeMutationHelpers = require("../transform/astNodeMutationHelpers");
|
|
11
|
+
|
|
12
|
+
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
13
|
+
|
|
14
|
+
var _createSyntaxError = require("../utils/createSyntaxError");
|
|
15
|
+
|
|
16
|
+
const nodeWith = _SimpleTransform.SimpleTransform.nodeWith;
|
|
17
|
+
const EMPTY_PARENT = null;
|
|
18
|
+
|
|
19
|
+
function createDefaultPosition() {
|
|
20
|
+
return {
|
|
21
|
+
line: 1,
|
|
22
|
+
column: 0
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function mapDeclareComponent(node) {
|
|
27
|
+
return {
|
|
28
|
+
type: 'DeclareVariable',
|
|
29
|
+
id: nodeWith(node.id, {
|
|
30
|
+
typeAnnotation: {
|
|
31
|
+
type: 'TypeAnnotation',
|
|
32
|
+
typeAnnotation: {
|
|
33
|
+
type: 'AnyTypeAnnotation',
|
|
34
|
+
loc: node.loc,
|
|
35
|
+
range: node.range,
|
|
36
|
+
parent: EMPTY_PARENT
|
|
37
|
+
},
|
|
38
|
+
loc: node.loc,
|
|
39
|
+
range: node.range,
|
|
40
|
+
parent: EMPTY_PARENT
|
|
41
|
+
}
|
|
42
|
+
}),
|
|
43
|
+
kind: 'const',
|
|
44
|
+
loc: node.loc,
|
|
45
|
+
range: node.range,
|
|
46
|
+
parent: node.parent
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getComponentParameterName(paramName) {
|
|
51
|
+
switch (paramName.type) {
|
|
52
|
+
case 'Identifier':
|
|
53
|
+
return paramName.name;
|
|
54
|
+
|
|
55
|
+
case 'Literal':
|
|
56
|
+
return paramName.value;
|
|
57
|
+
|
|
58
|
+
default:
|
|
59
|
+
throw (0, _createSyntaxError.createSyntaxError)(paramName, `Unknown Component parameter name type of "${paramName.type}"`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function createPropsTypeAnnotation(propTypes, spread, loc, range) {
|
|
64
|
+
const createParamsTypeLoc = () => ({
|
|
65
|
+
loc: {
|
|
66
|
+
start: (loc == null ? void 0 : loc.start) != null ? loc.start : createDefaultPosition(),
|
|
67
|
+
end: (loc == null ? void 0 : loc.end) != null ? loc.end : createDefaultPosition()
|
|
68
|
+
},
|
|
69
|
+
range: range != null ? range : [0, 0],
|
|
70
|
+
parent: EMPTY_PARENT
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (spread != null && propTypes.length === 0) {
|
|
74
|
+
return {
|
|
75
|
+
type: 'TypeAnnotation',
|
|
76
|
+
typeAnnotation: spread.argument,
|
|
77
|
+
...createParamsTypeLoc()
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const typeProperties = [...propTypes];
|
|
82
|
+
|
|
83
|
+
if (spread != null) {
|
|
84
|
+
typeProperties.unshift(spread);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const propTypeObj = {
|
|
88
|
+
type: 'ObjectTypeAnnotation',
|
|
89
|
+
callProperties: [],
|
|
90
|
+
properties: typeProperties,
|
|
91
|
+
indexers: [],
|
|
92
|
+
internalSlots: [],
|
|
93
|
+
exact: false,
|
|
94
|
+
inexact: false,
|
|
95
|
+
...createParamsTypeLoc()
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
type: 'TypeAnnotation',
|
|
99
|
+
typeAnnotation: {
|
|
100
|
+
type: 'GenericTypeAnnotation',
|
|
101
|
+
id: {
|
|
102
|
+
type: 'Identifier',
|
|
103
|
+
name: '$ReadOnly',
|
|
104
|
+
optional: false,
|
|
105
|
+
typeAnnotation: null,
|
|
106
|
+
...createParamsTypeLoc()
|
|
107
|
+
},
|
|
108
|
+
typeParameters: {
|
|
109
|
+
type: 'TypeParameterInstantiation',
|
|
110
|
+
params: [propTypeObj],
|
|
111
|
+
...createParamsTypeLoc()
|
|
112
|
+
},
|
|
113
|
+
...createParamsTypeLoc()
|
|
114
|
+
},
|
|
115
|
+
...createParamsTypeLoc()
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function mapComponentParameters(params, options) {
|
|
120
|
+
var _options$reactRuntime;
|
|
121
|
+
|
|
122
|
+
if (params.length === 0) {
|
|
123
|
+
return {
|
|
124
|
+
props: null,
|
|
125
|
+
ref: null
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (params.length === 1 && params[0].type === 'RestElement' && params[0].argument.type === 'Identifier') {
|
|
130
|
+
const restElementArgument = params[0].argument;
|
|
131
|
+
return {
|
|
132
|
+
props: restElementArgument,
|
|
133
|
+
ref: null
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let refParam = null;
|
|
138
|
+
const paramsWithoutRef = ((_options$reactRuntime = options.reactRuntimeTarget) != null ? _options$reactRuntime : '18') === '18' ? params.filter(param => {
|
|
139
|
+
if (param.type === 'ComponentParameter' && getComponentParameterName(param.name) === 'ref') {
|
|
140
|
+
refParam = param;
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return true;
|
|
145
|
+
}) : params;
|
|
146
|
+
const [propTypes, spread] = paramsWithoutRef.reduce(([propTypes, spread], param) => {
|
|
147
|
+
switch (param.type) {
|
|
148
|
+
case 'RestElement':
|
|
149
|
+
{
|
|
150
|
+
if (spread != null) {
|
|
151
|
+
throw (0, _createSyntaxError.createSyntaxError)(param, `Invalid state, multiple rest elements found as Component Parameters`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return [propTypes, mapComponentParameterRestElementType(param)];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
case 'ComponentParameter':
|
|
158
|
+
{
|
|
159
|
+
propTypes.push(mapComponentParameterType(param));
|
|
160
|
+
return [propTypes, spread];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}, [[], null]);
|
|
164
|
+
const propsProperties = paramsWithoutRef.flatMap(mapComponentParameter);
|
|
165
|
+
let props = null;
|
|
166
|
+
|
|
167
|
+
if (propsProperties.length === 0) {
|
|
168
|
+
if (refParam == null) {
|
|
169
|
+
throw new Error('TransformComponentSyntax: Invalid state, ref should always be set at this point if props are empty');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const emptyParamsLoc = {
|
|
173
|
+
start: refParam.loc.start,
|
|
174
|
+
end: refParam.loc.start
|
|
175
|
+
};
|
|
176
|
+
const emptyParamsRange = [refParam.range[0], refParam.range[0]];
|
|
177
|
+
props = {
|
|
178
|
+
type: 'Identifier',
|
|
179
|
+
name: '_$$empty_props_placeholder$$',
|
|
180
|
+
optional: false,
|
|
181
|
+
typeAnnotation: createPropsTypeAnnotation([], null, emptyParamsLoc, emptyParamsRange),
|
|
182
|
+
loc: emptyParamsLoc,
|
|
183
|
+
range: emptyParamsRange,
|
|
184
|
+
parent: EMPTY_PARENT
|
|
185
|
+
};
|
|
186
|
+
} else {
|
|
187
|
+
const lastPropsProperty = propsProperties[propsProperties.length - 1];
|
|
188
|
+
props = {
|
|
189
|
+
type: 'ObjectPattern',
|
|
190
|
+
properties: propsProperties,
|
|
191
|
+
typeAnnotation: createPropsTypeAnnotation(propTypes, spread, {
|
|
192
|
+
start: lastPropsProperty.loc.end,
|
|
193
|
+
end: lastPropsProperty.loc.end
|
|
194
|
+
}, [lastPropsProperty.range[1], lastPropsProperty.range[1]]),
|
|
195
|
+
loc: {
|
|
196
|
+
start: propsProperties[0].loc.start,
|
|
197
|
+
end: lastPropsProperty.loc.end
|
|
198
|
+
},
|
|
199
|
+
range: [propsProperties[0].range[0], lastPropsProperty.range[1]],
|
|
200
|
+
parent: EMPTY_PARENT
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let ref = null;
|
|
205
|
+
|
|
206
|
+
if (refParam != null) {
|
|
207
|
+
ref = refParam.local;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
props,
|
|
212
|
+
ref
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function mapComponentParameterType(param) {
|
|
217
|
+
var _typeAnnotation$typeA;
|
|
218
|
+
|
|
219
|
+
const typeAnnotation = param.local.type === 'AssignmentPattern' ? param.local.left.typeAnnotation : param.local.typeAnnotation;
|
|
220
|
+
const optional = param.local.type === 'AssignmentPattern' ? true : param.local.type === 'Identifier' ? param.local.optional : false;
|
|
221
|
+
return {
|
|
222
|
+
type: 'ObjectTypeProperty',
|
|
223
|
+
key: (0, _astNodeMutationHelpers.shallowCloneNode)(param.name),
|
|
224
|
+
value: (_typeAnnotation$typeA = typeAnnotation == null ? void 0 : typeAnnotation.typeAnnotation) != null ? _typeAnnotation$typeA : {
|
|
225
|
+
type: 'AnyTypeAnnotation',
|
|
226
|
+
loc: param.local.loc,
|
|
227
|
+
range: param.local.range,
|
|
228
|
+
parent: EMPTY_PARENT
|
|
229
|
+
},
|
|
230
|
+
kind: 'init',
|
|
231
|
+
optional,
|
|
232
|
+
method: false,
|
|
233
|
+
static: false,
|
|
234
|
+
proto: false,
|
|
235
|
+
variance: null,
|
|
236
|
+
loc: param.local.loc,
|
|
237
|
+
range: param.local.range,
|
|
238
|
+
parent: EMPTY_PARENT
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function mapComponentParameterRestElementType(param) {
|
|
243
|
+
var _param$argument$typeA, _param$argument$typeA2;
|
|
244
|
+
|
|
245
|
+
if (param.argument.type !== 'Identifier' && param.argument.type !== 'ObjectPattern') {
|
|
246
|
+
throw (0, _createSyntaxError.createSyntaxError)(param, `Invalid ${param.argument.type} encountered in restParameter`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
type: 'ObjectTypeSpreadProperty',
|
|
251
|
+
argument: (_param$argument$typeA = (_param$argument$typeA2 = param.argument.typeAnnotation) == null ? void 0 : _param$argument$typeA2.typeAnnotation) != null ? _param$argument$typeA : {
|
|
252
|
+
type: 'AnyTypeAnnotation',
|
|
253
|
+
loc: param.loc,
|
|
254
|
+
range: param.range,
|
|
255
|
+
parent: EMPTY_PARENT
|
|
256
|
+
},
|
|
257
|
+
loc: param.loc,
|
|
258
|
+
range: param.range,
|
|
259
|
+
parent: EMPTY_PARENT
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function mapComponentParameter(param) {
|
|
264
|
+
switch (param.type) {
|
|
265
|
+
case 'RestElement':
|
|
266
|
+
{
|
|
267
|
+
switch (param.argument.type) {
|
|
268
|
+
case 'Identifier':
|
|
269
|
+
{
|
|
270
|
+
const a = nodeWith(param, {
|
|
271
|
+
typeAnnotation: null,
|
|
272
|
+
argument: nodeWith(param.argument, {
|
|
273
|
+
typeAnnotation: null
|
|
274
|
+
})
|
|
275
|
+
});
|
|
276
|
+
return [a];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
case 'ObjectPattern':
|
|
280
|
+
{
|
|
281
|
+
return param.argument.properties.map(property => {
|
|
282
|
+
return nodeWith(property, {
|
|
283
|
+
typeAnnotation: null
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
default:
|
|
289
|
+
{
|
|
290
|
+
throw (0, _createSyntaxError.createSyntaxError)(param, `Unhandled ${param.argument.type} encountered in restParameter`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
case 'ComponentParameter':
|
|
296
|
+
{
|
|
297
|
+
let value;
|
|
298
|
+
|
|
299
|
+
if (param.local.type === 'AssignmentPattern') {
|
|
300
|
+
value = nodeWith(param.local, {
|
|
301
|
+
left: nodeWith(param.local.left, {
|
|
302
|
+
typeAnnotation: null,
|
|
303
|
+
optional: false
|
|
304
|
+
})
|
|
305
|
+
});
|
|
306
|
+
} else {
|
|
307
|
+
value = nodeWith(param.local, {
|
|
308
|
+
typeAnnotation: null,
|
|
309
|
+
optional: false
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (param.name.type === 'Identifier' && param.shorthand && (value.type === 'Identifier' || value.type === 'AssignmentPattern')) {
|
|
314
|
+
return [{
|
|
315
|
+
type: 'Property',
|
|
316
|
+
key: param.name,
|
|
317
|
+
kind: 'init',
|
|
318
|
+
value,
|
|
319
|
+
method: false,
|
|
320
|
+
shorthand: true,
|
|
321
|
+
computed: false,
|
|
322
|
+
loc: param.loc,
|
|
323
|
+
range: param.range,
|
|
324
|
+
parent: EMPTY_PARENT
|
|
325
|
+
}];
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return [{
|
|
329
|
+
type: 'Property',
|
|
330
|
+
key: param.name,
|
|
331
|
+
kind: 'init',
|
|
332
|
+
value,
|
|
333
|
+
method: false,
|
|
334
|
+
shorthand: false,
|
|
335
|
+
computed: false,
|
|
336
|
+
loc: param.loc,
|
|
337
|
+
range: param.range,
|
|
338
|
+
parent: EMPTY_PARENT
|
|
339
|
+
}];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
default:
|
|
343
|
+
{
|
|
344
|
+
throw (0, _createSyntaxError.createSyntaxError)(param, `Unknown Component parameter type of "${param.type}"`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function createForwardRefWrapper(originalComponent) {
|
|
350
|
+
const internalCompId = {
|
|
351
|
+
type: 'Identifier',
|
|
352
|
+
name: `${originalComponent.id.name}_withRef`,
|
|
353
|
+
optional: false,
|
|
354
|
+
typeAnnotation: null,
|
|
355
|
+
loc: originalComponent.id.loc,
|
|
356
|
+
range: originalComponent.id.range,
|
|
357
|
+
parent: EMPTY_PARENT
|
|
358
|
+
};
|
|
359
|
+
return {
|
|
360
|
+
forwardRefStatement: {
|
|
361
|
+
type: 'VariableDeclaration',
|
|
362
|
+
kind: 'const',
|
|
363
|
+
declarations: [{
|
|
364
|
+
type: 'VariableDeclarator',
|
|
365
|
+
id: (0, _astNodeMutationHelpers.shallowCloneNode)(originalComponent.id),
|
|
366
|
+
init: {
|
|
367
|
+
type: 'CallExpression',
|
|
368
|
+
callee: {
|
|
369
|
+
type: 'MemberExpression',
|
|
370
|
+
object: {
|
|
371
|
+
type: 'Identifier',
|
|
372
|
+
name: 'React',
|
|
373
|
+
optional: false,
|
|
374
|
+
typeAnnotation: null,
|
|
375
|
+
loc: originalComponent.loc,
|
|
376
|
+
range: originalComponent.range,
|
|
377
|
+
parent: EMPTY_PARENT
|
|
378
|
+
},
|
|
379
|
+
property: {
|
|
380
|
+
type: 'Identifier',
|
|
381
|
+
name: 'forwardRef',
|
|
382
|
+
optional: false,
|
|
383
|
+
typeAnnotation: null,
|
|
384
|
+
loc: originalComponent.loc,
|
|
385
|
+
range: originalComponent.range,
|
|
386
|
+
parent: EMPTY_PARENT
|
|
387
|
+
},
|
|
388
|
+
computed: false,
|
|
389
|
+
optional: false,
|
|
390
|
+
loc: originalComponent.loc,
|
|
391
|
+
range: originalComponent.range,
|
|
392
|
+
parent: EMPTY_PARENT
|
|
393
|
+
},
|
|
394
|
+
arguments: [(0, _astNodeMutationHelpers.shallowCloneNode)(internalCompId)],
|
|
395
|
+
typeArguments: null,
|
|
396
|
+
optional: false,
|
|
397
|
+
loc: originalComponent.loc,
|
|
398
|
+
range: originalComponent.range,
|
|
399
|
+
parent: EMPTY_PARENT
|
|
400
|
+
},
|
|
401
|
+
loc: originalComponent.loc,
|
|
402
|
+
range: originalComponent.range,
|
|
403
|
+
parent: EMPTY_PARENT
|
|
404
|
+
}],
|
|
405
|
+
loc: originalComponent.loc,
|
|
406
|
+
range: originalComponent.range,
|
|
407
|
+
parent: originalComponent.parent
|
|
408
|
+
},
|
|
409
|
+
internalCompId: internalCompId,
|
|
410
|
+
forwardRefCompId: originalComponent.id
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function mapComponentDeclaration(node, options) {
|
|
415
|
+
var _node$async;
|
|
416
|
+
|
|
417
|
+
const createRendersTypeLoc = () => ({
|
|
418
|
+
loc: {
|
|
419
|
+
start: node.body.loc.end,
|
|
420
|
+
end: node.body.loc.end
|
|
421
|
+
},
|
|
422
|
+
range: [node.body.range[1], node.body.range[1]],
|
|
423
|
+
parent: EMPTY_PARENT
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
const returnType = {
|
|
427
|
+
type: 'TypeAnnotation',
|
|
428
|
+
typeAnnotation: {
|
|
429
|
+
type: 'GenericTypeAnnotation',
|
|
430
|
+
id: {
|
|
431
|
+
type: 'QualifiedTypeIdentifier',
|
|
432
|
+
qualification: {
|
|
433
|
+
type: 'Identifier',
|
|
434
|
+
name: 'React',
|
|
435
|
+
optional: false,
|
|
436
|
+
typeAnnotation: null,
|
|
437
|
+
...createRendersTypeLoc()
|
|
438
|
+
},
|
|
439
|
+
id: {
|
|
440
|
+
type: 'Identifier',
|
|
441
|
+
name: 'Node',
|
|
442
|
+
optional: false,
|
|
443
|
+
typeAnnotation: null,
|
|
444
|
+
...createRendersTypeLoc()
|
|
445
|
+
},
|
|
446
|
+
...createRendersTypeLoc()
|
|
447
|
+
},
|
|
448
|
+
typeParameters: null,
|
|
449
|
+
...createRendersTypeLoc()
|
|
450
|
+
},
|
|
451
|
+
...createRendersTypeLoc()
|
|
452
|
+
};
|
|
453
|
+
const {
|
|
454
|
+
props,
|
|
455
|
+
ref
|
|
456
|
+
} = mapComponentParameters(node.params, options);
|
|
457
|
+
let forwardRefDetails = null;
|
|
458
|
+
|
|
459
|
+
if (ref != null) {
|
|
460
|
+
forwardRefDetails = createForwardRefWrapper(node);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const comp = {
|
|
464
|
+
type: 'FunctionDeclaration',
|
|
465
|
+
id: forwardRefDetails != null ? (0, _astNodeMutationHelpers.shallowCloneNode)(forwardRefDetails.internalCompId) : (0, _astNodeMutationHelpers.shallowCloneNode)(node.id),
|
|
466
|
+
__componentDeclaration: true,
|
|
467
|
+
typeParameters: node.typeParameters,
|
|
468
|
+
params: props == null ? [] : ref == null ? [props] : [props, ref],
|
|
469
|
+
returnType,
|
|
470
|
+
body: node.body,
|
|
471
|
+
async: (_node$async = node.async) != null ? _node$async : false,
|
|
472
|
+
generator: false,
|
|
473
|
+
predicate: null,
|
|
474
|
+
loc: node.loc,
|
|
475
|
+
range: node.range,
|
|
476
|
+
parent: node.parent
|
|
477
|
+
};
|
|
478
|
+
return {
|
|
479
|
+
comp,
|
|
480
|
+
forwardRefDetails
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function mapDeclareHook(node) {
|
|
485
|
+
return {
|
|
486
|
+
type: 'DeclareFunction',
|
|
487
|
+
id: {
|
|
488
|
+
type: 'Identifier',
|
|
489
|
+
name: node.id.name,
|
|
490
|
+
optional: node.id.optional,
|
|
491
|
+
typeAnnotation: {
|
|
492
|
+
type: 'TypeAnnotation',
|
|
493
|
+
typeAnnotation: {
|
|
494
|
+
type: 'FunctionTypeAnnotation',
|
|
495
|
+
this: null,
|
|
496
|
+
params: node.id.typeAnnotation.typeAnnotation.params,
|
|
497
|
+
typeParameters: node.id.typeAnnotation.typeAnnotation.typeParameters,
|
|
498
|
+
rest: node.id.typeAnnotation.typeAnnotation.rest,
|
|
499
|
+
returnType: node.id.typeAnnotation.typeAnnotation.returnType,
|
|
500
|
+
loc: node.id.typeAnnotation.typeAnnotation.loc,
|
|
501
|
+
range: node.id.typeAnnotation.typeAnnotation.range,
|
|
502
|
+
parent: node.id.typeAnnotation.typeAnnotation.parent
|
|
503
|
+
},
|
|
504
|
+
loc: node.id.typeAnnotation.loc,
|
|
505
|
+
range: node.id.typeAnnotation.range,
|
|
506
|
+
parent: node.id.typeAnnotation.parent
|
|
507
|
+
},
|
|
508
|
+
loc: node.id.loc,
|
|
509
|
+
range: node.id.range,
|
|
510
|
+
parent: node.id.parent
|
|
511
|
+
},
|
|
512
|
+
loc: node.loc,
|
|
513
|
+
range: node.range,
|
|
514
|
+
parent: node.parent,
|
|
515
|
+
predicate: null
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function mapHookDeclaration(node) {
|
|
520
|
+
var _node$async2;
|
|
521
|
+
|
|
522
|
+
const comp = {
|
|
523
|
+
type: 'FunctionDeclaration',
|
|
524
|
+
id: node.id && (0, _astNodeMutationHelpers.shallowCloneNode)(node.id),
|
|
525
|
+
__hookDeclaration: true,
|
|
526
|
+
typeParameters: node.typeParameters,
|
|
527
|
+
params: node.params,
|
|
528
|
+
returnType: node.returnType,
|
|
529
|
+
body: node.body,
|
|
530
|
+
async: (_node$async2 = node.async) != null ? _node$async2 : false,
|
|
531
|
+
generator: false,
|
|
532
|
+
predicate: null,
|
|
533
|
+
loc: node.loc,
|
|
534
|
+
range: node.range,
|
|
535
|
+
parent: node.parent
|
|
536
|
+
};
|
|
537
|
+
return comp;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function scanForFirstComponentReference(compName, bodyList) {
|
|
541
|
+
for (let i = 0; i < bodyList.length; i++) {
|
|
542
|
+
const bodyNode = bodyList[i];
|
|
543
|
+
let referencePos = null;
|
|
544
|
+
|
|
545
|
+
_SimpleTraverser.SimpleTraverser.traverse(bodyNode, {
|
|
546
|
+
enter(node) {
|
|
547
|
+
switch (node.type) {
|
|
548
|
+
case 'Identifier':
|
|
549
|
+
{
|
|
550
|
+
if (node.name === compName) {
|
|
551
|
+
referencePos = i;
|
|
552
|
+
throw _SimpleTraverser.SimpleTraverser.Break;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
|
|
558
|
+
leave(_node) {}
|
|
559
|
+
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
if (referencePos != null) {
|
|
563
|
+
return referencePos;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return null;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function mapComponentDeclarationIntoList(node, newBody, options, insertExport) {
|
|
571
|
+
const {
|
|
572
|
+
comp,
|
|
573
|
+
forwardRefDetails
|
|
574
|
+
} = mapComponentDeclaration(node, options);
|
|
575
|
+
|
|
576
|
+
if (forwardRefDetails != null) {
|
|
577
|
+
const referencePos = scanForFirstComponentReference(forwardRefDetails.forwardRefCompId.name, newBody);
|
|
578
|
+
|
|
579
|
+
if (referencePos != null) {
|
|
580
|
+
newBody.splice(referencePos, 0, forwardRefDetails.forwardRefStatement);
|
|
581
|
+
} else {
|
|
582
|
+
newBody.push(forwardRefDetails.forwardRefStatement);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
newBody.push(comp);
|
|
586
|
+
|
|
587
|
+
if (insertExport != null) {
|
|
588
|
+
newBody.push(insertExport(forwardRefDetails.forwardRefCompId));
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
newBody.push(insertExport != null ? insertExport(comp) : comp);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function mapStatementList(stmts, options) {
|
|
598
|
+
const newBody = [];
|
|
599
|
+
|
|
600
|
+
for (const node of stmts) {
|
|
601
|
+
switch (node.type) {
|
|
602
|
+
case 'ComponentDeclaration':
|
|
603
|
+
{
|
|
604
|
+
mapComponentDeclarationIntoList(node, newBody, options);
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
case 'HookDeclaration':
|
|
609
|
+
{
|
|
610
|
+
const decl = mapHookDeclaration(node);
|
|
611
|
+
newBody.push(decl);
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
case 'ExportNamedDeclaration':
|
|
616
|
+
{
|
|
617
|
+
var _node$declaration, _node$declaration2;
|
|
618
|
+
|
|
619
|
+
if (((_node$declaration = node.declaration) == null ? void 0 : _node$declaration.type) === 'ComponentDeclaration') {
|
|
620
|
+
mapComponentDeclarationIntoList(node.declaration, newBody, options, componentOrRef => {
|
|
621
|
+
switch (componentOrRef.type) {
|
|
622
|
+
case 'FunctionDeclaration':
|
|
623
|
+
{
|
|
624
|
+
return nodeWith(node, {
|
|
625
|
+
declaration: componentOrRef
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
case 'Identifier':
|
|
630
|
+
{
|
|
631
|
+
return {
|
|
632
|
+
type: 'ExportNamedDeclaration',
|
|
633
|
+
declaration: null,
|
|
634
|
+
specifiers: [{
|
|
635
|
+
type: 'ExportSpecifier',
|
|
636
|
+
exported: (0, _astNodeMutationHelpers.shallowCloneNode)(componentOrRef),
|
|
637
|
+
local: (0, _astNodeMutationHelpers.shallowCloneNode)(componentOrRef),
|
|
638
|
+
loc: node.loc,
|
|
639
|
+
range: node.range,
|
|
640
|
+
parent: EMPTY_PARENT
|
|
641
|
+
}],
|
|
642
|
+
exportKind: 'value',
|
|
643
|
+
source: null,
|
|
644
|
+
loc: node.loc,
|
|
645
|
+
range: node.range,
|
|
646
|
+
parent: node.parent
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (((_node$declaration2 = node.declaration) == null ? void 0 : _node$declaration2.type) === 'HookDeclaration') {
|
|
655
|
+
const comp = mapHookDeclaration(node.declaration);
|
|
656
|
+
newBody.push(nodeWith(node, {
|
|
657
|
+
declaration: comp
|
|
658
|
+
}));
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
newBody.push(node);
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
case 'ExportDefaultDeclaration':
|
|
667
|
+
{
|
|
668
|
+
var _node$declaration3, _node$declaration4;
|
|
669
|
+
|
|
670
|
+
if (((_node$declaration3 = node.declaration) == null ? void 0 : _node$declaration3.type) === 'ComponentDeclaration') {
|
|
671
|
+
mapComponentDeclarationIntoList(node.declaration, newBody, options, componentOrRef => nodeWith(node, {
|
|
672
|
+
declaration: componentOrRef
|
|
673
|
+
}));
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (((_node$declaration4 = node.declaration) == null ? void 0 : _node$declaration4.type) === 'HookDeclaration') {
|
|
678
|
+
const comp = mapHookDeclaration(node.declaration);
|
|
679
|
+
newBody.push(nodeWith(node, {
|
|
680
|
+
declaration: comp
|
|
681
|
+
}));
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
newBody.push(node);
|
|
686
|
+
break;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
default:
|
|
690
|
+
{
|
|
691
|
+
newBody.push(node);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
return newBody;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function transformProgram(program, options) {
|
|
700
|
+
return _SimpleTransform.SimpleTransform.transformProgram(program, {
|
|
701
|
+
transform(node) {
|
|
702
|
+
switch (node.type) {
|
|
703
|
+
case 'DeclareComponent':
|
|
704
|
+
{
|
|
705
|
+
return mapDeclareComponent(node);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
case 'DeclareHook':
|
|
709
|
+
{
|
|
710
|
+
return mapDeclareHook(node);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
case 'Program':
|
|
714
|
+
case 'BlockStatement':
|
|
715
|
+
{
|
|
716
|
+
return nodeWith(node, {
|
|
717
|
+
body: mapStatementList(node.body, options)
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
case 'SwitchCase':
|
|
722
|
+
{
|
|
723
|
+
const consequent = mapStatementList(node.consequent, options);
|
|
724
|
+
return nodeWith(node, {
|
|
725
|
+
consequent
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
case 'ComponentDeclaration':
|
|
730
|
+
{
|
|
731
|
+
var _node$parent;
|
|
732
|
+
|
|
733
|
+
throw (0, _createSyntaxError.createSyntaxError)(node, `Components must be defined at the top level of a module or within a ` + `BlockStatement, instead got parent of "${(_node$parent = node.parent) == null ? void 0 : _node$parent.type}".`);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
case 'HookDeclaration':
|
|
737
|
+
{
|
|
738
|
+
var _node$parent2;
|
|
739
|
+
|
|
740
|
+
throw (0, _createSyntaxError.createSyntaxError)(node, `Hooks must be defined at the top level of a module or within a ` + `BlockStatement, instead got parent of "${(_node$parent2 = node.parent) == null ? void 0 : _node$parent2.type}".`);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
default:
|
|
744
|
+
{
|
|
745
|
+
return node;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
});
|
|
751
|
+
}
|