@sigil-dev/compiler 0.9.24 → 0.9.25

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@sigil-dev/compiler",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.9.24",
5
+ "version": "0.9.25",
6
6
  "private": false,
7
7
  "description": "Compiles Sigil's JSX and $state/$derived macros into direct DOM updates",
8
8
  "license": "MIT",
@@ -13,7 +13,8 @@ export type ProcessElementFn = (
13
13
 
14
14
  /**
15
15
  * Detect pattern: signal.map(item => <Tag key={expr} />) — or, since unkeyed
16
- * maps are also reconciled (index-keyed), signal.map(item => <Tag />).
16
+ * maps are also reconciled (index-keyed), signal.map(item => <Tag />). The
17
+ * callback may take the index too: signal.map((item, i) => <Tag />).
17
18
  * Returns info needed to generate keyed list code, or null.
18
19
  * `hasKey` is false for the unkeyed form; buildKeyedList then keys by index.
19
20
  */
@@ -24,6 +25,8 @@ export function findKeyedMapExpr(
24
25
  callExpr: t.CallExpression;
25
26
  keyExpr: t.Expression | null;
26
27
  itemParam: t.Identifier;
28
+ /** `i` in `(item, i) => …`, or null when the callback takes only the item. */
29
+ indexParam: t.Identifier | null;
27
30
  jsxBody: t.JSXElement;
28
31
  signalExpr: t.Expression;
29
32
  hasKey: boolean;
@@ -41,9 +44,12 @@ export function findKeyedMapExpr(
41
44
  if (expr.arguments.length !== 1) return null;
42
45
  const arrowFn = expr.arguments[0];
43
46
  if (!t.isArrowFunctionExpression(arrowFn)) return null;
44
- if (arrowFn.params.length !== 1) return null;
47
+ if (arrowFn.params.length < 1 || arrowFn.params.length > 2) return null;
45
48
  const itemParam = arrowFn.params[0];
46
49
  if (!t.isIdentifier(itemParam)) return null;
50
+ const second = arrowFn.params[1];
51
+ if (second !== undefined && !t.isIdentifier(second)) return null;
52
+ const indexParam = second ?? null;
47
53
 
48
54
  const body = arrowFn.body;
49
55
  if (!t.isJSXElement(body)) return null;
@@ -61,6 +67,7 @@ export function findKeyedMapExpr(
61
67
  callExpr: expr,
62
68
  keyExpr: keyAttr.value.expression as t.Expression,
63
69
  itemParam,
70
+ indexParam,
64
71
  jsxBody: body,
65
72
  signalExpr: obj,
66
73
  hasKey: true,
@@ -72,6 +79,7 @@ export function findKeyedMapExpr(
72
79
  callExpr: expr,
73
80
  keyExpr: null,
74
81
  itemParam,
82
+ indexParam,
75
83
  jsxBody: body,
76
84
  signalExpr: obj,
77
85
  hasKey: false,
@@ -102,10 +110,20 @@ export function buildKeyedList(
102
110
  // (_item, _i) => _i works.
103
111
  const keyFn = t.arrowFunctionExpression(
104
112
  keyed.hasKey
105
- ? [keyed.itemParam]
113
+ ? keyed.indexParam
114
+ ? [keyed.itemParam, keyed.indexParam]
115
+ : [keyed.itemParam]
106
116
  : [t.identifier("_item"), t.identifier("_i")],
107
117
  keyed.keyExpr ?? t.identifier("_i"),
108
118
  );
119
+ // The row builder gets the index when the callback asked for it, and
120
+ // reconcile then rebuilds rows that move (their body baked the old index).
121
+ const rowParams = keyed.indexParam
122
+ ? [keyed.itemParam, keyed.indexParam]
123
+ : [keyed.itemParam];
124
+ const usesIndex: t.Expression[] = keyed.indexParam
125
+ ? [t.booleanLiteral(true)]
126
+ : [];
109
127
 
110
128
  // Inside a row, the item is an element of a $state array, which makes it a
111
129
  // reactive proxy: `item.done` must update the row when it changes, and
@@ -160,7 +178,7 @@ export function buildKeyedList(
160
178
  keyed.signalExpr,
161
179
  keyFn,
162
180
  t.arrowFunctionExpression(
163
- [keyed.itemParam],
181
+ rowParams,
164
182
  t.blockStatement([
165
183
  ...createNodeStmts,
166
184
  t.returnStatement(t.identifier(jsxVar)),
@@ -168,6 +186,8 @@ export function buildKeyedList(
168
186
  ),
169
187
  ];
170
188
  if (hydrate && parentVar) hydrateArgs.push(t.identifier(parentVar));
189
+ else if (usesIndex.length) hydrateArgs.push(t.identifier("undefined"));
190
+ hydrateArgs.push(...usesIndex);
171
191
  statements.push(
172
192
  t.variableDeclaration("const", [
173
193
  t.variableDeclarator(
@@ -211,12 +231,13 @@ export function buildKeyedList(
211
231
  keyed.signalExpr,
212
232
  keyFn,
213
233
  t.arrowFunctionExpression(
214
- [keyed.itemParam],
234
+ rowParams,
215
235
  t.blockStatement([
216
236
  ...createNodeStmts,
217
237
  t.returnStatement(t.identifier(jsxVar)),
218
238
  ]),
219
239
  ),
240
+ ...usesIndex,
220
241
  ]),
221
242
  ),
222
243
  ]),
@@ -301,12 +322,13 @@ export function buildKeyedList(
301
322
  keyed.signalExpr,
302
323
  keyFn,
303
324
  t.arrowFunctionExpression(
304
- [keyed.itemParam],
325
+ rowParams,
305
326
  t.blockStatement([
306
327
  ...createNodeStmts,
307
328
  t.returnStatement(t.identifier(jsxVar)),
308
329
  ]),
309
330
  ),
331
+ ...usesIndex,
310
332
  ]),
311
333
  ),
312
334
  ]),
@@ -825,3 +825,58 @@ describe("conditional fragments", () => {
825
825
  }
826
826
  });
827
827
  });
828
+
829
+ describe("map callbacks that take the index", () => {
830
+ const plain = (html: string) =>
831
+ noMarkers(html).replace(/ data-key="[^"]*"/g, "");
832
+ const g = globalThis as unknown as Record<string, () => void>;
833
+
834
+ function hydrated(src: string) {
835
+ const ssr = String(compile(src, "ssr")());
836
+ const host = document.createElement("div");
837
+ host.innerHTML = ssr;
838
+ runtime.pushHydrationNodes(Array.from(host.childNodes));
839
+ compile(src, "hydrate")();
840
+ runtime.popHydrationNodes();
841
+ return host;
842
+ }
843
+
844
+ test("(item, i) hydrates, and a fresh render agrees", () => {
845
+ const r = render(`let items = $state([{ id: 7, t: "a" }, { id: 9, t: "b" }]);
846
+ export const App = () => <ol>{items.map((it, i) => <li key={it.id}>{i + 1}. {it.t}</li>)}</ol>;`);
847
+ const html = "<ol><li>1. a</li><li>2. b</li></ol>";
848
+ expect(plain(r.ssr)).toBe(html);
849
+ expect(plain(r.hydrated.innerHTML)).toBe(html);
850
+ expect(plain(r.spa.innerHTML)).toBe(html);
851
+ });
852
+
853
+ // The shape that blanked a real page: an index map nested in keyed rows,
854
+ // inside a table, with a list in each cell.
855
+ test("an index map inside keyed table rows hydrates in place", () => {
856
+ const src = `let rows = $state([
857
+ { day: 1, cells: [{ who: "Ana", jobs: ["x"] }, { who: "Bo", jobs: [] }] },
858
+ { day: 2, cells: [{ who: "Ana", jobs: [] }, { who: "Bo", jobs: ["y", "z"] }] },
859
+ ]);
860
+ export const App = () => (
861
+ <table><tbody>{rows.map((r) => (
862
+ <tr key={r.day}><th>{r.day}</th>{r.cells.map((c, i) => (
863
+ <td key={i}>{c.who}:{c.jobs.map((j) => <b key={j}>{j}</b>)}</td>
864
+ ))}</tr>
865
+ ))}</tbody></table>
866
+ );`;
867
+ const host = hydrated(src);
868
+ expect(plain(host.innerHTML)).toBe(
869
+ "<table><tbody><tr><th>1</th><td>Ana:<b>x</b></td><td>Bo:</td></tr>" +
870
+ "<tr><th>2</th><td>Ana:</td><td>Bo:<b>y</b><b>z</b></td></tr></tbody></table>",
871
+ );
872
+ });
873
+
874
+ test("rows renumber when an item before them goes away", async () => {
875
+ const host = hydrated(`let items = $state([{ id: 1, t: "a" }, { id: 2, t: "b" }, { id: 3, t: "c" }]);
876
+ globalThis.__dropFirst = () => items.shift();
877
+ export const App = () => <ol>{items.map((it, i) => <li key={it.id}>{i + 1}. {it.t}</li>)}</ol>;`);
878
+ g.__dropFirst();
879
+ await runtime.tick();
880
+ expect(plain(host.innerHTML)).toBe("<ol><li>1. b</li><li>2. c</li></ol>");
881
+ });
882
+ });