@tiaforge-ui/sinter 0.1.0 → 0.2.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 +37 -11
- package/dist/check/index.d.ts +4 -1
- package/dist/check/index.js +7 -7
- package/dist/check/shadow.d.ts +3 -1
- package/dist/check/shadow.js +37 -3
- package/dist/cli.js +2 -2
- package/dist/compiler/check-unit.d.ts +12 -0
- package/dist/compiler/compile.d.ts +4 -0
- package/dist/compiler/compile.js +206 -23
- package/dist/compiler/diagnostic-docs.js +10 -5
- package/dist/compiler/diagnostics.d.ts +1 -0
- package/dist/compiler/diagnostics.js +1 -0
- package/dist/compiler/expr.d.ts +8 -0
- package/dist/compiler/expr.js +20 -5
- package/dist/compiler/index.d.ts +3 -2
- package/dist/compiler/index.js +70 -16
- package/dist/compiler/vocabulary.js +3 -2
- package/dist/each.d.ts +3 -0
- package/dist/each.js +121 -0
- package/dist/index.d.ts +1 -1
- package/dist/ir.d.ts +26 -1
- package/dist/lsp/typescript.js +1 -1
- package/dist/lsp/workspace.js +3 -2
- package/dist/paginate.js +18 -4
- package/dist/project.d.ts +6 -0
- package/dist/project.js +22 -19
- package/dist/runtime/app.d.ts +28 -1
- package/dist/runtime/app.js +97 -24
- package/dist/runtime/config.d.ts +3 -3
- package/dist/runtime/config.js +1 -1
- package/dist/runtime/store.d.ts +7 -4
- package/dist/runtime/store.js +154 -41
- package/dist/sse.js +8 -5
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +16 -2
- package/dist/vite.js +4 -2
- package/docs/diagnostics.md +15 -5
- package/html-custom-data.json +17 -1
- package/llms.txt +105 -4
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
**Your JSON API, bound to plain HTML.** sinter loads and caches server data, keeps it current from
|
|
4
4
|
server events, renders it through `<template>` bindings and runs mutations with optimistic updates,
|
|
5
|
-
without components, a router or a store library. A
|
|
6
|
-
|
|
5
|
+
without components, a router or a store library. A compiler resolves every name in every expression
|
|
6
|
+
at build time, and type-checks it when you give it your TypeScript types or OpenAPI spec. A typo
|
|
7
|
+
fails the build instead of the page, and the browser runs no parser and no `eval`.
|
|
7
8
|
|
|
8
9
|
```html
|
|
9
10
|
<sn-resource name="contacts" src="/api/contacts" items="contacts" key="id" order="name"></sn-resource>
|
|
@@ -30,6 +31,26 @@ That page loads the contacts, keeps them sorted, applies updates other users mak
|
|
|
30
31
|
filters as you type, and removes a contact at once when you click Delete, putting it back if the
|
|
31
32
|
server refuses.
|
|
32
33
|
|
|
34
|
+
## Mistakes fail the build
|
|
35
|
+
|
|
36
|
+
The compiler knows every name an expression can use: the state around it, loop variables,
|
|
37
|
+
resources, helpers. Misspell `q` in the filter above and the build stops:
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
contacts.html:11:70 error SN201 unknown-identifier: `qq` is not defined here. Did you mean `q`?
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The same pass rejects code that would break at runtime: writing to server data
|
|
44
|
+
(`c.name = 'x'` is `SN205`, use `patch('contacts', c, {…})`) and browser APIs inside expressions
|
|
45
|
+
(`localStorage` is `SN202`, use an island). With a `type` on the resource or your OpenAPI spec,
|
|
46
|
+
`sinter check` also type-checks fields, request bodies and event payloads, so `article.titel`
|
|
47
|
+
reports `Property 'titel' does not exist on type 'Article'. Did you mean 'title'?`. Your editor
|
|
48
|
+
shows these errors as you type.
|
|
49
|
+
|
|
50
|
+
htmx, Datastar and Alpine read their attribute strings in the browser. Nothing checks them against
|
|
51
|
+
your data before they ship, so the same typo shows up only when that code runs: as a console error,
|
|
52
|
+
or as an element that stays empty.
|
|
53
|
+
|
|
33
54
|
## Why sinter
|
|
34
55
|
|
|
35
56
|
- **The server-state plumbing is gone.** You don't write loading flags, stale-response guards,
|
|
@@ -37,11 +58,11 @@ server refuses.
|
|
|
37
58
|
They are attributes.
|
|
38
59
|
- **Optimistic updates that stay correct**, also when requests overlap or a server event arrives
|
|
39
60
|
while a request is in flight.
|
|
61
|
+
- **Checked before it ships.** Every name in every expression is resolved at build time and
|
|
62
|
+
type-checked against your TypeScript types or your OpenAPI spec. Your editor shows the errors as
|
|
63
|
+
you type, with hover, completion and go to definition.
|
|
40
64
|
- **Less code.** The [RealWorld](examples/realworld) app takes 735 lines in sinter. The official
|
|
41
|
-
Angular version takes 2,210. Each page downloads
|
|
42
|
-
- **Errors at build time.** Every expression is type-checked against your TypeScript types or your
|
|
43
|
-
OpenAPI spec. Your editor shows the errors as you type, with hover, completion and go to
|
|
44
|
-
definition.
|
|
65
|
+
Angular version takes 2,210. Each page downloads 14–16 KB of JS.
|
|
45
66
|
- **Safe by construction.** Directives are compiled, not read from the live DOM. Pages run under
|
|
46
67
|
`script-src 'self'` with no `unsafe-eval`, and markup injected at runtime stays inert.
|
|
47
68
|
- **Plain, valid HTML.** There is one way to do each thing, and everything is visible on the element.
|
|
@@ -57,11 +78,14 @@ page load (the shared code comes from the browser cache).
|
|
|
57
78
|
|
|
58
79
|
**htmx, Datastar.** These expect a server that renders HTML for every page and interaction. If you
|
|
59
80
|
have a JSON API, you would have to build and maintain that server. sinter uses the API you already
|
|
60
|
-
have, and its mutations are optimistic where htmx waits for the server's answer.
|
|
61
|
-
|
|
81
|
+
have, and its mutations are optimistic where htmx waits for the server's answer. Their attributes
|
|
82
|
+
are read in the browser, so a wrong name surfaces at runtime; sinter's are checked at build time.
|
|
83
|
+
If your backend renders HTML anyway and you want no build step, htmx is a good choice.
|
|
62
84
|
|
|
63
85
|
**Alpine, petite-vue.** These also render JSON with client-side templates, but they leave fetching,
|
|
64
|
-
caching, live updates and rollback to you.
|
|
86
|
+
caching, live updates and rollback to you. Their expressions are compiled in the browser with
|
|
87
|
+
`new Function`, so they need `unsafe-eval` in the CSP (Alpine has a restricted CSP build), and
|
|
88
|
+
nothing checks the names or fields they use until the code runs.
|
|
65
89
|
|
|
66
90
|
Measurements and sources: [`docs/realworld-comparison.md`](docs/realworld-comparison.md).
|
|
67
91
|
|
|
@@ -72,7 +96,7 @@ Measurements and sources: [`docs/realworld-comparison.md`](docs/realworld-compar
|
|
|
72
96
|
islands, which are plain custom elements.
|
|
73
97
|
- You can't have a build step.
|
|
74
98
|
- Your backend renders HTML, not JSON.
|
|
75
|
-
- You need stability guarantees. sinter is at v0.
|
|
99
|
+
- You need stability guarantees. sinter is at v0.2.
|
|
76
100
|
|
|
77
101
|
## Setup
|
|
78
102
|
|
|
@@ -113,6 +137,7 @@ Then write `data-sn-*` attributes in your HTML pages. Optional:
|
|
|
113
137
|
| `@tiaforge-ui/sinter/ws` | WebSocket transport for `<sn-sync>` |
|
|
114
138
|
| `@tiaforge-ui/sinter/optimistic` | optimistic layers |
|
|
115
139
|
| `@tiaforge-ui/sinter/paginate` | keyset, offset, append |
|
|
140
|
+
| `@tiaforge-ui/sinter/each` | fan-out resources (`each="a in accounts"`) |
|
|
116
141
|
| `@tiaforge-ui/sinter/persist` | `data-sn-persist` |
|
|
117
142
|
| `@tiaforge-ui/sinter/compiler` | `compile()` |
|
|
118
143
|
| `@tiaforge-ui/sinter/vite` | Vite plugin |
|
|
@@ -125,7 +150,8 @@ Then write `data-sn-*` attributes in your HTML pages. Optional:
|
|
|
125
150
|
npm test # vitest (happy-dom)
|
|
126
151
|
npm run typecheck
|
|
127
152
|
npm run build # dist/
|
|
128
|
-
npm run size # size
|
|
153
|
+
npm run size # size per page and module, with budgets
|
|
154
|
+
npm run bench # store and flush benchmarks (docs/benchmark.md)
|
|
129
155
|
npm run docs # regenerate llms.txt, html-custom-data.json (also the VS Code extension's) and docs/diagnostics.md
|
|
130
156
|
```
|
|
131
157
|
|
package/dist/check/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export interface CheckOptions {
|
|
|
9
9
|
/** from `compile(…, { check: true })`, one per page */
|
|
10
10
|
units: CheckUnit[];
|
|
11
11
|
helpers: HelperManifest;
|
|
12
|
+
/** the actions module (`data-sn-call`), same shape as helpers */
|
|
13
|
+
actions?: HelperManifest;
|
|
12
14
|
/** absolute path of the types module, or null */
|
|
13
15
|
types: string | null;
|
|
14
16
|
/** the specs the pages were compiled with (`compile(…, { openapi })`) */
|
|
@@ -57,12 +59,13 @@ export declare const COMPILER_OPTIONS: {
|
|
|
57
59
|
target: string;
|
|
58
60
|
module: string;
|
|
59
61
|
moduleResolution: string;
|
|
62
|
+
allowImportingTsExtensions: boolean;
|
|
60
63
|
lib: string[];
|
|
61
64
|
types: string[];
|
|
62
65
|
};
|
|
63
66
|
/** Where a page's shadow file goes. */
|
|
64
67
|
export declare function shadowPath(support: Support, root: string, page: string): string;
|
|
65
|
-
export declare function shadowOf(unit: CheckUnit, support: Support, root: string, helpers: HelperManifest): Shadow;
|
|
68
|
+
export declare function shadowOf(unit: CheckUnit, support: Support, root: string, helpers: HelperManifest, actions?: HelperManifest): Shadow;
|
|
66
69
|
/** A TypeScript error at a 1-based shadow position, as an SN208 diagnostic at the HTML position. */
|
|
67
70
|
export declare function typeDiagnostic(unit: CheckUnit, regions: Region[], line: number, col: number, code: string, message: string, root: string): Diagnostic;
|
|
68
71
|
export declare function check(o: CheckOptions): CheckResult;
|
package/dist/check/index.js
CHANGED
|
@@ -25,14 +25,14 @@ export function writeSupport(o) {
|
|
|
25
25
|
/** The compiler options shadow files are checked with. */
|
|
26
26
|
export const COMPILER_OPTIONS = {
|
|
27
27
|
allowJs: true, checkJs: true, strict: true, strictNullChecks: false, noImplicitAny: false, noEmit: true, skipLibCheck: true,
|
|
28
|
-
target: 'es2022', module: 'esnext', moduleResolution: 'bundler', lib: ['es2023', 'dom', 'dom.iterable'], types: [],
|
|
28
|
+
target: 'es2022', module: 'esnext', moduleResolution: 'bundler', allowImportingTsExtensions: true, lib: ['es2023', 'dom', 'dom.iterable'], types: [],
|
|
29
29
|
};
|
|
30
30
|
/** Where a page's shadow file goes. */
|
|
31
31
|
export function shadowPath(support, root, page) {
|
|
32
32
|
return join(support.outDir, relative(root, page) + '.js');
|
|
33
33
|
}
|
|
34
|
-
export function shadowOf(unit, support, root, helpers) {
|
|
35
|
-
return shadow({ unit, file: shadowPath(support, root, unit.file), root, declarations: support.declarations, types: support.types, helpers, openapi: support.openapi });
|
|
34
|
+
export function shadowOf(unit, support, root, helpers, actions) {
|
|
35
|
+
return shadow({ unit, file: shadowPath(support, root, unit.file), root, declarations: support.declarations, types: support.types, helpers, actions, openapi: support.openapi });
|
|
36
36
|
}
|
|
37
37
|
/** A TypeScript error at a 1-based shadow position, as an SN208 diagnostic at the HTML position. */
|
|
38
38
|
export function typeDiagnostic(unit, regions, line, col, code, message, root) {
|
|
@@ -56,7 +56,7 @@ export function check(o) {
|
|
|
56
56
|
const shadows = new Map();
|
|
57
57
|
for (const unit of o.units) {
|
|
58
58
|
const file = shadowPath(support, o.root, unit.file);
|
|
59
|
-
const s = shadowOf(unit, support, o.root, o.helpers);
|
|
59
|
+
const s = shadowOf(unit, support, o.root, o.helpers, o.actions);
|
|
60
60
|
mkdirSync(dirname(file), { recursive: true });
|
|
61
61
|
writeFileSync(file, s.text);
|
|
62
62
|
shadows.set(resolve(file), { unit, regions: s.regions, brokenFrom: s.brokenFrom });
|
|
@@ -72,7 +72,7 @@ export function check(o) {
|
|
|
72
72
|
throw run.error;
|
|
73
73
|
const diagnostics = parseTsc(run.stdout, outDir, o.root, shadows);
|
|
74
74
|
const resources = o.units.reduce((n, u) => n + u.resources.length, 0);
|
|
75
|
-
const typed = o.units.reduce((n, u) => n + u.resources.filter((r) => r.type || r.openapi).length, 0);
|
|
75
|
+
const typed = o.units.reduce((n, u) => n + u.resources.filter((r) => r.type || r.openapi || r.call).length, 0);
|
|
76
76
|
const viaOpenapi = o.units.reduce((n, u) => n + u.resources.filter((r) => !r.type && r.openapi).length, 0);
|
|
77
77
|
return { diagnostics, resources, typed, openapi: viaOpenapi };
|
|
78
78
|
}
|
|
@@ -91,8 +91,8 @@ export function checkPage(html, o = {}) {
|
|
|
91
91
|
const file = join(root, 'index.html');
|
|
92
92
|
writeFileSync(file, html);
|
|
93
93
|
const p = project(root, o);
|
|
94
|
-
const out = compile(html, { file, root, dev: false, helpers: p.helpers, readFile: p.readFile, check: true, openapi: p.openapi });
|
|
95
|
-
const r = check({ root, units: out.check ? [out.check] : [], helpers: p.helpers, types: p.types, openapi: p.openapi, tsc: findTsc(process.cwd()) });
|
|
94
|
+
const out = compile(html, { file, root, dev: false, helpers: p.helpers, actions: p.actions, readFile: p.readFile, check: true, openapi: p.openapi });
|
|
95
|
+
const r = check({ root, units: out.check ? [out.check] : [], helpers: p.helpers, actions: p.actions, types: p.types, openapi: p.openapi, tsc: findTsc(process.cwd()) });
|
|
96
96
|
return [...p.diagnostics, ...out.diagnostics, ...r.diagnostics].map((d) => ({ ...d, file: relative(root, d.file) }));
|
|
97
97
|
}
|
|
98
98
|
finally {
|
package/dist/check/shadow.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export interface ShadowOptions {
|
|
|
14
14
|
names: string[];
|
|
15
15
|
} | null;
|
|
16
16
|
helpers: HelperManifest;
|
|
17
|
+
/** the actions module, for `data-sn-call` arguments and results */
|
|
18
|
+
actions?: HelperManifest;
|
|
17
19
|
/** absolute paths of the declarations generated per OpenAPI spec, by spec index */
|
|
18
20
|
openapi?: string[];
|
|
19
21
|
}
|
|
@@ -36,7 +38,7 @@ export declare function shadow(o: ShadowOptions): Shadow;
|
|
|
36
38
|
/** Maps a 1-based shadow position to the HTML, or null for generated lines. */
|
|
37
39
|
export declare function mapPosition(regions: Region[], line: number, col: number): CheckLoc | null;
|
|
38
40
|
/** `sinter.d.ts`: the types the shadow files refer to. */
|
|
39
|
-
export declare const DECLARATIONS = "// Generated by `sinter check`. Do not edit.\nexport interface Meta {\n loading: boolean;\n error: Err | null;\n loadedAt: number | null;\n pending: number;\n total: number | null;\n page?: number;\n pages?: number | null;\n from?: number | null;\n to?: number | null;\n hasPrev?: boolean;\n hasNext?: boolean;\n}\nexport interface Err { status: number; body: any; message: string }\nexport type Item<T> = NonNullable<T> extends readonly (infer U)[] ? U : NonNullable<T> extends Iterable<infer U> ? U : any;\nexport type El<K extends string> = K extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[K] : HTMLElement;\nexport type Ev<K extends string> = K extends keyof HTMLElementEventMap ? HTMLElementEventMap[K] : CustomEvent;\nexport interface Store<R> {\n refresh(name: keyof R & string): void;\n upsert<K extends keyof R & string>(name: K, item: Item<R[K]>): void;\n patch<K extends keyof R & string>(name: K, item: Partial<Item<R[K]>>, set: Partial<Item<R[K]>>): void;\n remove<K extends keyof R & string>(name: K, item: Partial<Item<R[K]>>): void;\n page(name: keyof R & string, to: 'next' | 'prev' | 'first' | 'last' | number): void;\n close(): void;\n reset(): void;\n emit(event: string, detail?: unknown): void;\n}\ntype Style = 'short' | 'medium' | 'long' | 'full';\nexport function date(v: unknown, style?: Style): string;\nexport function time(v: unknown, style?: Style): string;\nexport function datetime(v: unknown, style?: Style): string;\nexport function relative(v: unknown, now?: unknown): string;\nexport function number(v: unknown, opts?: Intl.NumberFormatOptions): string;\nexport function bytes(v: unknown): string;\n";
|
|
41
|
+
export declare const DECLARATIONS = "// Generated by `sinter check`. Do not edit.\nexport interface Meta {\n loading: boolean;\n error: Err | null;\n loadedAt: number | null;\n pending: number;\n total: number | null;\n page?: number | null;\n pages?: number | null;\n from?: number | null;\n to?: number | null;\n hasPrev?: boolean;\n hasNext?: boolean;\n}\nexport interface Err { status: number | null; body: any; message: string }\nexport interface CallContext { signal: AbortSignal; request(url: string, init?: { method?: string; body?: unknown; headers?: Record<string, string>; signal?: AbortSignal }): Promise<any>; el: Element | null }\n/** an action's parameters without its trailing context */\nexport type CallArgs<F> = F extends (...a: infer P) => any ? (P extends [...infer A, infer L] ? ([L] extends [CallContext] ? A : P) : P) : never;\nexport type Item<T> = NonNullable<T> extends readonly (infer U)[] ? U : NonNullable<T> extends Iterable<infer U> ? U : any;\nexport type El<K extends string> = K extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[K] : HTMLElement;\nexport type Ev<K extends string> = K extends keyof HTMLElementEventMap ? HTMLElementEventMap[K] : CustomEvent;\nexport interface Store<R> {\n refresh(name: keyof R & string): void;\n upsert<K extends keyof R & string>(name: K, item: Item<R[K]>): void;\n patch<K extends keyof R & string>(name: K, item: Partial<Item<R[K]>>, set: Partial<Item<R[K]>>): void;\n remove<K extends keyof R & string>(name: K, item: Partial<Item<R[K]>>): void;\n page(name: keyof R & string, to: 'next' | 'prev' | 'first' | 'last' | number): void;\n close(): void;\n reset(): void;\n emit(event: string, detail?: unknown): void;\n}\ntype Style = 'short' | 'medium' | 'long' | 'full';\nexport function date(v: unknown, style?: Style): string;\nexport function time(v: unknown, style?: Style): string;\nexport function datetime(v: unknown, style?: Style): string;\nexport function relative(v: unknown, now?: unknown): string;\nexport function number(v: unknown, opts?: Intl.NumberFormatOptions): string;\nexport function bytes(v: unknown): string;\n";
|
|
40
42
|
/**
|
|
41
43
|
* Maps a 1-based position in an HTML file to the shadow, or null when it is not inside an
|
|
42
44
|
* expression. `lines` are the shadow's lines.
|
package/dist/check/shadow.js
CHANGED
|
@@ -51,7 +51,12 @@ export function shadow(o) {
|
|
|
51
51
|
if (!taken.has(ns))
|
|
52
52
|
line(`import * as ${ns} from '${moduleSpec(module)}';`);
|
|
53
53
|
}
|
|
54
|
+
if (o.actions?.module && (unit.resources.some((r) => r.call) || unit.exprs.some((e) => e.returns?.kind === 'call' || e.resultType?.kind === 'call'))) {
|
|
55
|
+
line(`import * as $A from '${moduleSpec(o.actions.module)}';`);
|
|
56
|
+
}
|
|
54
57
|
line('const $any = /** @type {any} */ (null);');
|
|
58
|
+
/** what a resource fed by `call` holds: the awaited result, at its `items` path */
|
|
59
|
+
const callResult = (c) => `Awaited<ReturnType<typeof $A.${c.name}>>${(c.items ? c.items.split('.') : []).map((k) => `[${JSON.stringify(k)}]`).join('')}`;
|
|
55
60
|
const types = [];
|
|
56
61
|
for (const r of unit.resources) {
|
|
57
62
|
const t = r.type ?? 'any';
|
|
@@ -64,6 +69,11 @@ export function shadow(o) {
|
|
|
64
69
|
line(' * }');
|
|
65
70
|
line(' */');
|
|
66
71
|
}
|
|
72
|
+
else if (r.call) {
|
|
73
|
+
const t = callResult(r.call);
|
|
74
|
+
types[types.length - 1] = `${JSON.stringify(r.name)}: ${t}`;
|
|
75
|
+
line(`/** @type {${t}} */`);
|
|
76
|
+
}
|
|
67
77
|
else if (r.openapi) {
|
|
68
78
|
const t = schemaToTs(r.openapi.schema, (n) => `$O${r.openapi.spec}.${typeName(n)}`);
|
|
69
79
|
types[types.length - 1] = `${JSON.stringify(r.name)}: ${t}`;
|
|
@@ -74,6 +84,14 @@ export function shadow(o) {
|
|
|
74
84
|
line('/** @type {any} */');
|
|
75
85
|
regions.push({ start: out.length + 1, lines: 1, loc: r.nameLoc ?? r.loc, fixed: true });
|
|
76
86
|
line(`const ${r.name} = $any;`);
|
|
87
|
+
// with `type`, what the action returns (at `items`) must fit it; reported on `type`
|
|
88
|
+
if (r.call && r.type) {
|
|
89
|
+
// own line: TypeScript drops a JSDoc tag that follows `{` on the same line
|
|
90
|
+
line('{');
|
|
91
|
+
regions.push({ start: out.length + 1, lines: 1, loc: r.loc, fixed: true });
|
|
92
|
+
line(`/** @type {${r.type.replace(/\s+/g, ' ')}} */ const $fits = /** @type {${callResult(r.call)}} */ ($any); void $fits;`);
|
|
93
|
+
line('}');
|
|
94
|
+
}
|
|
77
95
|
}
|
|
78
96
|
const names = unit.resources.map((r) => JSON.stringify(r.name)).join(' | ') || 'never';
|
|
79
97
|
line(`/** @type {Record<${names}, ${decl('Meta')}>} */`);
|
|
@@ -99,7 +117,9 @@ export function shadow(o) {
|
|
|
99
117
|
}
|
|
100
118
|
const text = t.kind === 'openapi'
|
|
101
119
|
? schemaToTs(t.schema, (n) => `$O${t.spec}.${typeName(n)}`)
|
|
102
|
-
:
|
|
120
|
+
: t.kind === 'call'
|
|
121
|
+
? (t.args ? `${decl('CallArgs')}<typeof $A.${t.name}>` : `Awaited<ReturnType<typeof $A.${t.name}>>`)
|
|
122
|
+
: `${t.partial ? 'Partial<' : ''}${decl('Item')}<typeof ${t.resource}>${t.partial ? '>' : ''}`;
|
|
103
123
|
line(`/** @${tag} {${text}} */${after}`);
|
|
104
124
|
};
|
|
105
125
|
/** declarations for the frames of an expression; returns the number of blocks opened */
|
|
@@ -148,8 +168,19 @@ export function shadow(o) {
|
|
|
148
168
|
regions.push({ start: out.length + 1, lines: 1, loc: e.loc, fixed: true });
|
|
149
169
|
line(`const ${n} = $p.${n};`);
|
|
150
170
|
}
|
|
171
|
+
// a state name shadows a payload field of the same name: an error when the payload type has it
|
|
172
|
+
if (e.payload.type) {
|
|
173
|
+
for (const n of e.payload.states ?? []) {
|
|
174
|
+
const msg = `state \`${n}\` shadows the payload field \`${n}\`: rename the state`;
|
|
175
|
+
regions.push({ start: out.length + 1, lines: 1, loc: e.loc, fixed: true });
|
|
176
|
+
line(`/** @type {${JSON.stringify(n)} extends keyof typeof $p ? ${JSON.stringify(msg)} : true} */ const $shadows_${n} = true; void $shadows_${n};`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
151
179
|
}
|
|
152
180
|
if (e.mode === 'value') {
|
|
181
|
+
// action arguments and bodies are taken when the trigger fires
|
|
182
|
+
if (e.event !== null)
|
|
183
|
+
line(`/** @type {${decl(`Ev<${JSON.stringify(e.event)}>`)}} */ const $event = $any;`);
|
|
153
184
|
// a value of the wrong type as a whole is reported on `return`
|
|
154
185
|
regions.push({ start: out.length + 1, lines: 1, loc: e.loc, fixed: true });
|
|
155
186
|
line('return (');
|
|
@@ -195,14 +226,17 @@ export interface Meta {
|
|
|
195
226
|
loadedAt: number | null;
|
|
196
227
|
pending: number;
|
|
197
228
|
total: number | null;
|
|
198
|
-
page?: number;
|
|
229
|
+
page?: number | null;
|
|
199
230
|
pages?: number | null;
|
|
200
231
|
from?: number | null;
|
|
201
232
|
to?: number | null;
|
|
202
233
|
hasPrev?: boolean;
|
|
203
234
|
hasNext?: boolean;
|
|
204
235
|
}
|
|
205
|
-
export interface Err { status: number; body: any; message: string }
|
|
236
|
+
export interface Err { status: number | null; body: any; message: string }
|
|
237
|
+
export interface CallContext { signal: AbortSignal; request(url: string, init?: { method?: string; body?: unknown; headers?: Record<string, string>; signal?: AbortSignal }): Promise<any>; el: Element | null }
|
|
238
|
+
/** an action's parameters without its trailing context */
|
|
239
|
+
export type CallArgs<F> = F extends (...a: infer P) => any ? (P extends [...infer A, infer L] ? ([L] extends [CallContext] ? A : P) : P) : never;
|
|
206
240
|
export type Item<T> = NonNullable<T> extends readonly (infer U)[] ? U : NonNullable<T> extends Iterable<infer U> ? U : any;
|
|
207
241
|
export type El<K extends string> = K extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[K] : HTMLElement;
|
|
208
242
|
export type Ev<K extends string> = K extends keyof HTMLElementEventMap ? HTMLElementEventMap[K] : CustomEvent;
|
package/dist/cli.js
CHANGED
|
@@ -36,12 +36,12 @@ async function main(argv) {
|
|
|
36
36
|
const diagnostics = [...p.diagnostics];
|
|
37
37
|
const units = [];
|
|
38
38
|
for (const file of files) {
|
|
39
|
-
const out = compile(readFileSync(file, 'utf8'), { file, root, dev: false, helpers: p.helpers, config: p.config, readFile: p.readFile, check: true, openapi: p.openapi });
|
|
39
|
+
const out = compile(readFileSync(file, 'utf8'), { file, root, dev: false, helpers: p.helpers, actions: p.actions, config: p.config, readFile: p.readFile, check: true, openapi: p.openapi });
|
|
40
40
|
diagnostics.push(...out.diagnostics);
|
|
41
41
|
if (out.check)
|
|
42
42
|
units.push(out.check);
|
|
43
43
|
}
|
|
44
|
-
const r = check({ root, units, helpers: p.helpers, types: p.types, openapi: p.openapi });
|
|
44
|
+
const r = check({ root, units, helpers: p.helpers, actions: p.actions, types: p.types, openapi: p.openapi });
|
|
45
45
|
diagnostics.push(...r.diagnostics);
|
|
46
46
|
for (const d of diagnostics)
|
|
47
47
|
console.log(formatDiagnostic({ ...d, file: relative(process.cwd(), d.file) || d.file }) + '\n');
|
|
@@ -47,6 +47,12 @@ export type TypeRef =
|
|
|
47
47
|
kind: 'item';
|
|
48
48
|
resource: string;
|
|
49
49
|
partial?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** an action (`archive`, `pgp.decrypt`): its arguments (`args`) or its awaited result */
|
|
52
|
+
| {
|
|
53
|
+
kind: 'call';
|
|
54
|
+
name: string;
|
|
55
|
+
args?: boolean;
|
|
50
56
|
};
|
|
51
57
|
export interface CheckExpr {
|
|
52
58
|
src: string;
|
|
@@ -70,6 +76,7 @@ export interface CheckExpr {
|
|
|
70
76
|
payload?: {
|
|
71
77
|
names: string[];
|
|
72
78
|
type: TypeRef | null;
|
|
79
|
+
states?: string[];
|
|
73
80
|
};
|
|
74
81
|
/** the compiler reported an error in it (it may not even parse); kept for editor completion, never type-checked */
|
|
75
82
|
broken?: boolean;
|
|
@@ -88,6 +95,11 @@ export interface CheckResource {
|
|
|
88
95
|
label: string;
|
|
89
96
|
schema: unknown;
|
|
90
97
|
};
|
|
98
|
+
/** `call`: the action and the `items` path into its result; with `type`, the result must fit it */
|
|
99
|
+
call?: {
|
|
100
|
+
name: string;
|
|
101
|
+
items: string | null;
|
|
102
|
+
};
|
|
91
103
|
}
|
|
92
104
|
export interface CheckUnit {
|
|
93
105
|
file: string;
|
|
@@ -25,6 +25,8 @@ export interface CompileOptions {
|
|
|
25
25
|
check?: boolean;
|
|
26
26
|
/** parsed OpenAPI specs: resources without `type` are matched against them */
|
|
27
27
|
openapi?: OpenApiSpec[];
|
|
28
|
+
/** the actions module (`data-sn-call`, `<sn-resource call>`), same shape as helpers */
|
|
29
|
+
actions?: HelperManifest;
|
|
28
30
|
}
|
|
29
31
|
export interface CompileResult {
|
|
30
32
|
html: string;
|
|
@@ -45,6 +47,8 @@ export interface CompiledParts {
|
|
|
45
47
|
builtin: string[];
|
|
46
48
|
};
|
|
47
49
|
namespaces: Record<string, string[]>;
|
|
50
|
+
/** action functions called, as written: `archive`, `pgp.decrypt` */
|
|
51
|
+
calls: string[];
|
|
48
52
|
diagnostics: Diagnostic[];
|
|
49
53
|
files: string[];
|
|
50
54
|
/** with `check: true` */
|