clarusjs 0.2.0 → 0.4.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 +5 -15
- package/dist/check/generate.d.ts +11 -0
- package/dist/check/generate.js +164 -0
- package/dist/cli/check.d.ts +17 -0
- package/dist/cli/check.js +91 -0
- package/dist/cli/explain.d.ts +9 -0
- package/dist/cli/explain.js +46 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +38 -0
- package/dist/compiler/emit-client.js +90 -17
- package/dist/compiler/emit-server.js +32 -24
- package/dist/compiler/errors.d.ts +1 -1
- package/dist/compiler/errors.js +1 -1
- package/dist/compiler/html-expr.d.ts +29 -0
- package/dist/compiler/html-expr.js +85 -0
- package/dist/compiler/ir.d.ts +40 -1
- package/dist/compiler/ir.js +155 -33
- package/dist/compiler/parse.d.ts +56 -0
- package/dist/compiler/parse.js +244 -11
- package/dist/compiler/script.d.ts +21 -2
- package/dist/compiler/script.js +67 -6
- package/dist/runtime/attrs.d.ts +5 -0
- package/dist/runtime/attrs.js +56 -0
- package/dist/runtime/dom.d.ts +61 -5
- package/dist/runtime/dom.js +257 -17
- package/dist/runtime/html.d.ts +10 -0
- package/dist/runtime/html.js +30 -0
- package/dist/runtime/index.d.ts +4 -2
- package/dist/runtime/index.js +4 -2
- package/dist/runtime/loader.d.ts +7 -2
- package/dist/runtime/loader.js +125 -28
- package/dist/runtime/serialize.d.ts +8 -0
- package/dist/runtime/serialize.js +93 -0
- package/dist/runtime/server.d.ts +9 -4
- package/dist/runtime/server.js +13 -24
- package/dist/runtime/signals.d.ts +12 -0
- package/dist/runtime/signals.js +44 -1
- package/dist/server/handler.d.ts +1 -1
- package/dist/server/handler.js +2 -2
- package/dist/server/manifest.d.ts +4 -0
- package/dist/server/manifest.js +17 -0
- package/dist/vite/build.d.ts +8 -4
- package/dist/vite/build.js +16 -9
- package/dist/vite/manifest.d.ts +38 -0
- package/dist/vite/manifest.js +69 -0
- package/dist/vite/plugin.js +49 -10
- package/package.json +10 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IRNode } from './ir.ts';
|
|
2
|
+
/**
|
|
3
|
+
* How a template expression becomes code. On the server it is used as written; in the browser signal reads become
|
|
4
|
+
* `.get()`. Inside a @for row's HTML, the row's `item` and `$index` are plain parameters, hence `without()`.
|
|
5
|
+
*/
|
|
6
|
+
export type Rewriter = {
|
|
7
|
+
expr(e: string): string;
|
|
8
|
+
without(names: string[]): Rewriter;
|
|
9
|
+
};
|
|
10
|
+
export declare const asWritten: Rewriter;
|
|
11
|
+
export declare function htmlExpr(nodes: IRNode[], rw?: Rewriter): string;
|
|
12
|
+
/** `render_child({ props }, { slots })` for a component node. */
|
|
13
|
+
export declare function componentCall(n: Extract<IRNode, {
|
|
14
|
+
kind: 'component';
|
|
15
|
+
}>, rw: Rewriter): string;
|
|
16
|
+
/** Index of the branch an @if picks: `(a ? 0 : b ? 1 : 2)`, or -1 when nothing matches and there is no @else. */
|
|
17
|
+
export declare function whichExpr(n: Extract<IRNode, {
|
|
18
|
+
kind: 'if';
|
|
19
|
+
}>, rw: Rewriter): string;
|
|
20
|
+
/** JS expression for an attribute's value: the bound expression, or the parts joined (null/undefined join as ''). */
|
|
21
|
+
export declare function attrValueExpr(value: Extract<IRNode, {
|
|
22
|
+
kind: 'attr';
|
|
23
|
+
}>['value'], rewrite: (e: string) => string): string;
|
|
24
|
+
/** The element's tag, passed only for attributes whose meaning depends on it (SVG animation values). */
|
|
25
|
+
export declare function tagArg(n: Extract<IRNode, {
|
|
26
|
+
kind: 'attr';
|
|
27
|
+
}>): string;
|
|
28
|
+
/** Every node, including those inside @if branches, @for bodies and component children. */
|
|
29
|
+
export declare function walk(nodes: IRNode[]): IRNode[];
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { renderName } from './shared.js';
|
|
2
|
+
import { wrapExpr } from './script.js';
|
|
3
|
+
export const asWritten = { expr: (e) => e, without: () => asWritten };
|
|
4
|
+
export function htmlExpr(nodes, rw = asWritten) {
|
|
5
|
+
const parts = nodes.map((n) => {
|
|
6
|
+
if (n.kind === 'static')
|
|
7
|
+
return JSON.stringify(n.html);
|
|
8
|
+
if (n.kind === 'slot')
|
|
9
|
+
return `(__cl_slots?.[${JSON.stringify(n.name)}] ?? ${n.fallback.length ? `(${htmlExpr(n.fallback, rw)})` : '""'})`;
|
|
10
|
+
if (n.kind === 'attr')
|
|
11
|
+
return `__cl_attrHtml(${JSON.stringify(n.name)}, ${attrValueExpr(n.value, rw.expr)}${tagArg(n)})`;
|
|
12
|
+
if (n.kind === 'if') {
|
|
13
|
+
if (n.block === -1) {
|
|
14
|
+
// Outside islands: a plain choice, no markers.
|
|
15
|
+
const last = n.branches[n.branches.length - 1];
|
|
16
|
+
let expr = last.cond === null ? `(${htmlExpr(last.nodes, rw)})` : '""';
|
|
17
|
+
const conditional = last.cond === null ? n.branches.slice(0, -1) : n.branches;
|
|
18
|
+
for (const b of [...conditional].reverse())
|
|
19
|
+
expr = `(${wrapExpr(rw.expr(b.cond))} ? (${htmlExpr(b.nodes, rw)}) : ${expr})`;
|
|
20
|
+
return expr;
|
|
21
|
+
}
|
|
22
|
+
const branches = n.branches.map((b) => `() => ${htmlExpr(b.nodes, rw)}`).join(', ');
|
|
23
|
+
return `__cl_blockHtml(${n.block}, ${whichExpr(n, rw)}, [${branches}])`;
|
|
24
|
+
}
|
|
25
|
+
if (n.kind === 'for') {
|
|
26
|
+
const list = wrapExpr(rw.expr(n.list));
|
|
27
|
+
const row = `(${n.item}, $index) => ${htmlExpr(n.nodes, rw.without([n.item, '$index']))}`;
|
|
28
|
+
const empty = n.empty ? `() => ${htmlExpr(n.empty, rw)}` : 'null';
|
|
29
|
+
if (n.block >= 0)
|
|
30
|
+
return `__cl_forHtml(${n.block}, ${list}, ${row}, ${empty})`;
|
|
31
|
+
// Outside islands: a plain loop, no markers.
|
|
32
|
+
return `((__cl_l) => __cl_l.length ? __cl_l.map(${row}).join("") : ${n.empty ? `(${htmlExpr(n.empty, rw)})` : '""'})([...(${list} ?? [])])`;
|
|
33
|
+
}
|
|
34
|
+
if (n.kind === 'component') {
|
|
35
|
+
const call = componentCall(n, rw);
|
|
36
|
+
return n.region >= 0 ? `__cl_regionHtml(${n.region}, () => ${call})` : call;
|
|
37
|
+
}
|
|
38
|
+
const value = `__cl_escape(${wrapExpr(rw.expr(n.expr))})`;
|
|
39
|
+
return n.reactive ? `"<!--[${n.index}-->" + ${value} + "<!--]-->"` : value;
|
|
40
|
+
});
|
|
41
|
+
return parts.length ? parts.join(' + ') : '""';
|
|
42
|
+
}
|
|
43
|
+
/** `render_child({ props }, { slots })` for a component node. */
|
|
44
|
+
export function componentCall(n, rw) {
|
|
45
|
+
const props = n.props.map((p) => `${p.name}: ${wrapExpr(rw.expr(p.expr))}`).join(', ');
|
|
46
|
+
const slots = [
|
|
47
|
+
...(n.children.length ? [`"default": ${htmlExpr(n.children, rw)}`] : []),
|
|
48
|
+
...n.fills.map((f) => `${JSON.stringify(f.name)}: ${htmlExpr(f.nodes, rw)}`),
|
|
49
|
+
];
|
|
50
|
+
const slotsArg = slots.length ? `{ ${slots.join(', ')} }` : undefined;
|
|
51
|
+
if (n.attach)
|
|
52
|
+
return `${renderName(n.name)}({ ${props} }, ${slotsArg ?? 'undefined'}, { attach: ${JSON.stringify(n.attach)} })`;
|
|
53
|
+
return slotsArg ? `${renderName(n.name)}({ ${props} }, ${slotsArg})` : `${renderName(n.name)}({ ${props} })`;
|
|
54
|
+
}
|
|
55
|
+
/** Index of the branch an @if picks: `(a ? 0 : b ? 1 : 2)`, or -1 when nothing matches and there is no @else. */
|
|
56
|
+
export function whichExpr(n, rw) {
|
|
57
|
+
let expr = n.branches[n.branches.length - 1].cond === null ? String(n.branches.length - 1) : '-1';
|
|
58
|
+
for (let i = n.branches.length - 1; i >= 0; i--) {
|
|
59
|
+
const cond = n.branches[i].cond;
|
|
60
|
+
if (cond !== null)
|
|
61
|
+
expr = `${wrapExpr(rw.expr(cond))} ? ${i} : ${expr}`;
|
|
62
|
+
}
|
|
63
|
+
return `(${expr})`;
|
|
64
|
+
}
|
|
65
|
+
/** JS expression for an attribute's value: the bound expression, or the parts joined (null/undefined join as ''). */
|
|
66
|
+
export function attrValueExpr(value, rewrite) {
|
|
67
|
+
if ('expr' in value)
|
|
68
|
+
return `(${rewrite(value.expr)})`;
|
|
69
|
+
return `[${value.parts.map((p) => (typeof p === 'string' ? JSON.stringify(p) : `(${rewrite(p.expr)})`)).join(', ')}].join("")`;
|
|
70
|
+
}
|
|
71
|
+
/** The element's tag, passed only for attributes whose meaning depends on it (SVG animation values). */
|
|
72
|
+
export function tagArg(n) {
|
|
73
|
+
return ['to', 'from', 'values', 'by'].includes(n.name.toLowerCase()) ? `, ${JSON.stringify(n.tag)}` : '';
|
|
74
|
+
}
|
|
75
|
+
/** Every node, including those inside @if branches, @for bodies and component children. */
|
|
76
|
+
export function walk(nodes) {
|
|
77
|
+
return nodes.flatMap((n) => [
|
|
78
|
+
n,
|
|
79
|
+
...(n.kind === 'if' ? n.branches.flatMap((b) => walk(b.nodes))
|
|
80
|
+
: n.kind === 'for' ? [...walk(n.nodes), ...walk(n.empty ?? [])]
|
|
81
|
+
: n.kind === 'component' ? [...walk(n.children), ...n.fills.flatMap((f) => walk(f.nodes))]
|
|
82
|
+
: n.kind === 'slot' ? walk(n.fallback)
|
|
83
|
+
: []),
|
|
84
|
+
]);
|
|
85
|
+
}
|
package/dist/compiler/ir.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DataImport, type ImportDecl, type InputDecl, type ParsedFile } from './parse.ts';
|
|
2
2
|
import { type Signals } from './script.ts';
|
|
3
3
|
export type IRNode = {
|
|
4
4
|
kind: 'static';
|
|
@@ -16,13 +16,50 @@ export type IRNode = {
|
|
|
16
16
|
expr: string;
|
|
17
17
|
}[];
|
|
18
18
|
children: IRNode[];
|
|
19
|
+
fills: {
|
|
20
|
+
name: string;
|
|
21
|
+
nodes: IRNode[];
|
|
22
|
+
}[];
|
|
23
|
+
region: number;
|
|
24
|
+
attach?: string;
|
|
19
25
|
} | {
|
|
20
26
|
kind: 'slot';
|
|
27
|
+
name: string;
|
|
28
|
+
fallback: IRNode[];
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'attr';
|
|
31
|
+
name: string;
|
|
32
|
+
value: {
|
|
33
|
+
expr: string;
|
|
34
|
+
} | {
|
|
35
|
+
parts: (string | {
|
|
36
|
+
expr: string;
|
|
37
|
+
})[];
|
|
38
|
+
};
|
|
39
|
+
reactive: boolean;
|
|
40
|
+
index: number;
|
|
41
|
+
tag: string;
|
|
42
|
+
} | {
|
|
43
|
+
kind: 'if';
|
|
44
|
+
branches: {
|
|
45
|
+
cond: string | null;
|
|
46
|
+
nodes: IRNode[];
|
|
47
|
+
}[];
|
|
48
|
+
block: number;
|
|
49
|
+
} | {
|
|
50
|
+
kind: 'for';
|
|
51
|
+
item: string;
|
|
52
|
+
list: string;
|
|
53
|
+
track: string | null;
|
|
54
|
+
nodes: IRNode[];
|
|
55
|
+
empty: IRNode[] | null;
|
|
56
|
+
block: number;
|
|
21
57
|
};
|
|
22
58
|
export type IREvent = {
|
|
23
59
|
index: number;
|
|
24
60
|
type: string;
|
|
25
61
|
handler: string;
|
|
62
|
+
scope?: string;
|
|
26
63
|
};
|
|
27
64
|
export type ComponentIR = {
|
|
28
65
|
file: string;
|
|
@@ -30,6 +67,8 @@ export type ComponentIR = {
|
|
|
30
67
|
inputs: InputDecl[];
|
|
31
68
|
imports: ImportDecl[];
|
|
32
69
|
script?: string;
|
|
70
|
+
scriptImports: string[];
|
|
71
|
+
dataImports: DataImport[];
|
|
33
72
|
signals: Signals;
|
|
34
73
|
stateful: boolean;
|
|
35
74
|
nodes: IRNode[];
|
package/dist/compiler/ir.js
CHANGED
|
@@ -1,21 +1,37 @@
|
|
|
1
1
|
// Syntax tree → IR: static HTML chunks, holes (reactive or not), child components, events. Build-time checks live here.
|
|
2
2
|
import { ClarusError } from './errors.js';
|
|
3
|
-
import {
|
|
3
|
+
import { importNames } from './parse.js';
|
|
4
|
+
import { analyzeScript, checkExpr, checkHandler, referencesNames, scriptNames, splitImports } from './script.js';
|
|
5
|
+
const ATTACH_MODES = ['load', 'idle', 'visible', 'interaction'];
|
|
4
6
|
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr']);
|
|
5
7
|
export function lower(parsed, file, source) {
|
|
6
8
|
const fail = (text, at) => { throw new ClarusError(text, file, source, at); };
|
|
7
9
|
const stateful = !!parsed.script;
|
|
8
|
-
const
|
|
10
|
+
const split = parsed.script ? splitImports(parsed.script.code, file, source, parsed.script.start) : { imports: [], body: '' };
|
|
11
|
+
const signals = parsed.script ? analyzeScript(split.body, file, source, parsed.script.start) : new Map();
|
|
9
12
|
// A hole is reactive if it reads anything the script declares: a signal directly, or a function/value that may read one.
|
|
10
13
|
// text()'s effect tracks the real dependencies, so over-marking only costs a pair of comment markers.
|
|
11
14
|
const scriptScope = parsed.script ? new Set([...signals.keys(), ...scriptNames(parsed.script.code)]) : new Set();
|
|
15
|
+
for (const d of parsed.dataImports) {
|
|
16
|
+
// In an island the import is bundled for the browser, like a script import.
|
|
17
|
+
if (stateful && d.from.startsWith('node:'))
|
|
18
|
+
fail(`\`${d.from}\` can't be imported in an island: its imports are bundled for the browser`, d.start);
|
|
19
|
+
if (stateful)
|
|
20
|
+
for (const name of importNames(d.clause))
|
|
21
|
+
scriptScope.add(name);
|
|
22
|
+
}
|
|
12
23
|
const components = new Set(parsed.imports.map((i) => i.name));
|
|
13
24
|
const nodes = [];
|
|
14
25
|
let out = nodes; // where lowering writes; switched while lowering a component's children
|
|
15
|
-
let slotCount = 0;
|
|
16
26
|
const events = [];
|
|
17
27
|
let holeCount = 0;
|
|
18
28
|
let eventElementCount = 0;
|
|
29
|
+
let attrElementCount = 0;
|
|
30
|
+
let blockCount = 0;
|
|
31
|
+
let forCount = 0;
|
|
32
|
+
let regionCount = 0;
|
|
33
|
+
let loopNames = new Set(); // @for item and $index names in scope: they make holes inside rows reactive
|
|
34
|
+
let scopeKey; // the @if branch being lowered, for events
|
|
19
35
|
const emitStatic = (html) => {
|
|
20
36
|
const last = out[out.length - 1];
|
|
21
37
|
if (last?.kind === 'static')
|
|
@@ -31,66 +47,170 @@ export function lower(parsed, file, source) {
|
|
|
31
47
|
function visit(node, rawText) {
|
|
32
48
|
if (node.type === 'text')
|
|
33
49
|
return emitStatic(node.value);
|
|
50
|
+
if (node.type === 'fill')
|
|
51
|
+
return fail('@slot only works directly inside a component tag', node.start);
|
|
52
|
+
if (node.type === 'if') {
|
|
53
|
+
const block = stateful ? blockCount++ : -1;
|
|
54
|
+
const parent = out;
|
|
55
|
+
const parentScope = scopeKey;
|
|
56
|
+
const branches = node.branches.map((b, i) => {
|
|
57
|
+
if (b.cond !== null)
|
|
58
|
+
checkExpr(b.cond, file, source, b.condStart);
|
|
59
|
+
const branchNodes = [];
|
|
60
|
+
out = branchNodes;
|
|
61
|
+
if (stateful)
|
|
62
|
+
scopeKey = `${block}.${i}`;
|
|
63
|
+
b.children.forEach((child) => visit(child, rawText));
|
|
64
|
+
return { cond: b.cond, nodes: branchNodes };
|
|
65
|
+
});
|
|
66
|
+
out = parent;
|
|
67
|
+
scopeKey = parentScope;
|
|
68
|
+
out.push({ kind: 'if', branches, block });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (node.type === 'for') {
|
|
72
|
+
checkExpr(node.list, file, source, node.listStart);
|
|
73
|
+
if (node.track !== null)
|
|
74
|
+
checkExpr(node.track, file, source, node.trackStart);
|
|
75
|
+
const block = stateful ? forCount++ : -1;
|
|
76
|
+
const parent = out;
|
|
77
|
+
const parentScope = scopeKey;
|
|
78
|
+
const parentLoop = loopNames;
|
|
79
|
+
const lowerInto = (children, key, names) => {
|
|
80
|
+
const list = [];
|
|
81
|
+
out = list;
|
|
82
|
+
loopNames = names;
|
|
83
|
+
if (stateful)
|
|
84
|
+
scopeKey = key;
|
|
85
|
+
children.forEach((child) => visit(child, rawText));
|
|
86
|
+
return list;
|
|
87
|
+
};
|
|
88
|
+
const nodes = lowerInto(node.children, `f${block}`, new Set([...parentLoop, node.item, '$index']));
|
|
89
|
+
const empty = node.empty ? lowerInto(node.empty, `f${block}e`, parentLoop) : null;
|
|
90
|
+
out = parent;
|
|
91
|
+
scopeKey = parentScope;
|
|
92
|
+
loopNames = parentLoop;
|
|
93
|
+
out.push({ kind: 'for', item: node.item, list: node.list, track: node.track, nodes, empty, block });
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (node.type === 'raw')
|
|
97
|
+
return emitStatic(node.value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'));
|
|
34
98
|
if (node.type === 'expr') {
|
|
35
99
|
checkExpr(node.expr, file, source, node.start);
|
|
36
|
-
const reactive = referencesNames(node.expr, scriptScope);
|
|
100
|
+
const reactive = stateful && (referencesNames(node.expr, scriptScope) || referencesNames(node.expr, loopNames));
|
|
37
101
|
if (reactive && rawText)
|
|
38
102
|
fail(`reactive {{ }} inside <${rawText}> is not supported yet (it cannot hold update markers)`, node.start);
|
|
39
103
|
out.push({ kind: 'hole', expr: node.expr, reactive, index: reactive ? holeCount++ : -1 });
|
|
40
104
|
return;
|
|
41
105
|
}
|
|
42
106
|
if (components.has(node.tag)) {
|
|
43
|
-
if (stateful)
|
|
44
|
-
fail('components inside a stateful component are not supported yet', node.start);
|
|
45
107
|
const props = [];
|
|
108
|
+
let attach;
|
|
46
109
|
for (const a of node.attrs) {
|
|
47
110
|
if (a.kind === 'prop') {
|
|
48
111
|
checkExpr(a.expr, file, source, a.start);
|
|
49
112
|
props.push({ name: a.name, expr: a.expr });
|
|
50
113
|
}
|
|
114
|
+
else if (a.kind === 'static' && a.name === 'attach') {
|
|
115
|
+
// When the island's code loads in the browser (see runtime/loader.ts).
|
|
116
|
+
if (typeof a.value !== 'string' || !ATTACH_MODES.includes(a.value))
|
|
117
|
+
fail(`attach must be one of ${ATTACH_MODES.join(', ')}`, a.start);
|
|
118
|
+
attach = a.value;
|
|
119
|
+
}
|
|
51
120
|
else
|
|
52
|
-
fail(`component <${node.tag}> only takes [prop] bindings`, a.start);
|
|
121
|
+
fail(`component <${node.tag}> only takes [prop] bindings and attach="…"`, a.start);
|
|
53
122
|
}
|
|
54
|
-
//
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
123
|
+
// Content is rendered here, in this component's scope, and handed to the child for its slots.
|
|
124
|
+
const hasContent = node.children.some((c) => c.type !== 'text' || c.value.trim());
|
|
125
|
+
const readsState = props.some((p) => referencesNames(p.expr, scriptScope) || referencesNames(p.expr, loopNames));
|
|
126
|
+
const region = stateful && (hasContent || readsState) ? regionCount++ : -1;
|
|
127
|
+
const parentScope = scopeKey;
|
|
128
|
+
if (region >= 0)
|
|
129
|
+
scopeKey = `c${region}`;
|
|
130
|
+
const lowerList = (list) => {
|
|
131
|
+
const nodes_ = [];
|
|
132
|
+
const parent = out;
|
|
133
|
+
out = nodes_;
|
|
134
|
+
list.forEach((child) => visit(child, rawText));
|
|
135
|
+
out = parent;
|
|
136
|
+
return nodes_;
|
|
137
|
+
};
|
|
138
|
+
const fills = [];
|
|
139
|
+
for (const child of node.children) {
|
|
140
|
+
if (child.type !== 'fill')
|
|
141
|
+
continue;
|
|
142
|
+
if (fills.some((f) => f.name === child.name))
|
|
143
|
+
fail(`slot "${child.name}" is filled twice`, child.start);
|
|
144
|
+
fills.push({ name: child.name, nodes: lowerList(child.children) });
|
|
145
|
+
}
|
|
146
|
+
const rest = node.children.filter((c) => c.type !== 'fill');
|
|
147
|
+
// Whitespace around @slot blocks is not content for the default slot.
|
|
148
|
+
const children = rest.every((c) => c.type === 'text' && !c.value.trim()) ? [] : lowerList(rest);
|
|
149
|
+
scopeKey = parentScope;
|
|
150
|
+
out.push({ kind: 'component', name: node.tag, props, children, fills, region, ...(attach ? { attach } : {}) });
|
|
61
151
|
return;
|
|
62
152
|
}
|
|
63
153
|
if (node.tag === 'slot') {
|
|
64
154
|
if (stateful)
|
|
65
155
|
fail('<slot /> is only supported in components without <script>', node.start);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
156
|
+
let name = 'default';
|
|
157
|
+
for (const a of node.attrs) {
|
|
158
|
+
if (a.kind === 'static' && a.name === 'name' && typeof a.value === 'string' && a.value)
|
|
159
|
+
name = a.value;
|
|
160
|
+
else
|
|
161
|
+
fail('<slot> only takes name="…"', a.start);
|
|
162
|
+
}
|
|
163
|
+
// Fallback content belongs to this component: its holes read this component's inputs.
|
|
164
|
+
const fallback = [];
|
|
165
|
+
const parent = out;
|
|
166
|
+
out = fallback;
|
|
167
|
+
node.children.forEach((child) => visit(child, rawText));
|
|
168
|
+
out = parent;
|
|
169
|
+
out.push({ kind: 'slot', name, fallback });
|
|
73
170
|
return;
|
|
74
171
|
}
|
|
75
|
-
|
|
172
|
+
emitStatic(`<${node.tag}`);
|
|
76
173
|
const elementEvents = node.attrs.filter((a) => a.kind === 'event');
|
|
174
|
+
const dynamic = [];
|
|
77
175
|
for (const a of node.attrs) {
|
|
78
176
|
if (a.kind === 'static')
|
|
79
|
-
|
|
80
|
-
else if (a.kind === '
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
fail('event bindings need a <script> with state; this component is static', a.start);
|
|
84
|
-
else
|
|
177
|
+
emitStatic(a.value === true ? ` ${a.name}` : ` ${a.name}="${a.value}"`);
|
|
178
|
+
else if (a.kind === 'event') {
|
|
179
|
+
if (!stateful)
|
|
180
|
+
fail('event bindings need a <script> with state; this component is static', a.start);
|
|
85
181
|
checkHandler(a.expr, file, source, a.start);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
if (/^on/i.test(a.name))
|
|
185
|
+
fail(`use (${a.name.slice(2).toLowerCase()}) for events, not an ${a.name} attribute`, a.start);
|
|
186
|
+
// srcdoc is a whole HTML document: binding data into it is HTML injection, so it stays static.
|
|
187
|
+
if (a.name.toLowerCase() === 'srcdoc')
|
|
188
|
+
fail('srcdoc cannot be bound; write it as a static attribute', a.start);
|
|
189
|
+
const exprs = a.kind === 'prop' ? [{ expr: a.expr, start: a.start }] : a.parts.filter((p) => typeof p !== 'string');
|
|
190
|
+
for (const e of exprs)
|
|
191
|
+
checkExpr(e.expr, file, source, e.start);
|
|
192
|
+
const reactive = stateful && exprs.some((e) => referencesNames(e.expr, scriptScope) || referencesNames(e.expr, loopNames));
|
|
193
|
+
const value = a.kind === 'prop' ? { expr: a.expr } : { parts: a.parts.map((p) => (typeof p === 'string' ? p : { expr: p.expr })) };
|
|
194
|
+
const node_ = { kind: 'attr', name: a.name, value, reactive, index: -1, tag: node.tag };
|
|
195
|
+
if (reactive)
|
|
196
|
+
dynamic.push(node_);
|
|
197
|
+
out.push(node_);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
// One marker per element for its reactive attributes, one for its events.
|
|
201
|
+
if (dynamic.length) {
|
|
202
|
+
const index = attrElementCount++;
|
|
203
|
+
for (const d of dynamic)
|
|
204
|
+
d.index = index;
|
|
205
|
+
emitStatic(` data-cl-a="${index}"`);
|
|
86
206
|
}
|
|
87
207
|
if (elementEvents.length) {
|
|
88
208
|
const index = eventElementCount++;
|
|
89
|
-
|
|
209
|
+
emitStatic(` data-cl-e="${index}"`);
|
|
90
210
|
for (const a of elementEvents)
|
|
91
|
-
events.push({ index, type: a.name, handler: a.kind === 'event' ? a.expr : '' });
|
|
211
|
+
events.push({ index, type: a.name, handler: a.kind === 'event' ? a.expr : '', scope: scopeKey });
|
|
92
212
|
}
|
|
93
|
-
emitStatic(
|
|
213
|
+
emitStatic('>');
|
|
94
214
|
node.children.forEach((child) => visit(child, RAW_TEXT.has(node.tag) ? node.tag : rawText));
|
|
95
215
|
if (!VOID.has(node.tag))
|
|
96
216
|
emitStatic(`</${node.tag}>`);
|
|
@@ -98,10 +218,12 @@ export function lower(parsed, file, source) {
|
|
|
98
218
|
parsed.nodes.forEach((n) => visit(n));
|
|
99
219
|
return {
|
|
100
220
|
file,
|
|
101
|
-
name: file.split('/').pop().replace(/\.
|
|
221
|
+
name: file.split('/').pop().replace(/\.tscl$/, ''),
|
|
102
222
|
inputs: parsed.inputs,
|
|
103
223
|
imports: parsed.imports,
|
|
104
|
-
script: parsed.script
|
|
224
|
+
script: parsed.script ? split.body : undefined,
|
|
225
|
+
scriptImports: split.imports.map((imp) => imp.text),
|
|
226
|
+
dataImports: parsed.dataImports,
|
|
105
227
|
signals,
|
|
106
228
|
stateful,
|
|
107
229
|
nodes,
|
package/dist/compiler/parse.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ export type Attr = {
|
|
|
13
13
|
name: string;
|
|
14
14
|
expr: string;
|
|
15
15
|
start: number;
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'interp';
|
|
18
|
+
name: string;
|
|
19
|
+
parts: (string | {
|
|
20
|
+
expr: string;
|
|
21
|
+
start: number;
|
|
22
|
+
})[];
|
|
23
|
+
start: number;
|
|
16
24
|
};
|
|
17
25
|
export type TemplateNode = {
|
|
18
26
|
type: 'element';
|
|
@@ -28,12 +36,56 @@ export type TemplateNode = {
|
|
|
28
36
|
type: 'expr';
|
|
29
37
|
expr: string;
|
|
30
38
|
start: number;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'raw';
|
|
41
|
+
value: string;
|
|
42
|
+
start: number;
|
|
43
|
+
} | {
|
|
44
|
+
type: 'if';
|
|
45
|
+
branches: {
|
|
46
|
+
cond: string | null;
|
|
47
|
+
condStart: number;
|
|
48
|
+
children: TemplateNode[];
|
|
49
|
+
}[];
|
|
50
|
+
start: number;
|
|
51
|
+
} | {
|
|
52
|
+
type: 'fill';
|
|
53
|
+
name: string;
|
|
54
|
+
children: TemplateNode[];
|
|
55
|
+
start: number;
|
|
56
|
+
} | {
|
|
57
|
+
type: 'for';
|
|
58
|
+
item: string;
|
|
59
|
+
list: string;
|
|
60
|
+
track: string | null;
|
|
61
|
+
headStart: number;
|
|
62
|
+
itemStart: number;
|
|
63
|
+
listStart: number;
|
|
64
|
+
trackStart: number;
|
|
65
|
+
children: TemplateNode[];
|
|
66
|
+
empty: TemplateNode[] | null;
|
|
67
|
+
start: number;
|
|
31
68
|
};
|
|
32
69
|
export type InputDecl = {
|
|
33
70
|
name: string;
|
|
34
71
|
type: string;
|
|
35
72
|
default?: string;
|
|
36
73
|
start: number;
|
|
74
|
+
nameStart: number;
|
|
75
|
+
typeStart: number;
|
|
76
|
+
defaultStart?: number;
|
|
77
|
+
};
|
|
78
|
+
export type TypeImport = {
|
|
79
|
+
clause: string;
|
|
80
|
+
from: string;
|
|
81
|
+
start: number;
|
|
82
|
+
clauseStart: number;
|
|
83
|
+
};
|
|
84
|
+
export type DataImport = {
|
|
85
|
+
clause: string;
|
|
86
|
+
from: string;
|
|
87
|
+
start: number;
|
|
88
|
+
clauseStart: number;
|
|
37
89
|
};
|
|
38
90
|
export type ImportDecl = {
|
|
39
91
|
name: string;
|
|
@@ -43,6 +95,8 @@ export type ImportDecl = {
|
|
|
43
95
|
export type ParsedFile = {
|
|
44
96
|
inputs: InputDecl[];
|
|
45
97
|
imports: ImportDecl[];
|
|
98
|
+
typeImports: TypeImport[];
|
|
99
|
+
dataImports: DataImport[];
|
|
46
100
|
script?: {
|
|
47
101
|
code: string;
|
|
48
102
|
start: number;
|
|
@@ -50,3 +104,5 @@ export type ParsedFile = {
|
|
|
50
104
|
nodes: TemplateNode[];
|
|
51
105
|
};
|
|
52
106
|
export declare function parse(source: string, file: string): ParsedFile;
|
|
107
|
+
/** Local names an import clause binds: `{ a, b as c, type T }` → a, c; `x` → x. */
|
|
108
|
+
export declare function importNames(clause: string): string[];
|