@torpor/view 1.1.3 → 1.2.1
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/compile.cjs +389 -124
- package/dist/compile.d.cts +18 -11
- package/dist/compile.d.cts.map +1 -1
- package/dist/compile.d.mts +18 -11
- package/dist/compile.d.mts.map +1 -1
- package/dist/compile.mjs +389 -124
- package/dist/compile.mjs.map +1 -1
- package/dist/index.cjs +15 -7
- package/dist/index.d.cts +10 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +10 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +15 -7
- package/dist/index.mjs.map +1 -1
- package/dist/ssr.cjs +75 -0
- package/dist/ssr.d.cts +49 -1
- package/dist/ssr.d.cts.map +1 -1
- package/dist/ssr.d.mts +49 -1
- package/dist/ssr.d.mts.map +1 -1
- package/dist/ssr.mjs +42 -1
- package/dist/ssr.mjs.map +1 -1
- package/package.json +11 -11
package/dist/compile.mjs
CHANGED
|
@@ -750,6 +750,157 @@ function addMappedTextWithOffsets(preText, text, postText, spans, offsets, lengt
|
|
|
750
750
|
} else b.append(preText + text + postText);
|
|
751
751
|
}
|
|
752
752
|
//#endregion
|
|
753
|
+
//#region src/compile/utils/containsIdentifier.ts
|
|
754
|
+
/**
|
|
755
|
+
* Whether `name` appears in `value` as a standalone identifier (not as part
|
|
756
|
+
* of a longer identifier or a property access). Used to decide which loop
|
|
757
|
+
* variables a chunk of generated code actually references.
|
|
758
|
+
*/
|
|
759
|
+
function containsIdentifier(value, name) {
|
|
760
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
761
|
+
return new RegExp(`(?<![\\w$])${escaped}(?![\\w$])`).test(value);
|
|
762
|
+
}
|
|
763
|
+
//#endregion
|
|
764
|
+
//#region src/compile/build/client/forVars.ts
|
|
765
|
+
/**
|
|
766
|
+
* Returns the per-scope shadow bindings for every read-only loop variable in
|
|
767
|
+
* scope (`const <name> = <dataPath>;`), one per line, or `""` when there are
|
|
768
|
+
* none.
|
|
769
|
+
*
|
|
770
|
+
* The hook is that a `@for` body's user code is emitted inside a number of
|
|
771
|
+
* compiler-generated scopes (`$run` effects, event handlers, control-node
|
|
772
|
+
* callbacks, the row-create callback, …). Emitting the bindings at the top of
|
|
773
|
+
* each scope makes the loop variable resolve lexically, so shadowing
|
|
774
|
+
* (`arr.filter(child => child.ok)`) and no-space operator styles just work —
|
|
775
|
+
* unlike the old textual substitution, which had no notion of scope.
|
|
776
|
+
*
|
|
777
|
+
* The binding is re-created on every invocation of the scope, so reads stay
|
|
778
|
+
* live and tracked (in the proxy path, reading `<dataPath>` subscribes the
|
|
779
|
+
* effect to the loop variable's signal).
|
|
780
|
+
*/
|
|
781
|
+
function forVarBindings(status, source) {
|
|
782
|
+
return collectBindings(status, (forVar) => source === void 0 || containsIdentifier(source, forVar.name));
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Like `forVarBindings`, but binds only the loop variables named in `names`.
|
|
786
|
+
* Used for the row-create scope, where a separate scan decides which variables
|
|
787
|
+
* the scope's directly-evaluated code reads.
|
|
788
|
+
*/
|
|
789
|
+
function forVarBindingsFor(status, names) {
|
|
790
|
+
return collectBindings(status, (forVar) => names.has(forVar.name));
|
|
791
|
+
}
|
|
792
|
+
function collectBindings(status, include) {
|
|
793
|
+
const bound = /* @__PURE__ */ new Set();
|
|
794
|
+
let result = "";
|
|
795
|
+
for (let i = status.forVars.length - 1; i >= 0; i--) {
|
|
796
|
+
const forVar = status.forVars[i];
|
|
797
|
+
if (!forVar.shadow || bound.has(forVar.name) || !include(forVar)) continue;
|
|
798
|
+
bound.add(forVar.name);
|
|
799
|
+
result = `const ${forVar.name} = ${forVar.path};\n` + result;
|
|
800
|
+
}
|
|
801
|
+
return result;
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Appends the shadow bindings for `status` to the builder, if there are any.
|
|
805
|
+
* (A bare `b.append("")` would still emit a blank line.)
|
|
806
|
+
*/
|
|
807
|
+
function appendForVarBindings(b, status, source) {
|
|
808
|
+
const bindings = forVarBindings(status, source);
|
|
809
|
+
if (bindings !== "") b.append(bindings);
|
|
810
|
+
}
|
|
811
|
+
/** Appends the shadow bindings for the named loop variables, if there are any. */
|
|
812
|
+
function appendForVarBindingsFor(b, status, names) {
|
|
813
|
+
const bindings = forVarBindingsFor(status, names);
|
|
814
|
+
if (bindings !== "") b.append(bindings);
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Adds the shadow bindings *inside* a user-supplied function expression (an
|
|
818
|
+
* event handler, transition factory, …), at invocation time, rather than
|
|
819
|
+
* letting them be captured from the enclosing row scope.
|
|
820
|
+
*
|
|
821
|
+
* This matters because these callbacks are created once (when the row is
|
|
822
|
+
* created) but invoked later — a binding captured from the row scope would
|
|
823
|
+
* snapshot the loop variable and go stale after keyed reconciliation
|
|
824
|
+
* replaces the row's data.
|
|
825
|
+
*
|
|
826
|
+
* When the expression is a plain arrow / function (the common case) the
|
|
827
|
+
* bindings are injected into its body, so no extra closure is allocated. Any
|
|
828
|
+
* other expression (a call returning a handler, a conditional, …) is wrapped
|
|
829
|
+
* in a passthrough closure instead.
|
|
830
|
+
*/
|
|
831
|
+
function injectForVarHandler(value, status) {
|
|
832
|
+
const bindings = forVarBindings(status, value);
|
|
833
|
+
if (bindings === "") return value;
|
|
834
|
+
const arrow = findTopLevelArrow(value);
|
|
835
|
+
if (arrow !== -1) {
|
|
836
|
+
let body = arrow + 2;
|
|
837
|
+
while (body < value.length && /\s/.test(value[body])) body += 1;
|
|
838
|
+
if (value[body] === "{") return `${value.substring(0, body + 1)}\n${bindings}${value.substring(body + 1)}`;
|
|
839
|
+
return `${value.substring(0, body)}{\n${bindings}return ${value.substring(body)}\n}`;
|
|
840
|
+
}
|
|
841
|
+
if (/\bfunction\b/.test(value)) {
|
|
842
|
+
const brace = findBodyBrace(value);
|
|
843
|
+
if (brace !== -1) return `${value.substring(0, brace + 1)}\n${bindings}${value.substring(brace + 1)}`;
|
|
844
|
+
}
|
|
845
|
+
return `function (...t_args) {\n${bindings}return (${value}).apply(this, t_args);\n}`;
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Returns the index of the first `=>` at the top level of `value` (not inside
|
|
849
|
+
* a string/comment or nested parens/brackets/braces), or -1. Used to find the
|
|
850
|
+
* outer arrow of a handler expression.
|
|
851
|
+
*/
|
|
852
|
+
function findTopLevelArrow(value) {
|
|
853
|
+
let depth = 0;
|
|
854
|
+
let i = 0;
|
|
855
|
+
while (i < value.length) {
|
|
856
|
+
const skipped = skipStringOrComment(value, i);
|
|
857
|
+
if (skipped !== -1) {
|
|
858
|
+
i = skipped;
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
const char = value[i];
|
|
862
|
+
if (char === "(" || char === "[" || char === "{") depth += 1;
|
|
863
|
+
else if (char === ")" || char === "]" || char === "}") depth -= 1;
|
|
864
|
+
else if (char === "=" && value[i + 1] === ">" && depth === 0) return i;
|
|
865
|
+
i += 1;
|
|
866
|
+
}
|
|
867
|
+
return -1;
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Injects the shadow bindings at the top of a `@function` declaration's body,
|
|
871
|
+
* so a named function can reference enclosing loop variables and still see
|
|
872
|
+
* their current values when it is called later.
|
|
873
|
+
*/
|
|
874
|
+
function injectIntoFunctionBody(statement, status) {
|
|
875
|
+
const bindings = forVarBindings(status, statement);
|
|
876
|
+
if (bindings === "") return statement;
|
|
877
|
+
const brace = findBodyBrace(statement);
|
|
878
|
+
if (brace === -1) return statement;
|
|
879
|
+
return `${statement.substring(0, brace + 1)}\n${bindings}${statement.substring(brace + 1)}`;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Returns the index of the `{` that opens an expression's function body (the
|
|
883
|
+
* first top-level brace, skipping parameter parens/brackets and
|
|
884
|
+
* strings/comments), or -1 if there isn't one.
|
|
885
|
+
*/
|
|
886
|
+
function findBodyBrace(value) {
|
|
887
|
+
let depth = 0;
|
|
888
|
+
let i = 0;
|
|
889
|
+
while (i < value.length) {
|
|
890
|
+
const skipped = skipStringOrComment(value, i);
|
|
891
|
+
if (skipped !== -1) {
|
|
892
|
+
i = skipped;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
const char = value[i];
|
|
896
|
+
if (char === "(" || char === "[") depth += 1;
|
|
897
|
+
else if (char === ")" || char === "]") depth -= 1;
|
|
898
|
+
else if (char === "{" && depth === 0) return i;
|
|
899
|
+
i += 1;
|
|
900
|
+
}
|
|
901
|
+
return -1;
|
|
902
|
+
}
|
|
903
|
+
//#endregion
|
|
753
904
|
//#region src/compile/build/client/buildAddFragment.ts
|
|
754
905
|
function buildAddFragment(node, status, b, parentName, anchorName) {
|
|
755
906
|
if (node.fragment) {
|
|
@@ -765,6 +916,7 @@ function buildAddFragment(node, status, b, parentName, anchorName) {
|
|
|
765
916
|
let trailing = "";
|
|
766
917
|
if (forVarMask !== void 0) trailing = `, undefined, { forVarMask: ${forVarMask} }`;
|
|
767
918
|
b.append("$run(() => {");
|
|
919
|
+
appendForVarBindings(b, status, fragment.effects.map((e) => e.functionBody).join("\n"));
|
|
768
920
|
if (status.options.mapped === true) for (let effect of fragment.effects) addMappedTextWithOffsets("", effect.functionBody, "", effect.spans, effect.offsets, effect.lengths, status, b);
|
|
769
921
|
else b.append(fragment.effects.map((e) => e.functionBody).join("\n"));
|
|
770
922
|
b.append(`}${status.options.dev === true ? `, "setAttributes"${trailing}` : trailing});`);
|
|
@@ -1159,58 +1311,35 @@ function getFragmentVarPathPart(path, varPath, status, root = false, rootFn = "t
|
|
|
1159
1311
|
//#endregion
|
|
1160
1312
|
//#region src/compile/utils/forVarsReadIn.ts
|
|
1161
1313
|
/**
|
|
1162
|
-
* Returns a bitmask of the for-loop variables whose
|
|
1163
|
-
*
|
|
1164
|
-
* is read.
|
|
1314
|
+
* Returns a bitmask of the for-loop variables whose names appear in `expr`.
|
|
1315
|
+
* Bit N is set when the Nth for-var (by position in `forVars`) is read.
|
|
1165
1316
|
*
|
|
1166
|
-
*
|
|
1167
|
-
* body has been rewritten to its data-bag path (e.g. `row` →
|
|
1168
|
-
* `t_item_42.data.row`, or `t_item_42.data` for single-var loops). This
|
|
1169
|
-
* helper scans the POST-substitution `expr` for those paths and returns a
|
|
1170
|
-
* bitmask — used to annotate each effect with `forVarMask` so the keyed-list
|
|
1317
|
+
* Used to annotate each effect with `forVarMask` so the keyed-list
|
|
1171
1318
|
* reconciler's no-proxy force-rerun path (`rerunRegionEffects`) can skip
|
|
1172
1319
|
* effects that don't depend on the changed for-vars via a single bitwise AND.
|
|
1173
1320
|
*
|
|
1321
|
+
* Read-only loop vars are referenced by their bare name (they resolve to a
|
|
1322
|
+
* per-scope shadow binding); written vars are substituted to their data path,
|
|
1323
|
+
* whose trailing property is the variable name. Matching on an
|
|
1324
|
+
* identifier-boundary regex catches both (`t_item.data.row` contains a
|
|
1325
|
+
* standalone `row`), without matching substrings of the compiler's generated
|
|
1326
|
+
* names (e.g. the `i` inside `t_item_1`).
|
|
1327
|
+
*
|
|
1174
1328
|
* Conservative by design: a false positive (claiming a dependency that
|
|
1175
1329
|
* doesn't really exist) just causes an unnecessary re-run, while a false
|
|
1176
|
-
* negative would drop a needed update.
|
|
1177
|
-
* contain the substituted path are extremely unlikely and harmless.
|
|
1330
|
+
* negative would drop a needed update.
|
|
1178
1331
|
*/
|
|
1179
|
-
function forVarsReadIn(expr,
|
|
1332
|
+
function forVarsReadIn(expr, forVars) {
|
|
1180
1333
|
let mask = 0;
|
|
1181
|
-
for (let i = 0; i <
|
|
1182
|
-
const subPath = forVarNames[i][1];
|
|
1183
|
-
if (subPath && expr.includes(subPath)) mask |= 1 << i;
|
|
1184
|
-
}
|
|
1334
|
+
for (let i = 0; i < forVars.length; i++) if (containsIdentifier(expr, forVars[i].name)) mask |= 1 << i;
|
|
1185
1335
|
return mask;
|
|
1186
1336
|
}
|
|
1187
1337
|
//#endregion
|
|
1188
|
-
//#region src/compile/build/client/replaceForVarNames.ts
|
|
1189
|
-
/**
|
|
1190
|
-
* Variables used within a for loop need to be retrieved from the loop data.
|
|
1191
|
-
*/
|
|
1192
|
-
function replaceForVarNames(value, status) {
|
|
1193
|
-
if (status.forVarNames.length === 0) return value;
|
|
1194
|
-
let result = "";
|
|
1195
|
-
let index = 0;
|
|
1196
|
-
for (let [start, end] of codeRanges(value)) {
|
|
1197
|
-
result += value.substring(index, start);
|
|
1198
|
-
result += replaceVarNames(value.substring(start, end), status);
|
|
1199
|
-
index = end;
|
|
1200
|
-
}
|
|
1201
|
-
result += value.substring(index);
|
|
1202
|
-
return result;
|
|
1203
|
-
}
|
|
1204
|
-
function replaceVarNames(code, status) {
|
|
1205
|
-
for (let varName of status.forVarNames) code = code.replaceAll(new RegExp(`(^|\\s|\\(|\\[|\\{|!|\\?|\\.\\.\\.)${varName[0]}($|\\s|\\.|,|\\(|\\)|\\[|\\]|\\}|;|\\?)`, "g"), `$1${varName[1]}$2`);
|
|
1206
|
-
return code;
|
|
1207
|
-
}
|
|
1208
|
-
//#endregion
|
|
1209
1338
|
//#region src/compile/build/client/buildRun.ts
|
|
1210
1339
|
function buildRun(functionName, functionBody, status, b) {
|
|
1211
|
-
functionBody = replaceForVarNames(functionBody, status);
|
|
1212
1340
|
addPushDevBoundary("run", functionName, status, b);
|
|
1213
|
-
let forVarMask = status.
|
|
1341
|
+
let forVarMask = status.forVars.length > 0 ? forVarsReadIn(functionBody, status.forVars) : void 0;
|
|
1342
|
+
functionBody = forVarBindings(status, functionBody) + functionBody;
|
|
1214
1343
|
let trailing = "";
|
|
1215
1344
|
if (status.options.dev === true) {
|
|
1216
1345
|
trailing = `, "${functionName}"`;
|
|
@@ -1365,15 +1494,15 @@ function buildComponentNode(node, status, b, root = false) {
|
|
|
1365
1494
|
});
|
|
1366
1495
|
b.append(`let ${propsName} = $watch({`);
|
|
1367
1496
|
for (let p of props) {
|
|
1368
|
-
let value =
|
|
1497
|
+
let value = p.value;
|
|
1369
1498
|
p.preText ??= "";
|
|
1370
1499
|
p.postText ??= "";
|
|
1371
1500
|
if (p.spans.length === 1) addMappedText(p.name ? `${p.name}: ` + p.preText : p.preText, value, p.postText + ",", p.spans[0], status, b);
|
|
1372
1501
|
else addMappedTextWithOffsets(p.name ? `${p.name}: ` + p.preText : p.preText, value, p.postText + ",", p.spans, p.offsets, p.lengths, status, b);
|
|
1373
1502
|
}
|
|
1374
1503
|
b.append("});");
|
|
1375
|
-
if (runs.length) buildRun("setProps",
|
|
1376
|
-
if (bindingRuns.length) buildRun("setBindings",
|
|
1504
|
+
if (runs.length) buildRun("setProps", runs.join("\n"), status, b);
|
|
1505
|
+
if (bindingRuns.length) buildRun("setBindings", bindingRuns.join("\n"), status, b);
|
|
1377
1506
|
if (root) {
|
|
1378
1507
|
status.imports.add("$run");
|
|
1379
1508
|
b.append(`
|
|
@@ -1439,6 +1568,7 @@ function buildAwaitNode(node, status, b) {
|
|
|
1439
1568
|
b.append(`
|
|
1440
1569
|
const ${regionName} = t_region(${status.options.dev === true ? `"await"` : ""});
|
|
1441
1570
|
t_run_await(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
1571
|
+
appendForVarBindings(b, status);
|
|
1442
1572
|
if (awaitBranch !== void 0 && awaitBranch.children.length > 0) {
|
|
1443
1573
|
buildFragment(awaitBranch, status, b, parentName, "t_before");
|
|
1444
1574
|
status.fragmentStack.push({
|
|
@@ -1451,6 +1581,7 @@ function buildAwaitNode(node, status, b) {
|
|
|
1451
1581
|
}
|
|
1452
1582
|
if (withBranch !== void 0) {
|
|
1453
1583
|
b.append(`}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
1584
|
+
appendForVarBindings(b, status);
|
|
1454
1585
|
if (withBranch.children.length > 0) {
|
|
1455
1586
|
buildFragment(withBranch, status, b, parentName, "t_before");
|
|
1456
1587
|
status.fragmentStack.push({
|
|
@@ -1526,6 +1657,78 @@ function isForBodyLeafSafe(forBodyChildren) {
|
|
|
1526
1657
|
return !hasNestedControl(forBodyChildren);
|
|
1527
1658
|
}
|
|
1528
1659
|
//#endregion
|
|
1660
|
+
//#region src/compile/utils/forVarsWrittenIn.ts
|
|
1661
|
+
/**
|
|
1662
|
+
* Returns the set of `forVars` that are written anywhere in `children`,
|
|
1663
|
+
* treating any write-target mention (direct assignment, property/index
|
|
1664
|
+
* assignment, `&`-binding) as a write.
|
|
1665
|
+
*
|
|
1666
|
+
* This is the conservative scan behind `isForBodyNoProxySafe`: anything
|
|
1667
|
+
* ambiguous must disable the no-proxy specialization, so a *property* write
|
|
1668
|
+
* (`row.name = …`) counts even though it doesn't replace the loop variable.
|
|
1669
|
+
*/
|
|
1670
|
+
function forVarsWrittenIn(children, forVars) {
|
|
1671
|
+
return scan(children, forVars, (escaped) => `\\b${escaped}(?:\\.[a-zA-Z_$][\\w$]*|\\[[^\\]]+\\])?\\s*(?:=(?![=>])|[+\\-*/%&|^]=|\\+\\+|--)`);
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* Returns the set of `forVars` whose *binding* is written in `children` — i.e.
|
|
1675
|
+
* the loop variable itself is assigned (`row = …`, `row += …`, `row++`), bound
|
|
1676
|
+
* as a two-way target (`&value={row}`), or destructured into (`[row] = …`,
|
|
1677
|
+
* `{row} = …`).
|
|
1678
|
+
*
|
|
1679
|
+
* Loop variables are read-only (a `@for` extracts a copy of each element), so
|
|
1680
|
+
* `buildForNode` reports each name in this set as a compile error. A write to
|
|
1681
|
+
* a *property* (`row.name = …`) or *index* (`row[i] = …`) mutates the row
|
|
1682
|
+
* object through the shadowed reference and is not included.
|
|
1683
|
+
*/
|
|
1684
|
+
function forVarsReassignedIn(children, forVars) {
|
|
1685
|
+
return scan(children, forVars, (escaped) => `(?<![\\w$.])${escaped}\\s*(?:=(?![=>])|[+\\-*/%&|^]=|\\+\\+|--)`);
|
|
1686
|
+
}
|
|
1687
|
+
function scan(children, forVars, bareWritePattern) {
|
|
1688
|
+
const found = /* @__PURE__ */ new Set();
|
|
1689
|
+
if (forVars.length === 0) return found;
|
|
1690
|
+
const collected = [];
|
|
1691
|
+
for (const child of children) collectExpressionStrings(child, collected);
|
|
1692
|
+
for (const varName of forVars) {
|
|
1693
|
+
const escaped = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1694
|
+
const writePattern = new RegExp(`(?<![\\w$.\\])\\)])\\[\\s*${escaped}\\s*\\]\\s*=(?![=>])|(?<![\\w$.\\])\\)])\\{\\s*${escaped}\\s*(:[^}]+)?\\}\\s*=(?![=>])|` + bareWritePattern(escaped) + `|(?<![\\w$.])(?:\\+\\+|--)\\s*${escaped}(?![\\w$])`);
|
|
1695
|
+
for (const text of collected) if (writePattern.test(text)) {
|
|
1696
|
+
found.add(varName);
|
|
1697
|
+
break;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
return found;
|
|
1701
|
+
}
|
|
1702
|
+
function isElementLike(node) {
|
|
1703
|
+
return node.type === "element" || node.type === "component" || node.type === "special";
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Collects every expression-like string from the body, including attribute
|
|
1707
|
+
* values (with `&`-binding targets marked by a trailing `=`), text
|
|
1708
|
+
* interpolations, and control statements (recursing into nested controls).
|
|
1709
|
+
*/
|
|
1710
|
+
function collectExpressionStrings(node, out) {
|
|
1711
|
+
if (isTextNode(node)) {
|
|
1712
|
+
out.push(node.content);
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
if (isElementLike(node)) {
|
|
1716
|
+
for (const attr of node.attributes) {
|
|
1717
|
+
if (attr.value !== void 0) {
|
|
1718
|
+
if (attr.name.startsWith("&")) out.push(`${attr.value} =`);
|
|
1719
|
+
else out.push(attr.value);
|
|
1720
|
+
}
|
|
1721
|
+
if (attr.fullyReactive && attr.name !== attr.value) out.push(attr.name);
|
|
1722
|
+
}
|
|
1723
|
+
for (const child of node.children) collectExpressionStrings(child, out);
|
|
1724
|
+
return;
|
|
1725
|
+
}
|
|
1726
|
+
if (isControlNode(node)) {
|
|
1727
|
+
out.push(node.statement);
|
|
1728
|
+
for (const child of node.children) collectExpressionStrings(child, out);
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
//#endregion
|
|
1529
1732
|
//#region src/compile/utils/isForBodyNoProxySafe.ts
|
|
1530
1733
|
/**
|
|
1531
1734
|
* Returns true if the body of a `@for` loop never writes to any of its loop
|
|
@@ -1557,37 +1760,59 @@ function isForBodyLeafSafe(forBodyChildren) {
|
|
|
1557
1760
|
function isForBodyNoProxySafe(forBodyChildren, forVars) {
|
|
1558
1761
|
if (forVars.length === 0) return false;
|
|
1559
1762
|
if (hasNestedControl(forBodyChildren)) return false;
|
|
1560
|
-
|
|
1561
|
-
for (const child of forBodyChildren) collectExpressionStrings(child, collected);
|
|
1562
|
-
for (const varName of forVars) {
|
|
1563
|
-
const escaped = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1564
|
-
const writePattern = new RegExp(`\\[\\s*${escaped}\\s*\\]\\s*=(?![=>])|\\{\\s*${escaped}\\s*(:[^}]+)?\\}\\s*=(?![=>])|\\b${escaped}(?:\\.[a-zA-Z_$][\\w$]*|\\[[^\\]]+\\])?\\s*(?:=(?![=>])|[+\\-*/%&|^]=|\\+\\+|--)|(?:\\+\\+|--)\\s*${escaped}\\b`);
|
|
1565
|
-
for (const text of collected) if (writePattern.test(text)) return false;
|
|
1566
|
-
}
|
|
1567
|
-
return true;
|
|
1763
|
+
return forVarsWrittenIn(forBodyChildren, forVars).size === 0;
|
|
1568
1764
|
}
|
|
1569
|
-
|
|
1570
|
-
|
|
1765
|
+
//#endregion
|
|
1766
|
+
//#region src/compile/utils/isComponentNode.ts
|
|
1767
|
+
function isComponentNode(node) {
|
|
1768
|
+
return node.type === "component";
|
|
1571
1769
|
}
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1770
|
+
//#endregion
|
|
1771
|
+
//#region src/compile/utils/forVarsReadDirectlyIn.ts
|
|
1772
|
+
/**
|
|
1773
|
+
* Returns the subset of `forVars` that are referenced by code evaluated
|
|
1774
|
+
* *synchronously in the enclosing row-create scope*, rather than inside a
|
|
1775
|
+
* nested function scope the compiler emits.
|
|
1776
|
+
*
|
|
1777
|
+
* Every compiler-generated scope (effects, handlers, control callbacks,
|
|
1778
|
+
* `$onmount`, `@function` bodies) emits its own shadow binding for the loop
|
|
1779
|
+
* vars it reads. The remaining "direct" positions are evaluated in the
|
|
1780
|
+
* row-create scope itself and rely on the binding emitted at the top of the
|
|
1781
|
+
* row callback:
|
|
1782
|
+
*
|
|
1783
|
+
* - a component's initial props object (`<Child label={item.name} />`),
|
|
1784
|
+
* including fill/slot content that closes over the row scope,
|
|
1785
|
+
* - `@const` / `@console` / `@debugger` statements,
|
|
1786
|
+
* - a `&ref` write target, and an interpolated `@element` tag.
|
|
1787
|
+
*
|
|
1788
|
+
* Emitting the row-scope binding only when one of these needs it avoids an
|
|
1789
|
+
* unused `const <var> = <data>;` line in the common case.
|
|
1790
|
+
*/
|
|
1791
|
+
function forVarsReadDirectlyIn(children, forVars) {
|
|
1792
|
+
const sources = [];
|
|
1793
|
+
collectDirect(children, sources);
|
|
1794
|
+
const found = /* @__PURE__ */ new Set();
|
|
1795
|
+
for (const forVar of forVars) {
|
|
1796
|
+
if (!forVar.shadow) continue;
|
|
1797
|
+
if (sources.some((source) => containsIdentifier(source, forVar.name))) found.add(forVar.name);
|
|
1576
1798
|
}
|
|
1577
|
-
|
|
1799
|
+
return found;
|
|
1800
|
+
}
|
|
1801
|
+
function collectDirect(nodes, out) {
|
|
1802
|
+
for (const node of nodes) if (isComponentNode(node)) {
|
|
1803
|
+
for (const attr of node.attributes) if (attr.value != null) out.push(attr.value);
|
|
1804
|
+
collectDirect(node.children, out);
|
|
1805
|
+
} else if (isElementNode(node) || isSpecialNode(node)) {
|
|
1806
|
+
const slotProps = isSpecialNode(node) && (node.tagName === "slot" || node.tagName === "fill" || node.tagName === "filldef");
|
|
1578
1807
|
for (const attr of node.attributes) {
|
|
1579
|
-
if (attr.value
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
if (attr.fullyReactive && attr.name !== attr.value) out.push(attr.name);
|
|
1808
|
+
if (attr.value == null) continue;
|
|
1809
|
+
if (slotProps) out.push(attr.value);
|
|
1810
|
+
else if (attr.name === "&ref") out.push(attr.value);
|
|
1811
|
+
else if (attr.name === "self" && !attr.fullyReactive) out.push(attr.value);
|
|
1584
1812
|
}
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
if (isControlNode(node)) {
|
|
1589
|
-
out.push(node.statement);
|
|
1590
|
-
for (const child of node.children) collectExpressionStrings(child, out);
|
|
1813
|
+
collectDirect(node.children, out);
|
|
1814
|
+
} else if (isControlNode(node)) {
|
|
1815
|
+
if (node.operation === "@const" || node.operation === "@console" || node.operation === "@debugger") out.push(node.statement);
|
|
1591
1816
|
}
|
|
1592
1817
|
}
|
|
1593
1818
|
//#endregion
|
|
@@ -1609,6 +1834,43 @@ function trimMatched(text, startChar, endChar) {
|
|
|
1609
1834
|
return text.substring(start, end);
|
|
1610
1835
|
}
|
|
1611
1836
|
//#endregion
|
|
1837
|
+
//#region src/compile/build/client/addBuildError.ts
|
|
1838
|
+
/**
|
|
1839
|
+
* Adds an error to the build status. Mirrors `parse/utils/addError`, but is
|
|
1840
|
+
* used during building (where loop-variable write rules, etc. are enforced).
|
|
1841
|
+
*
|
|
1842
|
+
* Line/char are derived from `status.options.source` when it is available;
|
|
1843
|
+
* when it isn't, they default to 0 and consumers should rely on `startIndex`.
|
|
1844
|
+
*/
|
|
1845
|
+
function addBuildError(status, message, start, end = start) {
|
|
1846
|
+
const source = status.options.source;
|
|
1847
|
+
let startLine = 0;
|
|
1848
|
+
let lastLineStart = 0;
|
|
1849
|
+
let i = 0;
|
|
1850
|
+
if (source !== void 0) {
|
|
1851
|
+
for (; i < start && i < source.length; i++) if (source[i] === "\n") {
|
|
1852
|
+
startLine += 1;
|
|
1853
|
+
lastLineStart = i + 1;
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
const startChar = start - lastLineStart;
|
|
1857
|
+
let endLine = startLine;
|
|
1858
|
+
for (; i < end && source !== void 0 && i < source.length; i++) if (source[i] === "\n") {
|
|
1859
|
+
endLine += 1;
|
|
1860
|
+
lastLineStart = i + 1;
|
|
1861
|
+
}
|
|
1862
|
+
const error = {
|
|
1863
|
+
message,
|
|
1864
|
+
startIndex: start,
|
|
1865
|
+
startLine,
|
|
1866
|
+
startChar,
|
|
1867
|
+
endIndex: end,
|
|
1868
|
+
endLine,
|
|
1869
|
+
endChar: end - lastLineStart
|
|
1870
|
+
};
|
|
1871
|
+
status.errors.push(error);
|
|
1872
|
+
}
|
|
1873
|
+
//#endregion
|
|
1612
1874
|
//#region src/compile/build/client/buildForNode.ts
|
|
1613
1875
|
const forLoopRegex = /for\s*\((.+?);.*?;.*?\)/;
|
|
1614
1876
|
const forLoopVarsRegex = /(?:let\s+|var\s+){0,1}([^\s,;+=]+)(?:\s*=\s*[^,;]+){0,1}/g;
|
|
@@ -1645,6 +1907,8 @@ function buildForNode(node, status, b) {
|
|
|
1645
1907
|
}
|
|
1646
1908
|
const noWatch = isForBodyNoProxySafe(node.children, forVarNames);
|
|
1647
1909
|
if (noWatch) status.imports.add("t_rerun_region_effects");
|
|
1910
|
+
const reassignedForVars = forVarsReassignedIn(node.children, forVarNames);
|
|
1911
|
+
for (const name of reassignedForVars) addBuildError(status, `Cannot assign to loop variable "${name}": a @for loop variable is read-only. Assign to a property of the item (\`${name}.foo = …\`) instead, or mutate the source collection directly.`, node.span.start, node.span.end);
|
|
1648
1912
|
const singleVar = noWatch && forVarNames.length === 1;
|
|
1649
1913
|
const leafRow = isForBodyLeafSafe(node.children);
|
|
1650
1914
|
status.imports.add("t_region");
|
|
@@ -1661,7 +1925,8 @@ function buildForNode(node, status, b) {
|
|
|
1661
1925
|
${anchorName},
|
|
1662
1926
|
${status.options.dev === true ? "function createNewItems() {" : "() => {"}
|
|
1663
1927
|
let ${listItemsName}: ListItemSpec[] = [];`);
|
|
1664
|
-
|
|
1928
|
+
appendForVarBindings(b, status, `${node.statement}\n${keyStatement}`);
|
|
1929
|
+
addMappedText("", `${node.statement}`, " {", node.span, status, b);
|
|
1665
1930
|
if (singleVar) b.append(`${listItemsName}.push({ data: ${forVarNames[0]}, key: `);
|
|
1666
1931
|
else b.append(`${listItemsName}.push({ data: { ${forVarNames.join(", ")} }, key: `);
|
|
1667
1932
|
if (key !== void 0) addMappedText("", `${keyStatement || "undefined"}`, " });", key.span, status, b);
|
|
@@ -1670,11 +1935,16 @@ function buildForNode(node, status, b) {
|
|
|
1670
1935
|
b.append(`return ${listItemsName};`);
|
|
1671
1936
|
b.append(`},
|
|
1672
1937
|
${status.options.dev === true ? `function createListItem(${itemName}, ${beforeName}) {` : `(${itemName}, ${beforeName}) => {`}`);
|
|
1673
|
-
let
|
|
1674
|
-
const forVarBitBase =
|
|
1675
|
-
status.
|
|
1938
|
+
let oldForVars = status.forVars;
|
|
1939
|
+
const forVarBitBase = oldForVars.length;
|
|
1940
|
+
status.forVars = [...oldForVars, ...forVarNames.map((v) => ({
|
|
1941
|
+
name: v,
|
|
1942
|
+
path: singleVar ? `${itemName}.data` : `${itemName}.data.${v}`,
|
|
1943
|
+
shadow: true
|
|
1944
|
+
}))];
|
|
1945
|
+
appendForVarBindingsFor(b, status, forVarsReadDirectlyIn(node.children, status.forVars));
|
|
1676
1946
|
buildForItem(node, status, b, parentName, beforeName, itemName, leafRow);
|
|
1677
|
-
status.
|
|
1947
|
+
status.forVars = oldForVars;
|
|
1678
1948
|
b.append(`},
|
|
1679
1949
|
${status.options.dev === true ? "function updateListItem(t_old_item, t_new_item) {" : "(t_old_item, t_new_item) => {"}`);
|
|
1680
1950
|
if (noWatch) {
|
|
@@ -1736,7 +2006,8 @@ function buildHtmlNode(node, status, b) {
|
|
|
1736
2006
|
b.append(`let ${firstNodeVar}: ChildNode | null = null;`);
|
|
1737
2007
|
b.append(`let ${lastNodeVar}: ChildNode | null = null;`);
|
|
1738
2008
|
b.append(`t_run_control(t_region(), ${htmlAnchorName}, (t_before) => {`);
|
|
1739
|
-
b
|
|
2009
|
+
appendForVarBindings(b, status);
|
|
2010
|
+
b.append(`${node.statement};`);
|
|
1740
2011
|
b.append(`if (${firstNodeVar} !== null && ${lastNodeVar} !== null) {`);
|
|
1741
2012
|
b.append(`let t_node: ChildNode | null = ${lastNodeVar};`);
|
|
1742
2013
|
b.append(`while (t_node !== null && t_node !== ${firstNodeVar}) {`);
|
|
@@ -1752,7 +2023,7 @@ function buildHtmlNode(node, status, b) {
|
|
|
1752
2023
|
b.append(`let ${templateName} = document.createElement("template");`);
|
|
1753
2024
|
node.span.start += 5;
|
|
1754
2025
|
node.span.end -= 2;
|
|
1755
|
-
addMappedText("", `${templateName}.innerHTML = ${
|
|
2026
|
+
addMappedText("", `${templateName}.innerHTML = ${node.statement};`, "", node.span, status, b);
|
|
1756
2027
|
b.append(`let ${fragmentName} = ${templateName}.content.cloneNode(true) as DocumentFragment;`);
|
|
1757
2028
|
b.append(`${firstNodeVar} = ${fragmentName}.firstChild;`);
|
|
1758
2029
|
b.append(`${lastNodeVar} = ${fragmentName}.lastChild;`);
|
|
@@ -1807,6 +2078,7 @@ function buildIfNode(node, status, b) {
|
|
|
1807
2078
|
const ${regionName} = t_region(${status.options.dev === true ? `"if"` : ""});
|
|
1808
2079
|
let ${indexName} = -1;
|
|
1809
2080
|
t_run_control(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
2081
|
+
appendForVarBindings(b, status);
|
|
1810
2082
|
let index = 0;
|
|
1811
2083
|
for (let branch of branches) buildIfBranch(branch, status, b, parentName, regionName, indexName, index++);
|
|
1812
2084
|
b.append(`}${status.options.dev === true ? `, "runIf"` : ""});`);
|
|
@@ -1814,7 +2086,7 @@ function buildIfNode(node, status, b) {
|
|
|
1814
2086
|
b.append("");
|
|
1815
2087
|
}
|
|
1816
2088
|
function buildIfBranch(node, status, b, parentName, regionName, indexName, index) {
|
|
1817
|
-
addMappedText("", `${
|
|
2089
|
+
addMappedText("", `${node.statement}`, " {", node.span, status, b);
|
|
1818
2090
|
b.append(`if (!t_run_branch(${regionName}, ${indexName}, ${index})) return;`);
|
|
1819
2091
|
if (node.children.length > 0) {
|
|
1820
2092
|
b.append(`
|
|
@@ -1851,13 +2123,14 @@ function buildReplaceNode(node, status, b) {
|
|
|
1851
2123
|
/* @replace */
|
|
1852
2124
|
const ${replaceRegionName} = t_region(${status.options.dev === true ? `"replace"` : ""});
|
|
1853
2125
|
t_run_control(${replaceRegionName}, ${replaceAnchorName}, (t_before) => {`);
|
|
2126
|
+
appendForVarBindings(b, status);
|
|
1854
2127
|
buildReplaceBranch(node, status, b, replaceParentName, replaceRegionName);
|
|
1855
2128
|
b.append(`}${status.options.dev === true ? ", \"replace\"" : ""});`);
|
|
1856
2129
|
b.append("");
|
|
1857
2130
|
}
|
|
1858
2131
|
function buildReplaceBranch(node, status, b, parentName, regionName) {
|
|
1859
2132
|
status.imports.add("t_run_branch");
|
|
1860
|
-
b.append(`${
|
|
2133
|
+
b.append(`${node.statement};`);
|
|
1861
2134
|
b.append(`if (!t_run_branch(${regionName}, 0, -1)) return;`);
|
|
1862
2135
|
b.append(`
|
|
1863
2136
|
const t_new_region = t_region(${status.options.dev === true ? `"replace_branch"` : ""});
|
|
@@ -1877,7 +2150,9 @@ function buildReplaceBranch(node, status, b, parentName, regionName) {
|
|
|
1877
2150
|
//#region src/compile/build/client/buildScriptNode.ts
|
|
1878
2151
|
function buildScriptNode(node, status, b) {
|
|
1879
2152
|
b.append(`/* ${node.operation} */`);
|
|
1880
|
-
|
|
2153
|
+
let text = maybeAppend$1(node.statement, ";");
|
|
2154
|
+
if (node.operation === "@function" || node.operation === "@async function") text = injectIntoFunctionBody(text, status);
|
|
2155
|
+
addMappedText("", text, "", node.span, status, b);
|
|
1881
2156
|
}
|
|
1882
2157
|
function maybeAppend$1(text, end) {
|
|
1883
2158
|
if (!text.endsWith(end)) text += end;
|
|
@@ -1913,7 +2188,8 @@ function buildSwitchNode(node, status, b) {
|
|
|
1913
2188
|
const ${regionName} = t_region(${status.options.dev === true ? `"switch"` : ""});
|
|
1914
2189
|
let ${indexName} = -1;
|
|
1915
2190
|
t_run_control(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
1916
|
-
|
|
2191
|
+
appendForVarBindings(b, status);
|
|
2192
|
+
addMappedText("", `${node.statement}`, " {", node.span, status, b);
|
|
1917
2193
|
let index = 0;
|
|
1918
2194
|
for (let branch of branches) buildSwitchBranch(branch, status, b, parentName, regionName, indexName, index++);
|
|
1919
2195
|
b.append(`
|
|
@@ -1923,7 +2199,7 @@ function buildSwitchNode(node, status, b) {
|
|
|
1923
2199
|
b.append("");
|
|
1924
2200
|
}
|
|
1925
2201
|
function buildSwitchBranch(node, status, b, parentName, regionName, indexName, index) {
|
|
1926
|
-
addMappedText("", `${
|
|
2202
|
+
addMappedText("", `${node.statement}`, " {", node.span, status, b);
|
|
1927
2203
|
b.append(`if (!t_run_branch(${regionName}, ${indexName}, ${index})) return;`);
|
|
1928
2204
|
if (node.children.length > 0) {
|
|
1929
2205
|
b.append(`
|
|
@@ -1962,10 +2238,12 @@ function buildTryNode(node, status, b) {
|
|
|
1962
2238
|
b.append(`
|
|
1963
2239
|
const ${regionName} = t_region(${status.options.dev === true ? `"try"` : ""});
|
|
1964
2240
|
t_run_try(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
|
|
2241
|
+
appendForVarBindings(b, status);
|
|
1965
2242
|
buildTryBranchBody(tryBranch, status, b, parentName);
|
|
1966
2243
|
const catchVar = catchBranch ? trimMatched(catchBranch.statement.substring(5).trim(), "(", ")") || "err" : null;
|
|
1967
2244
|
if (catchBranch && catchVar) {
|
|
1968
|
-
addMappedText("},", `(${status.inHead ? "" : "t_before"}, ${
|
|
2245
|
+
addMappedText("},", `(${status.inHead ? "" : "t_before"}, ${catchVar}) => {`, "", catchBranch.span, status, b);
|
|
2246
|
+
appendForVarBindings(b, status);
|
|
1969
2247
|
buildTryBranchBody(catchBranch, status, b, parentName);
|
|
1970
2248
|
b.append(`}${status.options.dev === true ? `, "runTry"` : ""});`);
|
|
1971
2249
|
} else b.append(`}, null${status.options.dev === true ? `, "runTry"` : ""});`);
|
|
@@ -2054,18 +2332,16 @@ function trimEnd(text, char) {
|
|
|
2054
2332
|
//#endregion
|
|
2055
2333
|
//#region src/compile/build/client/buildMount.ts
|
|
2056
2334
|
function buildMount(_functionName, functionBody, status, b) {
|
|
2057
|
-
functionBody = replaceForVarNames(functionBody, status);
|
|
2058
2335
|
status.imports.add("$onmount");
|
|
2059
2336
|
b.append("// @ts-ignore\n$onmount(() => {");
|
|
2060
|
-
b.append(functionBody);
|
|
2337
|
+
b.append(forVarBindings(status, functionBody) + functionBody);
|
|
2061
2338
|
b.append("});");
|
|
2062
2339
|
}
|
|
2063
2340
|
//#endregion
|
|
2064
2341
|
//#region src/compile/build/client/stashRun.ts
|
|
2065
2342
|
function stashRun(fragment, functionStart, value, functionEnd, span, status) {
|
|
2066
|
-
value = replaceForVarNames(value, status);
|
|
2067
2343
|
let functionBody = functionStart + value + functionEnd;
|
|
2068
|
-
let forVarMask = status.
|
|
2344
|
+
let forVarMask = status.forVars.length > 0 ? forVarsReadIn(value, status.forVars) : void 0;
|
|
2069
2345
|
fragment.effects.push({
|
|
2070
2346
|
functionBody,
|
|
2071
2347
|
spans: [span],
|
|
@@ -2078,15 +2354,8 @@ function stashRun(fragment, functionStart, value, functionEnd, span, status) {
|
|
|
2078
2354
|
//#region src/compile/build/client/stashRunWithOffsets.ts
|
|
2079
2355
|
function stashRunWithOffsets(fragment, functionStart, value, functionEnd, spans, offsets, lengths, status) {
|
|
2080
2356
|
let functionBody = functionStart + value + functionEnd;
|
|
2081
|
-
for (let i = 0; i < offsets.length; i++)
|
|
2082
|
-
|
|
2083
|
-
const oldLength = functionBody.length;
|
|
2084
|
-
functionBody = functionBody.substring(0, offsets[i]) + replaceForVarNames(functionBody.substring(offsets[i], offsets[i] + lengths[i]), status) + functionBody.substring(offsets[i] + lengths[i]);
|
|
2085
|
-
const changedLength = functionBody.length - oldLength;
|
|
2086
|
-
lengths[i] += changedLength;
|
|
2087
|
-
for (let j = i + 1; j < offsets.length; j++) offsets[j] += changedLength;
|
|
2088
|
-
}
|
|
2089
|
-
let forVarMask = status.forVarNames.length > 0 ? forVarsReadIn(functionBody, status.forVarNames) : void 0;
|
|
2357
|
+
for (let i = 0; i < offsets.length; i++) offsets[i] += functionStart.length;
|
|
2358
|
+
let forVarMask = status.forVars.length > 0 ? forVarsReadIn(functionBody, status.forVars) : void 0;
|
|
2090
2359
|
fragment.effects.push({
|
|
2091
2360
|
functionBody,
|
|
2092
2361
|
spans,
|
|
@@ -2169,7 +2438,6 @@ function buildHeadNode(node, status, b) {
|
|
|
2169
2438
|
const t_head_el = t_head_element("${node.tagName}", ${JSON.stringify(selector)});`);
|
|
2170
2439
|
for (let { name, value, span } of node.attributes) if (value != null) {
|
|
2171
2440
|
status.imports.add("t_attribute");
|
|
2172
|
-
value = replaceForVarNames(value, status);
|
|
2173
2441
|
addMappedText(`t_attribute(t_head_el, "${name}", `, value, ");", span, status, b);
|
|
2174
2442
|
}
|
|
2175
2443
|
b.append(`}${status.options.dev === true ? `, "runHead"` : ""});`);
|
|
@@ -2200,7 +2468,7 @@ function headElementSelector(node) {
|
|
|
2200
2468
|
function buildElementAttributes$1(node, varName, status, b) {
|
|
2201
2469
|
let fragment = status.fragmentStack.at(-1)?.fragment;
|
|
2202
2470
|
if (!fragment) throw new Error("No fragment on stack");
|
|
2203
|
-
const combinedHandlers = combinedEventHandlers(node
|
|
2471
|
+
const combinedHandlers = combinedEventHandlers(node);
|
|
2204
2472
|
const consumedHandlers = new Set(Array.from(combinedHandlers.values(), (c) => c.index));
|
|
2205
2473
|
for (let [index, { name, value, reactive, fullyReactive, span }] of node.attributes.entries()) if (name === "self" && node.tagName === "@element") {} else if (name === "&ref") {} else if (value != null && fullyReactive) {
|
|
2206
2474
|
if (name === "&group") buildBindGroupAttribute(node, varName, value, status, b, combinedHandlers.get(index));
|
|
@@ -2217,7 +2485,7 @@ function buildElementAttributes$1(node, varName, status, b) {
|
|
|
2217
2485
|
stashRun(fragment, `${status.ns ? "// @ts-ignore\n" : ""}${varName}.${propName} = t_class(`, params.join(", "), ");", span, status);
|
|
2218
2486
|
} else if (name === "style") {
|
|
2219
2487
|
status.imports.add("t_style");
|
|
2220
|
-
stashRun(fragment, `${varName}.style.cssText
|
|
2488
|
+
stashRun(fragment, `${varName}.style.cssText = t_style(`, value, ");", span, status);
|
|
2221
2489
|
} else if (name.includes("-")) {
|
|
2222
2490
|
status.imports.add("t_attribute");
|
|
2223
2491
|
stashRun(fragment, `t_attribute(${varName}, "${name}", `, value, ");", span, status);
|
|
@@ -2236,7 +2504,7 @@ function buildElementAttributes$1(node, varName, status, b) {
|
|
|
2236
2504
|
stashRunWithOffsets(fragment, `${status.ns ? "// @ts-ignore\n" : ""}${varName}.${propName} = t_class(`, params.join(", "), ");", spans, offsets, lengths, status);
|
|
2237
2505
|
} else if (name === "style") {
|
|
2238
2506
|
status.imports.add("t_style");
|
|
2239
|
-
stashRunWithOffsets(fragment, `${varName}.style.cssText
|
|
2507
|
+
stashRunWithOffsets(fragment, `${varName}.style.cssText = t_style(`, value, ");", spans, offsets, lengths, status);
|
|
2240
2508
|
} else if (name.includes("-")) {
|
|
2241
2509
|
status.imports.add("t_attribute");
|
|
2242
2510
|
stashRunWithOffsets(fragment, `t_attribute(${varName}, "${name}", `, value, ");", spans, offsets, lengths, status);
|
|
@@ -2247,7 +2515,6 @@ function buildElementAttributes$1(node, varName, status, b) {
|
|
|
2247
2515
|
}
|
|
2248
2516
|
}
|
|
2249
2517
|
function buildBindGroupAttribute(node, varName, value, status, b, combined) {
|
|
2250
|
-
value = replaceForVarNames(value, status);
|
|
2251
2518
|
let eventName = "change";
|
|
2252
2519
|
let inputValue = node.attributes.find((a) => a.name === "value")?.value;
|
|
2253
2520
|
let set = `${value} == ${inputValue}`;
|
|
@@ -2258,7 +2525,6 @@ function buildBindGroupAttribute(node, varName, value, status, b, combined) {
|
|
|
2258
2525
|
}`, `if (e.target.${propName}) ${value} = ${inputValue};`, combined, status, b);
|
|
2259
2526
|
}
|
|
2260
2527
|
function buildBindAttribute(node, varName, name, value, status, b, combined) {
|
|
2261
|
-
value = replaceForVarNames(value, status);
|
|
2262
2528
|
let eventName = bindEventName(node, name);
|
|
2263
2529
|
let defaultValue = "\"\"";
|
|
2264
2530
|
let inputValue = "e.target.value";
|
|
@@ -2306,7 +2572,7 @@ function bindEventName(node, name) {
|
|
|
2306
2572
|
* as one t_event that calls the binding write and the user handler in source
|
|
2307
2573
|
* order. Returns a map from binding attribute index to the combined handler.
|
|
2308
2574
|
*/
|
|
2309
|
-
function combinedEventHandlers(node
|
|
2575
|
+
function combinedEventHandlers(node) {
|
|
2310
2576
|
const bindings = /* @__PURE__ */ new Map();
|
|
2311
2577
|
const handlers = /* @__PURE__ */ new Map();
|
|
2312
2578
|
for (let [index, { name, value, fullyReactive }] of node.attributes.entries()) {
|
|
@@ -2321,7 +2587,7 @@ function combinedEventHandlers(node, status) {
|
|
|
2321
2587
|
const handler = node.attributes[handlerIndex];
|
|
2322
2588
|
combined.set(bindingIndex, {
|
|
2323
2589
|
index: handlerIndex,
|
|
2324
|
-
value:
|
|
2590
|
+
value: handler.value,
|
|
2325
2591
|
span: handler.span,
|
|
2326
2592
|
first: handlerIndex < bindingIndex
|
|
2327
2593
|
});
|
|
@@ -2336,10 +2602,11 @@ function combinedEventHandlers(node, status) {
|
|
|
2336
2602
|
function emitBindEvent(varName, eventName, listener, body, combined, status, b) {
|
|
2337
2603
|
status.imports.add("t_event");
|
|
2338
2604
|
if (!combined) {
|
|
2339
|
-
b.append(`t_event(${varName}, "${eventName}", ${listener});`);
|
|
2605
|
+
b.append(`t_event(${varName}, "${eventName}", ${injectForVarHandler(listener, status)});`);
|
|
2340
2606
|
return;
|
|
2341
2607
|
}
|
|
2342
2608
|
b.append(`t_event(${varName}, "${eventName}", (e) => {`);
|
|
2609
|
+
appendForVarBindings(b, status, combined.value);
|
|
2343
2610
|
const handlerCall = `((${combined.value}) as ((e: any) => any) | undefined)?.(e);`;
|
|
2344
2611
|
if (combined.first) {
|
|
2345
2612
|
addMappedText("", handlerCall, "", combined.span, status, b);
|
|
@@ -2351,13 +2618,13 @@ function emitBindEvent(varName, eventName, listener, body, combined, status, b)
|
|
|
2351
2618
|
b.append("});");
|
|
2352
2619
|
}
|
|
2353
2620
|
function buildEventAttribute(varName, name, value, span, status, b) {
|
|
2354
|
-
value =
|
|
2621
|
+
value = injectForVarHandler(value, status);
|
|
2355
2622
|
const eventName = name.substring(2);
|
|
2356
2623
|
status.imports.add("t_event");
|
|
2357
2624
|
addMappedText(`t_event(${varName}, "${eventName}", `, value, `);`, span, status, b);
|
|
2358
2625
|
}
|
|
2359
2626
|
function buildTransitionAttribute(node, varName, name, value, status, b) {
|
|
2360
|
-
value =
|
|
2627
|
+
value = injectForVarHandler(value, status);
|
|
2361
2628
|
status.imports.add("t_animate");
|
|
2362
2629
|
let entryVarName = nextVarName("trans_in", status);
|
|
2363
2630
|
let exitVarName = nextVarName("trans_out", status);
|
|
@@ -2368,7 +2635,7 @@ function buildTransitionAttribute(node, varName, name, value, status, b) {
|
|
|
2368
2635
|
} else if (name === "transition-in") {
|
|
2369
2636
|
let outAttribute = node.attributes.find((a) => a.name === "transition-out");
|
|
2370
2637
|
if (outAttribute && outAttribute.value && outAttribute.fullyReactive) {
|
|
2371
|
-
let outValue = outAttribute.value;
|
|
2638
|
+
let outValue = injectForVarHandler(outAttribute.value, status);
|
|
2372
2639
|
b.append(`const ${entryVarName} = (${value})(${varName});`);
|
|
2373
2640
|
b.append(`const ${exitVarName} = (${outValue})(${varName});`);
|
|
2374
2641
|
b.append(`t_animate(${varName}, ${entryVarName}, ${exitVarName});`);
|
|
@@ -2422,11 +2689,11 @@ function buildSlotNode(node, status, b) {
|
|
|
2422
2689
|
});
|
|
2423
2690
|
b.append(`const ${propsName} = $watch({`);
|
|
2424
2691
|
for (let p of props) {
|
|
2425
|
-
let value =
|
|
2692
|
+
let value = p.value;
|
|
2426
2693
|
addMappedText(`${p.name}: `, value, ",", p.span, status, b);
|
|
2427
2694
|
}
|
|
2428
2695
|
b.append("});");
|
|
2429
|
-
if (runs.length) buildRun("setProps",
|
|
2696
|
+
if (runs.length) buildRun("setProps", runs.join("\n"), status, b);
|
|
2430
2697
|
}
|
|
2431
2698
|
const slotParentName = node.parentName;
|
|
2432
2699
|
const slotAnchorName = node.varName;
|
|
@@ -2620,11 +2887,11 @@ const importsMap$1 = {
|
|
|
2620
2887
|
t_push_dev_bound: "import { t_push_dev_bound } from \"${folder}/dev\";",
|
|
2621
2888
|
t_pop_dev_bound: "import { t_pop_dev_bound } from \"${folder}/dev\";"
|
|
2622
2889
|
};
|
|
2623
|
-
function buildCode(template, map, options) {
|
|
2890
|
+
function buildCode(template, map, options, errors = []) {
|
|
2624
2891
|
let b = new Builder(options?.mapped);
|
|
2625
2892
|
let imports = /* @__PURE__ */ new Set();
|
|
2626
2893
|
imports.add("SlotRender");
|
|
2627
|
-
buildTemplate(template, map, imports, b, options);
|
|
2894
|
+
buildTemplate(template, map, imports, b, options, errors);
|
|
2628
2895
|
let startSize = b.toString().length;
|
|
2629
2896
|
if (imports.size) {
|
|
2630
2897
|
const folder = options?.renderFolder ?? "@torpor/view";
|
|
@@ -2641,7 +2908,7 @@ function buildCode(template, map, options) {
|
|
|
2641
2908
|
}
|
|
2642
2909
|
return b.toString();
|
|
2643
2910
|
}
|
|
2644
|
-
function buildTemplate(template, map, imports, b, options) {
|
|
2911
|
+
function buildTemplate(template, map, imports, b, options, errors = []) {
|
|
2645
2912
|
let script = template.script.map((s) => s.script).join("\n");
|
|
2646
2913
|
for (const component of template.components) {
|
|
2647
2914
|
if (component.markup) script += "\n" + collectMarkupExpressions(component.markup);
|
|
@@ -2666,10 +2933,10 @@ function buildTemplate(template, map, imports, b, options) {
|
|
|
2666
2933
|
if (/\$batch\b/.test(scriptCode)) imports.add("$batch");
|
|
2667
2934
|
let currentIndex = 0;
|
|
2668
2935
|
let current = template.components[0];
|
|
2669
|
-
let status = makeStatus(imports, map, current, options);
|
|
2936
|
+
let status = makeStatus(imports, map, current, options, errors);
|
|
2670
2937
|
let first = true;
|
|
2671
2938
|
for (let chunk of template.script) if (chunk.script === "/* @params */") {
|
|
2672
|
-
if (!first) status = makeStatus(imports, map, current, options);
|
|
2939
|
+
if (!first) status = makeStatus(imports, map, current, options, errors);
|
|
2673
2940
|
first = false;
|
|
2674
2941
|
b.append(`
|
|
2675
2942
|
${current.markup ? "$parent" : "_$parent"}: ParentNode,
|
|
@@ -2721,7 +2988,7 @@ function buildTemplate(template, map, imports, b, options) {
|
|
|
2721
2988
|
current = template.components[currentIndex];
|
|
2722
2989
|
} else addMappedText("", chunk.script, "", chunk.span, status, b);
|
|
2723
2990
|
}
|
|
2724
|
-
function makeStatus(imports, map, current, options) {
|
|
2991
|
+
function makeStatus(imports, map, current, options, errors = []) {
|
|
2725
2992
|
return {
|
|
2726
2993
|
imports,
|
|
2727
2994
|
props: current?.props ?? [],
|
|
@@ -2731,11 +2998,12 @@ function makeStatus(imports, map, current, options) {
|
|
|
2731
2998
|
map,
|
|
2732
2999
|
varNames: {},
|
|
2733
3000
|
fragmentStack: [],
|
|
2734
|
-
|
|
3001
|
+
forVars: [],
|
|
2735
3002
|
ns: false,
|
|
2736
3003
|
preserveWhitespace: false,
|
|
2737
3004
|
inHead: false,
|
|
2738
|
-
options: options ?? {}
|
|
3005
|
+
options: options ?? {},
|
|
3006
|
+
errors
|
|
2739
3007
|
};
|
|
2740
3008
|
}
|
|
2741
3009
|
//#endregion
|
|
@@ -3119,7 +3387,7 @@ function buildElementAttributes(node, status) {
|
|
|
3119
3387
|
value = `t_attr(${value})`;
|
|
3120
3388
|
defaultValue = "\"\"";
|
|
3121
3389
|
}
|
|
3122
|
-
let valueOrDefault =
|
|
3390
|
+
let valueOrDefault = `(${value}) || ${defaultValue}`;
|
|
3123
3391
|
const propName = name.substring(1);
|
|
3124
3392
|
attributes.push(`${propName}="\${${valueOrDefault}}"`);
|
|
3125
3393
|
} else if (name === "class") {
|
|
@@ -3136,7 +3404,7 @@ function buildElementAttributes(node, status) {
|
|
|
3136
3404
|
attributes.push(`\${t_style(${value}) !== "" ? \`style="\${t_style(${value})}"\` : ""}`);
|
|
3137
3405
|
} else {
|
|
3138
3406
|
status.imports.add("t_attr");
|
|
3139
|
-
attributes.push(`\${${value} ? \`${name}="\${t_attr(${value})}"\` : ""}`);
|
|
3407
|
+
attributes.push(`\${(${value}) ? \`${name}="\${t_attr(${value})}"\` : ""}`);
|
|
3140
3408
|
}
|
|
3141
3409
|
} else if (value != null && reactive) {
|
|
3142
3410
|
status.imports.add("t_attr");
|
|
@@ -3547,13 +3815,15 @@ function build(template, options) {
|
|
|
3547
3815
|
if (component.head) trimWhitespace(component.head);
|
|
3548
3816
|
}
|
|
3549
3817
|
let map = [];
|
|
3818
|
+
let errors = [];
|
|
3550
3819
|
return {
|
|
3551
|
-
code: options?.server ? buildServerCode(template, options) : buildCode(template, map, options),
|
|
3820
|
+
code: options?.server ? buildServerCode(template, options) : buildCode(template, map, options, errors),
|
|
3552
3821
|
styles: template.components.map((c) => c.style ? {
|
|
3553
3822
|
style: buildStyles(c.style, c.style.hash),
|
|
3554
3823
|
hash: c.style.hash
|
|
3555
3824
|
} : void 0).filter((c) => c !== void 0),
|
|
3556
|
-
map
|
|
3825
|
+
map,
|
|
3826
|
+
errors
|
|
3557
3827
|
};
|
|
3558
3828
|
}
|
|
3559
3829
|
//#endregion
|
|
@@ -4594,11 +4864,6 @@ function parseStyleAttribute(status) {
|
|
|
4594
4864
|
};
|
|
4595
4865
|
}
|
|
4596
4866
|
//#endregion
|
|
4597
|
-
//#region src/compile/utils/isComponentNode.ts
|
|
4598
|
-
function isComponentNode(node) {
|
|
4599
|
-
return node.type === "component";
|
|
4600
|
-
}
|
|
4601
|
-
//#endregion
|
|
4602
4867
|
//#region src/compile/parse/scopeStyles.ts
|
|
4603
4868
|
function scopeStyles(status) {
|
|
4604
4869
|
let selectors = [];
|