paseo-plugin-genui 0.1.3 → 0.1.5
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/catalog.ts +2 -0
- package/shared/core/evaluate.ts +27 -1
- package/shared/core/validate.ts +8 -5
- package/shared/genui.test.ts +24 -0
- package/shared/instructions.ts +1 -1
package/package.json
CHANGED
package/shared/core/catalog.ts
CHANGED
package/shared/core/evaluate.ts
CHANGED
|
@@ -287,7 +287,7 @@ const Interpreter = defineClass(
|
|
|
287
287
|
case "arrow":
|
|
288
288
|
throw new RuntimeError(
|
|
289
289
|
expr.pos,
|
|
290
|
-
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some or
|
|
290
|
+
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some, every or sort",
|
|
291
291
|
);
|
|
292
292
|
}
|
|
293
293
|
},
|
|
@@ -519,6 +519,10 @@ const Interpreter = defineClass(
|
|
|
519
519
|
for (const child of item) visit(child);
|
|
520
520
|
} else if (isNode(item)) {
|
|
521
521
|
out.push(item);
|
|
522
|
+
} else if (isAction(item) && item.type !== "set") {
|
|
523
|
+
// A bare action where a component belongs: render it as the Button the agent meant.
|
|
524
|
+
const text = item.type === "openUrl" ? `Open ${new URL(item.url).host}` : item.type === "copy" ? "Copy" : item.text;
|
|
525
|
+
out.push(this.node("Button", `${key}.${out.length}`, { label: text, action: [item], variant: null, icon: null }, pos));
|
|
522
526
|
} else {
|
|
523
527
|
throw new RuntimeError(pos, `${label} must be components or text, got ${describe(item)}`);
|
|
524
528
|
}
|
|
@@ -653,6 +657,7 @@ const Interpreter = defineClass(
|
|
|
653
657
|
method(call: Extract<Expr, { type: "method" }>, scope: Scope | null): Value {
|
|
654
658
|
const receiver = this.expr(call.object, scope);
|
|
655
659
|
if (isList(receiver) && LAMBDA_METHODS.has(call.name)) return this.lambdaMethod(receiver, call, scope);
|
|
660
|
+
if (isList(receiver) && call.name === "sort") return this.sort(receiver, call, scope);
|
|
656
661
|
const args = call.args.map((arg) => this.expr(arg, scope));
|
|
657
662
|
const label = `.${call.name}()`;
|
|
658
663
|
const textAt = (index: number) => this.text(args[index], call.args[index].pos, label);
|
|
@@ -670,6 +675,8 @@ const Interpreter = defineClass(
|
|
|
670
675
|
return receiver.includes(args[0]);
|
|
671
676
|
case "indexOf":
|
|
672
677
|
return receiver.indexOf(args[0]);
|
|
678
|
+
case "concat":
|
|
679
|
+
return checkLength(receiver.concat(...args), call.pos);
|
|
673
680
|
}
|
|
674
681
|
} else if (typeof receiver === "string") {
|
|
675
682
|
switch (call.name) {
|
|
@@ -697,6 +704,25 @@ const Interpreter = defineClass(
|
|
|
697
704
|
throw new RuntimeError(call.pos, `\`${label}\` is not available on ${describe(receiver)}`);
|
|
698
705
|
},
|
|
699
706
|
|
|
707
|
+
/** A sorted copy; without a comparator, numbers sort numerically and anything else as text. */
|
|
708
|
+
sort(list: readonly Value[], call: Extract<Expr, { type: "method" }>, scope: Scope | null): Value {
|
|
709
|
+
checkLength(list, call.pos);
|
|
710
|
+
const fn = call.args[0];
|
|
711
|
+
if (fn === undefined) {
|
|
712
|
+
return [...list].sort((a, b) =>
|
|
713
|
+
typeof a === "number" && typeof b === "number" ? a - b : toText(a, call.pos).localeCompare(toText(b, call.pos)),
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
if (fn.type !== "arrow") throw new RuntimeError(call.pos, "`.sort()` takes a function, as in `(a, b) => a.price - b.price`");
|
|
717
|
+
const [aName, bName] = fn.params;
|
|
718
|
+
return [...list].sort((a, b) => {
|
|
719
|
+
let inner = scope;
|
|
720
|
+
if (aName !== undefined) inner = { name: aName, value: a, parent: inner };
|
|
721
|
+
if (bName !== undefined) inner = { name: bName, value: b, parent: inner };
|
|
722
|
+
return this.number(this.expr(fn.body, inner), fn.body.pos, ".sort()");
|
|
723
|
+
});
|
|
724
|
+
},
|
|
725
|
+
|
|
700
726
|
lambdaMethod(list: readonly Value[], call: Extract<Expr, { type: "method" }>, scope: Scope | null): Value {
|
|
701
727
|
checkLength(list, call.pos);
|
|
702
728
|
const fn = call.args[0];
|
package/shared/core/validate.ts
CHANGED
|
@@ -31,7 +31,7 @@ type Arity = readonly [min: number, max: number];
|
|
|
31
31
|
const METHOD_ARITY: ReadonlyMap<string, Arity> = new Map(
|
|
32
32
|
Object.entries({
|
|
33
33
|
map: [1, 1], filter: [1, 1], find: [1, 1], some: [1, 1], every: [1, 1],
|
|
34
|
-
join: [0, 1], slice: [0, 2], includes: [1, 1], indexOf: [1, 1],
|
|
34
|
+
join: [0, 1], slice: [0, 2], includes: [1, 1], indexOf: [1, 1], concat: [1, Infinity], sort: [0, 1],
|
|
35
35
|
split: [1, 1], trim: [0, 0], toUpperCase: [0, 0], toLowerCase: [0, 0],
|
|
36
36
|
startsWith: [1, 1], endsWith: [1, 1], toFixed: [0, 1],
|
|
37
37
|
} satisfies Record<MethodName, Arity>),
|
|
@@ -46,7 +46,7 @@ const FUNCTION_ARITY: ReadonlyMap<string, Arity> = new Map(
|
|
|
46
46
|
|
|
47
47
|
const ACTION_LIST = [...ACTION_SPECS].map(([name, spec]) => signature(name, spec)).join(", ");
|
|
48
48
|
const ARROW_ONLY =
|
|
49
|
-
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some or
|
|
49
|
+
"arrow functions can only be declared as a named function (`share = p => ...`) or passed to map, filter, find, some, every or sort";
|
|
50
50
|
|
|
51
51
|
/** The type of a value, as named in diagnostics. */
|
|
52
52
|
export type Kind = "null" | "boolean" | "number" | "string" | "list" | "object" | "node" | "action" | "binding";
|
|
@@ -432,13 +432,16 @@ const Validator = defineClass(
|
|
|
432
432
|
this.diagnostics.report(expr.pos, `\`.${expr.name}()\` takes ${arityText(arity)}`);
|
|
433
433
|
}
|
|
434
434
|
const [first, ...rest] = expr.args;
|
|
435
|
-
|
|
435
|
+
const sort = expr.name === "sort";
|
|
436
|
+
if ((LAMBDA_METHODS.has(expr.name) || sort) && first !== undefined) {
|
|
436
437
|
if (first.type !== "arrow") {
|
|
437
|
-
|
|
438
|
+
const example = sort ? "(a, b) => a.price - b.price" : "x => x.price";
|
|
439
|
+
this.diagnostics.report(first.pos, `\`.${expr.name}()\` takes a function, as in \`${example}\``);
|
|
438
440
|
this.check(first, params, refs);
|
|
439
441
|
} else {
|
|
440
442
|
if (first.params.length > 2) {
|
|
441
|
-
|
|
443
|
+
const passes = sort ? "(a, b)" : "(item, index)";
|
|
444
|
+
this.diagnostics.report(first.pos, `\`.${expr.name}()\` passes ${passes}; use at most 2 parameters`);
|
|
442
445
|
}
|
|
443
446
|
this.check(first.body, [...params, ...first.params], refs);
|
|
444
447
|
}
|
package/shared/genui.test.ts
CHANGED
|
@@ -45,6 +45,30 @@ describe("paseo-plugin-genui core, instructions, and gallery", () => {
|
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
+
it("supports .concat() and .sort() on lists", () => {
|
|
49
|
+
const compiled = compile(
|
|
50
|
+
[
|
|
51
|
+
"a = [{n: \"x\", v: 3}, {n: \"y\", v: 1}]",
|
|
52
|
+
"all = a.concat([{n: \"z\", v: 2}])",
|
|
53
|
+
"Text(all.sort((p, q) => q.v - p.v).map(p => p.n).join(\",\"))",
|
|
54
|
+
"Text([10, 9, 1].sort().join(\",\"))",
|
|
55
|
+
"Text(a.map(p => p.n).join(\",\"))",
|
|
56
|
+
].join("\n"),
|
|
57
|
+
);
|
|
58
|
+
expect(compiled.diagnostics).toEqual([]);
|
|
59
|
+
const { roots } = evaluate(compiled.program!, initialState(compiled.program!));
|
|
60
|
+
expect(roots.map((root) => root.props.text)).toEqual(["x,z,y", "1,9,10", "x,y"]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("renders a bare Send, OpenUrl or Copy among children as a Button", () => {
|
|
64
|
+
const compiled = compile('Card("x", [Text("a"), OpenUrl("https://docs.google.com/x"), Send("Go"), Copy("id")])');
|
|
65
|
+
expect(compiled.diagnostics.filter((d) => d.severity === "error")).toEqual([]);
|
|
66
|
+
const { roots, diagnostics } = evaluate(compiled.program!, initialState(compiled.program!));
|
|
67
|
+
expect(diagnostics).toEqual([]);
|
|
68
|
+
const buttons = (roots[0]!.props.children as { type: string; props: { label: string } }[]).filter((c) => c.type === "Button");
|
|
69
|
+
expect(buttons.map((b) => b.props.label)).toEqual(["Open docs.google.com", "Go", "Copy"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
48
72
|
it("covers all 86 catalog components in the gallery and compiles + evaluates every gallery spec", async () => {
|
|
49
73
|
const files = await listGallery();
|
|
50
74
|
expect(files.length).toBeGreaterThanOrEqual(170);
|
package/shared/instructions.ts
CHANGED
|
@@ -24,7 +24,7 @@ export const REFERENCE_MORE_GROUPS: readonly ComponentGroup[] = ["Inputs", "Widg
|
|
|
24
24
|
|
|
25
25
|
export const UI_REFERENCE = `# Inline UI reference
|
|
26
26
|
The \`\`\`ui language used by the Inline UI section below.
|
|
27
|
-
Syntax: one statement per line. \`$x = 8\` is state inputs bind to; \`name = expr\` names a value and \`f = (a, b) => expr\` a function; each bare expression renders. JS expressions incl. ** ?: ?? \`\${x}\` [lists] {objects} x.y map/filter/find/join/length, plus round(x, d), fmt(x, d), sum, min, max, abs, entries, Load(path). Named args go last: Slider($n, 0, 10, label: "N").
|
|
27
|
+
Syntax: one statement per line. \`$x = 8\` is state inputs bind to; \`name = expr\` names a value and \`f = (a, b) => expr\` a function; each bare expression renders. JS expressions incl. ** ?: ?? \`\${x}\` [lists] {objects} x.y map/filter/find/sort/concat/join/length, plus round(x, d), fmt(x, d), sum, min, max, abs, entries, Load(path). Named args go last: Slider($n, 0, 10, label: "N").
|
|
28
28
|
Components (? optional; tone: neutral|good|warn|bad|info if meaningful; more in the next section):
|
|
29
29
|
${componentLines(REFERENCE_GROUPS)}
|
|
30
30
|
Actions: ${actionList}; a list runs in order; Send posts as the user.
|