@torpor/view 1.4.0 → 1.4.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.mjs CHANGED
@@ -1084,6 +1084,7 @@ function forVarBindingsFor(status, names) {
1084
1084
  return collectBindings$1(status, (forVar) => names.has(forVar.name));
1085
1085
  }
1086
1086
  function collectBindings$1(status, include) {
1087
+ if (status.options.check === true) return "";
1087
1088
  const bound = /* @__PURE__ */ new Set();
1088
1089
  let result = "";
1089
1090
  for (let i = status.forVars.length - 1; i >= 0; i--) {
@@ -1201,7 +1202,8 @@ function buildAddFragment(node, status, b, parentName, anchorName) {
1201
1202
  const fragment = node.fragment;
1202
1203
  const fragmentName = `t_fragment_${fragment.number}`;
1203
1204
  if (fragment.effects.length > 0) {
1204
- status.imports.add("$run");
1205
+ const check = status.options.check === true;
1206
+ if (!check) status.imports.add("$run");
1205
1207
  let forVarMask;
1206
1208
  for (let effect of fragment.effects) if (effect.forVarMask !== void 0) {
1207
1209
  if (forVarMask === void 0) forVarMask = 0;
@@ -1209,11 +1211,11 @@ function buildAddFragment(node, status, b, parentName, anchorName) {
1209
1211
  }
1210
1212
  let trailing = "";
1211
1213
  if (forVarMask !== void 0) trailing = `, undefined, { forVarMask: ${forVarMask} }`;
1212
- b.append("$run(() => {");
1214
+ b.append(check ? "(() => {" : "$run(() => {");
1213
1215
  appendForVarBindings(b, status, fragment.effects.map((e) => e.functionBody).join("\n"));
1214
1216
  if (status.options.mapped === true) for (let effect of fragment.effects) addMappedTextWithOffsets("", effect.functionBody, "", effect.spans, effect.offsets, effect.lengths, status, b);
1215
1217
  else b.append(fragment.effects.map((e) => e.functionBody).join("\n"));
1216
- b.append(`}${status.options.dev === true ? `, "setAttributes"${trailing}` : trailing});`);
1218
+ b.append(check ? "})();" : `}${status.options.dev === true ? `, "setAttributes"${trailing}` : trailing});`);
1217
1219
  }
1218
1220
  if (fragment.singleRootElement) {
1219
1221
  status.imports.add("t_add_element");
@@ -1676,10 +1678,16 @@ function buildRun(functionName, functionBody, status, b) {
1676
1678
  trailing = `, "${functionName}"`;
1677
1679
  if (forVarMask !== void 0) trailing += `, { forVarMask: ${forVarMask} }`;
1678
1680
  } else if (forVarMask !== void 0) trailing = `, undefined, { forVarMask: ${forVarMask} }`;
1679
- status.imports.add("$run");
1680
- b.append("$run(() => {");
1681
- b.append(functionBody);
1682
- b.append(`}${trailing});`);
1681
+ if (status.options.check === true) {
1682
+ b.append("(() => {");
1683
+ b.append(functionBody);
1684
+ b.append("})();");
1685
+ } else {
1686
+ status.imports.add("$run");
1687
+ b.append("$run(() => {");
1688
+ b.append(functionBody);
1689
+ b.append(`}${trailing});`);
1690
+ }
1683
1691
  addPopDevBoundary(status, b);
1684
1692
  }
1685
1693
  //#endregion
@@ -1861,7 +1869,8 @@ function buildComponentNode(node, status, b, root = false) {
1861
1869
  slot.hasSlotProps ? "$slot: Record<PropertyKey, any>" : "_$slot?: Record<PropertyKey, any>",
1862
1870
  fillUsesContext(slot) ? "$context?: Record<PropertyKey, any>" : "_$context?: Record<PropertyKey, any>"
1863
1871
  ];
1864
- b.append(`${slot.hasSlotProps ? "// @ts-ignore\n" : ""}${slotsName}["${slotName}"] = (\n${slotParams.join(",\n")}\n) => {`);
1872
+ const check = status.options.check === true;
1873
+ b.append(check ? `((\n${slotParams.join(",\n")}\n) => {` : `${slot.hasSlotProps ? "// @ts-ignore\n" : ""}${slotsName}["${slotName}"] = (\n${slotParams.join(",\n")}\n) => {`);
1865
1874
  buildFragment(slot, status, b, "$sparent", "$sanchor");
1866
1875
  status.fragmentStack.push({
1867
1876
  fragment: slot.fragment,
@@ -1870,7 +1879,7 @@ function buildComponentNode(node, status, b, root = false) {
1870
1879
  for (let child of slot.children) buildNode(child, status, b, "$sparent", "$sanchor");
1871
1880
  status.fragmentStack.pop();
1872
1881
  buildAddFragment(slot, status, b, "$sparent", "$sanchor");
1873
- b.append(`}`);
1882
+ b.append(check ? "})(null!, null, {}, undefined);" : `}`);
1874
1883
  }
1875
1884
  }
1876
1885
  let renderParams = `${node.parentName}, ${node.varName}, ${propsName}, $context`;
@@ -1884,6 +1893,49 @@ function buildComponentNode(node, status, b, root = false) {
1884
1893
  b.append("");
1885
1894
  }
1886
1895
  //#endregion
1896
+ //#region src/compile/build/client/checkCallbacks.ts
1897
+ /**
1898
+ * Helpers for the callbacks that control blocks (`@if`, `@switch`, `@await`,
1899
+ * …) pass to their runtime functions. A check build (`BuildOptions.check`)
1900
+ * calls each callback in place instead, as an immediately-invoked function,
1901
+ * because TypeScript carries narrowing into those but not into callbacks
1902
+ * passed elsewhere: `@if ($props.user) { {$props.user.name} }` then type
1903
+ * checks as it reads.
1904
+ */
1905
+ /** The callback's parameters: the insertion point, except in the head. */
1906
+ function controlParams(status) {
1907
+ return status.inHead ? "" : "t_before";
1908
+ }
1909
+ /** The arguments a check build calls a callback with, matching its parameters. */
1910
+ function controlArgs(params) {
1911
+ return params.split(",").map((p) => p.trim()).filter((p) => p !== "").map((p) => p === "t_before" ? "null as Node | null" : "undefined as any").join(", ");
1912
+ }
1913
+ /**
1914
+ * Opens a control block's callback.
1915
+ *
1916
+ * @param call The runtime call up to the callback, e.g. `t_run_control(region, anchor, `
1917
+ * @param params The callback's parameters, by default `t_before` (or none in the head)
1918
+ */
1919
+ function openControl(call, status, params = controlParams(status)) {
1920
+ return status.options.check === true ? `((${params}) => {` : `${call}(${params}) => {`;
1921
+ }
1922
+ /**
1923
+ * Closes a control block's callback and opens the next one, for runtime
1924
+ * calls that take several (e.g. `@await`'s content and `@with` branches).
1925
+ */
1926
+ function nextControl(status, params = controlParams(status), previousParams = controlParams(status)) {
1927
+ return status.options.check === true ? `})(${controlArgs(previousParams)});\n((${params}) => {` : `}, (${params}) => {`;
1928
+ }
1929
+ /**
1930
+ * Closes a control block's last callback, and the runtime call.
1931
+ *
1932
+ * @param trailing The runtime call's remaining arguments, e.g. `, "runIf"`
1933
+ * @param params The callback's parameters, to call it with in a check build
1934
+ */
1935
+ function closeControl(trailing, status, params = controlParams(status)) {
1936
+ return status.options.check === true ? `})(${controlArgs(params)});` : `}${trailing});`;
1937
+ }
1938
+ //#endregion
1887
1939
  //#region src/compile/build/client/buildAwaitNode.ts
1888
1940
  function buildAwaitNode(node, status, b) {
1889
1941
  const anchorName = node.varName ?? "null";
@@ -1899,7 +1951,7 @@ function buildAwaitNode(node, status, b) {
1899
1951
  addPushDevBoundary("control", "@await", status, b);
1900
1952
  b.append(`
1901
1953
  const ${regionName} = t_region(${status.options.dev === true ? `"await"` : ""});
1902
- t_run_await(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
1954
+ ${openControl(`t_run_await(${regionName}, ${anchorName}, `, status)}`);
1903
1955
  appendForVarBindings(b, status);
1904
1956
  if (awaitBranch !== void 0 && awaitBranch.children.length > 0) {
1905
1957
  buildFragment(awaitBranch, status, b, parentName, "t_before");
@@ -1912,7 +1964,7 @@ function buildAwaitNode(node, status, b) {
1912
1964
  buildAddFragment(awaitBranch, status, b, parentName, "t_before");
1913
1965
  }
1914
1966
  if (withBranch !== void 0) {
1915
- b.append(`}, (${status.inHead ? "" : "t_before"}) => {`);
1967
+ b.append(nextControl(status));
1916
1968
  appendForVarBindings(b, status);
1917
1969
  if (withBranch.children.length > 0) {
1918
1970
  buildFragment(withBranch, status, b, parentName, "t_before");
@@ -1924,8 +1976,8 @@ function buildAwaitNode(node, status, b) {
1924
1976
  status.fragmentStack.pop();
1925
1977
  buildAddFragment(withBranch, status, b, parentName, "t_before");
1926
1978
  }
1927
- b.append(`}${status.options.dev === true ? `, "runAwait"` : ""});`);
1928
- } else b.append(`}, null${status.options.dev === true ? `, "runAwait"` : ""});`);
1979
+ b.append(closeControl(status.options.dev === true ? `, "runAwait"` : "", status));
1980
+ } else b.append(closeControl(`, null${status.options.dev === true ? `, "runAwait"` : ""}`, status));
1929
1981
  addPopDevBoundary(status, b);
1930
1982
  b.append("");
1931
1983
  }
@@ -2422,6 +2474,10 @@ function buildForNode(node, status, b) {
2422
2474
  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);
2423
2475
  const singleVar = noWatch && forVarNames.length === 1;
2424
2476
  const leafRow = isForBodyLeafSafe(node.children);
2477
+ if (status.options.check === true) {
2478
+ buildCheckForNode(node, status, b, parentName, forVarNames, keyStatement, key, itemName, beforeName, leafRow);
2479
+ return;
2480
+ }
2425
2481
  status.imports.add("t_region");
2426
2482
  status.imports.add("t_run_list");
2427
2483
  status.imports.add("ListItemSpec");
@@ -2481,6 +2537,32 @@ ${status.options.dev === true ? "function updateListItem(t_old_item, t_new_item)
2481
2537
  addPopDevBoundary(status, b);
2482
2538
  b.append("");
2483
2539
  }
2540
+ /**
2541
+ * Builds a `@for` for type checking only (see `BuildOptions.check`): the loop
2542
+ * runs in place, with each row's body called inside it, so loop variables
2543
+ * keep the types of the iterable's items and narrowing from around the loop
2544
+ * carries into it.
2545
+ */
2546
+ function buildCheckForNode(node, status, b, parentName, forVarNames, keyStatement, key, itemName, beforeName, leafRow) {
2547
+ b.append("");
2548
+ b.append("/* @for */");
2549
+ b.append("(() => {");
2550
+ addMappedText("", `${node.statement}`, " {", node.span, status, b);
2551
+ if (key !== void 0 && keyStatement) addMappedText("void (", keyStatement, ");", key.span, status, b);
2552
+ b.append(`((${itemName}: any, ${beforeName}: Node | null) => {`);
2553
+ const oldForVars = status.forVars;
2554
+ status.forVars = [...oldForVars, ...forVarNames.map((v) => ({
2555
+ name: v,
2556
+ path: `${itemName}.data.${v}`,
2557
+ shadow: true
2558
+ }))];
2559
+ buildForItem(node, status, b, parentName, beforeName, itemName, leafRow);
2560
+ status.forVars = oldForVars;
2561
+ b.append("})(undefined, null);");
2562
+ b.append("}");
2563
+ b.append("})();");
2564
+ b.append("");
2565
+ }
2484
2566
  function buildForItem(node, status, b, parentName, beforeName, itemName, leafRow) {
2485
2567
  const oldRegionName = nextVarName("old_region", status);
2486
2568
  if (!leafRow) {
@@ -2509,6 +2591,13 @@ function buildHtmlNode(node, status, b) {
2509
2591
  const htmlAnchorName = node.varName;
2510
2592
  const htmlParentName = node.parentName || htmlAnchorName + ".parentNode";
2511
2593
  node = node.children[0];
2594
+ if (status.options.check === true) {
2595
+ b.append("");
2596
+ b.append(`/* @html */`);
2597
+ addMappedText("", node.statement, ";", node.span, status, b);
2598
+ b.append("");
2599
+ return;
2600
+ }
2512
2601
  status.imports.add("t_region");
2513
2602
  status.imports.add("t_run_control");
2514
2603
  const firstNodeVar = nextVarName("html_first", status);
@@ -2588,11 +2677,11 @@ function buildIfNode(node, status, b) {
2588
2677
  b.append(`
2589
2678
  const ${regionName} = t_region(${status.options.dev === true ? `"if"` : ""});
2590
2679
  let ${indexName} = -1;
2591
- t_run_control(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
2680
+ ${openControl(`t_run_control(${regionName}, ${anchorName}, `, status)}`);
2592
2681
  appendForVarBindings(b, status);
2593
2682
  let index = 0;
2594
2683
  for (let branch of branches) buildIfBranch(branch, status, b, parentName, regionName, indexName, index++);
2595
- b.append(`}${status.options.dev === true ? `, "runIf"` : ""});`);
2684
+ b.append(closeControl(status.options.dev === true ? `, "runIf"` : "", status));
2596
2685
  addPopDevBoundary(status, b);
2597
2686
  b.append("");
2598
2687
  }
@@ -2629,10 +2718,10 @@ function buildReplaceNode(node, status, b) {
2629
2718
  b.append(`
2630
2719
  /* @replace */
2631
2720
  const ${replaceRegionName} = t_region(${status.options.dev === true ? `"replace"` : ""});
2632
- t_run_control(${replaceRegionName}, ${replaceAnchorName}, (t_before) => {`);
2721
+ ${openControl(`t_run_control(${replaceRegionName}, ${replaceAnchorName}, `, status, "t_before")}`);
2633
2722
  appendForVarBindings(b, status);
2634
2723
  buildReplaceBranch(node, status, b, replaceParentName, replaceRegionName);
2635
- b.append(`}${status.options.dev === true ? ", \"replace\"" : ""});`);
2724
+ b.append(closeControl(status.options.dev === true ? ", \"replace\"" : "", status, "t_before"));
2636
2725
  b.append("");
2637
2726
  }
2638
2727
  function buildReplaceBranch(node, status, b, parentName, regionName) {
@@ -2689,14 +2778,13 @@ function buildSwitchNode(node, status, b) {
2689
2778
  b.append(`
2690
2779
  const ${regionName} = t_region(${status.options.dev === true ? `"switch"` : ""});
2691
2780
  let ${indexName} = -1;
2692
- t_run_control(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
2781
+ ${openControl(`t_run_control(${regionName}, ${anchorName}, `, status)}`);
2693
2782
  appendForVarBindings(b, status);
2694
2783
  addMappedText("", `${node.statement}`, " {", node.span, status, b);
2695
2784
  let index = 0;
2696
2785
  for (let branch of branches) buildSwitchBranch(branch, status, b, parentName, regionName, indexName, index++);
2697
- b.append(`
2698
- }
2699
- }${status.options.dev === true ? `, "runSwitch"` : ""});`);
2786
+ b.append("}");
2787
+ b.append(closeControl(status.options.dev === true ? `, "runSwitch"` : "", status));
2700
2788
  addPopDevBoundary(status, b);
2701
2789
  b.append("");
2702
2790
  }
@@ -2754,16 +2842,20 @@ function buildTryNode(node, status, b) {
2754
2842
  addPushDevBoundary("control", "@try", status, b);
2755
2843
  b.append(`
2756
2844
  const ${regionName} = t_region(${status.options.dev === true ? `"try"` : ""});
2757
- t_run_try(${regionName}, ${anchorName}, (${status.inHead ? "" : "t_before"}) => {`);
2845
+ ${openControl(`t_run_try(${regionName}, ${anchorName}, `, status)}`);
2758
2846
  appendForVarBindings(b, status);
2759
2847
  buildTryBranchBody(tryBranch, status, b, parentName);
2760
2848
  const catchVar = catchBranch ? trimMatched(catchBranch.statement.substring(5).trim(), "(", ")") || "err" : null;
2761
2849
  if (catchBranch && catchVar) {
2762
- addMappedText("},", `(${status.inHead ? "" : "t_before"}, ${catchVar}) => {`, "", catchBranch.span, status, b);
2850
+ const catchParams = `${status.inHead ? "" : "t_before"}, ${catchVar}`;
2851
+ if (status.options.check === true) {
2852
+ b.append(closeControl("", status));
2853
+ addMappedText("", `((${catchParams}) => {`, "", catchBranch.span, status, b);
2854
+ } else addMappedText("},", `(${catchParams}) => {`, "", catchBranch.span, status, b);
2763
2855
  appendForVarBindings(b, status);
2764
2856
  buildTryBranchBody(catchBranch, status, b, parentName);
2765
- b.append(`}${status.options.dev === true ? `, "runTry"` : ""});`);
2766
- } else b.append(`}, null${status.options.dev === true ? `, "runTry"` : ""});`);
2857
+ b.append(closeControl(status.options.dev === true ? `, "runTry"` : "", status, catchParams));
2858
+ } else b.append(closeControl(`, null${status.options.dev === true ? `, "runTry"` : ""}`, status));
2767
2859
  addPopDevBoundary(status, b);
2768
2860
  b.append("");
2769
2861
  }
@@ -2931,10 +3023,13 @@ function buildDynamicElementNode(node, status, b) {
2931
3023
  status.imports.add("t_dynamic");
2932
3024
  let selfValue = selfAttribute.value;
2933
3025
  if (selfAttribute.fullyReactive) {
2934
- status.imports.add("$run");
2935
- b.append("$run(() => {");
2936
- b.append(`${node.varName} = t_dynamic(${node.varName}, ${selfValue});`);
2937
- b.append(`}${status.options.dev === true ? `, "setDynamic"` : ""});`);
3026
+ if (status.options.check === true) b.append(`${node.varName} = t_dynamic(${node.varName}, ${selfValue});`);
3027
+ else {
3028
+ status.imports.add("$run");
3029
+ b.append("$run(() => {");
3030
+ b.append(`${node.varName} = t_dynamic(${node.varName}, ${selfValue});`);
3031
+ b.append(`}${status.options.dev === true ? `, "setDynamic"` : ""});`);
3032
+ }
2938
3033
  } else b.append(`${node.varName} = t_dynamic(${node.varName}, ${selfValue});`);
2939
3034
  }
2940
3035
  buildElementAttributes$1(node, node.varName, status, b);
@@ -3517,7 +3612,7 @@ function buildTemplate(template, map, imports, b, options, errors = [], warnings
3517
3612
  status.imports.add("t_region");
3518
3613
  status.imports.add("t_run_try");
3519
3614
  b.append(`const t_error_region = t_region();`);
3520
- b.append(`t_run_try(t_error_region, $anchor, () => {`);
3615
+ b.append(status.options.check === true ? "(() => {" : `t_run_try(t_error_region, $anchor, () => {`);
3521
3616
  }
3522
3617
  buildFragmentText(current.markup, status, b);
3523
3618
  b.append("");
@@ -3525,13 +3620,13 @@ function buildTemplate(template, map, imports, b, options, errors = [], warnings
3525
3620
  }
3526
3621
  } else if (chunk.script === "/* @error */") {
3527
3622
  if (current.markup && current.error) {
3528
- b.append(`}, (_, ${current.errorVar ?? "err"}) => {`);
3623
+ b.append(status.options.check === true ? `})();\n((_: Node | null, ${current.errorVar ?? "err"}: any) => {` : `}, (_, ${current.errorVar ?? "err"}) => {`);
3529
3624
  b.append("");
3530
3625
  b.append("/* User interface error */");
3531
3626
  buildFragmentText(current.error, status, b);
3532
3627
  b.append("");
3533
3628
  buildNode(current.error, status, b, "$parent", "$anchor", true);
3534
- b.append(`}${status.options.dev === true ? ", \"runError\"" : ""});`);
3629
+ b.append(status.options.check === true ? "})(null, undefined);" : `}${status.options.dev === true ? ", \"runError\"" : ""});`);
3535
3630
  }
3536
3631
  } else if (chunk.script === "/* @head */") {
3537
3632
  if (current.head) {