ripple 0.3.44 → 0.3.45

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.
@@ -1,1287 +0,0 @@
1
- /** @import * as AST from '@tsrx/core/types/estree' */
2
- /** @import * as ESTreeJSX from '@tsrx/core/types/estree-jsx' */
3
-
4
- import { regex_is_valid_identifier } from './patterns.js';
5
- import { sanitize_template_string } from './sanitize_template_string.js';
6
-
7
- /**
8
- * @template {AST.Node} T
9
- * @param {T} node
10
- * @param {AST.NodeWithLocation | undefined} loc_info
11
- * @param {boolean} is_deep_copy
12
- * @returns {T}
13
- */
14
- export function set_location(node, loc_info, is_deep_copy = false) {
15
- if (loc_info) {
16
- node.start = loc_info.start;
17
- node.end = loc_info.end;
18
-
19
- if (is_deep_copy) {
20
- node.loc = {
21
- start: { ...loc_info.loc.start },
22
- end: { ...loc_info.loc.end },
23
- };
24
- } else {
25
- node.loc = loc_info.loc;
26
- }
27
- }
28
-
29
- return node;
30
- }
31
-
32
- /**
33
- * @param {Array<AST.Expression | AST.SpreadElement | null>} elements
34
- * @returns {AST.ArrayExpression}
35
- */
36
- export function array(elements = []) {
37
- return { type: 'ArrayExpression', elements, metadata: { path: [] } };
38
- }
39
-
40
- /**
41
- * @param {Array<AST.Pattern | null>} elements
42
- * @returns {AST.ArrayPattern}
43
- */
44
- export function array_pattern(elements) {
45
- return { type: 'ArrayPattern', elements, metadata: { path: [] } };
46
- }
47
-
48
- /**
49
- * @param {AST.Pattern} left
50
- * @param {AST.Expression} right
51
- * @returns {AST.AssignmentPattern}
52
- */
53
- export function assignment_pattern(left, right) {
54
- return { type: 'AssignmentPattern', left, right, metadata: { path: [] } };
55
- }
56
-
57
- /**
58
- * @param {Array<AST.Pattern>} params
59
- * @param {AST.BlockStatement | AST.Expression} body
60
- * @returns {AST.ArrowFunctionExpression}
61
- */
62
- export function arrow(params, body, async = false) {
63
- return {
64
- type: 'ArrowFunctionExpression',
65
- params,
66
- body,
67
- expression: body.type !== 'BlockStatement',
68
- generator: false,
69
- async,
70
- metadata: /** @type {any} */ (null), // should not be used by codegen
71
- };
72
- }
73
-
74
- /**
75
- * @param {AST.Identifier} id
76
- * @param {AST.Pattern[]} params
77
- * @param {AST.Node[]} body
78
- * @returns {AST.Component}
79
- */
80
- export function component(id, params, body) {
81
- return {
82
- type: 'Component',
83
- id,
84
- params,
85
- body,
86
- css: null,
87
- metadata: { path: [] },
88
- default: false,
89
- };
90
- }
91
-
92
- /**
93
- * @param {AST.AssignmentOperator} operator
94
- * @param {AST.Pattern} left
95
- * @param {AST.Expression} right
96
- * @returns {AST.AssignmentExpression}
97
- */
98
- export function assignment(operator, left, right) {
99
- return { type: 'AssignmentExpression', operator, left, right, metadata: { path: [] } };
100
- }
101
-
102
- /**
103
- * @template T
104
- * @param {T & AST.BaseFunction} func
105
- * @returns {T & AST.BaseFunction}
106
- */
107
- export function async(func) {
108
- return { ...func, async: true };
109
- }
110
-
111
- /**
112
- * @param {AST.Expression} argument
113
- * @returns {AST.AwaitExpression}
114
- */
115
- function await_builder(argument) {
116
- return { type: 'AwaitExpression', argument, metadata: { path: [] } };
117
- }
118
-
119
- /**
120
- * @param {AST.BinaryOperator} operator
121
- * @param {AST.Expression} left
122
- * @param {AST.Expression} right
123
- * @returns {AST.BinaryExpression}
124
- */
125
- export function binary(operator, left, right) {
126
- return { type: 'BinaryExpression', operator, left, right, metadata: { path: [] } };
127
- }
128
-
129
- /**
130
- * @param {AST.Statement[]} body
131
- * @param {AST.NodeWithLocation} [loc_info]
132
- * @returns {AST.BlockStatement}
133
- */
134
- export function block(body, loc_info) {
135
- /** @type {AST.BlockStatement} */
136
- const node = { type: 'BlockStatement', body, metadata: { path: [] } };
137
-
138
- return set_location(node, loc_info);
139
- }
140
-
141
- /**
142
- * @param {string} name
143
- * @param {AST.Statement} body
144
- * @returns {AST.LabeledStatement}
145
- */
146
- export function labeled(name, body) {
147
- return { type: 'LabeledStatement', label: id(name), body, metadata: { path: [] } };
148
- }
149
-
150
- /**
151
- * @param {string | AST.Expression} callee
152
- * @param {...(AST.Expression | AST.SpreadElement | false | undefined)} args
153
- * @returns {AST.CallExpression}
154
- */
155
- export function call(callee, ...args) {
156
- if (typeof callee === 'string') callee = id(callee);
157
- args = args.slice();
158
-
159
- // replacing missing arguments with `void(0)`, unless they're at the end in which case remove them
160
- let i = args.length;
161
- let popping = true;
162
- while (i--) {
163
- if (!args[i]) {
164
- if (popping) {
165
- args.pop();
166
- } else {
167
- args[i] = void0;
168
- }
169
- } else {
170
- popping = false;
171
- }
172
- }
173
-
174
- return {
175
- type: 'CallExpression',
176
- callee,
177
- arguments: /** @type {Array<AST.Expression | AST.SpreadElement>} */ (args),
178
- optional: false,
179
- metadata: { path: [] },
180
- };
181
- }
182
-
183
- /**
184
- * @param {string | AST.Expression} callee
185
- * @param {...(AST.Expression | AST.SpreadElement | false | undefined)} args
186
- * @returns {AST.ChainExpression}
187
- */
188
- export function maybe_call(callee, ...args) {
189
- const expression = /** @type {AST.SimpleCallExpression} */ (call(callee, ...args));
190
- expression.optional = true;
191
-
192
- return {
193
- type: 'ChainExpression',
194
- expression,
195
- metadata: { path: [] },
196
- };
197
- }
198
-
199
- /**
200
- * @param {AST.UnaryOperator} operator
201
- * @param {AST.Expression} argument
202
- * @returns {AST.UnaryExpression}
203
- */
204
- export function unary(operator, argument) {
205
- return { type: 'UnaryExpression', argument, operator, prefix: true, metadata: { path: [] } };
206
- }
207
-
208
- /**
209
- * @param {AST.Expression} test
210
- * @param {AST.Expression} consequent
211
- * @param {AST.Expression} alternate
212
- * @returns {AST.ConditionalExpression}
213
- */
214
- export function conditional(test, consequent, alternate) {
215
- return { type: 'ConditionalExpression', test, consequent, alternate, metadata: { path: [] } };
216
- }
217
-
218
- /**
219
- * @param {AST.LogicalOperator} operator
220
- * @param {AST.Expression} left
221
- * @param {AST.Expression} right
222
- * @returns {AST.LogicalExpression}
223
- */
224
- export function logical(operator, left, right) {
225
- return { type: 'LogicalExpression', operator, left, right, metadata: { path: [] } };
226
- }
227
-
228
- /**
229
- * @param {'const' | 'let' | 'var'} kind
230
- * @param {AST.VariableDeclarator[]} declarations
231
- * @returns {AST.VariableDeclaration}
232
- */
233
- export function declaration(kind, declarations) {
234
- return {
235
- type: 'VariableDeclaration',
236
- kind,
237
- declarations,
238
- metadata: { path: [] },
239
- };
240
- }
241
-
242
- /**
243
- * @param {AST.Pattern | string} pattern
244
- * @param {AST.Expression} [init]
245
- * @returns {AST.VariableDeclarator}
246
- */
247
- export function declarator(pattern, init) {
248
- if (typeof pattern === 'string') pattern = id(pattern);
249
- return { type: 'VariableDeclarator', id: pattern, init, metadata: { path: [] } };
250
- }
251
-
252
- /** @type {AST.EmptyStatement} */
253
- export const empty = {
254
- type: 'EmptyStatement',
255
- metadata: { path: [] },
256
- };
257
-
258
- /**
259
- * @param {AST.Expression | AST.MaybeNamedClassDeclaration | AST.MaybeNamedFunctionDeclaration} declaration
260
- * @returns {AST.ExportDefaultDeclaration}
261
- */
262
- export function export_default(declaration) {
263
- return { type: 'ExportDefaultDeclaration', declaration, metadata: { path: [] } };
264
- }
265
-
266
- /**
267
- * @param {AST.Declaration | null} declaration
268
- * @param {AST.ExportSpecifier[]} [specifiers]
269
- * @param {AST.ImportAttribute[]} [attributes]
270
- * @param {AST.ExportNamedDeclaration['exportKind']} [exportKind]
271
- * @param {AST.Literal | null} [source]
272
- * @returns {AST.ExportNamedDeclaration}
273
- */
274
- export function export_builder(
275
- declaration,
276
- specifiers = [],
277
- attributes = [],
278
- exportKind = 'value',
279
- source = null,
280
- ) {
281
- return {
282
- type: 'ExportNamedDeclaration',
283
- declaration,
284
- specifiers,
285
- attributes,
286
- exportKind,
287
- source,
288
- metadata: { path: [] },
289
- };
290
- }
291
-
292
- /**
293
- * @param {AST.Identifier} id
294
- * @param {AST.Pattern[]} params
295
- * @param {AST.BlockStatement} body
296
- * @returns {AST.FunctionDeclaration}
297
- */
298
- export function function_declaration(id, params, body, async = false) {
299
- return {
300
- type: 'FunctionDeclaration',
301
- id,
302
- params,
303
- body,
304
- generator: false,
305
- async,
306
- metadata: { path: [] },
307
- };
308
- }
309
-
310
- /**
311
- * @param {string} name
312
- * @param {AST.Statement[]} body
313
- * @returns {AST.Property & { value: AST.FunctionExpression}}}
314
- */
315
- export function get(name, body) {
316
- return /** @type {AST.Property & { value: AST.FunctionExpression}} */ (
317
- prop('get', key(name), function_builder(null, [], block(body)))
318
- );
319
- }
320
-
321
- /**
322
- * @param {string} name
323
- * @param {AST.NodeWithLocation} [loc_info]
324
- * @returns {AST.Identifier}
325
- */
326
- export function id(name, loc_info) {
327
- /** @type {AST.Identifier} */
328
- const node = {
329
- type: 'Identifier',
330
- name,
331
- optional: false,
332
- decorators: [],
333
- metadata: { path: [] },
334
- };
335
-
336
- return set_location(node, loc_info);
337
- }
338
-
339
- /**
340
- * @param {string} name
341
- * @returns {AST.PrivateIdentifier}
342
- */
343
- export function private_id(name) {
344
- return { type: 'PrivateIdentifier', name, metadata: { path: [] } };
345
- }
346
-
347
- /**
348
- * @param {string} local
349
- * @returns {AST.ImportNamespaceSpecifier}
350
- */
351
- function import_namespace(local) {
352
- return {
353
- type: 'ImportNamespaceSpecifier',
354
- local: id(local),
355
- metadata: { path: [] },
356
- };
357
- }
358
-
359
- /**
360
- * @param {string} name
361
- * @param {AST.Expression} value
362
- * @returns {AST.Property}
363
- */
364
- export function init(name, value) {
365
- return prop('init', key(name), value);
366
- }
367
-
368
- /**
369
- * @param {boolean | string | number | bigint | false | RegExp | null | undefined} value
370
- * @param {AST.NodeWithLocation} [loc_info]
371
- * @returns {AST.Literal}
372
- */
373
- export function literal(value, loc_info) {
374
- const node = /** @type {AST.Literal} */ ({ type: 'Literal', value, metadata: { path: [] } });
375
-
376
- return set_location(node, loc_info);
377
- }
378
-
379
- /**
380
- * @param {AST.Expression | AST.Super} object
381
- * @param {string | AST.Expression | AST.PrivateIdentifier} property
382
- * @param {boolean} computed
383
- * @param {boolean} optional
384
- * @param {AST.NodeWithLocation} [loc_info]
385
- * @returns {AST.MemberExpression}
386
- */
387
- export function member(object, property, computed = false, optional = false, loc_info) {
388
- if (typeof property === 'string') {
389
- property = id(property);
390
- }
391
-
392
- /** @type {AST.MemberExpression} */
393
- const node = {
394
- type: 'MemberExpression',
395
- object,
396
- property,
397
- computed,
398
- optional,
399
- metadata: { path: [] },
400
- };
401
-
402
- return set_location(node, loc_info);
403
- }
404
-
405
- /**
406
- * @param {AST.Expression} expression
407
- * @param {AST.Node} type_annotation
408
- * @param {AST.NodeWithLocation} [loc_info]
409
- * @returns {AST.TSAsExpression}
410
- */
411
- export function ts_as(expression, type_annotation, loc_info) {
412
- const node = /** @type {AST.TSAsExpression} */ ({
413
- type: 'TSAsExpression',
414
- expression,
415
- typeAnnotation: type_annotation,
416
- metadata: { path: [] },
417
- });
418
-
419
- return set_location(node, loc_info);
420
- }
421
-
422
- /**
423
- * @param {AST.Expression} expression
424
- * @param {AST.NodeWithLocation} [loc_info]
425
- * @returns {AST.ParenthesizedExpression}
426
- */
427
- export function parenthesized(expression, loc_info) {
428
- const node = /** @type {AST.ParenthesizedExpression} */ ({
429
- type: 'ParenthesizedExpression',
430
- expression,
431
- metadata: { path: [] },
432
- });
433
-
434
- return set_location(node, loc_info);
435
- }
436
-
437
- /**
438
- * @param {AST.Identifier | AST.MemberExpression} expr_name
439
- * @param {AST.Node | null} [type_arguments]
440
- * @param {AST.NodeWithLocation} [loc_info]
441
- * @returns {AST.TSTypeQuery}
442
- */
443
- export function ts_type_query(expr_name, type_arguments = null, loc_info) {
444
- const node = /** @type {AST.TSTypeQuery} */ ({
445
- type: 'TSTypeQuery',
446
- exprName: expr_name,
447
- typeArguments: type_arguments,
448
- metadata: { path: [] },
449
- });
450
-
451
- return set_location(node, loc_info);
452
- }
453
-
454
- /**
455
- * @param {AST.Node[]} params
456
- * @param {AST.NodeWithLocation} [loc_info]
457
- * @returns {AST.TSTypeParameterInstantiation}
458
- */
459
- export function ts_type_parameter_instantiation(params, loc_info) {
460
- const node = /** @type {AST.TSTypeParameterInstantiation} */ ({
461
- type: 'TSTypeParameterInstantiation',
462
- params,
463
- metadata: { path: [] },
464
- });
465
-
466
- return set_location(node, loc_info);
467
- }
468
-
469
- /**
470
- * @param {AST.Identifier | AST.Node} type_name
471
- * @param {AST.Node | null} [type_arguments]
472
- * @param {AST.NodeWithLocation} [loc_info]
473
- * @returns {AST.TSTypeReference}
474
- */
475
- export function ts_type_reference(type_name, type_arguments = null, loc_info) {
476
- const node = /** @type {AST.TSTypeReference} */ ({
477
- type: 'TSTypeReference',
478
- typeName: type_name,
479
- typeArguments: type_arguments,
480
- metadata: { path: [] },
481
- });
482
-
483
- return set_location(node, loc_info);
484
- }
485
-
486
- /**
487
- * @param {AST.Literal} literal_node
488
- * @param {AST.NodeWithLocation} [loc_info]
489
- * @returns {AST.TSLiteralType}
490
- */
491
- export function ts_literal_type(literal_node, loc_info) {
492
- const node = /** @type {AST.TSLiteralType} */ ({
493
- type: 'TSLiteralType',
494
- literal: literal_node,
495
- metadata: { path: [] },
496
- });
497
-
498
- return set_location(node, loc_info);
499
- }
500
-
501
- /**
502
- * @param {AST.Node[]} types
503
- * @param {AST.NodeWithLocation} [loc_info]
504
- * @returns {AST.TSIntersectionType}
505
- */
506
- export function ts_intersection_type(types, loc_info) {
507
- const node = /** @type {AST.TSIntersectionType} */ ({
508
- type: 'TSIntersectionType',
509
- types,
510
- metadata: { path: [] },
511
- });
512
-
513
- return set_location(node, loc_info);
514
- }
515
-
516
- /**
517
- * @param {'string' | 'number' | 'boolean' | 'any' | 'void' | 'null' | 'undefined' | 'never' | 'unknown' | 'bigint' | 'symbol' | 'object'} keyword
518
- * @param {AST.NodeWithLocation} [loc_info]
519
- * @returns {AST.TypeNode}
520
- */
521
- export function ts_keyword_type(keyword, loc_info) {
522
- /** @type {Record<string, string>} */
523
- const keyword_to_type = {
524
- string: 'TSStringKeyword',
525
- number: 'TSNumberKeyword',
526
- boolean: 'TSBooleanKeyword',
527
- any: 'TSAnyKeyword',
528
- void: 'TSVoidKeyword',
529
- null: 'TSNullKeyword',
530
- undefined: 'TSUndefinedKeyword',
531
- never: 'TSNeverKeyword',
532
- unknown: 'TSUnknownKeyword',
533
- bigint: 'TSBigIntKeyword',
534
- symbol: 'TSSymbolKeyword',
535
- object: 'TSObjectKeyword',
536
- };
537
-
538
- const node = /** @type {AST.TypeNode} */ ({
539
- type: keyword_to_type[keyword],
540
- metadata: { path: [] },
541
- });
542
-
543
- return set_location(node, loc_info);
544
- }
545
-
546
- /**
547
- * @param {AST.Node} type_annotation
548
- * @param {AST.NodeWithLocation} [loc_info]
549
- * @returns {AST.TSTypeAnnotation}
550
- */
551
- export function ts_type_annotation(type_annotation, loc_info) {
552
- const node = /** @type {AST.TSTypeAnnotation} */ ({
553
- type: 'TSTypeAnnotation',
554
- typeAnnotation: type_annotation,
555
- metadata: { path: [] },
556
- });
557
-
558
- return set_location(node, loc_info);
559
- }
560
-
561
- /**
562
- * @param {AST.Expression} key
563
- * @param {AST.Node | null} type_annotation
564
- * @param {AST.NodeWithLocation} [loc_info]
565
- * @returns {AST.TSPropertySignature}
566
- */
567
- export function ts_property_signature(key, type_annotation = null, loc_info) {
568
- const node = /** @type {AST.TSPropertySignature} */ ({
569
- type: 'TSPropertySignature',
570
- key,
571
- accessibility: undefined,
572
- computed: false,
573
- optional: false,
574
- readonly: false,
575
- static: false,
576
- kind: 'init',
577
- typeAnnotation: type_annotation,
578
- metadata: { path: [] },
579
- });
580
-
581
- return set_location(node, loc_info);
582
- }
583
-
584
- /**
585
- * @param {AST.Node[]} members
586
- * @param {AST.NodeWithLocation} [loc_info]
587
- * @returns {AST.TSTypeLiteral}
588
- */
589
- export function ts_type_literal(members, loc_info) {
590
- const node = /** @type {AST.TSTypeLiteral} */ ({
591
- type: 'TSTypeLiteral',
592
- members,
593
- metadata: { path: [] },
594
- });
595
-
596
- return set_location(node, loc_info);
597
- }
598
-
599
- /**
600
- * @param {AST.Identifier} id
601
- * @param {AST.Node} type_annotation
602
- * @param {AST.NodeWithLocation} [loc_info]
603
- * @returns {AST.TSTypeAliasDeclaration}
604
- */
605
- export function ts_type_alias(id, type_annotation, loc_info) {
606
- const node = /** @type {AST.TSTypeAliasDeclaration} */ ({
607
- type: 'TSTypeAliasDeclaration',
608
- id,
609
- typeParameters: undefined,
610
- typeAnnotation: type_annotation,
611
- declare: false,
612
- metadata: { path: [] },
613
- });
614
-
615
- return set_location(node, loc_info);
616
- }
617
-
618
- /**
619
- * @param {string} path
620
- * @returns {AST.Identifier | AST.MemberExpression}
621
- */
622
- export function member_id(path) {
623
- const parts = path.split('.');
624
-
625
- /** @type {AST.Identifier | AST.MemberExpression} */
626
- let expression = id(parts[0]);
627
-
628
- for (let i = 1; i < parts.length; i += 1) {
629
- expression = member(expression, id(parts[i]));
630
- }
631
- return expression;
632
- }
633
-
634
- /**
635
- * @param {Array<AST.Property | AST.SpreadElement>} properties
636
- * @param {AST.NodeWithLocation} [loc_info]
637
- * @returns {AST.ObjectExpression}
638
- */
639
- export function object(properties, loc_info) {
640
- /** @type {AST.ObjectExpression} */
641
- const node = { type: 'ObjectExpression', properties, metadata: { path: [] } };
642
-
643
- return set_location(node, loc_info);
644
- }
645
-
646
- /**
647
- * @param {Array<AST.RestElement | AST.AssignmentProperty>} properties
648
- * @returns {AST.ObjectPattern}
649
- */
650
- export function object_pattern(properties) {
651
- return { type: 'ObjectPattern', properties, metadata: { path: [] } };
652
- }
653
-
654
- /**
655
- * @template {AST.Expression} Value
656
- * @param {AST.Property['kind']} kind
657
- * @param {AST.Expression } key
658
- * @param {Value} value
659
- * @param {boolean} computed
660
- * @param {boolean} shorthand
661
- * @returns {AST.Property}
662
- */
663
- export function prop(kind, key, value, computed = false, shorthand = false) {
664
- return {
665
- type: 'Property',
666
- kind,
667
- key,
668
- value,
669
- method: false,
670
- shorthand,
671
- computed,
672
- metadata: { path: [] },
673
- };
674
- }
675
-
676
- /**
677
- * @param {AST.Expression | AST.PrivateIdentifier} key
678
- * @param {AST.Expression | null | undefined} value
679
- * @param {boolean} computed
680
- * @param {boolean} is_static
681
- * @returns {AST.PropertyDefinition}
682
- */
683
- export function prop_def(key, value, computed = false, is_static = false) {
684
- return {
685
- type: 'PropertyDefinition',
686
- key,
687
- value,
688
- computed,
689
- static: is_static,
690
- metadata: { path: [] },
691
- };
692
- }
693
-
694
- /**
695
- * @param {string} cooked
696
- * @param {boolean} tail
697
- * @returns {AST.TemplateElement}
698
- */
699
- export function quasi(cooked, tail = false) {
700
- const raw = sanitize_template_string(cooked);
701
- return { type: 'TemplateElement', value: { raw, cooked }, tail, metadata: { path: [] } };
702
- }
703
-
704
- /**
705
- * @param {AST.Pattern} argument
706
- * @returns {AST.RestElement}
707
- */
708
- export function rest(argument) {
709
- return { type: 'RestElement', argument, metadata: { path: [] } };
710
- }
711
-
712
- /**
713
- * @param {AST.Expression[]} expressions
714
- * @returns {AST.SequenceExpression}
715
- */
716
- export function sequence(expressions) {
717
- return { type: 'SequenceExpression', expressions, metadata: { path: [] } };
718
- }
719
-
720
- /**
721
- * @param {string} name
722
- * @param {AST.Statement[]} body
723
- * @returns {AST.Property & { value: AST.FunctionExpression}}
724
- */
725
- export function set(name, body) {
726
- return /** @type {AST.Property & { value: AST.FunctionExpression}} */ (
727
- prop('set', key(name), function_builder(null, [id('$$value')], block(body)))
728
- );
729
- }
730
-
731
- /**
732
- * @param {AST.Expression} argument
733
- * @returns {AST.SpreadElement}
734
- */
735
- export function spread(argument) {
736
- return { type: 'SpreadElement', argument, metadata: { path: [] } };
737
- }
738
-
739
- /**
740
- * @param {AST.Expression} expression
741
- * @returns {AST.ExpressionStatement}
742
- */
743
- export function stmt(expression) {
744
- return { type: 'ExpressionStatement', expression, metadata: { path: [] } };
745
- }
746
-
747
- /**
748
- * @param {AST.TemplateElement[]} elements
749
- * @param {AST.Expression[]} expressions
750
- * @param {AST.NodeWithLocation} [loc_info]
751
- * @returns {AST.TemplateLiteral}
752
- */
753
- export function template(elements, expressions, loc_info) {
754
- /** @type {AST.TemplateLiteral} */
755
- const node = { type: 'TemplateLiteral', quasis: elements, expressions, metadata: { path: [] } };
756
-
757
- return set_location(node, loc_info);
758
- }
759
-
760
- /**
761
- * @param {AST.Expression | AST.BlockStatement} expression
762
- * @param {boolean} [async]
763
- * @returns {ReturnType<typeof unthunk>}
764
- */
765
- export function thunk(expression, async = false) {
766
- const fn = arrow([], expression);
767
- if (async) fn.async = true;
768
- return unthunk(fn);
769
- }
770
-
771
- /**
772
- * Replace "(arg) => func(arg)" to "func"
773
- * @param {AST.Expression} expression
774
- * @returns {AST.Expression}
775
- */
776
- export function unthunk(expression) {
777
- if (
778
- expression.type === 'ArrowFunctionExpression' &&
779
- expression.async === false &&
780
- expression.body.type === 'CallExpression' &&
781
- expression.body.callee.type === 'Identifier' &&
782
- expression.params.length === expression.body.arguments.length &&
783
- expression.params.every((param, index) => {
784
- const arg = /** @type {AST.SimpleCallExpression} */ (expression.body).arguments[index];
785
- return param.type === 'Identifier' && arg.type === 'Identifier' && param.name === arg.name;
786
- })
787
- ) {
788
- return expression.body.callee;
789
- }
790
- return expression;
791
- }
792
-
793
- /**
794
- *
795
- * @param {string | AST.Expression} expression
796
- * @param {AST.NodeWithLocation | undefined} loc_info
797
- * @param {...AST.Expression} args
798
- * @returns {AST.NewExpression}
799
- */
800
- function new_builder(expression, loc_info, ...args) {
801
- if (typeof expression === 'string') expression = id(expression);
802
-
803
- /** @type {AST.NewExpression} */
804
- const node = {
805
- callee: expression,
806
- arguments: args,
807
- type: 'NewExpression',
808
- metadata: { path: [] },
809
- };
810
-
811
- return set_location(node, loc_info);
812
- }
813
-
814
- /**
815
- * @param {AST.UpdateOperator} operator
816
- * @param {AST.Expression} argument
817
- * @param {boolean} prefix
818
- * @returns {AST.UpdateExpression}
819
- */
820
- export function update(operator, argument, prefix = false) {
821
- return { type: 'UpdateExpression', operator, argument, prefix, metadata: { path: [] } };
822
- }
823
-
824
- /**
825
- * @param {AST.Expression} test
826
- * @param {AST.Statement} body
827
- * @returns {AST.DoWhileStatement}
828
- */
829
- export function do_while(test, body) {
830
- return { type: 'DoWhileStatement', test, body, metadata: { path: [] } };
831
- }
832
-
833
- const true_instance = literal(true);
834
- const false_instance = literal(false);
835
- const null_instance = literal(null);
836
-
837
- /** @type {AST.DebuggerStatement} */
838
- const debugger_builder = {
839
- type: 'DebuggerStatement',
840
- metadata: { path: [] },
841
- };
842
-
843
- /** @type {AST.ThisExpression} */
844
- const this_instance = {
845
- type: 'ThisExpression',
846
- metadata: { path: [] },
847
- };
848
-
849
- /**
850
- * @param {string | AST.Pattern} pattern
851
- * @param { AST.Expression} [init]
852
- * @returns {AST.VariableDeclaration}
853
- */
854
- function let_builder(pattern, init) {
855
- return declaration('let', [declarator(pattern, init)]);
856
- }
857
-
858
- /**
859
- * @param {string | AST.Pattern} pattern
860
- * @param { AST.Expression} init
861
- * @returns {AST.VariableDeclaration}
862
- */
863
- function const_builder(pattern, init) {
864
- return declaration('const', [declarator(pattern, init)]);
865
- }
866
-
867
- /**
868
- * @param {string | AST.Pattern} pattern
869
- * @param { AST.Expression} [init]
870
- * @returns {AST.VariableDeclaration}
871
- */
872
- function var_builder(pattern, init) {
873
- return declaration('var', [declarator(pattern, init)]);
874
- }
875
-
876
- /**
877
- *
878
- * @param {AST.VariableDeclaration | AST.Expression | null} init
879
- * @param {AST.Expression} test
880
- * @param {AST.Expression} update
881
- * @param {AST.Statement} body
882
- * @returns {AST.ForStatement}
883
- */
884
- function for_builder(init, test, update, body) {
885
- return { type: 'ForStatement', init, test, update, body, metadata: { path: [] } };
886
- }
887
-
888
- /**
889
- * @param {AST.VariableDeclaration | AST.Pattern} left
890
- * @param {AST.Expression} right
891
- * @param {AST.Statement} body
892
- * @param {boolean} [await_flag]
893
- * @param {AST.NodeWithLocation} [loc_info]
894
- * @returns {AST.ForOfStatement}
895
- */
896
- export function for_of(left, right, body, await_flag = false, loc_info) {
897
- /** @type {AST.ForOfStatement} */
898
- const node = {
899
- type: 'ForOfStatement',
900
- left,
901
- right,
902
- body,
903
- await: await_flag,
904
- metadata: { path: [] },
905
- };
906
-
907
- return set_location(node, loc_info);
908
- }
909
-
910
- /**
911
- *
912
- * @param {'constructor' | 'method' | 'get' | 'set'} kind
913
- * @param {AST.Expression | AST.PrivateIdentifier} key
914
- * @param {AST.Pattern[]} params
915
- * @param {AST.Statement[]} body
916
- * @param {boolean} computed
917
- * @param {boolean} is_static
918
- * @returns {AST.MethodDefinition}
919
- */
920
- export function method(kind, key, params, body, computed = false, is_static = false) {
921
- return {
922
- type: 'MethodDefinition',
923
- key,
924
- kind,
925
- value: function_builder(null, params, block(body)),
926
- computed,
927
- static: is_static,
928
- metadata: { path: [] },
929
- };
930
- }
931
-
932
- /**
933
- *
934
- * @param {AST.Identifier | null} id
935
- * @param {AST.Pattern[]} params
936
- * @param {AST.BlockStatement} body
937
- * @param {boolean} async
938
- * @param {AST.NodeWithLocation} [loc_info]
939
- * @returns {AST.FunctionExpression}
940
- */
941
- function function_builder(id, params, body, async = false, loc_info) {
942
- /** @type {AST.FunctionExpression} */
943
- const node = {
944
- type: 'FunctionExpression',
945
- id,
946
- params,
947
- body,
948
- generator: false,
949
- async,
950
- metadata: { path: [] },
951
- };
952
-
953
- return set_location(node, loc_info);
954
- }
955
-
956
- /**
957
- * @param {AST.Expression} test
958
- * @param {AST.Statement} consequent
959
- * @param {AST.Statement | null} [alternate]
960
- * @param {AST.NodeWithLocation} [loc_info]
961
- * @returns {AST.IfStatement}
962
- */
963
- function if_builder(test, consequent, alternate, loc_info) {
964
- /** @type {AST.IfStatement} */
965
- const node = { type: 'IfStatement', test, consequent, alternate, metadata: { path: [] } };
966
- return set_location(node, loc_info);
967
- }
968
-
969
- /**
970
- * @param {string} as
971
- * @param {string} source
972
- * @param {Array<AST.ImportAttribute>} attributes
973
- * @param {AST.ImportDeclaration['importKind']} importKind
974
- * @returns {AST.ImportDeclaration}
975
- */
976
- export function import_all(as, source, attributes = [], importKind = 'value') {
977
- return {
978
- type: 'ImportDeclaration',
979
- source: literal(source),
980
- specifiers: [import_namespace(as)],
981
- attributes,
982
- importKind,
983
- metadata: { path: [] },
984
- };
985
- }
986
-
987
- /**
988
- * @param {Array<[string, string, AST.ImportDeclaration['importKind']]>} parts
989
- * @param {string} source
990
- * @param {Array<AST.ImportAttribute>} attributes
991
- * @param {AST.ImportDeclaration['importKind']} importKind
992
- * @returns {AST.ImportDeclaration}
993
- */
994
- export function imports(parts, source, attributes = [], importKind = 'value') {
995
- return {
996
- type: 'ImportDeclaration',
997
- source: literal(source),
998
- attributes,
999
- specifiers: parts.map((p) => ({
1000
- type: 'ImportSpecifier',
1001
- imported: id(p[0]),
1002
- local: id(p[1]),
1003
- importKind: p.length > 2 ? p[2] : 'value',
1004
- metadata: { path: [] },
1005
- })),
1006
- importKind,
1007
- metadata: { path: [] },
1008
- };
1009
- }
1010
-
1011
- /**
1012
- * @param {AST.Expression | null} argument
1013
- * @param {AST.NodeWithLocation} [loc_info]
1014
- * @returns {AST.ReturnStatement}
1015
- */
1016
- function return_builder(argument = null, loc_info) {
1017
- /** @type {AST.ReturnStatement} */
1018
- const node = { type: 'ReturnStatement', argument, metadata: { path: [] } };
1019
- return set_location(node, loc_info);
1020
- }
1021
-
1022
- /**
1023
- * @param {string} str
1024
- * @returns {AST.ThrowStatement}
1025
- */
1026
- export function throw_error(str) {
1027
- return {
1028
- type: 'ThrowStatement',
1029
- argument: new_builder('Error', undefined, literal(str)),
1030
- metadata: { path: [] },
1031
- };
1032
- }
1033
-
1034
- /**
1035
- * @param {AST.BlockStatement} block
1036
- * @param {AST.CatchClause | null} handler
1037
- * @param {AST.BlockStatement | null} finalizer
1038
- * @param {AST.BlockStatement | null} pending
1039
- * @returns {AST.TryStatement}
1040
- */
1041
- export function try_builder(block, handler = null, finalizer = null, pending = null) {
1042
- return {
1043
- type: 'TryStatement',
1044
- block,
1045
- handler,
1046
- finalizer,
1047
- pending,
1048
- metadata: { path: [] },
1049
- };
1050
- }
1051
-
1052
- /**
1053
- * @param {AST.Pattern | null} param
1054
- * @param {AST.Pattern | null} reset_param
1055
- * @param {AST.BlockStatement} body
1056
- * @param {AST.NodeWithLocation} [loc_info]
1057
- * @return {AST.CatchClause}
1058
- */
1059
- export function catch_clause_builder(param, reset_param, body, loc_info) {
1060
- /** @type {AST.CatchClause} */
1061
- const node = {
1062
- type: 'CatchClause',
1063
- param,
1064
- resetParam: reset_param,
1065
- body,
1066
- metadata: { path: [] },
1067
- };
1068
-
1069
- return set_location(node, loc_info);
1070
- }
1071
-
1072
- export { catch_clause_builder as catch_clause };
1073
-
1074
- /**
1075
- * @param {string} name
1076
- * @returns {AST.Expression}
1077
- */
1078
- export function key(name) {
1079
- return regex_is_valid_identifier.test(name) ? id(name) : literal(name);
1080
- }
1081
-
1082
- /**
1083
- * @param {ESTreeJSX.JSXIdentifier | ESTreeJSX.JSXNamespacedName} name
1084
- * @param {AST.Literal | ESTreeJSX.JSXExpressionContainer | null} value
1085
- * @param {boolean} [shorthand]
1086
- * @param {AST.NodeWithLocation} [loc_info]
1087
- * @returns {ESTreeJSX.JSXAttribute}
1088
- */
1089
- export function jsx_attribute(name, value = null, shorthand = false, loc_info) {
1090
- const node = /** @type {ESTreeJSX.JSXAttribute} */ ({
1091
- type: 'JSXAttribute',
1092
- name,
1093
- value,
1094
- shorthand,
1095
- metadata: { path: [] },
1096
- });
1097
-
1098
- return set_location(node, loc_info);
1099
- }
1100
-
1101
- /**
1102
- * @param {AST.Element} node
1103
- * @param {ESTreeJSX.JSXOpeningElement['attributes']} attributes
1104
- * @param {ESTreeJSX.JSXElement['children']} children
1105
- * @returns {ESTreeJSX.JSXElement}
1106
- */
1107
- export function jsx_element(node, attributes = [], children = []) {
1108
- return {
1109
- ...node,
1110
- type: 'JSXElement',
1111
- openingElement: {
1112
- ...node.openingElement,
1113
- attributes,
1114
- metadata: {
1115
- path: [...node.metadata.path],
1116
- },
1117
- },
1118
- closingElement: node.closingElement
1119
- ? {
1120
- ...node.closingElement,
1121
- metadata: {
1122
- path: [...node.metadata.path],
1123
- },
1124
- }
1125
- : null,
1126
- children,
1127
- };
1128
- }
1129
-
1130
- /**
1131
- * @param {ESTreeJSX.JSXFragment['children']} children
1132
- * @param {ESTreeJSX.JSXOpeningFragment['attributes']} [attributes]
1133
- * @returns {ESTreeJSX.JSXFragment}
1134
- */
1135
- export function jsx_fragment(children = [], attributes = []) {
1136
- return {
1137
- type: 'JSXFragment',
1138
- openingFragment: {
1139
- type: 'JSXOpeningFragment',
1140
- attributes,
1141
- metadata: { path: [] },
1142
- },
1143
- closingFragment: {
1144
- type: 'JSXClosingFragment',
1145
- metadata: { path: [] },
1146
- },
1147
- children,
1148
- metadata: { path: [] },
1149
- };
1150
- }
1151
-
1152
- /**
1153
- * @param {AST.Expression | ESTreeJSX.JSXEmptyExpression} expression
1154
- * @param {AST.NodeWithLocation} [loc_info]
1155
- * @returns {ESTreeJSX.JSXExpressionContainer}
1156
- */
1157
- export function jsx_expression_container(expression, loc_info) {
1158
- const node = /** @type {ESTreeJSX.JSXExpressionContainer} */ ({
1159
- type: 'JSXExpressionContainer',
1160
- expression,
1161
- metadata: { path: [] },
1162
- });
1163
-
1164
- return set_location(node, loc_info);
1165
- }
1166
-
1167
- /**
1168
- * @param {string} name
1169
- * @param {AST.NodeWithLocation} [loc_info]
1170
- * @returns {ESTreeJSX.JSXIdentifier}
1171
- */
1172
- export function jsx_id(name, loc_info) {
1173
- /** @type {ESTreeJSX.JSXIdentifier} */
1174
- const node = {
1175
- type: 'JSXIdentifier',
1176
- name,
1177
- metadata: { path: [] },
1178
- };
1179
- return set_location(node, loc_info);
1180
- }
1181
-
1182
- /**
1183
- * @param {ESTreeJSX.JSXIdentifier | ESTreeJSX.JSXMemberExpression} object
1184
- * @param {ESTreeJSX.JSXIdentifier} property
1185
- * @returns {ESTreeJSX.JSXMemberExpression}
1186
- */
1187
- export function jsx_member(object, property) {
1188
- return {
1189
- type: 'JSXMemberExpression',
1190
- object,
1191
- property,
1192
- metadata: { path: [] },
1193
- };
1194
- }
1195
-
1196
- /**
1197
- * @param {AST.Expression} argument
1198
- * @param {AST.NodeWithLocation} [loc_info]
1199
- * @returns {ESTreeJSX.JSXSpreadAttribute}
1200
- */
1201
- export function jsx_spread_attribute(argument, loc_info) {
1202
- const node = /** @type {ESTreeJSX.JSXSpreadAttribute} */ ({
1203
- type: 'JSXSpreadAttribute',
1204
- argument,
1205
- metadata: { path: [] },
1206
- });
1207
-
1208
- return set_location(node, loc_info);
1209
- }
1210
-
1211
- /**
1212
- * @param {string} value
1213
- * @param {string} raw
1214
- * @returns {ESTreeJSX.JSXText}
1215
- */
1216
- export function jsx_text(value, raw) {
1217
- return {
1218
- type: 'JSXText',
1219
- value,
1220
- raw,
1221
- metadata: { path: [] },
1222
- };
1223
- }
1224
-
1225
- /**
1226
- * @param {AST.Expression} discriminant
1227
- * @param {AST.SwitchCase[]} cases
1228
- * @param {AST.NodeWithLocation} [loc_info]
1229
- * @returns {AST.SwitchStatement}
1230
- */
1231
- export function switch_builder(discriminant, cases, loc_info) {
1232
- /** @type {AST.SwitchStatement} */
1233
- const node = {
1234
- type: 'SwitchStatement',
1235
- discriminant,
1236
- cases,
1237
- metadata: { path: [] },
1238
- };
1239
-
1240
- return set_location(node, loc_info);
1241
- }
1242
-
1243
- /**
1244
- * @param {AST.Expression | null} test
1245
- * @param {AST.Statement[]} consequent
1246
- * @returns {AST.SwitchCase}
1247
- */
1248
- export function switch_case(test = null, consequent = []) {
1249
- return {
1250
- type: 'SwitchCase',
1251
- test,
1252
- consequent,
1253
- metadata: { path: [] },
1254
- };
1255
- }
1256
-
1257
- export const void0 = unary('void', literal(0));
1258
-
1259
- /**
1260
- * @type {AST.BreakStatement}
1261
- */
1262
- export const break_statement = {
1263
- type: 'BreakStatement',
1264
- label: null,
1265
- metadata: { path: [] },
1266
- };
1267
-
1268
- export {
1269
- await_builder as await,
1270
- let_builder as let,
1271
- const_builder as const,
1272
- var_builder as var,
1273
- export_builder as export,
1274
- true_instance as true,
1275
- false_instance as false,
1276
- break_statement as break,
1277
- for_builder as for,
1278
- switch_builder as switch,
1279
- function_builder as function,
1280
- return_builder as return,
1281
- if_builder as if,
1282
- this_instance as this,
1283
- null_instance as null,
1284
- debugger_builder as debugger,
1285
- try_builder as try,
1286
- new_builder as new,
1287
- };