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