@zessjs/compiler 1.0.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.
@@ -0,0 +1,1348 @@
1
+ import { generate } from "astring";
2
+ import { decodeHTML } from "entities";
3
+ import merge from "merge-source-map";
4
+ import { parse } from "meriyah";
5
+ import { SourceMapGenerator } from "source-map";
6
+
7
+ //#region src/visit.ts
8
+ const ESTreeNodeKeys = {
9
+ AccessorProperty: [
10
+ "key",
11
+ "value",
12
+ "$decorators"
13
+ ],
14
+ ArrayExpression: ["$elements"],
15
+ ArrayPattern: ["$elements"],
16
+ ArrowFunctionExpression: ["$params", "body"],
17
+ AssignmentExpression: ["left", "right"],
18
+ AssignmentPattern: ["left", "right"],
19
+ AwaitExpression: ["argument"],
20
+ BinaryExpression: ["left", "right"],
21
+ BlockStatement: ["$body"],
22
+ BreakStatement: ["label"],
23
+ CallExpression: ["callee", "$arguments"],
24
+ ChainExpression: ["expression"],
25
+ ImportExpression: ["source", "options"],
26
+ CatchClause: ["param", "body"],
27
+ ClassBody: ["$body"],
28
+ ClassDeclaration: [
29
+ "id",
30
+ "body",
31
+ "superClass",
32
+ "$decorators"
33
+ ],
34
+ ClassExpression: [
35
+ "id",
36
+ "body",
37
+ "superClass",
38
+ "$decorators"
39
+ ],
40
+ ConditionalExpression: [
41
+ "test",
42
+ "consequent",
43
+ "alternate"
44
+ ],
45
+ ContinueStatement: ["label"],
46
+ Decorator: ["expression"],
47
+ DoWhileStatement: ["test", "body"],
48
+ ExportAllDeclaration: [
49
+ "source",
50
+ "exported",
51
+ "$attributes"
52
+ ],
53
+ ExportDefaultDeclaration: ["declaration"],
54
+ ExportNamedDeclaration: [
55
+ "declaration",
56
+ "$specifiers",
57
+ "source",
58
+ "$attributes"
59
+ ],
60
+ ExportSpecifier: ["local", "exported"],
61
+ ExpressionStatement: ["expression"],
62
+ PropertyDefinition: [
63
+ "key",
64
+ "value",
65
+ "$decorators"
66
+ ],
67
+ ForInStatement: [
68
+ "left",
69
+ "right",
70
+ "body"
71
+ ],
72
+ ForOfStatement: [
73
+ "left",
74
+ "right",
75
+ "body"
76
+ ],
77
+ ForStatement: [
78
+ "init",
79
+ "test",
80
+ "update",
81
+ "body"
82
+ ],
83
+ FunctionDeclaration: [
84
+ "id",
85
+ "$params",
86
+ "body"
87
+ ],
88
+ FunctionExpression: [
89
+ "id",
90
+ "$params",
91
+ "body"
92
+ ],
93
+ IfStatement: [
94
+ "test",
95
+ "consequent",
96
+ "alternate"
97
+ ],
98
+ ImportDeclaration: [
99
+ "source",
100
+ "$specifiers",
101
+ "$attributes"
102
+ ],
103
+ ImportDefaultSpecifier: ["local"],
104
+ ImportAttribute: ["key", "value"],
105
+ ImportNamespaceSpecifier: ["local"],
106
+ ImportSpecifier: ["local", "imported"],
107
+ JSXNamespacedName: ["namespace", "name"],
108
+ JSXAttribute: ["name", "value"],
109
+ JSXClosingElement: ["name"],
110
+ JSXElement: [
111
+ "openingElement",
112
+ "closingElement",
113
+ "$children"
114
+ ],
115
+ JSXExpressionContainer: ["expression"],
116
+ JSXFragment: [
117
+ "openingFragment",
118
+ "closingFragment",
119
+ "$children"
120
+ ],
121
+ JSXOpeningElement: ["name", "$attributes"],
122
+ JSXSpreadAttribute: ["argument"],
123
+ JSXSpreadChild: ["expression"],
124
+ JSXMemberExpression: ["object", "property"],
125
+ LabeledStatement: ["label", "body"],
126
+ LogicalExpression: ["left", "right"],
127
+ MemberExpression: ["object", "property"],
128
+ MetaProperty: ["meta", "property"],
129
+ MethodDefinition: [
130
+ "key",
131
+ "value",
132
+ "$decorators"
133
+ ],
134
+ NewExpression: ["callee", "$arguments"],
135
+ ObjectExpression: ["$properties"],
136
+ ObjectPattern: ["$properties"],
137
+ ParenthesizedExpression: ["expression"],
138
+ Program: ["$body"],
139
+ Property: ["key", "value"],
140
+ RestElement: ["argument", "value"],
141
+ ReturnStatement: ["argument"],
142
+ SequenceExpression: ["$expressions"],
143
+ SpreadElement: ["argument"],
144
+ StaticBlock: ["$body"],
145
+ SwitchCase: ["test", "$consequent"],
146
+ SwitchStatement: ["discriminant", "$cases"],
147
+ TaggedTemplateExpression: ["tag", "quasi"],
148
+ TemplateLiteral: ["$quasis", "$expressions"],
149
+ ThrowStatement: ["argument"],
150
+ TryStatement: [
151
+ "block",
152
+ "handler",
153
+ "finalizer"
154
+ ],
155
+ UpdateExpression: ["argument"],
156
+ UnaryExpression: ["argument"],
157
+ VariableDeclaration: ["$declarations"],
158
+ VariableDeclarator: ["id", "init"],
159
+ WhileStatement: ["test", "body"],
160
+ WithStatement: ["object", "body"],
161
+ YieldExpression: ["argument"]
162
+ };
163
+ function visit(ast, config) {
164
+ let shouldBreak = false;
165
+ let shouldSkipChildren = false;
166
+ const traverse = (node, parent, key, index) => {
167
+ if (shouldBreak) return;
168
+ let nodeToReplace;
169
+ let nodeToRemove;
170
+ const context = {
171
+ replace(newNode) {
172
+ nodeToReplace = newNode;
173
+ },
174
+ remove() {
175
+ nodeToRemove = node;
176
+ },
177
+ skip() {
178
+ shouldSkipChildren = true;
179
+ },
180
+ break() {
181
+ shouldBreak = true;
182
+ }
183
+ };
184
+ if (config.enter) {
185
+ config.enter.call(context, node, parent, key, index);
186
+ if (shouldBreak) return;
187
+ }
188
+ if (config[node.type]) {
189
+ config[node.type].call(context, node, parent, key, index);
190
+ if (shouldBreak) return;
191
+ }
192
+ if ((nodeToReplace || nodeToRemove) && parent && key) {
193
+ if (index === void 0) parent[key] = nodeToReplace ?? null;
194
+ else {
195
+ const nodes = parent[key];
196
+ if (nodeToReplace) nodes[index] = nodeToReplace;
197
+ else nodes.splice(index, 1);
198
+ }
199
+ if (nodeToReplace) traverse(nodeToReplace, parent, key, index);
200
+ return;
201
+ }
202
+ if (shouldSkipChildren) shouldSkipChildren = false;
203
+ else if (ESTreeNodeKeys[node.type]) {
204
+ const keys = ESTreeNodeKeys[node.type];
205
+ for (let i = 0; i < keys.length; ++i) {
206
+ const key$1 = keys[i];
207
+ if (key$1[0] === "$") {
208
+ const nodesKey = key$1.slice(1);
209
+ const values = node[nodesKey];
210
+ if (!values) continue;
211
+ let length = values.length;
212
+ for (let j = 0; j < length; ++j) {
213
+ const child = values[j];
214
+ if (!child) continue;
215
+ traverse(child, node, nodesKey, j);
216
+ if (shouldBreak) return;
217
+ if (length > values.length) {
218
+ length--;
219
+ j--;
220
+ }
221
+ }
222
+ } else {
223
+ const nodeKey = key$1;
224
+ const value = node[nodeKey];
225
+ if (!value) continue;
226
+ traverse(value, node, nodeKey);
227
+ if (shouldBreak) return;
228
+ }
229
+ }
230
+ }
231
+ if (config.exit) config.exit.call(context, node, parent, key, index);
232
+ };
233
+ traverse(ast);
234
+ }
235
+
236
+ //#endregion
237
+ //#region src/compiler.ts
238
+ const UPPERCASE_REGEX = /^[A-Z]/;
239
+ const NATIVE_EVENT_REGEX = /^on[a-z]+$/;
240
+ const NON_BLANK_LINE_REGEX = /\S|^[^\S\r\n]+$/;
241
+ const EDGE_SPACE_REGEX = /^\s*[\r\n]\s*|[\r\n]\s*$/g;
242
+ const SPACE_REGEX = /\s+/g;
243
+ const IDENTIFIER_REGEX = /^[a-z_$][\w$]*$/i;
244
+ const SVGTags = new Set([
245
+ "animate",
246
+ "animateMotion",
247
+ "animateTransform",
248
+ "circle",
249
+ "clipPath",
250
+ "cursor",
251
+ "defs",
252
+ "desc",
253
+ "ellipse",
254
+ "feBlend",
255
+ "feColorMatrix",
256
+ "feComponentTransfer",
257
+ "feComposite",
258
+ "feConvolveMatrix",
259
+ "feDiffuseLighting",
260
+ "feDisplacementMap",
261
+ "feDistantLight",
262
+ "feDropShadow",
263
+ "feFlood",
264
+ "feFuncA",
265
+ "feFuncB",
266
+ "feFuncG",
267
+ "feFuncR",
268
+ "feGaussianBlur",
269
+ "feImage",
270
+ "feMerge",
271
+ "feMergeNode",
272
+ "feMorphology",
273
+ "feOffset",
274
+ "fePointLight",
275
+ "feSpecularLighting",
276
+ "feSpotLight",
277
+ "feTile",
278
+ "feTurbulence",
279
+ "filter",
280
+ "font-face",
281
+ "font-face-format",
282
+ "font-face-name",
283
+ "font-face-src",
284
+ "font-face-uri",
285
+ "foreignObject",
286
+ "g",
287
+ "glyph",
288
+ "glyphRef",
289
+ "hkern",
290
+ "line",
291
+ "linearGradient",
292
+ "marker",
293
+ "mask",
294
+ "metadata",
295
+ "missing-glyph",
296
+ "mpath",
297
+ "path",
298
+ "pattern",
299
+ "polygon",
300
+ "polyline",
301
+ "radialGradient",
302
+ "rect",
303
+ "set",
304
+ "stop",
305
+ "svg",
306
+ "switch",
307
+ "symbol",
308
+ "text",
309
+ "textPath",
310
+ "tref",
311
+ "tspan",
312
+ "use",
313
+ "view",
314
+ "vkern"
315
+ ]);
316
+ const MathMLTags = new Set([
317
+ "annotation",
318
+ "annotation-xml",
319
+ "maction",
320
+ "math",
321
+ "menclose",
322
+ "merror",
323
+ "mfenced",
324
+ "mfrac",
325
+ "mi",
326
+ "mmultiscripts",
327
+ "mn",
328
+ "mo",
329
+ "mover",
330
+ "mpadded",
331
+ "mphantom",
332
+ "mprescripts",
333
+ "mroot",
334
+ "mrow",
335
+ "ms",
336
+ "mspace",
337
+ "msqrt",
338
+ "mstyle",
339
+ "msub",
340
+ "msubsup",
341
+ "msup",
342
+ "mtable",
343
+ "mtd",
344
+ "mtext",
345
+ "mtr",
346
+ "munder",
347
+ "munderover",
348
+ "semantics"
349
+ ]);
350
+ const DOMProperties = new Set([
351
+ "allowFullscreen",
352
+ "async",
353
+ "autofocus",
354
+ "autoplay",
355
+ "checked",
356
+ "controls",
357
+ "default",
358
+ "disabled",
359
+ "formNoValidate",
360
+ "hidden",
361
+ "indeterminate",
362
+ "inert",
363
+ "innerHTML",
364
+ "innerText",
365
+ "isMap",
366
+ "loop",
367
+ "multiple",
368
+ "muted",
369
+ "noValidate",
370
+ "nomodule",
371
+ "open",
372
+ "playsInline",
373
+ "readOnly",
374
+ "required",
375
+ "reversed",
376
+ "seamless",
377
+ "selected",
378
+ "textContent",
379
+ "value"
380
+ ]);
381
+ const keywords = new Set([
382
+ "async",
383
+ "await",
384
+ "break",
385
+ "case",
386
+ "catch",
387
+ "class",
388
+ "const",
389
+ "continue",
390
+ "debugger",
391
+ "default",
392
+ "delete",
393
+ "do",
394
+ "else",
395
+ "enum",
396
+ "export",
397
+ "extends",
398
+ "false",
399
+ "finally",
400
+ "for",
401
+ "function",
402
+ "if",
403
+ "import",
404
+ "in",
405
+ "instanceof",
406
+ "let",
407
+ "new",
408
+ "null",
409
+ "return",
410
+ "super",
411
+ "switch",
412
+ "this",
413
+ "throw",
414
+ "true",
415
+ "try",
416
+ "typeof",
417
+ "var",
418
+ "void",
419
+ "while",
420
+ "with",
421
+ "yield"
422
+ ]);
423
+ let currentContext;
424
+ function compile(code, options = {}) {
425
+ let prevFunctionParent;
426
+ let prevHasThisInFunction;
427
+ const ast = parse(code, {
428
+ jsx: true,
429
+ next: true,
430
+ module: true,
431
+ ranges: true,
432
+ loc: true
433
+ });
434
+ currentContext = {
435
+ config: {
436
+ sourcemap: options.sourcemap,
437
+ file: options.file ?? "output.js",
438
+ sourceRoot: options.sourceRoot ?? "",
439
+ modulePath: options.modulePath ?? "@zessjs/core"
440
+ },
441
+ ast,
442
+ events: [],
443
+ eventsCache: /* @__PURE__ */ new Set(),
444
+ generatedFunctions: /* @__PURE__ */ new WeakSet()
445
+ };
446
+ visit(ast, {
447
+ JSXElement(node) {
448
+ currentContext.JSXId ??= 0;
449
+ currentContext.refId ??= 0;
450
+ currentContext.thisId ??= 0;
451
+ const stmts = transformElement(node);
452
+ let expression;
453
+ if (stmts.length === 1) if (stmts[0].type === "VariableDeclaration") expression = stmts[0].declarations[0].init;
454
+ else expression = stmts[0].expression;
455
+ else {
456
+ const id = stmts[0].declarations[0].id;
457
+ const callExpressionPosition = copyPosition(node);
458
+ expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
459
+ }
460
+ currentContext.JSXParent ??= expression;
461
+ this.replace(expression);
462
+ },
463
+ JSXFragment(node) {
464
+ currentContext.JSXId ??= 0;
465
+ currentContext.refId ??= 0;
466
+ currentContext.thisId ??= 0;
467
+ const expression = transformFragment(node.children, copyPosition(node.openingFragment));
468
+ currentContext.JSXParent ??= expression;
469
+ this.replace(expression);
470
+ },
471
+ FunctionDeclaration(node) {
472
+ prevFunctionParent = currentContext.functionParent;
473
+ prevHasThisInFunction = currentContext.hasThisInFunction;
474
+ currentContext.functionParent = node;
475
+ currentContext.hasThisInFunction = false;
476
+ },
477
+ FunctionExpression(node) {
478
+ if (currentContext.generatedFunctions.has(node)) currentContext.generatedFunctions.delete(node);
479
+ else {
480
+ prevFunctionParent = currentContext.functionParent;
481
+ prevHasThisInFunction = currentContext.hasThisInFunction;
482
+ currentContext.functionParent = node;
483
+ currentContext.hasThisInFunction = false;
484
+ }
485
+ },
486
+ ArrowFunctionExpression(node, parent, key) {
487
+ if (parent?.type === "PropertyDefinition" && key === "value") {
488
+ prevFunctionParent = currentContext.functionParent;
489
+ prevHasThisInFunction = currentContext.hasThisInFunction;
490
+ currentContext.functionParent = node;
491
+ currentContext.hasThisInFunction = false;
492
+ }
493
+ },
494
+ ThisExpression(node) {
495
+ const expression = transformThisExpression(node);
496
+ if (expression) this.replace(expression);
497
+ },
498
+ exit(node) {
499
+ if (node === currentContext.functionParent) {
500
+ currentContext.functionParent = prevFunctionParent;
501
+ currentContext.hasThisInFunction = prevHasThisInFunction;
502
+ } else if (node === currentContext.JSXParent) {
503
+ currentContext.JSXId = void 0;
504
+ currentContext.refId = void 0;
505
+ currentContext.thisId = void 0;
506
+ currentContext.JSXParent = void 0;
507
+ }
508
+ }
509
+ });
510
+ injectRuntimeImport(ast);
511
+ const sourceMap = new SourceMapGenerator({
512
+ file: currentContext.config.file,
513
+ sourceRoot: currentContext.config.sourceRoot
514
+ });
515
+ code = generate(ast, { sourceMap });
516
+ let map = sourceMap.toJSON();
517
+ if (currentContext.config.sourcemap) map = merge(currentContext.config.sourcemap, map);
518
+ return {
519
+ code,
520
+ map
521
+ };
522
+ }
523
+ function transformElement(node) {
524
+ const stmts = [];
525
+ const opening = node.openingElement;
526
+ const tagName = transformJSXTagName(opening.name);
527
+ const tagNamePosition = copyPosition(tagName);
528
+ if (tagName.type === "Literal") {
529
+ const value = tagName.value;
530
+ const args = [createLiteral(value, tagNamePosition)];
531
+ const JSXId = createIdentifier(getUniqueId("el", "JSXId"), tagNamePosition);
532
+ const namespace = SVGTags.has(value) ? 1 : MathMLTags.has(value) ? 2 : 0;
533
+ if (namespace) args.push(createLiteral(namespace, tagNamePosition));
534
+ currentContext.shouldImportCreateElement = true;
535
+ stmts.push(createVariableDeclaration(JSXId, createCallExpression(createIdentifier("_$createElement", tagNamePosition), args, tagNamePosition), tagNamePosition), ...transformAttributes(opening.attributes, JSXId), ...transformChildren(node.children, JSXId));
536
+ } else {
537
+ const elementPosition = copyPosition(node);
538
+ currentContext.shouldImportCreateComponent = true;
539
+ stmts.push(createExpressionStatement(createCallExpression(createIdentifier("_$createComponent", elementPosition), [tagName, transformProperties(opening.attributes, node.children, copyPosition(opening))], elementPosition), elementPosition));
540
+ }
541
+ return stmts;
542
+ }
543
+ function transformFragment(children, position, isInComponent) {
544
+ const elements = [];
545
+ let hasDynamicChildren = false;
546
+ let shouldImportUseMemo = false;
547
+ let textContent;
548
+ let textPosition;
549
+ let elementsPosition;
550
+ let childExpression;
551
+ for (let i = 0; i < children.length; ++i) {
552
+ const childNode = children[i];
553
+ if (childNode.type === "JSXText") {
554
+ const text = childNode.value;
555
+ if (isBlankLine(text)) continue;
556
+ if (textContent) textContent = `${textContent}${text}`;
557
+ else {
558
+ textContent = text;
559
+ textPosition = copyPosition(childNode);
560
+ elementsPosition ??= textPosition;
561
+ }
562
+ } else if (isJSXEmptyExpression(childNode)) continue;
563
+ else if (childNode.type === "JSXFragment") children.splice(i--, 1, ...childNode.children);
564
+ else {
565
+ const childPosition = copyPosition(childNode);
566
+ let childElement;
567
+ if (textContent) {
568
+ elements.push(createLiteral(decodeText(textContent), textPosition));
569
+ textContent = textPosition = void 0;
570
+ }
571
+ if (childNode.type === "JSXElement") {
572
+ const stmts = transformElement(childNode);
573
+ if (isInComponent) hasDynamicChildren = true;
574
+ if (stmts.length > 1) {
575
+ const id = stmts[0].declarations[0].id;
576
+ childElement = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, childPosition)], childPosition), childPosition), [], childPosition);
577
+ } else if (stmts[0].type === "VariableDeclaration") childElement = stmts[0].declarations[0].init;
578
+ else childElement = stmts[0].expression;
579
+ } else {
580
+ const expression = childNode.expression;
581
+ if (isInComponent) childExpression ??= expression;
582
+ if (isDynamicExpression(expression, isInComponent)) {
583
+ shouldImportUseMemo = hasDynamicChildren = true;
584
+ childElement = createCallExpression(createIdentifier("_$memo", childPosition), [!isInComponent && isBareIdentifierCall(expression) ? expression.callee : createArrowFunctionExpression(expression, copyPosition(expression))], childPosition);
585
+ } else childElement = expression;
586
+ }
587
+ elements.push(childElement);
588
+ elementsPosition ??= childPosition;
589
+ }
590
+ }
591
+ if (textContent) elements.push(createLiteral(decodeText(textContent), textPosition));
592
+ if (isInComponent) {
593
+ currentContext.prevHasDynamicChildrenInComponent = currentContext.hasDynamicChildrenInComponent;
594
+ currentContext.hasDynamicChildrenInComponent = hasDynamicChildren;
595
+ }
596
+ if (elements.length > 1) {
597
+ if (shouldImportUseMemo) currentContext.shouldImportUseMemo = true;
598
+ return createArrayExpression(elements, elementsPosition);
599
+ }
600
+ if (elements.length === 1) {
601
+ if (!childExpression) {
602
+ if (shouldImportUseMemo) currentContext.shouldImportUseMemo = true;
603
+ return elements[0];
604
+ }
605
+ return childExpression;
606
+ }
607
+ if (!isInComponent) return createArrayExpression(elements, position);
608
+ }
609
+ function transformThisExpression(node) {
610
+ if (!currentContext.JSXParent) return;
611
+ const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
612
+ if (!currentContext.functionParent) {
613
+ if (currentContext.hasThisInProgram) return thisId;
614
+ const { body } = currentContext.ast;
615
+ const programPosition = copyPosition(currentContext.ast);
616
+ currentContext.hasThisInProgram = true;
617
+ for (let i = 0; i < body.length; ++i) {
618
+ if (body[i].type === "ImportDeclaration") continue;
619
+ body.splice(i, 0, createVariableDeclaration(thisId, createThisExpression(programPosition), programPosition));
620
+ break;
621
+ }
622
+ } else if (!currentContext.hasThisInFunction) {
623
+ const functionPosition = copyPosition(currentContext.functionParent);
624
+ const block = currentContext.functionParent.body;
625
+ const declaration = createVariableDeclaration(thisId, createThisExpression(functionPosition), functionPosition);
626
+ currentContext.hasThisInFunction = true;
627
+ if (block.type === "BlockStatement") block.body.unshift(declaration);
628
+ else currentContext.functionParent.body = createBlockStatement([declaration, createReturnStatement(block, copyPosition(block))], functionPosition);
629
+ }
630
+ return thisId;
631
+ }
632
+ function injectRuntimeImport(ast) {
633
+ const specifiers = [];
634
+ const programPosition = copyPosition(ast);
635
+ if (currentContext.shouldImportCreateComponent) specifiers.push(createImportSpecifier("_$createComponent", "createComponent", programPosition));
636
+ if (currentContext.shouldImportCreateElement) specifiers.push(createImportSpecifier("_$createElement", "createElement", programPosition));
637
+ if (currentContext.events.length) {
638
+ specifiers.push(createImportSpecifier("_$delegateEvents", "delegateEvents", programPosition));
639
+ ast.body.push(createExpressionStatement(createCallExpression(createIdentifier("_$delegateEvents", programPosition), [createArrayExpression(currentContext.events, programPosition)], programPosition), programPosition));
640
+ }
641
+ if (currentContext.shouldImportInsert) specifiers.push(createImportSpecifier("_$insert", "insert", programPosition));
642
+ if (currentContext.shouldImportMergeProps) specifiers.push(createImportSpecifier("_$mergeProps", "mergeProps", programPosition));
643
+ if (currentContext.shouldImportSetAttribute) specifiers.push(createImportSpecifier("_$setAttribute", "setAttribute", programPosition));
644
+ if (currentContext.shouldImportSetAttributeNS) specifiers.push(createImportSpecifier("_$setAttributeNS", "setAttributeNS", programPosition));
645
+ if (currentContext.shouldImportSetClassName) specifiers.push(createImportSpecifier("_$className", "setClassName", programPosition));
646
+ if (currentContext.shouldImportSetStyle) specifiers.push(createImportSpecifier("_$style", "setStyle", programPosition));
647
+ if (currentContext.shouldImportSpread) specifiers.push(createImportSpecifier("_$spread", "spread", programPosition));
648
+ if (currentContext.shouldImportUse) specifiers.push(createImportSpecifier("_$use", "use", programPosition));
649
+ if (currentContext.shouldImportUseEffect) specifiers.push(createImportSpecifier("_$effect", "useRenderEffect", programPosition));
650
+ if (currentContext.shouldImportUseMemo) specifiers.push(createImportSpecifier("_$memo", "useMemo", programPosition));
651
+ if (specifiers.length) ast.body.unshift({
652
+ type: "ImportDeclaration",
653
+ specifiers,
654
+ attributes: [],
655
+ source: createLiteral(currentContext.config.modulePath, programPosition),
656
+ ...programPosition
657
+ });
658
+ }
659
+ function transformJSXTagName(node) {
660
+ const tagNamePosition = copyPosition(node);
661
+ if (node.type === "JSXNamespacedName") {
662
+ const namespace = node.namespace;
663
+ return createLiteral(`${namespace.name}:${node.name.name}`, tagNamePosition);
664
+ }
665
+ let object;
666
+ let expression;
667
+ if (node.type === "JSXMemberExpression") {
668
+ object = expression = tagNamePosition;
669
+ do {
670
+ const isValidPropertyName = isValidIdentifier(node.property.name);
671
+ const propertyPosition = copyPosition(node.property);
672
+ object.type = "MemberExpression";
673
+ object.property = isValidPropertyName ? createIdentifier(node.property.name, propertyPosition) : createLiteral(node.property.name, propertyPosition);
674
+ object.computed = !isValidPropertyName;
675
+ object.object = object = copyPosition(node.object);
676
+ node = node.object;
677
+ } while (node.type === "JSXMemberExpression");
678
+ }
679
+ const tagName = node;
680
+ if (tagName.name === "this") {
681
+ const id = transformThisExpression(tagName);
682
+ if (expression) {
683
+ object.type = "Identifier";
684
+ object.name = id.name;
685
+ } else expression = id;
686
+ } else if (expression || UPPERCASE_REGEX.test(tagName.name)) if (expression) {
687
+ object.type = "Identifier";
688
+ object.name = tagName.name;
689
+ } else expression = createIdentifier(tagName.name, tagNamePosition);
690
+ else expression = createLiteral(tagName.name, tagNamePosition);
691
+ return expression;
692
+ }
693
+ function transformAttributes(attrs, id) {
694
+ const stmts = [];
695
+ const attributeNameCache = new Set(["children"]);
696
+ let shouldImportSpread = false;
697
+ let shouldImportMergeProps = false;
698
+ let properties = [];
699
+ let shouldImportSetStyle;
700
+ let shouldImportSetClassName;
701
+ let shouldImportSetAttribute;
702
+ let shouldImportSetAttributeNS;
703
+ let shouldImportUseEffect;
704
+ let ref;
705
+ let events;
706
+ let objectPosition;
707
+ let spreadArgs;
708
+ let spreadArgsPosition;
709
+ let effect;
710
+ let effectProperty;
711
+ let effectStmts;
712
+ let effectParameter;
713
+ let effectFunction;
714
+ let effectPosition;
715
+ let dynamicCount;
716
+ for (let i = 0; i < attrs.length; ++i) {
717
+ let expression;
718
+ const attr = attrs[i];
719
+ const attributePosition = copyPosition(attr);
720
+ if (attr.type === "JSXAttribute") {
721
+ const attributeName = getAttributeName(attr.name);
722
+ if (attributeNameCache.has(attributeName)) continue;
723
+ let value;
724
+ let isDynamicValue = false;
725
+ const attributeNamePosition = copyPosition(attr.name);
726
+ attributeNameCache.add(attributeName);
727
+ if (!attr.value) value = createLiteral(true, attributeNamePosition);
728
+ else if (attr.value.type === "Literal") value = attr.value;
729
+ else if (attr.value.type === "JSXExpressionContainer") {
730
+ expression = attr.value.expression;
731
+ if (attributeName === "ref") {
732
+ if (matchesType(expression, isRightValue)) {
733
+ const refId = createIdentifier(getUniqueId("ref", "refId"), attributeNamePosition);
734
+ const binaryExpression = createBinaryExpression(createUnaryExpression(refId, attributePosition), createLiteral("function", attributePosition), "===", attributePosition);
735
+ const callExpression = createCallExpression(createIdentifier("_$use", attributePosition), [refId, id], attributePosition);
736
+ currentContext.shouldImportUse = true;
737
+ ref = [createVariableDeclaration(refId, expression, attributePosition), createExpressionStatement(isLeftValue(expression) ? createConditionalExpression(binaryExpression, callExpression, createAssignmentExpression(expression, id, attributePosition), attributePosition) : createLogicalExpression(binaryExpression, callExpression, attributePosition), attributePosition)];
738
+ continue;
739
+ }
740
+ if (isFunctionExpression(expression)) {
741
+ currentContext.shouldImportUse = true;
742
+ ref = [createExpressionStatement(createCallExpression(createIdentifier("_$use", attributePosition), [expression, id], attributePosition), attributePosition)];
743
+ continue;
744
+ }
745
+ } else if (!shouldImportSpread && attributeName.startsWith("on") && matchesType(expression, isListener)) {
746
+ let eventName;
747
+ if (NATIVE_EVENT_REGEX.test(attributeName)) eventName = attributeName;
748
+ else {
749
+ eventName = attributeName.slice(2).toLowerCase();
750
+ if (eventName === "doubleclick") eventName = "dblclick";
751
+ if (!currentContext.eventsCache.has(eventName)) {
752
+ currentContext.eventsCache.add(eventName);
753
+ currentContext.events.push(createLiteral(eventName, attributeNamePosition));
754
+ }
755
+ eventName = `$$${eventName}`;
756
+ }
757
+ (events ??= []).push(createExpressionStatement(createAssignmentExpression(createMemberExpression(id, createIdentifier(eventName, attributeNamePosition), false, attributeNamePosition), expression, attributePosition), attributePosition));
758
+ continue;
759
+ }
760
+ value = expression;
761
+ isDynamicValue = isDynamicExpression(value);
762
+ }
763
+ const isValidPropertyName = isValidIdentifier(attributeName);
764
+ const property = createProperty(isValidPropertyName ? createIdentifier(attributeName, attributeNamePosition) : createLiteral(attributeName, attributeNamePosition), isDynamicValue ? createFunctionExpression([createReturnStatement(value, attributePosition)]) : value, isDynamicValue ? "get" : "init", !isValidPropertyName, false, attributePosition);
765
+ objectPosition ??= attributePosition;
766
+ properties.push(property);
767
+ if (shouldImportSpread) continue;
768
+ let styleOrClassArgs;
769
+ if (attributeName === "style") if (matchesType(value, isStylesheet)) {
770
+ shouldImportSetStyle = true;
771
+ expression = createCallExpression(createIdentifier("_$style", attributeNamePosition), styleOrClassArgs = [id, value], attributePosition);
772
+ } else expression = createAssignmentExpression(createMemberExpression(createMemberExpression(id, createIdentifier(attributeName, attributeNamePosition), false, attributeNamePosition), createIdentifier("cssText", attributeNamePosition), false, attributeNamePosition), value, attributePosition);
773
+ else if (attributeName === "class" || attributeName === "className") if (matchesType(value, isStylesheet)) {
774
+ shouldImportSetClassName = true;
775
+ expression = createCallExpression(createIdentifier("_$className", attributeNamePosition), styleOrClassArgs = [id, value], attributePosition);
776
+ } else {
777
+ shouldImportSetAttribute = true;
778
+ expression = createCallExpression(createIdentifier("_$setAttribute", attributeNamePosition), [
779
+ id,
780
+ createLiteral("class", attributeNamePosition),
781
+ value
782
+ ], attributePosition);
783
+ }
784
+ else if (DOMProperties.has(attributeName) || attributeName.startsWith("on")) expression = createAssignmentExpression(createMemberExpression(id, createIdentifier(attributeName, attributeNamePosition), false, attributeNamePosition), value, attributePosition);
785
+ else {
786
+ let method;
787
+ let attributeArgs;
788
+ const prefix = attributeName.split(":")[0];
789
+ const namespace = prefix === "xlink" ? 3 : prefix === "xml" ? 4 : 0;
790
+ const name = createLiteral(attributeName, attributeNamePosition);
791
+ if (namespace) {
792
+ method = "_$setAttributeNS";
793
+ shouldImportSetAttributeNS = true;
794
+ attributeArgs = [
795
+ id,
796
+ createLiteral(namespace, attributeNamePosition),
797
+ name,
798
+ value
799
+ ];
800
+ } else {
801
+ method = "_$setAttribute";
802
+ shouldImportSetAttribute = true;
803
+ attributeArgs = [
804
+ id,
805
+ name,
806
+ value
807
+ ];
808
+ }
809
+ expression = createCallExpression(createIdentifier(method, attributeNamePosition), attributeArgs, attributePosition);
810
+ }
811
+ if (isDynamicValue) {
812
+ if (effect) {
813
+ let valueId;
814
+ let effectKey;
815
+ let member;
816
+ let object;
817
+ let assignment;
818
+ let declaration;
819
+ if (effect.arguments.length === 1) {
820
+ let hasEffectParameter;
821
+ let effectValue;
822
+ let effectCall = effectFunction.body;
823
+ effectKey = effectProperty.key;
824
+ if (effectParameter) hasEffectParameter = true;
825
+ else {
826
+ hasEffectParameter = false;
827
+ effectParameter = createIdentifier(getUniqueId("p"), effectPosition);
828
+ }
829
+ dynamicCount = 0;
830
+ valueId = createIdentifier(getUniqueId("v", ++dynamicCount), effectPosition);
831
+ member = createMemberExpression(effectParameter, effectKey, effectProperty.computed, effectPosition);
832
+ if (hasEffectParameter) {
833
+ const args = effectCall.arguments;
834
+ effectValue = args[1];
835
+ args[1] = valueId;
836
+ args[2] = member;
837
+ effectCall = createAssignmentExpression(member, effectCall, effectPosition);
838
+ } else {
839
+ assignment = createAssignmentExpression(member, valueId, effectPosition);
840
+ if (effectCall.type === "CallExpression") {
841
+ const index = effectCall.arguments.length - 1;
842
+ effectValue = effectCall.arguments[index];
843
+ effectCall.arguments[index] = assignment;
844
+ } else {
845
+ effectValue = effectCall.right;
846
+ effectCall.right = assignment;
847
+ }
848
+ effectCall = createLogicalExpression(createBinaryExpression(valueId, member, "!==", effectPosition), effectCall, effectPosition);
849
+ effectFunction.params.push(effectParameter);
850
+ }
851
+ declaration = createVariableDeclaration(valueId, effectValue, effectPosition);
852
+ effectStmts = [declaration, createExpressionStatement(effectCall, effectPosition)];
853
+ effectFunction.body = createBlockStatement(effectStmts, effectPosition);
854
+ object = createObjectExpression([createProperty(effectKey, createIdentifier("undefined", effectPosition), "init", effectProperty.computed, false, effectPosition)], effectPosition);
855
+ effect.arguments.push(object);
856
+ } else {
857
+ object = effect.arguments[1];
858
+ declaration = effectStmts[0];
859
+ }
860
+ effectKey = property.key;
861
+ valueId = createIdentifier(getUniqueId("v", ++dynamicCount), attributePosition);
862
+ member = createMemberExpression(effectParameter, effectKey, property.computed, attributePosition);
863
+ if (styleOrClassArgs) {
864
+ styleOrClassArgs[1] = valueId;
865
+ styleOrClassArgs.push(member);
866
+ expression = createAssignmentExpression(member, expression, attributePosition);
867
+ } else {
868
+ assignment = createAssignmentExpression(member, valueId, attributePosition);
869
+ if (expression.type === "CallExpression") expression.arguments[expression.arguments.length - 1] = assignment;
870
+ else expression.right = assignment;
871
+ expression = createLogicalExpression(createBinaryExpression(valueId, member, "!==", attributePosition), expression, attributePosition);
872
+ }
873
+ object.properties.push(createProperty(effectKey, createIdentifier("undefined", attributePosition), "init", property.computed, false, attributePosition));
874
+ declaration.declarations.push(createVariableDeclarator(valueId, value, attributePosition));
875
+ effectStmts.push(createExpressionStatement(expression, attributePosition));
876
+ continue;
877
+ }
878
+ effectProperty = property;
879
+ effectPosition = attributePosition;
880
+ effectFunction = createArrowFunctionExpression(expression, effectPosition);
881
+ if (styleOrClassArgs) {
882
+ effectParameter = createIdentifier(getUniqueId("p"), effectPosition);
883
+ effectFunction.params.push(effectParameter);
884
+ styleOrClassArgs.push(effectParameter);
885
+ }
886
+ shouldImportUseEffect = true;
887
+ expression = effect = createCallExpression(createIdentifier("_$effect", effectPosition), [effectFunction], effectPosition);
888
+ }
889
+ } else {
890
+ let { argument } = attr;
891
+ spreadArgs ??= [id];
892
+ if (properties.length) {
893
+ spreadArgs.push(createObjectExpression(properties, objectPosition));
894
+ properties = [];
895
+ spreadArgsPosition ??= objectPosition;
896
+ objectPosition = void 0;
897
+ }
898
+ if (isDynamicExpression(argument)) {
899
+ shouldImportMergeProps = true;
900
+ argument = isBareIdentifierCall(argument) ? argument.callee : createArrowFunctionExpression(argument, copyPosition(argument));
901
+ }
902
+ spreadArgs.push(argument);
903
+ if (shouldImportSpread) continue;
904
+ currentContext.shouldImportSpread = shouldImportSpread = true;
905
+ spreadArgsPosition ??= attributePosition;
906
+ shouldImportSetStyle = false;
907
+ shouldImportSetClassName = false;
908
+ shouldImportSetAttribute = false;
909
+ shouldImportSetAttributeNS = false;
910
+ shouldImportUseEffect = false;
911
+ stmts.length = 0;
912
+ expression = createCallExpression(createIdentifier("_$spread", attributePosition), spreadArgs, attributePosition);
913
+ }
914
+ stmts.push(createExpressionStatement(expression, attributePosition));
915
+ }
916
+ if (spreadArgs) {
917
+ if (properties.length) spreadArgs.push(createObjectExpression(properties, objectPosition));
918
+ if (shouldImportMergeProps || spreadArgs.length > 2) {
919
+ currentContext.shouldImportMergeProps = true;
920
+ spreadArgs.splice(1, spreadArgs.length - 1, createCallExpression(createIdentifier("_$mergeProps", spreadArgsPosition), spreadArgs.slice(1), spreadArgsPosition));
921
+ }
922
+ } else {
923
+ if (shouldImportSetStyle) currentContext.shouldImportSetStyle = true;
924
+ if (shouldImportSetClassName) currentContext.shouldImportSetClassName = true;
925
+ if (shouldImportSetAttribute) currentContext.shouldImportSetAttribute = true;
926
+ if (shouldImportSetAttributeNS) currentContext.shouldImportSetAttributeNS = true;
927
+ if (shouldImportUseEffect) {
928
+ currentContext.shouldImportUseEffect = true;
929
+ if (effectStmts) effectStmts.push(createReturnStatement(effectParameter, effectPosition));
930
+ }
931
+ }
932
+ if (events) stmts.unshift(...events);
933
+ if (ref) stmts.unshift(...ref);
934
+ return stmts;
935
+ }
936
+ function transformChildren(children, id) {
937
+ const stmts = [];
938
+ let elements = [];
939
+ let textContent;
940
+ let textPosition;
941
+ let elementsPosition;
942
+ for (let i = 0; i < children.length; ++i) {
943
+ const childNode = children[i];
944
+ if (childNode.type === "JSXText") {
945
+ const text = childNode.value;
946
+ if (isBlankLine(text)) continue;
947
+ if (textContent) textContent = `${textContent}${text}`;
948
+ else {
949
+ textContent = text;
950
+ textPosition = copyPosition(childNode);
951
+ elementsPosition ??= textPosition;
952
+ }
953
+ } else if (isJSXEmptyExpression(childNode)) continue;
954
+ else if (childNode.type === "JSXFragment") children.splice(i--, 1, ...childNode.children);
955
+ else {
956
+ const childPosition = copyPosition(childNode);
957
+ let expression;
958
+ if (textContent) {
959
+ elements.push(createLiteral(decodeText(textContent), textPosition));
960
+ textContent = textPosition = void 0;
961
+ }
962
+ if (childNode.type === "JSXElement") {
963
+ const childStmts = transformElement(childNode);
964
+ if (childStmts.length > 1) {
965
+ const stmt = childStmts[0];
966
+ elements.push(stmt.declarations[0].id);
967
+ elementsPosition ??= childPosition;
968
+ stmts.push(...childStmts);
969
+ continue;
970
+ } else if (childStmts[0].type === "VariableDeclaration") {
971
+ elements.push(childStmts[0].declarations[0].init);
972
+ elementsPosition ??= childPosition;
973
+ continue;
974
+ } else expression = childStmts[0].expression;
975
+ } else {
976
+ expression = childNode.expression;
977
+ if (isBareIdentifierCall(expression)) expression = expression.callee;
978
+ else if (isDynamicExpression(expression)) expression = createArrowFunctionExpression(expression, copyPosition(expression));
979
+ }
980
+ if (elements.length) {
981
+ stmts.push(createExpressionStatement(createCallExpression(createMemberExpression(id, createIdentifier("append", elementsPosition), false, elementsPosition), elements, elementsPosition), elementsPosition));
982
+ elements = [];
983
+ elementsPosition = void 0;
984
+ }
985
+ currentContext.shouldImportInsert = true;
986
+ stmts.push(createExpressionStatement(createCallExpression(createIdentifier("_$insert", childPosition), [id, expression], childPosition), childPosition));
987
+ }
988
+ }
989
+ if (textContent) elements.push(createLiteral(decodeText(textContent), textPosition));
990
+ if (elements.length) stmts.push(createExpressionStatement(createCallExpression(createMemberExpression(id, createIdentifier("append", elementsPosition), false, elementsPosition), elements, elementsPosition), elementsPosition));
991
+ return stmts;
992
+ }
993
+ function transformProperties(attrs, children, position) {
994
+ const args = [];
995
+ const propertyNameCache = new Set(["children"]);
996
+ let shouldImportMergeProps = false;
997
+ let properties = [];
998
+ let objectPosition;
999
+ let argsPosition;
1000
+ for (let i = 0; i < attrs.length; ++i) {
1001
+ const attr = attrs[i];
1002
+ const propertyPosition = copyPosition(attr);
1003
+ if (attr.type === "JSXAttribute") {
1004
+ const propertyName = getAttributeName(attr.name);
1005
+ if (propertyNameCache.has(propertyName)) continue;
1006
+ let value;
1007
+ let isDynamicValue = false;
1008
+ const propertyNamePosition = copyPosition(attr.name);
1009
+ propertyNameCache.add(propertyName);
1010
+ if (!attr.value) value = createLiteral(true, propertyNamePosition);
1011
+ else if (attr.value.type === "Literal") value = attr.value;
1012
+ else if (attr.value.type === "JSXExpressionContainer") {
1013
+ const expression = attr.value.expression;
1014
+ if (propertyName === "ref") {
1015
+ if (matchesType(expression, isRightValue)) {
1016
+ const refId = createIdentifier(getUniqueId("ref", "refId"), propertyNamePosition);
1017
+ const argument = createIdentifier(getUniqueId("r"), copyPosition(expression));
1018
+ const binaryExpression = createBinaryExpression(createUnaryExpression(refId, propertyPosition), createLiteral("function", propertyPosition), "===", propertyPosition);
1019
+ const callExpression = createCallExpression(refId, [argument], propertyPosition);
1020
+ objectPosition ??= propertyPosition;
1021
+ properties.push(createProperty(createIdentifier(propertyName, propertyNamePosition), createFunctionExpression([createVariableDeclaration(refId, expression, propertyPosition), createExpressionStatement(isLeftValue(expression) ? createConditionalExpression(binaryExpression, callExpression, createAssignmentExpression(expression, argument, propertyPosition), propertyPosition) : createLogicalExpression(binaryExpression, callExpression, propertyPosition), propertyPosition)], argument), "init", false, true, propertyPosition));
1022
+ } else if (isFunctionExpression(expression)) {
1023
+ objectPosition ??= propertyPosition;
1024
+ properties.push(createProperty(createIdentifier(propertyName, propertyNamePosition), expression, "init", false, false, propertyPosition));
1025
+ }
1026
+ continue;
1027
+ }
1028
+ value = expression;
1029
+ isDynamicValue = isDynamicExpression(value, true);
1030
+ }
1031
+ const isValidPropertyName = isValidIdentifier(propertyName);
1032
+ objectPosition ??= propertyPosition;
1033
+ properties.push(createProperty(isValidPropertyName ? createIdentifier(propertyName, propertyNamePosition) : createLiteral(propertyName, propertyNamePosition), isDynamicValue ? createFunctionExpression([createReturnStatement(value, propertyPosition)]) : value, isDynamicValue ? "get" : "init", !isValidPropertyName, false, propertyPosition));
1034
+ } else {
1035
+ let { argument } = attr;
1036
+ if (properties.length) {
1037
+ args.push(createObjectExpression(properties, objectPosition));
1038
+ properties = [];
1039
+ argsPosition ??= objectPosition;
1040
+ objectPosition = void 0;
1041
+ }
1042
+ if (isDynamicExpression(argument)) {
1043
+ shouldImportMergeProps = true;
1044
+ argument = isBareIdentifierCall(argument) ? argument.callee : createArrowFunctionExpression(argument, copyPosition(argument));
1045
+ }
1046
+ args.push(argument);
1047
+ argsPosition ??= propertyPosition;
1048
+ }
1049
+ }
1050
+ if (children.length) {
1051
+ const childrenPosition = copyPosition(children[0]);
1052
+ const childrenExpression = transformFragment(children, childrenPosition, true);
1053
+ if (childrenExpression) {
1054
+ const hasDynamicChildren = currentContext.hasDynamicChildrenInComponent;
1055
+ properties.push(createProperty(createIdentifier("children", childrenPosition), hasDynamicChildren ? createFunctionExpression([createReturnStatement(childrenExpression, childrenPosition)]) : childrenExpression, hasDynamicChildren ? "get" : "init", false, false, childrenPosition));
1056
+ }
1057
+ currentContext.hasDynamicChildrenInComponent = currentContext.prevHasDynamicChildrenInComponent;
1058
+ }
1059
+ if (properties.length) {
1060
+ args.push(createObjectExpression(properties, objectPosition));
1061
+ argsPosition ??= objectPosition;
1062
+ }
1063
+ if (shouldImportMergeProps || args.length > 1) {
1064
+ currentContext.shouldImportMergeProps = true;
1065
+ return createCallExpression(createIdentifier("_$mergeProps", argsPosition), args, argsPosition);
1066
+ }
1067
+ if (args.length === 1) return args[0];
1068
+ return createObjectExpression(properties, position);
1069
+ }
1070
+ function getAttributeName(node) {
1071
+ if (node.type === "JSXIdentifier") return node.name;
1072
+ return `${node.namespace.name}:${node.name.name}`;
1073
+ }
1074
+ function copyPosition(node) {
1075
+ return {
1076
+ start: node.start,
1077
+ end: node.end,
1078
+ range: node.range,
1079
+ loc: node.loc
1080
+ };
1081
+ }
1082
+ function createExpressionStatement(expression, position) {
1083
+ return {
1084
+ type: "ExpressionStatement",
1085
+ expression,
1086
+ ...position
1087
+ };
1088
+ }
1089
+ function createVariableDeclaration(id, init, position) {
1090
+ return {
1091
+ type: "VariableDeclaration",
1092
+ kind: "const",
1093
+ declarations: [createVariableDeclarator(id, init, position)],
1094
+ ...position
1095
+ };
1096
+ }
1097
+ function createReturnStatement(argument, position) {
1098
+ return {
1099
+ type: "ReturnStatement",
1100
+ argument,
1101
+ ...position
1102
+ };
1103
+ }
1104
+ function createBlockStatement(body, position) {
1105
+ return {
1106
+ type: "BlockStatement",
1107
+ body,
1108
+ ...position
1109
+ };
1110
+ }
1111
+ function createArrowFunctionExpression(body, position) {
1112
+ return {
1113
+ type: "ArrowFunctionExpression",
1114
+ params: [],
1115
+ body,
1116
+ async: false,
1117
+ generator: false,
1118
+ expression: body.type !== "BlockStatement",
1119
+ ...position
1120
+ };
1121
+ }
1122
+ function createFunctionExpression(body, parameter) {
1123
+ const functionPosition = copyPosition(body[0]);
1124
+ const FunctionExpression = {
1125
+ type: "FunctionExpression",
1126
+ params: parameter ? [parameter] : [],
1127
+ body: createBlockStatement(body, functionPosition),
1128
+ async: false,
1129
+ generator: false,
1130
+ id: null,
1131
+ ...functionPosition
1132
+ };
1133
+ currentContext.generatedFunctions.add(FunctionExpression);
1134
+ return FunctionExpression;
1135
+ }
1136
+ function createCallExpression(callee, args, position) {
1137
+ return {
1138
+ type: "CallExpression",
1139
+ callee,
1140
+ arguments: args,
1141
+ optional: false,
1142
+ ...position
1143
+ };
1144
+ }
1145
+ function createMemberExpression(object, property, computed, position) {
1146
+ return {
1147
+ type: "MemberExpression",
1148
+ object,
1149
+ property,
1150
+ computed,
1151
+ optional: false,
1152
+ ...position
1153
+ };
1154
+ }
1155
+ function createProperty(key, value, kind, computed, method, position) {
1156
+ return {
1157
+ type: "Property",
1158
+ key,
1159
+ value,
1160
+ kind,
1161
+ computed,
1162
+ method,
1163
+ shorthand: false,
1164
+ ...position
1165
+ };
1166
+ }
1167
+ function createImportSpecifier(local, imported, position) {
1168
+ return {
1169
+ type: "ImportSpecifier",
1170
+ local: createIdentifier(local, position),
1171
+ imported: createIdentifier(imported, position),
1172
+ ...position
1173
+ };
1174
+ }
1175
+ function createObjectExpression(properties, position) {
1176
+ return {
1177
+ type: "ObjectExpression",
1178
+ properties,
1179
+ ...position
1180
+ };
1181
+ }
1182
+ function createArrayExpression(elements, position) {
1183
+ return {
1184
+ type: "ArrayExpression",
1185
+ elements,
1186
+ ...position
1187
+ };
1188
+ }
1189
+ function createAssignmentExpression(left, right, position) {
1190
+ return {
1191
+ type: "AssignmentExpression",
1192
+ left,
1193
+ right,
1194
+ operator: "=",
1195
+ ...position
1196
+ };
1197
+ }
1198
+ function createBinaryExpression(left, right, operator, position) {
1199
+ return {
1200
+ type: "BinaryExpression",
1201
+ left,
1202
+ right,
1203
+ operator,
1204
+ ...position
1205
+ };
1206
+ }
1207
+ function createLogicalExpression(left, right, position) {
1208
+ return {
1209
+ type: "LogicalExpression",
1210
+ left,
1211
+ right,
1212
+ operator: "&&",
1213
+ ...position
1214
+ };
1215
+ }
1216
+ function createConditionalExpression(test, consequent, alternate, position) {
1217
+ return {
1218
+ type: "ConditionalExpression",
1219
+ test,
1220
+ consequent,
1221
+ alternate,
1222
+ ...position
1223
+ };
1224
+ }
1225
+ function createUnaryExpression(argument, position) {
1226
+ return {
1227
+ type: "UnaryExpression",
1228
+ operator: "typeof",
1229
+ argument,
1230
+ prefix: true,
1231
+ ...position
1232
+ };
1233
+ }
1234
+ function createVariableDeclarator(id, init, position) {
1235
+ return {
1236
+ type: "VariableDeclarator",
1237
+ id,
1238
+ init,
1239
+ ...position
1240
+ };
1241
+ }
1242
+ function createIdentifier(name, position) {
1243
+ return {
1244
+ type: "Identifier",
1245
+ name,
1246
+ ...position
1247
+ };
1248
+ }
1249
+ function createLiteral(value, position) {
1250
+ return {
1251
+ type: "Literal",
1252
+ value,
1253
+ raw: JSON.stringify(value),
1254
+ ...position
1255
+ };
1256
+ }
1257
+ function createThisExpression(position) {
1258
+ return {
1259
+ type: "ThisExpression",
1260
+ ...position
1261
+ };
1262
+ }
1263
+ function getUniqueId(prefix, key) {
1264
+ let id;
1265
+ if (currentContext[key] != null && currentContext[key]++) id = currentContext[key];
1266
+ else if (key > 1) id = key;
1267
+ else id = "";
1268
+ return `_${prefix}$${id}`;
1269
+ }
1270
+ function isBlankLine(text) {
1271
+ return !NON_BLANK_LINE_REGEX.test(text);
1272
+ }
1273
+ function decodeText(text) {
1274
+ return decodeHTML(text.replaceAll(EDGE_SPACE_REGEX, "").replaceAll(SPACE_REGEX, " "));
1275
+ }
1276
+ function isValidIdentifier(name) {
1277
+ return IDENTIFIER_REGEX.test(name) && !keywords.has(name);
1278
+ }
1279
+ function matchesType(node, predicate) {
1280
+ if (predicate(node)) return true;
1281
+ if (node.type === "LogicalExpression") return predicate(node.left) || predicate(node.right);
1282
+ if (node.type === "ConditionalExpression") return predicate(node.consequent) || predicate(node.alternate);
1283
+ if (node.type === "AssignmentExpression") return predicate(node.right);
1284
+ if (node.type === "AwaitExpression") return predicate(node.argument);
1285
+ if (node.type === "YieldExpression" && node.argument) return predicate(node.argument);
1286
+ if (node.type === "SequenceExpression") return predicate(node.expressions.at(-1));
1287
+ return false;
1288
+ }
1289
+ function isListener(node) {
1290
+ return isFunctionExpression(node) || isRightValue(node);
1291
+ }
1292
+ function isStylesheet(node) {
1293
+ return node.type === "ObjectExpression" || isRightValue(node);
1294
+ }
1295
+ function isRightValue(node) {
1296
+ return isLeftValue(node) || node.type === "CallExpression" || node.type === "ChainExpression" || node.type === "JSXElement" || node.type === "JSXFragment" || node.type === "ThisExpression" || node.type === "NewExpression" || node.type === "TaggedTemplateExpression" || node.type === "ImportExpression" || node.type === "MetaProperty";
1297
+ }
1298
+ function isLeftValue(node) {
1299
+ return node.type === "Identifier" || node.type === "MemberExpression";
1300
+ }
1301
+ function isFunctionExpression(node) {
1302
+ return node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression";
1303
+ }
1304
+ function isBareIdentifierCall(node) {
1305
+ return node.type === "CallExpression" && node.callee.type === "Identifier" && node.arguments.length === 0;
1306
+ }
1307
+ function isJSXEmptyExpression(node) {
1308
+ return node.type === "JSXEmptyExpression" || (node.type === "JSXExpressionContainer" || node.type === "JSXSpreadChild") && node.expression.type === "JSXEmptyExpression";
1309
+ }
1310
+ function isDynamicExpression(node, checkTags) {
1311
+ let isDynamic = false;
1312
+ visit(node, {
1313
+ ArrowFunctionExpression() {
1314
+ this.skip();
1315
+ },
1316
+ FunctionExpression() {
1317
+ this.skip();
1318
+ },
1319
+ CallExpression() {
1320
+ isDynamic = true;
1321
+ this.break();
1322
+ },
1323
+ MemberExpression() {
1324
+ isDynamic = true;
1325
+ this.break();
1326
+ },
1327
+ ChainExpression() {
1328
+ isDynamic = true;
1329
+ this.break();
1330
+ },
1331
+ JSXElement() {
1332
+ if (checkTags) {
1333
+ isDynamic = true;
1334
+ this.break();
1335
+ }
1336
+ },
1337
+ JSXFragment(node$1) {
1338
+ if (checkTags && node$1.children.length) {
1339
+ isDynamic = true;
1340
+ this.break();
1341
+ }
1342
+ }
1343
+ });
1344
+ return isDynamic;
1345
+ }
1346
+
1347
+ //#endregion
1348
+ export { compile };