paseo-plugin-genui 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/shared/core/validate.ts +75 -0
- package/shared/genui.test.ts +22 -0
package/package.json
CHANGED
package/shared/core/validate.ts
CHANGED
|
@@ -130,6 +130,63 @@ export function coerceState(holds: StateKind | undefined, value: Value): Value |
|
|
|
130
130
|
return undefined;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
type StateExpr = Extract<Expr, { type: "state" }>;
|
|
134
|
+
|
|
135
|
+
/** The empty value an input starts with when its state was never declared. */
|
|
136
|
+
function emptyState(holds: StateKind | undefined): readonly [] | number | boolean | string {
|
|
137
|
+
return holds === "list" ? [] : holds === "number" ? 0 : holds === "boolean" ? false : "";
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function children(expr: Expr): readonly Expr[] {
|
|
141
|
+
switch (expr.type) {
|
|
142
|
+
case "literal":
|
|
143
|
+
case "state":
|
|
144
|
+
case "ident":
|
|
145
|
+
return [];
|
|
146
|
+
case "template":
|
|
147
|
+
return expr.parts;
|
|
148
|
+
case "list":
|
|
149
|
+
return expr.items;
|
|
150
|
+
case "object":
|
|
151
|
+
return expr.entries.flatMap((entry) => (typeof entry.key === "object" && entry.key !== null ? [entry.key, entry.value] : [entry.value]));
|
|
152
|
+
case "spread":
|
|
153
|
+
return [expr.value];
|
|
154
|
+
case "unary":
|
|
155
|
+
return [expr.operand];
|
|
156
|
+
case "binary":
|
|
157
|
+
return [expr.left, expr.right];
|
|
158
|
+
case "conditional":
|
|
159
|
+
return [expr.test, expr.then, expr.else];
|
|
160
|
+
case "member":
|
|
161
|
+
return [expr.object];
|
|
162
|
+
case "index":
|
|
163
|
+
return [expr.object, expr.index];
|
|
164
|
+
case "call":
|
|
165
|
+
return [...expr.args, ...expr.named.map((arg) => arg.value)];
|
|
166
|
+
case "method":
|
|
167
|
+
return [expr.object, ...expr.args, ...expr.named.map((arg) => arg.value)];
|
|
168
|
+
case "arrow":
|
|
169
|
+
return [expr.body];
|
|
170
|
+
case "let":
|
|
171
|
+
return [expr.value, expr.body];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Every `$name` passed as an input's binding, in source order, with what that input holds. */
|
|
176
|
+
function inputBindings(expr: Expr): { state: StateExpr; holds: StateKind | undefined }[] {
|
|
177
|
+
const found: { state: StateExpr; holds: StateKind | undefined }[] = [];
|
|
178
|
+
const spec = expr.type === "call" ? COMPONENT_SPECS.get(expr.name) : undefined;
|
|
179
|
+
if (expr.type === "call" && spec !== undefined) {
|
|
180
|
+
spec.params.forEach((param, index) => {
|
|
181
|
+
if (param.kind !== "binding") return;
|
|
182
|
+
const slot = expr.named.find((arg) => arg.name === param.name)?.value ?? expr.args[index];
|
|
183
|
+
if (slot?.type === "state") found.push({ state: slot, holds: param.holds });
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
for (const child of children(expr)) found.push(...inputBindings(child));
|
|
187
|
+
return found;
|
|
188
|
+
}
|
|
189
|
+
|
|
133
190
|
/** Params whose literal arguments can be type-checked before evaluation. */
|
|
134
191
|
const LITERAL_KINDS: Partial<Record<ParamKind, Kind>> = { list: "list", number: "number", boolean: "boolean" };
|
|
135
192
|
|
|
@@ -246,6 +303,24 @@ const Validator = defineClass(
|
|
|
246
303
|
}
|
|
247
304
|
}
|
|
248
305
|
|
|
306
|
+
// Agents often bind an input to `$name` without declaring it; declare it empty rather than fail the block.
|
|
307
|
+
const exprs = [...roots, ...[...this.named.values()].map((decl) => decl.value), ...stateDecls.map((decl) => decl.value)];
|
|
308
|
+
for (const slot of exprs.flatMap(inputBindings)) {
|
|
309
|
+
if (this.states.has(slot.state.name) || this.named.has(slot.state.name)) continue;
|
|
310
|
+
const empty = emptyState(slot.holds);
|
|
311
|
+
const { name, pos } = slot.state;
|
|
312
|
+
const value: Expr = typeof empty === "object" ? { type: "list", items: [], pos } : { type: "literal", value: empty, pos };
|
|
313
|
+
const decl: Decl = { name, value, pos };
|
|
314
|
+
this.states.set(decl.name, { index: stateDecls.length, decl });
|
|
315
|
+
stateDecls.push(decl);
|
|
316
|
+
const initial = JSON.stringify(empty);
|
|
317
|
+
this.diagnostics.report(
|
|
318
|
+
slot.state.pos,
|
|
319
|
+
`\`$${decl.name}\` was not declared, so it starts as ${initial}. Declare it first, as in \`$${decl.name} = ${initial}\`.`,
|
|
320
|
+
"warning",
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
249
324
|
const used = new Set<string>();
|
|
250
325
|
const namedRefs = new Map<string, Refs>();
|
|
251
326
|
for (const [name, decl] of this.named) {
|
package/shared/genui.test.ts
CHANGED
|
@@ -64,6 +64,28 @@ describe("paseo-plugin-genui core, instructions, and gallery", () => {
|
|
|
64
64
|
expect(roots.map((root) => root.props.text)).toEqual(["x,z,y", "1,9,10", "x,y"]);
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
it("declares state an input binds to but the block never declared", () => {
|
|
68
|
+
const compiled = compile(`Card("x", [
|
|
69
|
+
Text($org == "" ? "no org" : $org),
|
|
70
|
+
Form([
|
|
71
|
+
Radio($browser, [{value: "A", detail: "a"}], label: "Q1"),
|
|
72
|
+
Input($org, label: "Q2"),
|
|
73
|
+
Toggle($ok, "Q3"),
|
|
74
|
+
Checklist($picks, ["a", "b"]),
|
|
75
|
+
Slider($n, 0, 10)
|
|
76
|
+
], submit: "Send")
|
|
77
|
+
])`);
|
|
78
|
+
expect(compiled.diagnostics.filter((d) => d.severity === "error")).toEqual([]);
|
|
79
|
+
expect(compiled.diagnostics.filter((d) => d.severity === "warning")).toHaveLength(5);
|
|
80
|
+
expect(initialState(compiled.program!)).toEqual({ browser: "", org: "", ok: false, picks: [], n: 0 });
|
|
81
|
+
expect(evaluate(compiled.program!, initialState(compiled.program!)).diagnostics).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("still reports state that no input binds to", () => {
|
|
85
|
+
const compiled = compile('Text($missing)');
|
|
86
|
+
expect(compiled.diagnostics.some((d) => d.severity === "error" && d.message.includes("unknown state `$missing`"))).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
67
89
|
it("renders a bare Send, OpenUrl or Copy among children as a Button", () => {
|
|
68
90
|
const compiled = compile('Card("x", [Text("a"), OpenUrl("https://docs.google.com/x"), Send("Go"), Copy("id")])');
|
|
69
91
|
expect(compiled.diagnostics.filter((d) => d.severity === "error")).toEqual([]);
|