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,990 +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 _createSyntaxError = require("../utils/createSyntaxError");
13
-
14
- var _Builders = require("../utils/Builders");
15
-
16
- var _GenID = _interopRequireDefault(require("../utils/GenID"));
17
-
18
- function _interopRequireDefault(obj) {
19
- return obj && obj.__esModule ? obj : {
20
- default: obj
21
- };
22
- }
23
-
24
- let genID = null;
25
-
26
- function genIdent() {
27
- if (genID == null) {
28
- throw Error('GenID must be initialized at the start of the transform.');
29
- }
30
-
31
- return (0, _Builders.ident)(genID.id());
32
- }
33
-
34
- function objKeyToString(node) {
35
- switch (node.type) {
36
- case 'Identifier':
37
- return node.name;
38
-
39
- case 'Literal':
40
- {
41
- const {
42
- value
43
- } = node;
44
-
45
- if (typeof value === 'number') {
46
- return String(value);
47
- } else if (typeof value === 'string') {
48
- return value;
49
- } else {
50
- return node.raw;
51
- }
52
- }
53
- }
54
- }
55
-
56
- function convertMemberPattern(pattern) {
57
- const {
58
- base,
59
- property,
60
- loc,
61
- range
62
- } = pattern;
63
- const object = base.type === 'MatchIdentifierPattern' ? base.id : convertMemberPattern(base);
64
-
65
- if (property.type === 'Identifier') {
66
- return {
67
- type: 'MemberExpression',
68
- object,
69
- property,
70
- computed: false,
71
- optional: false,
72
- ...(0, _Builders.etc)({
73
- loc,
74
- range
75
- })
76
- };
77
- } else {
78
- return {
79
- type: 'MemberExpression',
80
- object,
81
- property,
82
- computed: true,
83
- optional: false,
84
- ...(0, _Builders.etc)({
85
- loc,
86
- range
87
- })
88
- };
89
- }
90
- }
91
-
92
- function checkDuplicateBindingName(seenBindingNames, node, name) {
93
- if (seenBindingNames.has(name)) {
94
- throw (0, _createSyntaxError.createSyntaxError)(node, `Duplicate variable name '${name}' in match case pattern.`);
95
- }
96
-
97
- seenBindingNames.add(name);
98
- }
99
-
100
- function checkBindingKind(node, kind) {
101
- if (kind === 'var') {
102
- throw (0, _createSyntaxError.createSyntaxError)(node, `'var' bindings are not allowed. Use 'const' or 'let'.`);
103
- }
104
- }
105
-
106
- function needsPropExistsCond(pattern) {
107
- switch (pattern.type) {
108
- case 'MatchWildcardPattern':
109
- case 'MatchBindingPattern':
110
- case 'MatchIdentifierPattern':
111
- case 'MatchMemberPattern':
112
- return true;
113
-
114
- case 'MatchLiteralPattern':
115
- case 'MatchUnaryPattern':
116
- case 'MatchObjectPattern':
117
- case 'MatchInstancePattern':
118
- case 'MatchArrayPattern':
119
- return false;
120
-
121
- case 'MatchAsPattern':
122
- {
123
- const {
124
- pattern: asPattern
125
- } = pattern;
126
- return needsPropExistsCond(asPattern);
127
- }
128
-
129
- case 'MatchOrPattern':
130
- {
131
- const {
132
- patterns
133
- } = pattern;
134
- return patterns.some(needsPropExistsCond);
135
- }
136
- }
137
- }
138
-
139
- function analyzeProperties(key, pattern, seenBindingNames, properties, rest) {
140
- const conditions = [];
141
- const bindings = [];
142
- const objKeys = [];
143
- const seenNames = new Set();
144
- properties.forEach(prop => {
145
- const {
146
- key: objKey,
147
- pattern: propPattern
148
- } = prop;
149
- objKeys.push(objKey);
150
- const name = objKeyToString(objKey);
151
-
152
- if (seenNames.has(name)) {
153
- throw (0, _createSyntaxError.createSyntaxError)(propPattern, `Duplicate property name '${name}' in match object pattern.`);
154
- }
155
-
156
- seenNames.add(name);
157
- const propKey = key.concat(objKey);
158
-
159
- if (needsPropExistsCond(propPattern)) {
160
- conditions.push({
161
- type: 'prop-exists',
162
- key,
163
- propName: name
164
- });
165
- }
166
-
167
- const {
168
- conditions: childConditions,
169
- bindings: childBindings
170
- } = analyzePattern(propPattern, propKey, seenBindingNames);
171
- conditions.push(...childConditions);
172
- bindings.push(...childBindings);
173
- });
174
-
175
- if (rest != null && rest.argument != null) {
176
- const {
177
- id,
178
- kind
179
- } = rest.argument;
180
- checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
181
- checkBindingKind(pattern, kind);
182
- bindings.push({
183
- type: 'object-rest',
184
- key,
185
- exclude: objKeys,
186
- kind,
187
- id
188
- });
189
- }
190
-
191
- return {
192
- conditions,
193
- bindings
194
- };
195
- }
196
-
197
- function constructorExpression(constructor) {
198
- switch (constructor.type) {
199
- case 'MatchIdentifierPattern':
200
- return constructor.id;
201
-
202
- case 'MatchMemberPattern':
203
- return convertMemberPattern(constructor);
204
- }
205
- }
206
-
207
- function analyzePattern(pattern, key, seenBindingNames) {
208
- switch (pattern.type) {
209
- case 'MatchWildcardPattern':
210
- {
211
- return {
212
- conditions: [],
213
- bindings: []
214
- };
215
- }
216
-
217
- case 'MatchLiteralPattern':
218
- {
219
- const {
220
- literal
221
- } = pattern;
222
- const condition = {
223
- type: 'eq',
224
- key,
225
- arg: literal
226
- };
227
- return {
228
- conditions: [condition],
229
- bindings: []
230
- };
231
- }
232
-
233
- case 'MatchUnaryPattern':
234
- {
235
- const {
236
- operator,
237
- argument,
238
- loc,
239
- range
240
- } = pattern;
241
-
242
- if (argument.value === 0) {
243
- throw (0, _createSyntaxError.createSyntaxError)(pattern, `'+0' and '-0' are not yet supported in match unary patterns.`);
244
- }
245
-
246
- const arg = {
247
- type: 'UnaryExpression',
248
- operator,
249
- argument,
250
- prefix: true,
251
- ...(0, _Builders.etc)({
252
- loc,
253
- range
254
- })
255
- };
256
- const condition = {
257
- type: 'eq',
258
- key,
259
- arg
260
- };
261
- return {
262
- conditions: [condition],
263
- bindings: []
264
- };
265
- }
266
-
267
- case 'MatchIdentifierPattern':
268
- {
269
- const {
270
- id
271
- } = pattern;
272
- const condition = id.name === 'NaN' ? {
273
- type: 'is-nan',
274
- key
275
- } : {
276
- type: 'eq',
277
- key,
278
- arg: id
279
- };
280
- return {
281
- conditions: [condition],
282
- bindings: []
283
- };
284
- }
285
-
286
- case 'MatchMemberPattern':
287
- {
288
- const arg = convertMemberPattern(pattern);
289
- const condition = {
290
- type: 'eq',
291
- key,
292
- arg
293
- };
294
- return {
295
- conditions: [condition],
296
- bindings: []
297
- };
298
- }
299
-
300
- case 'MatchBindingPattern':
301
- {
302
- const {
303
- id,
304
- kind
305
- } = pattern;
306
- checkDuplicateBindingName(seenBindingNames, pattern, id.name);
307
- checkBindingKind(pattern, kind);
308
- const binding = {
309
- type: 'id',
310
- key,
311
- kind,
312
- id
313
- };
314
- return {
315
- conditions: [],
316
- bindings: [binding]
317
- };
318
- }
319
-
320
- case 'MatchAsPattern':
321
- {
322
- const {
323
- pattern: asPattern,
324
- target
325
- } = pattern;
326
-
327
- if (asPattern.type === 'MatchBindingPattern') {
328
- throw (0, _createSyntaxError.createSyntaxError)(pattern, `Match 'as' patterns are not allowed directly on binding patterns.`);
329
- }
330
-
331
- const {
332
- conditions,
333
- bindings
334
- } = analyzePattern(asPattern, key, seenBindingNames);
335
- const defaultKind = 'const';
336
- const [id, kind] = target.type === 'MatchBindingPattern' ? [target.id, target.kind] : [target, defaultKind];
337
- checkDuplicateBindingName(seenBindingNames, pattern, id.name);
338
- checkBindingKind(pattern, kind);
339
- const binding = {
340
- type: 'id',
341
- key,
342
- kind,
343
- id
344
- };
345
- return {
346
- conditions,
347
- bindings: bindings.concat(binding)
348
- };
349
- }
350
-
351
- case 'MatchArrayPattern':
352
- {
353
- const {
354
- elements,
355
- rest
356
- } = pattern;
357
- const lengthOp = rest == null ? 'eq' : 'gte';
358
- const conditions = [{
359
- type: 'array',
360
- key,
361
- length: elements.length,
362
- lengthOp
363
- }];
364
- const bindings = [];
365
- elements.forEach((element, i) => {
366
- const elementKey = key.concat((0, _Builders.numberLiteral)(i));
367
- const {
368
- conditions: childConditions,
369
- bindings: childBindings
370
- } = analyzePattern(element, elementKey, seenBindingNames);
371
- conditions.push(...childConditions);
372
- bindings.push(...childBindings);
373
- });
374
-
375
- if (rest != null && rest.argument != null) {
376
- const {
377
- id,
378
- kind
379
- } = rest.argument;
380
- checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
381
- checkBindingKind(pattern, kind);
382
- bindings.push({
383
- type: 'array-rest',
384
- key,
385
- exclude: elements.length,
386
- kind,
387
- id
388
- });
389
- }
390
-
391
- return {
392
- conditions,
393
- bindings
394
- };
395
- }
396
-
397
- case 'MatchObjectPattern':
398
- {
399
- const {
400
- properties,
401
- rest
402
- } = pattern;
403
- const {
404
- conditions: propertyConditions,
405
- bindings
406
- } = analyzeProperties(key, pattern, seenBindingNames, properties, rest);
407
- const conditions = [{
408
- type: 'object',
409
- key
410
- }, ...propertyConditions];
411
- return {
412
- conditions,
413
- bindings
414
- };
415
- }
416
-
417
- case 'MatchInstancePattern':
418
- {
419
- const {
420
- targetConstructor,
421
- properties: {
422
- properties,
423
- rest
424
- }
425
- } = pattern;
426
- const {
427
- conditions: propertyConditions,
428
- bindings
429
- } = analyzeProperties(key, pattern, seenBindingNames, properties, rest);
430
- const conditions = [{
431
- type: 'instanceof',
432
- key,
433
- constructor: constructorExpression(targetConstructor)
434
- }, ...propertyConditions];
435
- return {
436
- conditions,
437
- bindings
438
- };
439
- }
440
-
441
- case 'MatchOrPattern':
442
- {
443
- const {
444
- patterns
445
- } = pattern;
446
- let hasWildcard = false;
447
- const orConditions = patterns.map(subpattern => {
448
- const {
449
- conditions,
450
- bindings
451
- } = analyzePattern(subpattern, key, seenBindingNames);
452
-
453
- if (bindings.length > 0) {
454
- throw (0, _createSyntaxError.createSyntaxError)(pattern, `Bindings in match 'or' patterns are not yet supported.`);
455
- }
456
-
457
- if (conditions.length === 0) {
458
- hasWildcard = true;
459
- }
460
-
461
- return conditions;
462
- });
463
-
464
- if (hasWildcard) {
465
- return {
466
- conditions: [],
467
- bindings: []
468
- };
469
- }
470
-
471
- return {
472
- conditions: [{
473
- type: 'or',
474
- orConditions
475
- }],
476
- bindings: []
477
- };
478
- }
479
- }
480
- }
481
-
482
- function expressionOfKey(root, key) {
483
- return key.reduce((acc, prop) => prop.type === 'Identifier' ? {
484
- type: 'MemberExpression',
485
- object: acc,
486
- property: (0, _astNodeMutationHelpers.shallowCloneNode)(prop),
487
- computed: false,
488
- optional: false,
489
- ...(0, _Builders.etc)()
490
- } : {
491
- type: 'MemberExpression',
492
- object: acc,
493
- property: (0, _astNodeMutationHelpers.shallowCloneNode)(prop),
494
- computed: true,
495
- optional: false,
496
- ...(0, _Builders.etc)()
497
- }, (0, _astNodeMutationHelpers.deepCloneNode)(root));
498
- }
499
-
500
- function testsOfCondition(root, condition) {
501
- switch (condition.type) {
502
- case 'eq':
503
- {
504
- const {
505
- key,
506
- arg
507
- } = condition;
508
- return [{
509
- type: 'BinaryExpression',
510
- left: expressionOfKey(root, key),
511
- right: arg,
512
- operator: '===',
513
- ...(0, _Builders.etc)()
514
- }];
515
- }
516
-
517
- case 'is-nan':
518
- {
519
- const {
520
- key
521
- } = condition;
522
- const callee = {
523
- type: 'MemberExpression',
524
- object: (0, _Builders.ident)('Number'),
525
- property: (0, _Builders.ident)('isNaN'),
526
- computed: false,
527
- optional: false,
528
- ...(0, _Builders.etc)()
529
- };
530
- return [(0, _Builders.callExpression)(callee, [expressionOfKey(root, key)])];
531
- }
532
-
533
- case 'array':
534
- {
535
- const {
536
- key,
537
- length,
538
- lengthOp
539
- } = condition;
540
- const operator = lengthOp === 'eq' ? '===' : '>=';
541
- const isArray = (0, _Builders.callExpression)({
542
- type: 'MemberExpression',
543
- object: (0, _Builders.ident)('Array'),
544
- property: (0, _Builders.ident)('isArray'),
545
- computed: false,
546
- optional: false,
547
- ...(0, _Builders.etc)()
548
- }, [expressionOfKey(root, key)]);
549
- const lengthCheck = {
550
- type: 'BinaryExpression',
551
- left: {
552
- type: 'MemberExpression',
553
- object: expressionOfKey(root, key),
554
- property: (0, _Builders.ident)('length'),
555
- computed: false,
556
- optional: false,
557
- ...(0, _Builders.etc)()
558
- },
559
- right: (0, _Builders.numberLiteral)(length),
560
- operator,
561
- ...(0, _Builders.etc)()
562
- };
563
- return [isArray, lengthCheck];
564
- }
565
-
566
- case 'object':
567
- {
568
- const {
569
- key
570
- } = condition;
571
- const typeofObject = (0, _Builders.typeofExpression)(expressionOfKey(root, key), 'object');
572
- const typeofFunction = (0, _Builders.typeofExpression)(expressionOfKey(root, key), 'function');
573
- const notNull = {
574
- type: 'BinaryExpression',
575
- left: expressionOfKey(root, key),
576
- right: (0, _Builders.nullLiteral)(),
577
- operator: '!==',
578
- ...(0, _Builders.etc)()
579
- };
580
- return [(0, _Builders.disjunction)([(0, _Builders.conjunction)([typeofObject, notNull]), typeofFunction])];
581
- }
582
-
583
- case 'instanceof':
584
- {
585
- const {
586
- key,
587
- constructor
588
- } = condition;
589
- return [{
590
- type: 'BinaryExpression',
591
- left: expressionOfKey(root, key),
592
- right: constructor,
593
- operator: 'instanceof',
594
- ...(0, _Builders.etc)()
595
- }];
596
- }
597
-
598
- case 'prop-exists':
599
- {
600
- const {
601
- key,
602
- propName
603
- } = condition;
604
- const inObject = {
605
- type: 'BinaryExpression',
606
- left: (0, _Builders.stringLiteral)(propName),
607
- right: expressionOfKey(root, key),
608
- operator: 'in',
609
- ...(0, _Builders.etc)()
610
- };
611
- return [inObject];
612
- }
613
-
614
- case 'or':
615
- {
616
- const {
617
- orConditions
618
- } = condition;
619
- const tests = orConditions.map(conditions => (0, _Builders.conjunction)(testsOfConditions(root, conditions)));
620
- return [(0, _Builders.disjunction)(tests)];
621
- }
622
- }
623
- }
624
-
625
- function testsOfConditions(root, conditions) {
626
- return conditions.flatMap(condition => testsOfCondition(root, condition));
627
- }
628
-
629
- function statementsOfBindings(root, bindings) {
630
- return bindings.map(binding => {
631
- switch (binding.type) {
632
- case 'id':
633
- {
634
- const {
635
- key,
636
- kind,
637
- id
638
- } = binding;
639
- return (0, _Builders.variableDeclaration)(kind, id, expressionOfKey(root, key));
640
- }
641
-
642
- case 'array-rest':
643
- {
644
- const {
645
- key,
646
- kind,
647
- id,
648
- exclude
649
- } = binding;
650
- const init = (0, _Builders.callExpression)({
651
- type: 'MemberExpression',
652
- object: expressionOfKey(root, key),
653
- property: (0, _Builders.ident)('slice'),
654
- computed: false,
655
- optional: false,
656
- ...(0, _Builders.etc)()
657
- }, [(0, _Builders.numberLiteral)(exclude)]);
658
- return (0, _Builders.variableDeclaration)(kind, id, init);
659
- }
660
-
661
- case 'object-rest':
662
- {
663
- const {
664
- key,
665
- kind,
666
- id,
667
- exclude
668
- } = binding;
669
- const destructuring = {
670
- type: 'ObjectPattern',
671
- properties: exclude.map(prop => prop.type === 'Identifier' ? {
672
- type: 'Property',
673
- key: (0, _astNodeMutationHelpers.shallowCloneNode)(prop),
674
- value: genIdent(),
675
- kind: 'init',
676
- computed: false,
677
- method: false,
678
- shorthand: false,
679
- ...(0, _Builders.etc)(),
680
- parent: _Builders.EMPTY_PARENT
681
- } : {
682
- type: 'Property',
683
- key: (0, _astNodeMutationHelpers.shallowCloneNode)(prop),
684
- value: genIdent(),
685
- kind: 'init',
686
- computed: true,
687
- method: false,
688
- shorthand: false,
689
- ...(0, _Builders.etc)(),
690
- parent: _Builders.EMPTY_PARENT
691
- }).concat({
692
- type: 'RestElement',
693
- argument: id,
694
- ...(0, _Builders.etc)()
695
- }),
696
- typeAnnotation: null,
697
- ...(0, _Builders.etc)()
698
- };
699
- return (0, _Builders.variableDeclaration)(kind, destructuring, expressionOfKey(root, key));
700
- }
701
- }
702
- });
703
- }
704
-
705
- const fallthroughErrorMsgText = `Match: No case succesfully matched. Make exhaustive or add a wildcard case using '_'.`;
706
-
707
- function fallthroughErrorMsg(value) {
708
- return {
709
- type: 'BinaryExpression',
710
- operator: '+',
711
- left: (0, _Builders.stringLiteral)(`${fallthroughErrorMsgText} Argument: `),
712
- right: value,
713
- ...(0, _Builders.etc)()
714
- };
715
- }
716
-
717
- function fallthroughError(value) {
718
- return (0, _Builders.throwStatement)(fallthroughErrorMsg(value));
719
- }
720
-
721
- function calculateSimpleArgument(node) {
722
- switch (node.type) {
723
- case 'Identifier':
724
- case 'Super':
725
- return true;
726
-
727
- case 'MemberExpression':
728
- {
729
- const {
730
- object,
731
- property,
732
- computed
733
- } = node;
734
-
735
- if (computed && property.type !== 'Literal') {
736
- return false;
737
- }
738
-
739
- return calculateSimpleArgument(object);
740
- }
741
-
742
- default:
743
- return false;
744
- }
745
- }
746
-
747
- function analyzeCases(cases) {
748
- let hasBindings = false;
749
- let hasWildcard = false;
750
- const analyses = [];
751
-
752
- for (let i = 0; i < cases.length; i++) {
753
- const {
754
- pattern,
755
- guard,
756
- body
757
- } = cases[i];
758
- const {
759
- conditions,
760
- bindings
761
- } = analyzePattern(pattern, [], new Set());
762
- hasBindings = hasBindings || bindings.length > 0;
763
- analyses.push({
764
- conditions,
765
- bindings,
766
- guard,
767
- body
768
- });
769
-
770
- if (conditions.length === 0 && guard == null) {
771
- hasWildcard = true;
772
- break;
773
- }
774
- }
775
-
776
- return {
777
- hasBindings,
778
- hasWildcard,
779
- analyses
780
- };
781
- }
782
-
783
- function mapMatchExpression(node) {
784
- const {
785
- argument,
786
- cases
787
- } = node;
788
- const {
789
- hasBindings,
790
- hasWildcard,
791
- analyses
792
- } = analyzeCases(cases);
793
- const isSimpleArgument = !hasBindings && calculateSimpleArgument(argument);
794
- const genRoot = !isSimpleArgument ? genIdent() : null;
795
- const root = genRoot == null ? argument : genRoot;
796
-
797
- if (isSimpleArgument) {
798
- const wildcardAnalaysis = hasWildcard ? analyses.pop() : null;
799
- const lastBody = wildcardAnalaysis != null ? wildcardAnalaysis.body : (0, _Builders.iife)([fallthroughError((0, _astNodeMutationHelpers.shallowCloneNode)(root))]);
800
- return analyses.reverse().reduce((acc, analysis) => {
801
- const {
802
- conditions,
803
- guard,
804
- body
805
- } = analysis;
806
- const tests = testsOfConditions(root, conditions);
807
-
808
- if (guard != null) {
809
- tests.push(guard);
810
- }
811
-
812
- return {
813
- type: 'ConditionalExpression',
814
- test: (0, _Builders.conjunction)(tests),
815
- consequent: body,
816
- alternate: acc,
817
- ...(0, _Builders.etc)()
818
- };
819
- }, lastBody);
820
- }
821
-
822
- const statements = analyses.map(({
823
- conditions,
824
- bindings,
825
- guard,
826
- body
827
- }) => {
828
- const returnNode = {
829
- type: 'ReturnStatement',
830
- argument: body,
831
- ...(0, _Builders.etc)()
832
- };
833
- const bodyNode = guard == null ? returnNode : {
834
- type: 'IfStatement',
835
- test: guard,
836
- consequent: returnNode,
837
- ...(0, _Builders.etc)()
838
- };
839
- const bindingNodes = statementsOfBindings(root, bindings);
840
- const caseBody = bindingNodes.concat(bodyNode);
841
-
842
- if (conditions.length > 0) {
843
- const tests = testsOfConditions(root, conditions);
844
- return {
845
- type: 'IfStatement',
846
- test: (0, _Builders.conjunction)(tests),
847
- consequent: {
848
- type: 'BlockStatement',
849
- body: caseBody,
850
- ...(0, _Builders.etc)()
851
- },
852
- ...(0, _Builders.etc)()
853
- };
854
- } else {
855
- if (bindingNodes.length > 0) {
856
- return {
857
- type: 'BlockStatement',
858
- body: caseBody,
859
- ...(0, _Builders.etc)()
860
- };
861
- } else {
862
- return bodyNode;
863
- }
864
- }
865
- });
866
-
867
- if (!hasWildcard) {
868
- statements.push(fallthroughError((0, _astNodeMutationHelpers.shallowCloneNode)(root)));
869
- }
870
-
871
- const [params, args] = genRoot == null ? [[], []] : [[genRoot], [argument]];
872
- return (0, _Builders.iife)(statements, params, args);
873
- }
874
-
875
- function mapMatchStatement(node) {
876
- const {
877
- argument,
878
- cases
879
- } = node;
880
- const {
881
- hasBindings,
882
- hasWildcard,
883
- analyses
884
- } = analyzeCases(cases);
885
- const topLabel = genIdent();
886
- const isSimpleArgument = !hasBindings && calculateSimpleArgument(argument);
887
- const genRoot = !isSimpleArgument ? genIdent() : null;
888
- const root = genRoot == null ? argument : genRoot;
889
- const statements = [];
890
-
891
- if (genRoot != null) {
892
- statements.push((0, _Builders.variableDeclaration)('const', genRoot, argument));
893
- }
894
-
895
- analyses.forEach(({
896
- conditions,
897
- bindings,
898
- guard,
899
- body
900
- }) => {
901
- const breakNode = {
902
- type: 'BreakStatement',
903
- label: (0, _astNodeMutationHelpers.shallowCloneNode)(topLabel),
904
- ...(0, _Builders.etc)()
905
- };
906
- const bodyStatements = body.body.concat(breakNode);
907
- const guardedBodyStatements = guard == null ? bodyStatements : [{
908
- type: 'IfStatement',
909
- test: guard,
910
- consequent: {
911
- type: 'BlockStatement',
912
- body: bodyStatements,
913
- ...(0, _Builders.etc)()
914
- },
915
- ...(0, _Builders.etc)()
916
- }];
917
- const bindingNodes = statementsOfBindings(root, bindings);
918
- const caseBody = bindingNodes.concat(guardedBodyStatements);
919
-
920
- if (conditions.length > 0) {
921
- const tests = testsOfConditions(root, conditions);
922
- statements.push({
923
- type: 'IfStatement',
924
- test: (0, _Builders.conjunction)(tests),
925
- consequent: {
926
- type: 'BlockStatement',
927
- body: caseBody,
928
- ...(0, _Builders.etc)()
929
- },
930
- ...(0, _Builders.etc)()
931
- });
932
- } else {
933
- statements.push({
934
- type: 'BlockStatement',
935
- body: caseBody,
936
- ...(0, _Builders.etc)()
937
- });
938
- }
939
- });
940
-
941
- if (!hasWildcard) {
942
- statements.push(fallthroughError((0, _astNodeMutationHelpers.shallowCloneNode)(root)));
943
- }
944
-
945
- return {
946
- type: 'LabeledStatement',
947
- label: topLabel,
948
- body: {
949
- type: 'BlockStatement',
950
- body: statements,
951
- ...(0, _Builders.etc)()
952
- },
953
- ...(0, _Builders.etc)()
954
- };
955
- }
956
-
957
- function transformProgram(program, _options) {
958
- genID = new _GenID.default('m');
959
- return _SimpleTransform.SimpleTransform.transformProgram(program, {
960
- transform(node) {
961
- switch (node.type) {
962
- case 'MatchExpression':
963
- {
964
- return mapMatchExpression(node);
965
- }
966
-
967
- case 'MatchStatement':
968
- {
969
- return mapMatchStatement(node);
970
- }
971
-
972
- case 'Identifier':
973
- {
974
- if (genID == null) {
975
- throw Error('GenID must be initialized at the start of the transform.');
976
- }
977
-
978
- genID.addUsage(node.name);
979
- return node;
980
- }
981
-
982
- default:
983
- {
984
- return node;
985
- }
986
- }
987
- }
988
-
989
- });
990
- }