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/scoped.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
/* Where scope ids reach: which components' roots a parent may hand ids to, and which components'
|
|
2
|
-
* slot content may be given a slot scope id. Both decide a generated signature — `render` taking
|
|
3
|
-
* `fv_attrs`, slot content taking `fv_sid` — so they are worked out for every component before any
|
|
4
|
-
* is generated.
|
|
5
|
-
*
|
|
6
|
-
* With `<style scoped>`, Vue's server renderer writes the component's id onto each of its elements
|
|
7
|
-
* (the compiled template spells it out) and hands ids to a child component's root through the
|
|
8
|
-
* child's `_attrs`: the id of the component that created it, what its parent passes on when the
|
|
9
|
-
* child is that parent's root, and the slot scope ids of the slot content it is rendered in
|
|
10
|
-
* (`renderComponentSubTree`). A component with `:slotted()` styles gives its slot content
|
|
11
|
-
* `data-v-…-s` as well, which the content writes onto its elements as `_scopeId`.
|
|
12
|
-
*
|
|
13
|
-
* The same `_attrs` carries the attributes a parent passes a child beyond its props, ahead of the
|
|
14
|
-
* ids: which components may be passed some (`attrsFlow`) decides whether `fv_attrs` is an
|
|
15
|
-
* `fv::Attrs` holding both, or the ids alone. */
|
|
16
|
-
import { parse as parseJs } from "@babel/parser";
|
|
17
|
-
import { declares } from "./model.js";
|
|
18
|
-
/** The child components and outlets in one component's compiled template. */
|
|
19
|
-
function flowOf(comp, ssr, children) {
|
|
20
|
-
const calls = [];
|
|
21
|
-
const outlets = [];
|
|
22
|
-
// `const _component_Tree = _resolveComponent("Tree", true)`: the component itself, by name.
|
|
23
|
-
const self = new Set();
|
|
24
|
-
const resolve = (target) => {
|
|
25
|
-
if (target?.type === "MemberExpression" && target.object.name === "$setup") {
|
|
26
|
-
return children.get(target.computed ? target.property.value : target.property.name) ?? null;
|
|
27
|
-
}
|
|
28
|
-
return target?.type === "Identifier" && self.has(target.name) ? comp.name : null;
|
|
29
|
-
};
|
|
30
|
-
const mentions = (n, name) => !!n && typeof n === "object" && (n.type === "Identifier" ? n.name === name : Object.values(n).some((v) => mentions(v, name)));
|
|
31
|
-
const walk = (n, within) => {
|
|
32
|
-
if (!n || typeof n !== "object")
|
|
33
|
-
return;
|
|
34
|
-
if (Array.isArray(n)) {
|
|
35
|
-
for (const x of n)
|
|
36
|
-
walk(x, within);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (n.type === "VariableDeclarator" && n.init?.type === "CallExpression" && n.init.callee.name === "_resolveComponent") {
|
|
40
|
-
if (n.init.arguments[0]?.value === comp.name && n.init.arguments[1])
|
|
41
|
-
self.add(n.id.name);
|
|
42
|
-
}
|
|
43
|
-
if (n.type === "CallExpression" && n.callee.type === "Identifier") {
|
|
44
|
-
const a = n.arguments;
|
|
45
|
-
if (n.callee.name === "_ssrRenderComponent") {
|
|
46
|
-
const child = resolve(a[0]);
|
|
47
|
-
if (child)
|
|
48
|
-
calls.push({ child, passesAttrs: mentions(a[1], "_attrs"), within: a[4] ? within : null });
|
|
49
|
-
// Slot content given to the child: its `_scopeId` is what the child's outlets pass.
|
|
50
|
-
walk(a[2], child);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
if (n.callee.name === "_ssrRenderSlot" || n.callee.name === "_ssrRenderSlotInner") {
|
|
54
|
-
const id = a[6];
|
|
55
|
-
const passes = !id || id.type === "NullLiteral" ? "none" : id.type === "Identifier" && id.name === "_scopeId" ? "forward" : "id";
|
|
56
|
-
outlets.push({ passes, within });
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
for (const [key, v] of Object.entries(n))
|
|
60
|
-
if (key !== "loc" && key !== "start" && key !== "end")
|
|
61
|
-
walk(v, within);
|
|
62
|
-
};
|
|
63
|
-
walk(parseJs(ssr, { sourceType: "module" }).program, null);
|
|
64
|
-
return { calls, outlets };
|
|
65
|
-
}
|
|
66
|
-
/** Set every component's `inherits` and `passesSlotIds`, each a least fixed point: a component's
|
|
67
|
-
* slot content may be given an id when one of its outlets passes one, or forwards what the slot
|
|
68
|
-
* content it sits in was given; a root may be handed ids by a scoped parent, by a parent that
|
|
69
|
-
* passes on what it inherits, or by slot content that may be given an id. */
|
|
70
|
-
export function scopeFlow(read) {
|
|
71
|
-
const flows = read.map((r) => ({ comp: r.comp, ...flowOf(r.comp, r.ssr, r.children) }));
|
|
72
|
-
const byName = new Map(read.map((r) => [r.comp.name, r.comp]));
|
|
73
|
-
const passes = (name) => (name !== null && byName.get(name)?.passesSlotIds) ?? false;
|
|
74
|
-
for (let changed = true; changed;) {
|
|
75
|
-
changed = false;
|
|
76
|
-
for (const f of flows) {
|
|
77
|
-
if (!f.comp.passesSlotIds && f.outlets.some((o) => o.passes === "id" || (o.passes === "forward" && passes(o.within)))) {
|
|
78
|
-
f.comp.passesSlotIds = changed = true;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
for (let changed = true; changed;) {
|
|
83
|
-
changed = false;
|
|
84
|
-
for (const f of flows) {
|
|
85
|
-
for (const c of f.calls) {
|
|
86
|
-
const child = byName.get(c.child);
|
|
87
|
-
if (!child || child.inherits)
|
|
88
|
-
continue;
|
|
89
|
-
if (f.comp.scopeId !== null || (c.passesAttrs && f.comp.inherits && child.inheritAttrs) || passes(c.within)) {
|
|
90
|
-
child.inherits = changed = true;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/** Whether `n` is the template's `$attrs`: `_ctx.$attrs`, or a setup binding of `useAttrs()`. */
|
|
97
|
-
export function isDollarAttrs(n, bindings) {
|
|
98
|
-
if (n?.type !== "MemberExpression" || n.object.type !== "Identifier")
|
|
99
|
-
return false;
|
|
100
|
-
const prop = n.computed ? n.property.value : n.property.name;
|
|
101
|
-
if (n.object.name === "_ctx")
|
|
102
|
-
return prop === "$attrs";
|
|
103
|
-
return n.object.name === "$setup" && prop !== undefined && bindings.has(prop);
|
|
104
|
-
}
|
|
105
|
-
/** Whether a key a parent passes a child reaches it as an attribute, unless it is a prop: not one
|
|
106
|
-
* Vue reserves, not a listener (which the server drops), not a model's modifiers. */
|
|
107
|
-
export function passedKey(child, key) {
|
|
108
|
-
if (["", "key", "ref", "ref_for", "ref_key", "innerHTML", "textContent"].includes(key) || /^on[^a-z]/.test(key))
|
|
109
|
-
return false;
|
|
110
|
-
return !(key.endsWith("Modifiers") && declares(child, key === "modelModifiers" ? "modelValue" : key.slice(0, -"Modifiers".length)));
|
|
111
|
-
}
|
|
112
|
-
/** Set every component's `attrNames` and `idsInAttrs`: the attributes its parents' calls pass it
|
|
113
|
-
* beyond its props, and those passed on to it — by a parent whose root it is, or that binds
|
|
114
|
-
* `$attrs` to it — from that parent's own, a least fixed point. Runs after `scopeFlow`. */
|
|
115
|
-
export function attrsFlow(read) {
|
|
116
|
-
const byName = new Map(read.map((r) => [r.comp.name, r.comp]));
|
|
117
|
-
const flows = read.map((r) => {
|
|
118
|
-
const calls = [];
|
|
119
|
-
const self = new Set();
|
|
120
|
-
const walk = (n) => {
|
|
121
|
-
if (!n || typeof n !== "object")
|
|
122
|
-
return;
|
|
123
|
-
if (Array.isArray(n)) {
|
|
124
|
-
n.forEach(walk);
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
if (n.type === "VariableDeclarator" && n.init?.type === "CallExpression" && n.init.callee.name === "_resolveComponent") {
|
|
128
|
-
if (n.init.arguments[0]?.value === r.comp.name && n.init.arguments[1])
|
|
129
|
-
self.add(n.id.name);
|
|
130
|
-
}
|
|
131
|
-
if (n.type === "CallExpression" && n.callee.type === "Identifier" && n.callee.name === "_ssrRenderComponent") {
|
|
132
|
-
const target = n.arguments[0];
|
|
133
|
-
const child = target?.type === "MemberExpression" && target.object.name === "$setup"
|
|
134
|
-
? r.children.get(target.computed ? target.property.value : target.property.name)
|
|
135
|
-
: target?.type === "Identifier" && self.has(target.name)
|
|
136
|
-
? r.comp.name
|
|
137
|
-
: undefined;
|
|
138
|
-
const props = n.arguments[1];
|
|
139
|
-
const parts = props?.type === "CallExpression" && props.callee.name === "_mergeProps" ? props.arguments : props ? [props] : [];
|
|
140
|
-
if (child) {
|
|
141
|
-
calls.push({
|
|
142
|
-
child,
|
|
143
|
-
keys: parts.filter((p) => p.type === "ObjectExpression").flatMap((p) => p.properties.filter((q) => q.type === "ObjectProperty" && !q.computed).map((q) => q.key.name ?? String(q.key.value))),
|
|
144
|
-
passesAttrs: parts.some((p) => p.type === "Identifier" && p.name === "_attrs"),
|
|
145
|
-
passesDollar: parts.some((p) => isDollarAttrs(p, r.attrsBindings)),
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
for (const [key, v] of Object.entries(n))
|
|
150
|
-
if (key !== "loc" && key !== "start" && key !== "end")
|
|
151
|
-
walk(v);
|
|
152
|
-
};
|
|
153
|
-
walk(parseJs(r.ssr, { sourceType: "module" }).program);
|
|
154
|
-
return { comp: r.comp, calls };
|
|
155
|
-
});
|
|
156
|
-
for (let changed = true; changed;) {
|
|
157
|
-
changed = false;
|
|
158
|
-
for (const f of flows) {
|
|
159
|
-
for (const c of f.calls) {
|
|
160
|
-
const child = byName.get(c.child);
|
|
161
|
-
if (!child)
|
|
162
|
-
continue;
|
|
163
|
-
const names = c.keys.filter((k) => passedKey(child, k));
|
|
164
|
-
if ((c.passesAttrs && f.comp.inheritAttrs) || c.passesDollar)
|
|
165
|
-
names.push(...f.comp.attrNames);
|
|
166
|
-
for (const name of names) {
|
|
167
|
-
if (declares(child, name) || child.attrNames.has(name))
|
|
168
|
-
continue;
|
|
169
|
-
child.attrNames.add(name);
|
|
170
|
-
changed = true;
|
|
171
|
-
}
|
|
172
|
-
if (c.passesAttrs && f.comp.inherits && !child.idsInAttrs)
|
|
173
|
-
child.idsInAttrs = changed = true;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
package/dist/stores.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { type Component } from "./model.ts";
|
|
2
|
-
/** The store module an import names, resolved and without its extension, when it is one. */
|
|
3
|
-
export declare function storeImport(comp: Component, from: string): string | null;
|
|
4
|
-
/** Every store in the configured directory, and the interfaces their files declare. */
|
|
5
|
-
export declare function readStores(root: string, dir: string): void;
|
|
6
|
-
export declare function storeHome(file: string): Component;
|
package/dist/stores.js
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
/* Pinia option stores: their state, its types, and their getters. */
|
|
2
|
-
import { parse as parseJs } from "@babel/parser";
|
|
3
|
-
import { readdirSync, readFileSync } from "node:fs";
|
|
4
|
-
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
5
|
-
import { BOOL, fail, FLOAT, INT, opt, sameTy, snake, STR, tagAst, UNDEF } from "./model.js";
|
|
6
|
-
import { ctx } from "./context.js";
|
|
7
|
-
import { markStore, structOf, tyOfTs, typesImports } from "./typescript.js";
|
|
8
|
-
import { patternNames, setupStatement } from "./script.js";
|
|
9
|
-
/** The store module an import names, resolved and without its extension, when it is one. */
|
|
10
|
-
export function storeImport(comp, from) {
|
|
11
|
-
if (!from.startsWith(".") || ctx.stores.size === 0)
|
|
12
|
-
return null;
|
|
13
|
-
const target = resolve(ctx.rootDir, dirname(comp.file), from).replace(/\.ts$/, "");
|
|
14
|
-
return [...ctx.stores.values()].some((s) => s.module === target) ? target : null;
|
|
15
|
-
}
|
|
16
|
-
/** Every store in the configured directory, and the interfaces their files declare. */
|
|
17
|
-
export function readStores(root, dir) {
|
|
18
|
-
ctx.stores = new Map();
|
|
19
|
-
ctx.storeStructs = new Map();
|
|
20
|
-
ctx.storeFiles = new Map();
|
|
21
|
-
const files = readdirSync(join(root, dir)).filter((f) => f.endsWith(".ts") && !f.endsWith(".test.ts")).toSorted();
|
|
22
|
-
for (const f of files) {
|
|
23
|
-
const path = join(root, dir, f);
|
|
24
|
-
const comp = storeHome(relative(root, path));
|
|
25
|
-
comp.source = readFileSync(path, "utf8");
|
|
26
|
-
const ast = parseJs(comp.source, { sourceType: "module", plugins: ["typescript"] }).program.body;
|
|
27
|
-
tagAst(ast, "source");
|
|
28
|
-
typesImports(comp, ast);
|
|
29
|
-
const decls = ast.map((st) => (st.type === "ExportNamedDeclaration" ? st.declaration : st)).filter(Boolean);
|
|
30
|
-
const interfaces = decls.filter((d) => d.type === "TSInterfaceDeclaration");
|
|
31
|
-
for (const d of interfaces) {
|
|
32
|
-
if (ctx.storeStructs.has(d.id.name))
|
|
33
|
-
fail(comp, `\`${d.id.name}\` is declared by another store too`, d);
|
|
34
|
-
ctx.storeStructs.set(d.id.name, { name: d.id.name, fields: [] });
|
|
35
|
-
ctx.storeFiles.set(d.id.name, comp.file);
|
|
36
|
-
}
|
|
37
|
-
for (const d of interfaces) {
|
|
38
|
-
const st = structOf(comp, d.id.name, d.body.body, ctx.storeStructs);
|
|
39
|
-
for (const field of st.fields)
|
|
40
|
-
field.ty = markStore(field.ty);
|
|
41
|
-
ctx.storeStructs.set(d.id.name, st);
|
|
42
|
-
}
|
|
43
|
-
for (const d of decls) {
|
|
44
|
-
if (d.type !== "VariableDeclaration")
|
|
45
|
-
continue;
|
|
46
|
-
for (const v of d.declarations) {
|
|
47
|
-
const init = v.init;
|
|
48
|
-
if (init?.type !== "CallExpression" || init.callee.type !== "Identifier" || init.callee.name !== "defineStore")
|
|
49
|
-
continue;
|
|
50
|
-
const [id, options] = init.arguments;
|
|
51
|
-
if (id?.type !== "StringLiteral")
|
|
52
|
-
fail(comp, "a store's id is a string literal", init);
|
|
53
|
-
if (options?.type === "ArrowFunctionExpression" || options?.type === "FunctionExpression") {
|
|
54
|
-
setupStore(comp, v.id.name, id.value, options, path);
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (options?.type !== "ObjectExpression")
|
|
58
|
-
fail(comp, "a store is `defineStore(id, { state, getters, actions })` or `defineStore(id, () => { … })`", init);
|
|
59
|
-
const state = options.properties.find((p) => (p.key?.name ?? p.key?.value) === "state");
|
|
60
|
-
const fn = state?.value ?? (state?.type === "ObjectMethod" ? state : null);
|
|
61
|
-
const ret = fn?.returnType?.typeAnnotation;
|
|
62
|
-
if (ret?.type !== "TSTypeReference" || !ctx.storeStructs.has(ret.typeName.name)) {
|
|
63
|
-
fail(comp, "a store's `state` declares its return type, an interface in the same file: `state: (): State => ({ … })`", state ?? init);
|
|
64
|
-
}
|
|
65
|
-
const getters = new Map();
|
|
66
|
-
const getterObj = options.properties.find((p) => (p.key?.name ?? p.key?.value) === "getters");
|
|
67
|
-
if (getterObj && getterObj.value?.type !== "ObjectExpression")
|
|
68
|
-
fail(comp, "a store's `getters` is an object literal", getterObj);
|
|
69
|
-
for (const g of getterObj?.value.properties ?? []) {
|
|
70
|
-
const getterFn = g.type === "ObjectMethod" ? g : g.value;
|
|
71
|
-
const name = g.key?.name ?? g.key?.value;
|
|
72
|
-
if (!getterFn || !["ArrowFunctionExpression", "FunctionExpression", "ObjectMethod"].includes(getterFn.type)) {
|
|
73
|
-
fail(comp, `getter \`${name}\` is a function of the state`, g);
|
|
74
|
-
}
|
|
75
|
-
let body = getterFn.body;
|
|
76
|
-
if (body.type === "BlockStatement") {
|
|
77
|
-
const only = body.body.length === 1 ? body.body[0] : null;
|
|
78
|
-
body = only?.type === "ReturnStatement" ? only.argument : null;
|
|
79
|
-
}
|
|
80
|
-
const param = getterFn.params[0];
|
|
81
|
-
// A getter the server cannot translate fails where a component reads it, not here.
|
|
82
|
-
getters.set(name, { param: param?.type === "Identifier" ? param.name : null, body, file: comp.file });
|
|
83
|
-
}
|
|
84
|
-
ctx.stores.set(v.id.name, {
|
|
85
|
-
hook: v.id.name,
|
|
86
|
-
id: id.value,
|
|
87
|
-
field: snake(id.value),
|
|
88
|
-
state: ret.typeName.name,
|
|
89
|
-
module: path.replace(/\.ts$/, ""),
|
|
90
|
-
getters,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/** A store file, as the thing its types are declared in: what errors name, and where they resolve. */
|
|
97
|
-
/** A setup store: \`defineStore(id, () => { …; return { … } })\`. The refs it returns are its state —
|
|
98
|
-
* typed by \`ref<T>(…)\`, or by the literal they start from — the computeds it returns its getters,
|
|
99
|
-
* and its functions actions, which only the client runs. */
|
|
100
|
-
function setupStore(comp, hook, id, fn, path) {
|
|
101
|
-
if (fn.body.type !== "BlockStatement")
|
|
102
|
-
fail(comp, "a setup store's function returns its state from a block: `() => { …; return { … } }`", fn);
|
|
103
|
-
const refs = new Map();
|
|
104
|
-
const computeds = new Map();
|
|
105
|
-
const others = new Set();
|
|
106
|
-
let returned = null;
|
|
107
|
-
for (const st of fn.body.body) {
|
|
108
|
-
if (st.type === "ReturnStatement") {
|
|
109
|
-
returned = st.argument;
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
if (st.type === "FunctionDeclaration") {
|
|
113
|
-
if (st.id)
|
|
114
|
-
others.add(st.id.name);
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
if (st.type === "ExpressionStatement") {
|
|
118
|
-
setupStatement(comp, st);
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (st.type !== "VariableDeclaration")
|
|
122
|
-
fail(comp, `\`${st.type}\` in a setup store is not supported`, st);
|
|
123
|
-
for (const d of st.declarations) {
|
|
124
|
-
if (d.id.type !== "Identifier") {
|
|
125
|
-
for (const name of patternNames(d.id))
|
|
126
|
-
others.add(name);
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
const init = d.init;
|
|
130
|
-
const callee = init?.type === "CallExpression" && init.callee.type === "Identifier" ? init.callee.name : null;
|
|
131
|
-
if (callee === "ref" || callee === "shallowRef") {
|
|
132
|
-
refs.set(d.id.name, refType(comp, init));
|
|
133
|
-
}
|
|
134
|
-
else if (callee === "computed") {
|
|
135
|
-
const getter = init.arguments[0];
|
|
136
|
-
let body = getter?.body;
|
|
137
|
-
if (body?.type === "BlockStatement") {
|
|
138
|
-
const only = body.body.length === 1 ? body.body[0] : null;
|
|
139
|
-
body = only?.type === "ReturnStatement" ? only.argument : null;
|
|
140
|
-
}
|
|
141
|
-
computeds.set(d.id.name, body);
|
|
142
|
-
}
|
|
143
|
-
else
|
|
144
|
-
others.add(d.id.name);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if (returned?.type !== "ObjectExpression")
|
|
148
|
-
fail(comp, "a setup store returns an object of its state, getters and actions", returned ?? fn);
|
|
149
|
-
const state = [];
|
|
150
|
-
const getters = new Map();
|
|
151
|
-
for (const p of returned.properties) {
|
|
152
|
-
if (p.type !== "ObjectProperty" || p.computed || p.value.type !== "Identifier")
|
|
153
|
-
fail(comp, "a setup store returns plain names: `{ count, doubled, increment }`", p);
|
|
154
|
-
const key = p.key.name ?? p.key.value;
|
|
155
|
-
const local = p.value.name;
|
|
156
|
-
if (refs.has(local))
|
|
157
|
-
state.push({ js: key, rust: snake(key), ty: markStore(refs.get(local)) });
|
|
158
|
-
else if (computeds.has(local))
|
|
159
|
-
getters.set(key, { param: null, body: computeds.get(local), file: comp.file, setup: true });
|
|
160
|
-
else if (!others.has(local))
|
|
161
|
-
fail(comp, `\`${local}\` is not declared in the store's function`, p);
|
|
162
|
-
}
|
|
163
|
-
const name = id.split(/[-_]/).map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join("") + "State";
|
|
164
|
-
if (ctx.storeStructs.has(name))
|
|
165
|
-
fail(comp, `\`${name}\` names this setup store's state; rename the interface`, fn);
|
|
166
|
-
ctx.storeStructs.set(name, { name, fields: state });
|
|
167
|
-
ctx.storeFiles.set(name, comp.file);
|
|
168
|
-
ctx.stores.set(hook, { hook, id, field: snake(id), state: name, module: path.replace(/\.ts$/, ""), getters });
|
|
169
|
-
}
|
|
170
|
-
/** A setup store ref's type: \`ref<T>(…)\`, or what its initial literal is; optional when it has none. */
|
|
171
|
-
function refType(comp, init) {
|
|
172
|
-
const typed = init.typeParameters?.params?.[0];
|
|
173
|
-
const value = init.arguments[0];
|
|
174
|
-
if (typed) {
|
|
175
|
-
const ty = tyOfTs(comp, typed, ctx.storeStructs);
|
|
176
|
-
return value ? ty : opt(ty);
|
|
177
|
-
}
|
|
178
|
-
const literal = (n) => {
|
|
179
|
-
if (n?.type === "StringLiteral" || (n?.type === "TemplateLiteral" && n.expressions.length === 0))
|
|
180
|
-
return STR;
|
|
181
|
-
if (n?.type === "NumericLiteral")
|
|
182
|
-
return Number.isInteger(n.value) ? INT : FLOAT;
|
|
183
|
-
if (n?.type === "UnaryExpression" && n.operator === "-")
|
|
184
|
-
return literal(n.argument);
|
|
185
|
-
if (n?.type === "BooleanLiteral")
|
|
186
|
-
return BOOL;
|
|
187
|
-
if (n?.type === "ArrayExpression" && n.elements.length) {
|
|
188
|
-
const of = literal(n.elements[0]);
|
|
189
|
-
return of && n.elements.every((el) => sameTy(literal(el) ?? UNDEF, of)) ? { k: "list", of } : null;
|
|
190
|
-
}
|
|
191
|
-
return null;
|
|
192
|
-
};
|
|
193
|
-
return literal(value) ?? fail(comp, "a setup store's ref declares its type: `ref<string[]>([])`", init);
|
|
194
|
-
}
|
|
195
|
-
export function storeHome(file) {
|
|
196
|
-
return {
|
|
197
|
-
name: basename(file),
|
|
198
|
-
module: "stores",
|
|
199
|
-
file,
|
|
200
|
-
props: { name: "Props", fields: [] },
|
|
201
|
-
structs: ctx.storeStructs,
|
|
202
|
-
trustedName: null,
|
|
203
|
-
floatName: null,
|
|
204
|
-
childProps: new Map(),
|
|
205
|
-
imports: new Set(),
|
|
206
|
-
slotNames: [],
|
|
207
|
-
routerView: false,
|
|
208
|
-
routerLink: false,
|
|
209
|
-
readsRoute: false,
|
|
210
|
-
usesRoute: false,
|
|
211
|
-
readsStores: false,
|
|
212
|
-
usesStores: false,
|
|
213
|
-
readsI18n: false,
|
|
214
|
-
usesI18n: false,
|
|
215
|
-
readsTeleports: false,
|
|
216
|
-
usesTeleports: false,
|
|
217
|
-
models: new Map(),
|
|
218
|
-
aliases: new Map(),
|
|
219
|
-
importedTypes: new Map(),
|
|
220
|
-
slotShapes: new Map(),
|
|
221
|
-
scopeId: null,
|
|
222
|
-
slotted: false,
|
|
223
|
-
inheritAttrs: true,
|
|
224
|
-
inherits: false,
|
|
225
|
-
passesSlotIds: false,
|
|
226
|
-
attrNames: new Set(),
|
|
227
|
-
idsInAttrs: false,
|
|
228
|
-
};
|
|
229
|
-
}
|