@fictjs/compiler 0.0.10 → 0.0.12
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/index.cjs +199 -86
- package/dist/index.js +199 -86
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -22760,114 +22760,151 @@ function lowerIntrinsicElement(jsx, ctx) {
|
|
|
22760
22760
|
for (const binding of bindings) {
|
|
22761
22761
|
const targetId = resolveHIRBindingPath(binding.path, nodeCache, statements, ctx);
|
|
22762
22762
|
if (binding.type === "event" && binding.expr && binding.name) {
|
|
22763
|
-
const shouldWrapHandler = isExpressionReactive(binding.expr, ctx);
|
|
22764
|
-
const prevWrapTracked = ctx.wrapTrackedExpressions;
|
|
22765
|
-
ctx.wrapTrackedExpressions = false;
|
|
22766
|
-
const valueExpr = lowerDomExpression(binding.expr, ctx, containingRegion, {
|
|
22767
|
-
skipHookAccessors: true,
|
|
22768
|
-
skipRegionRootOverride: true
|
|
22769
|
-
});
|
|
22770
|
-
ctx.wrapTrackedExpressions = prevWrapTracked;
|
|
22771
|
-
const eventParam = t2.identifier("_e");
|
|
22772
|
-
const isFn = t2.isArrowFunctionExpression(valueExpr) || t2.isFunctionExpression(valueExpr);
|
|
22773
|
-
const ensureHandlerParam = (fn) => {
|
|
22774
|
-
if (t2.isArrowFunctionExpression(fn)) {
|
|
22775
|
-
if (fn.params.length > 0) return fn;
|
|
22776
|
-
return t2.arrowFunctionExpression([eventParam], fn.body, fn.async);
|
|
22777
|
-
}
|
|
22778
|
-
if (t2.isFunctionExpression(fn)) {
|
|
22779
|
-
if (fn.params.length > 0) return fn;
|
|
22780
|
-
return t2.functionExpression(fn.id, [eventParam], fn.body, fn.generator, fn.async);
|
|
22781
|
-
}
|
|
22782
|
-
return t2.arrowFunctionExpression(
|
|
22783
|
-
[eventParam],
|
|
22784
|
-
t2.callExpression(fn, [eventParam])
|
|
22785
|
-
);
|
|
22786
|
-
};
|
|
22787
|
-
const handlerExpr = !isFn && shouldWrapHandler ? t2.arrowFunctionExpression([], valueExpr) : ensureHandlerParam(valueExpr);
|
|
22788
22763
|
const eventName = binding.name;
|
|
22789
22764
|
const hasEventOptions = binding.eventOptions && (binding.eventOptions.capture || binding.eventOptions.passive || binding.eventOptions.once);
|
|
22790
22765
|
const isDelegated = DelegatedEvents.has(eventName) && !hasEventOptions;
|
|
22791
|
-
const
|
|
22792
|
-
if (
|
|
22766
|
+
const hirDataBinding = isDelegated && binding.expr ? extractDelegatedEventDataFromHIR(binding.expr, ctx) : null;
|
|
22767
|
+
if (hirDataBinding) {
|
|
22793
22768
|
ctx.delegatedEventsUsed?.add(eventName);
|
|
22794
|
-
const
|
|
22795
|
-
const
|
|
22796
|
-
|
|
22797
|
-
|
|
22798
|
-
|
|
22799
|
-
|
|
22800
|
-
|
|
22801
|
-
const
|
|
22802
|
-
|
|
22803
|
-
|
|
22804
|
-
|
|
22805
|
-
let handlerToAssign = handlerIsCallableExpr ? handlerForDelegate : t2.arrowFunctionExpression([eventParam], handlerForDelegate);
|
|
22806
|
-
if (dataForDelegate) {
|
|
22807
|
-
let payloadExpr;
|
|
22808
|
-
if (t2.isArrowFunctionExpression(dataForDelegate) && dataForDelegate.params.length === 0) {
|
|
22809
|
-
payloadExpr = t2.isBlockStatement(dataForDelegate.body) ? t2.callExpression(t2.arrowFunctionExpression([], dataForDelegate.body), []) : dataForDelegate.body;
|
|
22810
|
-
} else {
|
|
22811
|
-
payloadExpr = t2.callExpression(dataForDelegate, []);
|
|
22812
|
-
}
|
|
22813
|
-
handlerToAssign = t2.arrowFunctionExpression(
|
|
22814
|
-
[eventParam],
|
|
22815
|
-
t2.callExpression(handlerForDelegate, [payloadExpr])
|
|
22816
|
-
);
|
|
22817
|
-
}
|
|
22769
|
+
const handlerExpr = lowerExpression(hirDataBinding.handler, ctx);
|
|
22770
|
+
const dataExpr = lowerDomExpression(hirDataBinding.data, ctx, containingRegion, {
|
|
22771
|
+
skipHookAccessors: false,
|
|
22772
|
+
skipRegionRootOverride: true
|
|
22773
|
+
});
|
|
22774
|
+
const dataParam = t2.identifier("__data");
|
|
22775
|
+
const eventParam = t2.identifier("_e");
|
|
22776
|
+
const wrappedHandler = t2.arrowFunctionExpression(
|
|
22777
|
+
[dataParam, eventParam],
|
|
22778
|
+
t2.callExpression(handlerExpr, [dataParam])
|
|
22779
|
+
);
|
|
22818
22780
|
statements.push(
|
|
22819
22781
|
t2.expressionStatement(
|
|
22820
22782
|
t2.assignmentExpression(
|
|
22821
22783
|
"=",
|
|
22822
22784
|
t2.memberExpression(targetId, t2.identifier(`$$${eventName}`)),
|
|
22823
|
-
|
|
22785
|
+
wrappedHandler
|
|
22824
22786
|
)
|
|
22825
22787
|
)
|
|
22826
22788
|
);
|
|
22827
|
-
|
|
22789
|
+
const dataGetter = t2.arrowFunctionExpression([], dataExpr);
|
|
22790
|
+
statements.push(
|
|
22791
|
+
t2.expressionStatement(
|
|
22792
|
+
t2.assignmentExpression(
|
|
22793
|
+
"=",
|
|
22794
|
+
t2.memberExpression(targetId, t2.identifier(`$$${eventName}Data`)),
|
|
22795
|
+
dataGetter
|
|
22796
|
+
)
|
|
22797
|
+
)
|
|
22798
|
+
);
|
|
22799
|
+
} else {
|
|
22800
|
+
const shouldWrapHandler = isExpressionReactive(binding.expr, ctx);
|
|
22801
|
+
const prevWrapTracked = ctx.wrapTrackedExpressions;
|
|
22802
|
+
ctx.wrapTrackedExpressions = false;
|
|
22803
|
+
const valueExpr = lowerDomExpression(binding.expr, ctx, containingRegion, {
|
|
22804
|
+
skipHookAccessors: true,
|
|
22805
|
+
skipRegionRootOverride: true
|
|
22806
|
+
});
|
|
22807
|
+
ctx.wrapTrackedExpressions = prevWrapTracked;
|
|
22808
|
+
const eventParam = t2.identifier("_e");
|
|
22809
|
+
const isFn = t2.isArrowFunctionExpression(valueExpr) || t2.isFunctionExpression(valueExpr);
|
|
22810
|
+
const ensureHandlerParam = (fn) => {
|
|
22811
|
+
if (t2.isArrowFunctionExpression(fn)) {
|
|
22812
|
+
if (fn.params.length > 0) return fn;
|
|
22813
|
+
return t2.arrowFunctionExpression([eventParam], fn.body, fn.async);
|
|
22814
|
+
}
|
|
22815
|
+
if (t2.isFunctionExpression(fn)) {
|
|
22816
|
+
if (fn.params.length > 0) return fn;
|
|
22817
|
+
return t2.functionExpression(fn.id, [eventParam], fn.body, fn.generator, fn.async);
|
|
22818
|
+
}
|
|
22819
|
+
return t2.arrowFunctionExpression(
|
|
22820
|
+
[eventParam],
|
|
22821
|
+
t2.callExpression(fn, [eventParam])
|
|
22822
|
+
);
|
|
22823
|
+
};
|
|
22824
|
+
const handlerExpr = !isFn && shouldWrapHandler ? t2.arrowFunctionExpression([], valueExpr) : ensureHandlerParam(valueExpr);
|
|
22825
|
+
const dataBinding = isDelegated && !shouldWrapHandler ? extractDelegatedEventData(valueExpr, t2) : null;
|
|
22826
|
+
if (isDelegated) {
|
|
22827
|
+
ctx.delegatedEventsUsed?.add(eventName);
|
|
22828
|
+
const finalHandler = !isFn && shouldWrapHandler ? t2.arrowFunctionExpression([eventParam], t2.callExpression(valueExpr, [eventParam])) : handlerExpr;
|
|
22829
|
+
const normalizeHandler = (expr) => {
|
|
22830
|
+
if (t2.isCallExpression(expr) && (t2.isIdentifier(expr.callee) || t2.isMemberExpression(expr.callee))) {
|
|
22831
|
+
return expr.callee;
|
|
22832
|
+
}
|
|
22833
|
+
return expr;
|
|
22834
|
+
};
|
|
22835
|
+
const normalizedDataHandler = dataBinding !== null ? normalizeHandler(
|
|
22836
|
+
dataBinding?.handler ?? handlerExpr
|
|
22837
|
+
) : null;
|
|
22838
|
+
const dataForDelegate = dataBinding?.data && (t2.isArrowFunctionExpression(dataBinding.data) || t2.isFunctionExpression(dataBinding.data) ? dataBinding.data : t2.arrowFunctionExpression([], dataBinding.data));
|
|
22839
|
+
const handlerForDelegate = normalizedDataHandler ?? (dataBinding ? normalizeHandler(handlerExpr) : finalHandler);
|
|
22840
|
+
const handlerIsCallableExpr = t2.isArrowFunctionExpression(handlerForDelegate) || t2.isFunctionExpression(handlerForDelegate) || t2.isIdentifier(handlerForDelegate) || t2.isMemberExpression(handlerForDelegate);
|
|
22841
|
+
let handlerToAssign = handlerIsCallableExpr ? handlerForDelegate : t2.arrowFunctionExpression([eventParam], handlerForDelegate);
|
|
22842
|
+
if (dataForDelegate) {
|
|
22843
|
+
let payloadExpr;
|
|
22844
|
+
if (t2.isArrowFunctionExpression(dataForDelegate) && dataForDelegate.params.length === 0) {
|
|
22845
|
+
payloadExpr = t2.isBlockStatement(dataForDelegate.body) ? t2.callExpression(t2.arrowFunctionExpression([], dataForDelegate.body), []) : dataForDelegate.body;
|
|
22846
|
+
} else {
|
|
22847
|
+
payloadExpr = t2.callExpression(dataForDelegate, []);
|
|
22848
|
+
}
|
|
22849
|
+
handlerToAssign = t2.arrowFunctionExpression(
|
|
22850
|
+
[eventParam],
|
|
22851
|
+
t2.callExpression(handlerForDelegate, [payloadExpr])
|
|
22852
|
+
);
|
|
22853
|
+
}
|
|
22828
22854
|
statements.push(
|
|
22829
22855
|
t2.expressionStatement(
|
|
22830
22856
|
t2.assignmentExpression(
|
|
22831
22857
|
"=",
|
|
22832
|
-
t2.memberExpression(targetId, t2.identifier(`$$${eventName}
|
|
22833
|
-
|
|
22858
|
+
t2.memberExpression(targetId, t2.identifier(`$$${eventName}`)),
|
|
22859
|
+
handlerToAssign
|
|
22834
22860
|
)
|
|
22835
22861
|
)
|
|
22836
22862
|
);
|
|
22837
|
-
|
|
22838
|
-
|
|
22839
|
-
|
|
22840
|
-
|
|
22841
|
-
|
|
22842
|
-
|
|
22843
|
-
|
|
22844
|
-
|
|
22845
|
-
|
|
22846
|
-
|
|
22847
|
-
if (hasEventOptions && binding.eventOptions) {
|
|
22848
|
-
const optionProps = [];
|
|
22849
|
-
if (binding.eventOptions.capture) {
|
|
22850
|
-
optionProps.push(t2.objectProperty(t2.identifier("capture"), t2.booleanLiteral(true)));
|
|
22851
|
-
}
|
|
22852
|
-
if (binding.eventOptions.passive) {
|
|
22853
|
-
optionProps.push(t2.objectProperty(t2.identifier("passive"), t2.booleanLiteral(true)));
|
|
22863
|
+
if (dataForDelegate) {
|
|
22864
|
+
statements.push(
|
|
22865
|
+
t2.expressionStatement(
|
|
22866
|
+
t2.assignmentExpression(
|
|
22867
|
+
"=",
|
|
22868
|
+
t2.memberExpression(targetId, t2.identifier(`$$${eventName}Data`)),
|
|
22869
|
+
dataForDelegate
|
|
22870
|
+
)
|
|
22871
|
+
)
|
|
22872
|
+
);
|
|
22854
22873
|
}
|
|
22855
|
-
|
|
22856
|
-
|
|
22874
|
+
} else {
|
|
22875
|
+
ctx.helpersUsed.add("bindEvent");
|
|
22876
|
+
ctx.helpersUsed.add("onDestroy");
|
|
22877
|
+
const cleanupId = genTemp(ctx, "evt");
|
|
22878
|
+
const args = [
|
|
22879
|
+
targetId,
|
|
22880
|
+
t2.stringLiteral(eventName),
|
|
22881
|
+
handlerExpr
|
|
22882
|
+
];
|
|
22883
|
+
if (hasEventOptions && binding.eventOptions) {
|
|
22884
|
+
const optionProps = [];
|
|
22885
|
+
if (binding.eventOptions.capture) {
|
|
22886
|
+
optionProps.push(t2.objectProperty(t2.identifier("capture"), t2.booleanLiteral(true)));
|
|
22887
|
+
}
|
|
22888
|
+
if (binding.eventOptions.passive) {
|
|
22889
|
+
optionProps.push(t2.objectProperty(t2.identifier("passive"), t2.booleanLiteral(true)));
|
|
22890
|
+
}
|
|
22891
|
+
if (binding.eventOptions.once) {
|
|
22892
|
+
optionProps.push(t2.objectProperty(t2.identifier("once"), t2.booleanLiteral(true)));
|
|
22893
|
+
}
|
|
22894
|
+
args.push(t2.objectExpression(optionProps));
|
|
22857
22895
|
}
|
|
22858
|
-
|
|
22859
|
-
|
|
22860
|
-
|
|
22861
|
-
|
|
22862
|
-
|
|
22863
|
-
|
|
22864
|
-
|
|
22896
|
+
statements.push(
|
|
22897
|
+
t2.variableDeclaration("const", [
|
|
22898
|
+
t2.variableDeclarator(
|
|
22899
|
+
cleanupId,
|
|
22900
|
+
t2.callExpression(t2.identifier(RUNTIME_ALIASES.bindEvent), args)
|
|
22901
|
+
)
|
|
22902
|
+
]),
|
|
22903
|
+
t2.expressionStatement(
|
|
22904
|
+
t2.callExpression(t2.identifier(RUNTIME_ALIASES.onDestroy), [cleanupId])
|
|
22865
22905
|
)
|
|
22866
|
-
|
|
22867
|
-
|
|
22868
|
-
t2.callExpression(t2.identifier(RUNTIME_ALIASES.onDestroy), [cleanupId])
|
|
22869
|
-
)
|
|
22870
|
-
);
|
|
22906
|
+
);
|
|
22907
|
+
}
|
|
22871
22908
|
}
|
|
22872
22909
|
} else if (binding.type === "attr" && binding.name) {
|
|
22873
22910
|
const attrName = binding.name;
|
|
@@ -23254,6 +23291,82 @@ function extractDelegatedEventData(expr, t2) {
|
|
|
23254
23291
|
data: dataArg && t2.isExpression(dataArg) ? dataArg : void 0
|
|
23255
23292
|
};
|
|
23256
23293
|
}
|
|
23294
|
+
function extractDelegatedEventDataFromHIR(expr, ctx) {
|
|
23295
|
+
if (expr.kind !== "ArrowFunction" && expr.kind !== "FunctionExpression") {
|
|
23296
|
+
return null;
|
|
23297
|
+
}
|
|
23298
|
+
let bodyExpr = null;
|
|
23299
|
+
if (expr.kind === "ArrowFunction") {
|
|
23300
|
+
if (expr.isExpression && !Array.isArray(expr.body)) {
|
|
23301
|
+
bodyExpr = expr.body;
|
|
23302
|
+
}
|
|
23303
|
+
}
|
|
23304
|
+
if (!bodyExpr || bodyExpr.kind !== "CallExpression") {
|
|
23305
|
+
return null;
|
|
23306
|
+
}
|
|
23307
|
+
const callee = bodyExpr.callee;
|
|
23308
|
+
if (callee.kind !== "Identifier") {
|
|
23309
|
+
return null;
|
|
23310
|
+
}
|
|
23311
|
+
if (bodyExpr.arguments.length !== 1) {
|
|
23312
|
+
return null;
|
|
23313
|
+
}
|
|
23314
|
+
if (callee.kind === "Identifier") {
|
|
23315
|
+
const handlerName = deSSAVarName(callee.name);
|
|
23316
|
+
const isTrackedAccessor = ctx.signalVars?.has(handlerName) || ctx.memoVars?.has(handlerName) || ctx.aliasVars?.has(handlerName);
|
|
23317
|
+
if (isTrackedAccessor) {
|
|
23318
|
+
return null;
|
|
23319
|
+
}
|
|
23320
|
+
}
|
|
23321
|
+
const paramNames = new Set(expr.params.map((p) => p.name));
|
|
23322
|
+
const dataExpr = bodyExpr.arguments[0];
|
|
23323
|
+
if (!dataExpr) {
|
|
23324
|
+
return null;
|
|
23325
|
+
}
|
|
23326
|
+
if (hirExpressionUsesIdentifiers(dataExpr, paramNames)) {
|
|
23327
|
+
return null;
|
|
23328
|
+
}
|
|
23329
|
+
return {
|
|
23330
|
+
handler: callee,
|
|
23331
|
+
data: dataExpr
|
|
23332
|
+
};
|
|
23333
|
+
}
|
|
23334
|
+
function hirExpressionUsesIdentifiers(expr, names) {
|
|
23335
|
+
if (expr.kind === "Identifier") {
|
|
23336
|
+
return names.has(deSSAVarName(expr.name));
|
|
23337
|
+
}
|
|
23338
|
+
switch (expr.kind) {
|
|
23339
|
+
case "BinaryExpression":
|
|
23340
|
+
case "LogicalExpression":
|
|
23341
|
+
return hirExpressionUsesIdentifiers(expr.left, names) || hirExpressionUsesIdentifiers(expr.right, names);
|
|
23342
|
+
case "UnaryExpression":
|
|
23343
|
+
return hirExpressionUsesIdentifiers(expr.argument, names);
|
|
23344
|
+
case "ConditionalExpression":
|
|
23345
|
+
return hirExpressionUsesIdentifiers(expr.test, names) || hirExpressionUsesIdentifiers(expr.consequent, names) || hirExpressionUsesIdentifiers(expr.alternate, names);
|
|
23346
|
+
case "CallExpression":
|
|
23347
|
+
case "OptionalCallExpression":
|
|
23348
|
+
return hirExpressionUsesIdentifiers(expr.callee, names) || expr.arguments.some((arg) => hirExpressionUsesIdentifiers(arg, names));
|
|
23349
|
+
case "MemberExpression":
|
|
23350
|
+
case "OptionalMemberExpression":
|
|
23351
|
+
return hirExpressionUsesIdentifiers(expr.object, names) || expr.computed && hirExpressionUsesIdentifiers(expr.property, names);
|
|
23352
|
+
case "ArrayExpression":
|
|
23353
|
+
return expr.elements.some((el) => el && hirExpressionUsesIdentifiers(el, names));
|
|
23354
|
+
case "ObjectExpression":
|
|
23355
|
+
return expr.properties.some((prop) => {
|
|
23356
|
+
if (prop.kind === "SpreadElement") {
|
|
23357
|
+
return hirExpressionUsesIdentifiers(prop.argument, names);
|
|
23358
|
+
}
|
|
23359
|
+
return hirExpressionUsesIdentifiers(prop.key, names) || hirExpressionUsesIdentifiers(prop.value, names);
|
|
23360
|
+
});
|
|
23361
|
+
case "TemplateLiteral":
|
|
23362
|
+
return expr.expressions.some((e) => hirExpressionUsesIdentifiers(e, names));
|
|
23363
|
+
case "ArrowFunction":
|
|
23364
|
+
case "FunctionExpression":
|
|
23365
|
+
return false;
|
|
23366
|
+
default:
|
|
23367
|
+
return false;
|
|
23368
|
+
}
|
|
23369
|
+
}
|
|
23257
23370
|
function getTrackedCallIdentifier(expr, ctx, itemParamName) {
|
|
23258
23371
|
if (ctx.t.isCallExpression(expr) && ctx.t.isIdentifier(expr.callee)) {
|
|
23259
23372
|
if (expr.arguments.length !== 0) return null;
|
package/dist/index.js
CHANGED
|
@@ -22748,114 +22748,151 @@ function lowerIntrinsicElement(jsx, ctx) {
|
|
|
22748
22748
|
for (const binding of bindings) {
|
|
22749
22749
|
const targetId = resolveHIRBindingPath(binding.path, nodeCache, statements, ctx);
|
|
22750
22750
|
if (binding.type === "event" && binding.expr && binding.name) {
|
|
22751
|
-
const shouldWrapHandler = isExpressionReactive(binding.expr, ctx);
|
|
22752
|
-
const prevWrapTracked = ctx.wrapTrackedExpressions;
|
|
22753
|
-
ctx.wrapTrackedExpressions = false;
|
|
22754
|
-
const valueExpr = lowerDomExpression(binding.expr, ctx, containingRegion, {
|
|
22755
|
-
skipHookAccessors: true,
|
|
22756
|
-
skipRegionRootOverride: true
|
|
22757
|
-
});
|
|
22758
|
-
ctx.wrapTrackedExpressions = prevWrapTracked;
|
|
22759
|
-
const eventParam = t2.identifier("_e");
|
|
22760
|
-
const isFn = t2.isArrowFunctionExpression(valueExpr) || t2.isFunctionExpression(valueExpr);
|
|
22761
|
-
const ensureHandlerParam = (fn) => {
|
|
22762
|
-
if (t2.isArrowFunctionExpression(fn)) {
|
|
22763
|
-
if (fn.params.length > 0) return fn;
|
|
22764
|
-
return t2.arrowFunctionExpression([eventParam], fn.body, fn.async);
|
|
22765
|
-
}
|
|
22766
|
-
if (t2.isFunctionExpression(fn)) {
|
|
22767
|
-
if (fn.params.length > 0) return fn;
|
|
22768
|
-
return t2.functionExpression(fn.id, [eventParam], fn.body, fn.generator, fn.async);
|
|
22769
|
-
}
|
|
22770
|
-
return t2.arrowFunctionExpression(
|
|
22771
|
-
[eventParam],
|
|
22772
|
-
t2.callExpression(fn, [eventParam])
|
|
22773
|
-
);
|
|
22774
|
-
};
|
|
22775
|
-
const handlerExpr = !isFn && shouldWrapHandler ? t2.arrowFunctionExpression([], valueExpr) : ensureHandlerParam(valueExpr);
|
|
22776
22751
|
const eventName = binding.name;
|
|
22777
22752
|
const hasEventOptions = binding.eventOptions && (binding.eventOptions.capture || binding.eventOptions.passive || binding.eventOptions.once);
|
|
22778
22753
|
const isDelegated = DelegatedEvents.has(eventName) && !hasEventOptions;
|
|
22779
|
-
const
|
|
22780
|
-
if (
|
|
22754
|
+
const hirDataBinding = isDelegated && binding.expr ? extractDelegatedEventDataFromHIR(binding.expr, ctx) : null;
|
|
22755
|
+
if (hirDataBinding) {
|
|
22781
22756
|
ctx.delegatedEventsUsed?.add(eventName);
|
|
22782
|
-
const
|
|
22783
|
-
const
|
|
22784
|
-
|
|
22785
|
-
|
|
22786
|
-
|
|
22787
|
-
|
|
22788
|
-
|
|
22789
|
-
const
|
|
22790
|
-
|
|
22791
|
-
|
|
22792
|
-
|
|
22793
|
-
let handlerToAssign = handlerIsCallableExpr ? handlerForDelegate : t2.arrowFunctionExpression([eventParam], handlerForDelegate);
|
|
22794
|
-
if (dataForDelegate) {
|
|
22795
|
-
let payloadExpr;
|
|
22796
|
-
if (t2.isArrowFunctionExpression(dataForDelegate) && dataForDelegate.params.length === 0) {
|
|
22797
|
-
payloadExpr = t2.isBlockStatement(dataForDelegate.body) ? t2.callExpression(t2.arrowFunctionExpression([], dataForDelegate.body), []) : dataForDelegate.body;
|
|
22798
|
-
} else {
|
|
22799
|
-
payloadExpr = t2.callExpression(dataForDelegate, []);
|
|
22800
|
-
}
|
|
22801
|
-
handlerToAssign = t2.arrowFunctionExpression(
|
|
22802
|
-
[eventParam],
|
|
22803
|
-
t2.callExpression(handlerForDelegate, [payloadExpr])
|
|
22804
|
-
);
|
|
22805
|
-
}
|
|
22757
|
+
const handlerExpr = lowerExpression(hirDataBinding.handler, ctx);
|
|
22758
|
+
const dataExpr = lowerDomExpression(hirDataBinding.data, ctx, containingRegion, {
|
|
22759
|
+
skipHookAccessors: false,
|
|
22760
|
+
skipRegionRootOverride: true
|
|
22761
|
+
});
|
|
22762
|
+
const dataParam = t2.identifier("__data");
|
|
22763
|
+
const eventParam = t2.identifier("_e");
|
|
22764
|
+
const wrappedHandler = t2.arrowFunctionExpression(
|
|
22765
|
+
[dataParam, eventParam],
|
|
22766
|
+
t2.callExpression(handlerExpr, [dataParam])
|
|
22767
|
+
);
|
|
22806
22768
|
statements.push(
|
|
22807
22769
|
t2.expressionStatement(
|
|
22808
22770
|
t2.assignmentExpression(
|
|
22809
22771
|
"=",
|
|
22810
22772
|
t2.memberExpression(targetId, t2.identifier(`$$${eventName}`)),
|
|
22811
|
-
|
|
22773
|
+
wrappedHandler
|
|
22812
22774
|
)
|
|
22813
22775
|
)
|
|
22814
22776
|
);
|
|
22815
|
-
|
|
22777
|
+
const dataGetter = t2.arrowFunctionExpression([], dataExpr);
|
|
22778
|
+
statements.push(
|
|
22779
|
+
t2.expressionStatement(
|
|
22780
|
+
t2.assignmentExpression(
|
|
22781
|
+
"=",
|
|
22782
|
+
t2.memberExpression(targetId, t2.identifier(`$$${eventName}Data`)),
|
|
22783
|
+
dataGetter
|
|
22784
|
+
)
|
|
22785
|
+
)
|
|
22786
|
+
);
|
|
22787
|
+
} else {
|
|
22788
|
+
const shouldWrapHandler = isExpressionReactive(binding.expr, ctx);
|
|
22789
|
+
const prevWrapTracked = ctx.wrapTrackedExpressions;
|
|
22790
|
+
ctx.wrapTrackedExpressions = false;
|
|
22791
|
+
const valueExpr = lowerDomExpression(binding.expr, ctx, containingRegion, {
|
|
22792
|
+
skipHookAccessors: true,
|
|
22793
|
+
skipRegionRootOverride: true
|
|
22794
|
+
});
|
|
22795
|
+
ctx.wrapTrackedExpressions = prevWrapTracked;
|
|
22796
|
+
const eventParam = t2.identifier("_e");
|
|
22797
|
+
const isFn = t2.isArrowFunctionExpression(valueExpr) || t2.isFunctionExpression(valueExpr);
|
|
22798
|
+
const ensureHandlerParam = (fn) => {
|
|
22799
|
+
if (t2.isArrowFunctionExpression(fn)) {
|
|
22800
|
+
if (fn.params.length > 0) return fn;
|
|
22801
|
+
return t2.arrowFunctionExpression([eventParam], fn.body, fn.async);
|
|
22802
|
+
}
|
|
22803
|
+
if (t2.isFunctionExpression(fn)) {
|
|
22804
|
+
if (fn.params.length > 0) return fn;
|
|
22805
|
+
return t2.functionExpression(fn.id, [eventParam], fn.body, fn.generator, fn.async);
|
|
22806
|
+
}
|
|
22807
|
+
return t2.arrowFunctionExpression(
|
|
22808
|
+
[eventParam],
|
|
22809
|
+
t2.callExpression(fn, [eventParam])
|
|
22810
|
+
);
|
|
22811
|
+
};
|
|
22812
|
+
const handlerExpr = !isFn && shouldWrapHandler ? t2.arrowFunctionExpression([], valueExpr) : ensureHandlerParam(valueExpr);
|
|
22813
|
+
const dataBinding = isDelegated && !shouldWrapHandler ? extractDelegatedEventData(valueExpr, t2) : null;
|
|
22814
|
+
if (isDelegated) {
|
|
22815
|
+
ctx.delegatedEventsUsed?.add(eventName);
|
|
22816
|
+
const finalHandler = !isFn && shouldWrapHandler ? t2.arrowFunctionExpression([eventParam], t2.callExpression(valueExpr, [eventParam])) : handlerExpr;
|
|
22817
|
+
const normalizeHandler = (expr) => {
|
|
22818
|
+
if (t2.isCallExpression(expr) && (t2.isIdentifier(expr.callee) || t2.isMemberExpression(expr.callee))) {
|
|
22819
|
+
return expr.callee;
|
|
22820
|
+
}
|
|
22821
|
+
return expr;
|
|
22822
|
+
};
|
|
22823
|
+
const normalizedDataHandler = dataBinding !== null ? normalizeHandler(
|
|
22824
|
+
dataBinding?.handler ?? handlerExpr
|
|
22825
|
+
) : null;
|
|
22826
|
+
const dataForDelegate = dataBinding?.data && (t2.isArrowFunctionExpression(dataBinding.data) || t2.isFunctionExpression(dataBinding.data) ? dataBinding.data : t2.arrowFunctionExpression([], dataBinding.data));
|
|
22827
|
+
const handlerForDelegate = normalizedDataHandler ?? (dataBinding ? normalizeHandler(handlerExpr) : finalHandler);
|
|
22828
|
+
const handlerIsCallableExpr = t2.isArrowFunctionExpression(handlerForDelegate) || t2.isFunctionExpression(handlerForDelegate) || t2.isIdentifier(handlerForDelegate) || t2.isMemberExpression(handlerForDelegate);
|
|
22829
|
+
let handlerToAssign = handlerIsCallableExpr ? handlerForDelegate : t2.arrowFunctionExpression([eventParam], handlerForDelegate);
|
|
22830
|
+
if (dataForDelegate) {
|
|
22831
|
+
let payloadExpr;
|
|
22832
|
+
if (t2.isArrowFunctionExpression(dataForDelegate) && dataForDelegate.params.length === 0) {
|
|
22833
|
+
payloadExpr = t2.isBlockStatement(dataForDelegate.body) ? t2.callExpression(t2.arrowFunctionExpression([], dataForDelegate.body), []) : dataForDelegate.body;
|
|
22834
|
+
} else {
|
|
22835
|
+
payloadExpr = t2.callExpression(dataForDelegate, []);
|
|
22836
|
+
}
|
|
22837
|
+
handlerToAssign = t2.arrowFunctionExpression(
|
|
22838
|
+
[eventParam],
|
|
22839
|
+
t2.callExpression(handlerForDelegate, [payloadExpr])
|
|
22840
|
+
);
|
|
22841
|
+
}
|
|
22816
22842
|
statements.push(
|
|
22817
22843
|
t2.expressionStatement(
|
|
22818
22844
|
t2.assignmentExpression(
|
|
22819
22845
|
"=",
|
|
22820
|
-
t2.memberExpression(targetId, t2.identifier(`$$${eventName}
|
|
22821
|
-
|
|
22846
|
+
t2.memberExpression(targetId, t2.identifier(`$$${eventName}`)),
|
|
22847
|
+
handlerToAssign
|
|
22822
22848
|
)
|
|
22823
22849
|
)
|
|
22824
22850
|
);
|
|
22825
|
-
|
|
22826
|
-
|
|
22827
|
-
|
|
22828
|
-
|
|
22829
|
-
|
|
22830
|
-
|
|
22831
|
-
|
|
22832
|
-
|
|
22833
|
-
|
|
22834
|
-
|
|
22835
|
-
if (hasEventOptions && binding.eventOptions) {
|
|
22836
|
-
const optionProps = [];
|
|
22837
|
-
if (binding.eventOptions.capture) {
|
|
22838
|
-
optionProps.push(t2.objectProperty(t2.identifier("capture"), t2.booleanLiteral(true)));
|
|
22839
|
-
}
|
|
22840
|
-
if (binding.eventOptions.passive) {
|
|
22841
|
-
optionProps.push(t2.objectProperty(t2.identifier("passive"), t2.booleanLiteral(true)));
|
|
22851
|
+
if (dataForDelegate) {
|
|
22852
|
+
statements.push(
|
|
22853
|
+
t2.expressionStatement(
|
|
22854
|
+
t2.assignmentExpression(
|
|
22855
|
+
"=",
|
|
22856
|
+
t2.memberExpression(targetId, t2.identifier(`$$${eventName}Data`)),
|
|
22857
|
+
dataForDelegate
|
|
22858
|
+
)
|
|
22859
|
+
)
|
|
22860
|
+
);
|
|
22842
22861
|
}
|
|
22843
|
-
|
|
22844
|
-
|
|
22862
|
+
} else {
|
|
22863
|
+
ctx.helpersUsed.add("bindEvent");
|
|
22864
|
+
ctx.helpersUsed.add("onDestroy");
|
|
22865
|
+
const cleanupId = genTemp(ctx, "evt");
|
|
22866
|
+
const args = [
|
|
22867
|
+
targetId,
|
|
22868
|
+
t2.stringLiteral(eventName),
|
|
22869
|
+
handlerExpr
|
|
22870
|
+
];
|
|
22871
|
+
if (hasEventOptions && binding.eventOptions) {
|
|
22872
|
+
const optionProps = [];
|
|
22873
|
+
if (binding.eventOptions.capture) {
|
|
22874
|
+
optionProps.push(t2.objectProperty(t2.identifier("capture"), t2.booleanLiteral(true)));
|
|
22875
|
+
}
|
|
22876
|
+
if (binding.eventOptions.passive) {
|
|
22877
|
+
optionProps.push(t2.objectProperty(t2.identifier("passive"), t2.booleanLiteral(true)));
|
|
22878
|
+
}
|
|
22879
|
+
if (binding.eventOptions.once) {
|
|
22880
|
+
optionProps.push(t2.objectProperty(t2.identifier("once"), t2.booleanLiteral(true)));
|
|
22881
|
+
}
|
|
22882
|
+
args.push(t2.objectExpression(optionProps));
|
|
22845
22883
|
}
|
|
22846
|
-
|
|
22847
|
-
|
|
22848
|
-
|
|
22849
|
-
|
|
22850
|
-
|
|
22851
|
-
|
|
22852
|
-
|
|
22884
|
+
statements.push(
|
|
22885
|
+
t2.variableDeclaration("const", [
|
|
22886
|
+
t2.variableDeclarator(
|
|
22887
|
+
cleanupId,
|
|
22888
|
+
t2.callExpression(t2.identifier(RUNTIME_ALIASES.bindEvent), args)
|
|
22889
|
+
)
|
|
22890
|
+
]),
|
|
22891
|
+
t2.expressionStatement(
|
|
22892
|
+
t2.callExpression(t2.identifier(RUNTIME_ALIASES.onDestroy), [cleanupId])
|
|
22853
22893
|
)
|
|
22854
|
-
|
|
22855
|
-
|
|
22856
|
-
t2.callExpression(t2.identifier(RUNTIME_ALIASES.onDestroy), [cleanupId])
|
|
22857
|
-
)
|
|
22858
|
-
);
|
|
22894
|
+
);
|
|
22895
|
+
}
|
|
22859
22896
|
}
|
|
22860
22897
|
} else if (binding.type === "attr" && binding.name) {
|
|
22861
22898
|
const attrName = binding.name;
|
|
@@ -23242,6 +23279,82 @@ function extractDelegatedEventData(expr, t2) {
|
|
|
23242
23279
|
data: dataArg && t2.isExpression(dataArg) ? dataArg : void 0
|
|
23243
23280
|
};
|
|
23244
23281
|
}
|
|
23282
|
+
function extractDelegatedEventDataFromHIR(expr, ctx) {
|
|
23283
|
+
if (expr.kind !== "ArrowFunction" && expr.kind !== "FunctionExpression") {
|
|
23284
|
+
return null;
|
|
23285
|
+
}
|
|
23286
|
+
let bodyExpr = null;
|
|
23287
|
+
if (expr.kind === "ArrowFunction") {
|
|
23288
|
+
if (expr.isExpression && !Array.isArray(expr.body)) {
|
|
23289
|
+
bodyExpr = expr.body;
|
|
23290
|
+
}
|
|
23291
|
+
}
|
|
23292
|
+
if (!bodyExpr || bodyExpr.kind !== "CallExpression") {
|
|
23293
|
+
return null;
|
|
23294
|
+
}
|
|
23295
|
+
const callee = bodyExpr.callee;
|
|
23296
|
+
if (callee.kind !== "Identifier") {
|
|
23297
|
+
return null;
|
|
23298
|
+
}
|
|
23299
|
+
if (bodyExpr.arguments.length !== 1) {
|
|
23300
|
+
return null;
|
|
23301
|
+
}
|
|
23302
|
+
if (callee.kind === "Identifier") {
|
|
23303
|
+
const handlerName = deSSAVarName(callee.name);
|
|
23304
|
+
const isTrackedAccessor = ctx.signalVars?.has(handlerName) || ctx.memoVars?.has(handlerName) || ctx.aliasVars?.has(handlerName);
|
|
23305
|
+
if (isTrackedAccessor) {
|
|
23306
|
+
return null;
|
|
23307
|
+
}
|
|
23308
|
+
}
|
|
23309
|
+
const paramNames = new Set(expr.params.map((p) => p.name));
|
|
23310
|
+
const dataExpr = bodyExpr.arguments[0];
|
|
23311
|
+
if (!dataExpr) {
|
|
23312
|
+
return null;
|
|
23313
|
+
}
|
|
23314
|
+
if (hirExpressionUsesIdentifiers(dataExpr, paramNames)) {
|
|
23315
|
+
return null;
|
|
23316
|
+
}
|
|
23317
|
+
return {
|
|
23318
|
+
handler: callee,
|
|
23319
|
+
data: dataExpr
|
|
23320
|
+
};
|
|
23321
|
+
}
|
|
23322
|
+
function hirExpressionUsesIdentifiers(expr, names) {
|
|
23323
|
+
if (expr.kind === "Identifier") {
|
|
23324
|
+
return names.has(deSSAVarName(expr.name));
|
|
23325
|
+
}
|
|
23326
|
+
switch (expr.kind) {
|
|
23327
|
+
case "BinaryExpression":
|
|
23328
|
+
case "LogicalExpression":
|
|
23329
|
+
return hirExpressionUsesIdentifiers(expr.left, names) || hirExpressionUsesIdentifiers(expr.right, names);
|
|
23330
|
+
case "UnaryExpression":
|
|
23331
|
+
return hirExpressionUsesIdentifiers(expr.argument, names);
|
|
23332
|
+
case "ConditionalExpression":
|
|
23333
|
+
return hirExpressionUsesIdentifiers(expr.test, names) || hirExpressionUsesIdentifiers(expr.consequent, names) || hirExpressionUsesIdentifiers(expr.alternate, names);
|
|
23334
|
+
case "CallExpression":
|
|
23335
|
+
case "OptionalCallExpression":
|
|
23336
|
+
return hirExpressionUsesIdentifiers(expr.callee, names) || expr.arguments.some((arg) => hirExpressionUsesIdentifiers(arg, names));
|
|
23337
|
+
case "MemberExpression":
|
|
23338
|
+
case "OptionalMemberExpression":
|
|
23339
|
+
return hirExpressionUsesIdentifiers(expr.object, names) || expr.computed && hirExpressionUsesIdentifiers(expr.property, names);
|
|
23340
|
+
case "ArrayExpression":
|
|
23341
|
+
return expr.elements.some((el) => el && hirExpressionUsesIdentifiers(el, names));
|
|
23342
|
+
case "ObjectExpression":
|
|
23343
|
+
return expr.properties.some((prop) => {
|
|
23344
|
+
if (prop.kind === "SpreadElement") {
|
|
23345
|
+
return hirExpressionUsesIdentifiers(prop.argument, names);
|
|
23346
|
+
}
|
|
23347
|
+
return hirExpressionUsesIdentifiers(prop.key, names) || hirExpressionUsesIdentifiers(prop.value, names);
|
|
23348
|
+
});
|
|
23349
|
+
case "TemplateLiteral":
|
|
23350
|
+
return expr.expressions.some((e) => hirExpressionUsesIdentifiers(e, names));
|
|
23351
|
+
case "ArrowFunction":
|
|
23352
|
+
case "FunctionExpression":
|
|
23353
|
+
return false;
|
|
23354
|
+
default:
|
|
23355
|
+
return false;
|
|
23356
|
+
}
|
|
23357
|
+
}
|
|
23245
23358
|
function getTrackedCallIdentifier(expr, ctx, itemParamName) {
|
|
23246
23359
|
if (ctx.t.isCallExpression(expr) && ctx.t.isIdentifier(expr.callee)) {
|
|
23247
23360
|
if (expr.arguments.length !== 0) return null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fictjs/compiler",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "Babel plugin for Fict Compiler",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@types/babel__helper-plugin-utils": "^7.10.3",
|
|
46
46
|
"@types/babel__traverse": "^7.28.0",
|
|
47
47
|
"tsup": "^8.5.1",
|
|
48
|
-
"@fictjs/runtime": "0.0.
|
|
48
|
+
"@fictjs/runtime": "0.0.12"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|