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