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.
- package/README.md +225 -42
- package/dist/attrs.d.ts +3 -53
- package/dist/attrs.js +41 -469
- package/dist/calls.d.ts +4 -0
- package/dist/calls.js +221 -0
- package/dist/children.d.ts +16 -0
- package/dist/children.js +286 -0
- package/dist/classes.d.ts +13 -0
- package/dist/classes.js +176 -0
- package/dist/cli.js +31 -22
- package/dist/client.d.ts +35 -2
- package/dist/client.js +77 -14
- package/dist/compiler.d.ts +1 -1
- package/dist/compiler.js +31 -92
- package/dist/component.d.ts +3 -1
- package/dist/component.js +123 -104
- package/dist/constants.d.ts +42 -0
- package/dist/constants.js +292 -0
- package/dist/context.d.ts +29 -67
- package/dist/context.js +13 -38
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.js +30 -0
- package/dist/diff.d.ts +0 -3
- package/dist/diff.js +0 -9
- package/dist/emitter.d.ts +0 -8
- package/dist/emitter.js +0 -12
- package/dist/errors.d.ts +867 -0
- package/dist/errors.js +330 -0
- package/dist/expr.d.ts +1 -70
- package/dist/expr.js +142 -934
- package/dist/fallthrough.d.ts +9 -0
- package/dist/fallthrough.js +78 -0
- package/dist/fixture.d.ts +4 -10
- package/dist/fixture.js +38 -27
- package/dist/hydration.d.ts +15 -0
- package/dist/hydration.js +80 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/islands.d.ts +1 -0
- package/dist/islands.js +1 -0
- package/dist/link-router.d.ts +24 -0
- package/dist/link-router.js +36 -0
- package/dist/lists.d.ts +0 -11
- package/dist/lists.js +34 -60
- package/dist/loops.d.ts +3 -0
- package/dist/loops.js +168 -0
- package/dist/model.d.ts +46 -144
- package/dist/model.js +150 -18
- package/dist/narrowing.d.ts +27 -0
- package/dist/narrowing.js +157 -0
- package/dist/numbers.d.ts +8 -0
- package/dist/numbers.js +73 -0
- package/dist/parens.d.ts +0 -27
- package/dist/parens.js +0 -44
- package/dist/plugin.d.ts +84 -0
- package/dist/plugin.js +26 -0
- package/dist/plugins/client-only.d.ts +8 -0
- package/dist/plugins/client-only.js +107 -0
- package/dist/plugins/i18n.d.ts +23 -0
- package/dist/{i18n.js → plugins/i18n.js} +88 -31
- package/dist/plugins/index.d.ts +2 -0
- package/dist/plugins/index.js +10 -0
- package/dist/plugins/router-link.d.ts +8 -0
- package/dist/{router.js → plugins/router-link.js} +52 -91
- package/dist/plugins/router.d.ts +42 -0
- package/dist/plugins/router.js +256 -0
- package/dist/plugins/scoped.d.ts +12 -0
- package/dist/plugins/scoped.js +125 -0
- package/dist/plugins/shared-types.d.ts +2 -0
- package/dist/plugins/shared-types.js +7 -0
- package/dist/plugins/stores.d.ts +37 -0
- package/dist/plugins/stores.js +379 -0
- package/dist/plugins/teleport.d.ts +5 -0
- package/dist/plugins/teleport.js +55 -0
- package/dist/plugins/twins.d.ts +10 -0
- package/dist/plugins/twins.js +164 -0
- package/dist/routes.d.ts +10 -0
- package/dist/routes.js +6 -0
- package/dist/rust.d.ts +1 -7
- package/dist/rust.js +56 -174
- package/dist/script.d.ts +3 -6
- package/dist/script.js +127 -140
- package/dist/settle.d.ts +6 -0
- package/dist/settle.js +34 -0
- package/dist/slots.d.ts +13 -0
- package/dist/slots.js +160 -0
- package/dist/strings.d.ts +13 -0
- package/dist/strings.js +208 -0
- package/dist/styles.d.ts +14 -0
- package/dist/styles.js +181 -0
- package/dist/template.d.ts +1 -42
- package/dist/template.js +25 -749
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +4 -10
- package/dist/types.d.ts +2 -0
- package/dist/typescript.d.ts +2 -14
- package/dist/typescript.js +53 -59
- package/dist/vite.d.ts +2 -1
- package/dist/vite.js +11 -32
- package/package.json +15 -6
- package/dist/i18n.d.ts +0 -27
- package/dist/router.d.ts +0 -17
- package/dist/scoped.d.ts +0 -24
- package/dist/scoped.js +0 -177
- package/dist/stores.d.ts +0 -6
- package/dist/stores.js +0 -229
package/dist/rust.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
/* The Rust source written for each component, and the modules beside them. */
|
|
2
1
|
import { parse as parseJs } from "@babel/parser";
|
|
3
2
|
import { basename } from "node:path";
|
|
4
|
-
import { fail, GenError, rustStr, snake, tagAst, takesAttrs } from "./model.js";
|
|
5
|
-
import {
|
|
3
|
+
import { blankComponent, fail, GenError, rustStr, snake, tagAst, takesAttrs } from "./model.js";
|
|
4
|
+
import { ctx } from "./context.js";
|
|
6
5
|
import { lookupStruct } from "./typescript.js";
|
|
7
6
|
import { childOf } from "./expr.js";
|
|
8
7
|
import { Emitter } from "./emitter.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { statements } from "./template.js";
|
|
9
|
+
import { slotFieldBorrows, slotFieldTy, slotTypeName } from "./slots.js";
|
|
10
|
+
import { extraParams, fieldInit, takesSlots } from "./children.js";
|
|
11
|
+
import { paramsOf, renderParams, slotFieldsOf } from "./plugin.js";
|
|
11
12
|
import { scopeFor } from "./script.js";
|
|
12
13
|
export function needsLifetime(ty, comp, seen = new Set()) {
|
|
13
14
|
switch (ty.k) {
|
|
14
|
-
// A record's keys are strings.
|
|
15
15
|
case "str":
|
|
16
16
|
case "record":
|
|
17
17
|
return true;
|
|
@@ -19,7 +19,6 @@ export function needsLifetime(ty, comp, seen = new Set()) {
|
|
|
19
19
|
case "list":
|
|
20
20
|
return needsLifetime(ty.of, comp, seen);
|
|
21
21
|
case "struct": {
|
|
22
|
-
// A struct that holds itself borrows only if something else in it does.
|
|
23
22
|
if (seen.has(ty.name))
|
|
24
23
|
return false;
|
|
25
24
|
const { st, owner } = lookupStruct(comp, ty);
|
|
@@ -30,7 +29,6 @@ export function needsLifetime(ty, comp, seen = new Set()) {
|
|
|
30
29
|
const child = childOf(ty.name);
|
|
31
30
|
return structLifetime(child.props, child);
|
|
32
31
|
}
|
|
33
|
-
// A configured type that borrows says so by naming the props' lifetime: `Trusted<'a>`.
|
|
34
32
|
case "html":
|
|
35
33
|
return ctx.trustedHtml?.includes("'a") ?? false;
|
|
36
34
|
default:
|
|
@@ -59,7 +57,7 @@ export function rustTy(ty, comp) {
|
|
|
59
57
|
case "struct": {
|
|
60
58
|
const { st, owner, path } = lookupStruct(comp, ty);
|
|
61
59
|
if (!st)
|
|
62
|
-
throw new GenError(`no type \`${ty.name}\``);
|
|
60
|
+
throw new GenError("FV0308", `no type \`${ty.name}\``);
|
|
63
61
|
return `${path}${ty.name}${structLifetime(st, owner) ? "<'a>" : ""}`;
|
|
64
62
|
}
|
|
65
63
|
case "html":
|
|
@@ -69,11 +67,9 @@ export function rustTy(ty, comp) {
|
|
|
69
67
|
return `super::${child.module}::Props${structLifetime(child.props, child) ? "<'a>" : ""}`;
|
|
70
68
|
}
|
|
71
69
|
default:
|
|
72
|
-
throw new GenError("no Rust type for `undefined`");
|
|
70
|
+
throw new GenError("FV0309", "no Rust type for `undefined`");
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
|
-
/** A constructor parameter for a field of type \`ty\`, and how its argument becomes the field's
|
|
76
|
-
* value: strings from anything that turns into a \`Cow\`, lists of them from any iterator. */
|
|
77
73
|
function param(ty, comp, arg) {
|
|
78
74
|
if (ty.k === "str")
|
|
79
75
|
return { ty: "impl Into<Cow<'a, str>>", value: `${arg}.into()` };
|
|
@@ -82,12 +78,16 @@ function param(ty, comp, arg) {
|
|
|
82
78
|
}
|
|
83
79
|
return { ty: rustTy(ty, comp), value: arg };
|
|
84
80
|
}
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
function absentNote(optional) {
|
|
82
|
+
const nulls = optional.filter((f) => f.ty.k === "opt" && f.ty.none === "null").length;
|
|
83
|
+
if (!nulls)
|
|
84
|
+
return optional.length ? ", every optional one absent" : "";
|
|
85
|
+
return nulls === optional.length ? ", every nullable one `null`" : ", every optional one absent and every nullable one `null`";
|
|
86
|
+
}
|
|
87
87
|
function builderSource(st, comp, life) {
|
|
88
88
|
const required = st.fields.filter((f) => f.ty.k !== "opt");
|
|
89
89
|
const optional = st.fields.filter((f) => f.ty.k === "opt");
|
|
90
|
-
const arg = (f) => f.rust
|
|
90
|
+
const arg = (f) => f.rust;
|
|
91
91
|
const params = required.map((f) => `${arg(f)}: ${param(f.ty, comp, arg(f)).ty}`).join(", ");
|
|
92
92
|
const inits = st.fields
|
|
93
93
|
.map((f) => (f.ty.k === "opt" ? `${f.rust}: None` : fieldInit(f.rust, param(f.ty, comp, arg(f)).value)))
|
|
@@ -97,7 +97,7 @@ function builderSource(st, comp, life) {
|
|
|
97
97
|
.map((f) => {
|
|
98
98
|
const p = param(f.ty.of, comp, arg(f));
|
|
99
99
|
return `
|
|
100
|
-
/// Set \`${f.js}\`, which is absent otherwise.
|
|
100
|
+
/// Set \`${f.js}\`, which is ${f.ty.none === "null" ? "`null`" : "absent"} otherwise.
|
|
101
101
|
pub fn ${f.rust}(mut self, ${arg(f)}: ${p.ty}) -> Self {
|
|
102
102
|
self.${f.rust} = Some(${p.value});
|
|
103
103
|
self
|
|
@@ -105,10 +105,9 @@ function builderSource(st, comp, life) {
|
|
|
105
105
|
})
|
|
106
106
|
.join("\n");
|
|
107
107
|
const usesA = /'a/.test(params + setters);
|
|
108
|
-
// A struct without strings has no lifetime for its constructor to name.
|
|
109
108
|
const impl = life ? "impl<'a>" : usesA ? "impl<'a>" : "impl";
|
|
110
109
|
return `${impl} ${st.name}${life} {
|
|
111
|
-
/// ${st.name} with ${required.length ? "its required fields" : "nothing set"}${optional
|
|
110
|
+
/// ${st.name} with ${required.length ? "its required fields" : "nothing set"}${absentNote(optional)}.
|
|
112
111
|
${required.length > 7 ? " // One argument per required field, however many the type declares.\n #[allow(clippy::too_many_arguments)]\n" : ""} pub fn new(${params}) -> Self {
|
|
113
112
|
${st.name} { ${inits} }
|
|
114
113
|
}
|
|
@@ -120,29 +119,25 @@ export function structSource(st, comp, doc) {
|
|
|
120
119
|
const life = structLifetime(st, comp) ? "<'a>" : "";
|
|
121
120
|
const fields = st.fields
|
|
122
121
|
.map((f) => {
|
|
123
|
-
const attrs = f.ty.k === "opt"
|
|
122
|
+
const attrs = f.ty.k === "opt" && f.ty.none === undefined
|
|
124
123
|
? ` #[serde(rename = ${rustStr(f.js)}, default, skip_serializing_if = "Option::is_none")]`
|
|
125
124
|
: ` #[serde(rename = ${rustStr(f.js)})]`;
|
|
126
125
|
return `${attrs}\n pub ${f.rust}: ${rustTy(f.ty, comp)},`;
|
|
127
126
|
})
|
|
128
127
|
.join("\n");
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return `${doc}#[derive(Debug, Clone, serde::Serialize)]
|
|
128
|
+
const derives = st.fields.some((f) => f.ty.k !== "opt") ? "Debug, Clone" : "Debug, Clone, Default";
|
|
129
|
+
return `${doc}#[derive(${derives}, serde::Serialize)]
|
|
132
130
|
#[cfg_attr(test, derive(serde::Deserialize))]
|
|
133
131
|
pub struct ${st.name}${life} {
|
|
134
132
|
${fields}
|
|
135
133
|
}
|
|
136
|
-
|
|
137
|
-
${builderSource(st, comp, life)}
|
|
138
|
-
`;
|
|
134
|
+
${ctx.builders ? `\n${builderSource(st, comp, life)}\n` : ""}`;
|
|
139
135
|
}
|
|
140
136
|
export function header(source, edit = "the `.vue` file") {
|
|
141
137
|
return `// @generated by ferrovue from ${source}. Do not edit: change ${edit} and run
|
|
142
138
|
// \`ferrovue\`.
|
|
143
139
|
`;
|
|
144
140
|
}
|
|
145
|
-
/** Rust expressions summing the lengths of the strings a value of type `ty` at `place` holds. */
|
|
146
141
|
export function textLen(comp, place, ty, seen = new Set()) {
|
|
147
142
|
switch (ty.k) {
|
|
148
143
|
case "str":
|
|
@@ -160,7 +155,6 @@ export function textLen(comp, place, ty, seen = new Set()) {
|
|
|
160
155
|
}
|
|
161
156
|
return [];
|
|
162
157
|
case "struct": {
|
|
163
|
-
// A recursive structure is counted to its first level: the reservation is an estimate.
|
|
164
158
|
if (seen.has(ty.name))
|
|
165
159
|
return [];
|
|
166
160
|
const { st, owner } = lookupStruct(comp, ty);
|
|
@@ -179,18 +173,12 @@ export function textLen(comp, place, ty, seen = new Set()) {
|
|
|
179
173
|
return [];
|
|
180
174
|
}
|
|
181
175
|
}
|
|
182
|
-
/** `render`, and for a component a parent may hand scope ids or pass attributes to,
|
|
183
|
-
* `render_scoped`, which takes them last, as `ssrRenderAttrs` writes them onto its root; `render`
|
|
184
|
-
* hands it none. */
|
|
185
176
|
function renderSource(comp, life, args, e) {
|
|
186
177
|
const doc = "/// Write the component's server render into `out`.\n";
|
|
187
|
-
// Props or ids the render never reads, as when a component only passes its slot on.
|
|
188
178
|
const props = e.reads("props", 0) ? "props" : "_props";
|
|
189
179
|
if (!comp.inherits && !takesAttrs(comp))
|
|
190
180
|
return `${doc}pub fn render(out: &mut String, ${props}: &Props${life}${extraParams(comp)}) {\n${e.lines.join("\n")}\n}`;
|
|
191
|
-
// A root that is a fragment, or a `<Teleport>`, takes no ids.
|
|
192
181
|
const attrs = e.reads("fv_attrs", 0) ? "fv_attrs" : "_fv_attrs";
|
|
193
|
-
// A component a parent may pass attributes takes them with the ids, as one `fv::Attrs`.
|
|
194
182
|
const [none, ty, what] = takesAttrs(comp)
|
|
195
183
|
? ["&fv::Attrs::NONE", "&fv::Attrs<'_>", "the attributes a parent passes beyond the props, and the scope ids it hands the root"]
|
|
196
184
|
: ['""', "&str", "the scope ids a parent hands the root: ` data-v-…` each"];
|
|
@@ -204,37 +192,29 @@ pub fn render_scoped(out: &mut String, ${props}: &Props${life}${extraParams(comp
|
|
|
204
192
|
${e.lines.join("\n")}
|
|
205
193
|
}`;
|
|
206
194
|
}
|
|
207
|
-
/** Whether the component gets an `island()`: it renders from its props alone, which `data-props` is
|
|
208
|
-
* all the client gets to mount it with. */
|
|
209
195
|
export function isIsland(comp) {
|
|
210
|
-
return !takesSlots(comp) &&
|
|
196
|
+
return !takesSlots(comp) && comp.takes.size === 0;
|
|
211
197
|
}
|
|
212
198
|
export function componentSource(comp, ast, ssr, components) {
|
|
213
199
|
if (takesAttrs(comp) && comp.inheritAttrs && comp.attrsDropped) {
|
|
214
|
-
fail(comp, `a root \`<Transition>\` or \`<KeepAlive>\` around a \`v-if\` in ${comp.name}, which a parent passes attributes: Vue's server drops them, where its client puts them on the element; set \`inheritAttrs: false\` and bind \`$attrs\` on the element`, comp.attrsDropped);
|
|
200
|
+
fail(comp, "FV0416", `a root \`<Transition>\` or \`<KeepAlive>\` around a \`v-if\` in ${comp.name}, which a parent passes attributes: Vue's server drops them, where its client puts them on the element; set \`inheritAttrs: false\` and bind \`$attrs\` on the element`, comp.attrsDropped);
|
|
215
201
|
}
|
|
216
202
|
const { scope, lets } = scopeFor(comp, ast, components);
|
|
217
203
|
const program = parseJs(ssr, { sourceType: "module" }).program;
|
|
218
204
|
tagAst(program, "template");
|
|
219
205
|
const fn = program.body.find((s) => s.type === "ExportNamedDeclaration" && s.declaration?.id?.name === "ssrRender")?.declaration;
|
|
220
206
|
if (!fn)
|
|
221
|
-
fail(comp, "the compiled template has no `ssrRender`");
|
|
207
|
+
fail(comp, "FV0006", "the compiled template has no `ssrRender`");
|
|
222
208
|
const e = new Emitter();
|
|
223
209
|
for (const l of lets)
|
|
224
210
|
e.stmt(l);
|
|
225
211
|
statements(scope, e, fn.body.body);
|
|
226
|
-
// A setup value the render never reads is not computed. Checked from the last up, since one may
|
|
227
|
-
// read another declared before it.
|
|
228
212
|
for (let i = lets.length - 1; i >= 0; i--) {
|
|
229
213
|
const name = /^let (\w+)/.exec(lets[i])[1];
|
|
230
214
|
if (!e.reads(name, i + 1))
|
|
231
215
|
e.lines.splice(i, 1);
|
|
232
216
|
}
|
|
233
217
|
e.flush();
|
|
234
|
-
/* The literal markup, a loop's once per item, what the helpers can write, and every string the
|
|
235
|
-
* props hold. Literals in untaken branches overcount and escaping or a string used twice
|
|
236
|
-
* undercounts, so it is an estimate — close enough that the buffer is sized once, where
|
|
237
|
-
* reserving the literals alone leaves it to grow again at the first long label. */
|
|
238
218
|
const text = textLen(comp, "props", { k: "struct", name: "Props" });
|
|
239
219
|
const fixed = e.literalBytes + scope.helperBytes.n;
|
|
240
220
|
const reserve = [...(fixed || !(e.perItem.length + text.length) ? [String(fixed)] : []), ...e.perItem, ...text];
|
|
@@ -242,16 +222,12 @@ export function componentSource(comp, ast, ssr, components) {
|
|
|
242
222
|
const life = structLifetime(comp.props, comp) ? "<'_>" : "";
|
|
243
223
|
const gen = life ? "<'p, 'a>" : "<'p>";
|
|
244
224
|
const named = life ? "<'a>" : "";
|
|
245
|
-
// What holds its props borrows only what they borrow.
|
|
246
225
|
const owned = life ? "<'a>" : "";
|
|
247
226
|
const ownedLife = life ? "'a" : "'static";
|
|
248
|
-
// An `interface Props` that `defineProps` takes is the props struct itself, written once below.
|
|
249
227
|
const structs = [...comp.structs.values()]
|
|
250
228
|
.filter((st) => st.name !== "Props" && !st.slot)
|
|
251
229
|
.map((st) => structSource(st, comp, `/// \`${st.name}\` in \`${basename(comp.file)}\`.\n`))
|
|
252
230
|
.join("\n");
|
|
253
|
-
// `Cow` is imported only where a field is one: a lifetime that comes from another component's props
|
|
254
|
-
// alone borrows through that type, not through a `Cow` written here.
|
|
255
231
|
const usesCow = /Cow</.test(structs + structSource(comp.props, comp, ""));
|
|
256
232
|
const plain = isIsland(comp);
|
|
257
233
|
const slotFields = [
|
|
@@ -260,7 +236,7 @@ export function componentSource(comp, ast, ssr, components) {
|
|
|
260
236
|
const type = comp.slotShapes.has(n) ? `&'s ${slotTypeName(n, "Slot")}<'s>` : "fv::Slot<'s>";
|
|
261
237
|
return ` /// ${outlet}\n pub ${snake(n)}: Option<${type}>,`;
|
|
262
238
|
}),
|
|
263
|
-
...(comp.
|
|
239
|
+
...slotFieldsOf(comp).map((f) => ` /// ${f.doc}\n pub ${f.rust}: fv::Slot<'s>,`),
|
|
264
240
|
];
|
|
265
241
|
const slotTypes = [...comp.slotShapes.entries()]
|
|
266
242
|
.map(([n, shape]) => {
|
|
@@ -281,19 +257,15 @@ pub type ${slotTypeName(n, "Slot")}<'s> = dyn ${borrows ? "for<'v> " : ""}Fn(&mu
|
|
|
281
257
|
.join("");
|
|
282
258
|
const slotsStruct = takesSlots(comp)
|
|
283
259
|
? `${slotTypes}/// What a parent puts in the slots \`${basename(comp.file)}\` renders.
|
|
284
|
-
#[derive(Clone, Copy${comp.
|
|
260
|
+
#[derive(Clone, Copy${slotFieldsOf(comp).length ? "" : ", Default"})]
|
|
285
261
|
pub struct Slots<'s> {
|
|
286
262
|
${slotFields.join("\n")}
|
|
287
263
|
}
|
|
288
264
|
|
|
289
265
|
`
|
|
290
266
|
: "";
|
|
291
|
-
const args = (takesSlots(comp) ? ", fv_slots" : "") + (comp.
|
|
292
|
-
const params = (takesSlots(comp) ? ", fv_slots: Slots<'p>" : "") +
|
|
293
|
-
(comp.usesRoute ? ", fv_route: &'p fv::Route<'p>" : "") +
|
|
294
|
-
(comp.usesStores ? ", fv_stores: &'p super::stores::Stores<'p>" : "") +
|
|
295
|
-
(comp.usesI18n ? ", fv_i18n: &'p fv::I18n" : "") +
|
|
296
|
-
(comp.usesTeleports ? ", fv_teleports: &'p fv::Teleports" : "");
|
|
267
|
+
const args = (takesSlots(comp) ? ", fv_slots" : "") + paramsOf(comp).map((p) => `, ${p.name}`).join("");
|
|
268
|
+
const params = (takesSlots(comp) ? ", fv_slots: Slots<'p>" : "") + paramsOf(comp).map((p) => `, ${p.name}: ${p.pageTy}`).join("");
|
|
297
269
|
const wrappers = plain
|
|
298
270
|
? `/// The component's markup, for a maud page that shows it without hydrating it.
|
|
299
271
|
pub fn html${gen}(props: &'p Props${named}) -> fv::Html<'p, Props${named}> {
|
|
@@ -331,20 +303,22 @@ ${slotsStruct}${renderSource(comp, life, args, e)}
|
|
|
331
303
|
|
|
332
304
|
${wrappers}`;
|
|
333
305
|
}
|
|
334
|
-
|
|
306
|
+
function readsFixture(c) {
|
|
307
|
+
return takesSlots(c) || paramsOf(c).some((p) => p.test.fixture);
|
|
308
|
+
}
|
|
309
|
+
export function modSource(comps, modules) {
|
|
335
310
|
const arms = comps
|
|
336
311
|
.map((c) => {
|
|
337
312
|
const lines = [`let props: ${c.module}::Props = serde_json::from_str(json).map_err(|e| e.to_string())?;`];
|
|
338
313
|
let args = "";
|
|
339
|
-
if (
|
|
314
|
+
if (readsFixture(c))
|
|
340
315
|
lines.push("let fixture: Fixture = serde_json::from_str(json).map_err(|e| e.to_string())?;");
|
|
341
316
|
if (takesSlots(c)) {
|
|
342
|
-
const names = [...c.slotNames, ...(c.
|
|
317
|
+
const names = [...c.slotNames, ...slotFieldsOf(c).map((f) => f.js)];
|
|
343
318
|
for (const n of names) {
|
|
344
319
|
const local = `s_${snake(n).replace(/^r#/, "")}`;
|
|
345
320
|
const shape = c.slotShapes.get(n);
|
|
346
321
|
if (shape) {
|
|
347
|
-
// A fixture's content for a scoped slot is static: it is given the props and ignores them.
|
|
348
322
|
const life = shape.fields.some((f) => slotFieldBorrows(f.ty)) ? "<'_>" : "";
|
|
349
323
|
lines.push(`let ${local} = |out: &mut String, _: &${c.module}::${shape.name}${life}${c.passesSlotIds ? ", _: &str" : ""}| -> bool { out.push_str(fixture.slot(${rustStr(n)}).unwrap_or_default()); true };`);
|
|
350
324
|
}
|
|
@@ -356,34 +330,23 @@ export function modSource(comps) {
|
|
|
356
330
|
const value = c.slotShapes.has(n) ? `&${local} as &${c.module}::${slotTypeName(n, "Slot")}` : `ferrovue::Slot::new(&${local})`;
|
|
357
331
|
return `${snake(n)}: fixture.slot(${rustStr(n)}).map(|_| ${value})`;
|
|
358
332
|
});
|
|
359
|
-
|
|
360
|
-
fields.push(
|
|
333
|
+
for (const f of slotFieldsOf(c))
|
|
334
|
+
fields.push(`${f.rust}: ferrovue::Slot::new(&s_${snake(f.js).replace(/^r#/, "")})`);
|
|
361
335
|
args += `, ${c.module}::Slots { ${fields.join(", ")} }`;
|
|
362
336
|
}
|
|
363
|
-
|
|
364
|
-
lines.push(
|
|
365
|
-
args +=
|
|
366
|
-
}
|
|
367
|
-
if (c.usesStores) {
|
|
368
|
-
lines.push("let state: stores::Stores = serde_json::from_value(fixture.stores.clone()).map_err(|e| e.to_string())?;");
|
|
369
|
-
args += ", &state";
|
|
370
|
-
}
|
|
371
|
-
if (c.usesI18n) {
|
|
372
|
-
lines.push("let i18n = i18n::i18n(fixture.locale.as_deref().unwrap_or(i18n::LOCALE));");
|
|
373
|
-
args += ", &i18n";
|
|
374
|
-
}
|
|
375
|
-
if (c.usesTeleports) {
|
|
376
|
-
lines.push("let teleports = ferrovue::Teleports::new();");
|
|
377
|
-
args += ", &teleports";
|
|
337
|
+
for (const p of paramsOf(c)) {
|
|
338
|
+
lines.push(...p.test.lines);
|
|
339
|
+
args += `, ${p.test.arg}`;
|
|
378
340
|
}
|
|
379
341
|
lines.push(`${c.module}::render(&mut out, &props${args});`);
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
lines.push("teleports_into(&mut out, teleports);");
|
|
342
|
+
for (const p of paramsOf(c))
|
|
343
|
+
lines.push(...(p.test.after ?? []));
|
|
383
344
|
return ` ${rustStr(c.name)} => {\n${lines.map((l) => " " + l).join("\n")}\n }`;
|
|
384
345
|
})
|
|
385
346
|
.join("\n");
|
|
386
|
-
const
|
|
347
|
+
const fields = renderParams().flatMap((p) => (p.fixtureField === undefined ? [] : [p.fixtureField]));
|
|
348
|
+
const defaults = renderParams().flatMap((p) => (p.fixtureDefault === undefined ? [] : [p.fixtureDefault]));
|
|
349
|
+
const fixture = comps.some(readsFixture)
|
|
387
350
|
? `
|
|
388
351
|
/// What a fixture holds besides the props: each slot's content, and the location it renders at.
|
|
389
352
|
#[cfg(test)]
|
|
@@ -391,25 +354,12 @@ export function modSource(comps) {
|
|
|
391
354
|
struct Fixture {
|
|
392
355
|
#[serde(rename = "$slots", default)]
|
|
393
356
|
slots: std::collections::HashMap<String, String>,
|
|
394
|
-
|
|
395
|
-
route: String,
|
|
396
|
-
#[serde(rename = "$stores", default = "Fixture::no_stores")]
|
|
397
|
-
stores: serde_json::Value,
|
|
398
|
-
#[serde(rename = "$locale", default)]
|
|
399
|
-
locale: Option<String>,
|
|
357
|
+
${fields.join("\n")}
|
|
400
358
|
}
|
|
401
359
|
|
|
402
360
|
#[cfg(test)]
|
|
403
361
|
impl Fixture {
|
|
404
|
-
fn
|
|
405
|
-
"/".to_owned()
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
fn no_stores() -> serde_json::Value {
|
|
409
|
-
serde_json::Value::Object(Default::default())
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
fn slot(&self, name: &str) -> Option<&str> {
|
|
362
|
+
${defaults.map((d) => `${d}\n\n`).join("")} fn slot(&self, name: &str) -> Option<&str> {
|
|
413
363
|
self.slots.get(name).map(String::as_str)
|
|
414
364
|
}
|
|
415
365
|
}
|
|
@@ -420,29 +370,15 @@ impl Fixture {
|
|
|
420
370
|
|
|
421
371
|
// The modules pass rustc's default warnings and clippy's default lints, with one exception:
|
|
422
372
|
// \`dead_code\`. Every component gets the whole of its API (\`render\`, \`html\`, \`island\`, their
|
|
423
|
-
//
|
|
424
|
-
// it needs.
|
|
373
|
+
${ctx.builders ? "// `into_` forms, `NAME`, a constructor and a setter per optional prop) and an app calls only what\n// it needs." : "// `into_` forms and `NAME`) and an app calls only what it needs."}
|
|
425
374
|
#![allow(dead_code)]
|
|
426
375
|
|
|
427
|
-
${comps.map((c) => `pub mod ${c.module};`).join("\n")}${
|
|
376
|
+
${comps.map((c) => `pub mod ${c.module};`).join("\n")}${modules.map((m) => `\npub mod ${m.replace(/\.rs$/, "")};`).join("")}
|
|
428
377
|
${fixture}
|
|
429
|
-
${
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
if targets.is_empty() {
|
|
434
|
-
return;
|
|
435
|
-
}
|
|
436
|
-
let pairs: Vec<String> = targets
|
|
437
|
-
.iter()
|
|
438
|
-
.map(|(t, html)| format!("{}:{}", serde_json::to_string(t).unwrap(), serde_json::to_string(html).unwrap()))
|
|
439
|
-
.collect();
|
|
440
|
-
out.push_str("<!--fv-teleports-->{");
|
|
441
|
-
out.push_str(&pairs.join(","));
|
|
442
|
-
out.push('}');
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
` : ""}/// Render one component from its props as JSON, for the conformance suite.
|
|
378
|
+
${renderParams()
|
|
379
|
+
.filter((p) => p.testSupport !== undefined && comps.some((c) => c.takes.has(p.name)))
|
|
380
|
+
.map((p) => `${p.testSupport}\n\n`)
|
|
381
|
+
.join("")}/// Render one component from its props as JSON, for the conformance suite.
|
|
446
382
|
#[cfg(test)]
|
|
447
383
|
pub fn render_json(component: &str, json: &str) -> Result<String, String> {
|
|
448
384
|
let mut out = String::new();
|
|
@@ -454,70 +390,16 @@ ${arms}
|
|
|
454
390
|
}
|
|
455
391
|
`;
|
|
456
392
|
}
|
|
457
|
-
export function storesSource(dir) {
|
|
458
|
-
const home = storeHome(dir);
|
|
459
|
-
// Test-only `Default` lets a fixture name only the stores it reads; the rest are never looked at.
|
|
460
|
-
const testDerive = (src) => src.replace("#[cfg_attr(test, derive(serde::Deserialize))]", "#[cfg_attr(test, derive(Default, serde::Deserialize))]\n#[cfg_attr(test, serde(default))]");
|
|
461
|
-
const structs = [...ctx.storeStructs.values()]
|
|
462
|
-
.map((st) => testDerive(structSource(st, home, `/// \`${st.name}\` in \`${ctx.storeFiles.get(st.name)}\`.\n`)))
|
|
463
|
-
.join("\n");
|
|
464
|
-
const all = {
|
|
465
|
-
name: "Stores",
|
|
466
|
-
fields: [...ctx.stores.values()].map((st) => ({ js: st.id, rust: st.field, ty: { k: "struct", name: st.state, store: true } })),
|
|
467
|
-
};
|
|
468
|
-
const top = testDerive(structSource(all, home, "/// Every store's state, keyed by id as `pinia.state.value` is: what the page sends the client.\n"));
|
|
469
|
-
return `${header(dir, "the store files")}
|
|
470
|
-
//! The Pinia stores' state, which components read while they render on the server.
|
|
471
|
-
|
|
472
|
-
${/Cow</.test(structs) ? "use std::borrow::Cow;\n\n" : ""}${structs}
|
|
473
|
-
${top}`;
|
|
474
|
-
}
|
|
475
393
|
export function typesSource() {
|
|
476
|
-
const home =
|
|
477
|
-
|
|
478
|
-
home.module = "types";
|
|
479
|
-
const files = [...new Set(ctx.typeFiles.values())].toSorted();
|
|
394
|
+
const home = blankComponent("types", "types", "types", ctx.typeStructs);
|
|
395
|
+
const files = [...new Set([...ctx.typeFiles.values(), ...[...ctx.typeConsts.values()].map((c) => c.file)])].toSorted();
|
|
480
396
|
const structs = [...ctx.typeStructs.values()]
|
|
481
397
|
.map((st) => structSource(st, home, `/// \`${st.name}\` in \`${ctx.typeFiles.get(st.name)}\`.\n`))
|
|
482
398
|
.join("\n");
|
|
399
|
+
const consts = [...ctx.typeConsts.values()].map((c) => `${c.text}\n`).join("");
|
|
483
400
|
return `${header(files.join(", "), "the type files")}
|
|
484
401
|
//! The types components import from shared \`.ts\` files, written once so that components passing
|
|
485
402
|
//! them to one another agree on them.
|
|
486
403
|
|
|
487
|
-
${/Cow</.test(structs) ? "use std::borrow::Cow;\n\n" : ""}${structs}`;
|
|
488
|
-
}
|
|
489
|
-
/** Route definitions as \`ferrovue::RouteDef\` literals, children nested. */
|
|
490
|
-
function routeDefs(routes, depth) {
|
|
491
|
-
const pad = " ".repeat(depth);
|
|
492
|
-
return routes
|
|
493
|
-
.map((r) => {
|
|
494
|
-
const name = r.name === undefined ? "None" : `Some(${rustStr(r.name)})`;
|
|
495
|
-
const children = r.children?.length ? `&[\n${routeDefs(r.children, depth + 1)}\n${pad}]` : "&[]";
|
|
496
|
-
return `${pad}ferrovue::RouteDef { path: ${rustStr(r.path)}, name: ${name}, children: ${children} },`;
|
|
497
|
-
})
|
|
498
|
-
.join("\n");
|
|
499
|
-
}
|
|
500
|
-
export function routesSource(routes, file) {
|
|
501
|
-
return `${header(file, "the routes file")}
|
|
502
|
-
//! The app's routes: what \`<RouterLink>\` resolves against and \`useRoute()\` reads.
|
|
503
|
-
|
|
504
|
-
/// Each route: its vue-router path, its name if it has one, and the routes nested in it.
|
|
505
|
-
pub const ROUTES: &[ferrovue::RouteDef<'static>] = &[
|
|
506
|
-
${routeDefs(routes, 1)}
|
|
507
|
-
];
|
|
508
|
-
|
|
509
|
-
/// Every route's full path, nested ones included.
|
|
510
|
-
pub const PATHS: &[&str] = &[
|
|
511
|
-
${allRoutes(routes).map((r) => ` ${rustStr(r.fullPath)},`).join("\n")}
|
|
512
|
-
];
|
|
513
|
-
|
|
514
|
-
/// The history's base, which every link's \`href\` starts with.
|
|
515
|
-
pub const BASE: &str = ${rustStr(ctx.routerBase)};
|
|
516
|
-
|
|
517
|
-
/// The router these routes make: build it once, and resolve each request's location with
|
|
518
|
-
/// [\`ferrovue::Router::at\`].
|
|
519
|
-
pub fn router() -> ferrovue::Router {
|
|
520
|
-
ferrovue::Router::tree(ROUTES).with_base(BASE)
|
|
521
|
-
}
|
|
522
|
-
`;
|
|
404
|
+
${/Cow</.test(structs) ? "use std::borrow::Cow;\n\n" : ""}${structs}${consts}`;
|
|
523
405
|
}
|
package/dist/script.d.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { type Component, type N, type Scope } from "./model.ts";
|
|
2
|
-
/** Lifecycle hooks, which never run on the server: setup may register them freely. */
|
|
3
2
|
export declare const CLIENT_HOOKS: Set<string>;
|
|
4
|
-
/** Compiler macros and calls with no effect on the server's render. */
|
|
5
3
|
export declare const INERT_CALLS: Set<string>;
|
|
6
|
-
/** A statement in setup that is a call on its own: allowed only when it cannot change the render. */
|
|
7
4
|
export declare function setupStatement(comp: Component, st: N): void;
|
|
8
|
-
/** What a setup binding's value is computed from: `ref(x)` and `shallowRef(x)` hold `x`,
|
|
9
|
-
* `computed(() => x)` is `x`, and a plain `const y = x` is `x`. */
|
|
10
5
|
export declare function setupSource(init: N): N | null;
|
|
11
|
-
/** The names a destructuring pattern binds. */
|
|
12
6
|
export declare function patternNames(p: N): string[];
|
|
7
|
+
/** The components `<script setup>` declares with `defineAsyncComponent(() => import("./X.vue"))`,
|
|
8
|
+
* by local name, each the name of the `.vue` file it loads. */
|
|
9
|
+
export declare function asyncChildren(comp: Component, ast: N[]): Map<string, string>;
|
|
13
10
|
export declare function scopeFor(comp: Component, ast: N[], components: Map<string, Component>): {
|
|
14
11
|
scope: Scope;
|
|
15
12
|
lets: string[];
|