@zeus-js/compiler 0.1.0-beta.8 → 0.1.0-beta.9
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.
- package/dist/compiler.cjs.js +156 -240
- package/dist/compiler.cjs.prod.js +156 -240
- package/dist/compiler.d.ts +1 -0
- package/dist/compiler.esm-browser.js +12845 -504
- package/dist/compiler.esm-browser.prod.js +10 -5
- package/dist/compiler.esm-bundler.js +188 -278
- package/dist/compiler.global.js +12870 -505
- package/dist/compiler.global.prod.js +10 -5
- package/package.json +4 -2
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* compiler v0.1.0-beta.
|
|
2
|
+
* compiler v0.1.0-beta.9
|
|
3
3
|
* (c) 2026 baicie
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
**/
|
|
6
6
|
import { declare } from "@babel/helper-plugin-utils";
|
|
7
7
|
import { extend } from "@zeus-js/shared";
|
|
8
8
|
import * as t from "@babel/types";
|
|
9
|
+
import { parseExpression } from "@babel/parser";
|
|
10
|
+
import { attrBindingIR, componentIR, dynamicTextIR, elementIR, eventBindingIR, expressionIR, forIR, fragmentIR, hostIR, id, identifierIR, propBindingIR, ref, refBindingIR, showIR, slotIR, staticAttrIR, textIR } from "@zeus-js/compiler-shared";
|
|
11
|
+
export * from "@zeus-js/compiler-shared";
|
|
9
12
|
//#region packages/core/compiler/src/codegen/support/imports.ts
|
|
10
13
|
/**
|
|
11
14
|
* Runtime helpers registration and program injection.
|
|
@@ -239,6 +242,37 @@ function createProgramVisitor(config) {
|
|
|
239
242
|
};
|
|
240
243
|
}
|
|
241
244
|
//#endregion
|
|
245
|
+
//#region packages/core/compiler/src/adapters/babel/expression.ts
|
|
246
|
+
function lowerExpressionIR(path) {
|
|
247
|
+
return expressionIR(path.toString(), sourceSpanFromBabelNode(path.node));
|
|
248
|
+
}
|
|
249
|
+
function expressionIRFromCode(code, node) {
|
|
250
|
+
return expressionIR(code, sourceSpanFromBabelNode(node));
|
|
251
|
+
}
|
|
252
|
+
function parseExpressionIR(expression) {
|
|
253
|
+
return parseExpression(expression.code, {
|
|
254
|
+
sourceType: "module",
|
|
255
|
+
plugins: ["typescript", "jsx"]
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
function sourceSpanFromBabelNode(node) {
|
|
259
|
+
if (!(node === null || node === void 0 ? void 0 : node.loc)) return void 0;
|
|
260
|
+
return {
|
|
261
|
+
start: sourcePosition(node.loc.start.line, node.loc.start.column, node.start),
|
|
262
|
+
end: sourcePosition(node.loc.end.line, node.loc.end.column, node.end)
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function sourcePosition(line, column, offset) {
|
|
266
|
+
return typeof offset === "number" ? {
|
|
267
|
+
line,
|
|
268
|
+
column,
|
|
269
|
+
offset
|
|
270
|
+
} : {
|
|
271
|
+
line,
|
|
272
|
+
column
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
242
276
|
//#region packages/core/compiler/src/codegen/dom/emitBinding.ts
|
|
243
277
|
function emitBindings(node, context) {
|
|
244
278
|
const statements = [];
|
|
@@ -269,7 +303,7 @@ function emitRawTextValue(children) {
|
|
|
269
303
|
const values = children.flatMap((child) => {
|
|
270
304
|
switch (child.kind) {
|
|
271
305
|
case "Text": return [t.stringLiteral(child.value)];
|
|
272
|
-
case "DynamicText": return [child.expr];
|
|
306
|
+
case "DynamicText": return [parseExpressionIR(child.expr)];
|
|
273
307
|
case "Fragment": return [emitRawTextValue(child.children)];
|
|
274
308
|
default: return [];
|
|
275
309
|
}
|
|
@@ -292,12 +326,12 @@ function emitChildBinding(node, context) {
|
|
|
292
326
|
}
|
|
293
327
|
function emitAttrBinding(target, binding, context) {
|
|
294
328
|
const name = normalizeAttrName(binding.name);
|
|
295
|
-
if (name === "class") return t.expressionStatement(t.callExpression(context.importRuntime("bindClass"), [t.identifier(target.ref.name), emitGetter(binding.expr)]));
|
|
296
|
-
if (name === "style") return t.expressionStatement(t.callExpression(context.importRuntime("bindStyle"), [t.identifier(target.ref.name), emitGetter(binding.expr)]));
|
|
329
|
+
if (name === "class") return t.expressionStatement(t.callExpression(context.importRuntime("bindClass"), [t.identifier(target.ref.name), emitGetter(parseExpressionIR(binding.expr))]));
|
|
330
|
+
if (name === "style") return t.expressionStatement(t.callExpression(context.importRuntime("bindStyle"), [t.identifier(target.ref.name), emitGetter(parseExpressionIR(binding.expr))]));
|
|
297
331
|
return t.expressionStatement(t.callExpression(context.importRuntime("bindAttr"), [
|
|
298
332
|
t.identifier(target.ref.name),
|
|
299
333
|
t.stringLiteral(name),
|
|
300
|
-
emitGetter(binding.expr)
|
|
334
|
+
emitGetter(parseExpressionIR(binding.expr))
|
|
301
335
|
]));
|
|
302
336
|
}
|
|
303
337
|
function normalizeAttrName(name) {
|
|
@@ -308,7 +342,7 @@ function emitEventBinding(target, binding, context) {
|
|
|
308
342
|
return t.expressionStatement(t.callExpression(context.importRuntime("bindEvent"), [
|
|
309
343
|
t.identifier(target.ref.name),
|
|
310
344
|
t.stringLiteral(binding.eventName),
|
|
311
|
-
normalizeEventHandler(binding.handler, context)
|
|
345
|
+
normalizeEventHandler(parseExpressionIR(binding.handler), context)
|
|
312
346
|
]));
|
|
313
347
|
}
|
|
314
348
|
function normalizeEventHandler(handler, context) {
|
|
@@ -322,7 +356,7 @@ function emitPropBinding(target, binding, context) {
|
|
|
322
356
|
return t.expressionStatement(t.callExpression(context.importRuntime("bindProp"), [
|
|
323
357
|
t.identifier(target.ref.name),
|
|
324
358
|
t.stringLiteral(binding.name),
|
|
325
|
-
emitGetter(binding.expr)
|
|
359
|
+
emitGetter(parseExpressionIR(binding.expr))
|
|
326
360
|
]));
|
|
327
361
|
}
|
|
328
362
|
function emitGetter(expr) {
|
|
@@ -330,12 +364,13 @@ function emitGetter(expr) {
|
|
|
330
364
|
return t.arrowFunctionExpression([], expr);
|
|
331
365
|
}
|
|
332
366
|
function emitRefBinding(target, binding, context) {
|
|
333
|
-
return t.expressionStatement(t.callExpression(context.importRuntime("bindRef"), [t.identifier(target.ref.name), binding.expr]));
|
|
367
|
+
return t.expressionStatement(t.callExpression(context.importRuntime("bindRef"), [t.identifier(target.ref.name), parseExpressionIR(binding.expr)]));
|
|
334
368
|
}
|
|
335
369
|
function emitDynamicText(node, context) {
|
|
336
370
|
if (!node.domPath || node.domPath.kind !== "Marker") return [];
|
|
337
371
|
const textRef = context.uid("text$");
|
|
338
|
-
|
|
372
|
+
const expression = parseExpressionIR(node.expr);
|
|
373
|
+
if (node.once) return [t.variableDeclaration("const", [t.variableDeclarator(textRef, t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createTextNode")), [t.callExpression(t.identifier("String"), [expression])]))]), t.expressionStatement(t.callExpression(context.importRuntime("insert"), [
|
|
339
374
|
t.identifier(node.domPath.parent.name),
|
|
340
375
|
textRef,
|
|
341
376
|
t.identifier(node.ref.name)
|
|
@@ -347,7 +382,7 @@ function emitDynamicText(node, context) {
|
|
|
347
382
|
textRef,
|
|
348
383
|
t.identifier(node.ref.name)
|
|
349
384
|
])),
|
|
350
|
-
t.expressionStatement(t.callExpression(context.importRuntime("bindText"), [textRef, t.arrowFunctionExpression([],
|
|
385
|
+
t.expressionStatement(t.callExpression(context.importRuntime("bindText"), [textRef, t.arrowFunctionExpression([], expression)]))
|
|
351
386
|
];
|
|
352
387
|
}
|
|
353
388
|
function emitComponentInsert(node, context) {
|
|
@@ -580,7 +615,7 @@ function emitFragment(node, context) {
|
|
|
580
615
|
function emitNodeExpression(node, context) {
|
|
581
616
|
switch (node.kind) {
|
|
582
617
|
case "Text": return t.stringLiteral(node.value);
|
|
583
|
-
case "DynamicText": return node.expr;
|
|
618
|
+
case "DynamicText": return parseExpressionIR(node.expr);
|
|
584
619
|
case "Element": return emitElement(node, context);
|
|
585
620
|
case "Component": return emitComponent(node, context);
|
|
586
621
|
case "Fragment": return emitFragment(node, context);
|
|
@@ -595,13 +630,14 @@ function emitNodeExpression(node, context) {
|
|
|
595
630
|
//#region packages/core/compiler/src/codegen/dom/emitComponent.ts
|
|
596
631
|
function emitComponent(node, context) {
|
|
597
632
|
const props = t.objectExpression(node.props.map((prop) => emitComponentProp(prop, context)));
|
|
598
|
-
return t.callExpression(context.importRuntime("createComponent"), [node.callee, props]);
|
|
633
|
+
return t.callExpression(context.importRuntime("createComponent"), [parseExpressionIR(node.callee), props]);
|
|
599
634
|
}
|
|
600
635
|
function emitComponentProp(prop, context) {
|
|
601
636
|
const key = createObjectKey$1(prop.name);
|
|
602
637
|
if (Array.isArray(prop.value)) return t.objectMethod("get", key, [], t.blockStatement([t.returnStatement(emitChildrenProp(prop.value, context))]));
|
|
603
|
-
|
|
604
|
-
return t.
|
|
638
|
+
const value = parseExpressionIR(prop.value);
|
|
639
|
+
if (isStaticPropValue(value)) return t.objectProperty(key, value);
|
|
640
|
+
return t.objectMethod("get", key, [], t.blockStatement([t.returnStatement(value)]));
|
|
605
641
|
}
|
|
606
642
|
function emitChildrenProp(children, context) {
|
|
607
643
|
const nodes = children.map((child) => emitNodeExpression(child, context));
|
|
@@ -617,8 +653,8 @@ function createObjectKey$1(key) {
|
|
|
617
653
|
//#endregion
|
|
618
654
|
//#region packages/core/compiler/src/codegen/dom/emitBuiltin.ts
|
|
619
655
|
function emitShow(node, context) {
|
|
620
|
-
const props = [t.objectProperty(t.identifier("when"), node.when), t.objectProperty(t.identifier("children"), t.arrowFunctionExpression([], emitChildrenProp(node.children, context)))];
|
|
621
|
-
if (node.fallback) props.push(t.objectProperty(t.identifier("fallback"), Array.isArray(node.fallback) ? t.arrowFunctionExpression([], emitChildrenProp(node.fallback, context)) : node.fallback));
|
|
656
|
+
const props = [t.objectProperty(t.identifier("when"), parseExpressionIR(node.when)), t.objectProperty(t.identifier("children"), t.arrowFunctionExpression([], emitChildrenProp(node.children, context)))];
|
|
657
|
+
if (node.fallback) props.push(t.objectProperty(t.identifier("fallback"), Array.isArray(node.fallback) ? t.arrowFunctionExpression([], emitChildrenProp(node.fallback, context)) : parseExpressionIR(node.fallback)));
|
|
622
658
|
return t.callExpression(context.importRuntime("createComponent"), [context.importRuntime("Show"), t.objectExpression(props)]);
|
|
623
659
|
}
|
|
624
660
|
function emitMountShow(node, context) {
|
|
@@ -627,29 +663,28 @@ function emitMountShow(node, context) {
|
|
|
627
663
|
return t.callExpression(context.importRuntime("mountShow"), [
|
|
628
664
|
t.identifier(path.parent.name),
|
|
629
665
|
emitMarkerIdentifier(node),
|
|
630
|
-
t.arrowFunctionExpression([], node.when),
|
|
666
|
+
t.arrowFunctionExpression([], parseExpressionIR(node.when)),
|
|
631
667
|
t.arrowFunctionExpression([], emitChildrenProp(node.children, context)),
|
|
632
|
-
node.fallback ? Array.isArray(node.fallback) ? t.arrowFunctionExpression([], emitChildrenProp(node.fallback, context)) : t.arrowFunctionExpression([], node.fallback) : t.identifier("undefined")
|
|
668
|
+
node.fallback ? Array.isArray(node.fallback) ? t.arrowFunctionExpression([], emitChildrenProp(node.fallback, context)) : t.arrowFunctionExpression([], parseExpressionIR(node.fallback)) : t.identifier("undefined")
|
|
633
669
|
]);
|
|
634
670
|
}
|
|
635
671
|
function emitFor(node, context) {
|
|
636
|
-
const params = [node.item];
|
|
637
|
-
if (node.index) params.push(node.index);
|
|
638
|
-
const props = [t.objectProperty(t.identifier("each"), node.each), t.objectProperty(t.identifier("children"), t.arrowFunctionExpression(params, emitChildrenProp(node.body, context)))];
|
|
639
|
-
if (node.by) props.push(t.objectProperty(t.identifier("by"), node.by));
|
|
672
|
+
const params = [t.identifier(node.item.name)];
|
|
673
|
+
if (node.index) params.push(t.identifier(node.index.name));
|
|
674
|
+
const props = [t.objectProperty(t.identifier("each"), parseExpressionIR(node.each)), t.objectProperty(t.identifier("children"), t.arrowFunctionExpression(params, emitChildrenProp(node.body, context)))];
|
|
675
|
+
if (node.by) props.push(t.objectProperty(t.identifier("by"), parseExpressionIR(node.by)));
|
|
640
676
|
return t.callExpression(context.importRuntime("createComponent"), [context.importRuntime("For"), t.objectExpression(props)]);
|
|
641
677
|
}
|
|
642
678
|
function emitMountFor(node, context) {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (node.index) params.push(node.index);
|
|
679
|
+
const params = [t.identifier(node.item.name)];
|
|
680
|
+
if (node.index) params.push(t.identifier(node.index.name));
|
|
646
681
|
const path = node.domPath;
|
|
647
682
|
if (!path || path.kind !== "Marker") throw new Error("For DOM path is not assigned");
|
|
648
683
|
return t.callExpression(context.importRuntime("mountFor"), [
|
|
649
684
|
t.identifier(path.parent.name),
|
|
650
685
|
emitMarkerIdentifier(node),
|
|
651
|
-
t.arrowFunctionExpression([], node.each),
|
|
652
|
-
|
|
686
|
+
t.arrowFunctionExpression([], parseExpressionIR(node.each)),
|
|
687
|
+
node.by ? parseExpressionIR(node.by) : t.identifier("undefined"),
|
|
653
688
|
t.arrowFunctionExpression(params, emitChildrenProp(node.body, context))
|
|
654
689
|
]);
|
|
655
690
|
}
|
|
@@ -665,8 +700,9 @@ function buildHostProps(node, context) {
|
|
|
665
700
|
const props = [];
|
|
666
701
|
for (const attr of node.attrs) {
|
|
667
702
|
const key = createObjectKey(attr.name);
|
|
668
|
-
|
|
669
|
-
|
|
703
|
+
const expression = parseExpressionIR(attr.expr);
|
|
704
|
+
if (isStaticValue(expression) || isGetterExpression(expression)) props.push(t.objectProperty(key, expression));
|
|
705
|
+
else props.push(t.objectProperty(key, t.arrowFunctionExpression([], expression)));
|
|
670
706
|
}
|
|
671
707
|
return props;
|
|
672
708
|
}
|
|
@@ -696,7 +732,7 @@ function emitDOM(node, context) {
|
|
|
696
732
|
case "For": return emitFor(node, context);
|
|
697
733
|
case "Host": return emitHost(node, context);
|
|
698
734
|
case "Slot": return emitSlot(node, context);
|
|
699
|
-
case "DynamicText": return t.arrowFunctionExpression([], node.expr);
|
|
735
|
+
case "DynamicText": return t.arrowFunctionExpression([], parseExpressionIR(node.expr));
|
|
700
736
|
default: throw new Error(`Unsupported root IR node: ${node.kind}`);
|
|
701
737
|
}
|
|
702
738
|
}
|
|
@@ -792,203 +828,6 @@ var CompilerError = class extends Error {
|
|
|
792
828
|
}
|
|
793
829
|
};
|
|
794
830
|
//#endregion
|
|
795
|
-
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
|
|
796
|
-
function _typeof(o) {
|
|
797
|
-
"@babel/helpers - typeof";
|
|
798
|
-
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
799
|
-
return typeof o;
|
|
800
|
-
} : function(o) {
|
|
801
|
-
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
802
|
-
}, _typeof(o);
|
|
803
|
-
}
|
|
804
|
-
//#endregion
|
|
805
|
-
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
|
|
806
|
-
function toPrimitive(t, r) {
|
|
807
|
-
if ("object" != _typeof(t) || !t) return t;
|
|
808
|
-
var e = t[Symbol.toPrimitive];
|
|
809
|
-
if (void 0 !== e) {
|
|
810
|
-
var i = e.call(t, r || "default");
|
|
811
|
-
if ("object" != _typeof(i)) return i;
|
|
812
|
-
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
813
|
-
}
|
|
814
|
-
return ("string" === r ? String : Number)(t);
|
|
815
|
-
}
|
|
816
|
-
//#endregion
|
|
817
|
-
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
|
|
818
|
-
function toPropertyKey(t) {
|
|
819
|
-
var i = toPrimitive(t, "string");
|
|
820
|
-
return "symbol" == _typeof(i) ? i : i + "";
|
|
821
|
-
}
|
|
822
|
-
//#endregion
|
|
823
|
-
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
|
|
824
|
-
function _defineProperty(e, r, t) {
|
|
825
|
-
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
826
|
-
value: t,
|
|
827
|
-
enumerable: !0,
|
|
828
|
-
configurable: !0,
|
|
829
|
-
writable: !0
|
|
830
|
-
}) : e[r] = t, e;
|
|
831
|
-
}
|
|
832
|
-
//#endregion
|
|
833
|
-
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/objectSpread2.js
|
|
834
|
-
function ownKeys(e, r) {
|
|
835
|
-
var t = Object.keys(e);
|
|
836
|
-
if (Object.getOwnPropertySymbols) {
|
|
837
|
-
var o = Object.getOwnPropertySymbols(e);
|
|
838
|
-
r && (o = o.filter(function(r) {
|
|
839
|
-
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
840
|
-
})), t.push.apply(t, o);
|
|
841
|
-
}
|
|
842
|
-
return t;
|
|
843
|
-
}
|
|
844
|
-
function _objectSpread2(e) {
|
|
845
|
-
for (var r = 1; r < arguments.length; r++) {
|
|
846
|
-
var t = null != arguments[r] ? arguments[r] : {};
|
|
847
|
-
r % 2 ? ownKeys(Object(t), !0).forEach(function(r) {
|
|
848
|
-
_defineProperty(e, r, t[r]);
|
|
849
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r) {
|
|
850
|
-
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
return e;
|
|
854
|
-
}
|
|
855
|
-
//#endregion
|
|
856
|
-
//#region packages/core/compiler/src/ir/semanticBuilders.ts
|
|
857
|
-
let nextId = 0;
|
|
858
|
-
function id() {
|
|
859
|
-
return nextId++;
|
|
860
|
-
}
|
|
861
|
-
function ref(name) {
|
|
862
|
-
return { name };
|
|
863
|
-
}
|
|
864
|
-
function elementIR(input) {
|
|
865
|
-
var _input$attrs, _input$children;
|
|
866
|
-
return {
|
|
867
|
-
id: id(),
|
|
868
|
-
kind: "Element",
|
|
869
|
-
ref: input.ref,
|
|
870
|
-
tagName: input.tagName,
|
|
871
|
-
attrs: (_input$attrs = input.attrs) !== null && _input$attrs !== void 0 ? _input$attrs : [],
|
|
872
|
-
children: (_input$children = input.children) !== null && _input$children !== void 0 ? _input$children : [],
|
|
873
|
-
flags: _objectSpread2({
|
|
874
|
-
isSVG: false,
|
|
875
|
-
isVoid: false,
|
|
876
|
-
isCustomElement: input.tagName.includes("-")
|
|
877
|
-
}, input.flags)
|
|
878
|
-
};
|
|
879
|
-
}
|
|
880
|
-
function textIR(value) {
|
|
881
|
-
return {
|
|
882
|
-
id: id(),
|
|
883
|
-
kind: "Text",
|
|
884
|
-
value
|
|
885
|
-
};
|
|
886
|
-
}
|
|
887
|
-
function dynamicTextIR(expr, nodeRef, once = false) {
|
|
888
|
-
return {
|
|
889
|
-
id: id(),
|
|
890
|
-
kind: "DynamicText",
|
|
891
|
-
expr,
|
|
892
|
-
ref: nodeRef,
|
|
893
|
-
once
|
|
894
|
-
};
|
|
895
|
-
}
|
|
896
|
-
function fragmentIR(children) {
|
|
897
|
-
return {
|
|
898
|
-
id: id(),
|
|
899
|
-
kind: "Fragment",
|
|
900
|
-
children
|
|
901
|
-
};
|
|
902
|
-
}
|
|
903
|
-
function staticAttrIR(name, value) {
|
|
904
|
-
return {
|
|
905
|
-
id: id(),
|
|
906
|
-
kind: "StaticAttribute",
|
|
907
|
-
name,
|
|
908
|
-
value
|
|
909
|
-
};
|
|
910
|
-
}
|
|
911
|
-
function attrBindingIR(name, expr) {
|
|
912
|
-
return {
|
|
913
|
-
id: id(),
|
|
914
|
-
kind: "AttrBinding",
|
|
915
|
-
name,
|
|
916
|
-
expr
|
|
917
|
-
};
|
|
918
|
-
}
|
|
919
|
-
function propBindingIR(name, expr) {
|
|
920
|
-
return {
|
|
921
|
-
id: id(),
|
|
922
|
-
kind: "PropBinding",
|
|
923
|
-
name,
|
|
924
|
-
expr
|
|
925
|
-
};
|
|
926
|
-
}
|
|
927
|
-
function eventBindingIR(eventName, handler) {
|
|
928
|
-
return {
|
|
929
|
-
id: id(),
|
|
930
|
-
kind: "EventBinding",
|
|
931
|
-
eventName,
|
|
932
|
-
handler
|
|
933
|
-
};
|
|
934
|
-
}
|
|
935
|
-
function refBindingIR(expr) {
|
|
936
|
-
return {
|
|
937
|
-
id: id(),
|
|
938
|
-
kind: "RefBinding",
|
|
939
|
-
expr
|
|
940
|
-
};
|
|
941
|
-
}
|
|
942
|
-
function componentIR(input) {
|
|
943
|
-
return {
|
|
944
|
-
id: id(),
|
|
945
|
-
kind: "Component",
|
|
946
|
-
ref: input.ref,
|
|
947
|
-
callee: input.callee,
|
|
948
|
-
props: input.props
|
|
949
|
-
};
|
|
950
|
-
}
|
|
951
|
-
function showIR(input) {
|
|
952
|
-
return {
|
|
953
|
-
id: id(),
|
|
954
|
-
kind: "Show",
|
|
955
|
-
ref: input.ref,
|
|
956
|
-
when: input.when,
|
|
957
|
-
children: input.children,
|
|
958
|
-
fallback: input.fallback
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
function forIR(input) {
|
|
962
|
-
return {
|
|
963
|
-
id: id(),
|
|
964
|
-
kind: "For",
|
|
965
|
-
ref: input.ref,
|
|
966
|
-
each: input.each,
|
|
967
|
-
by: input.by,
|
|
968
|
-
item: input.item,
|
|
969
|
-
index: input.index,
|
|
970
|
-
body: input.body
|
|
971
|
-
};
|
|
972
|
-
}
|
|
973
|
-
function hostIR(input) {
|
|
974
|
-
return {
|
|
975
|
-
id: id(),
|
|
976
|
-
kind: "Host",
|
|
977
|
-
attrs: input.attrs,
|
|
978
|
-
child: input.child
|
|
979
|
-
};
|
|
980
|
-
}
|
|
981
|
-
function slotIR(input) {
|
|
982
|
-
var _input$fallback;
|
|
983
|
-
return {
|
|
984
|
-
id: id(),
|
|
985
|
-
kind: "Slot",
|
|
986
|
-
ref: input.ref,
|
|
987
|
-
name: input.name,
|
|
988
|
-
fallback: (_input$fallback = input.fallback) !== null && _input$fallback !== void 0 ? _input$fallback : []
|
|
989
|
-
};
|
|
990
|
-
}
|
|
991
|
-
//#endregion
|
|
992
831
|
//#region packages/core/compiler/src/parse/jsx.ts
|
|
993
832
|
/**
|
|
994
833
|
* JSX AST parsing utilities.
|
|
@@ -1026,8 +865,8 @@ function lowerAttribute(path, _context) {
|
|
|
1026
865
|
});
|
|
1027
866
|
const node = path.node;
|
|
1028
867
|
const name = getJSXAttrName(node.name);
|
|
1029
|
-
const value =
|
|
1030
|
-
if (!value) {
|
|
868
|
+
const value = path.get("value");
|
|
869
|
+
if (!value.node) {
|
|
1031
870
|
if (name === "ref") throw new CompilerError({
|
|
1032
871
|
code: CompilerErrorCode.EMPTY_EXPRESSION,
|
|
1033
872
|
message: "ref attribute requires an expression.",
|
|
@@ -1036,22 +875,24 @@ function lowerAttribute(path, _context) {
|
|
|
1036
875
|
});
|
|
1037
876
|
return staticAttrIR(name, true);
|
|
1038
877
|
}
|
|
1039
|
-
if (
|
|
878
|
+
if (value.isStringLiteral()) {
|
|
1040
879
|
if (name === "ref") throw new CompilerError({
|
|
1041
880
|
code: CompilerErrorCode.INVALID_REF_USAGE,
|
|
1042
881
|
message: "String refs are not supported in Zeus.",
|
|
1043
882
|
path,
|
|
1044
883
|
hint: "Use a state holder or callback ref: <div ref={el} />."
|
|
1045
884
|
});
|
|
1046
|
-
return staticAttrIR(name, value.value);
|
|
885
|
+
return staticAttrIR(name, value.node.value);
|
|
1047
886
|
}
|
|
1048
|
-
if (
|
|
1049
|
-
const
|
|
1050
|
-
if (
|
|
887
|
+
if (value.isJSXExpressionContainer()) {
|
|
888
|
+
const expression = value.get("expression");
|
|
889
|
+
if (expression.isJSXEmptyExpression()) throw new CompilerError({
|
|
1051
890
|
code: CompilerErrorCode.EMPTY_EXPRESSION,
|
|
1052
891
|
message: `Attribute "${name}" expression cannot be empty.`,
|
|
1053
892
|
path
|
|
1054
893
|
});
|
|
894
|
+
if (!expression.isExpression()) return null;
|
|
895
|
+
const expr = lowerExpressionIR(expression);
|
|
1055
896
|
if (name === "ref") return refBindingIR(expr);
|
|
1056
897
|
if (name.startsWith("on") && name.length > 2) return eventBindingIR(toEventName(name), expr);
|
|
1057
898
|
if (name.startsWith("prop:")) return propBindingIR(name.slice(5), expr);
|
|
@@ -1070,9 +911,9 @@ function lowerChildren(children, context) {
|
|
|
1070
911
|
continue;
|
|
1071
912
|
}
|
|
1072
913
|
if (child.isJSXExpressionContainer()) {
|
|
1073
|
-
const
|
|
1074
|
-
if (
|
|
1075
|
-
if (
|
|
914
|
+
const expression = child.get("expression");
|
|
915
|
+
if (expression.isJSXEmptyExpression()) continue;
|
|
916
|
+
if (expression.isExpression()) result.push(dynamicTextIR(lowerExpressionIR(expression), ref(context.uid("anchor$").name)));
|
|
1076
917
|
continue;
|
|
1077
918
|
}
|
|
1078
919
|
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
@@ -1128,25 +969,24 @@ function optionalShowFallbackAttr(path, context) {
|
|
|
1128
969
|
});
|
|
1129
970
|
if (!(attr === null || attr === void 0 ? void 0 : attr.isJSXAttribute())) return void 0;
|
|
1130
971
|
const value = attr.get("value");
|
|
1131
|
-
if (!value.node) return
|
|
1132
|
-
if (value.isStringLiteral()) return value.node;
|
|
972
|
+
if (!value.node) return expressionIRFromCode("true", attr.node);
|
|
973
|
+
if (value.isStringLiteral()) return expressionIRFromCode(JSON.stringify(value.node.value), value.node);
|
|
1133
974
|
if (!value.isJSXExpressionContainer()) return void 0;
|
|
1134
975
|
const expression = value.get("expression");
|
|
1135
976
|
if (expression.isJSXEmptyExpression()) return void 0;
|
|
1136
977
|
if (expression.isJSXElement() || expression.isJSXFragment()) return [lowerJSX(expression, context)];
|
|
1137
|
-
if (expression.isExpression()) return expression
|
|
978
|
+
if (expression.isExpression()) return lowerExpressionIR(expression);
|
|
1138
979
|
}
|
|
1139
980
|
function lowerFor(path, context) {
|
|
1140
|
-
var _getParamIdentifier;
|
|
1141
981
|
const each = requiredExpressionAttr(path, "each");
|
|
1142
982
|
const by = optionalExpressionAttr(path, "by");
|
|
1143
983
|
const render = getOnlyRenderFunction(path);
|
|
1144
|
-
const item =
|
|
984
|
+
const item = getParamIdentifier(render, 0, "item");
|
|
1145
985
|
const index = getParamIdentifier(render, 1);
|
|
1146
986
|
const bodyPath = render.get("body");
|
|
1147
987
|
const body = [];
|
|
1148
988
|
if (bodyPath.isJSXElement() || bodyPath.isJSXFragment()) body.push(lowerJSX(bodyPath, context));
|
|
1149
|
-
else if (bodyPath.isExpression()) body.push(dynamicTextIR(bodyPath
|
|
989
|
+
else if (bodyPath.isExpression()) body.push(dynamicTextIR(lowerExpressionIR(bodyPath), ref(context.uid("anchor$").name)));
|
|
1150
990
|
return forIR({
|
|
1151
991
|
ref: ref(context.uid("for$").name),
|
|
1152
992
|
each,
|
|
@@ -1166,7 +1006,7 @@ function lowerSlot(path, context) {
|
|
|
1166
1006
|
}
|
|
1167
1007
|
function requiredExpressionAttr(path, name) {
|
|
1168
1008
|
const value = optionalExpressionAttr(path, name);
|
|
1169
|
-
if (!value
|
|
1009
|
+
if (!value) throw new CompilerError({
|
|
1170
1010
|
code: CompilerErrorCode.INVALID_BUILTIN_USAGE,
|
|
1171
1011
|
message: `<${getBuiltinName(path)}> requires "${name}".`,
|
|
1172
1012
|
path
|
|
@@ -1179,10 +1019,12 @@ function optionalExpressionAttr(path, name) {
|
|
|
1179
1019
|
return getJSXAttrName(attrPath.node.name) === name;
|
|
1180
1020
|
});
|
|
1181
1021
|
if (!(attr === null || attr === void 0 ? void 0 : attr.isJSXAttribute())) return void 0;
|
|
1182
|
-
const value = attr.
|
|
1183
|
-
if (!value) return
|
|
1184
|
-
if (
|
|
1185
|
-
if (
|
|
1022
|
+
const value = attr.get("value");
|
|
1023
|
+
if (!value.node) return expressionIRFromCode("true", attr.node);
|
|
1024
|
+
if (value.isStringLiteral()) return expressionIRFromCode(JSON.stringify(value.node.value), value.node);
|
|
1025
|
+
if (!value.isJSXExpressionContainer()) return void 0;
|
|
1026
|
+
const expression = value.get("expression");
|
|
1027
|
+
if (expression.isExpression()) return lowerExpressionIR(expression);
|
|
1186
1028
|
}
|
|
1187
1029
|
function optionalStringAttr(path, name) {
|
|
1188
1030
|
const attr = path.get("openingElement").get("attributes").find((attrPath) => {
|
|
@@ -1205,8 +1047,8 @@ function getOnlyRenderFunction(path) {
|
|
|
1205
1047
|
}
|
|
1206
1048
|
function getParamIdentifier(path, index, fallback) {
|
|
1207
1049
|
const param = path.node.params[index];
|
|
1208
|
-
if (t.isIdentifier(param)) return param;
|
|
1209
|
-
if (fallback) return
|
|
1050
|
+
if (t.isIdentifier(param)) return identifierIR(param.name, sourceSpanFromBabelNode(param));
|
|
1051
|
+
if (fallback) return identifierIR(fallback);
|
|
1210
1052
|
}
|
|
1211
1053
|
function getBuiltinName(path) {
|
|
1212
1054
|
const name = path.node.openingElement.name;
|
|
@@ -1246,31 +1088,34 @@ function lowerHost(path, context) {
|
|
|
1246
1088
|
id: id(),
|
|
1247
1089
|
kind: "HostAttr",
|
|
1248
1090
|
name: "ref",
|
|
1249
|
-
expr: expr
|
|
1091
|
+
expr: lowerExpressionIR(expr)
|
|
1250
1092
|
});
|
|
1251
1093
|
}
|
|
1252
1094
|
continue;
|
|
1253
1095
|
}
|
|
1254
1096
|
if (isEventLikeProp(name)) continue;
|
|
1255
|
-
const value =
|
|
1256
|
-
if (!value) attrs.push({
|
|
1257
|
-
id: id(),
|
|
1258
|
-
kind: "HostAttr",
|
|
1259
|
-
name: normalizeHostAttrName(name),
|
|
1260
|
-
expr: t.booleanLiteral(true)
|
|
1261
|
-
});
|
|
1262
|
-
else if (t.isStringLiteral(value)) attrs.push({
|
|
1097
|
+
const value = attrPath.get("value");
|
|
1098
|
+
if (!value.node) attrs.push({
|
|
1263
1099
|
id: id(),
|
|
1264
1100
|
kind: "HostAttr",
|
|
1265
1101
|
name: normalizeHostAttrName(name),
|
|
1266
|
-
expr:
|
|
1102
|
+
expr: expressionIRFromCode("true", node)
|
|
1267
1103
|
});
|
|
1268
|
-
else if (
|
|
1104
|
+
else if (value.isStringLiteral()) attrs.push({
|
|
1269
1105
|
id: id(),
|
|
1270
1106
|
kind: "HostAttr",
|
|
1271
1107
|
name: normalizeHostAttrName(name),
|
|
1272
|
-
expr: value.
|
|
1108
|
+
expr: expressionIRFromCode(JSON.stringify(value.node.value), value.node)
|
|
1273
1109
|
});
|
|
1110
|
+
else if (value.isJSXExpressionContainer()) {
|
|
1111
|
+
const expression = value.get("expression");
|
|
1112
|
+
if (expression.isExpression()) attrs.push({
|
|
1113
|
+
id: id(),
|
|
1114
|
+
kind: "HostAttr",
|
|
1115
|
+
name: normalizeHostAttrName(name),
|
|
1116
|
+
expr: lowerExpressionIR(expression)
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1274
1119
|
}
|
|
1275
1120
|
return hostIR({
|
|
1276
1121
|
attrs,
|
|
@@ -1290,29 +1135,31 @@ function lowerComponent(path, context) {
|
|
|
1290
1135
|
path: attr
|
|
1291
1136
|
});
|
|
1292
1137
|
const name = getJSXAttrName(node.name);
|
|
1293
|
-
|
|
1138
|
+
const value = attr.get("value");
|
|
1139
|
+
if (!value.node) {
|
|
1294
1140
|
props.push({
|
|
1295
1141
|
name,
|
|
1296
|
-
value:
|
|
1142
|
+
value: expressionIRFromCode("true", node)
|
|
1297
1143
|
});
|
|
1298
1144
|
continue;
|
|
1299
1145
|
}
|
|
1300
|
-
if (
|
|
1146
|
+
if (value.isStringLiteral()) {
|
|
1301
1147
|
props.push({
|
|
1302
1148
|
name,
|
|
1303
|
-
value: node.value
|
|
1149
|
+
value: expressionIRFromCode(JSON.stringify(value.node.value), value.node)
|
|
1304
1150
|
});
|
|
1305
1151
|
continue;
|
|
1306
1152
|
}
|
|
1307
|
-
if (
|
|
1308
|
-
|
|
1153
|
+
if (value.isJSXExpressionContainer()) {
|
|
1154
|
+
const expression = value.get("expression");
|
|
1155
|
+
if (expression.isJSXEmptyExpression()) throw new CompilerError({
|
|
1309
1156
|
code: CompilerErrorCode.EMPTY_EXPRESSION,
|
|
1310
1157
|
message: `Component prop "${name}" expression cannot be empty.`,
|
|
1311
1158
|
path: attr
|
|
1312
1159
|
});
|
|
1313
|
-
props.push({
|
|
1160
|
+
if (expression.isExpression()) props.push({
|
|
1314
1161
|
name,
|
|
1315
|
-
value:
|
|
1162
|
+
value: lowerExpressionIR(expression)
|
|
1316
1163
|
});
|
|
1317
1164
|
}
|
|
1318
1165
|
}
|
|
@@ -1328,18 +1175,20 @@ function lowerComponent(path, context) {
|
|
|
1328
1175
|
});
|
|
1329
1176
|
}
|
|
1330
1177
|
function convertComponentIdentifier(node) {
|
|
1178
|
+
return expressionIRFromCode(componentIdentifierCode(node), node);
|
|
1179
|
+
}
|
|
1180
|
+
function componentIdentifierCode(node) {
|
|
1331
1181
|
if (t.isJSXIdentifier(node)) {
|
|
1332
|
-
if (node.name === "this") return
|
|
1333
|
-
|
|
1334
|
-
return t.stringLiteral(node.name);
|
|
1182
|
+
if (node.name === "this") return "this";
|
|
1183
|
+
return t.isValidIdentifier(node.name) ? node.name : JSON.stringify(node.name);
|
|
1335
1184
|
}
|
|
1336
1185
|
if (t.isJSXMemberExpression(node)) {
|
|
1337
|
-
const object =
|
|
1338
|
-
const property =
|
|
1339
|
-
return t.
|
|
1186
|
+
const object = componentIdentifierCode(node.object);
|
|
1187
|
+
const property = node.property.name;
|
|
1188
|
+
return t.isValidIdentifier(property) ? `${object}.${property}` : `${object}[${JSON.stringify(property)}]`;
|
|
1340
1189
|
}
|
|
1341
|
-
if (t.isJSXNamespacedName(node)) return
|
|
1342
|
-
return
|
|
1190
|
+
if (t.isJSXNamespacedName(node)) return JSON.stringify(`${node.namespace.name}:${node.name.name}`);
|
|
1191
|
+
return JSON.stringify("");
|
|
1343
1192
|
}
|
|
1344
1193
|
//#endregion
|
|
1345
1194
|
//#region packages/core/compiler/src/lower/lowerElement.ts
|
|
@@ -1622,6 +1471,67 @@ function appendPhysicalChild(result, node) {
|
|
|
1622
1471
|
}
|
|
1623
1472
|
}
|
|
1624
1473
|
//#endregion
|
|
1474
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
|
|
1475
|
+
function _typeof(o) {
|
|
1476
|
+
"@babel/helpers - typeof";
|
|
1477
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
1478
|
+
return typeof o;
|
|
1479
|
+
} : function(o) {
|
|
1480
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
1481
|
+
}, _typeof(o);
|
|
1482
|
+
}
|
|
1483
|
+
//#endregion
|
|
1484
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
|
|
1485
|
+
function toPrimitive(t, r) {
|
|
1486
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
1487
|
+
var e = t[Symbol.toPrimitive];
|
|
1488
|
+
if (void 0 !== e) {
|
|
1489
|
+
var i = e.call(t, r || "default");
|
|
1490
|
+
if ("object" != _typeof(i)) return i;
|
|
1491
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
1492
|
+
}
|
|
1493
|
+
return ("string" === r ? String : Number)(t);
|
|
1494
|
+
}
|
|
1495
|
+
//#endregion
|
|
1496
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
|
|
1497
|
+
function toPropertyKey(t) {
|
|
1498
|
+
var i = toPrimitive(t, "string");
|
|
1499
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
1500
|
+
}
|
|
1501
|
+
//#endregion
|
|
1502
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
|
|
1503
|
+
function _defineProperty(e, r, t) {
|
|
1504
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
1505
|
+
value: t,
|
|
1506
|
+
enumerable: !0,
|
|
1507
|
+
configurable: !0,
|
|
1508
|
+
writable: !0
|
|
1509
|
+
}) : e[r] = t, e;
|
|
1510
|
+
}
|
|
1511
|
+
//#endregion
|
|
1512
|
+
//#region \0@oxc-project+runtime@0.137.0/helpers/esm/objectSpread2.js
|
|
1513
|
+
function ownKeys(e, r) {
|
|
1514
|
+
var t = Object.keys(e);
|
|
1515
|
+
if (Object.getOwnPropertySymbols) {
|
|
1516
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
1517
|
+
r && (o = o.filter(function(r) {
|
|
1518
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
1519
|
+
})), t.push.apply(t, o);
|
|
1520
|
+
}
|
|
1521
|
+
return t;
|
|
1522
|
+
}
|
|
1523
|
+
function _objectSpread2(e) {
|
|
1524
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
1525
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
1526
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r) {
|
|
1527
|
+
_defineProperty(e, r, t[r]);
|
|
1528
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r) {
|
|
1529
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
1530
|
+
});
|
|
1531
|
+
}
|
|
1532
|
+
return e;
|
|
1533
|
+
}
|
|
1534
|
+
//#endregion
|
|
1625
1535
|
//#region packages/core/compiler/src/passes/validateBuiltins.ts
|
|
1626
1536
|
function validateBuiltins(node) {
|
|
1627
1537
|
visit$1(node, {
|