ferrovue 0.3.0 → 0.5.0

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.
Files changed (106) hide show
  1. package/README.md +225 -42
  2. package/dist/attrs.d.ts +3 -53
  3. package/dist/attrs.js +41 -469
  4. package/dist/calls.d.ts +4 -0
  5. package/dist/calls.js +221 -0
  6. package/dist/children.d.ts +16 -0
  7. package/dist/children.js +286 -0
  8. package/dist/classes.d.ts +13 -0
  9. package/dist/classes.js +176 -0
  10. package/dist/cli.js +31 -22
  11. package/dist/client.d.ts +35 -2
  12. package/dist/client.js +77 -14
  13. package/dist/compiler.d.ts +1 -1
  14. package/dist/compiler.js +31 -92
  15. package/dist/component.d.ts +3 -1
  16. package/dist/component.js +123 -104
  17. package/dist/constants.d.ts +42 -0
  18. package/dist/constants.js +292 -0
  19. package/dist/context.d.ts +29 -67
  20. package/dist/context.js +13 -38
  21. package/dist/diagnostics.d.ts +23 -0
  22. package/dist/diagnostics.js +30 -0
  23. package/dist/diff.d.ts +0 -3
  24. package/dist/diff.js +0 -9
  25. package/dist/emitter.d.ts +0 -8
  26. package/dist/emitter.js +0 -12
  27. package/dist/errors.d.ts +867 -0
  28. package/dist/errors.js +330 -0
  29. package/dist/expr.d.ts +1 -70
  30. package/dist/expr.js +142 -934
  31. package/dist/fallthrough.d.ts +9 -0
  32. package/dist/fallthrough.js +78 -0
  33. package/dist/fixture.d.ts +4 -10
  34. package/dist/fixture.js +38 -27
  35. package/dist/hydration.d.ts +15 -0
  36. package/dist/hydration.js +80 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.js +2 -0
  39. package/dist/islands.d.ts +1 -0
  40. package/dist/islands.js +1 -0
  41. package/dist/link-router.d.ts +24 -0
  42. package/dist/link-router.js +36 -0
  43. package/dist/lists.d.ts +0 -11
  44. package/dist/lists.js +34 -60
  45. package/dist/loops.d.ts +3 -0
  46. package/dist/loops.js +168 -0
  47. package/dist/model.d.ts +46 -144
  48. package/dist/model.js +150 -18
  49. package/dist/narrowing.d.ts +27 -0
  50. package/dist/narrowing.js +157 -0
  51. package/dist/numbers.d.ts +8 -0
  52. package/dist/numbers.js +73 -0
  53. package/dist/parens.d.ts +0 -27
  54. package/dist/parens.js +0 -44
  55. package/dist/plugin.d.ts +84 -0
  56. package/dist/plugin.js +26 -0
  57. package/dist/plugins/client-only.d.ts +8 -0
  58. package/dist/plugins/client-only.js +107 -0
  59. package/dist/plugins/i18n.d.ts +23 -0
  60. package/dist/{i18n.js → plugins/i18n.js} +88 -31
  61. package/dist/plugins/index.d.ts +2 -0
  62. package/dist/plugins/index.js +10 -0
  63. package/dist/plugins/router-link.d.ts +8 -0
  64. package/dist/{router.js → plugins/router-link.js} +52 -91
  65. package/dist/plugins/router.d.ts +42 -0
  66. package/dist/plugins/router.js +256 -0
  67. package/dist/plugins/scoped.d.ts +12 -0
  68. package/dist/plugins/scoped.js +125 -0
  69. package/dist/plugins/shared-types.d.ts +2 -0
  70. package/dist/plugins/shared-types.js +7 -0
  71. package/dist/plugins/stores.d.ts +37 -0
  72. package/dist/plugins/stores.js +379 -0
  73. package/dist/plugins/teleport.d.ts +5 -0
  74. package/dist/plugins/teleport.js +55 -0
  75. package/dist/plugins/twins.d.ts +10 -0
  76. package/dist/plugins/twins.js +164 -0
  77. package/dist/routes.d.ts +10 -0
  78. package/dist/routes.js +6 -0
  79. package/dist/rust.d.ts +1 -7
  80. package/dist/rust.js +56 -174
  81. package/dist/script.d.ts +3 -6
  82. package/dist/script.js +127 -140
  83. package/dist/settle.d.ts +6 -0
  84. package/dist/settle.js +34 -0
  85. package/dist/slots.d.ts +13 -0
  86. package/dist/slots.js +160 -0
  87. package/dist/strings.d.ts +13 -0
  88. package/dist/strings.js +208 -0
  89. package/dist/styles.d.ts +14 -0
  90. package/dist/styles.js +181 -0
  91. package/dist/template.d.ts +1 -42
  92. package/dist/template.js +25 -749
  93. package/dist/testing.d.ts +2 -0
  94. package/dist/testing.js +4 -10
  95. package/dist/types.d.ts +2 -0
  96. package/dist/typescript.d.ts +2 -14
  97. package/dist/typescript.js +53 -59
  98. package/dist/vite.d.ts +2 -1
  99. package/dist/vite.js +11 -32
  100. package/package.json +15 -6
  101. package/dist/i18n.d.ts +0 -27
  102. package/dist/router.d.ts +0 -17
  103. package/dist/scoped.d.ts +0 -24
  104. package/dist/scoped.js +0 -177
  105. package/dist/stores.d.ts +0 -6
  106. package/dist/stores.js +0 -229
package/dist/lists.js CHANGED
@@ -1,76 +1,57 @@
1
- /* Lists a template computes: array methods taking arrow functions (`filter`, `map`, `some`,
2
- * `every`, `find`, `findIndex`), `slice`, and `Object.keys` / `values` of a record.
3
- *
4
- * A computed list is an iterator of its items, each in one form whatever list it came from: a
5
- * string as a `Cow<str>` (borrowed from the props, or owned when `map` made it), a number or a
6
- * boolean copied, an object borrowed. That is what lets one method's result feed the next, a
7
- * `v-for`, `.join` or `.length`, without knowing where it came from. */
8
1
  import { BOOL, fail, FLOAT, INT, opt, sameTy, STR } from "./model.js";
9
2
  import { ctx } from "./context.js";
10
- import { asCow, yieldsCow, asF64, cond, expr, fieldVal, isNumber, isTemporary, lonely, meet } from "./expr.js";
11
- import { atom, operand, strArg, UNARY } from "./parens.js";
3
+ import { expr, fieldVal } from "./expr.js";
4
+ import { cond } from "./narrowing.js";
5
+ import { asF64, isNumber } from "./numbers.js";
6
+ import { asCow, isTemporary, lonely, meet, yieldsCow } from "./strings.js";
7
+ import { atom, binary, operand, strArg, UNARY } from "./parens.js";
12
8
  const COW = "std::borrow::Cow::<str>";
13
- /** The type of a list's items, which must be one these methods can pass along. */
14
9
  function itemsTy(s, v, n) {
15
10
  if (v.ty.k !== "list")
16
- return fail(s.comp, "not a list", n);
11
+ return fail(s.comp, "FV0803", "not a list", n);
17
12
  const of = v.ty.of;
18
13
  if (!["str", "int", "float", "bool", "struct", "child"].includes(of.k)) {
19
- fail(s.comp, "this method takes a list of strings, numbers, booleans or objects", n);
14
+ fail(s.comp, "FV0804", "this method takes a list of strings, numbers, booleans or objects", n);
20
15
  }
21
16
  return of;
22
17
  }
23
- /** A list as an iterator of its items, in the form a computed list holds them. */
24
18
  export function items(v) {
25
19
  if (v.iter !== undefined)
26
20
  return v.iter;
27
21
  const of = v.ty.k === "list" || v.ty.k === "record" ? v.ty.of : v.ty;
28
- // A list of strings holds `Cow`s, an array literal `&str`s: either way `&**v` is the `&str`.
29
22
  if (of.k === "str")
30
23
  return `${atom(v.code)}.iter().map(|v| ${COW}::Borrowed(&**v))`;
31
24
  if (of.k === "struct" || of.k === "child")
32
25
  return `${atom(v.code)}.iter()`;
33
26
  return `${atom(v.code)}.iter().copied()`;
34
27
  }
35
- /** A computed list: its iterator, and its items collected as `code` for anything that needs a
36
- * `Vec`. */
37
28
  export function computed(iter, of, lone) {
38
29
  return { code: `${iter}.collect::<Vec<_>>()`, ty: { k: "list", of }, iter, ...(lone ? { lone } : {}) };
39
30
  }
40
- /** A list of any kind as the `Vec` of items a computed list collects: what a branch of `?:` or a
41
- * setup `let` holds, so that both branches have one type. Kept beyond the statement, its strings
42
- * own their text, which may live in a temporary (`name.toUpperCase().split(" ")`). */
43
31
  export function collected(v) {
44
32
  const of = v.ty.k === "list" ? v.ty.of : v.ty;
45
33
  return `${items(v)}${of.k === "str" ? `.map(|v| ${COW}::Owned(v.into_owned()))` : ""}.collect::<Vec<_>>()`;
46
34
  }
47
- /** The computed list a setup `let` named `name` holds, read again by item. */
48
35
  export function heldList(name, of, lone) {
49
36
  const iter = of.k === "str" ? `${name}.iter().map(|v| ${COW}::Borrowed(&**v))` : `${name}.iter().copied()`;
50
37
  return { code: name, ty: { k: "list", of }, iter, ...(lone ? { lone } : {}) };
51
38
  }
52
- /** An item as the closure that a method hands it to sees it: `byRef` for `filter` and `find`, which
53
- * pass `&item`. */
54
39
  function itemVal(of, param, byRef, lone) {
55
- // An object's fields are read through the reference; a number or a boolean is copied out of it.
56
40
  const scalar = of.k === "int" || of.k === "float" || of.k === "bool";
57
41
  const code = of.k === "str" ? (byRef ? `&**${param}` : `&*${param}`) : byRef && scalar ? `*${param}` : param;
58
42
  return { code, ty: of, ...(lone ? { lone } : {}) };
59
43
  }
60
- /** An arrow function's body, translated with its parameters bound — the item, and the index when it
61
- * names one — as the closure a method takes. `enumerate` says the items must come numbered. */
62
44
  function arrow(s, fn, list, of, byRef, method, body) {
63
45
  const comp = s.comp;
64
46
  if (fn?.type !== "ArrowFunctionExpression" || fn.async || fn.params.length > 2) {
65
- fail(comp, `\`.${method}()\` takes an arrow function of the item, and of its index: \`x => …\`, \`(x, i) => …\``, fn);
47
+ fail(comp, "FV0805", `\`.${method}()\` takes an arrow function of the item, and of its index: \`x => …\`, \`(x, i) => …\``, fn);
66
48
  }
67
49
  if (fn.body.type === "BlockStatement")
68
- fail(comp, `\`.${method}()\` takes an arrow function whose body is an expression, without \`{ return … }\``, fn);
50
+ fail(comp, "FV0806", `\`.${method}()\` takes an arrow function whose body is an expression, without \`{ return … }\``, fn);
69
51
  const [item, index] = fn.params;
70
52
  if (index && index.type !== "Identifier")
71
- fail(comp, "an arrow function's index is a plain name", index);
53
+ fail(comp, "FV0807", "an arrow function's index is a plain name", index);
72
54
  const n = ++ctx.narrowCount;
73
- // A string item is named apart: `map` cannot hand back a borrow of it (see `mapped`).
74
55
  const p = of.k === "str" ? `fv_s${n}` : `fv_a${n}`;
75
56
  const i = `fv_i${n}`;
76
57
  const v = itemVal(of, p, byRef, list.lone);
@@ -81,22 +62,20 @@ function arrow(s, fn, list, of, byRef, method, body) {
81
62
  bound.push(item.name);
82
63
  }
83
64
  else if (item?.type === "ObjectPattern" && (of.k === "struct" || of.k === "child")) {
84
- // `({ id, label }) => …`: each name is that field of the item.
85
65
  for (const prop of item.properties) {
86
66
  if (prop.type !== "ObjectProperty" || prop.computed || prop.value.type !== "Identifier") {
87
- fail(comp, "a destructured arrow function parameter binds plain names, without defaults", prop);
67
+ fail(comp, "FV0808", "a destructured arrow function parameter binds plain names, without defaults", prop);
88
68
  }
89
69
  locals.set(prop.value.name, fieldVal(comp, v.code, of, prop.key.name ?? prop.key.value, prop));
90
70
  bound.push(prop.value.name);
91
71
  }
92
72
  }
93
73
  else if (item)
94
- fail(comp, "an arrow function's item is a name, or an object pattern of an object", item);
74
+ fail(comp, "FV0809", "an arrow function's item is a name, or an object pattern of an object", item);
95
75
  if (index) {
96
76
  locals.set(index.name, { code: i, ty: INT });
97
77
  bound.push(index.name);
98
78
  }
99
- // The parameters hide whatever an outer `v-if` narrowed under the same names.
100
79
  const narrowed = new Map([...s.narrowed].filter(([path]) => !bound.some((b) => path === b || path.startsWith(`${b}.`))));
101
80
  const code = body({ ...s, locals, narrowed });
102
81
  const reads = (name) => new RegExp(`\\b${name}\\b`).test(code);
@@ -112,15 +91,11 @@ function arrow(s, fn, list, of, byRef, method, body) {
112
91
  }
113
92
  return { closure: `|(${reads(i) ? i : "_"}, ${pName})| { ${lets}${code} }`, enumerate: true };
114
93
  }
115
- /** What `map` makes of each item: a string as a `Cow` — owned when it was built, or borrows the
116
- * item, which the closure owns — a number or a boolean as it is, an object borrowed. */
117
94
  function mapped(s, v, n) {
118
95
  switch (v.ty.k) {
119
96
  case "str":
120
- // A routine's `Cow` may borrow the item, which the closure owns: owned.
121
97
  if (v.code.startsWith("&*fv::") && yieldsCow(v.code.slice(2)))
122
98
  return `${COW}::Owned(${v.code.slice(2)}.into_owned())`;
123
- // An item itself, of this list or one outside it, which the closure only borrows: copied.
124
99
  const item = /^&\**(fv_s\d+)$/.exec(v.code)?.[1];
125
100
  if (item !== undefined)
126
101
  return `${COW}::Owned(${item}.to_string())`;
@@ -135,10 +110,9 @@ function mapped(s, v, n) {
135
110
  case "child":
136
111
  return `&${operand(v.code, UNARY)}`;
137
112
  default:
138
- return fail(s.comp, `\`.map()\` makes a list of strings, numbers, booleans or objects, not ${v.ty.k === "opt" ? "optional values" : "these"}`, n);
113
+ return fail(s.comp, "FV0810", `\`.map()\` makes a list of strings, numbers, booleans or objects, not ${v.ty.k === "opt" ? "optional values" : "these"}`, n);
139
114
  }
140
115
  }
141
- /** A list method other than `includes` and `join`: `null` when `method` is not one of them. */
142
116
  export function listMethod(s, target, method, args, n) {
143
117
  const comp = s.comp;
144
118
  if (!["filter", "map", "some", "every", "find", "findIndex", "slice"].includes(method))
@@ -147,13 +121,13 @@ export function listMethod(s, target, method, args, n) {
147
121
  const it = items(target);
148
122
  if (method === "slice") {
149
123
  if (args.length < 1 || args.length > 2)
150
- fail(comp, "`.slice()` takes a start, and an end", n);
124
+ fail(comp, "FV0811", "`.slice()` takes a start, and an end", n);
151
125
  const [start, end] = args.map((a) => expr(s, a));
152
126
  const index = (v) => {
153
127
  if (v === undefined || v.ty.k === "undef")
154
128
  return null;
155
129
  if (!isNumber(v.ty))
156
- fail(comp, "`.slice()` takes numbers", n);
130
+ fail(comp, "FV0812", "`.slice()` takes numbers", n);
157
131
  return asF64(v);
158
132
  };
159
133
  const from = index(start) ?? "0.0";
@@ -161,7 +135,7 @@ export function listMethod(s, target, method, args, n) {
161
135
  return computed(`fv::js_slice_items(${it}, ${from}, ${to === null ? "None" : `Some(${to})`})`, of, target.lone);
162
136
  }
163
137
  if (args.length !== 1)
164
- fail(comp, `\`.${method}()\` takes one arrow function`, n);
138
+ fail(comp, "FV0813", `\`.${method}()\` takes one arrow function`, n);
165
139
  const fn = args[0];
166
140
  switch (method) {
167
141
  case "filter": {
@@ -172,8 +146,6 @@ export function listMethod(s, target, method, args, n) {
172
146
  let out;
173
147
  const { closure, enumerate } = arrow(s, fn, target, of, false, method, (inner) => {
174
148
  out = expr(inner, fn.body);
175
- // An integer negated, multiplied or taken modulo may be JavaScript's -0, which a list of
176
- // `i64` loses: a list of fractions then. (Generated code spaces a binary `-`, never a unary one.)
177
149
  if (out.ty.k === "int" && out.f64 !== undefined && /-[\w(]|\s[*%]\s/.test(out.f64))
178
150
  out = { ...out, code: out.f64, ty: FLOAT };
179
151
  return mapped(inner, out, fn.body);
@@ -189,8 +161,6 @@ export function listMethod(s, target, method, args, n) {
189
161
  const { closure, enumerate } = arrow(s, fn, target, of, true, method, (inner) => cond(inner, fn.body));
190
162
  const found = enumerate ? `${it}.enumerate().find(${closure}).map(|(_, v)| v)` : `${it}.find(${closure})`;
191
163
  const lone = target.lone ? { lone: true } : {};
192
- // A string found is a `Cow` the statement holds, read as the `&str` an optional string is; one
193
- // kept beyond the statement owns its text, which may live in a temporary.
194
164
  if (of.k === "str")
195
165
  return { code: `${atom(found)}.as_deref()`, ty: opt(STR), held: `${found}.map(|v| ${COW}::Owned(v.into_owned()))`, ...lone };
196
166
  return { code: found, ty: opt(of), ...lone };
@@ -201,39 +171,44 @@ export function listMethod(s, target, method, args, n) {
201
171
  }
202
172
  }
203
173
  }
204
- /** `Object.keys(r)`, `Object.values(r)`, and `Object.entries(r)`, which is only a `v-for`'s source. */
205
174
  export function objectCall(s, method, args, n) {
206
175
  const comp = s.comp;
207
176
  const r = args.length === 1 ? expr(s, args[0]) : null;
208
177
  if (r?.ty.k !== "record")
209
- return fail(comp, `\`Object.${method}()\` takes a \`Record<string, T>\``, n);
178
+ return fail(comp, "FV0814", `\`Object.${method}()\` takes a \`Record<string, T>\``, n);
210
179
  const of = r.ty.of;
211
180
  if (method === "keys")
212
181
  return computed(`${atom(r.code)}.keys().map(${COW}::Borrowed)`, STR);
213
182
  if (method === "values") {
214
183
  if (of.k === "list")
215
- fail(comp, "`Object.values()` of a record of lists, which these methods cannot walk", n);
184
+ fail(comp, "FV0815", "`Object.values()` of a record of lists, which these methods cannot walk", n);
216
185
  const values = of.k === "str" ? `.values().map(|v| ${COW}::Borrowed(&**v))` : of.k === "struct" || of.k === "child" ? ".values()" : ".values().copied()";
217
186
  return computed(`${atom(r.code)}${values}`, of);
218
187
  }
219
188
  if (method === "entries")
220
- return fail(comp, "`Object.entries()` is supported as the source of a `v-for`: `([key, value], i) in Object.entries(r)`", n);
221
- return fail(comp, `\`Object.${method}()\` is not supported: \`keys\`, \`values\` and \`entries\` of a record are`, n);
189
+ return fail(comp, "FV0816", "`Object.entries()` is supported as the source of a `v-for`: `([key, value], i) in Object.entries(r)`", n);
190
+ return fail(comp, "FV0817", `\`Object.${method}()\` is not supported: \`keys\`, \`values\` and \`entries\` of a record are`, n);
222
191
  }
223
- /** `list.includes(x)` and `list.join(sep)` of a computed list, or of a list of fractions or
224
- * booleans. */
225
192
  export function computedListMethod(s, target, method, args, n) {
226
193
  if (target.ty.k !== "list")
227
194
  return null;
228
195
  const of = target.ty.of;
229
196
  if (method === "includes" && args.length === 1) {
230
197
  const x = expr(s, args[0]);
231
- if (!sameTy(x.ty, of) || !["str", "int", "float", "bool"].includes(of.k))
232
- fail(s.comp, "`.includes()` looks for a value of the list's own type", args[0]);
198
+ const numbers = isNumber(x.ty) && isNumber(of);
199
+ if ((!sameTy(x.ty, of) && !numbers) || !["str", "int", "float", "bool"].includes(of.k))
200
+ fail(s.comp, "FV0801", "`.includes()` looks for a value of the list's own type", args[0]);
233
201
  if (of.k === "str")
234
202
  meet(s.comp, target, x, "`.includes()`", n, "equal");
235
- // `includes` finds `NaN`, which `==` never equals.
236
- const test = of.k === "str" ? `*v == ${x.code.startsWith("&*") ? `*${x.code.slice(2)}` : `*${operand(x.code, UNARY)}`}` : of.k === "float" ? `v == ${x.code} || (v.is_nan() && ${atom(x.code)}.is_nan())` : `v == ${x.code}`;
203
+ const test = of.k === "str"
204
+ ? `*v == ${x.code.startsWith("&*") ? `*${x.code.slice(2)}` : `*${operand(x.code, UNARY)}`}`
205
+ : of.k === "float" && x.ty.k === "int"
206
+ ? binary("v", "==", asF64(x))
207
+ : of.k === "float"
208
+ ? `v == ${x.code} || (v.is_nan() && ${atom(x.code)}.is_nan())`
209
+ : of.k === "int" && x.ty.k === "float"
210
+ ? binary("v as f64", "==", x.code)
211
+ : `v == ${x.code}`;
237
212
  return { code: `${items(target)}.any(|v| ${test})`, ty: BOOL };
238
213
  }
239
214
  if (method === "join" && args.length <= 1) {
@@ -241,11 +216,10 @@ export function computedListMethod(s, target, method, args, n) {
241
216
  return null;
242
217
  const sep = args.length ? expr(s, args[0]) : { code: '","', ty: STR };
243
218
  if (sep.ty.k !== "str")
244
- fail(s.comp, "`.join()` takes a string", args[0]);
245
- // Two halves of a pair would meet where items are joined without a separator between them.
219
+ fail(s.comp, "FV0802", "`.join()` takes a string", args[0]);
246
220
  const parted = /^"[^"]/.test(sep.code);
247
221
  if (of.k === "str" && target.lone && (!parted || sep.lone))
248
- fail(s.comp, lonely("`.join()` with no literal separator"), n);
222
+ fail(s.comp, "FV0706", lonely("`.join()` with no literal separator"), n);
249
223
  const each = of.k === "str" ? "" : of.k === "bool" ? `.map(|v| if v { "true" } else { "false" })` : ".map(|v| fv::Js(v).to_string())";
250
224
  return { code: `&*${items(target)}${each}.collect::<Vec<_>>().join(${strArg(sep.code)})`, ty: STR, ...(target.lone || sep.lone ? { lone: true } : {}) };
251
225
  }
@@ -0,0 +1,3 @@
1
+ import { type N, type Scope } from "./model.ts";
2
+ import { Emitter } from "./emitter.ts";
3
+ export declare function list(s: Scope, e: Emitter, c: N): void;
package/dist/loops.js ADDED
@@ -0,0 +1,168 @@
1
+ import { fail, INT, snake, STR } from "./model.js";
2
+ import { ctx } from "./context.js";
3
+ import { expr, fieldVal } from "./expr.js";
4
+ import { isObjectCall } from "./calls.js";
5
+ import { atom, operand, OR } from "./parens.js";
6
+ import { Emitter } from "./emitter.js";
7
+ import { statements } from "./template.js";
8
+ export function list(s, e, c) {
9
+ const fn = c.arguments[1];
10
+ if (fn.type !== "ArrowFunctionExpression" || fn.body.type !== "BlockStatement") {
11
+ fail(s.comp, "FV0006", "unexpected `ssrRenderList` callback", c);
12
+ }
13
+ if (isObjectCall(c.arguments[0], "entries")) {
14
+ const [pair, index] = fn.params;
15
+ const r = c.arguments[0].arguments.length === 1 ? expr(s, c.arguments[0].arguments[0]) : null;
16
+ if (r?.ty.k !== "record")
17
+ return fail(s.comp, "FV0814", "`Object.entries()` takes a `Record<string, T>`", c.arguments[0]);
18
+ if (pair?.type !== "ArrayPattern" || pair.elements.length > 2 || pair.elements.some((x) => x?.type !== "Identifier")) {
19
+ return fail(s.comp, "FV0818", "a `v-for` over `Object.entries()` names its items `[key, value]`", pair ?? c);
20
+ }
21
+ recordLoop(s, e, r, fn, pair.elements[1], pair.elements[0], index);
22
+ return;
23
+ }
24
+ const src = expr(s, c.arguments[0]);
25
+ if (src.ty.k === "record") {
26
+ const [value, key, index] = fn.params;
27
+ recordLoop(s, e, src, fn, value, key, index);
28
+ return;
29
+ }
30
+ const [item, index] = fn.params;
31
+ const idx = index ? snake(index.name) : null;
32
+ const itemName = item.type === "Identifier" ? snake(item.name) : `fv_item${++ctx.narrowCount}`;
33
+ const inner = new Map(s.locals);
34
+ let of;
35
+ let walk;
36
+ let bound;
37
+ let itemLet = -1;
38
+ const loop = (it, i) => (i ? `for (${i}, ${it}) in ${atom(walk)}.enumerate()` : `for ${it} in ${walk}`);
39
+ if (src.ty.k === "list" && src.iter !== undefined) {
40
+ of = src.ty.of;
41
+ walk = src.iter;
42
+ bound = of.k === "str" ? `${itemName}_cow` : itemName;
43
+ e.open(loop(bound, idx));
44
+ if (of.k === "str") {
45
+ e.stmt(`let ${itemName}: &str = &${bound};`);
46
+ itemLet = e.lines.length - 1;
47
+ }
48
+ }
49
+ else if (src.ty.k === "int") {
50
+ of = INT;
51
+ walk = `1..=${operand(src.code, OR)}`;
52
+ bound = itemName;
53
+ e.open(loop(bound, idx));
54
+ }
55
+ else if (src.ty.k === "list") {
56
+ of = src.ty.of;
57
+ if (of.k === "undef")
58
+ fail(s.comp, "FV0819", "`v-for` over an empty array literal", c);
59
+ const itemCode = `${itemName}_ref`;
60
+ walk = `${atom(src.code)}.iter()`;
61
+ bound = itemCode;
62
+ e.open(loop(bound, idx));
63
+ if (of.k === "str")
64
+ e.stmt(`let ${itemName}: &str = ${itemCode};`);
65
+ else if (of.k === "struct" || of.k === "child")
66
+ e.stmt(`let ${itemName} = ${itemCode};`);
67
+ else if (of.k === "opt" && of.of.k === "str")
68
+ e.stmt(`let ${itemName} = ${itemCode}.as_deref();`);
69
+ else if (of.k === "opt" && !["int", "float", "bool"].includes(of.of.k))
70
+ e.stmt(`let ${itemName} = ${itemCode}.as_ref();`);
71
+ else
72
+ e.stmt(`let ${itemName} = *${itemName}_ref;`);
73
+ itemLet = e.lines.length - 1;
74
+ }
75
+ else {
76
+ return fail(s.comp, "FV0820", "`v-for` walks an array, or counts to a number", c);
77
+ }
78
+ let idxLet = -1;
79
+ if (idx) {
80
+ e.stmt(`let ${idx} = ${idx} as i64;`);
81
+ idxLet = e.lines.length - 1;
82
+ }
83
+ const bodyFrom = e.lines.length;
84
+ if (item.type === "Identifier")
85
+ inner.set(item.name, { code: itemName, ty: of, ...(src.lone ? { lone: true } : {}) });
86
+ else if (item.type === "ObjectPattern") {
87
+ for (const p of item.properties) {
88
+ if (p.type !== "ObjectProperty" || p.computed || p.value.type !== "Identifier") {
89
+ fail(s.comp, "FV0821", "a destructured `v-for` item binds plain names, without defaults", p);
90
+ }
91
+ inner.set(p.value.name, fieldVal(s.comp, itemName, of, p.key.name ?? p.key.value, p));
92
+ }
93
+ }
94
+ else
95
+ fail(s.comp, "FV0822", "a `v-for` item is a name or an object pattern", item);
96
+ if (index)
97
+ inner.set(index.name, { code: idx, ty: INT });
98
+ const before = e.literalBytes;
99
+ const fromProps = src.ty.k === "list" && src.iter === undefined && /^\(?props\./.test(src.code);
100
+ statements({ ...s, locals: inner, loop: fromProps ? { item: itemName, over: src.code } : undefined }, e, fn.body.body);
101
+ const unusedItem = !e.reads(itemName, bodyFrom);
102
+ const unusedIdx = idx !== null && !e.reads(idx, bodyFrom);
103
+ const head = bodyFrom - 1 - (idx ? 1 : 0) - (itemLet >= 0 ? 1 : 0);
104
+ e.replace(head, loop(bound, idx), loop(unusedItem ? "_" : bound, unusedIdx ? null : idx));
105
+ if (unusedIdx)
106
+ e.lines.splice(idxLet, 1);
107
+ if (unusedItem && itemLet >= 0)
108
+ e.lines.splice(itemLet, 1);
109
+ e.close();
110
+ const body = e.literalBytes - before;
111
+ e.literalBytes = before;
112
+ if (body > 0 && fromProps)
113
+ e.perItem.push(`${body} * ${atom(src.code)}.len()`);
114
+ else if (body > 0 && src.ty.k === "list" && s.loop && src.code.startsWith(`${s.loop.item}.`)) {
115
+ const outer = s.loop;
116
+ e.perItem.push(`${body} * ${atom(outer.over)}.iter().map(|${outer.item}| ${src.code}.len()).sum::<usize>()`);
117
+ }
118
+ }
119
+ function recordLoop(s, e, r, fn, value, key, index) {
120
+ if (r.ty.k !== "record")
121
+ return;
122
+ const of = r.ty.of;
123
+ for (const p of [key, index])
124
+ if (p && p.type !== "Identifier")
125
+ fail(s.comp, "FV0823", "a record's key and index in `v-for` are plain names", p);
126
+ const n = ++ctx.narrowCount;
127
+ const v = value?.type === "Identifier" ? snake(value.name) : `fv_value${n}`;
128
+ const k = key ? snake(key.name) : `fv_key${n}`;
129
+ const i = index ? snake(index.name) : `fv_index${n}`;
130
+ e.open(`for (${i}, (${k}, ${v}_ref)) in ${atom(r.code)}.iter().enumerate()`);
131
+ const head = e.lines.length - 1;
132
+ const indent = /^ */.exec(e.lines[head])[0];
133
+ const inner = new Map(s.locals);
134
+ e.stmt(of.k === "str" ? `let ${v}: &str = ${v}_ref;` : of.k === "int" || of.k === "float" || of.k === "bool" ? `let ${v} = *${v}_ref;` : `let ${v} = ${v}_ref;`);
135
+ e.stmt(`let ${i} = ${i} as i64;`);
136
+ const bodyFrom = e.lines.length;
137
+ if (value?.type === "Identifier")
138
+ inner.set(value.name, { code: v, ty: of });
139
+ else if (value?.type === "ObjectPattern" && (of.k === "struct" || of.k === "child")) {
140
+ for (const p of value.properties) {
141
+ if (p.type !== "ObjectProperty" || p.computed || p.value.type !== "Identifier") {
142
+ fail(s.comp, "FV0821", "a destructured `v-for` item binds plain names, without defaults", p);
143
+ }
144
+ inner.set(p.value.name, fieldVal(s.comp, v, of, p.key.name ?? p.key.value, p));
145
+ }
146
+ }
147
+ else if (value)
148
+ fail(s.comp, "FV0822", "a `v-for` item is a name, or an object pattern of an object", value);
149
+ if (key)
150
+ inner.set(key.name, { code: k, ty: STR });
151
+ if (index)
152
+ inner.set(index.name, { code: i, ty: INT });
153
+ const before = e.literalBytes;
154
+ statements({ ...s, locals: inner }, e, fn.body.body);
155
+ const reads = (name) => e.reads(name, bodyFrom);
156
+ const [readsV, readsK, readsI] = [reads(v), reads(k), reads(i)];
157
+ if (!readsI)
158
+ e.lines.splice(head + 2, 1);
159
+ if (!readsV)
160
+ e.lines.splice(head + 1, 1);
161
+ const pair = `(${readsK ? k : "_"}, ${readsV ? `${v}_ref` : "_"})`;
162
+ e.lines[head] = `${indent}for ${readsI ? `(${i}, ${pair}) in ${atom(r.code)}.iter().enumerate()` : `${pair} in ${atom(r.code)}.iter()`} {`;
163
+ e.close();
164
+ const body = e.literalBytes - before;
165
+ e.literalBytes = before;
166
+ if (body > 0 && /^\(?props\./.test(r.code))
167
+ e.perItem.push(`${body} * ${atom(r.code)}.len()`);
168
+ }