paseo-plugin-genui 0.1.0 → 0.1.2
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/client/components/content.tsx +27 -11
- package/client/components/image-lightbox.tsx +110 -0
- package/client/components/inputs.tsx +1 -1
- package/client/components/media.tsx +2 -2
- package/client/live-ui.tsx +2 -2
- package/client/node-renderer.tsx +37 -19
- package/client/web.ts +22 -0
- package/package.json +1 -1
- package/server/data-source.ts +94 -40
- package/shared/core/data.ts +9 -1
- package/shared/core/define-class.ts +36 -0
- package/shared/core/diagnostics.ts +24 -13
- package/shared/core/evaluate.ts +561 -525
- package/shared/core/junit.ts +9 -1
- package/shared/core/lexer.ts +187 -177
- package/shared/core/parser.ts +280 -263
- package/shared/core/validate.ts +366 -356
- package/shared/genui.test.ts +61 -1
- package/shared/hermes-syntax.test.ts +41 -0
package/shared/core/validate.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
import type { DiagnosticList, Pos } from "./diagnostics";
|
|
15
15
|
import { loadPath } from "./data";
|
|
16
16
|
import type { Value } from "./types";
|
|
17
|
+
import { defineClass } from "./define-class";
|
|
17
18
|
|
|
18
19
|
// Maps, not the catalog objects, so spec-controlled names like `constructor`
|
|
19
20
|
// never hit Object.prototype.
|
|
@@ -184,391 +185,400 @@ export function validate(statements: readonly Statement[], diagnostics: Diagnost
|
|
|
184
185
|
return new Validator(diagnostics).run(statements);
|
|
185
186
|
}
|
|
186
187
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
interface ValidatorFields {
|
|
189
|
+
states: Map<string, { readonly index: number; readonly decl: Decl }>;
|
|
190
|
+
named: Map<string, Decl>;
|
|
191
|
+
diagnostics: DiagnosticList;
|
|
192
|
+
}
|
|
192
193
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
194
|
+
const Validator = defineClass(
|
|
195
|
+
function (this: ValidatorFields, diagnostics: DiagnosticList): void {
|
|
196
|
+
this.diagnostics = diagnostics;
|
|
197
|
+
this.states = new Map<string, { readonly index: number; readonly decl: Decl }>();
|
|
198
|
+
this.named = new Map<string, Decl>();
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
run(statements: readonly Statement[]): Program {
|
|
202
|
+
const stateDecls: Decl[] = [];
|
|
203
|
+
const roots: Expr[] = [];
|
|
204
|
+
for (const statement of statements) {
|
|
205
|
+
if (statement.kind === "root") {
|
|
206
|
+
roots.push(statement.value);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
this.forbidden(statement.name, statement.pos);
|
|
210
|
+
const isState = statement.kind === "state";
|
|
211
|
+
const previous = isState ? this.states.get(statement.name)?.decl : this.named.get(statement.name);
|
|
212
|
+
if (previous !== undefined) {
|
|
213
|
+
const label = isState ? `$${statement.name}` : statement.name;
|
|
214
|
+
this.diagnostics.report(statement.pos, `\`${label}\` is already declared on line ${previous.pos.line}`);
|
|
215
|
+
} else if (isState) {
|
|
216
|
+
this.states.set(statement.name, { index: stateDecls.length, decl: statement });
|
|
217
|
+
stateDecls.push(statement);
|
|
218
|
+
} else {
|
|
219
|
+
this.named.set(statement.name, statement);
|
|
220
|
+
}
|
|
212
221
|
}
|
|
213
|
-
}
|
|
214
222
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
for (const root of roots) for (const ref of this.refs(root).named) used.add(ref.name);
|
|
223
|
-
|
|
224
|
-
const rootDecl = this.named.get("root");
|
|
225
|
-
if (roots.length === 0) {
|
|
226
|
-
if (rootDecl === undefined) {
|
|
227
|
-
this.diagnostics.report(
|
|
228
|
-
{ line: 1, column: 1 },
|
|
229
|
-
"Nothing to render: add a bare component expression such as Card(...)",
|
|
230
|
-
);
|
|
231
|
-
} else {
|
|
232
|
-
roots.push({ type: "ident", name: "root", pos: rootDecl.pos });
|
|
233
|
-
used.add("root");
|
|
223
|
+
const used = new Set<string>();
|
|
224
|
+
const namedRefs = new Map<string, Refs>();
|
|
225
|
+
for (const [name, decl] of this.named) {
|
|
226
|
+
const refs = this.declRefs(decl);
|
|
227
|
+
namedRefs.set(name, refs);
|
|
228
|
+
for (const ref of refs.named) if (ref.name !== name) used.add(ref.name);
|
|
234
229
|
}
|
|
235
|
-
|
|
230
|
+
for (const root of roots) for (const ref of this.refs(root).named) used.add(ref.name);
|
|
236
231
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const refs = this.refs(decl.value);
|
|
241
|
-
for (const ref of refs.named) used.add(ref.name);
|
|
242
|
-
for (const ref of refs.states) {
|
|
243
|
-
const target = this.states.get(ref.name);
|
|
244
|
-
if (target === undefined || target.index < index) continue;
|
|
245
|
-
this.diagnostics.report(
|
|
246
|
-
ref.pos,
|
|
247
|
-
target.index === index
|
|
248
|
-
? `\`$${decl.name}\` cannot use itself in its initial value`
|
|
249
|
-
: `\`$${ref.name}\` is declared after \`$${decl.name}\`; initial values can only use earlier state`,
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
for (const ref of refs.named) {
|
|
253
|
-
const late = [...statesOf(ref.name)].find((name) => (this.states.get(name)?.index ?? -1) >= index);
|
|
254
|
-
if (late !== undefined) {
|
|
232
|
+
const rootDecl = this.named.get("root");
|
|
233
|
+
if (roots.length === 0) {
|
|
234
|
+
if (rootDecl === undefined) {
|
|
255
235
|
this.diagnostics.report(
|
|
256
|
-
|
|
257
|
-
|
|
236
|
+
{ line: 1, column: 1 },
|
|
237
|
+
"Nothing to render: add a bare component expression such as Card(...)",
|
|
258
238
|
);
|
|
239
|
+
} else {
|
|
240
|
+
roots.push({ type: "ident", name: "root", pos: rootDecl.pos });
|
|
241
|
+
used.add("root");
|
|
259
242
|
}
|
|
260
243
|
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
for (const [name, decl] of this.named) {
|
|
264
|
-
if (!used.has(name)) this.diagnostics.report(decl.pos, `\`${name}\` is never used`, "warning");
|
|
265
|
-
}
|
|
266
|
-
return { states: stateDecls, named: this.named, roots };
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
private refs(expr: Expr): Refs {
|
|
270
|
-
const refs: Refs = { named: [], states: [] };
|
|
271
|
-
this.check(expr, [], refs);
|
|
272
|
-
return refs;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/** A named value's refs; a named function (`share = p => ...`) is checked with its parameters in scope. */
|
|
276
|
-
private declRefs(decl: Decl): Refs {
|
|
277
|
-
if (decl.value.type !== "arrow") return this.refs(decl.value);
|
|
278
|
-
if (COMPONENT_SPECS.has(decl.name) || ACTION_SPECS.has(decl.name) || FUNCTION_ARITY.has(decl.name)) {
|
|
279
|
-
this.diagnostics.report(decl.pos, `\`${decl.name}\` is a built-in name; give your function another name`);
|
|
280
|
-
}
|
|
281
|
-
const refs: Refs = { named: [], states: [] };
|
|
282
|
-
this.check(decl.value.body, decl.value.params, refs);
|
|
283
|
-
return refs;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
private checkCycles(namedRefs: ReadonlyMap<string, Refs>): void {
|
|
287
|
-
const status = new Map<string, "visiting" | "done">();
|
|
288
|
-
const path: string[] = [];
|
|
289
|
-
const visit = (name: string): void => {
|
|
290
|
-
const current = status.get(name);
|
|
291
|
-
if (current === "done") return;
|
|
292
|
-
if (current === "visiting") {
|
|
293
|
-
const cycle = [...path.slice(path.indexOf(name)), name];
|
|
294
|
-
this.diagnostics.report(this.named.get(name)!.pos, `\`${name}\` depends on itself: ${cycle.join(" → ")}`);
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
status.set(name, "visiting");
|
|
298
|
-
path.push(name);
|
|
299
|
-
for (const ref of namedRefs.get(name)?.named ?? []) if (this.named.has(ref.name)) visit(ref.name);
|
|
300
|
-
path.pop();
|
|
301
|
-
status.set(name, "done");
|
|
302
|
-
};
|
|
303
|
-
for (const name of this.named.keys()) visit(name);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/** State names each named value reads, directly or through other named values. */
|
|
307
|
-
private transitiveStates(namedRefs: ReadonlyMap<string, Refs>): (name: string) => ReadonlySet<string> {
|
|
308
|
-
const memo = new Map<string, Set<string>>();
|
|
309
|
-
const statesOf = (name: string): ReadonlySet<string> => {
|
|
310
|
-
const cached = memo.get(name);
|
|
311
|
-
if (cached !== undefined) return cached;
|
|
312
|
-
const result = new Set<string>();
|
|
313
|
-
memo.set(name, result); // cycles were reported; this stops the recursion
|
|
314
|
-
const refs = namedRefs.get(name);
|
|
315
|
-
for (const ref of refs?.states ?? []) result.add(ref.name);
|
|
316
|
-
for (const ref of refs?.named ?? []) for (const state of statesOf(ref.name)) result.add(state);
|
|
317
|
-
return result;
|
|
318
|
-
};
|
|
319
|
-
return statesOf;
|
|
320
|
-
}
|
|
321
244
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if (!this.states.has(expr.name)) {
|
|
337
|
-
const hint = this.named.has(expr.name)
|
|
338
|
-
? ` \`${expr.name}\` is a named value; write it without \`$\`.`
|
|
339
|
-
: didYouMean(expr.name, this.states.keys(), (name) => `$${name}`) ||
|
|
340
|
-
` Declare it first, as in \`$${expr.name} = 0\`.`;
|
|
341
|
-
this.diagnostics.report(expr.pos, `unknown state \`$${expr.name}\`.${hint}`);
|
|
245
|
+
this.checkCycles(namedRefs);
|
|
246
|
+
const statesOf = this.transitiveStates(namedRefs);
|
|
247
|
+
stateDecls.forEach((decl, index) => {
|
|
248
|
+
const refs = this.refs(decl.value);
|
|
249
|
+
for (const ref of refs.named) used.add(ref.name);
|
|
250
|
+
for (const ref of refs.states) {
|
|
251
|
+
const target = this.states.get(ref.name);
|
|
252
|
+
if (target === undefined || target.index < index) continue;
|
|
253
|
+
this.diagnostics.report(
|
|
254
|
+
ref.pos,
|
|
255
|
+
target.index === index
|
|
256
|
+
? `\`$${decl.name}\` cannot use itself in its initial value`
|
|
257
|
+
: `\`$${ref.name}\` is declared after \`$${decl.name}\`; initial values can only use earlier state`,
|
|
258
|
+
);
|
|
342
259
|
}
|
|
343
|
-
refs.
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
260
|
+
for (const ref of refs.named) {
|
|
261
|
+
const late = [...statesOf(ref.name)].find((name) => (this.states.get(name)?.index ?? -1) >= index);
|
|
262
|
+
if (late !== undefined) {
|
|
263
|
+
this.diagnostics.report(
|
|
264
|
+
ref.pos,
|
|
265
|
+
`\`${ref.name}\` reads \`$${late}\`, which is not initialized before \`$${decl.name}\``,
|
|
266
|
+
);
|
|
350
267
|
}
|
|
351
|
-
refs.named.push(expr);
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
this.diagnostics.report(expr.pos, this.unknownName(expr.name, params));
|
|
355
|
-
return;
|
|
356
|
-
case "list":
|
|
357
|
-
for (const item of expr.items) this.check(item, params, refs);
|
|
358
|
-
return;
|
|
359
|
-
case "object":
|
|
360
|
-
for (const entry of expr.entries) {
|
|
361
|
-
this.forbidden(entry.key, entry.pos);
|
|
362
|
-
this.check(entry.value, params, refs);
|
|
363
268
|
}
|
|
364
|
-
|
|
365
|
-
case "unary":
|
|
366
|
-
this.check(expr.operand, params, refs);
|
|
367
|
-
return;
|
|
368
|
-
case "binary":
|
|
369
|
-
this.check(expr.left, params, refs);
|
|
370
|
-
this.check(expr.right, params, refs);
|
|
371
|
-
return;
|
|
372
|
-
case "conditional":
|
|
373
|
-
this.check(expr.test, params, refs);
|
|
374
|
-
this.check(expr.then, params, refs);
|
|
375
|
-
this.check(expr.else, params, refs);
|
|
376
|
-
return;
|
|
377
|
-
case "member":
|
|
378
|
-
this.forbidden(expr.name, expr.pos);
|
|
379
|
-
this.check(expr.object, params, refs);
|
|
380
|
-
return;
|
|
381
|
-
case "index":
|
|
382
|
-
if (expr.index.type === "literal" && typeof expr.index.value === "string") {
|
|
383
|
-
this.forbidden(expr.index.value, expr.index.pos);
|
|
384
|
-
}
|
|
385
|
-
this.check(expr.object, params, refs);
|
|
386
|
-
this.check(expr.index, params, refs);
|
|
387
|
-
return;
|
|
388
|
-
case "arrow":
|
|
389
|
-
this.diagnostics.report(expr.pos, ARROW_ONLY);
|
|
390
|
-
this.check(expr.body, [...params, ...expr.params], refs);
|
|
391
|
-
return;
|
|
392
|
-
case "method":
|
|
393
|
-
this.method(expr, params, refs);
|
|
394
|
-
return;
|
|
395
|
-
case "call":
|
|
396
|
-
this.call(expr, params, refs);
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
269
|
+
});
|
|
400
270
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
return
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
271
|
+
for (const [name, decl] of this.named) {
|
|
272
|
+
if (!used.has(name)) this.diagnostics.report(decl.pos, `\`${name}\` is never used`, "warning");
|
|
273
|
+
}
|
|
274
|
+
return { states: stateDecls, named: this.named, roots };
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
refs(expr: Expr): Refs {
|
|
278
|
+
const refs: Refs = { named: [], states: [] };
|
|
279
|
+
this.check(expr, [], refs);
|
|
280
|
+
return refs;
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
/** A named value's refs; a named function (`share = p => ...`) is checked with its parameters in scope. */
|
|
284
|
+
declRefs(decl: Decl): Refs {
|
|
285
|
+
if (decl.value.type !== "arrow") return this.refs(decl.value);
|
|
286
|
+
if (COMPONENT_SPECS.has(decl.name) || ACTION_SPECS.has(decl.name) || FUNCTION_ARITY.has(decl.name)) {
|
|
287
|
+
this.diagnostics.report(decl.pos, `\`${decl.name}\` is a built-in name; give your function another name`);
|
|
288
|
+
}
|
|
289
|
+
const refs: Refs = { named: [], states: [] };
|
|
290
|
+
this.check(decl.value.body, decl.value.params, refs);
|
|
291
|
+
return refs;
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
checkCycles(namedRefs: ReadonlyMap<string, Refs>): void {
|
|
295
|
+
const status = new Map<string, "visiting" | "done">();
|
|
296
|
+
const path: string[] = [];
|
|
297
|
+
const visit = (name: string): void => {
|
|
298
|
+
const current = status.get(name);
|
|
299
|
+
if (current === "done") return;
|
|
300
|
+
if (current === "visiting") {
|
|
301
|
+
const cycle = [...path.slice(path.indexOf(name)), name];
|
|
302
|
+
this.diagnostics.report(this.named.get(name)!.pos, `\`${name}\` depends on itself: ${cycle.join(" → ")}`);
|
|
303
|
+
return;
|
|
434
304
|
}
|
|
435
|
-
|
|
305
|
+
status.set(name, "visiting");
|
|
306
|
+
path.push(name);
|
|
307
|
+
for (const ref of namedRefs.get(name)?.named ?? []) if (this.named.has(ref.name)) visit(ref.name);
|
|
308
|
+
path.pop();
|
|
309
|
+
status.set(name, "done");
|
|
310
|
+
};
|
|
311
|
+
for (const name of this.named.keys()) visit(name);
|
|
312
|
+
},
|
|
313
|
+
|
|
314
|
+
/** State names each named value reads, directly or through other named values. */
|
|
315
|
+
transitiveStates(namedRefs: ReadonlyMap<string, Refs>): (name: string) => ReadonlySet<string> {
|
|
316
|
+
const memo = new Map<string, Set<string>>();
|
|
317
|
+
const statesOf = (name: string): ReadonlySet<string> => {
|
|
318
|
+
const cached = memo.get(name);
|
|
319
|
+
if (cached !== undefined) return cached;
|
|
320
|
+
const result = new Set<string>();
|
|
321
|
+
memo.set(name, result); // cycles were reported; this stops the recursion
|
|
322
|
+
const refs = namedRefs.get(name);
|
|
323
|
+
for (const ref of refs?.states ?? []) result.add(ref.name);
|
|
324
|
+
for (const ref of refs?.named ?? []) for (const state of statesOf(ref.name)) result.add(state);
|
|
325
|
+
return result;
|
|
326
|
+
};
|
|
327
|
+
return statesOf;
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
forbidden(name: string, pos: Pos): void {
|
|
331
|
+
if ((FORBIDDEN_PROPERTIES as readonly string[]).includes(name)) {
|
|
332
|
+
this.diagnostics.report(pos, `\`${name}\` is not accessible`);
|
|
436
333
|
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
check(expr: Expr, params: readonly string[], refs: Refs): void {
|
|
337
|
+
switch (expr.type) {
|
|
338
|
+
case "literal":
|
|
339
|
+
return;
|
|
340
|
+
case "template":
|
|
341
|
+
for (const part of expr.parts) this.check(part, params, refs);
|
|
342
|
+
return;
|
|
343
|
+
case "state":
|
|
344
|
+
if (!this.states.has(expr.name)) {
|
|
345
|
+
const hint = this.named.has(expr.name)
|
|
346
|
+
? ` \`${expr.name}\` is a named value; write it without \`$\`.`
|
|
347
|
+
: didYouMean(expr.name, this.states.keys(), (name) => `$${name}`) ||
|
|
348
|
+
` Declare it first, as in \`$${expr.name} = 0\`.`;
|
|
349
|
+
this.diagnostics.report(expr.pos, `unknown state \`$${expr.name}\`.${hint}`);
|
|
350
|
+
}
|
|
351
|
+
refs.states.push(expr);
|
|
352
|
+
return;
|
|
353
|
+
case "ident":
|
|
354
|
+
if (params.includes(expr.name)) return;
|
|
355
|
+
if (this.named.has(expr.name)) {
|
|
356
|
+
if (this.named.get(expr.name)!.value.type === "arrow") {
|
|
357
|
+
this.diagnostics.report(expr.pos, `\`${expr.name}\` is a function; call it, as in \`${expr.name}(...)\``);
|
|
358
|
+
}
|
|
359
|
+
refs.named.push(expr);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
this.diagnostics.report(expr.pos, this.unknownName(expr.name, params));
|
|
363
|
+
return;
|
|
364
|
+
case "list":
|
|
365
|
+
for (const item of expr.items) this.check(item, params, refs);
|
|
366
|
+
return;
|
|
367
|
+
case "object":
|
|
368
|
+
for (const entry of expr.entries) {
|
|
369
|
+
this.forbidden(entry.key, entry.pos);
|
|
370
|
+
this.check(entry.value, params, refs);
|
|
371
|
+
}
|
|
372
|
+
return;
|
|
373
|
+
case "unary":
|
|
374
|
+
this.check(expr.operand, params, refs);
|
|
375
|
+
return;
|
|
376
|
+
case "binary":
|
|
377
|
+
this.check(expr.left, params, refs);
|
|
378
|
+
this.check(expr.right, params, refs);
|
|
379
|
+
return;
|
|
380
|
+
case "conditional":
|
|
381
|
+
this.check(expr.test, params, refs);
|
|
382
|
+
this.check(expr.then, params, refs);
|
|
383
|
+
this.check(expr.else, params, refs);
|
|
384
|
+
return;
|
|
385
|
+
case "member":
|
|
386
|
+
this.forbidden(expr.name, expr.pos);
|
|
387
|
+
this.check(expr.object, params, refs);
|
|
388
|
+
return;
|
|
389
|
+
case "index":
|
|
390
|
+
if (expr.index.type === "literal" && typeof expr.index.value === "string") {
|
|
391
|
+
this.forbidden(expr.index.value, expr.index.pos);
|
|
392
|
+
}
|
|
393
|
+
this.check(expr.object, params, refs);
|
|
394
|
+
this.check(expr.index, params, refs);
|
|
395
|
+
return;
|
|
396
|
+
case "arrow":
|
|
397
|
+
this.diagnostics.report(expr.pos, ARROW_ONLY);
|
|
398
|
+
this.check(expr.body, [...params, ...expr.params], refs);
|
|
399
|
+
return;
|
|
400
|
+
case "method":
|
|
401
|
+
this.method(expr, params, refs);
|
|
402
|
+
return;
|
|
403
|
+
case "call":
|
|
404
|
+
this.call(expr, params, refs);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
},
|
|
442
408
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
this.catalogCall(expr, spec);
|
|
448
|
-
} else if (own?.type === "arrow" && !params.includes(expr.name)) {
|
|
449
|
-
if (expr.named.length > 0) {
|
|
450
|
-
this.diagnostics.report(expr.named[0].pos, `\`${expr.name}\` takes positional arguments only`);
|
|
409
|
+
unknownName(name: string, params: readonly string[]): string {
|
|
410
|
+
if (this.states.has(name)) return `\`${name}\` is not defined. Did you mean \`$${name}\`?`;
|
|
411
|
+
if (COMPONENT_SPECS.has(name) || ACTION_SPECS.has(name) || FUNCTION_ARITY.has(name)) {
|
|
412
|
+
return `\`${name}\` must be called, as in \`${name}(...)\``;
|
|
451
413
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
414
|
+
const hint = didYouMean(name, [...params, ...this.named.keys()]);
|
|
415
|
+
return `\`${name}\` is not defined.${hint || ` Declare it first, as in \`${name} = ...\`.`}`;
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
method(expr: Extract<Expr, { type: "method" }>, params: readonly string[], refs: Refs): void {
|
|
419
|
+
this.check(expr.object, params, refs);
|
|
420
|
+
for (const arg of expr.named) {
|
|
421
|
+
this.diagnostics.report(arg.pos, "methods take positional arguments only");
|
|
422
|
+
this.check(arg.value, params, refs);
|
|
455
423
|
}
|
|
456
|
-
|
|
457
|
-
} else {
|
|
458
|
-
const arity = FUNCTION_ARITY.get(expr.name);
|
|
424
|
+
const arity = METHOD_ARITY.get(expr.name);
|
|
459
425
|
if (arity === undefined) {
|
|
460
|
-
|
|
461
|
-
|
|
426
|
+
const hint = didYouMean(expr.name, METHOD_ARITY.keys(), (name) => `.${name}()`);
|
|
427
|
+
this.diagnostics.report(
|
|
428
|
+
expr.pos,
|
|
429
|
+
`\`.${expr.name}()\` is not an allowed method.${hint} Lists: ${METHODS.list.join(", ")}. Text: ${METHODS.string.join(", ")}. Numbers: ${METHODS.number.join(", ")}.`,
|
|
430
|
+
);
|
|
431
|
+
} else if (expr.args.length < arity[0] || expr.args.length > arity[1]) {
|
|
432
|
+
this.diagnostics.report(expr.pos, `\`.${expr.name}()\` takes ${arityText(arity)}`);
|
|
433
|
+
}
|
|
434
|
+
const [first, ...rest] = expr.args;
|
|
435
|
+
if (LAMBDA_METHODS.has(expr.name) && first !== undefined) {
|
|
436
|
+
if (first.type !== "arrow") {
|
|
437
|
+
this.diagnostics.report(first.pos, `\`.${expr.name}()\` takes a function, as in \`x => x.price\``);
|
|
438
|
+
this.check(first, params, refs);
|
|
439
|
+
} else {
|
|
440
|
+
if (first.params.length > 2) {
|
|
441
|
+
this.diagnostics.report(first.pos, `\`.${expr.name}()\` passes (item, index); use at most 2 parameters`);
|
|
442
|
+
}
|
|
443
|
+
this.check(first.body, [...params, ...first.params], refs);
|
|
444
|
+
}
|
|
445
|
+
} else if (first !== undefined) {
|
|
446
|
+
this.check(first, params, refs);
|
|
447
|
+
}
|
|
448
|
+
for (const arg of rest) this.check(arg, params, refs);
|
|
449
|
+
},
|
|
450
|
+
|
|
451
|
+
call(expr: CallExpr, params: readonly string[], refs: Refs): void {
|
|
452
|
+
const spec = COMPONENT_SPECS.get(expr.name) ?? ACTION_SPECS.get(expr.name);
|
|
453
|
+
const own = this.named.get(expr.name)?.value;
|
|
454
|
+
if (spec !== undefined) {
|
|
455
|
+
this.catalogCall(expr, spec);
|
|
456
|
+
} else if (own?.type === "arrow" && !params.includes(expr.name)) {
|
|
462
457
|
if (expr.named.length > 0) {
|
|
463
458
|
this.diagnostics.report(expr.named[0].pos, `\`${expr.name}\` takes positional arguments only`);
|
|
464
459
|
}
|
|
465
|
-
if (expr.args.length
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
460
|
+
if (expr.args.length > own.params.length) {
|
|
461
|
+
const count = own.params.length;
|
|
462
|
+
this.diagnostics.report(expr.pos, `\`${expr.name}\` takes ${count} argument${count === 1 ? "" : "s"}`);
|
|
463
|
+
}
|
|
464
|
+
refs.named.push(expr);
|
|
465
|
+
} else {
|
|
466
|
+
const arity = FUNCTION_ARITY.get(expr.name);
|
|
467
|
+
if (arity === undefined) {
|
|
468
|
+
this.diagnostics.report(expr.pos, this.unknownCallee(expr.name, params));
|
|
469
|
+
} else {
|
|
470
|
+
if (expr.named.length > 0) {
|
|
471
|
+
this.diagnostics.report(expr.named[0].pos, `\`${expr.name}\` takes positional arguments only`);
|
|
472
|
+
}
|
|
473
|
+
if (expr.args.length < arity[0] || expr.args.length > arity[1]) {
|
|
474
|
+
this.diagnostics.report(expr.pos, `\`${expr.name}\` takes ${arityText(arity)}: ${FUNCTIONS[expr.name as FunctionName]}`);
|
|
475
|
+
} else if (expr.name === "Load") {
|
|
476
|
+
this.literalPath(expr.args[0], "Load");
|
|
477
|
+
}
|
|
469
478
|
}
|
|
470
479
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
}
|
|
480
|
+
for (const arg of expr.args) this.check(arg, params, refs);
|
|
481
|
+
for (const arg of expr.named) this.check(arg.value, params, refs);
|
|
482
|
+
},
|
|
475
483
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
}
|
|
480
|
-
const isFunctionLike = name[0] === name[0].toLowerCase();
|
|
481
|
-
const candidates = [...COMPONENT_SPECS.keys(), ...ACTION_SPECS.keys(), ...FUNCTION_ARITY.keys()];
|
|
482
|
-
const fallback = isFunctionLike
|
|
483
|
-
? ` Functions: ${[...FUNCTION_ARITY.keys()].join(", ")}.`
|
|
484
|
-
: ` Components: ${[...COMPONENT_SPECS.keys()].join(", ")}.`;
|
|
485
|
-
return `\`${name}\` is not a component, action or function.${didYouMean(name, candidates) || fallback}`;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/** The host fetches files before evaluating, so the path must be readable from the source alone. */
|
|
489
|
-
private literalPath(arg: Expr, owner: "Load" | "Snippet"): void {
|
|
490
|
-
const path = loadPath(arg);
|
|
491
|
-
if (path === null) {
|
|
492
|
-
const usage =
|
|
493
|
-
owner === "Load"
|
|
494
|
-
? '`Load` takes a plain string path, as in Load("data/sales.csv")'
|
|
495
|
-
: '`Snippet` takes its path as a literal string, as in Snippet("src/app.ts", 10, 40)';
|
|
496
|
-
this.diagnostics.report(
|
|
497
|
-
arg.pos,
|
|
498
|
-
`${usage}; the file is read before the UI runs, so the path cannot be computed or use \`\${}\``,
|
|
499
|
-
);
|
|
500
|
-
} else if (path.trim() === "") {
|
|
501
|
-
this.diagnostics.report(arg.pos, `\`${owner}\` needs a file path`);
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
/** Maps arguments onto catalog params and checks what is knowable before evaluation. */
|
|
506
|
-
private catalogCall(expr: CallExpr, spec: ComponentSpec): void {
|
|
507
|
-
const sig = signature(expr.name, spec);
|
|
508
|
-
const slots: (Expr | undefined)[] = spec.params.map((_, index) => expr.args[index]);
|
|
509
|
-
if (expr.args.length > spec.params.length) {
|
|
510
|
-
this.diagnostics.report(expr.args[spec.params.length].pos, `too many arguments: ${sig}`);
|
|
511
|
-
}
|
|
512
|
-
for (const arg of expr.named) {
|
|
513
|
-
const index = spec.params.findIndex((param) => param.name === arg.name);
|
|
514
|
-
if (index < 0) {
|
|
515
|
-
const hint = didYouMean(arg.name, spec.params.map((param) => param.name));
|
|
516
|
-
this.diagnostics.report(arg.pos, `\`${expr.name}\` has no parameter \`${arg.name}\`.${hint} ${sig}`);
|
|
517
|
-
} else if (slots[index] !== undefined) {
|
|
518
|
-
this.diagnostics.report(arg.pos, `\`${arg.name}\` is given twice: ${sig}`);
|
|
519
|
-
} else {
|
|
520
|
-
slots[index] = arg.value;
|
|
484
|
+
unknownCallee(name: string, params: readonly string[]): string {
|
|
485
|
+
if (this.named.has(name) || params.includes(name) || this.states.has(name)) {
|
|
486
|
+
return `\`${name}\` is a value and cannot be called; only components, actions and functions can`;
|
|
521
487
|
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}
|
|
528
|
-
|
|
488
|
+
const isFunctionLike = name[0] === name[0].toLowerCase();
|
|
489
|
+
const candidates = [...COMPONENT_SPECS.keys(), ...ACTION_SPECS.keys(), ...FUNCTION_ARITY.keys()];
|
|
490
|
+
const fallback = isFunctionLike
|
|
491
|
+
? ` Functions: ${[...FUNCTION_ARITY.keys()].join(", ")}.`
|
|
492
|
+
: ` Components: ${[...COMPONENT_SPECS.keys()].join(", ")}.`;
|
|
493
|
+
return `\`${name}\` is not a component, action or function.${didYouMean(name, candidates) || fallback}`;
|
|
494
|
+
},
|
|
495
|
+
|
|
496
|
+
/** The host fetches files before evaluating, so the path must be readable from the source alone. */
|
|
497
|
+
literalPath(arg: Expr, owner: "Load" | "Snippet"): void {
|
|
498
|
+
const path = loadPath(arg);
|
|
499
|
+
if (path === null) {
|
|
500
|
+
const usage =
|
|
501
|
+
owner === "Load"
|
|
502
|
+
? '`Load` takes a plain string path, as in Load("data/sales.csv")'
|
|
503
|
+
: '`Snippet` takes its path as a literal string, as in Snippet("src/app.ts", 10, 40)';
|
|
504
|
+
this.diagnostics.report(
|
|
505
|
+
arg.pos,
|
|
506
|
+
`${usage}; the file is read before the UI runs, so the path cannot be computed or use \`\${}\``,
|
|
507
|
+
);
|
|
508
|
+
} else if (path.trim() === "") {
|
|
509
|
+
this.diagnostics.report(arg.pos, `\`${owner}\` needs a file path`);
|
|
529
510
|
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
if (param.kind === "binding" && slot.type !== "state" && !isBareState) {
|
|
539
|
-
this.diagnostics.report(slot.pos, `${label} must be a state variable written as \`$name\``);
|
|
540
|
-
}
|
|
541
|
-
if (param.kind === "binding" && slot.type === "state") {
|
|
542
|
-
const initial = this.states.get(slot.name)?.decl.value;
|
|
543
|
-
const kind = initial === undefined ? undefined : staticKind(initial);
|
|
544
|
-
const mismatch = kind === undefined ? null : stateMismatch(label, param, slot.name, kind, "starts as");
|
|
545
|
-
// A literal the input can read losslessly is only worth a warning.
|
|
546
|
-
const coercible = initial?.type === "literal" && coerceState(param.holds, initial.value) !== undefined;
|
|
547
|
-
if (mismatch !== null) this.diagnostics.report(slot.pos, mismatch, coercible ? "warning" : "error");
|
|
548
|
-
}
|
|
549
|
-
const wanted = LITERAL_KINDS[param.kind];
|
|
550
|
-
const got = staticKind(slot);
|
|
551
|
-
if (wanted !== undefined && got !== undefined && got !== wanted && !(got === "null" && (param.optional || param.nullable))) {
|
|
552
|
-
this.diagnostics.report(slot.pos, `${label} needs ${EXPECTED[param.kind]}, got ${DESCRIPTIONS[got]}`);
|
|
553
|
-
}
|
|
554
|
-
if (param.kind === "enum" && slot.type === "literal" && !(slot.value === null && param.optional)) {
|
|
555
|
-
const values = param.values ?? [];
|
|
556
|
-
if (typeof slot.value !== "string" || !values.includes(slot.value)) {
|
|
557
|
-
this.diagnostics.report(slot.pos, `${label} must be one of ${values.join(", ")}; got ${JSON.stringify(slot.value)}`);
|
|
511
|
+
},
|
|
512
|
+
|
|
513
|
+
/** Maps arguments onto catalog params and checks what is knowable before evaluation. */
|
|
514
|
+
catalogCall(expr: CallExpr, spec: ComponentSpec): void {
|
|
515
|
+
const sig = signature(expr.name, spec);
|
|
516
|
+
const slots: (Expr | undefined)[] = spec.params.map((_, index) => expr.args[index]);
|
|
517
|
+
if (expr.args.length > spec.params.length) {
|
|
518
|
+
this.diagnostics.report(expr.args[spec.params.length].pos, `too many arguments: ${sig}`);
|
|
558
519
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
520
|
+
for (const arg of expr.named) {
|
|
521
|
+
const index = spec.params.findIndex((param) => param.name === arg.name);
|
|
522
|
+
if (index < 0) {
|
|
523
|
+
const hint = didYouMean(arg.name, spec.params.map((param) => param.name));
|
|
524
|
+
this.diagnostics.report(arg.pos, `\`${expr.name}\` has no parameter \`${arg.name}\`.${hint} ${sig}`);
|
|
525
|
+
} else if (slots[index] !== undefined) {
|
|
526
|
+
this.diagnostics.report(arg.pos, `\`${arg.name}\` is given twice: ${sig}`);
|
|
527
|
+
} else {
|
|
528
|
+
slots[index] = arg.value;
|
|
567
529
|
}
|
|
568
|
-
|
|
569
|
-
|
|
530
|
+
}
|
|
531
|
+
spec.params.forEach((param, index) => {
|
|
532
|
+
const slot = slots[index];
|
|
533
|
+
if (slot === undefined) {
|
|
534
|
+
if (!param.optional) this.diagnostics.report(expr.pos, `\`${expr.name}\` is missing \`${param.name}\`: ${sig}`);
|
|
535
|
+
} else {
|
|
536
|
+
this.checkParam(expr.name, param, slot);
|
|
570
537
|
}
|
|
538
|
+
});
|
|
539
|
+
},
|
|
540
|
+
|
|
541
|
+
checkParam(owner: string, param: ParamSpec, slot: Expr): void {
|
|
542
|
+
const label = `\`${owner}\` ${param.name}`;
|
|
543
|
+
if (owner === "Snippet" && param.name === "path") this.literalPath(slot, "Snippet");
|
|
544
|
+
// A bare state name (`plan` for `$plan`) is already reported with a `$` suggestion.
|
|
545
|
+
const isBareState = slot.type === "ident" && this.states.has(slot.name);
|
|
546
|
+
if (param.kind === "binding" && slot.type !== "state" && !isBareState) {
|
|
547
|
+
this.diagnostics.report(slot.pos, `${label} must be a state variable written as \`$name\``);
|
|
571
548
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
549
|
+
if (param.kind === "binding" && slot.type === "state") {
|
|
550
|
+
const initial = this.states.get(slot.name)?.decl.value;
|
|
551
|
+
const kind = initial === undefined ? undefined : staticKind(initial);
|
|
552
|
+
const mismatch = kind === undefined ? null : stateMismatch(label, param, slot.name, kind, "starts as");
|
|
553
|
+
// A literal the input can read losslessly is only worth a warning.
|
|
554
|
+
const coercible = initial?.type === "literal" && coerceState(param.holds, initial.value) !== undefined;
|
|
555
|
+
if (mismatch !== null) this.diagnostics.report(slot.pos, mismatch, coercible ? "warning" : "error");
|
|
556
|
+
}
|
|
557
|
+
const wanted = LITERAL_KINDS[param.kind];
|
|
558
|
+
const got = staticKind(slot);
|
|
559
|
+
if (wanted !== undefined && got !== undefined && got !== wanted && !(got === "null" && (param.optional || param.nullable))) {
|
|
560
|
+
this.diagnostics.report(slot.pos, `${label} needs ${EXPECTED[param.kind]}, got ${DESCRIPTIONS[got]}`);
|
|
561
|
+
}
|
|
562
|
+
if (param.kind === "enum" && slot.type === "literal" && !(slot.value === null && param.optional)) {
|
|
563
|
+
const values = param.values ?? [];
|
|
564
|
+
if (typeof slot.value !== "string" || !values.includes(slot.value)) {
|
|
565
|
+
this.diagnostics.report(slot.pos, `${label} must be one of ${values.join(", ")}; got ${JSON.stringify(slot.value)}`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
if (param.kind === "actions") {
|
|
569
|
+
for (const item of slot.type === "list" ? slot.items : [slot]) {
|
|
570
|
+
let got: string | undefined;
|
|
571
|
+
if (item.type === "call" && (COMPONENT_SPECS.has(item.name) || FUNCTION_ARITY.has(item.name))) {
|
|
572
|
+
got = `\`${item.name}(...)\``;
|
|
573
|
+
} else if (item.type === "literal" && item.value !== null) {
|
|
574
|
+
got = JSON.stringify(item.value);
|
|
575
|
+
}
|
|
576
|
+
if (got !== undefined) {
|
|
577
|
+
this.diagnostics.report(item.pos, `${label} takes actions: ${ACTION_LIST}; got ${got}`);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
},
|
|
582
|
+
},
|
|
583
|
+
);
|
|
584
|
+
type Validator = InstanceType<typeof Validator>;
|