@barefootjs/jsx 0.39.4 → 0.39.6
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/adapters/array-method.d.ts +93 -0
- package/dist/adapters/array-method.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +135 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +84 -9
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +126 -0
- package/dist/adapters/callback-method.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +99 -0
- package/dist/adapters/component-props.d.ts.map +1 -0
- package/dist/adapters/component-tree.d.ts +25 -0
- package/dist/adapters/component-tree.d.ts.map +1 -0
- package/dist/adapters/filter-capture-collisions.d.ts +17 -0
- package/dist/adapters/filter-capture-collisions.d.ts.map +1 -0
- package/dist/adapters/literal-value.d.ts +22 -0
- package/dist/adapters/literal-value.d.ts.map +1 -0
- package/dist/adapters/loop-plan.d.ts +53 -0
- package/dist/adapters/loop-plan.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +8 -0
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/prop-classes.d.ts +37 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -0
- package/dist/adapters/template-name-collisions.d.ts +21 -0
- package/dist/adapters/template-name-collisions.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +48 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +20 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2077 -1000
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +8 -0
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts +3 -2
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +39 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +465 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +31 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +10 -0
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
- package/src/__tests__/action-accessor.test.ts +6 -24
- package/src/__tests__/array-method-catalogue.test.ts +99 -0
- package/src/__tests__/attr-plan.test.ts +121 -0
- package/src/__tests__/binding-scope-ratchet.test.ts +13 -13
- package/src/__tests__/binding-scope.test.ts +11 -0
- package/src/__tests__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +189 -0
- package/src/__tests__/compile-without-node-process.test.ts +78 -0
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/doc-examples.test.ts +1 -0
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +104 -0
- package/src/__tests__/map-call-decisions.test.ts +521 -0
- package/src/__tests__/nested-row-shadowing.test.ts +57 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/__tests__/rename-free-identifiers.test.ts +26 -0
- package/src/__tests__/template-name-collisions.test.ts +81 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +200 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +301 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/filter-capture-collisions.ts +29 -0
- package/src/adapters/literal-value.ts +36 -0
- package/src/adapters/loop-plan.ts +88 -0
- package/src/adapters/parsed-expr-emitter.ts +18 -0
- package/src/adapters/prop-classes.ts +73 -0
- package/src/adapters/template-name-collisions.ts +120 -0
- package/src/analyzer-context.ts +15 -1
- package/src/expression-parser.ts +569 -374
- package/src/index.ts +58 -3
- package/src/ir-to-client-js/collect-elements.ts +11 -7
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +31 -10
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +12 -5
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +2 -2
- package/src/ir-to-client-js/control-flow/shared.ts +4 -1
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/ir-to-client-js/html-template.ts +13 -7
- package/src/ir-to-client-js/index.ts +7 -2
- package/src/ir-to-client-js/utils.ts +81 -7
- package/src/jsx-to-ir.ts +1328 -991
- package/src/nullable-signal-attr.ts +116 -6
- package/src/scope/binding-scope.ts +19 -0
- package/src/types.ts +7 -0
package/dist/index.js
CHANGED
|
@@ -501,6 +501,23 @@ var CALLBACK_METHODS = new Set([
|
|
|
501
501
|
"reduceRight",
|
|
502
502
|
"flatMap"
|
|
503
503
|
]);
|
|
504
|
+
function evaluatorBoundParamCount(method, scope) {
|
|
505
|
+
if (method === "sort" || method === "toSorted" || method === "reduce" || method === "reduceRight")
|
|
506
|
+
return 2;
|
|
507
|
+
return scope === "evaluator" && (method === "map" || method === "filter") ? 2 : 1;
|
|
508
|
+
}
|
|
509
|
+
function unboundCallbackParamRead(method, arrow, scope = "template") {
|
|
510
|
+
const extra = arrow.params.slice(evaluatorBoundParamCount(method, scope));
|
|
511
|
+
if (extra.length === 0)
|
|
512
|
+
return null;
|
|
513
|
+
const free = freeIdentifiers(arrow.body);
|
|
514
|
+
if (free === null)
|
|
515
|
+
return extra[0];
|
|
516
|
+
return extra.find((name) => free.has(name)) ?? null;
|
|
517
|
+
}
|
|
518
|
+
function unboundCallbackParamReason(method, name) {
|
|
519
|
+
return `'.${method}()' callback reads '${name}', its index or array parameter, which a template evaluator does not bind. Pre-compute the value, or add /* @client */ for client-only (no SSR).`;
|
|
520
|
+
}
|
|
504
521
|
function asCallbackMethodCall(expr) {
|
|
505
522
|
if (expr.kind !== "call")
|
|
506
523
|
return null;
|
|
@@ -545,134 +562,20 @@ function convertNode(node, raw) {
|
|
|
545
562
|
if (ts2.isCallExpression(node)) {
|
|
546
563
|
const callee = convertNode(node.expression, raw);
|
|
547
564
|
const args = node.arguments.map((arg) => convertNode(arg, raw));
|
|
548
|
-
if (callee.kind === "member" &&
|
|
565
|
+
if (callee.kind === "member" && callee.optional) {
|
|
549
566
|
return {
|
|
550
|
-
kind: "
|
|
551
|
-
|
|
552
|
-
|
|
567
|
+
kind: "conditional",
|
|
568
|
+
test: {
|
|
569
|
+
kind: "logical",
|
|
570
|
+
op: "||",
|
|
571
|
+
left: { kind: "binary", op: "===", left: callee.object, right: { kind: "literal", value: null, literalType: "null" } },
|
|
572
|
+
right: { kind: "binary", op: "===", left: callee.object, right: { kind: "identifier", name: "undefined" } }
|
|
573
|
+
},
|
|
574
|
+
consequent: { kind: "identifier", name: "undefined" },
|
|
575
|
+
alternate: convertCallExpression(node, raw, { ...callee, optional: false }, args)
|
|
553
576
|
};
|
|
554
577
|
}
|
|
555
|
-
|
|
556
|
-
if (callee.property === "join") {
|
|
557
|
-
return { kind: "array-method", method: "join", object: callee.object, args };
|
|
558
|
-
}
|
|
559
|
-
if (callee.property === "includes" && args.length === 1) {
|
|
560
|
-
return { kind: "array-method", method: "includes", object: callee.object, args };
|
|
561
|
-
}
|
|
562
|
-
if ((callee.property === "indexOf" || callee.property === "lastIndexOf") && args.length === 1) {
|
|
563
|
-
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
564
|
-
}
|
|
565
|
-
if (callee.property === "at") {
|
|
566
|
-
return { kind: "array-method", method: "at", object: callee.object, args };
|
|
567
|
-
}
|
|
568
|
-
if (callee.property === "concat" && args.length <= 1) {
|
|
569
|
-
return { kind: "array-method", method: "concat", object: callee.object, args };
|
|
570
|
-
}
|
|
571
|
-
if (callee.property === "slice") {
|
|
572
|
-
return { kind: "array-method", method: "slice", object: callee.object, args };
|
|
573
|
-
}
|
|
574
|
-
if (callee.property === "reverse" || callee.property === "toReversed") {
|
|
575
|
-
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
576
|
-
}
|
|
577
|
-
if (callee.property === "flat") {
|
|
578
|
-
const depthNode = node.arguments[0];
|
|
579
|
-
let flatDepth;
|
|
580
|
-
let depthExpr;
|
|
581
|
-
if (depthNode === undefined) {
|
|
582
|
-
flatDepth = 1;
|
|
583
|
-
} else if (ts2.isIdentifier(depthNode) && depthNode.text === "Infinity") {
|
|
584
|
-
flatDepth = "infinity";
|
|
585
|
-
} else {
|
|
586
|
-
let n;
|
|
587
|
-
if (ts2.isNumericLiteral(depthNode)) {
|
|
588
|
-
n = Number(depthNode.text);
|
|
589
|
-
} else if (ts2.isPrefixUnaryExpression(depthNode) && depthNode.operator === ts2.SyntaxKind.MinusToken && ts2.isNumericLiteral(depthNode.operand)) {
|
|
590
|
-
n = -Number(depthNode.operand.text);
|
|
591
|
-
}
|
|
592
|
-
if (n === undefined || Number.isNaN(n)) {
|
|
593
|
-
const parsedDepth = convertNode(depthNode, raw);
|
|
594
|
-
if (checkSupport(parsedDepth, "rendered", []).supported) {
|
|
595
|
-
depthExpr = parsedDepth;
|
|
596
|
-
flatDepth = 1;
|
|
597
|
-
} else {
|
|
598
|
-
return {
|
|
599
|
-
kind: "unsupported",
|
|
600
|
-
raw,
|
|
601
|
-
reason: `\`.flat(depth)\` needs a literal integer, \`Infinity\`, or a supported dynamic depth expression — this depth can't be resolved. Use a literal depth, a supported expression (prop/signal/arithmetic), or pre-compute the value before the template.`
|
|
602
|
-
};
|
|
603
|
-
}
|
|
604
|
-
} else {
|
|
605
|
-
const truncated = Math.trunc(n);
|
|
606
|
-
flatDepth = truncated < 0 ? 0 : truncated;
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
return depthExpr !== undefined ? { kind: "array-method", method: "flat", object: callee.object, args: [], flatDepth, depthExpr } : { kind: "array-method", method: "flat", object: callee.object, args: [], flatDepth };
|
|
610
|
-
}
|
|
611
|
-
if (callee.property === "toLowerCase") {
|
|
612
|
-
return { kind: "array-method", method: "toLowerCase", object: callee.object, args };
|
|
613
|
-
}
|
|
614
|
-
if (callee.property === "toUpperCase") {
|
|
615
|
-
return { kind: "array-method", method: "toUpperCase", object: callee.object, args };
|
|
616
|
-
}
|
|
617
|
-
if (callee.property === "trim") {
|
|
618
|
-
return { kind: "array-method", method: "trim", object: callee.object, args };
|
|
619
|
-
}
|
|
620
|
-
if (callee.property === "trimStart" || callee.property === "trimEnd") {
|
|
621
|
-
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
622
|
-
}
|
|
623
|
-
if (callee.property === "toFixed") {
|
|
624
|
-
return { kind: "array-method", method: "toFixed", object: callee.object, args };
|
|
625
|
-
}
|
|
626
|
-
if (callee.property === "split") {
|
|
627
|
-
return { kind: "array-method", method: "split", object: callee.object, args };
|
|
628
|
-
}
|
|
629
|
-
if (LOWERED_ARRAY_METHODS.has(callee.property)) {
|
|
630
|
-
const argName = callee.property === "concat" ? "other" : "x";
|
|
631
|
-
const detail = callee.property === "concat" ? "the variadic `.concat(a, b, …)` form" : `\`.${callee.property}(…)\` with ${args.length} argument(s)`;
|
|
632
|
-
return {
|
|
633
|
-
kind: "unsupported",
|
|
634
|
-
raw,
|
|
635
|
-
reason: `${detail} is not yet lowered to the Go/Mojo template adapters. Use the single-argument \`.${callee.property}(${argName})\` form, or pre-compute the value before the template.`
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
if (callee.property === "startsWith" || callee.property === "endsWith") {
|
|
639
|
-
if (args.length === 0) {
|
|
640
|
-
return {
|
|
641
|
-
kind: "unsupported",
|
|
642
|
-
raw,
|
|
643
|
-
reason: `\`.${callee.property}()\` with no search string is not lowered — JS coerces the missing argument to the string "undefined", a degenerate result. Pass an explicit search string, or pre-compute the value before the template.`
|
|
644
|
-
};
|
|
645
|
-
}
|
|
646
|
-
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
647
|
-
}
|
|
648
|
-
if (callee.property === "replace" || callee.property === "replaceAll") {
|
|
649
|
-
const method = callee.property;
|
|
650
|
-
if (args.length < 2) {
|
|
651
|
-
return {
|
|
652
|
-
kind: "unsupported",
|
|
653
|
-
raw,
|
|
654
|
-
reason: `\`.${method}(${args.length === 0 ? "" : "pattern"})\` needs both a pattern and a replacement — JS coerces the missing argument to the string "undefined", a degenerate result. Pass both arguments, or pre-compute the value before the template.`
|
|
655
|
-
};
|
|
656
|
-
}
|
|
657
|
-
const patternNode = node.arguments[0];
|
|
658
|
-
if (patternNode && ts2.isRegularExpressionLiteral(patternNode)) {
|
|
659
|
-
return { kind: "array-method", method, object: callee.object, args };
|
|
660
|
-
}
|
|
661
|
-
const badArg = args[0].kind === "unsupported" || args[0].kind === "object-literal" ? args[0] : args[1].kind === "unsupported" || args[1].kind === "object-literal" ? args[1] : undefined;
|
|
662
|
-
if (badArg) {
|
|
663
|
-
const reason = badArg.kind === "unsupported" ? badArg.reason : "Unsupported syntax: ObjectLiteralExpression";
|
|
664
|
-
return { kind: "unsupported", raw, reason };
|
|
665
|
-
}
|
|
666
|
-
return { kind: "array-method", method, object: callee.object, args };
|
|
667
|
-
}
|
|
668
|
-
if (callee.property === "repeat") {
|
|
669
|
-
return { kind: "array-method", method: "repeat", object: callee.object, args };
|
|
670
|
-
}
|
|
671
|
-
if (callee.property === "padStart" || callee.property === "padEnd") {
|
|
672
|
-
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
return { kind: "call", callee, args };
|
|
578
|
+
return convertCallExpression(node, raw, callee, args);
|
|
676
579
|
}
|
|
677
580
|
if (ts2.isArrayLiteralExpression(node)) {
|
|
678
581
|
const elements = node.elements.map((el) => convertNode(el, raw));
|
|
@@ -1375,7 +1278,35 @@ function isSupported(expr, opts) {
|
|
|
1375
1278
|
function isSupportedValue(expr, opts) {
|
|
1376
1279
|
return checkSupport(expr, "value", opts?.loweringMatchers ?? []);
|
|
1377
1280
|
}
|
|
1378
|
-
function
|
|
1281
|
+
function checkCallbackMethodSupport(cb, pos, matchers, scope) {
|
|
1282
|
+
const objSupport = checkSupport(cb.object, pos, matchers, scope);
|
|
1283
|
+
if (!objSupport.supported)
|
|
1284
|
+
return objSupport;
|
|
1285
|
+
const unbound = unboundCallbackParamRead(cb.method, cb.arrow, scope);
|
|
1286
|
+
if (unbound !== null) {
|
|
1287
|
+
return {
|
|
1288
|
+
supported: false,
|
|
1289
|
+
level: "L5_UNSUPPORTED",
|
|
1290
|
+
selfContained: true,
|
|
1291
|
+
reason: unboundCallbackParamReason(cb.method, unbound)
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
const bodySupport = checkSupport(cb.arrow.body, pos, matchers, "evaluator");
|
|
1295
|
+
if (!bodySupport.supported) {
|
|
1296
|
+
return {
|
|
1297
|
+
supported: false,
|
|
1298
|
+
level: "L5_UNSUPPORTED",
|
|
1299
|
+
reason: `Higher-order method '.${cb.method}()' with complex callback. ${bodySupport.reason || "Simplify the callback."}`
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
for (const rest of cb.args) {
|
|
1303
|
+
const restSupport = checkSupport(rest, pos, matchers, scope);
|
|
1304
|
+
if (!restSupport.supported)
|
|
1305
|
+
return restSupport;
|
|
1306
|
+
}
|
|
1307
|
+
return { supported: true, level: "L5" };
|
|
1308
|
+
}
|
|
1309
|
+
function checkSupport(expr, pos, matchers, scope = "template") {
|
|
1379
1310
|
switch (expr.kind) {
|
|
1380
1311
|
case "unsupported":
|
|
1381
1312
|
return { supported: false, reason: expr.reason };
|
|
@@ -1384,7 +1315,7 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1384
1315
|
return { supported: false, reason: "Unsupported syntax: ObjectLiteralExpression" };
|
|
1385
1316
|
}
|
|
1386
1317
|
for (const prop of expr.properties) {
|
|
1387
|
-
const propSupport = checkSupport(prop.kind === "spread" ? prop.expr : prop.value, pos, matchers);
|
|
1318
|
+
const propSupport = checkSupport(prop.kind === "spread" ? prop.expr : prop.value, pos, matchers, scope);
|
|
1388
1319
|
if (!propSupport.supported)
|
|
1389
1320
|
return propSupport;
|
|
1390
1321
|
}
|
|
@@ -1399,7 +1330,7 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1399
1330
|
return { supported: false, reason: "Standalone arrow functions / regex literals are not supported" };
|
|
1400
1331
|
case "array-literal": {
|
|
1401
1332
|
for (const el of expr.elements) {
|
|
1402
|
-
const elSupport = checkSupport(el, pos, matchers);
|
|
1333
|
+
const elSupport = checkSupport(el, pos, matchers, scope);
|
|
1403
1334
|
if (!elSupport.supported)
|
|
1404
1335
|
return elSupport;
|
|
1405
1336
|
}
|
|
@@ -1412,16 +1343,16 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1412
1343
|
reason: `String.prototype.${expr.method} supports only a string pattern + string replacement (the regex form is deferred); use a string pattern or wrap the expression in /* @client */`
|
|
1413
1344
|
};
|
|
1414
1345
|
}
|
|
1415
|
-
const objSupport = checkSupport(expr.object, pos, matchers);
|
|
1346
|
+
const objSupport = checkSupport(expr.object, pos, matchers, scope);
|
|
1416
1347
|
if (!objSupport.supported)
|
|
1417
1348
|
return objSupport;
|
|
1418
1349
|
for (const arg of expr.args) {
|
|
1419
|
-
const argSupport = checkSupport(arg, pos, matchers);
|
|
1350
|
+
const argSupport = checkSupport(arg, pos, matchers, scope);
|
|
1420
1351
|
if (!argSupport.supported)
|
|
1421
1352
|
return argSupport;
|
|
1422
1353
|
}
|
|
1423
1354
|
if (expr.method === "flat" && expr.depthExpr) {
|
|
1424
|
-
const depthSupport = checkSupport(expr.depthExpr, pos, matchers);
|
|
1355
|
+
const depthSupport = checkSupport(expr.depthExpr, pos, matchers, scope);
|
|
1425
1356
|
if (!depthSupport.supported)
|
|
1426
1357
|
return depthSupport;
|
|
1427
1358
|
}
|
|
@@ -1433,33 +1364,16 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1433
1364
|
if (!node)
|
|
1434
1365
|
continue;
|
|
1435
1366
|
for (const child of loweringNodeChildren(node)) {
|
|
1436
|
-
const childSupport = checkSupport(child, pos, matchers);
|
|
1367
|
+
const childSupport = checkSupport(child, pos, matchers, scope);
|
|
1437
1368
|
if (!childSupport.supported)
|
|
1438
1369
|
return childSupport;
|
|
1439
1370
|
}
|
|
1440
1371
|
return { supported: true, level: "L2" };
|
|
1441
1372
|
}
|
|
1442
1373
|
const cb = asCallbackMethodCall(expr);
|
|
1443
|
-
if (cb)
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
return objSupport;
|
|
1447
|
-
const bodySupport = checkSupport(cb.arrow.body, pos, matchers);
|
|
1448
|
-
if (!bodySupport.supported) {
|
|
1449
|
-
return {
|
|
1450
|
-
supported: false,
|
|
1451
|
-
level: "L5_UNSUPPORTED",
|
|
1452
|
-
reason: `Higher-order method '.${cb.method}()' with complex callback. ${bodySupport.reason || "Simplify the callback."}`
|
|
1453
|
-
};
|
|
1454
|
-
}
|
|
1455
|
-
for (const rest of cb.args) {
|
|
1456
|
-
const restSupport = checkSupport(rest, pos, matchers);
|
|
1457
|
-
if (!restSupport.supported)
|
|
1458
|
-
return restSupport;
|
|
1459
|
-
}
|
|
1460
|
-
return { supported: true, level: "L5" };
|
|
1461
|
-
}
|
|
1462
|
-
const calleeSupport = checkSupport(expr.callee, pos, matchers);
|
|
1374
|
+
if (cb)
|
|
1375
|
+
return checkCallbackMethodSupport(cb, pos, matchers, scope);
|
|
1376
|
+
const calleeSupport = checkSupport(expr.callee, pos, matchers, scope);
|
|
1463
1377
|
if (!calleeSupport.supported) {
|
|
1464
1378
|
return calleeSupport;
|
|
1465
1379
|
}
|
|
@@ -1478,7 +1392,7 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1478
1392
|
return { supported: true, level: "L1" };
|
|
1479
1393
|
}
|
|
1480
1394
|
for (const arg of expr.args) {
|
|
1481
|
-
const argSupport = checkSupport(arg, pos, matchers);
|
|
1395
|
+
const argSupport = checkSupport(arg, pos, matchers, scope);
|
|
1482
1396
|
if (!argSupport.supported) {
|
|
1483
1397
|
return argSupport;
|
|
1484
1398
|
}
|
|
@@ -1486,7 +1400,7 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1486
1400
|
return { supported: true, level: "L2" };
|
|
1487
1401
|
}
|
|
1488
1402
|
case "member": {
|
|
1489
|
-
const objSupport = checkSupport(expr.object, pos, matchers);
|
|
1403
|
+
const objSupport = checkSupport(expr.object, pos, matchers, scope);
|
|
1490
1404
|
if (!objSupport.supported) {
|
|
1491
1405
|
return objSupport;
|
|
1492
1406
|
}
|
|
@@ -1496,19 +1410,19 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1496
1410
|
return { supported: true, level: "L2" };
|
|
1497
1411
|
}
|
|
1498
1412
|
case "index-access": {
|
|
1499
|
-
const objSupport = checkSupport(expr.object, pos, matchers);
|
|
1413
|
+
const objSupport = checkSupport(expr.object, pos, matchers, scope);
|
|
1500
1414
|
if (!objSupport.supported)
|
|
1501
1415
|
return objSupport;
|
|
1502
|
-
const indexSupport = checkSupport(expr.index, pos, matchers);
|
|
1416
|
+
const indexSupport = checkSupport(expr.index, pos, matchers, scope);
|
|
1503
1417
|
if (!indexSupport.supported)
|
|
1504
1418
|
return indexSupport;
|
|
1505
1419
|
return { supported: true, level: "L2" };
|
|
1506
1420
|
}
|
|
1507
1421
|
case "binary": {
|
|
1508
|
-
const leftSupport = checkSupport(expr.left, pos, matchers);
|
|
1422
|
+
const leftSupport = checkSupport(expr.left, pos, matchers, scope);
|
|
1509
1423
|
if (!leftSupport.supported)
|
|
1510
1424
|
return leftSupport;
|
|
1511
|
-
const rightSupport = checkSupport(expr.right, pos, matchers);
|
|
1425
|
+
const rightSupport = checkSupport(expr.right, pos, matchers, scope);
|
|
1512
1426
|
if (!rightSupport.supported)
|
|
1513
1427
|
return rightSupport;
|
|
1514
1428
|
if (["===", "==", "!==", "!=", ">", "<", ">=", "<="].includes(expr.op)) {
|
|
@@ -1520,7 +1434,7 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1520
1434
|
return { supported: false, reason: `Unknown operator: ${expr.op}` };
|
|
1521
1435
|
}
|
|
1522
1436
|
case "unary": {
|
|
1523
|
-
const argSupport = checkSupport(expr.argument, pos, matchers);
|
|
1437
|
+
const argSupport = checkSupport(expr.argument, pos, matchers, scope);
|
|
1524
1438
|
if (!argSupport.supported)
|
|
1525
1439
|
return argSupport;
|
|
1526
1440
|
if (expr.op === "!") {
|
|
@@ -1532,25 +1446,25 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1532
1446
|
return { supported: false, reason: `Unsupported unary operator: ${expr.op}` };
|
|
1533
1447
|
}
|
|
1534
1448
|
case "logical": {
|
|
1535
|
-
const leftSupport = checkSupport(expr.left, pos, matchers);
|
|
1449
|
+
const leftSupport = checkSupport(expr.left, pos, matchers, scope);
|
|
1536
1450
|
if (!leftSupport.supported)
|
|
1537
1451
|
return leftSupport;
|
|
1538
1452
|
if (expr.op === "??" && expr.right.kind === "object-literal" && expr.right.properties.length === 0) {
|
|
1539
1453
|
return { supported: true, level: "L4" };
|
|
1540
1454
|
}
|
|
1541
|
-
const rightSupport = checkSupport(expr.right, pos, matchers);
|
|
1455
|
+
const rightSupport = checkSupport(expr.right, pos, matchers, scope);
|
|
1542
1456
|
if (!rightSupport.supported)
|
|
1543
1457
|
return rightSupport;
|
|
1544
1458
|
return { supported: true, level: "L4" };
|
|
1545
1459
|
}
|
|
1546
1460
|
case "conditional": {
|
|
1547
|
-
const testSupport = checkSupport(expr.test, pos, matchers);
|
|
1461
|
+
const testSupport = checkSupport(expr.test, pos, matchers, scope);
|
|
1548
1462
|
if (!testSupport.supported)
|
|
1549
1463
|
return testSupport;
|
|
1550
|
-
const consSupport = checkSupport(expr.consequent, pos, matchers);
|
|
1464
|
+
const consSupport = checkSupport(expr.consequent, pos, matchers, scope);
|
|
1551
1465
|
if (!consSupport.supported)
|
|
1552
1466
|
return consSupport;
|
|
1553
|
-
const altSupport = checkSupport(expr.alternate, pos, matchers);
|
|
1467
|
+
const altSupport = checkSupport(expr.alternate, pos, matchers, scope);
|
|
1554
1468
|
if (!altSupport.supported)
|
|
1555
1469
|
return altSupport;
|
|
1556
1470
|
return { supported: true, level: "L4" };
|
|
@@ -1558,7 +1472,7 @@ function checkSupport(expr, pos, matchers) {
|
|
|
1558
1472
|
case "template-literal": {
|
|
1559
1473
|
for (const part of expr.parts) {
|
|
1560
1474
|
if (part.type === "expression") {
|
|
1561
|
-
const partSupport = checkSupport(part.expr, pos, matchers);
|
|
1475
|
+
const partSupport = checkSupport(part.expr, pos, matchers, scope);
|
|
1562
1476
|
if (!partSupport.supported)
|
|
1563
1477
|
return partSupport;
|
|
1564
1478
|
}
|
|
@@ -2144,6 +2058,33 @@ function freeVarsInBody(body, params) {
|
|
|
2144
2058
|
visit(body, params);
|
|
2145
2059
|
return [...found].sort();
|
|
2146
2060
|
}
|
|
2061
|
+
function renameFreeIdentifiers(expr, renames) {
|
|
2062
|
+
if (renames.size === 0)
|
|
2063
|
+
return expr;
|
|
2064
|
+
const walk = (value, active) => {
|
|
2065
|
+
if (Array.isArray(value))
|
|
2066
|
+
return value.map((v) => walk(v, active));
|
|
2067
|
+
if (value === null || typeof value !== "object")
|
|
2068
|
+
return value;
|
|
2069
|
+
const node = value;
|
|
2070
|
+
if (node.kind === "identifier" && typeof node.name === "string") {
|
|
2071
|
+
const renamed = active.get(node.name);
|
|
2072
|
+
return renamed === undefined ? node : { ...node, name: renamed };
|
|
2073
|
+
}
|
|
2074
|
+
let inner = active;
|
|
2075
|
+
if (node.kind === "arrow" && Array.isArray(node.params)) {
|
|
2076
|
+
const shadowed = new Map(active);
|
|
2077
|
+
for (const p of node.params)
|
|
2078
|
+
shadowed.delete(p);
|
|
2079
|
+
inner = shadowed;
|
|
2080
|
+
}
|
|
2081
|
+
const out = {};
|
|
2082
|
+
for (const [k, v] of Object.entries(node))
|
|
2083
|
+
out[k] = walk(v, inner);
|
|
2084
|
+
return out;
|
|
2085
|
+
};
|
|
2086
|
+
return walk(expr, renames);
|
|
2087
|
+
}
|
|
2147
2088
|
function freeIdentifiers(expr) {
|
|
2148
2089
|
const free = new Set;
|
|
2149
2090
|
function visit(e, bound) {
|
|
@@ -2244,6 +2185,58 @@ function evalBuiltinCalleeName(callee) {
|
|
|
2244
2185
|
}
|
|
2245
2186
|
return null;
|
|
2246
2187
|
}
|
|
2188
|
+
function hasOpaqueCall(expr) {
|
|
2189
|
+
let found = false;
|
|
2190
|
+
const visit = (node) => {
|
|
2191
|
+
if (found || node === null || typeof node !== "object")
|
|
2192
|
+
return;
|
|
2193
|
+
if (Array.isArray(node)) {
|
|
2194
|
+
node.forEach(visit);
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
const e = node;
|
|
2198
|
+
if (e.kind === "call") {
|
|
2199
|
+
const cb = asCallbackMethodCall(e);
|
|
2200
|
+
if (cb) {
|
|
2201
|
+
visit(cb.object);
|
|
2202
|
+
visit(cb.arrow.body);
|
|
2203
|
+
cb.args.forEach(visit);
|
|
2204
|
+
return;
|
|
2205
|
+
}
|
|
2206
|
+
const getter = e.callee.kind === "identifier" && e.args.length === 0;
|
|
2207
|
+
if (!getter && evalBuiltinCalleeName(e.callee) === null) {
|
|
2208
|
+
found = true;
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
e.args.forEach(visit);
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
for (const value of Object.values(e))
|
|
2215
|
+
visit(value);
|
|
2216
|
+
};
|
|
2217
|
+
visit(expr);
|
|
2218
|
+
return found;
|
|
2219
|
+
}
|
|
2220
|
+
function readsNestedCallbackIndex(expr) {
|
|
2221
|
+
let found = false;
|
|
2222
|
+
const visit = (node) => {
|
|
2223
|
+
if (found || node === null || typeof node !== "object")
|
|
2224
|
+
return;
|
|
2225
|
+
if (Array.isArray(node)) {
|
|
2226
|
+
node.forEach(visit);
|
|
2227
|
+
return;
|
|
2228
|
+
}
|
|
2229
|
+
const cb = node.kind === "call" ? asCallbackMethodCall(node) : null;
|
|
2230
|
+
if (cb && unboundCallbackParamRead(cb.method, cb.arrow) !== null) {
|
|
2231
|
+
found = true;
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
for (const value of Object.values(node))
|
|
2235
|
+
visit(value);
|
|
2236
|
+
};
|
|
2237
|
+
visit(expr);
|
|
2238
|
+
return found;
|
|
2239
|
+
}
|
|
2247
2240
|
function toEvalNode(e) {
|
|
2248
2241
|
switch (e.kind) {
|
|
2249
2242
|
case "literal":
|
|
@@ -2390,6 +2383,136 @@ function identifierPath(callee) {
|
|
|
2390
2383
|
}
|
|
2391
2384
|
return null;
|
|
2392
2385
|
}
|
|
2386
|
+
function convertCallExpression(node, raw, callee, args) {
|
|
2387
|
+
if (callee.kind === "member" && !callee.computed && callee.property === "filter" && args.length === 1 && args[0].kind === "identifier" && args[0].name === "Boolean") {
|
|
2388
|
+
return {
|
|
2389
|
+
kind: "call",
|
|
2390
|
+
callee,
|
|
2391
|
+
args: [{ kind: "arrow", params: ["_"], body: { kind: "identifier", name: "_" } }]
|
|
2392
|
+
};
|
|
2393
|
+
}
|
|
2394
|
+
if (callee.kind === "member" && !callee.computed) {
|
|
2395
|
+
if (callee.property === "join") {
|
|
2396
|
+
return { kind: "array-method", method: "join", object: callee.object, args };
|
|
2397
|
+
}
|
|
2398
|
+
if (callee.property === "includes" && args.length === 1) {
|
|
2399
|
+
return { kind: "array-method", method: "includes", object: callee.object, args };
|
|
2400
|
+
}
|
|
2401
|
+
if ((callee.property === "indexOf" || callee.property === "lastIndexOf") && args.length === 1) {
|
|
2402
|
+
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
2403
|
+
}
|
|
2404
|
+
if (callee.property === "at") {
|
|
2405
|
+
return { kind: "array-method", method: "at", object: callee.object, args };
|
|
2406
|
+
}
|
|
2407
|
+
if (callee.property === "concat" && args.length <= 1) {
|
|
2408
|
+
return { kind: "array-method", method: "concat", object: callee.object, args };
|
|
2409
|
+
}
|
|
2410
|
+
if (callee.property === "slice") {
|
|
2411
|
+
return { kind: "array-method", method: "slice", object: callee.object, args };
|
|
2412
|
+
}
|
|
2413
|
+
if (callee.property === "reverse" || callee.property === "toReversed") {
|
|
2414
|
+
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
2415
|
+
}
|
|
2416
|
+
if (callee.property === "flat") {
|
|
2417
|
+
const depthNode = node.arguments[0];
|
|
2418
|
+
let flatDepth;
|
|
2419
|
+
let depthExpr;
|
|
2420
|
+
if (depthNode === undefined) {
|
|
2421
|
+
flatDepth = 1;
|
|
2422
|
+
} else if (ts2.isIdentifier(depthNode) && depthNode.text === "Infinity") {
|
|
2423
|
+
flatDepth = "infinity";
|
|
2424
|
+
} else {
|
|
2425
|
+
let n;
|
|
2426
|
+
if (ts2.isNumericLiteral(depthNode)) {
|
|
2427
|
+
n = Number(depthNode.text);
|
|
2428
|
+
} else if (ts2.isPrefixUnaryExpression(depthNode) && depthNode.operator === ts2.SyntaxKind.MinusToken && ts2.isNumericLiteral(depthNode.operand)) {
|
|
2429
|
+
n = -Number(depthNode.operand.text);
|
|
2430
|
+
}
|
|
2431
|
+
if (n === undefined || Number.isNaN(n)) {
|
|
2432
|
+
const parsedDepth = convertNode(depthNode, raw);
|
|
2433
|
+
if (checkSupport(parsedDepth, "rendered", []).supported) {
|
|
2434
|
+
depthExpr = parsedDepth;
|
|
2435
|
+
flatDepth = 1;
|
|
2436
|
+
} else {
|
|
2437
|
+
return {
|
|
2438
|
+
kind: "unsupported",
|
|
2439
|
+
raw,
|
|
2440
|
+
reason: `\`.flat(depth)\` needs a literal integer, \`Infinity\`, or a supported dynamic depth expression — this depth can't be resolved. Use a literal depth, a supported expression (prop/signal/arithmetic), or pre-compute the value before the template.`
|
|
2441
|
+
};
|
|
2442
|
+
}
|
|
2443
|
+
} else {
|
|
2444
|
+
const truncated = Math.trunc(n);
|
|
2445
|
+
flatDepth = truncated < 0 ? 0 : truncated;
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
return depthExpr !== undefined ? { kind: "array-method", method: "flat", object: callee.object, args: [], flatDepth, depthExpr } : { kind: "array-method", method: "flat", object: callee.object, args: [], flatDepth };
|
|
2449
|
+
}
|
|
2450
|
+
if (callee.property === "toLowerCase") {
|
|
2451
|
+
return { kind: "array-method", method: "toLowerCase", object: callee.object, args };
|
|
2452
|
+
}
|
|
2453
|
+
if (callee.property === "toUpperCase") {
|
|
2454
|
+
return { kind: "array-method", method: "toUpperCase", object: callee.object, args };
|
|
2455
|
+
}
|
|
2456
|
+
if (callee.property === "trim") {
|
|
2457
|
+
return { kind: "array-method", method: "trim", object: callee.object, args };
|
|
2458
|
+
}
|
|
2459
|
+
if (callee.property === "trimStart" || callee.property === "trimEnd") {
|
|
2460
|
+
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
2461
|
+
}
|
|
2462
|
+
if (callee.property === "toFixed") {
|
|
2463
|
+
return { kind: "array-method", method: "toFixed", object: callee.object, args };
|
|
2464
|
+
}
|
|
2465
|
+
if (callee.property === "split") {
|
|
2466
|
+
return { kind: "array-method", method: "split", object: callee.object, args };
|
|
2467
|
+
}
|
|
2468
|
+
if (LOWERED_ARRAY_METHODS.has(callee.property)) {
|
|
2469
|
+
const argName = callee.property === "concat" ? "other" : "x";
|
|
2470
|
+
const detail = callee.property === "concat" ? "the variadic `.concat(a, b, …)` form" : `\`.${callee.property}(…)\` with ${args.length} argument(s)`;
|
|
2471
|
+
return {
|
|
2472
|
+
kind: "unsupported",
|
|
2473
|
+
raw,
|
|
2474
|
+
reason: `${detail} is not yet lowered to the Go/Mojo template adapters. Use the single-argument \`.${callee.property}(${argName})\` form, or pre-compute the value before the template.`
|
|
2475
|
+
};
|
|
2476
|
+
}
|
|
2477
|
+
if (callee.property === "startsWith" || callee.property === "endsWith") {
|
|
2478
|
+
if (args.length === 0) {
|
|
2479
|
+
return {
|
|
2480
|
+
kind: "unsupported",
|
|
2481
|
+
raw,
|
|
2482
|
+
reason: `\`.${callee.property}()\` with no search string is not lowered — JS coerces the missing argument to the string "undefined", a degenerate result. Pass an explicit search string, or pre-compute the value before the template.`
|
|
2483
|
+
};
|
|
2484
|
+
}
|
|
2485
|
+
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
2486
|
+
}
|
|
2487
|
+
if (callee.property === "replace" || callee.property === "replaceAll") {
|
|
2488
|
+
const method = callee.property;
|
|
2489
|
+
if (args.length < 2) {
|
|
2490
|
+
return {
|
|
2491
|
+
kind: "unsupported",
|
|
2492
|
+
raw,
|
|
2493
|
+
reason: `\`.${method}(${args.length === 0 ? "" : "pattern"})\` needs both a pattern and a replacement — JS coerces the missing argument to the string "undefined", a degenerate result. Pass both arguments, or pre-compute the value before the template.`
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
const patternNode = node.arguments[0];
|
|
2497
|
+
if (patternNode && ts2.isRegularExpressionLiteral(patternNode)) {
|
|
2498
|
+
return { kind: "array-method", method, object: callee.object, args };
|
|
2499
|
+
}
|
|
2500
|
+
const badArg = args[0].kind === "unsupported" || args[0].kind === "object-literal" ? args[0] : args[1].kind === "unsupported" || args[1].kind === "object-literal" ? args[1] : undefined;
|
|
2501
|
+
if (badArg) {
|
|
2502
|
+
const reason = badArg.kind === "unsupported" ? badArg.reason : "Unsupported syntax: ObjectLiteralExpression";
|
|
2503
|
+
return { kind: "unsupported", raw, reason };
|
|
2504
|
+
}
|
|
2505
|
+
return { kind: "array-method", method, object: callee.object, args };
|
|
2506
|
+
}
|
|
2507
|
+
if (callee.property === "repeat") {
|
|
2508
|
+
return { kind: "array-method", method: "repeat", object: callee.object, args };
|
|
2509
|
+
}
|
|
2510
|
+
if (callee.property === "padStart" || callee.property === "padEnd") {
|
|
2511
|
+
return { kind: "array-method", method: callee.property, object: callee.object, args };
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
return { kind: "call", callee, args };
|
|
2515
|
+
}
|
|
2393
2516
|
|
|
2394
2517
|
// src/prop-rewrite.ts
|
|
2395
2518
|
import ts11 from "typescript";
|
|
@@ -2397,6 +2520,112 @@ import ts11 from "typescript";
|
|
|
2397
2520
|
// src/ir-to-client-js/utils.ts
|
|
2398
2521
|
import ts5 from "typescript";
|
|
2399
2522
|
|
|
2523
|
+
// src/scope/binding-scope.ts
|
|
2524
|
+
function isStringLoopValue(type) {
|
|
2525
|
+
return type.primitive === "string" || type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue);
|
|
2526
|
+
}
|
|
2527
|
+
|
|
2528
|
+
class BindingScope {
|
|
2529
|
+
frames;
|
|
2530
|
+
static EMPTY = new BindingScope([]);
|
|
2531
|
+
constructor(frames) {
|
|
2532
|
+
this.frames = frames;
|
|
2533
|
+
}
|
|
2534
|
+
enterLoopRow(loop) {
|
|
2535
|
+
const bindings = new Map;
|
|
2536
|
+
if (loop.paramBindings && loop.paramBindings.length > 0) {
|
|
2537
|
+
for (const b of loop.paramBindings)
|
|
2538
|
+
bindings.set(b.name, { source: "destructure" });
|
|
2539
|
+
} else {
|
|
2540
|
+
bindings.set(loop.param, {
|
|
2541
|
+
source: "item",
|
|
2542
|
+
...loop.itemType ? { stringValue: isStringLoopValue(loop.itemType) } : {}
|
|
2543
|
+
});
|
|
2544
|
+
}
|
|
2545
|
+
if (loop.index != null)
|
|
2546
|
+
bindings.set(loop.index, { source: "index" });
|
|
2547
|
+
for (const name of loop.preamble?.declaredNames ?? [])
|
|
2548
|
+
bindings.set(name, { source: "preamble" });
|
|
2549
|
+
const frame = { kind: "loop-row", bindings };
|
|
2550
|
+
return new BindingScope([frame, ...this.frames]);
|
|
2551
|
+
}
|
|
2552
|
+
enterCallback(params) {
|
|
2553
|
+
const bindings = new Map;
|
|
2554
|
+
for (const name of params)
|
|
2555
|
+
bindings.set(name, { source: "param" });
|
|
2556
|
+
const frame = { kind: "callback", bindings };
|
|
2557
|
+
return new BindingScope([frame, ...this.frames]);
|
|
2558
|
+
}
|
|
2559
|
+
enterAliases(aliasToOriginal) {
|
|
2560
|
+
const bindings = new Map;
|
|
2561
|
+
for (const [alias, original] of aliasToOriginal) {
|
|
2562
|
+
const hit = this.lookup(original);
|
|
2563
|
+
if (hit)
|
|
2564
|
+
bindings.set(alias, hit.binding);
|
|
2565
|
+
}
|
|
2566
|
+
if (bindings.size === 0)
|
|
2567
|
+
return this;
|
|
2568
|
+
return new BindingScope([{ kind: "callback", bindings }, ...this.frames]);
|
|
2569
|
+
}
|
|
2570
|
+
isBound(name) {
|
|
2571
|
+
for (const frame of this.frames) {
|
|
2572
|
+
if (frame.bindings.has(name))
|
|
2573
|
+
return true;
|
|
2574
|
+
}
|
|
2575
|
+
return false;
|
|
2576
|
+
}
|
|
2577
|
+
isStringValueName(name, outer, componentProperty = false) {
|
|
2578
|
+
if (componentProperty)
|
|
2579
|
+
return outer(name);
|
|
2580
|
+
const hit = this.lookup(name);
|
|
2581
|
+
return hit ? hit.binding.stringValue === true : outer(name);
|
|
2582
|
+
}
|
|
2583
|
+
shadowedNames() {
|
|
2584
|
+
if (!this.frames[0])
|
|
2585
|
+
return [];
|
|
2586
|
+
return [...this.frames[0].bindings.keys()].filter((name) => this.frames.slice(1).some((frame) => frame.bindings.has(name)));
|
|
2587
|
+
}
|
|
2588
|
+
lookup(name) {
|
|
2589
|
+
for (let depth = 0;depth < this.frames.length; depth++) {
|
|
2590
|
+
const frame = this.frames[depth];
|
|
2591
|
+
const binding = frame.bindings.get(name);
|
|
2592
|
+
if (binding)
|
|
2593
|
+
return { depth, frame, binding };
|
|
2594
|
+
}
|
|
2595
|
+
return null;
|
|
2596
|
+
}
|
|
2597
|
+
boundNames() {
|
|
2598
|
+
if (this.boundNamesCache)
|
|
2599
|
+
return this.boundNamesCache;
|
|
2600
|
+
const names = new Set;
|
|
2601
|
+
for (const frame of this.frames) {
|
|
2602
|
+
for (const name of frame.bindings.keys())
|
|
2603
|
+
names.add(name);
|
|
2604
|
+
}
|
|
2605
|
+
this.boundNamesCache = names;
|
|
2606
|
+
return names;
|
|
2607
|
+
}
|
|
2608
|
+
boundNamesCache;
|
|
2609
|
+
valueBoundNamesCache;
|
|
2610
|
+
valueBoundNames() {
|
|
2611
|
+
if (this.valueBoundNamesCache)
|
|
2612
|
+
return this.valueBoundNamesCache;
|
|
2613
|
+
const names = new Set;
|
|
2614
|
+
for (const frame of this.frames) {
|
|
2615
|
+
for (const [name, binding] of frame.bindings) {
|
|
2616
|
+
if (binding.source === "item" || binding.source === "index" || binding.source === "destructure") {
|
|
2617
|
+
names.add(name);
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
this.valueBoundNamesCache = names;
|
|
2622
|
+
return names;
|
|
2623
|
+
}
|
|
2624
|
+
asShadowPredicate() {
|
|
2625
|
+
return (name) => this.isBound(name);
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2400
2629
|
// src/loop-chain.ts
|
|
2401
2630
|
function buildLoopChainExpr(opts) {
|
|
2402
2631
|
const sortExpr = opts.sortComparator ? `.toSorted((${opts.sortComparator.paramA}, ${opts.sortComparator.paramB}) => ${opts.sortComparator.raw})` : "";
|
|
@@ -2823,6 +3052,9 @@ function isCommentScopedRoot(root) {
|
|
|
2823
3052
|
function isScopeCommentFragmentRoot(root) {
|
|
2824
3053
|
return root.type === "fragment" && !!root.needsScopeComment;
|
|
2825
3054
|
}
|
|
3055
|
+
function isTransparentFragmentRoot(root) {
|
|
3056
|
+
return root.type === "fragment" && !!root.transparent;
|
|
3057
|
+
}
|
|
2826
3058
|
function componentDefScopeFlags(root) {
|
|
2827
3059
|
const flags = [];
|
|
2828
3060
|
if (isCommentScopedRoot(root))
|
|
@@ -3066,7 +3298,7 @@ function scanForIdentifiers(expr, predicate) {
|
|
|
3066
3298
|
return false;
|
|
3067
3299
|
}
|
|
3068
3300
|
function renderLoopBindingAccess(b, base) {
|
|
3069
|
-
const parent = `${base}${b.path}`;
|
|
3301
|
+
const parent = `${b.accessor ?? base}${b.path}`;
|
|
3070
3302
|
if (b.rest?.kind === "object") {
|
|
3071
3303
|
if (b.rest.exclude.length === 0) {
|
|
3072
3304
|
return `({...${parent}})`;
|
|
@@ -3081,18 +3313,27 @@ function renderLoopBindingAccess(b, base) {
|
|
|
3081
3313
|
}
|
|
3082
3314
|
return parent;
|
|
3083
3315
|
}
|
|
3084
|
-
function wrapLoopParamAsAccessor(expr, paramName, bindings, indexParam) {
|
|
3316
|
+
function wrapLoopParamAsAccessor(expr, paramName, bindings, indexParam, shadowed) {
|
|
3317
|
+
const destructured = !!bindings && bindings.length > 0;
|
|
3318
|
+
const liveBindings = destructured && shadowed ? bindings.filter((b) => !shadowed.has(b.name)) : bindings;
|
|
3319
|
+
const liveIndex = indexParam && shadowed?.has(indexParam) ? null : indexParam;
|
|
3085
3320
|
let result;
|
|
3086
|
-
if (
|
|
3087
|
-
result = rewriteLoopBindingRefs(expr,
|
|
3321
|
+
if (destructured) {
|
|
3322
|
+
result = liveBindings && liveBindings.length > 0 ? rewriteLoopBindingRefs(expr, liveBindings, "__bfItem()") : expr;
|
|
3088
3323
|
} else {
|
|
3089
|
-
result = rewriteIdentifierAsAccessor(expr, paramName);
|
|
3324
|
+
result = shadowed?.has(paramName) ? expr : rewriteIdentifierAsAccessor(expr, paramName);
|
|
3090
3325
|
}
|
|
3091
|
-
if (
|
|
3092
|
-
result = wrapIndexParamAsAccessor(result,
|
|
3326
|
+
if (liveIndex && liveIndex !== paramName) {
|
|
3327
|
+
result = wrapIndexParamAsAccessor(result, liveIndex);
|
|
3093
3328
|
}
|
|
3094
3329
|
return result;
|
|
3095
3330
|
}
|
|
3331
|
+
function unionNames(a, b) {
|
|
3332
|
+
return b && b.size > 0 ? new Set([...a, ...b]) : a;
|
|
3333
|
+
}
|
|
3334
|
+
function rowBoundNames(loop) {
|
|
3335
|
+
return BindingScope.EMPTY.enterLoopRow(loop).boundNames();
|
|
3336
|
+
}
|
|
3096
3337
|
function wrapIndexParamAsAccessor(expr, indexParam) {
|
|
3097
3338
|
return rewriteIdentifierAsAccessor(expr, indexParam);
|
|
3098
3339
|
}
|
|
@@ -3171,15 +3412,33 @@ function wrapExprWithLoopParams(expr, loopParams) {
|
|
|
3171
3412
|
let result = expr;
|
|
3172
3413
|
for (const p of loopParams) {
|
|
3173
3414
|
const spec = typeof p === "string" ? { param: p } : p;
|
|
3174
|
-
result = wrapLoopParamAsAccessor(result, spec.param, spec.bindings, spec.index);
|
|
3415
|
+
result = wrapLoopParamAsAccessor(result, spec.param, spec.bindings, spec.index, spec.shadowed);
|
|
3175
3416
|
}
|
|
3176
3417
|
return result;
|
|
3177
3418
|
}
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3419
|
+
function specsInsideRow(specs, loop) {
|
|
3420
|
+
if (!specs)
|
|
3421
|
+
return specs;
|
|
3422
|
+
const names = rowBoundNames(loop);
|
|
3423
|
+
if (names.size === 0)
|
|
3424
|
+
return specs;
|
|
3425
|
+
return specs.map((p) => {
|
|
3426
|
+
const spec = typeof p === "string" ? { param: p } : p;
|
|
3427
|
+
const shadowed = spec.shadowed ? new Set([...spec.shadowed, ...names]) : names;
|
|
3428
|
+
return { ...spec, shadowed };
|
|
3429
|
+
});
|
|
3430
|
+
}
|
|
3431
|
+
function withRowAccessor(bindings, markerId) {
|
|
3432
|
+
if (!bindings || bindings.length === 0)
|
|
3433
|
+
return bindings;
|
|
3434
|
+
const accessor = `__bfItem_${markerId}()`;
|
|
3435
|
+
return bindings.map((b) => ({ ...b, accessor }));
|
|
3436
|
+
}
|
|
3437
|
+
|
|
3438
|
+
// src/html-constants.ts
|
|
3439
|
+
import { BOOLEAN_ATTRS, isBooleanAttr } from "@barefootjs/shared";
|
|
3440
|
+
|
|
3441
|
+
// src/ir-to-client-js/component-scope.ts
|
|
3183
3442
|
function computeFileScope(entryPath) {
|
|
3184
3443
|
let h = 2166136261;
|
|
3185
3444
|
for (let i = 0;i < entryPath.length; i++) {
|
|
@@ -4168,101 +4427,6 @@ function renderChildScopeArgs(comp, keyArg) {
|
|
|
4168
4427
|
return derivesScopeFromSlot(comp) ? `${key}, '${comp.slotId}'` : `${key}, '${comp.slotId}', true`;
|
|
4169
4428
|
}
|
|
4170
4429
|
|
|
4171
|
-
// src/scope/binding-scope.ts
|
|
4172
|
-
function isStringLoopValue(type) {
|
|
4173
|
-
return type.primitive === "string" || type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue);
|
|
4174
|
-
}
|
|
4175
|
-
|
|
4176
|
-
class BindingScope {
|
|
4177
|
-
frames;
|
|
4178
|
-
static EMPTY = new BindingScope([]);
|
|
4179
|
-
constructor(frames) {
|
|
4180
|
-
this.frames = frames;
|
|
4181
|
-
}
|
|
4182
|
-
enterLoopRow(loop) {
|
|
4183
|
-
const bindings = new Map;
|
|
4184
|
-
if (loop.paramBindings && loop.paramBindings.length > 0) {
|
|
4185
|
-
for (const b of loop.paramBindings)
|
|
4186
|
-
bindings.set(b.name, { source: "destructure" });
|
|
4187
|
-
} else {
|
|
4188
|
-
bindings.set(loop.param, {
|
|
4189
|
-
source: "item",
|
|
4190
|
-
...loop.itemType ? { stringValue: isStringLoopValue(loop.itemType) } : {}
|
|
4191
|
-
});
|
|
4192
|
-
}
|
|
4193
|
-
if (loop.index != null)
|
|
4194
|
-
bindings.set(loop.index, { source: "index" });
|
|
4195
|
-
for (const name of loop.preamble?.declaredNames ?? [])
|
|
4196
|
-
bindings.set(name, { source: "preamble" });
|
|
4197
|
-
const frame = { kind: "loop-row", bindings };
|
|
4198
|
-
return new BindingScope([frame, ...this.frames]);
|
|
4199
|
-
}
|
|
4200
|
-
enterCallback(params) {
|
|
4201
|
-
const bindings = new Map;
|
|
4202
|
-
for (const name of params)
|
|
4203
|
-
bindings.set(name, { source: "param" });
|
|
4204
|
-
const frame = { kind: "callback", bindings };
|
|
4205
|
-
return new BindingScope([frame, ...this.frames]);
|
|
4206
|
-
}
|
|
4207
|
-
isBound(name) {
|
|
4208
|
-
for (const frame of this.frames) {
|
|
4209
|
-
if (frame.bindings.has(name))
|
|
4210
|
-
return true;
|
|
4211
|
-
}
|
|
4212
|
-
return false;
|
|
4213
|
-
}
|
|
4214
|
-
isStringValueName(name, outer, componentProperty = false) {
|
|
4215
|
-
if (componentProperty)
|
|
4216
|
-
return outer(name);
|
|
4217
|
-
const hit = this.lookup(name);
|
|
4218
|
-
return hit ? hit.binding.stringValue === true : outer(name);
|
|
4219
|
-
}
|
|
4220
|
-
shadowedNames() {
|
|
4221
|
-
if (!this.frames[0])
|
|
4222
|
-
return [];
|
|
4223
|
-
return [...this.frames[0].bindings.keys()].filter((name) => this.frames.slice(1).some((frame) => frame.bindings.has(name)));
|
|
4224
|
-
}
|
|
4225
|
-
lookup(name) {
|
|
4226
|
-
for (let depth = 0;depth < this.frames.length; depth++) {
|
|
4227
|
-
const frame = this.frames[depth];
|
|
4228
|
-
const binding = frame.bindings.get(name);
|
|
4229
|
-
if (binding)
|
|
4230
|
-
return { depth, frame, binding };
|
|
4231
|
-
}
|
|
4232
|
-
return null;
|
|
4233
|
-
}
|
|
4234
|
-
boundNames() {
|
|
4235
|
-
if (this.boundNamesCache)
|
|
4236
|
-
return this.boundNamesCache;
|
|
4237
|
-
const names = new Set;
|
|
4238
|
-
for (const frame of this.frames) {
|
|
4239
|
-
for (const name of frame.bindings.keys())
|
|
4240
|
-
names.add(name);
|
|
4241
|
-
}
|
|
4242
|
-
this.boundNamesCache = names;
|
|
4243
|
-
return names;
|
|
4244
|
-
}
|
|
4245
|
-
boundNamesCache;
|
|
4246
|
-
valueBoundNamesCache;
|
|
4247
|
-
valueBoundNames() {
|
|
4248
|
-
if (this.valueBoundNamesCache)
|
|
4249
|
-
return this.valueBoundNamesCache;
|
|
4250
|
-
const names = new Set;
|
|
4251
|
-
for (const frame of this.frames) {
|
|
4252
|
-
for (const [name, binding] of frame.bindings) {
|
|
4253
|
-
if (binding.source === "item" || binding.source === "index" || binding.source === "destructure") {
|
|
4254
|
-
names.add(name);
|
|
4255
|
-
}
|
|
4256
|
-
}
|
|
4257
|
-
}
|
|
4258
|
-
this.valueBoundNamesCache = names;
|
|
4259
|
-
return names;
|
|
4260
|
-
}
|
|
4261
|
-
asShadowPredicate() {
|
|
4262
|
-
return (name) => this.isBound(name);
|
|
4263
|
-
}
|
|
4264
|
-
}
|
|
4265
|
-
|
|
4266
4430
|
// src/ir-to-client-js/safe-html.ts
|
|
4267
4431
|
function safeHtml(expr) {
|
|
4268
4432
|
return expr;
|
|
@@ -4791,7 +4955,8 @@ function irToHtmlTemplate(node, restSpreadNames, loopDepth = 0, loopParams, bran
|
|
|
4791
4955
|
return interp(renderChildCall(nameForRegistryRef(node.name), propsExpr, renderChildScopeArgs(node, keyArg)));
|
|
4792
4956
|
}
|
|
4793
4957
|
case "loop": {
|
|
4794
|
-
const
|
|
4958
|
+
const rowLoopParams = specsInsideRow(loopParams, node);
|
|
4959
|
+
const innerRecurse = (n) => irToHtmlTemplate(n, restSpreadNames, loopDepth + 1, rowLoopParams, branchSlotsVar);
|
|
4795
4960
|
let childTemplate = node.children.map(innerRecurse).join("");
|
|
4796
4961
|
if (node.bodyIsItemConditional && node.key) {
|
|
4797
4962
|
childTemplate = `${itemAnchorTemplate(node.key)}${childTemplate}`;
|
|
@@ -4804,11 +4969,11 @@ function irToHtmlTemplate(node, restSpreadNames, loopDepth = 0, loopParams, bran
|
|
|
4804
4969
|
let mapExpr;
|
|
4805
4970
|
if (node.flatMapCallback) {
|
|
4806
4971
|
const body = renderPreamble(node.flatMapCallback, {
|
|
4807
|
-
renderLeaf: (ir) => irToHtmlTemplate(leafKeyAsDataKeyAttr(ir), restSpreadNames, loopDepth + 1,
|
|
4972
|
+
renderLeaf: (ir) => irToHtmlTemplate(leafKeyAsDataKeyAttr(ir), restSpreadNames, loopDepth + 1, rowLoopParams, branchSlotsVar)
|
|
4808
4973
|
});
|
|
4809
4974
|
mapExpr = interp(mappedRowsMarkup(wrappedArray, "flatMap", node.flatMapCallback.params, body));
|
|
4810
4975
|
} else if (node.preamble) {
|
|
4811
|
-
const preamble = renderPreamble(node.preamble, { textVariant: "client", renderLeaf: (ir) => irToHtmlTemplate(ir, restSpreadNames, loopDepth + 1,
|
|
4976
|
+
const preamble = renderPreamble(node.preamble, { textVariant: "client", renderLeaf: (ir) => irToHtmlTemplate(ir, restSpreadNames, loopDepth + 1, rowLoopParams, branchSlotsVar) });
|
|
4812
4977
|
mapExpr = interp(mappedRowsMarkup(wrappedArray, iterMethod, callbackParam, `{ ${preamble} return \`${childTemplate}\` }`));
|
|
4813
4978
|
} else {
|
|
4814
4979
|
mapExpr = interp(mappedRowsMarkup(wrappedArray, iterMethod, callbackParam, `\`${childTemplate}\``));
|
|
@@ -5090,7 +5255,8 @@ function irToPlaceholderTemplate(node, restSpreadNames, loopDepth = 0, loopParam
|
|
|
5090
5255
|
return `<div ${DATA_BF_PH}="${phId}"></div>`;
|
|
5091
5256
|
}
|
|
5092
5257
|
case "loop": {
|
|
5093
|
-
const
|
|
5258
|
+
const rowLoopParams = specsInsideRow(loopParams, node);
|
|
5259
|
+
const innerRecurse = (n) => irToPlaceholderTemplate(n, restSpreadNames, loopDepth + 1, rowLoopParams);
|
|
5094
5260
|
const childTemplate = node.children.map(innerRecurse).join("");
|
|
5095
5261
|
const indexParam = node.index ? `, ${node.index}` : "";
|
|
5096
5262
|
const rawChainedArray = applyLoopChain(node);
|
|
@@ -5100,11 +5266,11 @@ function irToPlaceholderTemplate(node, restSpreadNames, loopDepth = 0, loopParam
|
|
|
5100
5266
|
let mapExpr;
|
|
5101
5267
|
if (node.flatMapCallback) {
|
|
5102
5268
|
const body = renderPreamble(node.flatMapCallback, {
|
|
5103
|
-
renderLeaf: (ir) => irToPlaceholderTemplate(leafKeyAsDataKeyAttr(ir), restSpreadNames, loopDepth + 1,
|
|
5269
|
+
renderLeaf: (ir) => irToPlaceholderTemplate(leafKeyAsDataKeyAttr(ir), restSpreadNames, loopDepth + 1, rowLoopParams)
|
|
5104
5270
|
});
|
|
5105
5271
|
mapExpr = interp(mappedRowsMarkup(wrappedArray, "flatMap", node.flatMapCallback.params, body));
|
|
5106
5272
|
} else if (node.preamble) {
|
|
5107
|
-
const preamble = renderPreamble(node.preamble, { textVariant: "client", renderLeaf: (ir) => irToPlaceholderTemplate(ir, restSpreadNames, loopDepth + 1,
|
|
5273
|
+
const preamble = renderPreamble(node.preamble, { textVariant: "client", renderLeaf: (ir) => irToPlaceholderTemplate(ir, restSpreadNames, loopDepth + 1, rowLoopParams) });
|
|
5108
5274
|
mapExpr = interp(mappedRowsMarkup(wrappedArray, iterMethod, callbackParam, `{ ${preamble} return \`${childTemplate}\` }`));
|
|
5109
5275
|
} else {
|
|
5110
5276
|
mapExpr = interp(mappedRowsMarkup(wrappedArray, iterMethod, callbackParam, `\`${childTemplate}\``));
|
|
@@ -6376,6 +6542,10 @@ function collectReactiveBrandLeaves(node, checker) {
|
|
|
6376
6542
|
}
|
|
6377
6543
|
|
|
6378
6544
|
// src/analyzer-context.ts
|
|
6545
|
+
function isGetJsAssertionArmed() {
|
|
6546
|
+
const env = globalThis.process?.env;
|
|
6547
|
+
return env?.BF_ASSERT_NO_JSX_IN_GETJS === "1";
|
|
6548
|
+
}
|
|
6379
6549
|
function createAnalyzerContext(sourceFile, filePath, acceptsCallbackBody) {
|
|
6380
6550
|
return {
|
|
6381
6551
|
sourceFile,
|
|
@@ -6426,7 +6596,7 @@ function createAnalyzerContext(sourceFile, filePath, acceptsCallbackBody) {
|
|
|
6426
6596
|
} catch {
|
|
6427
6597
|
ownSourceFile = undefined;
|
|
6428
6598
|
}
|
|
6429
|
-
if (
|
|
6599
|
+
if (isGetJsAssertionArmed() && !this.errors.some((e) => e.severity === "error") && nodeContainsJsx(node)) {
|
|
6430
6600
|
throw new Error("getJS() called on a JSX-bearing node — raw JSX must never be spliced " + "into emitted output. Carry mixed content as structured segments " + "(MapCallbackPreamble / FlatMapCallback) instead.");
|
|
6431
6601
|
}
|
|
6432
6602
|
if (ownSourceFile && ownSourceFile !== sourceFile) {
|
|
@@ -14175,6 +14345,9 @@ function extractFilterPredicate(callback, ctx) {
|
|
|
14175
14345
|
if (!support2.supported) {
|
|
14176
14346
|
return { result: null, unsupportedReason: support2.reason };
|
|
14177
14347
|
}
|
|
14348
|
+
const unbound2 = unboundFilterParamReason(callback, predicate2, ctx);
|
|
14349
|
+
if (unbound2)
|
|
14350
|
+
return { result: null, unsupportedReason: unbound2 };
|
|
14178
14351
|
return { result: { param, predicate: predicate2, raw: raw2 } };
|
|
14179
14352
|
}
|
|
14180
14353
|
const raw = ctx.getJS(callback.body);
|
|
@@ -14183,8 +14356,16 @@ function extractFilterPredicate(callback, ctx) {
|
|
|
14183
14356
|
if (!support.supported) {
|
|
14184
14357
|
return { result: null, unsupportedReason: support.reason };
|
|
14185
14358
|
}
|
|
14359
|
+
const unbound = unboundFilterParamReason(callback, predicate, ctx);
|
|
14360
|
+
if (unbound)
|
|
14361
|
+
return { result: null, unsupportedReason: unbound };
|
|
14186
14362
|
return { result: { param, predicate, raw } };
|
|
14187
14363
|
}
|
|
14364
|
+
function unboundFilterParamReason(callback, predicate, ctx) {
|
|
14365
|
+
const params = callback.parameters.map((p) => p.name.getText(ctx.sourceFile));
|
|
14366
|
+
const unbound = unboundCallbackParamRead("filter", { kind: "arrow", params, body: predicate });
|
|
14367
|
+
return unbound === null ? undefined : unboundCallbackParamReason("filter", unbound);
|
|
14368
|
+
}
|
|
14188
14369
|
function extractLoopParamBindings(pattern) {
|
|
14189
14370
|
if (ts19.isIdentifier(pattern))
|
|
14190
14371
|
return null;
|
|
@@ -14682,469 +14863,544 @@ function extractItemConditionalKey(cond) {
|
|
|
14682
14863
|
}
|
|
14683
14864
|
return a ?? b;
|
|
14684
14865
|
}
|
|
14685
|
-
|
|
14686
|
-
|
|
14687
|
-
|
|
14688
|
-
const depth = ctx.loopDepth;
|
|
14689
|
-
const propAccess = node.expression;
|
|
14690
|
-
const mapSource = propAccess.expression;
|
|
14691
|
-
let array = "";
|
|
14692
|
-
let templateArray;
|
|
14693
|
-
let arrayExpr = mapSource;
|
|
14694
|
-
let filterPredicate;
|
|
14695
|
-
let sortComparator;
|
|
14696
|
-
let chainOrder;
|
|
14697
|
-
let preamble;
|
|
14698
|
-
let iterationShape;
|
|
14699
|
-
let objectIteration;
|
|
14700
|
-
const setArray = (node2) => {
|
|
14701
|
-
array = ctx.getJS(node2);
|
|
14702
|
-
templateArray = rewriteBarePropRefs2(array, node2, ctx);
|
|
14703
|
-
arrayExpr = node2;
|
|
14704
|
-
};
|
|
14705
|
-
let chainSource = mapSource;
|
|
14866
|
+
var CLIENT_ONLY_EXPRESSION_SUGGESTION = "Add /* @client */ to evaluate this expression on the client only";
|
|
14867
|
+
var CLIENT_ONLY_LOOP_SUGGESTION = "Add /* @client */ to render this loop on the client only";
|
|
14868
|
+
function stripIteratorWrapper(mapSource) {
|
|
14706
14869
|
const iteratorInfo = isIteratorShapeCall(mapSource);
|
|
14707
14870
|
if (iteratorInfo) {
|
|
14708
|
-
|
|
14709
|
-
|
|
14710
|
-
|
|
14711
|
-
|
|
14712
|
-
|
|
14713
|
-
|
|
14714
|
-
|
|
14715
|
-
|
|
14716
|
-
|
|
14717
|
-
|
|
14718
|
-
|
|
14871
|
+
const iterationShape = iteratorInfo.shape === "entries" ? "entries" : iteratorInfo.shape === "keys" ? "keys" : undefined;
|
|
14872
|
+
return { chainSource: iteratorInfo.array, iterationShape, objectIteration: undefined };
|
|
14873
|
+
}
|
|
14874
|
+
const objectIteratorInfo = isObjectIteratorCall(mapSource);
|
|
14875
|
+
if (objectIteratorInfo) {
|
|
14876
|
+
return {
|
|
14877
|
+
chainSource: objectIteratorInfo.object,
|
|
14878
|
+
iterationShape: undefined,
|
|
14879
|
+
objectIteration: objectIteratorInfo.shape
|
|
14880
|
+
};
|
|
14881
|
+
}
|
|
14882
|
+
return { chainSource: mapSource, iterationShape: undefined, objectIteration: undefined };
|
|
14883
|
+
}
|
|
14884
|
+
function refuseOffSubsetCallback(kind, callback, unsupportedReason, ctx, isClientOnly) {
|
|
14885
|
+
if (isClientOnly || !unsupportedReason)
|
|
14886
|
+
return;
|
|
14887
|
+
if (ctx.analyzer.acceptsCallbackBody?.(kind) ?? false)
|
|
14888
|
+
return;
|
|
14889
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(callback, ctx.sourceFile, ctx.filePath), {
|
|
14890
|
+
message: `Expression cannot be compiled to marked template: ${unsupportedReason}`,
|
|
14891
|
+
suggestion: {
|
|
14892
|
+
message: CLIENT_ONLY_EXPRESSION_SUGGESTION
|
|
14719
14893
|
}
|
|
14894
|
+
}));
|
|
14895
|
+
}
|
|
14896
|
+
function resolveSortOuterChain(sortInfo, mapSource, plan, setArray, ctx, isClientOnly) {
|
|
14897
|
+
const innerFilter = isFilterCall(sortInfo.array);
|
|
14898
|
+
const sortExtraction = extractSortComparator(sortInfo.callback, sortInfo.method, ctx);
|
|
14899
|
+
if (isClientOnly || !sortExtraction.result) {
|
|
14900
|
+
refuseOffSubsetCallback("sort", sortInfo.callback, sortExtraction.unsupportedReason, ctx, isClientOnly);
|
|
14901
|
+
setArray(mapSource);
|
|
14902
|
+
return;
|
|
14720
14903
|
}
|
|
14721
|
-
|
|
14722
|
-
|
|
14904
|
+
plan.sortComparator = sortExtraction.result;
|
|
14905
|
+
if (!innerFilter) {
|
|
14906
|
+
setArray(sortInfo.array);
|
|
14907
|
+
return;
|
|
14908
|
+
}
|
|
14909
|
+
plan.chainOrder = "filter-sort";
|
|
14910
|
+
const filterExtraction = extractFilterPredicate(innerFilter.callback, ctx);
|
|
14911
|
+
if (isClientOnly || !filterExtraction.result) {
|
|
14912
|
+
refuseOffSubsetCallback("filter", innerFilter.callback, filterExtraction.unsupportedReason, ctx, isClientOnly);
|
|
14913
|
+
setArray(mapSource);
|
|
14914
|
+
plan.sortComparator = undefined;
|
|
14915
|
+
plan.chainOrder = undefined;
|
|
14916
|
+
return;
|
|
14917
|
+
}
|
|
14918
|
+
setArray(innerFilter.array);
|
|
14919
|
+
plan.filterPredicate = filterExtraction.result;
|
|
14920
|
+
}
|
|
14921
|
+
function resolveFilterOuterChain(filterInfo, mapSource, plan, setArray, ctx, isClientOnly) {
|
|
14922
|
+
const innerSort = isSortCall(filterInfo.array);
|
|
14923
|
+
const filterExtraction = extractFilterPredicate(filterInfo.callback, ctx);
|
|
14924
|
+
if (isClientOnly || !filterExtraction.result) {
|
|
14925
|
+
refuseOffSubsetCallback("filter", filterInfo.callback, filterExtraction.unsupportedReason, ctx, isClientOnly);
|
|
14926
|
+
setArray(mapSource);
|
|
14927
|
+
return;
|
|
14928
|
+
}
|
|
14929
|
+
plan.filterPredicate = filterExtraction.result;
|
|
14930
|
+
if (!innerSort) {
|
|
14931
|
+
setArray(filterInfo.array);
|
|
14932
|
+
return;
|
|
14933
|
+
}
|
|
14934
|
+
plan.chainOrder = "sort-filter";
|
|
14935
|
+
const sortExtraction = extractSortComparator(innerSort.callback, innerSort.method, ctx);
|
|
14936
|
+
if (isClientOnly || !sortExtraction.result) {
|
|
14937
|
+
refuseOffSubsetCallback("sort", innerSort.callback, sortExtraction.unsupportedReason, ctx, isClientOnly);
|
|
14938
|
+
setArray(filterInfo.array);
|
|
14939
|
+
return;
|
|
14940
|
+
}
|
|
14941
|
+
plan.sortComparator = sortExtraction.result;
|
|
14942
|
+
setArray(innerSort.array);
|
|
14943
|
+
}
|
|
14944
|
+
function resolveLoopSource(node, ctx, isClientOnly) {
|
|
14945
|
+
const propAccess = node.expression;
|
|
14946
|
+
const mapSource = propAccess.expression;
|
|
14947
|
+
const iteration = stripIteratorWrapper(mapSource);
|
|
14948
|
+
const plan = {
|
|
14949
|
+
array: "",
|
|
14950
|
+
templateArray: undefined,
|
|
14951
|
+
arrayExpr: mapSource,
|
|
14952
|
+
filterPredicate: undefined,
|
|
14953
|
+
sortComparator: undefined,
|
|
14954
|
+
chainOrder: undefined,
|
|
14955
|
+
iterationShape: iteration.iterationShape,
|
|
14956
|
+
objectIteration: iteration.objectIteration
|
|
14957
|
+
};
|
|
14958
|
+
const setArray = (expr) => {
|
|
14959
|
+
plan.array = ctx.getJS(expr);
|
|
14960
|
+
plan.templateArray = rewriteBarePropRefs2(plan.array, expr, ctx);
|
|
14961
|
+
plan.arrayExpr = expr;
|
|
14962
|
+
};
|
|
14963
|
+
const filterInfo = isFilterCall(iteration.chainSource);
|
|
14964
|
+
const sortInfo = isSortCall(iteration.chainSource);
|
|
14723
14965
|
if (sortInfo) {
|
|
14724
|
-
|
|
14725
|
-
const sortExtraction = extractSortComparator(sortInfo.callback, sortInfo.method, ctx);
|
|
14726
|
-
if (isClientOnly || !sortExtraction.result) {
|
|
14727
|
-
if (!isClientOnly && sortExtraction.unsupportedReason && !(ctx.analyzer.acceptsCallbackBody?.("sort") ?? false)) {
|
|
14728
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(sortInfo.callback, ctx.sourceFile, ctx.filePath), {
|
|
14729
|
-
message: `Expression cannot be compiled to marked template: ${sortExtraction.unsupportedReason}`,
|
|
14730
|
-
suggestion: {
|
|
14731
|
-
message: "Add /* @client */ to evaluate this expression on the client only"
|
|
14732
|
-
}
|
|
14733
|
-
}));
|
|
14734
|
-
}
|
|
14735
|
-
setArray(mapSource);
|
|
14736
|
-
} else {
|
|
14737
|
-
sortComparator = sortExtraction.result;
|
|
14738
|
-
if (innerFilter) {
|
|
14739
|
-
chainOrder = "filter-sort";
|
|
14740
|
-
const filterExtraction = extractFilterPredicate(innerFilter.callback, ctx);
|
|
14741
|
-
if (isClientOnly || !filterExtraction.result) {
|
|
14742
|
-
if (!isClientOnly && filterExtraction.unsupportedReason && !(ctx.analyzer.acceptsCallbackBody?.("filter") ?? false)) {
|
|
14743
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(innerFilter.callback, ctx.sourceFile, ctx.filePath), {
|
|
14744
|
-
message: `Expression cannot be compiled to marked template: ${filterExtraction.unsupportedReason}`,
|
|
14745
|
-
suggestion: {
|
|
14746
|
-
message: "Add /* @client */ to evaluate this expression on the client only"
|
|
14747
|
-
}
|
|
14748
|
-
}));
|
|
14749
|
-
}
|
|
14750
|
-
setArray(mapSource);
|
|
14751
|
-
sortComparator = undefined;
|
|
14752
|
-
chainOrder = undefined;
|
|
14753
|
-
} else {
|
|
14754
|
-
setArray(innerFilter.array);
|
|
14755
|
-
filterPredicate = filterExtraction.result;
|
|
14756
|
-
}
|
|
14757
|
-
} else {
|
|
14758
|
-
setArray(sortInfo.array);
|
|
14759
|
-
}
|
|
14760
|
-
}
|
|
14966
|
+
resolveSortOuterChain(sortInfo, mapSource, plan, setArray, ctx, isClientOnly);
|
|
14761
14967
|
} else if (filterInfo) {
|
|
14762
|
-
|
|
14763
|
-
const filterExtraction = extractFilterPredicate(filterInfo.callback, ctx);
|
|
14764
|
-
if (isClientOnly || !filterExtraction.result) {
|
|
14765
|
-
if (!isClientOnly && filterExtraction.unsupportedReason && !(ctx.analyzer.acceptsCallbackBody?.("filter") ?? false)) {
|
|
14766
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(filterInfo.callback, ctx.sourceFile, ctx.filePath), {
|
|
14767
|
-
message: `Expression cannot be compiled to marked template: ${filterExtraction.unsupportedReason}`,
|
|
14768
|
-
suggestion: {
|
|
14769
|
-
message: "Add /* @client */ to evaluate this expression on the client only"
|
|
14770
|
-
}
|
|
14771
|
-
}));
|
|
14772
|
-
}
|
|
14773
|
-
setArray(mapSource);
|
|
14774
|
-
} else {
|
|
14775
|
-
filterPredicate = filterExtraction.result;
|
|
14776
|
-
if (innerSort) {
|
|
14777
|
-
chainOrder = "sort-filter";
|
|
14778
|
-
const sortExtraction = extractSortComparator(innerSort.callback, innerSort.method, ctx);
|
|
14779
|
-
if (isClientOnly || !sortExtraction.result) {
|
|
14780
|
-
if (!isClientOnly && sortExtraction.unsupportedReason && !(ctx.analyzer.acceptsCallbackBody?.("sort") ?? false)) {
|
|
14781
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(innerSort.callback, ctx.sourceFile, ctx.filePath), {
|
|
14782
|
-
message: `Expression cannot be compiled to marked template: ${sortExtraction.unsupportedReason}`,
|
|
14783
|
-
suggestion: {
|
|
14784
|
-
message: "Add /* @client */ to evaluate this expression on the client only"
|
|
14785
|
-
}
|
|
14786
|
-
}));
|
|
14787
|
-
}
|
|
14788
|
-
setArray(filterInfo.array);
|
|
14789
|
-
} else {
|
|
14790
|
-
sortComparator = sortExtraction.result;
|
|
14791
|
-
setArray(innerSort.array);
|
|
14792
|
-
}
|
|
14793
|
-
} else {
|
|
14794
|
-
setArray(filterInfo.array);
|
|
14795
|
-
}
|
|
14796
|
-
}
|
|
14968
|
+
resolveFilterOuterChain(filterInfo, mapSource, plan, setArray, ctx, isClientOnly);
|
|
14797
14969
|
} else {
|
|
14798
|
-
setArray(chainSource);
|
|
14970
|
+
setArray(iteration.chainSource);
|
|
14799
14971
|
}
|
|
14800
|
-
|
|
14801
|
-
|
|
14802
|
-
|
|
14803
|
-
|
|
14804
|
-
|
|
14805
|
-
|
|
14806
|
-
|
|
14807
|
-
|
|
14808
|
-
|
|
14809
|
-
if (ts19.
|
|
14810
|
-
|
|
14811
|
-
|
|
14812
|
-
|
|
14813
|
-
|
|
14814
|
-
|
|
14815
|
-
|
|
14816
|
-
|
|
14817
|
-
|
|
14818
|
-
|
|
14819
|
-
|
|
14820
|
-
|
|
14821
|
-
|
|
14822
|
-
|
|
14823
|
-
|
|
14824
|
-
|
|
14825
|
-
|
|
14826
|
-
|
|
14827
|
-
|
|
14828
|
-
|
|
14829
|
-
|
|
14830
|
-
|
|
14831
|
-
|
|
14832
|
-
|
|
14833
|
-
|
|
14834
|
-
|
|
14835
|
-
|
|
14836
|
-
|
|
14837
|
-
|
|
14838
|
-
|
|
14839
|
-
|
|
14840
|
-
|
|
14841
|
-
|
|
14842
|
-
|
|
14843
|
-
index = secondParam.name.getText(ctx.sourceFile);
|
|
14844
|
-
if (secondParam.type) {
|
|
14845
|
-
indexType = secondParam.type.getText(ctx.sourceFile);
|
|
14846
|
-
}
|
|
14847
|
-
}
|
|
14848
|
-
const savedScope = ctx.scope;
|
|
14849
|
-
ctx.scope = ctx.scope.enterLoopRow({ param, index, paramBindings });
|
|
14850
|
-
ctx.loopDepth++;
|
|
14851
|
-
const tryTransformRenderableBody = (expr) => {
|
|
14852
|
-
if (!ts19.isBinaryExpression(expr))
|
|
14853
|
-
return;
|
|
14854
|
-
const op = expr.operatorToken.kind;
|
|
14855
|
-
if (op !== ts19.SyntaxKind.AmpersandAmpersandToken && op !== ts19.SyntaxKind.BarBarToken && op !== ts19.SyntaxKind.QuestionQuestionToken) {
|
|
14856
|
-
return;
|
|
14857
|
-
}
|
|
14858
|
-
if (!containsJsxInExpression(expr) && !callsJsxHelper(expr, ctx))
|
|
14859
|
-
return;
|
|
14860
|
-
const transformed = transformJsxExpression(expr, ctx, isClientOnly);
|
|
14861
|
-
if (transformed)
|
|
14862
|
-
children = [transformed];
|
|
14863
|
-
};
|
|
14864
|
-
const body = callback.body;
|
|
14865
|
-
if (ts19.isJsxElement(body) || ts19.isJsxSelfClosingElement(body) || ts19.isJsxFragment(body)) {
|
|
14866
|
-
const transformed = transformNode(body, ctx);
|
|
14867
|
-
if (transformed) {
|
|
14868
|
-
children = [transformed];
|
|
14869
|
-
}
|
|
14870
|
-
} else if (ts19.isConditionalExpression(body)) {
|
|
14871
|
-
children = [transformConditional(body, ctx)];
|
|
14872
|
-
} else if (ts19.isParenthesizedExpression(body)) {
|
|
14873
|
-
let inner = body.expression;
|
|
14874
|
-
while (ts19.isParenthesizedExpression(inner)) {
|
|
14875
|
-
inner = inner.expression;
|
|
14876
|
-
}
|
|
14877
|
-
if (ts19.isJsxElement(inner) || ts19.isJsxSelfClosingElement(inner) || ts19.isJsxFragment(inner)) {
|
|
14878
|
-
const transformed = transformNode(inner, ctx);
|
|
14879
|
-
if (transformed) {
|
|
14880
|
-
children = [transformed];
|
|
14881
|
-
}
|
|
14882
|
-
} else if (ts19.isConditionalExpression(inner)) {
|
|
14883
|
-
children = [transformConditional(inner, ctx)];
|
|
14884
|
-
} else if (method === "flatMap" && ts19.isArrayLiteralExpression(inner)) {
|
|
14885
|
-
children = transformArrayLiteralChildren(inner, ctx);
|
|
14886
|
-
} else {
|
|
14887
|
-
tryTransformRenderableBody(inner);
|
|
14888
|
-
}
|
|
14889
|
-
} else if (method === "flatMap" && ts19.isArrayLiteralExpression(body)) {
|
|
14890
|
-
children = transformArrayLiteralChildren(body, ctx);
|
|
14891
|
-
} else if (ts19.isBlock(body)) {
|
|
14892
|
-
const multiReturn = method !== "flatMap" ? extractMultiReturnJsxBranches(body, true) : null;
|
|
14893
|
-
if (multiReturn && multiReturn.branches.length > 0) {
|
|
14894
|
-
const loc = getSourceLocation(body, ctx.sourceFile, ctx.filePath);
|
|
14895
|
-
children = [foldMultiReturnBranches(multiReturn, ctx, loc, ctx.getJS)];
|
|
14896
|
-
const pre = multiReturn.preamble ?? [];
|
|
14897
|
-
if (pre.length > 0) {
|
|
14898
|
-
preamble = preambleFromValueStatements(pre, ctx, body.statements);
|
|
14899
|
-
if (!preamble.declarations && !isClientOnly && !(ctx.analyzer.acceptsCallbackBody?.("map") ?? false)) {
|
|
14900
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, loc, {
|
|
14901
|
-
message: "A .map() callback body with a preamble before its branches cannot be " + "lowered to a template: the preamble is not a sequence of value " + "declarations, so this backend has no per-row local to carry into the " + "branches.",
|
|
14902
|
-
suggestion: {
|
|
14903
|
-
message: "Add /* @client */ to evaluate this expression on the client only"
|
|
14904
|
-
}
|
|
14905
|
-
}));
|
|
14906
|
-
}
|
|
14907
|
-
}
|
|
14908
|
-
}
|
|
14909
|
-
const returnStmt = children.length === 0 ? body.statements.find((s) => ts19.isReturnStatement(s) && s.expression != null) : undefined;
|
|
14910
|
-
let rowScopeBeforePreamble = null;
|
|
14911
|
-
if (returnStmt) {
|
|
14912
|
-
const preambleNames = new Set;
|
|
14913
|
-
for (const stmt of body.statements) {
|
|
14914
|
-
if (stmt === returnStmt)
|
|
14915
|
-
break;
|
|
14916
|
-
collectPreambleDeclaredNames(stmt, preambleNames);
|
|
14917
|
-
}
|
|
14918
|
-
if (preambleNames.size > 0) {
|
|
14919
|
-
rowScopeBeforePreamble = ctx.scope;
|
|
14920
|
-
ctx.scope = savedScope.enterLoopRow({
|
|
14921
|
-
param,
|
|
14922
|
-
index,
|
|
14923
|
-
paramBindings,
|
|
14924
|
-
preamble: { declaredNames: [...preambleNames] }
|
|
14925
|
-
});
|
|
14926
|
-
}
|
|
14927
|
-
}
|
|
14928
|
-
if (returnStmt && returnStmt.expression) {
|
|
14929
|
-
let returnExpr = returnStmt.expression;
|
|
14930
|
-
while (ts19.isParenthesizedExpression(returnExpr)) {
|
|
14931
|
-
returnExpr = returnExpr.expression;
|
|
14932
|
-
}
|
|
14933
|
-
if (ts19.isJsxElement(returnExpr) || ts19.isJsxSelfClosingElement(returnExpr) || ts19.isJsxFragment(returnExpr)) {
|
|
14934
|
-
const transformed = transformNode(returnExpr, ctx);
|
|
14935
|
-
if (transformed) {
|
|
14936
|
-
children = [transformed];
|
|
14937
|
-
}
|
|
14938
|
-
}
|
|
14939
|
-
let jsxPreambleStmt;
|
|
14940
|
-
for (const stmt of body.statements) {
|
|
14941
|
-
if (stmt === returnStmt)
|
|
14942
|
-
break;
|
|
14943
|
-
if (containsJsxInExpression(stmt)) {
|
|
14944
|
-
jsxPreambleStmt = stmt;
|
|
14945
|
-
break;
|
|
14946
|
-
}
|
|
14947
|
-
}
|
|
14948
|
-
if (jsxPreambleStmt) {
|
|
14949
|
-
const jsRuntime = isClientOnly || (ctx.analyzer.acceptsCallbackBody?.("map") ?? false);
|
|
14950
|
-
if (children.length === 0) {
|
|
14951
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(jsxPreambleStmt, ctx.sourceFile, ctx.filePath), {
|
|
14952
|
-
message: "A .map() callback that builds JSX in a statement before its " + "`return` must return a single JSX element that embeds the " + "built array — this return shape has no element root to host it.",
|
|
14953
|
-
suggestion: {
|
|
14954
|
-
message: "Wrap the result in one element root, e.g. `return <tr key={item.id}>{out}</tr>`."
|
|
14955
|
-
}
|
|
14956
|
-
}));
|
|
14957
|
-
} else if (!jsRuntime) {
|
|
14958
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(jsxPreambleStmt, ctx.sourceFile, ctx.filePath), {
|
|
14959
|
-
message: "A .map() callback that builds JSX in a statement before its " + "`return` (for example pushing elements into an array in a loop) " + "cannot be lowered to a template on this backend.",
|
|
14960
|
-
suggestion: {
|
|
14961
|
-
message: "Add /* @client */ to render this loop on the client only"
|
|
14962
|
-
}
|
|
14963
|
-
}));
|
|
14964
|
-
} else {
|
|
14965
|
-
const collected = buildPreambleSegments(body.statements, returnStmt, ctx);
|
|
14966
|
-
if (collected.refusalNode) {
|
|
14967
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(collected.refusalNode, ctx.sourceFile, ctx.filePath), {
|
|
14968
|
-
message: "A JSX element built in a .map() callback preamble cannot carry " + "event handlers, components, nested loops, or reactive expressions, " + "and cannot sit inside a template literal — the verbatim render " + "path builds it once, with no reactive wiring.",
|
|
14969
|
-
suggestion: {
|
|
14970
|
-
message: "Return the element directly (not through a preamble variable), " + "or add /* @client */ to render the loop on the client only."
|
|
14971
|
-
}
|
|
14972
|
-
}));
|
|
14973
|
-
} else {
|
|
14974
|
-
preamble = collected.preamble;
|
|
14975
|
-
}
|
|
14976
|
-
}
|
|
14977
|
-
} else {
|
|
14978
|
-
const valueStmts = [];
|
|
14979
|
-
for (const stmt of body.statements) {
|
|
14980
|
-
if (stmt === returnStmt)
|
|
14981
|
-
break;
|
|
14982
|
-
valueStmts.push(stmt);
|
|
14983
|
-
}
|
|
14984
|
-
if (valueStmts.length > 0) {
|
|
14985
|
-
preamble = preambleFromValueStatements(valueStmts, ctx, body.statements);
|
|
14986
|
-
if (!preamble.declarations && !isClientOnly && !(ctx.analyzer.acceptsCallbackBody?.("map") ?? false)) {
|
|
14987
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(body, ctx.sourceFile, ctx.filePath), {
|
|
14988
|
-
message: "A .map() callback preamble that is not a sequence of value " + "declarations cannot be lowered to a template: this backend can " + "declare a per-row local, but cannot run arbitrary statements per row.",
|
|
14989
|
-
suggestion: {
|
|
14990
|
-
message: "Add /* @client */ to evaluate this expression on the client only"
|
|
14991
|
-
}
|
|
14992
|
-
}));
|
|
14993
|
-
}
|
|
14994
|
-
}
|
|
14995
|
-
}
|
|
14996
|
-
}
|
|
14997
|
-
if (rowScopeBeforePreamble) {
|
|
14998
|
-
ctx.scope = rowScopeBeforePreamble;
|
|
14999
|
-
}
|
|
15000
|
-
if (method === "flatMap" && children.length === 0 && !flatMapProjectionCall(body)) {
|
|
15001
|
-
flatMapCallback = buildFlatMapCallback(callback, body, ctx);
|
|
15002
|
-
}
|
|
14972
|
+
return plan;
|
|
14973
|
+
}
|
|
14974
|
+
function entriesTupleBindings(pattern) {
|
|
14975
|
+
const elements = pattern.elements.filter((el) => !ts19.isOmittedExpression(el));
|
|
14976
|
+
if (elements.length !== 2)
|
|
14977
|
+
return null;
|
|
14978
|
+
const [first, second] = elements;
|
|
14979
|
+
if (!ts19.isBindingElement(first) || !ts19.isIdentifier(first.name))
|
|
14980
|
+
return null;
|
|
14981
|
+
if (!ts19.isBindingElement(second) || !ts19.isIdentifier(second.name))
|
|
14982
|
+
return null;
|
|
14983
|
+
return { index: first.name.text, param: second.name.text, itemTypeNode: second.name };
|
|
14984
|
+
}
|
|
14985
|
+
function loopParamBindingsOrRefuse(firstParam, ctx) {
|
|
14986
|
+
const bindingResult = extractLoopParamBindings(firstParam.name);
|
|
14987
|
+
if (bindingResult && !Array.isArray(bindingResult)) {
|
|
14988
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_DESTRUCTURE_REST, getSourceLocation(firstParam, ctx.sourceFile, ctx.filePath)));
|
|
14989
|
+
return;
|
|
14990
|
+
}
|
|
14991
|
+
return Array.isArray(bindingResult) ? bindingResult : undefined;
|
|
14992
|
+
}
|
|
14993
|
+
function resolveLoopRowParams(callback, source, ctx) {
|
|
14994
|
+
const row = {
|
|
14995
|
+
param: "item",
|
|
14996
|
+
paramType: undefined,
|
|
14997
|
+
index: null,
|
|
14998
|
+
indexType: undefined,
|
|
14999
|
+
paramBindings: undefined,
|
|
15000
|
+
itemTypeNode: undefined
|
|
15001
|
+
};
|
|
15002
|
+
const isEntriesShape = source.iterationShape === "entries" || source.objectIteration === "entries";
|
|
15003
|
+
if (callback.parameters.length > 0) {
|
|
15004
|
+
const firstParam = callback.parameters[0];
|
|
15005
|
+
row.itemTypeNode = firstParam;
|
|
15006
|
+
row.param = firstParam.name.getText(ctx.sourceFile);
|
|
15007
|
+
if (firstParam.type) {
|
|
15008
|
+
row.paramType = firstParam.type.getText(ctx.sourceFile);
|
|
15009
|
+
}
|
|
15010
|
+
const tuple = isEntriesShape && ts19.isArrayBindingPattern(firstParam.name) ? entriesTupleBindings(firstParam.name) : null;
|
|
15011
|
+
if (tuple) {
|
|
15012
|
+
row.index = tuple.index;
|
|
15013
|
+
row.param = tuple.param;
|
|
15014
|
+
row.itemTypeNode = tuple.itemTypeNode;
|
|
15003
15015
|
} else {
|
|
15004
|
-
|
|
15005
|
-
}
|
|
15006
|
-
if (method === "flatMap" && children.length === 0 && !flatMapCallback) {
|
|
15007
|
-
const projection = flatMapProjectionCall(body);
|
|
15008
|
-
if (projection) {
|
|
15009
|
-
const transformed = transformJsxExpression(projection, ctx, isClientOnly);
|
|
15010
|
-
if (transformed && transformed.type === "loop") {
|
|
15011
|
-
children = [transformed];
|
|
15012
|
-
}
|
|
15013
|
-
}
|
|
15014
|
-
}
|
|
15015
|
-
if (method === "flatMap" && children.length === 0 && !flatMapCallback && !ts19.isBlock(body)) {
|
|
15016
|
-
flatMapCallback = buildFlatMapCallback(callback, body, ctx);
|
|
15017
|
-
}
|
|
15018
|
-
if (flatMapCallback)
|
|
15019
|
-
preamble = undefined;
|
|
15020
|
-
if (flatMapCallback && !isClientOnly && !(ctx.analyzer.acceptsCallbackBody?.("flatMap") ?? false)) {
|
|
15021
|
-
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(body, ctx.sourceFile, ctx.filePath), {
|
|
15022
|
-
message: "A .flatMap() callback body with statements or a nested projection " + "cannot be lowered to a template on this backend.",
|
|
15023
|
-
suggestion: {
|
|
15024
|
-
message: "Add /* @client */ to render this loop on the client only"
|
|
15025
|
-
}
|
|
15026
|
-
}));
|
|
15016
|
+
row.paramBindings = loopParamBindingsOrRefuse(firstParam, ctx);
|
|
15027
15017
|
}
|
|
15028
|
-
ctx.scope = savedScope;
|
|
15029
|
-
ctx.loopDepth--;
|
|
15030
15018
|
}
|
|
15031
|
-
if (
|
|
15032
|
-
const
|
|
15033
|
-
|
|
15034
|
-
if (
|
|
15035
|
-
|
|
15036
|
-
message: `A .${method}() callback that builds JSX in this shape cannot be ` + "compiled — the JSX would leak verbatim into the client bundle. " + "Recognized bodies: a JSX element/fragment, a ternary or " + "&& / || / ?? expression, an array literal (flatMap), or a block " + "body whose return the compiler can lower.",
|
|
15037
|
-
suggestion: {
|
|
15038
|
-
message: "Restructure the callback to return the JSX element directly " + "(or via a block body with a plain `return`)."
|
|
15039
|
-
}
|
|
15040
|
-
}));
|
|
15019
|
+
if (callback.parameters.length > 1 && !isEntriesShape) {
|
|
15020
|
+
const secondParam = callback.parameters[1];
|
|
15021
|
+
row.index = secondParam.name.getText(ctx.sourceFile);
|
|
15022
|
+
if (secondParam.type) {
|
|
15023
|
+
row.indexType = secondParam.type.getText(ctx.sourceFile);
|
|
15041
15024
|
}
|
|
15042
|
-
return null;
|
|
15043
15025
|
}
|
|
15044
|
-
|
|
15045
|
-
|
|
15026
|
+
return row;
|
|
15027
|
+
}
|
|
15028
|
+
function transformRenderableLogicalBody(expr, ctx, isClientOnly) {
|
|
15029
|
+
if (!ts19.isBinaryExpression(expr))
|
|
15030
|
+
return [];
|
|
15031
|
+
const op = expr.operatorToken.kind;
|
|
15032
|
+
if (op !== ts19.SyntaxKind.AmpersandAmpersandToken && op !== ts19.SyntaxKind.BarBarToken && op !== ts19.SyntaxKind.QuestionQuestionToken) {
|
|
15033
|
+
return [];
|
|
15046
15034
|
}
|
|
15047
|
-
|
|
15048
|
-
|
|
15049
|
-
const
|
|
15050
|
-
|
|
15051
|
-
|
|
15052
|
-
|
|
15053
|
-
|
|
15054
|
-
|
|
15055
|
-
|
|
15056
|
-
}
|
|
15035
|
+
if (!containsJsxInExpression(expr) && !callsJsxHelper(expr, ctx))
|
|
15036
|
+
return [];
|
|
15037
|
+
const transformed = transformJsxExpression(expr, ctx, isClientOnly);
|
|
15038
|
+
return transformed ? [transformed] : [];
|
|
15039
|
+
}
|
|
15040
|
+
function transformLoopExpressionBody(body, ctx, isClientOnly, method) {
|
|
15041
|
+
let inner = body;
|
|
15042
|
+
while (ts19.isParenthesizedExpression(inner)) {
|
|
15043
|
+
inner = inner.expression;
|
|
15057
15044
|
}
|
|
15058
|
-
|
|
15059
|
-
|
|
15060
|
-
|
|
15061
|
-
|
|
15062
|
-
|
|
15063
|
-
|
|
15064
|
-
|
|
15065
|
-
|
|
15066
|
-
|
|
15067
|
-
|
|
15068
|
-
|
|
15069
|
-
|
|
15045
|
+
if (ts19.isJsxElement(inner) || ts19.isJsxSelfClosingElement(inner) || ts19.isJsxFragment(inner)) {
|
|
15046
|
+
const transformed = transformNode(inner, ctx);
|
|
15047
|
+
return transformed ? [transformed] : [];
|
|
15048
|
+
}
|
|
15049
|
+
if (ts19.isConditionalExpression(inner)) {
|
|
15050
|
+
return [transformConditional(inner, ctx)];
|
|
15051
|
+
}
|
|
15052
|
+
if (method === "flatMap" && ts19.isArrayLiteralExpression(inner)) {
|
|
15053
|
+
return transformArrayLiteralChildren(inner, ctx);
|
|
15054
|
+
}
|
|
15055
|
+
return transformRenderableLogicalBody(inner, ctx, isClientOnly);
|
|
15056
|
+
}
|
|
15057
|
+
function refuseUndeclarablePreamble(preamble, loc, message, ctx, isClientOnly) {
|
|
15058
|
+
if (preamble.declarations)
|
|
15059
|
+
return;
|
|
15060
|
+
if (isClientOnly)
|
|
15061
|
+
return;
|
|
15062
|
+
if (ctx.analyzer.acceptsCallbackBody?.("map") ?? false)
|
|
15063
|
+
return;
|
|
15064
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, loc, {
|
|
15065
|
+
message,
|
|
15066
|
+
suggestion: {
|
|
15067
|
+
message: CLIENT_ONLY_EXPRESSION_SUGGESTION
|
|
15070
15068
|
}
|
|
15069
|
+
}));
|
|
15070
|
+
}
|
|
15071
|
+
function lowerMultiReturnLoopBody(body, ctx, isClientOnly) {
|
|
15072
|
+
const multiReturn = extractMultiReturnJsxBranches(body, true);
|
|
15073
|
+
if (!multiReturn || multiReturn.branches.length === 0)
|
|
15074
|
+
return null;
|
|
15075
|
+
const loc = getSourceLocation(body, ctx.sourceFile, ctx.filePath);
|
|
15076
|
+
const children = [foldMultiReturnBranches(multiReturn, ctx, loc, ctx.getJS)];
|
|
15077
|
+
const pre = multiReturn.preamble ?? [];
|
|
15078
|
+
if (pre.length === 0)
|
|
15079
|
+
return { children, preamble: undefined };
|
|
15080
|
+
const preamble = preambleFromValueStatements(pre, ctx, body.statements);
|
|
15081
|
+
refuseUndeclarablePreamble(preamble, loc, "A .map() callback body with a preamble before its branches cannot be " + "lowered to a template: the preamble is not a sequence of value " + "declarations, so this backend has no per-row local to carry into the " + "branches.", ctx, isClientOnly);
|
|
15082
|
+
return { children, preamble };
|
|
15083
|
+
}
|
|
15084
|
+
function withPreambleRowScope(ctx, savedScope, row, body, returnStmt, run) {
|
|
15085
|
+
const preambleNames = new Set;
|
|
15086
|
+
for (const stmt of body.statements) {
|
|
15087
|
+
if (stmt === returnStmt)
|
|
15088
|
+
break;
|
|
15089
|
+
collectPreambleDeclaredNames(stmt, preambleNames);
|
|
15090
|
+
}
|
|
15091
|
+
if (preambleNames.size === 0)
|
|
15092
|
+
return run();
|
|
15093
|
+
const rowScopeBeforePreamble = ctx.scope;
|
|
15094
|
+
ctx.scope = savedScope.enterLoopRow({
|
|
15095
|
+
param: row.param,
|
|
15096
|
+
index: row.index,
|
|
15097
|
+
paramBindings: row.paramBindings,
|
|
15098
|
+
preamble: { declaredNames: [...preambleNames] }
|
|
15099
|
+
});
|
|
15100
|
+
try {
|
|
15101
|
+
return run();
|
|
15102
|
+
} finally {
|
|
15103
|
+
ctx.scope = rowScopeBeforePreamble;
|
|
15104
|
+
}
|
|
15105
|
+
}
|
|
15106
|
+
function findJsxBuildingPreambleStatement(body, returnStmt) {
|
|
15107
|
+
for (const stmt of body.statements) {
|
|
15108
|
+
if (stmt === returnStmt)
|
|
15109
|
+
break;
|
|
15110
|
+
if (containsJsxInExpression(stmt))
|
|
15111
|
+
return stmt;
|
|
15112
|
+
}
|
|
15113
|
+
return;
|
|
15114
|
+
}
|
|
15115
|
+
function lowerJsxBuildingPreamble(body, returnStmt, jsxPreambleStmt, children, ctx, isClientOnly) {
|
|
15116
|
+
const jsRuntime = isClientOnly || (ctx.analyzer.acceptsCallbackBody?.("map") ?? false);
|
|
15117
|
+
if (children.length === 0) {
|
|
15118
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(jsxPreambleStmt, ctx.sourceFile, ctx.filePath), {
|
|
15119
|
+
message: "A .map() callback that builds JSX in a statement before its " + "`return` must return a single JSX element that embeds the " + "built array — this return shape has no element root to host it.",
|
|
15120
|
+
suggestion: {
|
|
15121
|
+
message: "Wrap the result in one element root, e.g. `return <tr key={item.id}>{out}</tr>`."
|
|
15122
|
+
}
|
|
15123
|
+
}));
|
|
15124
|
+
return;
|
|
15071
15125
|
}
|
|
15072
|
-
if (
|
|
15073
|
-
|
|
15126
|
+
if (!jsRuntime) {
|
|
15127
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(jsxPreambleStmt, ctx.sourceFile, ctx.filePath), {
|
|
15128
|
+
message: "A .map() callback that builds JSX in a statement before its " + "`return` (for example pushing elements into an array in a loop) " + "cannot be lowered to a template on this backend.",
|
|
15129
|
+
suggestion: {
|
|
15130
|
+
message: CLIENT_ONLY_LOOP_SUGGESTION
|
|
15131
|
+
}
|
|
15132
|
+
}));
|
|
15133
|
+
return;
|
|
15074
15134
|
}
|
|
15075
|
-
|
|
15076
|
-
|
|
15077
|
-
|
|
15135
|
+
const collected = buildPreambleSegments(body.statements, returnStmt, ctx);
|
|
15136
|
+
if (collected.refusalNode) {
|
|
15137
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(collected.refusalNode, ctx.sourceFile, ctx.filePath), {
|
|
15138
|
+
message: "A JSX element built in a .map() callback preamble cannot carry " + "event handlers, components, nested loops, or reactive expressions, " + "and cannot sit inside a template literal — the verbatim render " + "path builds it once, with no reactive wiring.",
|
|
15078
15139
|
suggestion: {
|
|
15079
|
-
message: "Return
|
|
15140
|
+
message: "Return the element directly (not through a preamble variable), " + "or add /* @client */ to render the loop on the client only."
|
|
15080
15141
|
}
|
|
15081
15142
|
}));
|
|
15082
|
-
|
|
15143
|
+
return;
|
|
15083
15144
|
}
|
|
15084
|
-
|
|
15085
|
-
|
|
15086
|
-
|
|
15087
|
-
|
|
15088
|
-
|
|
15089
|
-
|
|
15090
|
-
|
|
15091
|
-
|
|
15092
|
-
name: p.name,
|
|
15093
|
-
value: p.value,
|
|
15094
|
-
isEventHandler: isEventHandlerName(p.name)
|
|
15095
|
-
})),
|
|
15096
|
-
children: comp.children
|
|
15097
|
-
};
|
|
15145
|
+
return collected.preamble;
|
|
15146
|
+
}
|
|
15147
|
+
function lowerValuePreamble(body, returnStmt, ctx, isClientOnly) {
|
|
15148
|
+
const valueStmts = [];
|
|
15149
|
+
for (const stmt of body.statements) {
|
|
15150
|
+
if (stmt === returnStmt)
|
|
15151
|
+
break;
|
|
15152
|
+
valueStmts.push(stmt);
|
|
15098
15153
|
}
|
|
15099
|
-
|
|
15100
|
-
|
|
15101
|
-
const
|
|
15102
|
-
|
|
15103
|
-
|
|
15104
|
-
|
|
15105
|
-
|
|
15106
|
-
|
|
15107
|
-
|
|
15108
|
-
|
|
15154
|
+
if (valueStmts.length === 0)
|
|
15155
|
+
return;
|
|
15156
|
+
const preamble = preambleFromValueStatements(valueStmts, ctx, body.statements);
|
|
15157
|
+
refuseUndeclarablePreamble(preamble, getSourceLocation(body, ctx.sourceFile, ctx.filePath), "A .map() callback preamble that is not a sequence of value " + "declarations cannot be lowered to a template: this backend can " + "declare a per-row local, but cannot run arbitrary statements per row.", ctx, isClientOnly);
|
|
15158
|
+
return preamble;
|
|
15159
|
+
}
|
|
15160
|
+
function lowerSingleReturnLoopBody(body, returnStmt, ctx, isClientOnly) {
|
|
15161
|
+
let returnExpr = returnStmt.expression;
|
|
15162
|
+
while (ts19.isParenthesizedExpression(returnExpr)) {
|
|
15163
|
+
returnExpr = returnExpr.expression;
|
|
15164
|
+
}
|
|
15165
|
+
let children = [];
|
|
15166
|
+
if (ts19.isJsxElement(returnExpr) || ts19.isJsxSelfClosingElement(returnExpr) || ts19.isJsxFragment(returnExpr)) {
|
|
15167
|
+
const transformed = transformNode(returnExpr, ctx);
|
|
15168
|
+
if (transformed) {
|
|
15169
|
+
children = [transformed];
|
|
15109
15170
|
}
|
|
15110
15171
|
}
|
|
15111
|
-
const
|
|
15172
|
+
const jsxPreambleStmt = findJsxBuildingPreambleStatement(body, returnStmt);
|
|
15173
|
+
const preamble = jsxPreambleStmt ? lowerJsxBuildingPreamble(body, returnStmt, jsxPreambleStmt, children, ctx, isClientOnly) : lowerValuePreamble(body, returnStmt, ctx, isClientOnly);
|
|
15174
|
+
return { children, preamble };
|
|
15175
|
+
}
|
|
15176
|
+
function transformLoopBlockBody(body, callback, row, savedScope, ctx, isClientOnly, method) {
|
|
15177
|
+
const out = { children: [], preamble: undefined, flatMapCallback: undefined };
|
|
15178
|
+
const multi = method !== "flatMap" ? lowerMultiReturnLoopBody(body, ctx, isClientOnly) : null;
|
|
15179
|
+
if (multi) {
|
|
15180
|
+
out.children = multi.children;
|
|
15181
|
+
out.preamble = multi.preamble;
|
|
15182
|
+
}
|
|
15183
|
+
const returnStmt = out.children.length === 0 ? body.statements.find((s) => ts19.isReturnStatement(s) && s.expression != null) : undefined;
|
|
15184
|
+
if (returnStmt) {
|
|
15185
|
+
const single = withPreambleRowScope(ctx, savedScope, row, body, returnStmt, () => lowerSingleReturnLoopBody(body, returnStmt, ctx, isClientOnly));
|
|
15186
|
+
out.children = single.children;
|
|
15187
|
+
out.preamble = single.preamble;
|
|
15188
|
+
}
|
|
15189
|
+
if (method === "flatMap" && out.children.length === 0 && !flatMapProjectionCall(body)) {
|
|
15190
|
+
out.flatMapCallback = buildFlatMapCallback(callback, body, ctx);
|
|
15191
|
+
}
|
|
15192
|
+
return out;
|
|
15193
|
+
}
|
|
15194
|
+
function applyFlatMapFallbacks(out, callback, ctx, isClientOnly, method) {
|
|
15195
|
+
if (method !== "flatMap")
|
|
15196
|
+
return;
|
|
15197
|
+
const body = callback.body;
|
|
15198
|
+
if (out.children.length === 0 && !out.flatMapCallback) {
|
|
15199
|
+
const projection = flatMapProjectionCall(body);
|
|
15200
|
+
if (projection) {
|
|
15201
|
+
const transformed = transformJsxExpression(projection, ctx, isClientOnly);
|
|
15202
|
+
if (transformed && transformed.type === "loop") {
|
|
15203
|
+
out.children = [transformed];
|
|
15204
|
+
}
|
|
15205
|
+
}
|
|
15206
|
+
}
|
|
15207
|
+
if (out.children.length === 0 && !out.flatMapCallback && !ts19.isBlock(body)) {
|
|
15208
|
+
out.flatMapCallback = buildFlatMapCallback(callback, body, ctx);
|
|
15209
|
+
}
|
|
15210
|
+
if (!out.flatMapCallback)
|
|
15211
|
+
return;
|
|
15212
|
+
out.preamble = undefined;
|
|
15213
|
+
if (isClientOnly || (ctx.analyzer.acceptsCallbackBody?.("flatMap") ?? false))
|
|
15214
|
+
return;
|
|
15215
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(body, ctx.sourceFile, ctx.filePath), {
|
|
15216
|
+
message: "A .flatMap() callback body with statements or a nested projection " + "cannot be lowered to a template on this backend.",
|
|
15217
|
+
suggestion: {
|
|
15218
|
+
message: CLIENT_ONLY_LOOP_SUGGESTION
|
|
15219
|
+
}
|
|
15220
|
+
}));
|
|
15221
|
+
}
|
|
15222
|
+
function withLoopRowScope(ctx, row, fn) {
|
|
15223
|
+
const savedScope = ctx.scope;
|
|
15224
|
+
ctx.scope = ctx.scope.enterLoopRow({ param: row.param, index: row.index, paramBindings: row.paramBindings });
|
|
15225
|
+
ctx.loopDepth++;
|
|
15226
|
+
try {
|
|
15227
|
+
return fn(savedScope);
|
|
15228
|
+
} finally {
|
|
15229
|
+
ctx.scope = savedScope;
|
|
15230
|
+
ctx.loopDepth--;
|
|
15231
|
+
}
|
|
15232
|
+
}
|
|
15233
|
+
function transformLoopCallbackBody(callback, row, savedScope, ctx, isClientOnly, method) {
|
|
15234
|
+
const body = callback.body;
|
|
15235
|
+
const out = ts19.isBlock(body) ? transformLoopBlockBody(body, callback, row, savedScope, ctx, isClientOnly, method) : { children: transformLoopExpressionBody(body, ctx, isClientOnly, method), preamble: undefined, flatMapCallback: undefined };
|
|
15236
|
+
applyFlatMapFallbacks(out, callback, ctx, isClientOnly, method);
|
|
15237
|
+
return out;
|
|
15238
|
+
}
|
|
15239
|
+
function refuseUnloweredJsxCallback(node, ctx, method, diagCountAtEntry) {
|
|
15240
|
+
const cb = node.arguments[0];
|
|
15241
|
+
const cbBody = cb && (ts19.isArrowFunction(cb) || ts19.isFunctionExpression(cb)) ? cb.body : undefined;
|
|
15242
|
+
if (!cbBody || !containsJsxInExpression(cbBody))
|
|
15243
|
+
return;
|
|
15244
|
+
if (ctx.analyzer.errors.length !== diagCountAtEntry)
|
|
15245
|
+
return;
|
|
15246
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(cbBody, ctx.sourceFile, ctx.filePath), {
|
|
15247
|
+
message: `A .${method}() callback that builds JSX in this shape cannot be ` + "compiled — the JSX would leak verbatim into the client bundle. " + "Recognized bodies: a JSX element/fragment, a ternary or " + "&& / || / ?? expression, an array literal (flatMap), or a block " + "body whose return the compiler can lower.",
|
|
15248
|
+
suggestion: {
|
|
15249
|
+
message: "Restructure the callback to return the JSX element directly " + "(or via a block body with a plain `return`)."
|
|
15250
|
+
}
|
|
15251
|
+
}));
|
|
15252
|
+
}
|
|
15253
|
+
function resolveLoopKey(callback, children, ctx, isNested, depth) {
|
|
15254
|
+
if (children.length > 0) {
|
|
15255
|
+
checkLoopKey(callback, ctx, isNested);
|
|
15256
|
+
}
|
|
15257
|
+
const itemConditional = children.length > 0 ? loopBodyItemConditional(children) : null;
|
|
15258
|
+
const bodyIsItemConditional = itemConditional !== null;
|
|
15259
|
+
const key = itemConditional ? extractItemConditionalKey(itemConditional) : children.length > 0 ? extractLoopKey(children[0]) : null;
|
|
15260
|
+
if (key !== null) {
|
|
15261
|
+
const resolvedKeyAttrName2 = keyAttrName2(depth);
|
|
15262
|
+
if (itemConditional) {
|
|
15263
|
+
applyLoopKeyAttr(itemConditional, resolvedKeyAttrName2, key);
|
|
15264
|
+
} else if (children.length > 0) {
|
|
15265
|
+
applyLoopKeyAttr(children[0], resolvedKeyAttrName2, key);
|
|
15266
|
+
}
|
|
15267
|
+
}
|
|
15268
|
+
applyRowPortalKeyAttrs(children, key, depth, ctx);
|
|
15269
|
+
return { key, bodyIsItemConditional };
|
|
15270
|
+
}
|
|
15271
|
+
function refusePreambleDerivedKey(node, key, preamble, ctx) {
|
|
15272
|
+
if (!key || !preamble || preamble.declaredNames.length === 0)
|
|
15273
|
+
return;
|
|
15274
|
+
const declaredNameSet = new Set(preamble.declaredNames);
|
|
15275
|
+
const keyRefs = extractFreeIdentifiersFromText(key);
|
|
15276
|
+
const usesLocal = [...keyRefs].some((r) => declaredNameSet.has(r));
|
|
15277
|
+
if (!usesLocal)
|
|
15278
|
+
return;
|
|
15279
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(node, ctx.sourceFile, ctx.filePath), {
|
|
15280
|
+
message: "A .map() loop key must be derivable from the loop item — it is " + "evaluated before the callback body runs, so it cannot reference a " + "value computed in the callback preamble.",
|
|
15281
|
+
suggestion: {
|
|
15282
|
+
message: "Derive the key directly from the loop item (e.g. key={item.id})."
|
|
15283
|
+
}
|
|
15284
|
+
}));
|
|
15285
|
+
}
|
|
15286
|
+
function applyPreambleBuilderPasses(node, children, preamble, ctx) {
|
|
15287
|
+
if (!preamble || preamble.builderNames.length === 0)
|
|
15288
|
+
return preamble;
|
|
15289
|
+
flagArrayChildExpressions(children, new Set(preamble.builderNames));
|
|
15290
|
+
if (children.length !== 1 || children[0].type !== "component")
|
|
15291
|
+
return preamble;
|
|
15292
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(node, ctx.sourceFile, ctx.filePath), {
|
|
15293
|
+
message: "A .map() callback that builds JSX in its preamble cannot return a " + "component: the built elements would reach the component as raw HTML " + "strings on the client but as JSX elements at SSR (a silent divergence).",
|
|
15294
|
+
suggestion: {
|
|
15295
|
+
message: "Return a plain element root that embeds the array, or move the " + "building logic inside the component."
|
|
15296
|
+
}
|
|
15297
|
+
}));
|
|
15298
|
+
return;
|
|
15299
|
+
}
|
|
15300
|
+
function loopChildComponentOf(children) {
|
|
15301
|
+
if (children.length !== 1 || children[0].type !== "component")
|
|
15302
|
+
return;
|
|
15303
|
+
const comp = children[0];
|
|
15304
|
+
return {
|
|
15305
|
+
name: comp.name,
|
|
15306
|
+
slotId: comp.slotId,
|
|
15307
|
+
props: comp.props.filter((p) => p.name !== "key").map((p) => ({
|
|
15308
|
+
name: p.name,
|
|
15309
|
+
value: p.value,
|
|
15310
|
+
isEventHandler: isEventHandlerName(p.name)
|
|
15311
|
+
})),
|
|
15312
|
+
children: comp.children
|
|
15313
|
+
};
|
|
15314
|
+
}
|
|
15315
|
+
function classifyLoopArray(source, method, ctx) {
|
|
15316
|
+
const callsReactive = exprCallsReactiveGetters(source.arrayExpr, ctx);
|
|
15317
|
+
const hasCalls = exprHasFunctionCalls(source.arrayExpr);
|
|
15318
|
+
const isDirectPropArray = method !== "flatMap" && isArrayExprDirectPropRef(source.arrayExpr, ctx);
|
|
15319
|
+
const isStaticArray = !isSignalOrMemoArray(source.array, ctx) && !isDirectPropArray && !hasCalls && !source.objectIteration;
|
|
15320
|
+
return { callsReactive, hasCalls, isDirectPropArray, isStaticArray };
|
|
15321
|
+
}
|
|
15322
|
+
function markPreambleReactivity(children, preamble, isStaticArray, ctx) {
|
|
15323
|
+
if (!preamble || isStaticArray)
|
|
15324
|
+
return;
|
|
15325
|
+
const declared = new Set(preamble.declaredNames);
|
|
15326
|
+
const preambleRegions = collectPreambleRegions(children, declared, ctx);
|
|
15327
|
+
markPreambleAttrSlots(children, declared, ctx);
|
|
15328
|
+
if (preamble.reactiveNames && preamble.reactiveNames.length > 0) {
|
|
15329
|
+
markPreambleConditionalReactivity(children, new Set(preamble.reactiveNames), ctx);
|
|
15330
|
+
}
|
|
15331
|
+
return preambleRegions;
|
|
15332
|
+
}
|
|
15333
|
+
function scalarLoopItemType(itemTypeNode, ctx) {
|
|
15112
15334
|
const callbackType = itemTypeNode && ctx.analyzer.checker ? tsTypeToTypeInfo(ctx.analyzer.checker.getTypeAtLocation(itemTypeNode), ctx.analyzer.checker) : null;
|
|
15113
|
-
|
|
15335
|
+
if (!callbackType)
|
|
15336
|
+
return null;
|
|
15337
|
+
if (callbackType.kind === "primitive")
|
|
15338
|
+
return callbackType;
|
|
15339
|
+
if (callbackType.kind === "union" && callbackType.unionTypes?.every((t) => t.kind === "primitive")) {
|
|
15340
|
+
return callbackType;
|
|
15341
|
+
}
|
|
15342
|
+
return null;
|
|
15343
|
+
}
|
|
15344
|
+
function transformMapCall(node, ctx, isClientOnly = false, method = "map") {
|
|
15345
|
+
const isNested = ctx.scope.valueBoundNames().size > 0;
|
|
15346
|
+
const diagCountAtEntry = ctx.analyzer.errors.length;
|
|
15347
|
+
const depth = ctx.loopDepth;
|
|
15348
|
+
const source = resolveLoopSource(node, ctx, isClientOnly);
|
|
15349
|
+
const callback = node.arguments[0];
|
|
15350
|
+
if (!ts19.isArrowFunction(callback)) {
|
|
15351
|
+
refuseUnloweredJsxCallback(node, ctx, method, diagCountAtEntry);
|
|
15352
|
+
return null;
|
|
15353
|
+
}
|
|
15354
|
+
const row = resolveLoopRowParams(callback, source, ctx);
|
|
15355
|
+
const body = withLoopRowScope(ctx, row, (savedScope) => transformLoopCallbackBody(callback, row, savedScope, ctx, isClientOnly, method));
|
|
15356
|
+
const { children, flatMapCallback } = body;
|
|
15357
|
+
if (children.length === 0 && !flatMapCallback) {
|
|
15358
|
+
refuseUnloweredJsxCallback(node, ctx, method, diagCountAtEntry);
|
|
15359
|
+
return null;
|
|
15360
|
+
}
|
|
15361
|
+
const { key, bodyIsItemConditional } = resolveLoopKey(callback, children, ctx, isNested, depth);
|
|
15362
|
+
refusePreambleDerivedKey(node, key, body.preamble, ctx);
|
|
15363
|
+
const preamble = applyPreambleBuilderPasses(node, children, body.preamble, ctx);
|
|
15364
|
+
tagLoopItemRootComponents(children);
|
|
15365
|
+
const childComponent = loopChildComponentOf(children);
|
|
15366
|
+
const bodyIsMultiRoot = loopBodyIsMultiRoot(children);
|
|
15367
|
+
const arrayKind = classifyLoopArray(source, method, ctx);
|
|
15368
|
+
const preambleRegions = markPreambleReactivity(children, preamble, arrayKind.isStaticArray, ctx);
|
|
15369
|
+
const nestedComponents = collectNestedComponents(children).filter((c) => c.name !== childComponent?.name);
|
|
15114
15370
|
return {
|
|
15115
15371
|
type: "loop",
|
|
15116
15372
|
method: method === "flatMap" ? "flatMap" : undefined,
|
|
15117
|
-
array,
|
|
15118
|
-
templateArray,
|
|
15373
|
+
array: source.array,
|
|
15374
|
+
templateArray: source.templateArray,
|
|
15119
15375
|
arrayType: null,
|
|
15120
|
-
itemType:
|
|
15121
|
-
param,
|
|
15122
|
-
index,
|
|
15376
|
+
itemType: scalarLoopItemType(row.itemTypeNode, ctx),
|
|
15377
|
+
param: row.param,
|
|
15378
|
+
index: row.index,
|
|
15123
15379
|
key,
|
|
15124
15380
|
children,
|
|
15125
15381
|
slotId: null,
|
|
15126
15382
|
markerId: `l${ctx.loopMarkerCounter++}`,
|
|
15127
|
-
isStaticArray,
|
|
15128
|
-
isPropDerivedArray: isDirectPropArray || undefined,
|
|
15129
|
-
callsReactiveGetters: callsReactive || undefined,
|
|
15130
|
-
hasFunctionCalls: hasCalls || undefined,
|
|
15383
|
+
isStaticArray: arrayKind.isStaticArray,
|
|
15384
|
+
isPropDerivedArray: arrayKind.isDirectPropArray || undefined,
|
|
15385
|
+
callsReactiveGetters: arrayKind.callsReactive || undefined,
|
|
15386
|
+
hasFunctionCalls: arrayKind.hasCalls || undefined,
|
|
15131
15387
|
bodyIsMultiRoot: bodyIsMultiRoot || undefined,
|
|
15132
15388
|
bodyIsItemConditional: bodyIsItemConditional || undefined,
|
|
15133
15389
|
childComponent,
|
|
15134
15390
|
nestedComponents,
|
|
15135
|
-
filterPredicate,
|
|
15136
|
-
sortComparator,
|
|
15137
|
-
chainOrder,
|
|
15138
|
-
iterationShape,
|
|
15139
|
-
objectIteration,
|
|
15391
|
+
filterPredicate: source.filterPredicate,
|
|
15392
|
+
sortComparator: source.sortComparator,
|
|
15393
|
+
chainOrder: source.chainOrder,
|
|
15394
|
+
iterationShape: source.iterationShape,
|
|
15395
|
+
objectIteration: source.objectIteration,
|
|
15140
15396
|
depth,
|
|
15141
15397
|
clientOnly: isClientOnly || undefined,
|
|
15142
15398
|
preamble,
|
|
15143
15399
|
preambleRegions: preambleRegions && preambleRegions.length > 0 ? preambleRegions : undefined,
|
|
15144
|
-
paramType,
|
|
15145
|
-
indexType,
|
|
15146
|
-
paramBindings,
|
|
15147
|
-
arrayFreeIdentifiers: extractFreeIdentifiersFromNode(arrayExpr),
|
|
15400
|
+
paramType: row.paramType,
|
|
15401
|
+
indexType: row.indexType,
|
|
15402
|
+
paramBindings: row.paramBindings,
|
|
15403
|
+
arrayFreeIdentifiers: extractFreeIdentifiersFromNode(source.arrayExpr),
|
|
15148
15404
|
flatMapCallback,
|
|
15149
15405
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath)
|
|
15150
15406
|
};
|
|
@@ -17883,17 +18139,18 @@ function collectInnerLoops(nodes, siblingOffsets, outerLoopParam, ctx, options)
|
|
|
17883
18139
|
},
|
|
17884
18140
|
loop: ({ node: n, scope, descend }) => {
|
|
17885
18141
|
const emitDepth = fixedDepth ?? scope.depth + 1;
|
|
17886
|
-
const
|
|
18142
|
+
const rowBindings = withRowAccessor(n.paramBindings, n.markerId);
|
|
18143
|
+
const loopParamsForTemplate = outerSpec ? [{ ...outerSpec, shadowed: rowBoundNames(n) }, { param: n.param, bindings: rowBindings, index: n.index }] : undefined;
|
|
17887
18144
|
const template = n.children.map((c) => irToPlaceholderTemplate(c, undefined, emitDepth, loopParamsForTemplate)).join("");
|
|
17888
18145
|
const bindings = emptyLoopChildBindings();
|
|
17889
18146
|
const innerPreambleNames = preambleNamesOf(n);
|
|
17890
18147
|
if (ctx) {
|
|
17891
18148
|
for (const child of n.children) {
|
|
17892
|
-
bindings.reactiveTexts.push(...collectLoopChildReactiveTexts(child, ctx, n.param,
|
|
17893
|
-
bindings.reactiveAttrs.push(...collectLoopChildReactiveAttrs(child, ctx, n.param,
|
|
18149
|
+
bindings.reactiveTexts.push(...collectLoopChildReactiveTexts(child, ctx, n.param, rowBindings, true, innerPreambleNames, n.index, scope.bindingScope));
|
|
18150
|
+
bindings.reactiveAttrs.push(...collectLoopChildReactiveAttrs(child, ctx, n.param, rowBindings, true, innerPreambleNames, n.index, scope.bindingScope));
|
|
17894
18151
|
bindings.refs.push(...collectLoopChildRefs(child));
|
|
17895
18152
|
}
|
|
17896
|
-
bindings.conditionals.push(...collectLoopChildConditionals({ type: "fragment", children: n.children, loc: n.loc }, ctx, siblingOffsets, n.param,
|
|
18153
|
+
bindings.conditionals.push(...collectLoopChildConditionals({ type: "fragment", children: n.children, loc: n.loc }, ctx, siblingOffsets, n.param, rowBindings, innerPreambleNames, n.index, loopParamsForTemplate, scope.bindingScope));
|
|
17897
18154
|
}
|
|
17898
18155
|
let childComponents;
|
|
17899
18156
|
if (collectBindings) {
|
|
@@ -17924,7 +18181,7 @@ function collectInnerLoops(nodes, siblingOffsets, outerLoopParam, ctx, options)
|
|
|
17924
18181
|
array: n.array,
|
|
17925
18182
|
arrayFreeIdentifiers: n.arrayFreeIdentifiers,
|
|
17926
18183
|
param: n.param,
|
|
17927
|
-
paramBindings:
|
|
18184
|
+
paramBindings: rowBindings,
|
|
17928
18185
|
index: n.index,
|
|
17929
18186
|
key: n.key,
|
|
17930
18187
|
markerId: n.markerId,
|
|
@@ -17944,7 +18201,7 @@ function collectInnerLoops(nodes, siblingOffsets, outerLoopParam, ctx, options)
|
|
|
17944
18201
|
descend({
|
|
17945
18202
|
...scope,
|
|
17946
18203
|
depth: scope.depth + 1,
|
|
17947
|
-
bindingScope: buildLoopRowScope(n.param,
|
|
18204
|
+
bindingScope: buildLoopRowScope(n.param, rowBindings, innerPreambleNames, n.index, scope.bindingScope)
|
|
17948
18205
|
});
|
|
17949
18206
|
}
|
|
17950
18207
|
}
|
|
@@ -20508,7 +20765,8 @@ function exprRefsLoopBinding(freeIds, loop) {
|
|
|
20508
20765
|
function destructureLoopParam(param, paramBindings) {
|
|
20509
20766
|
if (param.startsWith("[") || param.startsWith("{")) {
|
|
20510
20767
|
if (paramBindings && paramBindings.length > 0) {
|
|
20511
|
-
|
|
20768
|
+
const accessor = paramBindings[0].accessor;
|
|
20769
|
+
return { head: accessor ? accessor.replace(/\(\)$/, "") : "__bfItem", unwrap: "" };
|
|
20512
20770
|
}
|
|
20513
20771
|
return {
|
|
20514
20772
|
head: "__bfItem",
|
|
@@ -20743,7 +21001,9 @@ function buildBranchInnerLoopsPlan(args) {
|
|
|
20743
21001
|
if (!inner.template)
|
|
20744
21002
|
continue;
|
|
20745
21003
|
const wrapInner = (expr) => wrapLoopParamAsAccessor(expr, inner.param, inner.paramBindings, inner.index);
|
|
20746
|
-
const
|
|
21004
|
+
const innerShadowed = rowBoundNames(inner);
|
|
21005
|
+
const wrapOuterRow = (expr, shadowed) => wrapOuter(expr, unionNames(innerShadowed, shadowed));
|
|
21006
|
+
const wrapBoth = (expr, shadowed) => wrapLoopParamAsAccessor(wrapOuterRow(expr, shadowed), inner.param, inner.paramBindings, inner.index, shadowed);
|
|
20747
21007
|
const csl = inner.containerSlotId;
|
|
20748
21008
|
const containerExpr = csl ? `(${scopeVar}.querySelector('[bf="${csl}"]') ?? ${scopeVar}.querySelector(\`[${BF_HOST2}="\${__scopeId}"][${BF_AT2}="${csl}"]\`) ?? ${scopeVar})` : `findCondContainer(${scopeVar}, '${condSlotId}')`;
|
|
20749
21009
|
const { head: paramHead, unwrap: paramUnwrap } = destructureLoopParam(inner.param, inner.paramBindings);
|
|
@@ -20922,7 +21182,7 @@ function buildLoopChildArmPlan(args) {
|
|
|
20922
21182
|
// src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts
|
|
20923
21183
|
function buildReactiveEffectsPlan(args) {
|
|
20924
21184
|
const { attrs, texts, conditionals, loopParam, loopParamBindings, loopIndex, profileComponentName } = args;
|
|
20925
|
-
const wrap = (expr) => wrapLoopParamAsAccessor(expr, loopParam, loopParamBindings, loopIndex);
|
|
21185
|
+
const wrap = (expr, shadowed) => wrapLoopParamAsAccessor(expr, loopParam, loopParamBindings, loopIndex, shadowed);
|
|
20926
21186
|
const attrsBySlot = new Map;
|
|
20927
21187
|
for (const attr of attrs) {
|
|
20928
21188
|
let bucket = attrsBySlot.get(attr.childSlotId);
|
|
@@ -21728,7 +21988,7 @@ function wrapAttrValueExpression2(value, wrap) {
|
|
|
21728
21988
|
}
|
|
21729
21989
|
function buildInnerLoopsPlan(args) {
|
|
21730
21990
|
const { levels, parentElVar, outerLoopParam, outerLoopParamBindings, outerLoopIndex, rowHasPortal } = args;
|
|
21731
|
-
const wrapOuter = outerLoopParam ? (expr) => wrapLoopParamAsAccessor(expr, outerLoopParam, outerLoopParamBindings, outerLoopIndex) : (expr) => expr;
|
|
21991
|
+
const wrapOuter = args.wrapOuter ?? (outerLoopParam ? (expr, shadowed) => wrapLoopParamAsAccessor(expr, outerLoopParam, outerLoopParamBindings, outerLoopIndex, shadowed) : (expr) => expr);
|
|
21732
21992
|
const plan = [];
|
|
21733
21993
|
let i = 0;
|
|
21734
21994
|
while (i < levels.length) {
|
|
@@ -21757,7 +22017,8 @@ function buildInnerLoopsPlan(args) {
|
|
|
21757
22017
|
parentElVar: `__innerEl${uidSuffix}`,
|
|
21758
22018
|
outerLoopParam: inner.param,
|
|
21759
22019
|
outerLoopParamBindings: inner.paramBindings,
|
|
21760
|
-
outerLoopIndex: inner.index
|
|
22020
|
+
outerLoopIndex: inner.index,
|
|
22021
|
+
wrapOuter: (expr, shadowed) => wrapLoopParamAsAccessor(wrapOuter(expr, unionNames(rowBoundNames(inner), shadowed)), inner.param, inner.paramBindings, inner.index, shadowed)
|
|
21761
22022
|
}) : [];
|
|
21762
22023
|
plan.push({
|
|
21763
22024
|
uidSuffix,
|
|
@@ -21780,7 +22041,9 @@ function buildInnerLoopsPlan(args) {
|
|
|
21780
22041
|
}
|
|
21781
22042
|
function buildReactiveEmit(inner, level, wrapOuter, uidSuffix, outerLoopParam, outerLoopParamBindings, outerLoopIndex) {
|
|
21782
22043
|
const wrapInner = (expr) => wrapLoopParamAsAccessor(expr, inner.param, inner.paramBindings, inner.index);
|
|
21783
|
-
const
|
|
22044
|
+
const innerShadowed = rowBoundNames(inner);
|
|
22045
|
+
const wrapOuterRow = (expr, shadowed) => wrapOuter(expr, unionNames(innerShadowed, shadowed));
|
|
22046
|
+
const wrapBoth = (expr, shadowed) => wrapLoopParamAsAccessor(wrapOuterRow(expr, shadowed), inner.param, inner.paramBindings, inner.index, shadowed);
|
|
21784
22047
|
const { head: paramHead, unwrap: paramUnwrap } = destructureLoopParam(inner.param, inner.paramBindings);
|
|
21785
22048
|
const wrappedKey = inner.key ? wrapLoopParamAsAccessor(inner.key, inner.param, inner.paramBindings, inner.index) : null;
|
|
21786
22049
|
const wrapIRNode = (node) => {
|
|
@@ -21813,11 +22076,11 @@ function buildReactiveEmit(inner, level, wrapOuter, uidSuffix, outerLoopParam, o
|
|
|
21813
22076
|
}));
|
|
21814
22077
|
const reactiveTexts = inner.bindings.reactiveTexts.map((text) => ({
|
|
21815
22078
|
slotId: text.slotId,
|
|
21816
|
-
wrappedExpression: wrapLoopParamAsAccessor(
|
|
22079
|
+
wrappedExpression: wrapLoopParamAsAccessor(wrapOuterRow(text.expression), inner.param, inner.paramBindings, inner.index),
|
|
21817
22080
|
insideConditional: !!text.insideConditional
|
|
21818
22081
|
}));
|
|
21819
22082
|
const reactiveAttrs = inner.bindings.reactiveAttrs.map((attr) => {
|
|
21820
|
-
const wrapped = wrapLoopParamAsAccessor(
|
|
22083
|
+
const wrapped = wrapLoopParamAsAccessor(wrapOuterRow(attr.expression), inner.param, inner.paramBindings, inner.index);
|
|
21821
22084
|
return {
|
|
21822
22085
|
slotId: attr.childSlotId,
|
|
21823
22086
|
attrName: attr.attrName,
|
|
@@ -21833,11 +22096,11 @@ function buildReactiveEmit(inner, level, wrapOuter, uidSuffix, outerLoopParam, o
|
|
|
21833
22096
|
preludeStatements.push(paramUnwrap);
|
|
21834
22097
|
if (inner.preamble) {
|
|
21835
22098
|
const leafLoopParams = outerLoopParam ? [
|
|
21836
|
-
{ param: outerLoopParam, bindings: outerLoopParamBindings, index: outerLoopIndex },
|
|
22099
|
+
{ param: outerLoopParam, bindings: outerLoopParamBindings, index: outerLoopIndex, shadowed: innerShadowed },
|
|
21837
22100
|
{ param: inner.param, bindings: inner.paramBindings, index: inner.index }
|
|
21838
22101
|
] : [{ param: inner.param, bindings: inner.paramBindings, index: inner.index }];
|
|
21839
22102
|
preludeStatements.push(renderPreamble(inner.preamble, {
|
|
21840
|
-
transformJs: (t) => wrapInner(
|
|
22103
|
+
transformJs: (t) => wrapInner(wrapOuterRow(t)),
|
|
21841
22104
|
renderLeaf: (ir) => irToHtmlTemplate(ir, undefined, 1, leafLoopParams, undefined)
|
|
21842
22105
|
}));
|
|
21843
22106
|
}
|
|
@@ -24366,7 +24629,7 @@ function emitKeyedLookup(ls, ev, handlerCall, lookup) {
|
|
|
24366
24629
|
ls.push(` const key = li.getAttribute('${DATA_KEY}')`);
|
|
24367
24630
|
if (hasBindings) {
|
|
24368
24631
|
ls.push(` const __bfLoopItem = ${arrayExpr}.find(item => String(${keyWithItem}) === key)`);
|
|
24369
|
-
ls.push(` if (__bfLoopItem) {`);
|
|
24632
|
+
ls.push(` if (__bfLoopItem !== undefined) {`);
|
|
24370
24633
|
ls.push(` const ${param} = __bfLoopItem`);
|
|
24371
24634
|
if (preambleLine)
|
|
24372
24635
|
ls.push(` ${preambleLine}`);
|
|
@@ -24376,7 +24639,7 @@ function emitKeyedLookup(ls, ev, handlerCall, lookup) {
|
|
|
24376
24639
|
ls.push(` }`);
|
|
24377
24640
|
} else {
|
|
24378
24641
|
ls.push(` const ${param} = ${arrayExpr}.find(item => String(${keyWithItem}) === key)`);
|
|
24379
|
-
ls.push(` if (${param}) {`);
|
|
24642
|
+
ls.push(` if (${param} !== undefined) {`);
|
|
24380
24643
|
if (preambleLine)
|
|
24381
24644
|
ls.push(` ${preambleLine}`);
|
|
24382
24645
|
if (idxLine2)
|
|
@@ -24405,12 +24668,12 @@ function emitKeyedLookup(ls, ev, handlerCall, lookup) {
|
|
|
24405
24668
|
const rawKey = nested.key ?? "";
|
|
24406
24669
|
const innerKeyExpr = nested.paramBindings && nested.paramBindings.length > 0 ? substituteLoopBindings(rawKey, nested.paramBindings, "item") : rawKey.replace(identifierPattern(nested.param, "g"), "item");
|
|
24407
24670
|
const outerRef = hasBindings ? "__bfLoopItem" : param;
|
|
24408
|
-
ls.push(` const ${nested.param} = ${outerRef}
|
|
24671
|
+
ls.push(` const ${nested.param} = ${outerRef} !== undefined ? ${nested.array}.find(item => String(${innerKeyExpr}) === innerKey${nested.depth}) : undefined`);
|
|
24409
24672
|
}
|
|
24410
24673
|
const outerGuard = hasBindings ? "__bfLoopItem" : param;
|
|
24411
24674
|
const allParams = [outerGuard, ...ev.nestedLoops.map((n) => n.param)];
|
|
24412
24675
|
const idxLine = indexBindingLine(ev.handler, indexParam, `${arrayExpr}.findIndex(item => String(${keyWithItem}) === outerKey)`);
|
|
24413
|
-
ls.push(` if (${allParams.join(" && ")}) {`);
|
|
24676
|
+
ls.push(` if (${allParams.map((p) => `${p} !== undefined`).join(" && ")}) {`);
|
|
24414
24677
|
if (preambleLine)
|
|
24415
24678
|
ls.push(` ${preambleLine}`);
|
|
24416
24679
|
if (idxLine)
|
|
@@ -24427,7 +24690,7 @@ function emitDynamicIndexLookup(ls, ev, handlerCall, lookup) {
|
|
|
24427
24690
|
ls.push(` const idx = Array.from(li.parentElement.children).indexOf(li)`);
|
|
24428
24691
|
if (hasBindings) {
|
|
24429
24692
|
ls.push(` const __bfLoopItem = ${arrayExpr}[idx]`);
|
|
24430
|
-
ls.push(` if (__bfLoopItem) {`);
|
|
24693
|
+
ls.push(` if (__bfLoopItem !== undefined) {`);
|
|
24431
24694
|
ls.push(` const ${param} = __bfLoopItem`);
|
|
24432
24695
|
if (preambleLine)
|
|
24433
24696
|
ls.push(` ${preambleLine}`);
|
|
@@ -24437,7 +24700,7 @@ function emitDynamicIndexLookup(ls, ev, handlerCall, lookup) {
|
|
|
24437
24700
|
ls.push(` }`);
|
|
24438
24701
|
} else {
|
|
24439
24702
|
ls.push(` const ${param} = ${arrayExpr}[idx]`);
|
|
24440
|
-
ls.push(` if (${param}) {`);
|
|
24703
|
+
ls.push(` if (${param} !== undefined) {`);
|
|
24441
24704
|
if (preambleLine)
|
|
24442
24705
|
ls.push(` ${preambleLine}`);
|
|
24443
24706
|
if (idxLine)
|
|
@@ -24457,7 +24720,7 @@ function emitStaticIndexLookup(ls, ev, handlerCall, lookup, containerVar) {
|
|
|
24457
24720
|
const idxOffset = buildLoopChildIndexSubtraction(offset ?? undefined);
|
|
24458
24721
|
ls.push(` const __idx = Array.from(${containerVar}.children).indexOf(__el)${idxOffset}`);
|
|
24459
24722
|
ls.push(` const ${param} = ${arrayExpr}[__idx]`);
|
|
24460
|
-
ls.push(` if (${param}) {`);
|
|
24723
|
+
ls.push(` if (${param} !== undefined) {`);
|
|
24461
24724
|
const { guardNames: nestedGuardNames, paramLines: nestedParamLines } = emitNestedIndexParams(ls, ev, "__el", " ");
|
|
24462
24725
|
const guarded = nestedGuardNames.length > 0;
|
|
24463
24726
|
if (guarded)
|
|
@@ -25456,7 +25719,8 @@ function generateTemplateOnlyMount(ir, ctx) {
|
|
|
25456
25719
|
lines.push(`function ${initName}() {}`);
|
|
25457
25720
|
lines.push("");
|
|
25458
25721
|
const nameField = registryKey !== name ? `, name: '${name}'` : "";
|
|
25459
|
-
const
|
|
25722
|
+
const transparentFlag = isTransparentFragmentRoot(ir.root) ? ["transparent: true"] : [];
|
|
25723
|
+
const scopeFlags = [...componentDefScopeFlags(ir.root), ...transparentFlag].map((f) => `, ${f}`).join("");
|
|
25460
25724
|
lines.push(`hydrate('${registryKey}', { init: ${initName}, template: (${PROPS_PARAM}) => \`${templateHtml}\`${scopeFlags}${nameField} })`);
|
|
25461
25725
|
lines.push(`export function ${name}(${PROPS_PARAM}, __bfKey) { return createComponent('${registryKey}', ${PROPS_PARAM}, __bfKey) }`);
|
|
25462
25726
|
const generatedCode = lines.join(`
|
|
@@ -28854,6 +29118,17 @@ function isStringTypedOperand(expr, isStringName) {
|
|
|
28854
29118
|
function isStringConcatBinary(op, left, right, isStringName) {
|
|
28855
29119
|
return op === "+" && (isStringTypedOperand(left, isStringName) || isStringTypedOperand(right, isStringName));
|
|
28856
29120
|
}
|
|
29121
|
+
function isNumericArithmeticResult(expr, isStringName) {
|
|
29122
|
+
if (!expr)
|
|
29123
|
+
return false;
|
|
29124
|
+
if (expr.kind === "unary")
|
|
29125
|
+
return expr.op === "-";
|
|
29126
|
+
if (expr.kind !== "binary")
|
|
29127
|
+
return false;
|
|
29128
|
+
if (expr.op === "+")
|
|
29129
|
+
return !isStringConcatBinary(expr.op, expr.left, expr.right, isStringName);
|
|
29130
|
+
return expr.op === "-" || expr.op === "*" || expr.op === "/" || expr.op === "%";
|
|
29131
|
+
}
|
|
28857
29132
|
function groupBinaryOperand(operand, emitted) {
|
|
28858
29133
|
return operand.kind === "binary" || operand.kind === "logical" || operand.kind === "conditional" ? `(${emitted})` : emitted;
|
|
28859
29134
|
}
|
|
@@ -29002,6 +29277,122 @@ function collectLoopBoundNames(ir) {
|
|
|
29002
29277
|
visit3(ir.root);
|
|
29003
29278
|
return names;
|
|
29004
29279
|
}
|
|
29280
|
+
// src/adapters/filter-capture-collisions.ts
|
|
29281
|
+
function filterCaptureCollisions(loop) {
|
|
29282
|
+
const filter = loop.filterPredicate;
|
|
29283
|
+
if (!filter?.predicate)
|
|
29284
|
+
return [];
|
|
29285
|
+
const free = freeIdentifiers(filter.predicate);
|
|
29286
|
+
if (!free)
|
|
29287
|
+
return [];
|
|
29288
|
+
free.delete(filter.param);
|
|
29289
|
+
const rowBound = new Set(loop.paramBindings && loop.paramBindings.length > 0 ? loop.paramBindings.map((b) => b.name) : [loop.param]);
|
|
29290
|
+
if (loop.index)
|
|
29291
|
+
rowBound.add(loop.index);
|
|
29292
|
+
return [...free].filter((name) => rowBound.has(name)).sort();
|
|
29293
|
+
}
|
|
29294
|
+
// src/adapters/template-name-collisions.ts
|
|
29295
|
+
function templateNameCollisionErrors(ir, propNames, ident, adapterName, componentName) {
|
|
29296
|
+
const { metadata } = ir;
|
|
29297
|
+
const rootNames = new Set([
|
|
29298
|
+
...propNames,
|
|
29299
|
+
...metadata.localConstants.map((c) => c.name),
|
|
29300
|
+
...metadata.localFunctions.map((f) => f.name),
|
|
29301
|
+
...metadata.signals.flatMap((s) => s.setter ? [s.getter, s.setter] : [s.getter]),
|
|
29302
|
+
...metadata.memos.map((m) => m.name)
|
|
29303
|
+
]);
|
|
29304
|
+
const errors = [];
|
|
29305
|
+
const reported = new Set;
|
|
29306
|
+
const check = (scope) => {
|
|
29307
|
+
const byTarget = new Map;
|
|
29308
|
+
for (const name of new Set([...rootNames, ...scope.boundNames()])) {
|
|
29309
|
+
const target = ident(name);
|
|
29310
|
+
const group = byTarget.get(target);
|
|
29311
|
+
if (group)
|
|
29312
|
+
group.push(name);
|
|
29313
|
+
else
|
|
29314
|
+
byTarget.set(target, [name]);
|
|
29315
|
+
}
|
|
29316
|
+
for (const [target, group] of byTarget) {
|
|
29317
|
+
if (group.length < 2)
|
|
29318
|
+
continue;
|
|
29319
|
+
const key = `${target}\x00${[...group].sort().join("\x00")}`;
|
|
29320
|
+
if (reported.has(key))
|
|
29321
|
+
continue;
|
|
29322
|
+
reported.add(key);
|
|
29323
|
+
const list = group.map((n) => `\`${n}\``).join(" and ");
|
|
29324
|
+
errors.push({
|
|
29325
|
+
code: "BF105",
|
|
29326
|
+
severity: "error",
|
|
29327
|
+
message: `${list} both become the ${adapterName} template variable \`${target}\`, so they would read the same value. ${adapterName} reserves some names and renames them, and the new name collides with another name in this component.`,
|
|
29328
|
+
loc: { file: `${componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
29329
|
+
suggestion: {
|
|
29330
|
+
message: `Rename one of ${list} so the two names stay distinct after the ${adapterName} renaming.`,
|
|
29331
|
+
escape: [{ kind: "rewrite" }]
|
|
29332
|
+
}
|
|
29333
|
+
});
|
|
29334
|
+
}
|
|
29335
|
+
};
|
|
29336
|
+
const visit3 = (node, scope) => {
|
|
29337
|
+
if (!node)
|
|
29338
|
+
return;
|
|
29339
|
+
switch (node.type) {
|
|
29340
|
+
case "element":
|
|
29341
|
+
case "component":
|
|
29342
|
+
case "fragment":
|
|
29343
|
+
case "provider":
|
|
29344
|
+
for (const child of node.children)
|
|
29345
|
+
visit3(child, scope);
|
|
29346
|
+
break;
|
|
29347
|
+
case "async":
|
|
29348
|
+
visit3(node.fallback, scope);
|
|
29349
|
+
for (const child of node.children)
|
|
29350
|
+
visit3(child, scope);
|
|
29351
|
+
break;
|
|
29352
|
+
case "loop": {
|
|
29353
|
+
if (node.filterPredicate)
|
|
29354
|
+
check(scope.enterCallback([node.filterPredicate.param]));
|
|
29355
|
+
const row = scope.enterLoopRow(node);
|
|
29356
|
+
check(row);
|
|
29357
|
+
for (const child of node.children)
|
|
29358
|
+
visit3(child, row);
|
|
29359
|
+
if (node.childComponent) {
|
|
29360
|
+
for (const child of node.childComponent.children)
|
|
29361
|
+
visit3(child, row);
|
|
29362
|
+
}
|
|
29363
|
+
for (const nested of node.nestedComponents ?? []) {
|
|
29364
|
+
for (const child of nested.children)
|
|
29365
|
+
visit3(child, row);
|
|
29366
|
+
}
|
|
29367
|
+
for (const seg of node.flatMapCallback?.segments ?? []) {
|
|
29368
|
+
if (seg.kind === "jsx")
|
|
29369
|
+
visit3(seg.ir, row);
|
|
29370
|
+
}
|
|
29371
|
+
for (const seg of node.preamble?.segments ?? []) {
|
|
29372
|
+
if (seg.kind === "jsx")
|
|
29373
|
+
visit3(seg.ir, row);
|
|
29374
|
+
}
|
|
29375
|
+
break;
|
|
29376
|
+
}
|
|
29377
|
+
case "conditional":
|
|
29378
|
+
visit3(node.whenTrue, scope);
|
|
29379
|
+
visit3(node.whenFalse, scope);
|
|
29380
|
+
break;
|
|
29381
|
+
case "if-statement":
|
|
29382
|
+
visit3(node.consequent, scope);
|
|
29383
|
+
if (node.alternate)
|
|
29384
|
+
visit3(node.alternate, scope);
|
|
29385
|
+
break;
|
|
29386
|
+
case "text":
|
|
29387
|
+
case "expression":
|
|
29388
|
+
case "slot":
|
|
29389
|
+
break;
|
|
29390
|
+
}
|
|
29391
|
+
};
|
|
29392
|
+
check(BindingScope.EMPTY);
|
|
29393
|
+
visit3(ir.root, BindingScope.EMPTY);
|
|
29394
|
+
return errors;
|
|
29395
|
+
}
|
|
29005
29396
|
// src/adapters/boolean-result.ts
|
|
29006
29397
|
var COMPARISON_OPS = new Set([
|
|
29007
29398
|
"<",
|
|
@@ -29013,7 +29404,7 @@ var COMPARISON_OPS = new Set([
|
|
|
29013
29404
|
"!=",
|
|
29014
29405
|
"!=="
|
|
29015
29406
|
]);
|
|
29016
|
-
function isBooleanResultParsed(node) {
|
|
29407
|
+
function isBooleanResultParsed(node, isBooleanName) {
|
|
29017
29408
|
switch (node.kind) {
|
|
29018
29409
|
case "literal":
|
|
29019
29410
|
return node.literalType === "boolean";
|
|
@@ -29022,9 +29413,15 @@ function isBooleanResultParsed(node) {
|
|
|
29022
29413
|
case "unary":
|
|
29023
29414
|
return node.op === "!";
|
|
29024
29415
|
case "logical":
|
|
29025
|
-
return isBooleanResultParsed(node.left) && isBooleanResultParsed(node.right);
|
|
29416
|
+
return isBooleanResultParsed(node.left, isBooleanName) && isBooleanResultParsed(node.right, isBooleanName);
|
|
29026
29417
|
case "conditional":
|
|
29027
|
-
return isBooleanResultParsed(node.consequent) && isBooleanResultParsed(node.alternate);
|
|
29418
|
+
return isBooleanResultParsed(node.consequent, isBooleanName) && isBooleanResultParsed(node.alternate, isBooleanName);
|
|
29419
|
+
case "call":
|
|
29420
|
+
return !!isBooleanName && node.callee.kind === "identifier" && node.args.length === 0 && isBooleanName(node.callee.name);
|
|
29421
|
+
case "member":
|
|
29422
|
+
return !!isBooleanName && !node.computed && node.object.kind === "identifier" && node.object.name === "props" && isBooleanName(node.property, true);
|
|
29423
|
+
case "identifier":
|
|
29424
|
+
return !!isBooleanName && isBooleanName(node.name);
|
|
29028
29425
|
default:
|
|
29029
29426
|
return false;
|
|
29030
29427
|
}
|
|
@@ -29051,45 +29448,892 @@ function stringifyBooleanBranch(branch) {
|
|
|
29051
29448
|
alternate: { kind: "literal", value: "false", literalType: "string" }
|
|
29052
29449
|
};
|
|
29053
29450
|
}
|
|
29054
|
-
|
|
29055
|
-
|
|
29056
|
-
|
|
29057
|
-
|
|
29058
|
-
|
|
29059
|
-
|
|
29060
|
-
|
|
29061
|
-
|
|
29062
|
-
|
|
29063
|
-
|
|
29064
|
-
|
|
29065
|
-
|
|
29066
|
-
|
|
29067
|
-
|
|
29068
|
-
|
|
29069
|
-
|
|
29070
|
-
|
|
29071
|
-
|
|
29451
|
+
function collectBooleanValueNames(metadata) {
|
|
29452
|
+
const isBoolean = (type) => type?.kind === "primitive" && type.primitive === "boolean";
|
|
29453
|
+
const isBooleanSeed = (type, seed) => {
|
|
29454
|
+
const text = seed?.trim();
|
|
29455
|
+
return isBoolean(type) || (!type || type.kind === "unknown") && (text === "true" || text === "false");
|
|
29456
|
+
};
|
|
29457
|
+
const names = new Set;
|
|
29458
|
+
const props = new Set;
|
|
29459
|
+
for (const signal of metadata.signals) {
|
|
29460
|
+
if (isBooleanSeed(signal.type, signal.initialValue))
|
|
29461
|
+
names.add(signal.getter);
|
|
29462
|
+
}
|
|
29463
|
+
for (const memo of metadata.memos ?? []) {
|
|
29464
|
+
if (isBoolean(memo.type))
|
|
29465
|
+
names.add(memo.name);
|
|
29466
|
+
}
|
|
29467
|
+
const provenParams = new Set;
|
|
29468
|
+
for (const param of metadata.propsParams) {
|
|
29469
|
+
if (!isBoolean(param.type))
|
|
29470
|
+
continue;
|
|
29471
|
+
if (param.optional && !(param.parsed && isBooleanResultParsed(param.parsed, (n, member) => !member && provenParams.has(n))))
|
|
29472
|
+
continue;
|
|
29473
|
+
provenParams.add(param.name);
|
|
29474
|
+
props.add(param.sourceName ?? param.name);
|
|
29475
|
+
if (!metadata.propsObjectName)
|
|
29476
|
+
names.add(param.name);
|
|
29477
|
+
}
|
|
29478
|
+
const propsObject = metadata.propsObjectName;
|
|
29479
|
+
const booleanTyped = new Set(metadata.propsParams.filter((p) => isBoolean(p.type)).map((p) => p.sourceName ?? p.name));
|
|
29480
|
+
const readsBooleanProp = (e) => {
|
|
29481
|
+
if (!e)
|
|
29482
|
+
return false;
|
|
29483
|
+
if (e.kind === "member") {
|
|
29484
|
+
return !e.computed && !!propsObject && e.object.kind === "identifier" && e.object.name === propsObject && props.has(e.property);
|
|
29072
29485
|
}
|
|
29073
|
-
|
|
29074
|
-
const
|
|
29075
|
-
|
|
29076
|
-
const resolved = evaluateStaticLiteral(element, bindings);
|
|
29077
|
-
if (!resolved)
|
|
29078
|
-
return null;
|
|
29079
|
-
values.push(resolved.value);
|
|
29080
|
-
}
|
|
29081
|
-
return { value: values };
|
|
29486
|
+
if (e.kind === "logical" && (e.op === "??" || e.op === "||")) {
|
|
29487
|
+
const left = e.left;
|
|
29488
|
+
return left.kind === "member" && !left.computed && !!propsObject && left.object.kind === "identifier" && left.object.name === propsObject && booleanTyped.has(left.property) && e.right.kind === "literal" && e.right.literalType === "boolean";
|
|
29082
29489
|
}
|
|
29083
|
-
|
|
29084
|
-
|
|
29085
|
-
|
|
29086
|
-
|
|
29087
|
-
|
|
29088
|
-
|
|
29089
|
-
|
|
29090
|
-
|
|
29091
|
-
|
|
29092
|
-
|
|
29490
|
+
return false;
|
|
29491
|
+
};
|
|
29492
|
+
for (const constant of metadata.localConstants ?? []) {
|
|
29493
|
+
if (readsBooleanProp(constant.parsed) || isBooleanSeed(constant.type ?? undefined, constant.value))
|
|
29494
|
+
names.add(constant.name);
|
|
29495
|
+
else
|
|
29496
|
+
names.delete(constant.name);
|
|
29497
|
+
}
|
|
29498
|
+
return { names, props };
|
|
29499
|
+
}
|
|
29500
|
+
function isBooleanResultExpr(expr) {
|
|
29501
|
+
const parsed = parseExpression(expr.trim());
|
|
29502
|
+
if (!parsed)
|
|
29503
|
+
return false;
|
|
29504
|
+
return isBooleanResultParsed(parsed);
|
|
29505
|
+
}
|
|
29506
|
+
function isExplicitStringCall(expr) {
|
|
29507
|
+
const parsed = parseExpression(expr.trim());
|
|
29508
|
+
return !!parsed && parsed.kind === "call" && parsed.callee.kind === "identifier" && parsed.callee.name === "String" && parsed.args.length === 1;
|
|
29509
|
+
}
|
|
29510
|
+
var ARIA_BOOLEAN_ATTRS = new Set([
|
|
29511
|
+
"aria-atomic",
|
|
29512
|
+
"aria-busy",
|
|
29513
|
+
"aria-disabled",
|
|
29514
|
+
"aria-hidden",
|
|
29515
|
+
"aria-modal",
|
|
29516
|
+
"aria-multiline",
|
|
29517
|
+
"aria-multiselectable",
|
|
29518
|
+
"aria-readonly",
|
|
29519
|
+
"aria-required",
|
|
29520
|
+
"aria-selected",
|
|
29521
|
+
"aria-expanded",
|
|
29522
|
+
"aria-checked",
|
|
29523
|
+
"aria-pressed"
|
|
29524
|
+
]);
|
|
29525
|
+
function isAriaBooleanAttr(name) {
|
|
29526
|
+
return ARIA_BOOLEAN_ATTRS.has(name);
|
|
29527
|
+
}
|
|
29528
|
+
// src/adapters/literal-value.ts
|
|
29529
|
+
function isStringTypeInfo(type) {
|
|
29530
|
+
return type?.kind === "primitive" && type.primitive === "string";
|
|
29531
|
+
}
|
|
29532
|
+
function isBareStringLiteral(initialValue) {
|
|
29533
|
+
if (!initialValue)
|
|
29534
|
+
return false;
|
|
29535
|
+
const v = initialValue.trim();
|
|
29536
|
+
return v.startsWith("'") && v.endsWith("'") || v.startsWith('"') && v.endsWith('"');
|
|
29537
|
+
}
|
|
29538
|
+
function isLiteralIndex(index) {
|
|
29539
|
+
return index.kind === "literal" && (index.literalType === "number" || index.literalType === "string");
|
|
29540
|
+
}
|
|
29541
|
+
// src/adapters/prop-classes.ts
|
|
29542
|
+
function collectBooleanTypedProps(ir) {
|
|
29543
|
+
return new Set(ir.metadata.propsParams.filter((prop) => prop.type?.primitive === "boolean" || prop.type?.raw === "boolean").map((prop) => prop.name));
|
|
29544
|
+
}
|
|
29545
|
+
function collectNullableOptionalProps(ir) {
|
|
29546
|
+
return new Set([
|
|
29547
|
+
...ir.metadata.propsParams.filter((p) => p.defaultValue === undefined && !p.isRest && (p.type?.kind !== "primitive" || p.optional)).map((p) => p.name),
|
|
29548
|
+
...ir.metadata.restPropsExpandedKeys
|
|
29549
|
+
]);
|
|
29550
|
+
}
|
|
29551
|
+
function isBooleanTypedPropRef(expr, ctx) {
|
|
29552
|
+
let bare = expr.trim();
|
|
29553
|
+
if (ctx.propsObjectName && bare.startsWith(`${ctx.propsObjectName}.`)) {
|
|
29554
|
+
bare = bare.slice(ctx.propsObjectName.length + 1);
|
|
29555
|
+
}
|
|
29556
|
+
if (!/^[A-Za-z_$][\w$]*$/.test(bare))
|
|
29557
|
+
return false;
|
|
29558
|
+
if (ctx.isBound(bare))
|
|
29559
|
+
return false;
|
|
29560
|
+
return ctx.booleanTypedProps.has(bare);
|
|
29561
|
+
}
|
|
29562
|
+
// src/adapters/component-tree.ts
|
|
29563
|
+
function hasClientInteractivity(ir) {
|
|
29564
|
+
return ir.metadata.signals.length > 0 || ir.metadata.effects.length > 0 || ir.metadata.onMounts.length > 0 || (ir.metadata.clientAnalysis?.needsInit ?? false);
|
|
29565
|
+
}
|
|
29566
|
+
function collectImportedLoopChildComponentErrors(ir, componentName, adapterName) {
|
|
29567
|
+
const errors = [];
|
|
29568
|
+
const relativeImports = new Set;
|
|
29569
|
+
for (const imp of ir.metadata.templateImports ?? ir.metadata.imports ?? []) {
|
|
29570
|
+
if (!imp.source.startsWith("./") && !imp.source.startsWith("../"))
|
|
29571
|
+
continue;
|
|
29572
|
+
if (imp.isTypeOnly)
|
|
29573
|
+
continue;
|
|
29574
|
+
for (const spec of imp.specifiers) {
|
|
29575
|
+
relativeImports.add(spec.alias ?? spec.name);
|
|
29576
|
+
}
|
|
29577
|
+
}
|
|
29578
|
+
if (relativeImports.size === 0)
|
|
29579
|
+
return errors;
|
|
29580
|
+
const loc = { file: componentName + ".tsx", start: { line: 1, column: 0 }, end: { line: 1, column: 0 } };
|
|
29581
|
+
const visit3 = (node, inLoop) => {
|
|
29582
|
+
if (node.type === "component" && inLoop && relativeImports.has(node.name)) {
|
|
29583
|
+
errors.push(importedLoopChildError(node, loc, adapterName));
|
|
29584
|
+
}
|
|
29585
|
+
const childInLoop = inLoop || node.type === "loop";
|
|
29586
|
+
for (const child of childNodes(node))
|
|
29587
|
+
visit3(child, childInLoop);
|
|
29588
|
+
};
|
|
29589
|
+
visit3(ir.root, false);
|
|
29590
|
+
return errors;
|
|
29591
|
+
}
|
|
29592
|
+
function importedLoopChildError(comp, loc, adapterName) {
|
|
29593
|
+
return {
|
|
29594
|
+
code: "BF103",
|
|
29595
|
+
severity: "error",
|
|
29596
|
+
message: `Component <${comp.name}> is imported from a sibling module and used inside a loop. The ${adapterName} adapter emits a cross-template call; the child template must be registered alongside the parent at render time.`,
|
|
29597
|
+
loc: comp.loc ?? loc,
|
|
29598
|
+
suggestion: {
|
|
29599
|
+
message: `Options:
|
|
29600
|
+
` + ` 1. Compile '${comp.name}' (its source file) with the same adapter and register the resulting ${adapterName} template alongside the parent at render time.
|
|
29601
|
+
` + ` 2. Inline <${comp.name}> directly inside the loop body so no cross-file template lookup is needed.
|
|
29602
|
+
` + ` 3. Mark the loop position as @client-only so the template is materialised on the client instead of at SSR time.`
|
|
29603
|
+
}
|
|
29604
|
+
};
|
|
29605
|
+
}
|
|
29606
|
+
function childNodes(node) {
|
|
29607
|
+
switch (node.type) {
|
|
29608
|
+
case "component":
|
|
29609
|
+
case "element":
|
|
29610
|
+
case "fragment":
|
|
29611
|
+
case "loop":
|
|
29612
|
+
case "provider":
|
|
29613
|
+
return node.children;
|
|
29614
|
+
case "conditional": {
|
|
29615
|
+
const cond = node;
|
|
29616
|
+
return cond.whenFalse ? [cond.whenTrue, cond.whenFalse] : [cond.whenTrue];
|
|
29617
|
+
}
|
|
29618
|
+
case "if-statement": {
|
|
29619
|
+
const stmt = node;
|
|
29620
|
+
return stmt.alternate ? [stmt.consequent, stmt.alternate] : [stmt.consequent];
|
|
29621
|
+
}
|
|
29622
|
+
case "async": {
|
|
29623
|
+
const a = node;
|
|
29624
|
+
return [a.fallback, ...a.children];
|
|
29625
|
+
}
|
|
29626
|
+
default:
|
|
29627
|
+
return [];
|
|
29628
|
+
}
|
|
29629
|
+
}
|
|
29630
|
+
// src/adapters/array-method.ts
|
|
29631
|
+
var required = { kind: "required" };
|
|
29632
|
+
var optional = { kind: "optional" };
|
|
29633
|
+
var zero = { kind: "default", value: { kind: "number", value: 0 } };
|
|
29634
|
+
var ARRAY_METHOD_CATALOGUE = {
|
|
29635
|
+
join: { helper: "join", params: [{ kind: "default", value: { kind: "string", value: "," } }] },
|
|
29636
|
+
includes: { helper: "includes", params: [required] },
|
|
29637
|
+
indexOf: { helper: "index_of", params: [required] },
|
|
29638
|
+
lastIndexOf: { helper: "last_index_of", params: [required] },
|
|
29639
|
+
at: { helper: "at", params: [zero] },
|
|
29640
|
+
concat: { helper: "concat", params: [required], receiverWhenNoArgs: true },
|
|
29641
|
+
slice: { helper: "slice", params: [zero, { kind: "default", value: { kind: "undefined" } }] },
|
|
29642
|
+
reverse: { helper: "reverse", params: [] },
|
|
29643
|
+
toReversed: { helper: "reverse", params: [] },
|
|
29644
|
+
toLowerCase: { helper: "lc", params: [] },
|
|
29645
|
+
toUpperCase: { helper: "uc", params: [] },
|
|
29646
|
+
trim: { helper: "trim", params: [] },
|
|
29647
|
+
trimStart: { helper: "trim_start", params: [] },
|
|
29648
|
+
trimEnd: { helper: "trim_end", params: [] },
|
|
29649
|
+
toFixed: { helper: "to_fixed", params: [zero] },
|
|
29650
|
+
split: { helper: "split", params: [optional, optional] },
|
|
29651
|
+
startsWith: { helper: "starts_with", params: [required, optional] },
|
|
29652
|
+
endsWith: { helper: "ends_with", params: [required, optional] },
|
|
29653
|
+
replace: { helper: "replace", params: [required, required] },
|
|
29654
|
+
replaceAll: { helper: "replace_all", params: [required, required] },
|
|
29655
|
+
repeat: { helper: "repeat", params: [zero] },
|
|
29656
|
+
padStart: { helper: "pad_start", params: [zero, optional] },
|
|
29657
|
+
padEnd: { helper: "pad_end", params: [zero, optional] }
|
|
29658
|
+
};
|
|
29659
|
+
function lowerArrayMethod(method, object, args) {
|
|
29660
|
+
const entry = ARRAY_METHOD_CATALOGUE[method];
|
|
29661
|
+
if (entry.receiverWhenNoArgs && args.length === 0)
|
|
29662
|
+
return { kind: "receiver", object };
|
|
29663
|
+
const out = [];
|
|
29664
|
+
for (const [i, param] of entry.params.entries()) {
|
|
29665
|
+
const arg = args[i];
|
|
29666
|
+
if (arg) {
|
|
29667
|
+
out.push({ kind: "expr", expr: arg });
|
|
29668
|
+
} else if (param.kind === "default") {
|
|
29669
|
+
out.push({ kind: "default", value: param.value });
|
|
29670
|
+
} else if (param.kind === "optional") {
|
|
29671
|
+
break;
|
|
29672
|
+
} else {
|
|
29673
|
+
throw new Error(`lowerArrayMethod: '.${method}()' needs argument ${i + 1}`);
|
|
29674
|
+
}
|
|
29675
|
+
}
|
|
29676
|
+
return { kind: "call", helper: entry.helper, object, args: out };
|
|
29677
|
+
}
|
|
29678
|
+
function emitArrayMethod(plan, emit, spelling) {
|
|
29679
|
+
if (plan.kind === "receiver")
|
|
29680
|
+
return emit(plan.object);
|
|
29681
|
+
const operands = [emit(plan.object)];
|
|
29682
|
+
for (const arg of plan.args) {
|
|
29683
|
+
operands.push(arg.kind === "expr" ? emit(arg.expr) : defaultLiteral(arg.value, spelling));
|
|
29684
|
+
}
|
|
29685
|
+
return spelling.call(plan.helper, operands);
|
|
29686
|
+
}
|
|
29687
|
+
function defaultLiteral(value, spelling) {
|
|
29688
|
+
switch (value.kind) {
|
|
29689
|
+
case "number":
|
|
29690
|
+
return String(value.value);
|
|
29691
|
+
case "string":
|
|
29692
|
+
return `'${value.value}'`;
|
|
29693
|
+
case "undefined":
|
|
29694
|
+
return spelling.undefinedLiteral;
|
|
29695
|
+
}
|
|
29696
|
+
}
|
|
29697
|
+
function prefixedArrayMethodSpelling(prefix, undefinedLiteral) {
|
|
29698
|
+
return {
|
|
29699
|
+
call: (helper, operands) => `${prefix}${helper}(${operands.join(", ")})`,
|
|
29700
|
+
undefinedLiteral
|
|
29701
|
+
};
|
|
29702
|
+
}
|
|
29703
|
+
// src/adapters/callback-method.ts
|
|
29704
|
+
var PREDICATE_EVAL = {
|
|
29705
|
+
filter: { helper: "filter_eval" },
|
|
29706
|
+
every: { helper: "every_eval" },
|
|
29707
|
+
some: { helper: "some_eval" },
|
|
29708
|
+
find: { helper: "find_eval", forward: true },
|
|
29709
|
+
findLast: { helper: "find_eval", forward: false },
|
|
29710
|
+
findIndex: { helper: "find_index_eval", forward: true },
|
|
29711
|
+
findLastIndex: { helper: "find_index_eval", forward: false }
|
|
29712
|
+
};
|
|
29713
|
+
function isPredicateMethod(method) {
|
|
29714
|
+
return Object.hasOwn(PREDICATE_EVAL, method);
|
|
29715
|
+
}
|
|
29716
|
+
function lowerCallbackMethod(method, arrow, restArgs, options) {
|
|
29717
|
+
const { adapterName } = options;
|
|
29718
|
+
const { body, params } = arrow;
|
|
29719
|
+
if (isPredicateMethod(method))
|
|
29720
|
+
return lowerPredicate(method, arrow, options);
|
|
29721
|
+
if (method === "sort" || method === "toSorted") {
|
|
29722
|
+
const plan = sortEvalPlan(body, params);
|
|
29723
|
+
if (plan)
|
|
29724
|
+
return plan;
|
|
29725
|
+
const comparator = sortComparatorFromArrow(arrow);
|
|
29726
|
+
if (comparator)
|
|
29727
|
+
return { kind: "structured-sort", comparator };
|
|
29728
|
+
return sortRefusal(method, adapterName);
|
|
29729
|
+
}
|
|
29730
|
+
if (method === "reduce" || method === "reduceRight") {
|
|
29731
|
+
const init = restArgs[0];
|
|
29732
|
+
const plan = init ? reduceEvalPlan(body, params, init, method === "reduceRight" ? "right" : "left") : null;
|
|
29733
|
+
return plan ?? {
|
|
29734
|
+
kind: "refuse",
|
|
29735
|
+
message: `'.${method}(...)' is outside the ${adapterName} adapter's evaluable surface (needs a literal initial value and an evaluable reducer body)`,
|
|
29736
|
+
suggestion: `Pre-compute the reduced value, or move this position to a '/* @client */' boundary.`
|
|
29737
|
+
};
|
|
29738
|
+
}
|
|
29739
|
+
if (method === "flatMap" || method === "map") {
|
|
29740
|
+
const plan = projectionEvalPlan(method === "map" ? "map_eval" : "flat_map_eval", body, params);
|
|
29741
|
+
return plan ?? {
|
|
29742
|
+
kind: "refuse",
|
|
29743
|
+
message: `'.${method}(...)' projection is outside the ${adapterName} adapter's evaluable surface`,
|
|
29744
|
+
suggestion: `Pre-compute the projected array, or move this position to a '/* @client */' boundary.`
|
|
29745
|
+
};
|
|
29746
|
+
}
|
|
29747
|
+
return { kind: "receiver" };
|
|
29748
|
+
}
|
|
29749
|
+
function lowerPredicate(method, arrow, options) {
|
|
29750
|
+
const param = String(arrow.params[0]);
|
|
29751
|
+
const native = { kind: "native-predicate", method, param, predicate: arrow.body };
|
|
29752
|
+
const nativeOk = options.nativePredicate === true && !hasOpaqueCall(arrow.body) && !readsNestedCallbackIndex(arrow.body);
|
|
29753
|
+
const identity = method === "filter" && arrow.body.kind === "identifier" && arrow.body.name === param;
|
|
29754
|
+
if (nativeOk && identity)
|
|
29755
|
+
return native;
|
|
29756
|
+
const json = serializeParsedExpr(arrow.body);
|
|
29757
|
+
if (json !== null) {
|
|
29758
|
+
const { helper, forward } = PREDICATE_EVAL[method];
|
|
29759
|
+
const operands = [
|
|
29760
|
+
{ kind: "string", value: json },
|
|
29761
|
+
{ kind: "string", value: param }
|
|
29762
|
+
];
|
|
29763
|
+
if (forward !== undefined)
|
|
29764
|
+
operands.push({ kind: "boolean", value: forward });
|
|
29765
|
+
return { kind: "eval", helper, operands, captured: freeVarsInBody(arrow.body, new Set(arrow.params.slice(0, 1))) };
|
|
29766
|
+
}
|
|
29767
|
+
if (nativeOk)
|
|
29768
|
+
return native;
|
|
29769
|
+
return {
|
|
29770
|
+
kind: "refuse",
|
|
29771
|
+
message: `'.${method}(...)' predicate is outside the ${options.adapterName} adapter's evaluable surface`,
|
|
29772
|
+
suggestion: `Rewrite the predicate as a pure expression the evaluator can serialize (no nested method-call callbacks), or move this position to a '/* @client */' boundary.`
|
|
29773
|
+
};
|
|
29774
|
+
}
|
|
29775
|
+
function sortEvalPlan(body, params) {
|
|
29776
|
+
const json = serializeParsedExpr(body);
|
|
29777
|
+
if (json === null || params.length < 2)
|
|
29778
|
+
return null;
|
|
29779
|
+
return {
|
|
29780
|
+
kind: "eval",
|
|
29781
|
+
helper: "sort_eval",
|
|
29782
|
+
operands: [
|
|
29783
|
+
{ kind: "string", value: json },
|
|
29784
|
+
{ kind: "string", value: params[0] },
|
|
29785
|
+
{ kind: "string", value: params[1] }
|
|
29786
|
+
],
|
|
29787
|
+
captured: freeVarsInBody(body, new Set(params))
|
|
29788
|
+
};
|
|
29789
|
+
}
|
|
29790
|
+
function sortRefusal(method, adapterName) {
|
|
29791
|
+
return {
|
|
29792
|
+
kind: "refuse",
|
|
29793
|
+
message: `'.${method}(...)' comparator is outside the ${adapterName} adapter's evaluable / structured surface`,
|
|
29794
|
+
suggestion: `Pre-compute the sorted array, or move this position to a '/* @client */' boundary.`
|
|
29795
|
+
};
|
|
29796
|
+
}
|
|
29797
|
+
function reduceEvalPlan(body, params, init, direction) {
|
|
29798
|
+
const json = serializeParsedExpr(body);
|
|
29799
|
+
if (json === null || init.kind !== "literal" || params.length < 2)
|
|
29800
|
+
return null;
|
|
29801
|
+
let seed;
|
|
29802
|
+
if (init.literalType === "string")
|
|
29803
|
+
seed = { kind: "string", value: String(init.value) };
|
|
29804
|
+
else if (init.literalType === "number")
|
|
29805
|
+
seed = { kind: "number", text: String(init.value) };
|
|
29806
|
+
else
|
|
29807
|
+
return null;
|
|
29808
|
+
return {
|
|
29809
|
+
kind: "eval",
|
|
29810
|
+
helper: "reduce_eval",
|
|
29811
|
+
operands: [
|
|
29812
|
+
{ kind: "string", value: json },
|
|
29813
|
+
{ kind: "string", value: params[0] },
|
|
29814
|
+
{ kind: "string", value: params[1] },
|
|
29815
|
+
seed,
|
|
29816
|
+
{ kind: "string", value: direction }
|
|
29817
|
+
],
|
|
29818
|
+
captured: freeVarsInBody(body, new Set(params))
|
|
29819
|
+
};
|
|
29820
|
+
}
|
|
29821
|
+
function projectionEvalPlan(helper, body, params) {
|
|
29822
|
+
const json = serializeParsedExpr(body);
|
|
29823
|
+
if (json === null)
|
|
29824
|
+
return null;
|
|
29825
|
+
return {
|
|
29826
|
+
kind: "eval",
|
|
29827
|
+
helper,
|
|
29828
|
+
operands: [
|
|
29829
|
+
{ kind: "string", value: json },
|
|
29830
|
+
{ kind: "string", value: String(params[0]) }
|
|
29831
|
+
],
|
|
29832
|
+
captured: freeVarsInBody(body, new Set(params.slice(0, 1)))
|
|
29833
|
+
};
|
|
29834
|
+
}
|
|
29835
|
+
function emitCallbackEval(plan, recv, emit, spelling) {
|
|
29836
|
+
const operands = [recv];
|
|
29837
|
+
for (const operand of plan.operands) {
|
|
29838
|
+
if (operand.kind === "string")
|
|
29839
|
+
operands.push(spelling.string(operand.value));
|
|
29840
|
+
else if (operand.kind === "number")
|
|
29841
|
+
operands.push(operand.text);
|
|
29842
|
+
else
|
|
29843
|
+
operands.push(spelling.boolean(operand.value));
|
|
29844
|
+
}
|
|
29845
|
+
const env = plan.captured.map((name) => [name, emit({ kind: "identifier", name })]);
|
|
29846
|
+
operands.push(spelling.env(env));
|
|
29847
|
+
return spelling.call(plan.helper, operands);
|
|
29848
|
+
}
|
|
29849
|
+
function nestedCallbackRefusal(method, adapterName) {
|
|
29850
|
+
return {
|
|
29851
|
+
message: `Filter predicate contains a nested '.${method}(...)' callback, which has no ${adapterName} scalar form`,
|
|
29852
|
+
suggestion: `Rewrite the predicate without a nested callback method, or add /* @client */ for client-only evaluation (no SSR).`
|
|
29853
|
+
};
|
|
29854
|
+
}
|
|
29855
|
+
// src/adapters/component-props.ts
|
|
29856
|
+
import { resolveJsxChildrenProp } from "@barefootjs/shared";
|
|
29857
|
+
function componentPropKey(entry) {
|
|
29858
|
+
if (entry.kind === "slot-id")
|
|
29859
|
+
return "_bf_slot";
|
|
29860
|
+
if (entry.kind === "children")
|
|
29861
|
+
return "children";
|
|
29862
|
+
return entry.name;
|
|
29863
|
+
}
|
|
29864
|
+
function planComponentProps(comp, options) {
|
|
29865
|
+
const segments = [{ kind: "entries", entries: [] }];
|
|
29866
|
+
const push = (entry) => {
|
|
29867
|
+
const last = segments[segments.length - 1];
|
|
29868
|
+
if (last.kind !== "entries") {
|
|
29869
|
+
segments.push({ kind: "entries", entries: [entry] });
|
|
29870
|
+
return;
|
|
29871
|
+
}
|
|
29872
|
+
const at = last.entries.findIndex((e) => componentPropKey(e) === componentPropKey(entry));
|
|
29873
|
+
if (at === -1)
|
|
29874
|
+
last.entries.push(entry);
|
|
29875
|
+
else
|
|
29876
|
+
last.entries[at] = entry;
|
|
29877
|
+
};
|
|
29878
|
+
for (const p of comp.props) {
|
|
29879
|
+
if ((/^on[A-Z]/.test(p.name) || p.name === "ref") && p.value.kind === "expression")
|
|
29880
|
+
continue;
|
|
29881
|
+
if (p.value.kind === "jsx-children") {
|
|
29882
|
+
if (p.name !== "children")
|
|
29883
|
+
push({ kind: "named-slot", name: p.name, children: p.value.children });
|
|
29884
|
+
} else if (p.value.kind === "spread") {
|
|
29885
|
+
if (options.propsObjectName !== null && options.propsObjectName === p.value.expr.trim()) {
|
|
29886
|
+
for (const param of options.propsParams)
|
|
29887
|
+
push({ kind: "props-param", name: param.name });
|
|
29888
|
+
} else {
|
|
29889
|
+
segments.push({ kind: "spread", value: p.value });
|
|
29890
|
+
}
|
|
29891
|
+
} else {
|
|
29892
|
+
push({ kind: "value", name: p.name, value: p.value });
|
|
29893
|
+
}
|
|
29894
|
+
}
|
|
29895
|
+
if (derivesScopeFromSlot(comp))
|
|
29896
|
+
push({ kind: "slot-id", slotId: comp.slotId });
|
|
29897
|
+
const children = comp.children.length > 0 ? comp.children : resolveJsxChildrenProp(comp.props);
|
|
29898
|
+
if (children.length > 0)
|
|
29899
|
+
push({ kind: "children", children });
|
|
29900
|
+
return segments;
|
|
29901
|
+
}
|
|
29902
|
+
function spellComponentProps(segments, spelling) {
|
|
29903
|
+
return segments.map((segment) => {
|
|
29904
|
+
if (segment.kind === "spread")
|
|
29905
|
+
return { kind: "spread", expr: spelling.spread(segment.value) };
|
|
29906
|
+
const parts = [];
|
|
29907
|
+
for (const entry of segment.entries) {
|
|
29908
|
+
const part = spelling.entry(entry);
|
|
29909
|
+
if (part)
|
|
29910
|
+
parts.push(part);
|
|
29911
|
+
}
|
|
29912
|
+
return { kind: "entries", parts };
|
|
29913
|
+
});
|
|
29914
|
+
}
|
|
29915
|
+
function isEmptyComponentProps(segments) {
|
|
29916
|
+
return segments.every((segment) => segment.kind === "entries" && segment.parts.length === 0);
|
|
29917
|
+
}
|
|
29918
|
+
function childTemplateName(componentName, importAliases) {
|
|
29919
|
+
const declaredName = importAliases.get(componentName) ?? componentName;
|
|
29920
|
+
return declaredName.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
|
|
29921
|
+
}
|
|
29922
|
+
// src/nullable-signal-attr.ts
|
|
29923
|
+
function isNullishType(type) {
|
|
29924
|
+
return type.kind === "primitive" && (type.primitive === "undefined" || type.primitive === "null");
|
|
29925
|
+
}
|
|
29926
|
+
function admitsNullish(type) {
|
|
29927
|
+
if (isNullishType(type))
|
|
29928
|
+
return true;
|
|
29929
|
+
return type.kind === "union" && (type.unionTypes ?? []).some(isNullishType);
|
|
29930
|
+
}
|
|
29931
|
+
function isNullishLiteral(expr) {
|
|
29932
|
+
if (!expr)
|
|
29933
|
+
return false;
|
|
29934
|
+
if (expr.kind === "identifier")
|
|
29935
|
+
return expr.name === "undefined";
|
|
29936
|
+
return expr.kind === "literal" && expr.literalType === "null";
|
|
29937
|
+
}
|
|
29938
|
+
function isNullableSignal(signal) {
|
|
29939
|
+
if (admitsNullish(signal.type))
|
|
29940
|
+
return true;
|
|
29941
|
+
return signal.type.kind === "unknown" && isNullishLiteral(signal.parsed);
|
|
29942
|
+
}
|
|
29943
|
+
function collectNullableSignalGetters(ir) {
|
|
29944
|
+
return new Set((ir.metadata.signals ?? []).filter(isNullableSignal).map((s) => s.getter));
|
|
29945
|
+
}
|
|
29946
|
+
function bareGetterCallName(parsed) {
|
|
29947
|
+
if (!parsed || parsed.kind !== "call" || parsed.args.length !== 0)
|
|
29948
|
+
return null;
|
|
29949
|
+
return parsed.callee.kind === "identifier" ? parsed.callee.name : null;
|
|
29950
|
+
}
|
|
29951
|
+
function nullableSignalAttrGetter(parsed, nullable) {
|
|
29952
|
+
const getter = bareGetterCallName(parsed);
|
|
29953
|
+
return getter !== null && nullable.has(getter) ? getter : null;
|
|
29954
|
+
}
|
|
29955
|
+
function collectNullishAttrContext(ir) {
|
|
29956
|
+
const signals = ir.metadata.signals ?? [];
|
|
29957
|
+
return {
|
|
29958
|
+
nullableSignals: collectNullableSignalGetters(ir),
|
|
29959
|
+
nullSeededSignals: new Set(signals.filter((s) => s.parsed?.kind === "literal" && s.parsed.literalType === "null").map((s) => s.getter)),
|
|
29960
|
+
undefinedSeededSignals: new Set(signals.filter((s) => s.parsed?.kind === "identifier" && s.parsed.name === "undefined").map((s) => s.getter)),
|
|
29961
|
+
memos: new Map((ir.metadata.memos ?? []).map((m) => [m.name, m])),
|
|
29962
|
+
nullableNumberProps: collectNullableNumberProps(ir)
|
|
29963
|
+
};
|
|
29964
|
+
}
|
|
29965
|
+
function isNullableNumberType(type, optional2) {
|
|
29966
|
+
if (!type)
|
|
29967
|
+
return false;
|
|
29968
|
+
const members = type.kind === "union" ? type.unionTypes ?? [] : [type];
|
|
29969
|
+
const families = new Set(members.map((m) => m.kind === "primitive" ? m.primitive : m.kind));
|
|
29970
|
+
const nullable = optional2 || families.has("null") || families.has("undefined");
|
|
29971
|
+
families.delete("null");
|
|
29972
|
+
families.delete("undefined");
|
|
29973
|
+
return nullable && families.size === 1 && families.has("number");
|
|
29974
|
+
}
|
|
29975
|
+
function collectNullableNumberProps(ir) {
|
|
29976
|
+
const names = new Set;
|
|
29977
|
+
const objectName = ir.metadata.propsObjectName;
|
|
29978
|
+
for (const p of ir.metadata.propsParams) {
|
|
29979
|
+
if (p.isRest || p.defaultValue !== undefined || !isNullableNumberType(p.type, p.optional))
|
|
29980
|
+
continue;
|
|
29981
|
+
names.add(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name);
|
|
29982
|
+
}
|
|
29983
|
+
return names;
|
|
29984
|
+
}
|
|
29985
|
+
function attrValueMayBeNullish(parsed, ctx, isShadowed = () => false) {
|
|
29986
|
+
const visiting = new Set;
|
|
29987
|
+
const walk = (expr, shadowed) => {
|
|
29988
|
+
switch (expr.kind) {
|
|
29989
|
+
case "identifier":
|
|
29990
|
+
return expr.name === "undefined";
|
|
29991
|
+
case "literal":
|
|
29992
|
+
return expr.literalType === "null";
|
|
29993
|
+
case "call": {
|
|
29994
|
+
const getter = bareGetterCallName(expr);
|
|
29995
|
+
if (getter === null || shadowed(getter))
|
|
29996
|
+
return false;
|
|
29997
|
+
if (ctx.nullableSignals.has(getter))
|
|
29998
|
+
return true;
|
|
29999
|
+
const memo = ctx.memos.get(getter);
|
|
30000
|
+
if (!memo || visiting.has(getter))
|
|
30001
|
+
return false;
|
|
30002
|
+
if (admitsNullish(memo.type))
|
|
30003
|
+
return true;
|
|
30004
|
+
visiting.add(getter);
|
|
30005
|
+
const result = memo.parsed !== undefined && walk(memo.parsed, () => false);
|
|
30006
|
+
visiting.delete(getter);
|
|
30007
|
+
return result;
|
|
30008
|
+
}
|
|
30009
|
+
case "member":
|
|
30010
|
+
return expr.optional || walk(expr.object, shadowed);
|
|
30011
|
+
case "index-access":
|
|
30012
|
+
return walk(expr.object, shadowed);
|
|
30013
|
+
case "conditional":
|
|
30014
|
+
return walk(expr.consequent, shadowed) || walk(expr.alternate, shadowed);
|
|
30015
|
+
case "logical":
|
|
30016
|
+
return expr.op === "&&" ? walk(expr.left, shadowed) || walk(expr.right, shadowed) : walk(expr.right, shadowed);
|
|
30017
|
+
default:
|
|
30018
|
+
return false;
|
|
30019
|
+
}
|
|
30020
|
+
};
|
|
30021
|
+
return parsed !== undefined && walk(parsed, isShadowed);
|
|
30022
|
+
}
|
|
30023
|
+
function arithmeticOperandIsNull(parsed, ctx, isShadowed = () => false) {
|
|
30024
|
+
return arithmeticOperandSeed(parsed, ctx, isShadowed) === "null";
|
|
30025
|
+
}
|
|
30026
|
+
function arithmeticOperandIsUndefined(parsed, ctx, isShadowed = () => false) {
|
|
30027
|
+
return arithmeticOperandSeed(parsed, ctx, isShadowed) === "undefined";
|
|
30028
|
+
}
|
|
30029
|
+
function arithmeticOperandSeed(parsed, ctx, isShadowed) {
|
|
30030
|
+
const visiting = new Set;
|
|
30031
|
+
const walk = (expr, shadowed) => {
|
|
30032
|
+
if (expr.kind === "literal")
|
|
30033
|
+
return expr.literalType === "null" ? "null" : null;
|
|
30034
|
+
if (expr.kind === "identifier" && expr.name === "undefined" && !shadowed("undefined"))
|
|
30035
|
+
return "undefined";
|
|
30036
|
+
const prop = nullableNumberPropRead(expr, ctx, shadowed);
|
|
30037
|
+
if (prop)
|
|
30038
|
+
return "null";
|
|
30039
|
+
const getter = expr.kind === "identifier" ? expr.name : bareGetterCallName(expr);
|
|
30040
|
+
if (getter === null || shadowed(getter))
|
|
30041
|
+
return null;
|
|
30042
|
+
if (ctx.nullSeededSignals?.has(getter))
|
|
30043
|
+
return "null";
|
|
30044
|
+
if (ctx.undefinedSeededSignals?.has(getter))
|
|
30045
|
+
return "undefined";
|
|
30046
|
+
const memo = ctx.memos.get(getter);
|
|
30047
|
+
if (!memo?.parsed || visiting.has(getter))
|
|
30048
|
+
return null;
|
|
30049
|
+
visiting.add(getter);
|
|
30050
|
+
const result = walk(memo.parsed, () => false);
|
|
30051
|
+
visiting.delete(getter);
|
|
30052
|
+
return result;
|
|
30053
|
+
};
|
|
30054
|
+
return parsed === undefined ? null : walk(parsed, isShadowed);
|
|
30055
|
+
}
|
|
30056
|
+
function nullableNumberPropRead(expr, ctx, shadowed) {
|
|
30057
|
+
if (!ctx.nullableNumberProps?.size)
|
|
30058
|
+
return false;
|
|
30059
|
+
if (expr.kind === "identifier")
|
|
30060
|
+
return !shadowed(expr.name) && ctx.nullableNumberProps.has(expr.name);
|
|
30061
|
+
if (expr.kind === "member" && !expr.computed && expr.object.kind === "identifier" && !shadowed(expr.object.name)) {
|
|
30062
|
+
return ctx.nullableNumberProps.has(`${expr.object.name}.${expr.property}`);
|
|
30063
|
+
}
|
|
30064
|
+
return false;
|
|
30065
|
+
}
|
|
30066
|
+
var NULL_COERCING_ARITHMETIC_OPS = new Set(["-", "*", "/", "%", "**"]);
|
|
30067
|
+
function arithmeticOperandCoercion(op, operand, other, ctx, isShadowed = () => false) {
|
|
30068
|
+
const numeric = NULL_COERCING_ARITHMETIC_OPS.has(op) || op === "+" && isNumberLiteral(other);
|
|
30069
|
+
return numeric ? arithmeticOperandSeed(operand, ctx, isShadowed) : null;
|
|
30070
|
+
}
|
|
30071
|
+
function isNumberLiteral(expr) {
|
|
30072
|
+
if (expr?.kind === "unary" && expr.op === "-")
|
|
30073
|
+
return isNumberLiteral(expr.argument);
|
|
30074
|
+
return expr?.kind === "literal" && expr.literalType === "number";
|
|
30075
|
+
}
|
|
30076
|
+
|
|
30077
|
+
// src/adapters/attr-plan.ts
|
|
30078
|
+
var IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
30079
|
+
function unsupportedAttrExpression(expr, loweringMatchers) {
|
|
30080
|
+
let probe = expr.trim();
|
|
30081
|
+
while (probe.startsWith("("))
|
|
30082
|
+
probe = probe.slice(1).trimStart();
|
|
30083
|
+
const startsAsObjectLiteral = probe.startsWith("{");
|
|
30084
|
+
const hasTaggedTemplate = /[A-Za-z_$][\w$]*\s*`/.test(probe);
|
|
30085
|
+
if (!startsAsObjectLiteral && !hasTaggedTemplate)
|
|
30086
|
+
return null;
|
|
30087
|
+
const parsed = parseExpression(expr.trim());
|
|
30088
|
+
const support = isSupported(parsed, { loweringMatchers });
|
|
30089
|
+
if (parsed.kind !== "unsupported" && support.supported)
|
|
30090
|
+
return null;
|
|
30091
|
+
return { reason: support.reason ?? (parsed.kind === "unsupported" ? parsed.reason : undefined) };
|
|
30092
|
+
}
|
|
30093
|
+
function planAttrExpression(value, name, ctx) {
|
|
30094
|
+
const booleanAttr = isBooleanAttr(name);
|
|
30095
|
+
if (!booleanAttr && !value.presenceOrUndefined) {
|
|
30096
|
+
const nullable = nullableBareRead(value, ctx);
|
|
30097
|
+
if (nullable !== null)
|
|
30098
|
+
return { kind: "omit-nullable", boolStr: ctx.boolStr(value.expr, name), restKey: nullable.restKey };
|
|
30099
|
+
}
|
|
30100
|
+
if (booleanAttr)
|
|
30101
|
+
return { kind: "boolean-attr" };
|
|
30102
|
+
if (value.presenceOrUndefined)
|
|
30103
|
+
return { kind: "presence", boolStr: ctx.boolStr(value.expr, name) };
|
|
30104
|
+
const ternary = parseUndefinedAlternateTernary(value.expr);
|
|
30105
|
+
if (ternary)
|
|
30106
|
+
return { kind: "undefined-alternate", ternary };
|
|
30107
|
+
const boolStr = ctx.boolStr(value.expr, name);
|
|
30108
|
+
if (attrValueMayBeNullish(value.parsed, ctx.nullishAttrCtx, (n) => ctx.isLoopBound(n)))
|
|
30109
|
+
return { kind: "omit-nil", boolStr };
|
|
30110
|
+
return { kind: "plain", boolStr };
|
|
30111
|
+
}
|
|
30112
|
+
function nullableBareRead(value, ctx) {
|
|
30113
|
+
const bare = value.expr.trim();
|
|
30114
|
+
const restKey = ctx.restPropsName !== null && bare.startsWith(`${ctx.restPropsName}.`);
|
|
30115
|
+
let normalized = bare;
|
|
30116
|
+
if (ctx.propsObjectName && bare.startsWith(`${ctx.propsObjectName}.`))
|
|
30117
|
+
normalized = bare.slice(ctx.propsObjectName.length + 1);
|
|
30118
|
+
else if (restKey)
|
|
30119
|
+
normalized = bare.slice(ctx.restPropsName.length + 1);
|
|
30120
|
+
const prop = IDENTIFIER.test(normalized) && ctx.nullableOptionalProps.has(normalized) && !ctx.isLoopBound(normalized);
|
|
30121
|
+
const signal = nullableSignalAttrGetter(value.parsed, ctx.nullableSignalGetters);
|
|
30122
|
+
if (prop || signal !== null && !ctx.isLoopBound(signal))
|
|
30123
|
+
return { restKey };
|
|
30124
|
+
return null;
|
|
30125
|
+
}
|
|
30126
|
+
function parseUndefinedAlternateTernary(expr) {
|
|
30127
|
+
const parsed = parseExpression(expr.trim());
|
|
30128
|
+
if (parsed?.kind !== "conditional")
|
|
30129
|
+
return null;
|
|
30130
|
+
const alt = parsed.alternate;
|
|
30131
|
+
const isUndef = alt.kind === "identifier" && (alt.name === "undefined" || alt.name === "null") || alt.kind === "literal" && (alt.value === null || alt.value === undefined);
|
|
30132
|
+
if (!isUndef)
|
|
30133
|
+
return null;
|
|
30134
|
+
return {
|
|
30135
|
+
condition: exprToString(parsed.test),
|
|
30136
|
+
consequent: exprToString(parsed.consequent),
|
|
30137
|
+
testParsed: parsed.test,
|
|
30138
|
+
consequentParsed: parsed.consequent
|
|
30139
|
+
};
|
|
30140
|
+
}
|
|
30141
|
+
function planSpreadAttr(value, ctx) {
|
|
30142
|
+
const trimmed = value.expr.trim();
|
|
30143
|
+
if (ctx.propsObjectName && ctx.propsObjectName === trimmed) {
|
|
30144
|
+
return { kind: "props-object", params: ctx.propsParams.map((p) => p.name) };
|
|
30145
|
+
}
|
|
30146
|
+
const candidates = [value.parsed];
|
|
30147
|
+
const init = localConstInitializer(trimmed, ctx);
|
|
30148
|
+
if (init !== null)
|
|
30149
|
+
candidates.push(parseExpression(init));
|
|
30150
|
+
return { kind: "value", candidates };
|
|
30151
|
+
}
|
|
30152
|
+
function localConstInitializer(name, ctx) {
|
|
30153
|
+
if (!IDENTIFIER.test(name) || ctx.isBound(name))
|
|
30154
|
+
return null;
|
|
30155
|
+
const init = ctx.localConstants.find((c) => c.name === name && !c.isModule)?.value?.trim();
|
|
30156
|
+
return init !== undefined && !IDENTIFIER.test(init) ? init : null;
|
|
30157
|
+
}
|
|
30158
|
+
// src/loop-destructure.ts
|
|
30159
|
+
function isLowerableLoopDestructure(loop) {
|
|
30160
|
+
const bindings = loop.paramBindings;
|
|
30161
|
+
if (!bindings || bindings.length === 0)
|
|
30162
|
+
return false;
|
|
30163
|
+
if (loop.filterPredicate)
|
|
30164
|
+
return false;
|
|
30165
|
+
for (const name of [...bindings.map((b) => b.name), loop.index]) {
|
|
30166
|
+
if (name && name.startsWith("__bf_"))
|
|
30167
|
+
return false;
|
|
30168
|
+
}
|
|
30169
|
+
for (const b of bindings) {
|
|
30170
|
+
if (b.rest) {
|
|
30171
|
+
if (!b.segments)
|
|
30172
|
+
return false;
|
|
30173
|
+
} else if (!b.segments || b.segments.length === 0) {
|
|
30174
|
+
return false;
|
|
30175
|
+
}
|
|
30176
|
+
}
|
|
30177
|
+
const objectRestNames = bindings.filter((b) => b.rest?.kind === "object").map((b) => b.name);
|
|
30178
|
+
if (objectRestNames.length === 0)
|
|
30179
|
+
return true;
|
|
30180
|
+
return !restNamesMisused(loop, objectRestNames);
|
|
30181
|
+
}
|
|
30182
|
+
var isLowerableObjectRestDestructure = isLowerableLoopDestructure;
|
|
30183
|
+
function restNamesMisused(loop, names) {
|
|
30184
|
+
const nameSet = new Set(names);
|
|
30185
|
+
const valueUse = names.map((n) => new RegExp(`(?<![\\w.$])${escapeRe(n)}(?!\\s*\\??\\.)(?![\\w$])`));
|
|
30186
|
+
let misused = false;
|
|
30187
|
+
const check = (s) => {
|
|
30188
|
+
if (!s || misused)
|
|
30189
|
+
return;
|
|
30190
|
+
for (const re of valueUse) {
|
|
30191
|
+
if (re.test(s)) {
|
|
30192
|
+
misused = true;
|
|
30193
|
+
return;
|
|
30194
|
+
}
|
|
30195
|
+
}
|
|
30196
|
+
};
|
|
30197
|
+
const attr = (v, isIntrinsicElementAttrs) => {
|
|
30198
|
+
if (v.kind === "spread" && isIntrinsicElementAttrs && nameSet.has(v.expr.trim())) {
|
|
30199
|
+
return;
|
|
30200
|
+
}
|
|
30201
|
+
if (v.kind === "expression" || v.kind === "spread") {
|
|
30202
|
+
check(v.expr);
|
|
30203
|
+
check(v.templateExpr);
|
|
30204
|
+
} else if (v.kind === "template") {
|
|
30205
|
+
v.parts.forEach(part);
|
|
30206
|
+
}
|
|
30207
|
+
};
|
|
30208
|
+
const part = (p) => {
|
|
30209
|
+
if (p.type === "ternary") {
|
|
30210
|
+
check(p.condition);
|
|
30211
|
+
check(p.templateCondition);
|
|
30212
|
+
check(p.whenTrue);
|
|
30213
|
+
check(p.whenFalse);
|
|
30214
|
+
} else if (p.type === "lookup") {
|
|
30215
|
+
check(p.key);
|
|
30216
|
+
check(p.templateKey);
|
|
30217
|
+
}
|
|
30218
|
+
};
|
|
30219
|
+
const visitLoop = (l) => {
|
|
30220
|
+
if (misused)
|
|
30221
|
+
return;
|
|
30222
|
+
check(l.array);
|
|
30223
|
+
check(l.templateArray);
|
|
30224
|
+
check(l.key);
|
|
30225
|
+
if (l.preamble) {
|
|
30226
|
+
check(preambleAnalysisText(l.preamble));
|
|
30227
|
+
check(preambleAnalysisTemplateText(l.preamble));
|
|
30228
|
+
}
|
|
30229
|
+
if (l.flatMapCallback) {
|
|
30230
|
+
check(preambleAnalysisText(l.flatMapCallback));
|
|
30231
|
+
for (const seg of l.flatMapCallback.segments) {
|
|
30232
|
+
if (seg.kind === "jsx")
|
|
30233
|
+
visit3(seg.ir);
|
|
30234
|
+
}
|
|
30235
|
+
}
|
|
30236
|
+
l.children.forEach(visit3);
|
|
30237
|
+
};
|
|
30238
|
+
const visit3 = (node) => {
|
|
30239
|
+
if (misused)
|
|
30240
|
+
return;
|
|
30241
|
+
switch (node.type) {
|
|
30242
|
+
case "expression":
|
|
30243
|
+
check(node.expr);
|
|
30244
|
+
check(node.templateExpr);
|
|
30245
|
+
break;
|
|
30246
|
+
case "conditional":
|
|
30247
|
+
check(node.condition);
|
|
30248
|
+
check(node.templateCondition);
|
|
30249
|
+
visit3(node.whenTrue);
|
|
30250
|
+
visit3(node.whenFalse);
|
|
30251
|
+
break;
|
|
30252
|
+
case "if-statement":
|
|
30253
|
+
check(node.condition);
|
|
30254
|
+
check(node.templateCondition);
|
|
30255
|
+
for (const sv of node.scopeVariables) {
|
|
30256
|
+
check(sv.initializer);
|
|
30257
|
+
check(sv.templateInitializer);
|
|
30258
|
+
}
|
|
30259
|
+
visit3(node.consequent);
|
|
30260
|
+
if (node.alternate)
|
|
30261
|
+
visit3(node.alternate);
|
|
30262
|
+
break;
|
|
30263
|
+
case "element":
|
|
30264
|
+
node.attrs.forEach((a) => attr(a.value, true));
|
|
30265
|
+
node.events.forEach((e) => check(e.handler));
|
|
30266
|
+
node.children.forEach(visit3);
|
|
30267
|
+
break;
|
|
30268
|
+
case "component":
|
|
30269
|
+
node.props.forEach((p) => attr(p.value, false));
|
|
30270
|
+
node.children.forEach(visit3);
|
|
30271
|
+
break;
|
|
30272
|
+
case "provider":
|
|
30273
|
+
attr(node.valueProp.value, false);
|
|
30274
|
+
node.children.forEach(visit3);
|
|
30275
|
+
break;
|
|
30276
|
+
case "fragment":
|
|
30277
|
+
node.children.forEach(visit3);
|
|
30278
|
+
break;
|
|
30279
|
+
case "async":
|
|
30280
|
+
visit3(node.fallback);
|
|
30281
|
+
node.children.forEach(visit3);
|
|
30282
|
+
break;
|
|
30283
|
+
case "loop":
|
|
30284
|
+
visitLoop(node);
|
|
30285
|
+
break;
|
|
30286
|
+
case "text":
|
|
30287
|
+
case "slot":
|
|
30288
|
+
break;
|
|
30289
|
+
}
|
|
30290
|
+
};
|
|
30291
|
+
visitLoop(loop);
|
|
30292
|
+
return misused;
|
|
30293
|
+
}
|
|
30294
|
+
function escapeRe(s) {
|
|
30295
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
30296
|
+
}
|
|
30297
|
+
|
|
30298
|
+
// src/static-literal.ts
|
|
30299
|
+
function evaluateStaticLiteral(expr, bindings) {
|
|
30300
|
+
switch (expr.kind) {
|
|
30301
|
+
case "literal":
|
|
30302
|
+
return { value: expr.value };
|
|
30303
|
+
case "template-literal": {
|
|
30304
|
+
let out = "";
|
|
30305
|
+
for (const part of expr.parts) {
|
|
30306
|
+
if (part.type === "string") {
|
|
30307
|
+
out += part.value;
|
|
30308
|
+
continue;
|
|
30309
|
+
}
|
|
30310
|
+
const resolved = evaluateStaticLiteral(part.expr, bindings);
|
|
30311
|
+
if (!resolved)
|
|
30312
|
+
return null;
|
|
30313
|
+
out += String(resolved.value);
|
|
30314
|
+
}
|
|
30315
|
+
return { value: out };
|
|
30316
|
+
}
|
|
30317
|
+
case "array-literal": {
|
|
30318
|
+
const values = [];
|
|
30319
|
+
for (const element of expr.elements) {
|
|
30320
|
+
const resolved = evaluateStaticLiteral(element, bindings);
|
|
30321
|
+
if (!resolved)
|
|
30322
|
+
return null;
|
|
30323
|
+
values.push(resolved.value);
|
|
30324
|
+
}
|
|
30325
|
+
return { value: values };
|
|
30326
|
+
}
|
|
30327
|
+
case "object-literal": {
|
|
30328
|
+
const out = {};
|
|
30329
|
+
for (const prop of expr.properties) {
|
|
30330
|
+
if (prop.kind === "spread") {
|
|
30331
|
+
const resolved2 = evaluateStaticLiteral(prop.expr, bindings);
|
|
30332
|
+
if (!resolved2)
|
|
30333
|
+
return null;
|
|
30334
|
+
if (resolved2.value === null || resolved2.value === undefined)
|
|
30335
|
+
continue;
|
|
30336
|
+
if (typeof resolved2.value !== "object" || Array.isArray(resolved2.value))
|
|
29093
30337
|
return null;
|
|
29094
30338
|
Object.assign(out, resolved2.value);
|
|
29095
30339
|
continue;
|
|
@@ -29152,106 +30396,36 @@ function resolveStaticLoopSource(arrayParsed, localConstants, opts) {
|
|
|
29152
30396
|
return null;
|
|
29153
30397
|
return resolved.value;
|
|
29154
30398
|
}
|
|
29155
|
-
|
|
29156
|
-
|
|
29157
|
-
|
|
29158
|
-
|
|
29159
|
-
|
|
29160
|
-
|
|
29161
|
-
|
|
29162
|
-
return type.kind === "union" && (type.unionTypes ?? []).some(isNullishType);
|
|
29163
|
-
}
|
|
29164
|
-
function isNullishLiteral(expr) {
|
|
29165
|
-
if (!expr)
|
|
29166
|
-
return false;
|
|
29167
|
-
if (expr.kind === "identifier")
|
|
29168
|
-
return expr.name === "undefined";
|
|
29169
|
-
return expr.kind === "literal" && expr.literalType === "null";
|
|
29170
|
-
}
|
|
29171
|
-
function isNullableSignal(signal) {
|
|
29172
|
-
if (admitsNullish(signal.type))
|
|
29173
|
-
return true;
|
|
29174
|
-
return signal.type.kind === "unknown" && isNullishLiteral(signal.parsed);
|
|
29175
|
-
}
|
|
29176
|
-
function collectNullableSignalGetters(ir) {
|
|
29177
|
-
return new Set((ir.metadata.signals ?? []).filter(isNullableSignal).map((s) => s.getter));
|
|
29178
|
-
}
|
|
29179
|
-
function bareGetterCallName(parsed) {
|
|
29180
|
-
if (!parsed || parsed.kind !== "call" || parsed.args.length !== 0)
|
|
29181
|
-
return null;
|
|
29182
|
-
return parsed.callee.kind === "identifier" ? parsed.callee.name : null;
|
|
29183
|
-
}
|
|
29184
|
-
function nullableSignalAttrGetter(parsed, nullable) {
|
|
29185
|
-
const getter = bareGetterCallName(parsed);
|
|
29186
|
-
return getter !== null && nullable.has(getter) ? getter : null;
|
|
29187
|
-
}
|
|
29188
|
-
function collectNullishAttrContext(ir) {
|
|
29189
|
-
const signals = ir.metadata.signals ?? [];
|
|
30399
|
+
|
|
30400
|
+
// src/adapters/loop-plan.ts
|
|
30401
|
+
function planLoop(loop, ctx) {
|
|
30402
|
+
const staticItems = resolveStaticLoopSource(loop.arrayParsed, ctx.localConstants, {
|
|
30403
|
+
isNameShadowed: ctx.scope.asShadowPredicate()
|
|
30404
|
+
});
|
|
30405
|
+
const staticSource = staticItems !== null ? ctx.spellStatic(staticItems) : null;
|
|
29190
30406
|
return {
|
|
29191
|
-
|
|
29192
|
-
|
|
29193
|
-
|
|
30407
|
+
destructure: destructureKind(loop),
|
|
30408
|
+
staticSource,
|
|
30409
|
+
unresolvableConst: staticSource === null ? unresolvableConst(loop, ctx) : null,
|
|
30410
|
+
sort: loop.sortComparator ? lowerCallbackMethod("sort", loop.sortComparator.arrow, [], ctx.callbackLowering) : null,
|
|
30411
|
+
captureAliases: filterCaptureCollisions(loop).map((name) => ({ name, alias: `__bf_cap_${loop.markerId}_${name}` }))
|
|
29194
30412
|
};
|
|
29195
30413
|
}
|
|
29196
|
-
function
|
|
29197
|
-
|
|
29198
|
-
|
|
29199
|
-
|
|
29200
|
-
case "identifier":
|
|
29201
|
-
return expr.name === "undefined";
|
|
29202
|
-
case "literal":
|
|
29203
|
-
return expr.literalType === "null";
|
|
29204
|
-
case "call": {
|
|
29205
|
-
const getter = bareGetterCallName(expr);
|
|
29206
|
-
if (getter === null || shadowed(getter))
|
|
29207
|
-
return false;
|
|
29208
|
-
if (ctx.nullableSignals.has(getter))
|
|
29209
|
-
return true;
|
|
29210
|
-
const memo = ctx.memos.get(getter);
|
|
29211
|
-
if (!memo || visiting.has(getter))
|
|
29212
|
-
return false;
|
|
29213
|
-
if (admitsNullish(memo.type))
|
|
29214
|
-
return true;
|
|
29215
|
-
visiting.add(getter);
|
|
29216
|
-
const result = memo.parsed !== undefined && walk(memo.parsed, () => false);
|
|
29217
|
-
visiting.delete(getter);
|
|
29218
|
-
return result;
|
|
29219
|
-
}
|
|
29220
|
-
case "member":
|
|
29221
|
-
return expr.optional || walk(expr.object, shadowed);
|
|
29222
|
-
case "index-access":
|
|
29223
|
-
return walk(expr.object, shadowed);
|
|
29224
|
-
case "conditional":
|
|
29225
|
-
return walk(expr.consequent, shadowed) || walk(expr.alternate, shadowed);
|
|
29226
|
-
case "logical":
|
|
29227
|
-
return expr.op === "&&" ? walk(expr.left, shadowed) || walk(expr.right, shadowed) : walk(expr.right, shadowed);
|
|
29228
|
-
default:
|
|
29229
|
-
return false;
|
|
29230
|
-
}
|
|
29231
|
-
};
|
|
29232
|
-
return parsed !== undefined && walk(parsed, isShadowed);
|
|
30414
|
+
function destructureKind(loop) {
|
|
30415
|
+
if (!loop.paramBindings || loop.paramBindings.length === 0)
|
|
30416
|
+
return "none";
|
|
30417
|
+
return isLowerableLoopDestructure(loop) ? "lowerable" : "unlowerable";
|
|
29233
30418
|
}
|
|
29234
|
-
function
|
|
29235
|
-
const
|
|
29236
|
-
|
|
29237
|
-
|
|
29238
|
-
|
|
29239
|
-
|
|
29240
|
-
|
|
29241
|
-
|
|
29242
|
-
|
|
29243
|
-
return true;
|
|
29244
|
-
const memo = ctx.memos.get(getter);
|
|
29245
|
-
if (!memo?.parsed || visiting.has(getter))
|
|
29246
|
-
return false;
|
|
29247
|
-
visiting.add(getter);
|
|
29248
|
-
const result = walk(memo.parsed, () => false);
|
|
29249
|
-
visiting.delete(getter);
|
|
29250
|
-
return result;
|
|
29251
|
-
};
|
|
29252
|
-
return parsed !== undefined && walk(parsed, isShadowed);
|
|
30419
|
+
function unresolvableConst(loop, ctx) {
|
|
30420
|
+
const source = loop.arrayParsed ?? parseExpression(loop.array.trim());
|
|
30421
|
+
if (source?.kind !== "identifier")
|
|
30422
|
+
return null;
|
|
30423
|
+
const name = source.name;
|
|
30424
|
+
if (ctx.scope.isBound(name))
|
|
30425
|
+
return null;
|
|
30426
|
+
const constant = ctx.localConstants.find((c) => c.name === name);
|
|
30427
|
+
return constant && ctx.resolveLiteralConst(name) === null ? constant : null;
|
|
29253
30428
|
}
|
|
29254
|
-
var NULL_COERCING_ARITHMETIC_OPS = new Set(["-", "*", "/", "%", "**"]);
|
|
29255
30429
|
// src/query-href-lowering.ts
|
|
29256
30430
|
function matchQueryHrefCall(callee, args, localNames) {
|
|
29257
30431
|
if (callee.kind !== "identifier" || !localNames.has(callee.name))
|
|
@@ -29581,145 +30755,6 @@ function literalMemberIndex(property) {
|
|
|
29581
30755
|
}
|
|
29582
30756
|
return { kind: "literal", value: property, literalType: "string" };
|
|
29583
30757
|
}
|
|
29584
|
-
// src/loop-destructure.ts
|
|
29585
|
-
function isLowerableLoopDestructure(loop) {
|
|
29586
|
-
const bindings = loop.paramBindings;
|
|
29587
|
-
if (!bindings || bindings.length === 0)
|
|
29588
|
-
return false;
|
|
29589
|
-
if (loop.filterPredicate)
|
|
29590
|
-
return false;
|
|
29591
|
-
for (const name of [...bindings.map((b) => b.name), loop.index]) {
|
|
29592
|
-
if (name && name.startsWith("__bf_"))
|
|
29593
|
-
return false;
|
|
29594
|
-
}
|
|
29595
|
-
for (const b of bindings) {
|
|
29596
|
-
if (b.rest) {
|
|
29597
|
-
if (!b.segments)
|
|
29598
|
-
return false;
|
|
29599
|
-
} else if (!b.segments || b.segments.length === 0) {
|
|
29600
|
-
return false;
|
|
29601
|
-
}
|
|
29602
|
-
}
|
|
29603
|
-
const objectRestNames = bindings.filter((b) => b.rest?.kind === "object").map((b) => b.name);
|
|
29604
|
-
if (objectRestNames.length === 0)
|
|
29605
|
-
return true;
|
|
29606
|
-
return !restNamesMisused(loop, objectRestNames);
|
|
29607
|
-
}
|
|
29608
|
-
var isLowerableObjectRestDestructure = isLowerableLoopDestructure;
|
|
29609
|
-
function restNamesMisused(loop, names) {
|
|
29610
|
-
const nameSet = new Set(names);
|
|
29611
|
-
const valueUse = names.map((n) => new RegExp(`(?<![\\w.$])${escapeRe(n)}(?!\\s*\\??\\.)(?![\\w$])`));
|
|
29612
|
-
let misused = false;
|
|
29613
|
-
const check = (s) => {
|
|
29614
|
-
if (!s || misused)
|
|
29615
|
-
return;
|
|
29616
|
-
for (const re of valueUse) {
|
|
29617
|
-
if (re.test(s)) {
|
|
29618
|
-
misused = true;
|
|
29619
|
-
return;
|
|
29620
|
-
}
|
|
29621
|
-
}
|
|
29622
|
-
};
|
|
29623
|
-
const attr = (v, isIntrinsicElementAttrs) => {
|
|
29624
|
-
if (v.kind === "spread" && isIntrinsicElementAttrs && nameSet.has(v.expr.trim())) {
|
|
29625
|
-
return;
|
|
29626
|
-
}
|
|
29627
|
-
if (v.kind === "expression" || v.kind === "spread") {
|
|
29628
|
-
check(v.expr);
|
|
29629
|
-
check(v.templateExpr);
|
|
29630
|
-
} else if (v.kind === "template") {
|
|
29631
|
-
v.parts.forEach(part);
|
|
29632
|
-
}
|
|
29633
|
-
};
|
|
29634
|
-
const part = (p) => {
|
|
29635
|
-
if (p.type === "ternary") {
|
|
29636
|
-
check(p.condition);
|
|
29637
|
-
check(p.templateCondition);
|
|
29638
|
-
check(p.whenTrue);
|
|
29639
|
-
check(p.whenFalse);
|
|
29640
|
-
} else if (p.type === "lookup") {
|
|
29641
|
-
check(p.key);
|
|
29642
|
-
check(p.templateKey);
|
|
29643
|
-
}
|
|
29644
|
-
};
|
|
29645
|
-
const visitLoop = (l) => {
|
|
29646
|
-
if (misused)
|
|
29647
|
-
return;
|
|
29648
|
-
check(l.array);
|
|
29649
|
-
check(l.templateArray);
|
|
29650
|
-
check(l.key);
|
|
29651
|
-
if (l.preamble) {
|
|
29652
|
-
check(preambleAnalysisText(l.preamble));
|
|
29653
|
-
check(preambleAnalysisTemplateText(l.preamble));
|
|
29654
|
-
}
|
|
29655
|
-
if (l.flatMapCallback) {
|
|
29656
|
-
check(preambleAnalysisText(l.flatMapCallback));
|
|
29657
|
-
for (const seg of l.flatMapCallback.segments) {
|
|
29658
|
-
if (seg.kind === "jsx")
|
|
29659
|
-
visit3(seg.ir);
|
|
29660
|
-
}
|
|
29661
|
-
}
|
|
29662
|
-
l.children.forEach(visit3);
|
|
29663
|
-
};
|
|
29664
|
-
const visit3 = (node) => {
|
|
29665
|
-
if (misused)
|
|
29666
|
-
return;
|
|
29667
|
-
switch (node.type) {
|
|
29668
|
-
case "expression":
|
|
29669
|
-
check(node.expr);
|
|
29670
|
-
check(node.templateExpr);
|
|
29671
|
-
break;
|
|
29672
|
-
case "conditional":
|
|
29673
|
-
check(node.condition);
|
|
29674
|
-
check(node.templateCondition);
|
|
29675
|
-
visit3(node.whenTrue);
|
|
29676
|
-
visit3(node.whenFalse);
|
|
29677
|
-
break;
|
|
29678
|
-
case "if-statement":
|
|
29679
|
-
check(node.condition);
|
|
29680
|
-
check(node.templateCondition);
|
|
29681
|
-
for (const sv of node.scopeVariables) {
|
|
29682
|
-
check(sv.initializer);
|
|
29683
|
-
check(sv.templateInitializer);
|
|
29684
|
-
}
|
|
29685
|
-
visit3(node.consequent);
|
|
29686
|
-
if (node.alternate)
|
|
29687
|
-
visit3(node.alternate);
|
|
29688
|
-
break;
|
|
29689
|
-
case "element":
|
|
29690
|
-
node.attrs.forEach((a) => attr(a.value, true));
|
|
29691
|
-
node.events.forEach((e) => check(e.handler));
|
|
29692
|
-
node.children.forEach(visit3);
|
|
29693
|
-
break;
|
|
29694
|
-
case "component":
|
|
29695
|
-
node.props.forEach((p) => attr(p.value, false));
|
|
29696
|
-
node.children.forEach(visit3);
|
|
29697
|
-
break;
|
|
29698
|
-
case "provider":
|
|
29699
|
-
attr(node.valueProp.value, false);
|
|
29700
|
-
node.children.forEach(visit3);
|
|
29701
|
-
break;
|
|
29702
|
-
case "fragment":
|
|
29703
|
-
node.children.forEach(visit3);
|
|
29704
|
-
break;
|
|
29705
|
-
case "async":
|
|
29706
|
-
visit3(node.fallback);
|
|
29707
|
-
node.children.forEach(visit3);
|
|
29708
|
-
break;
|
|
29709
|
-
case "loop":
|
|
29710
|
-
visitLoop(node);
|
|
29711
|
-
break;
|
|
29712
|
-
case "text":
|
|
29713
|
-
case "slot":
|
|
29714
|
-
break;
|
|
29715
|
-
}
|
|
29716
|
-
};
|
|
29717
|
-
visitLoop(loop);
|
|
29718
|
-
return misused;
|
|
29719
|
-
}
|
|
29720
|
-
function escapeRe(s) {
|
|
29721
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
29722
|
-
}
|
|
29723
30758
|
// src/scope/root-prop-alias.ts
|
|
29724
30759
|
function rootPropAliasNames(ir, propNames) {
|
|
29725
30760
|
const { metadata } = ir;
|
|
@@ -32598,12 +33633,19 @@ function profileToJSON(profile) {
|
|
|
32598
33633
|
// src/index.ts
|
|
32599
33634
|
registerBuiltinLoweringPlugins();
|
|
32600
33635
|
export {
|
|
33636
|
+
unsupportedAttrExpression,
|
|
33637
|
+
unboundCallbackParamReason,
|
|
33638
|
+
unboundCallbackParamRead,
|
|
32601
33639
|
tsNodeToParsedExpr,
|
|
32602
33640
|
traceableNames,
|
|
32603
33641
|
traceUpdatePath,
|
|
32604
33642
|
testAdapter,
|
|
33643
|
+
templateNameCollisionErrors,
|
|
32605
33644
|
stringifyParsedExpr,
|
|
32606
33645
|
stringifyBooleanTernaryBranches,
|
|
33646
|
+
spellComponentProps,
|
|
33647
|
+
sortRefusal,
|
|
33648
|
+
sortEvalPlan,
|
|
32607
33649
|
sortComparatorFromArrow,
|
|
32608
33650
|
serializeParsedExpr,
|
|
32609
33651
|
searchParamsLocalNames,
|
|
@@ -32620,6 +33662,7 @@ export {
|
|
|
32620
33662
|
resolveDangerousInnerHtml,
|
|
32621
33663
|
resolveBodyDestructuredPropAliases,
|
|
32622
33664
|
resetCompilerCounters,
|
|
33665
|
+
renameFreeIdentifiers,
|
|
32623
33666
|
registerLoweringPlugin,
|
|
32624
33667
|
registerBuiltinLoweringPlugins,
|
|
32625
33668
|
queryHrefPlugin,
|
|
@@ -32628,7 +33671,13 @@ export {
|
|
|
32628
33671
|
propsDestructureBinding,
|
|
32629
33672
|
profileToJSON,
|
|
32630
33673
|
prepareLoweringMatchers,
|
|
33674
|
+
prefixedArrayMethodSpelling,
|
|
32631
33675
|
predicateTernaryToLogical,
|
|
33676
|
+
planSpreadAttr,
|
|
33677
|
+
planLoop,
|
|
33678
|
+
planComponentProps,
|
|
33679
|
+
planAttrExpression,
|
|
33680
|
+
parseUndefinedAlternateTernary,
|
|
32632
33681
|
parseStyleObjectEntries,
|
|
32633
33682
|
parseRecordIndexAccess,
|
|
32634
33683
|
parseProviderObjectLiteral,
|
|
@@ -32637,6 +33686,7 @@ export {
|
|
|
32637
33686
|
parseBlockBodyTolerant,
|
|
32638
33687
|
parseBlockBody,
|
|
32639
33688
|
nullableSignalAttrGetter,
|
|
33689
|
+
nestedCallbackRefusal,
|
|
32640
33690
|
needsTypeBasedDetection,
|
|
32641
33691
|
materializeGetterCalls,
|
|
32642
33692
|
matchSearchParamsMethodCall,
|
|
@@ -32644,6 +33694,8 @@ export {
|
|
|
32644
33694
|
matchLoweringCall,
|
|
32645
33695
|
makeIdCallRegex,
|
|
32646
33696
|
loweringNodeChildren,
|
|
33697
|
+
lowerCallbackMethod,
|
|
33698
|
+
lowerArrayMethod,
|
|
32647
33699
|
lookupStaticRecordLiteral,
|
|
32648
33700
|
lookupLiteralConst,
|
|
32649
33701
|
literalMemberIndex,
|
|
@@ -32656,23 +33708,35 @@ export {
|
|
|
32656
33708
|
isSupportedValue,
|
|
32657
33709
|
isSupported,
|
|
32658
33710
|
isStringTypedOperand,
|
|
33711
|
+
isStringTypeInfo,
|
|
32659
33712
|
isStringConcatBinary,
|
|
33713
|
+
isPredicateMethod,
|
|
32660
33714
|
isOpaqueLocalAccessorName,
|
|
33715
|
+
isNumericArithmeticResult,
|
|
32661
33716
|
isNullishLiteral,
|
|
32662
33717
|
isNullableSignal,
|
|
32663
33718
|
isLowerableObjectRestDestructure,
|
|
32664
33719
|
isLowerableLoopDestructure,
|
|
33720
|
+
isLiteralIndex,
|
|
32665
33721
|
isIdentifierName,
|
|
32666
33722
|
isFunctionShapedExpression,
|
|
32667
33723
|
isFullyStaticLiteral,
|
|
33724
|
+
isExplicitStringCall,
|
|
32668
33725
|
isEventHandlerName,
|
|
33726
|
+
isEmptyComponentProps,
|
|
32669
33727
|
isElidedFactoryGetter,
|
|
32670
33728
|
isDangerousInnerHtmlAttr,
|
|
33729
|
+
isBooleanTypedPropRef,
|
|
32671
33730
|
isBooleanResultParsed,
|
|
33731
|
+
isBooleanResultExpr,
|
|
32672
33732
|
isBooleanAttr,
|
|
33733
|
+
isBareStringLiteral,
|
|
33734
|
+
isAriaBooleanAttr,
|
|
32673
33735
|
importsSearchParams,
|
|
32674
33736
|
identifierPath,
|
|
32675
33737
|
hasUnsafeStyleValue,
|
|
33738
|
+
hasOpaqueCall,
|
|
33739
|
+
hasClientInteractivity,
|
|
32676
33740
|
groupObjectLiteralSegments,
|
|
32677
33741
|
groupBinaryOperand,
|
|
32678
33742
|
graphToJSON,
|
|
@@ -32707,6 +33771,7 @@ export {
|
|
|
32707
33771
|
foldBlockToExpr,
|
|
32708
33772
|
findUninstrumentedEffects,
|
|
32709
33773
|
findReachableNames,
|
|
33774
|
+
filterCaptureCollisions,
|
|
32710
33775
|
extractSsrDefaults,
|
|
32711
33776
|
extractFunctionParams,
|
|
32712
33777
|
extractArrowBodyExpression,
|
|
@@ -32720,7 +33785,9 @@ export {
|
|
|
32720
33785
|
enableCompilerInstrumentation,
|
|
32721
33786
|
emitParsedExpr,
|
|
32722
33787
|
emitIRNode,
|
|
33788
|
+
emitCallbackEval,
|
|
32723
33789
|
emitAttrValue,
|
|
33790
|
+
emitArrayMethod,
|
|
32724
33791
|
disableCompilerInstrumentation,
|
|
32725
33792
|
diffStaticBudget,
|
|
32726
33793
|
diffProfiles,
|
|
@@ -32736,15 +33803,21 @@ export {
|
|
|
32736
33803
|
createAnchoredProgram,
|
|
32737
33804
|
containsHigherOrder,
|
|
32738
33805
|
computeSsrSeedPlan,
|
|
33806
|
+
componentPropKey,
|
|
32739
33807
|
compileJSX,
|
|
32740
33808
|
combineParentChildClientJs,
|
|
32741
33809
|
collectValueReferencedNames,
|
|
32742
33810
|
collectNullishAttrContext,
|
|
32743
33811
|
collectNullableSignalGetters,
|
|
33812
|
+
collectNullableOptionalProps,
|
|
32744
33813
|
collectModuleStringConsts,
|
|
32745
33814
|
collectLoopBoundNames,
|
|
33815
|
+
collectImportedLoopChildComponentErrors,
|
|
32746
33816
|
collectContextConsumers,
|
|
33817
|
+
collectBooleanValueNames,
|
|
33818
|
+
collectBooleanTypedProps,
|
|
32747
33819
|
collectAliasableGetterNames,
|
|
33820
|
+
childTemplateName,
|
|
32748
33821
|
buildWhyUpdate,
|
|
32749
33822
|
buildStaticBudget,
|
|
32750
33823
|
buildSourceMapFromIR,
|
|
@@ -32763,10 +33836,13 @@ export {
|
|
|
32763
33836
|
buildComponentSummary,
|
|
32764
33837
|
buildComponentGraph,
|
|
32765
33838
|
buildComponentAnalysis,
|
|
33839
|
+
bareGetterCallName,
|
|
32766
33840
|
augmentInheritedPropAccesses,
|
|
32767
33841
|
attrValueMayBeNullish,
|
|
32768
33842
|
asCallbackMethodCall,
|
|
33843
|
+
arithmeticOperandIsUndefined,
|
|
32769
33844
|
arithmeticOperandIsNull,
|
|
33845
|
+
arithmeticOperandCoercion,
|
|
32770
33846
|
applyCssLayerPrefix,
|
|
32771
33847
|
analyzeWastedReReruns,
|
|
32772
33848
|
analyzeHotSubscribers,
|
|
@@ -32797,5 +33873,6 @@ export {
|
|
|
32797
33873
|
BROWSER_ONLY_CLIENT_APIS,
|
|
32798
33874
|
BOOLEAN_ATTRS,
|
|
32799
33875
|
AttrValueOf,
|
|
32800
|
-
ARRAY_METHOD_NAMES
|
|
33876
|
+
ARRAY_METHOD_NAMES,
|
|
33877
|
+
ARRAY_METHOD_CATALOGUE
|
|
32801
33878
|
};
|