clarusjs 0.8.1 → 0.10.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/dist/check/generate.d.ts +20 -4
- package/dist/check/generate.js +157 -21
- package/dist/cli/check.d.ts +13 -0
- package/dist/cli/check.js +65 -18
- package/dist/cli/index.js +9 -3
- package/dist/cli/mapper.d.ts +25 -0
- package/dist/cli/mapper.js +65 -0
- package/dist/cli/project.d.ts +5 -0
- package/dist/cli/project.js +21 -0
- package/dist/compiler/emit-client.js +33 -9
- package/dist/compiler/emit-server.js +3 -2
- package/dist/compiler/errors.d.ts +14 -1
- package/dist/compiler/html-expr.d.ts +21 -0
- package/dist/compiler/html-expr.js +29 -3
- package/dist/compiler/index.d.ts +28 -3
- package/dist/compiler/index.js +36 -24
- package/dist/compiler/ir.d.ts +19 -1
- package/dist/compiler/ir.js +261 -11
- package/dist/compiler/parse.d.ts +52 -1
- package/dist/compiler/parse.js +145 -6
- package/dist/compiler/script.d.ts +22 -1
- package/dist/compiler/script.js +108 -5
- package/dist/fetch/index.d.ts +149 -0
- package/dist/fetch/index.js +312 -0
- package/dist/forms/index.d.ts +26 -0
- package/dist/forms/index.js +374 -0
- package/dist/forms/types.d.ts +111 -0
- package/dist/forms/types.js +1 -0
- package/dist/runtime/attrs.js +10 -2
- package/dist/runtime/dom.d.ts +26 -11
- package/dist/runtime/dom.js +102 -14
- package/dist/runtime/env.d.ts +14 -0
- package/dist/runtime/env.js +14 -0
- package/dist/runtime/html.d.ts +9 -7
- package/dist/runtime/html.js +12 -7
- package/dist/runtime/index.d.ts +3 -2
- package/dist/runtime/index.js +3 -2
- package/dist/runtime/loader.d.ts +1 -1
- package/dist/runtime/loader.js +36 -5
- package/dist/runtime/navigate.d.ts +5 -0
- package/dist/runtime/navigation.d.ts +85 -13
- package/dist/runtime/navigation.js +77 -11
- package/dist/runtime/router.js +10 -1
- package/dist/runtime/serialize.d.ts +1 -1
- package/dist/runtime/serialize.js +1 -1
- package/dist/runtime/server.d.ts +9 -2
- package/dist/runtime/server.js +23 -5
- package/dist/runtime/signals.d.ts +98 -3
- package/dist/runtime/signals.js +112 -6
- package/dist/server/handler.d.ts +14 -1
- package/dist/server/handler.js +2 -2
- package/dist/server/head.d.ts +4 -1
- package/dist/server/manifest.d.ts +14 -1
- package/dist/server/views.d.ts +64 -6
- package/dist/server/views.js +46 -0
- package/dist/vite/build.d.ts +1 -1
- package/dist/vite/manifest.d.ts +12 -0
- package/dist/vite/plugin.d.ts +30 -2
- package/dist/vite/plugin.js +17 -2
- package/dist/vite/sitemap.d.ts +1 -0
- package/package.json +19 -1
package/dist/compiler/ir.js
CHANGED
|
@@ -1,14 +1,54 @@
|
|
|
1
|
+
import { parseExpressionAt } from 'acorn';
|
|
1
2
|
import { ClarusError } from './errors.js';
|
|
2
|
-
import { importNames } from './parse.js';
|
|
3
|
-
import { analyzeScript, checkExpr, checkHandler, referencesNames, scriptNames, splitImports } from './script.js';
|
|
3
|
+
import { FOR_IMPLICIT, importNames } from './parse.js';
|
|
4
|
+
import { analyzeScript, writesTo, checkExpr, checkHandler, referencesNames, scriptNames, splitImports } from './script.js';
|
|
4
5
|
const ATTACH_MODES = ['load', 'idle', 'visible', 'interaction'];
|
|
5
6
|
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr']);
|
|
6
|
-
export function lower(parsed, file, source) {
|
|
7
|
+
export function lower(parsed, file, source, options = {}) {
|
|
7
8
|
const fail = (text, at) => { throw new ClarusError(text, file, source, at); };
|
|
8
|
-
const stateful = !!parsed.script;
|
|
9
|
+
const stateful = !!parsed.script || parsed.inputs.some((i) => i.model);
|
|
10
|
+
const warnings = [];
|
|
11
|
+
const modelNames = new Set(parsed.inputs.filter((i) => i.model).map((i) => i.name));
|
|
9
12
|
const split = parsed.script ? splitImports(parsed.script.code, file, source, parsed.script.start) : { imports: [], body: '' };
|
|
10
13
|
const signals = parsed.script ? analyzeScript(split.body, file, source, parsed.script.start) : new Map();
|
|
11
14
|
const scriptScope = parsed.script ? new Set([...signals.keys(), ...scriptNames(parsed.script.code)]) : new Set();
|
|
15
|
+
const inputNames = new Set(stateful ? parsed.inputs.filter((i) => !i.model).map((i) => i.name).filter((name) => !signals.has(name)) : []);
|
|
16
|
+
for (const name of inputNames) {
|
|
17
|
+
signals.set(name, 'input');
|
|
18
|
+
scriptScope.add(name);
|
|
19
|
+
}
|
|
20
|
+
for (const i of stateful ? parsed.inputs.filter((x) => x.model && !signals.has(x.name)) : []) {
|
|
21
|
+
signals.set(i.name, 'model');
|
|
22
|
+
scriptScope.add(i.name);
|
|
23
|
+
}
|
|
24
|
+
const writeBack = (path, value, name, at) => {
|
|
25
|
+
if (!stateful)
|
|
26
|
+
fail(`[(${name})] needs a <script> with state to write back to`, at);
|
|
27
|
+
const invalid = () => fail(`[(${name})] needs a variable or a property path (form.name, rows[$index].name) to write back to, not "${path}"`, at);
|
|
28
|
+
const parsed = pathSegments(path) ?? invalid();
|
|
29
|
+
const { root, segments } = parsed;
|
|
30
|
+
if (loopNames.has(root))
|
|
31
|
+
fail(`[(${name})] can't write back to the row variable "${root}": bind the list by index, like [(${name})]="rows[$index].${path.slice(root.length).replace(/^\./, '') || 'name'}"`, at);
|
|
32
|
+
const bases = [root];
|
|
33
|
+
for (const seg of segments)
|
|
34
|
+
bases.push('key' in seg ? `${bases.at(-1)}.${seg.key}` : `${bases.at(-1)}[${seg.index}]`);
|
|
35
|
+
let next = value;
|
|
36
|
+
for (let k = segments.length - 1; k >= 0; k--) {
|
|
37
|
+
const seg = segments[k];
|
|
38
|
+
const parent = bases[k];
|
|
39
|
+
next = 'key' in seg
|
|
40
|
+
? `{ ...${parent}, ${seg.key}: ${next} }`
|
|
41
|
+
: `(Array.isArray(${parent}) ? ${parent}.map((__cl_x, __cl_i) => __cl_i === (${seg.index}) ? ${next} : __cl_x) : { ...${parent}, [${seg.index}]: ${next} })`;
|
|
42
|
+
}
|
|
43
|
+
const statement = `${root} = ${next}`;
|
|
44
|
+
for (const w of writesTo(statement, inputNames))
|
|
45
|
+
readOnly(w.name, at);
|
|
46
|
+
return statement;
|
|
47
|
+
};
|
|
48
|
+
const readOnly = (name, at) => fail(`@input ${name} is read-only: inputs belong to the parent. Copy it to change it here: let local = state(${name})`, at);
|
|
49
|
+
if (parsed.script && inputNames.size)
|
|
50
|
+
for (const w of writesTo(parsed.script.code, inputNames))
|
|
51
|
+
readOnly(w.name, parsed.script.start + w.start);
|
|
12
52
|
for (const d of parsed.dataImports) {
|
|
13
53
|
if (stateful && d.from.startsWith('node:'))
|
|
14
54
|
fail(`\`${d.from}\` can't be imported in an island: its imports are bundled for the browser`, d.start);
|
|
@@ -23,6 +63,18 @@ export function lower(parsed, file, source) {
|
|
|
23
63
|
let holeCount = 0;
|
|
24
64
|
let eventElementCount = 0;
|
|
25
65
|
let attrElementCount = 0;
|
|
66
|
+
let formCount = 0;
|
|
67
|
+
let selectModel;
|
|
68
|
+
let engineScope = null;
|
|
69
|
+
const sameRoot = (path, at) => {
|
|
70
|
+
const root = pathSegments(path)?.root;
|
|
71
|
+
if (!engineScope || !root)
|
|
72
|
+
return;
|
|
73
|
+
if (engineScope.root === null)
|
|
74
|
+
engineScope.root = root;
|
|
75
|
+
else if (engineScope.root !== root)
|
|
76
|
+
fail(`fields under [engine] bind one object: found ${engineScope.root} and ${root}`, at);
|
|
77
|
+
};
|
|
26
78
|
let blockCount = 0;
|
|
27
79
|
let forCount = 0;
|
|
28
80
|
let regionCount = 0;
|
|
@@ -39,9 +91,12 @@ export function lower(parsed, file, source) {
|
|
|
39
91
|
if (input.default !== undefined)
|
|
40
92
|
checkExpr(input.default, file, source, input.start);
|
|
41
93
|
const RAW_TEXT = new Set(['textarea', 'title', 'style']);
|
|
94
|
+
const KEEP_WHITESPACE = new Set(['pre', 'textarea', 'script', 'style']);
|
|
95
|
+
let keep = 0;
|
|
96
|
+
const text = (value) => options.collapseWhitespace && !keep ? value.replace(/[ \t\n\f\r]+/g, (run) => (run.includes('\n') ? '\n' : ' ')) : value;
|
|
42
97
|
function visit(node, rawText) {
|
|
43
98
|
if (node.type === 'text')
|
|
44
|
-
return emitStatic(node.value);
|
|
99
|
+
return emitStatic(text(node.value));
|
|
45
100
|
if (node.type === 'fill')
|
|
46
101
|
return fail('@slot only works directly inside a component tag', node.start);
|
|
47
102
|
if (node.type === 'if') {
|
|
@@ -80,7 +135,11 @@ export function lower(parsed, file, source) {
|
|
|
80
135
|
children.forEach((child) => visit(child, rawText));
|
|
81
136
|
return list;
|
|
82
137
|
};
|
|
83
|
-
|
|
138
|
+
if (node.track !== null && referencesNames(node.track, new Set(FOR_IMPLICIT.filter((x) => x !== '$index'))))
|
|
139
|
+
fail('track can use the item and $index, not $count, $first, $last, $even or $odd (a key must not change when the list grows)', node.trackStart);
|
|
140
|
+
if (block >= 0 && node.track === null)
|
|
141
|
+
warnings.push(new ClarusError('@for without track: rows are matched by position, so a row\'s state moves to another item when the list changes order; add track item.id (or another unique key)', file, source, node.start));
|
|
142
|
+
const nodes = lowerInto(node.children, `f${block}`, new Set([...parentLoop, node.item, ...FOR_IMPLICIT]));
|
|
84
143
|
const empty = node.empty ? lowerInto(node.empty, `f${block}e`, parentLoop) : null;
|
|
85
144
|
out = parent;
|
|
86
145
|
scopeKey = parentScope;
|
|
@@ -106,23 +165,41 @@ export function lower(parsed, file, source) {
|
|
|
106
165
|
}
|
|
107
166
|
if (components.has(node.tag)) {
|
|
108
167
|
const props = [];
|
|
168
|
+
const models = [];
|
|
109
169
|
let attach;
|
|
170
|
+
let cls;
|
|
171
|
+
const paths = [];
|
|
110
172
|
for (const a of node.attrs) {
|
|
173
|
+
if (a.name === 'class') {
|
|
174
|
+
if (a.kind !== 'static' || typeof a.value !== 'string')
|
|
175
|
+
fail('a component takes a static class="…" only ([class] and {{ }} in it aren\'t supported on a component yet)', a.start);
|
|
176
|
+
else
|
|
177
|
+
cls = a.value;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
111
180
|
if (a.kind === 'prop') {
|
|
112
181
|
checkExpr(a.expr, file, source, a.start);
|
|
113
182
|
props.push({ name: a.name, expr: a.expr });
|
|
114
183
|
}
|
|
184
|
+
else if (a.kind === 'model') {
|
|
185
|
+
const set = writeBack(a.expr, '__cl_v', a.name, a.start);
|
|
186
|
+
checkExpr(a.expr, file, source, a.start);
|
|
187
|
+
props.push({ name: a.name, expr: a.expr });
|
|
188
|
+
sameRoot(a.expr, a.start);
|
|
189
|
+
paths.push({ name: a.name, expr: modelPath(a.expr, modelNames) });
|
|
190
|
+
models.push({ name: a.name, set });
|
|
191
|
+
}
|
|
115
192
|
else if (a.kind === 'static' && a.name === 'attach') {
|
|
116
193
|
if (typeof a.value !== 'string' || !ATTACH_MODES.includes(a.value))
|
|
117
194
|
fail(`attach must be one of ${ATTACH_MODES.join(', ')}`, a.start);
|
|
118
195
|
attach = a.value;
|
|
119
196
|
}
|
|
120
197
|
else
|
|
121
|
-
fail(`component <${node.tag}> only takes [prop] bindings and attach="…"`, a.start);
|
|
198
|
+
fail(`component <${node.tag}> only takes [prop] and [(prop)] bindings and attach="…"`, a.start);
|
|
122
199
|
}
|
|
123
200
|
const hasContent = node.children.some((c) => c.type !== 'text' || c.value.trim());
|
|
124
201
|
const readsState = props.some((p) => referencesNames(p.expr, scriptScope) || referencesNames(p.expr, loopNames));
|
|
125
|
-
const region = stateful && (hasContent || readsState) ? regionCount++ : -1;
|
|
202
|
+
const region = stateful && (hasContent || readsState || models.length > 0) ? regionCount++ : -1;
|
|
126
203
|
const parentScope = scopeKey;
|
|
127
204
|
if (region >= 0)
|
|
128
205
|
scopeKey = `c${region}`;
|
|
@@ -145,7 +222,7 @@ export function lower(parsed, file, source) {
|
|
|
145
222
|
const rest = node.children.filter((c) => c.type !== 'fill');
|
|
146
223
|
const children = rest.every((c) => c.type === 'text' && !c.value.trim()) ? [] : lowerList(rest);
|
|
147
224
|
scopeKey = parentScope;
|
|
148
|
-
out.push({ kind: 'component', name: node.tag, props, children, fills, region, ...(attach ? { attach } : {}) });
|
|
225
|
+
out.push({ kind: 'component', name: node.tag, props, ...(models.length ? { models } : {}), children, fills, region, ...(attach ? { attach } : {}), ...(paths.length ? { paths } : {}), ...(cls ? { class: cls } : {}) });
|
|
149
226
|
return;
|
|
150
227
|
}
|
|
151
228
|
if (node.tag === 'slot') {
|
|
@@ -166,22 +243,113 @@ export function lower(parsed, file, source) {
|
|
|
166
243
|
out.push({ kind: 'slot', name, fallback });
|
|
167
244
|
return;
|
|
168
245
|
}
|
|
246
|
+
const outerSelect = selectModel;
|
|
247
|
+
const tag = node.tag;
|
|
248
|
+
const attrs = node.attrs.flatMap((a) => {
|
|
249
|
+
if (a.kind !== 'model')
|
|
250
|
+
return [a];
|
|
251
|
+
const bindable = (a.name === 'value' && ['input', 'select', 'textarea'].includes(tag)) || (a.name === 'checked' && tag === 'input');
|
|
252
|
+
if (!bindable)
|
|
253
|
+
fail(`[(${a.name})] on <${tag}>: an element binds two ways only with [(value)] (input, select, textarea) or [(checked)]`, a.start);
|
|
254
|
+
const type = a.name === 'checked' || tag === 'select' ? 'change' : 'input';
|
|
255
|
+
sameRoot(a.expr, a.start);
|
|
256
|
+
const typeAttr = node.attrs.find((x) => x.kind === 'static' && x.name === 'type');
|
|
257
|
+
const inputType = typeAttr?.kind === 'static' && typeof typeAttr.value === 'string' ? typeAttr.value.toLowerCase() : 'text';
|
|
258
|
+
const path = { kind: 'prop', name: 'data-cl-path', expr: modelPath(a.expr, modelNames), start: a.start };
|
|
259
|
+
if (a.name === 'value' && tag === 'input' && inputType === 'checkbox') {
|
|
260
|
+
const own = node.attrs.find((x) => x.name === 'value' && (x.kind === 'static' || x.kind === 'prop'));
|
|
261
|
+
if (!own)
|
|
262
|
+
return fail("a checkbox bound with [(value)] needs a value (value=\"…\" or [value]): it's what goes into the array; for a boolean, use [(checked)]", a.start);
|
|
263
|
+
const mine = own.kind === 'static' ? JSON.stringify(own.value === true ? '' : own.value) : `(${own.expr})`;
|
|
264
|
+
const next = `($event.target.checked ? [...((${a.expr}) ?? []), ${mine}] : ((${a.expr}) ?? []).filter((__cl_x) => __cl_x !== ${mine}))`;
|
|
265
|
+
return [
|
|
266
|
+
{ kind: 'prop', name: 'checked', expr: `((${a.expr}) ?? []).includes(${mine})`, start: a.start },
|
|
267
|
+
{ kind: 'event', name: 'change', expr: writeBack(a.expr, next, a.name, a.start), start: a.start },
|
|
268
|
+
path,
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
if (a.name === 'value' && tag === 'select') {
|
|
272
|
+
selectModel = a.expr;
|
|
273
|
+
return [
|
|
274
|
+
{ kind: 'prop', name: a.name, expr: a.expr, start: a.start },
|
|
275
|
+
{ kind: 'event', name: 'change', expr: writeBack(a.expr, '($event.target.multiple ? Array.from($event.target.selectedOptions, (__cl_o) => __cl_o.value) : $event.target.value)', a.name, a.start), start: a.start },
|
|
276
|
+
path,
|
|
277
|
+
];
|
|
278
|
+
}
|
|
279
|
+
if (a.name === 'value' && tag === 'input' && (inputType === 'number' || inputType === 'range')) {
|
|
280
|
+
return [
|
|
281
|
+
{ kind: 'prop', name: a.name, expr: a.expr, start: a.start },
|
|
282
|
+
{ kind: 'event', name: type, expr: writeBack(a.expr, '((__cl_v) => (isNaN(parseFloat(__cl_v)) ? __cl_v : parseFloat(__cl_v)))($event.target.value)', a.name, a.start), start: a.start },
|
|
283
|
+
path,
|
|
284
|
+
];
|
|
285
|
+
}
|
|
286
|
+
if (type === 'input' && !(tag === 'input' && inputType === 'radio')) {
|
|
287
|
+
const write = writeBack(a.expr, `$event.target.${a.name}`, a.name, a.start);
|
|
288
|
+
return [
|
|
289
|
+
{ kind: 'prop', name: a.name, expr: a.expr, start: a.start },
|
|
290
|
+
{ kind: 'event', name: 'input', expr: `if (!$event.isComposing) { ${write} }`, start: a.start },
|
|
291
|
+
{ kind: 'event', name: 'compositionend', expr: write, start: a.start },
|
|
292
|
+
path,
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
if (a.name === 'value' && tag === 'input' && inputType === 'radio') {
|
|
296
|
+
const own = node.attrs.find((x) => x.name === 'value' && (x.kind === 'static' || x.kind === 'prop'));
|
|
297
|
+
if (!own)
|
|
298
|
+
return fail("a radio bound with [(value)] needs a value (value=\"…\" or [value]): it's what the radio writes when picked", a.start);
|
|
299
|
+
const mine = own.kind === 'static' ? JSON.stringify(own.value === true ? '' : own.value) : `(${own.expr})`;
|
|
300
|
+
return [
|
|
301
|
+
{ kind: 'prop', name: 'checked', expr: `(${a.expr}) === ${mine}`, start: a.start },
|
|
302
|
+
{ kind: 'event', name: 'change', expr: writeBack(a.expr, mine, a.name, a.start), start: a.start },
|
|
303
|
+
{ kind: 'prop', name: 'data-cl-path', expr: modelPath(a.expr, modelNames), start: a.start },
|
|
304
|
+
];
|
|
305
|
+
}
|
|
306
|
+
const event = { kind: 'event', name: type, expr: writeBack(a.expr, `$event.target.${a.name}`, a.name, a.start), start: a.start };
|
|
307
|
+
return [{ kind: 'prop', name: a.name, expr: a.expr, start: a.start }, event, { kind: 'prop', name: 'data-cl-path', expr: modelPath(a.expr, modelNames), start: a.start }];
|
|
308
|
+
});
|
|
309
|
+
const late = (x) => x.kind !== 'event' && (x.name === 'value' || x.name === 'checked');
|
|
310
|
+
attrs.sort((x, y) => Number(late(x)) - Number(late(y)));
|
|
311
|
+
if (tag === 'option' && selectModel !== undefined) {
|
|
312
|
+
const own = node.attrs.find((x) => x.name === 'value' && (x.kind === 'static' || x.kind === 'prop'));
|
|
313
|
+
if (own) {
|
|
314
|
+
const mine = own.kind === 'static' ? JSON.stringify(own.value === true ? '' : own.value) : `(${own.expr})`;
|
|
315
|
+
attrs.push({ kind: 'prop', name: 'selected', expr: `((__cl_s, __cl_v) => (Array.isArray(__cl_s) ? __cl_s.map(String).includes(String(__cl_v)) : String(__cl_s) === String(__cl_v)))(${selectModel}, ${mine})`, start: own.start });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
169
318
|
emitStatic(`<${node.tag}`);
|
|
170
|
-
const elementEvents =
|
|
319
|
+
const elementEvents = attrs.filter((a) => a.kind === 'event');
|
|
171
320
|
const dynamic = [];
|
|
172
|
-
|
|
321
|
+
let engine = false;
|
|
322
|
+
for (const a of attrs) {
|
|
173
323
|
if (a.kind === 'static')
|
|
174
324
|
emitStatic(a.value === true ? ` ${a.name}` : ` ${a.name}="${a.value}"`);
|
|
175
325
|
else if (a.kind === 'event') {
|
|
176
326
|
if (!stateful)
|
|
177
327
|
fail('event bindings need a <script> with state; this component is static', a.start);
|
|
178
328
|
checkHandler(a.expr, file, source, a.start);
|
|
329
|
+
if (inputNames.size) {
|
|
330
|
+
const found = source.indexOf(a.expr, a.start);
|
|
331
|
+
const exprStart = found === -1 ? a.start : found;
|
|
332
|
+
for (const w of writesTo(a.expr, inputNames))
|
|
333
|
+
readOnly(w.name, exprStart + w.start);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else if (a.kind === 'prop' && a.name === 'engine') {
|
|
337
|
+
if (!stateful)
|
|
338
|
+
fail('[engine] needs a <script> (an island): the forms engine runs in the browser', a.start);
|
|
339
|
+
checkExpr(a.expr, file, source, a.start);
|
|
340
|
+
const index = formCount++;
|
|
341
|
+
emitStatic(` data-cl-form="${index}"`);
|
|
342
|
+
out.push({ kind: 'connect', expr: a.expr, index });
|
|
343
|
+
engine = true;
|
|
179
344
|
}
|
|
180
345
|
else {
|
|
181
346
|
if (/^on/i.test(a.name))
|
|
182
347
|
fail(`use (${a.name.slice(2).toLowerCase()}) for events, not an ${a.name} attribute`, a.start);
|
|
183
348
|
if (a.name.toLowerCase() === 'srcdoc')
|
|
184
349
|
fail('srcdoc cannot be bound; write it as a static attribute', a.start);
|
|
350
|
+
const loads = loadingUrl(node.tag, a.name, node.attrs);
|
|
351
|
+
if (loads)
|
|
352
|
+
fail(`${a.name} on <${node.tag}> ${loads}, so it can't come from data: write it as a static attribute`, a.start);
|
|
185
353
|
const exprs = a.kind === 'prop' ? [{ expr: a.expr, start: a.start }] : a.parts.filter((p) => typeof p !== 'string');
|
|
186
354
|
for (const e of exprs)
|
|
187
355
|
checkExpr(e.expr, file, source, e.start);
|
|
@@ -206,7 +374,19 @@ export function lower(parsed, file, source) {
|
|
|
206
374
|
events.push({ index, type: a.name, handler: a.kind === 'event' ? a.expr : '', scope: scopeKey });
|
|
207
375
|
}
|
|
208
376
|
emitStatic('>');
|
|
377
|
+
const outer = engineScope;
|
|
378
|
+
if (engine)
|
|
379
|
+
engineScope = { root: null };
|
|
380
|
+
if (tag !== 'select' || !node.attrs.some((x) => x.kind === 'model'))
|
|
381
|
+
selectModel = tag === 'optgroup' || tag === 'option' ? selectModel : undefined;
|
|
382
|
+
const keeps = KEEP_WHITESPACE.has(node.tag);
|
|
383
|
+
if (keeps)
|
|
384
|
+
keep++;
|
|
209
385
|
node.children.forEach((child) => visit(child, RAW_TEXT.has(node.tag) ? node.tag : rawText));
|
|
386
|
+
if (keeps)
|
|
387
|
+
keep--;
|
|
388
|
+
engineScope = outer;
|
|
389
|
+
selectModel = outerSelect;
|
|
210
390
|
if (!VOID.has(node.tag))
|
|
211
391
|
emitStatic(`</${node.tag}>`);
|
|
212
392
|
}
|
|
@@ -223,5 +403,75 @@ export function lower(parsed, file, source) {
|
|
|
223
403
|
stateful,
|
|
224
404
|
nodes,
|
|
225
405
|
events,
|
|
406
|
+
warnings,
|
|
226
407
|
};
|
|
227
408
|
}
|
|
409
|
+
function pathSegments(path) {
|
|
410
|
+
const root = /^[A-Za-z_$][\w$]*/.exec(path)?.[0];
|
|
411
|
+
if (!root)
|
|
412
|
+
return null;
|
|
413
|
+
const segments = [];
|
|
414
|
+
for (let rest = path.slice(root.length); rest;) {
|
|
415
|
+
const key = /^\.([A-Za-z_$][\w$]*)/.exec(rest);
|
|
416
|
+
if (key) {
|
|
417
|
+
segments.push({ key: key[1] });
|
|
418
|
+
rest = rest.slice(key[0].length);
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
if (rest[0] !== '[')
|
|
422
|
+
return null;
|
|
423
|
+
let depth = 0;
|
|
424
|
+
let close = 0;
|
|
425
|
+
for (; close < rest.length; close++) {
|
|
426
|
+
if (rest[close] === '[')
|
|
427
|
+
depth++;
|
|
428
|
+
else if (rest[close] === ']' && --depth === 0)
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
const index = rest.slice(1, close).trim();
|
|
432
|
+
if (close >= rest.length || !index)
|
|
433
|
+
return null;
|
|
434
|
+
segments.push({ index });
|
|
435
|
+
rest = rest.slice(close + 1);
|
|
436
|
+
}
|
|
437
|
+
return { root, segments };
|
|
438
|
+
}
|
|
439
|
+
function modelPath(path, models = new Set()) {
|
|
440
|
+
const parsed = pathSegments(path);
|
|
441
|
+
const parts = (parsed?.segments ?? []).map((seg) => {
|
|
442
|
+
if ('key' in seg)
|
|
443
|
+
return JSON.stringify(seg.key);
|
|
444
|
+
let literal;
|
|
445
|
+
try {
|
|
446
|
+
const node = parseExpressionAt(seg.index, 0, { ecmaVersion: 'latest' });
|
|
447
|
+
if (node.type === 'Literal' && node.end === seg.index.length && (typeof node.value === 'string' || typeof node.value === 'number'))
|
|
448
|
+
literal = node.value;
|
|
449
|
+
}
|
|
450
|
+
catch { }
|
|
451
|
+
return literal !== undefined ? JSON.stringify(String(literal)) : `(${seg.index})`;
|
|
452
|
+
});
|
|
453
|
+
if (parsed && models.has(parsed.root))
|
|
454
|
+
parts.unshift(JSON.stringify(`@${parsed.root}`));
|
|
455
|
+
if (!parts.length)
|
|
456
|
+
return '""';
|
|
457
|
+
return parts.map((p, i) => (i ? `"." + ${p}` : p)).join(' + ');
|
|
458
|
+
}
|
|
459
|
+
const LOADS = {
|
|
460
|
+
iframe: { src: 'loads a page into this one', sandbox: 'sets what the iframe may do', allow: 'sets what the iframe may do', referrerpolicy: 'sets what the iframe sends' },
|
|
461
|
+
frame: { src: 'loads a page into this one' },
|
|
462
|
+
embed: { src: 'loads a plugin or page into this one' },
|
|
463
|
+
object: { data: 'loads a plugin or page into this one', codebase: 'sets where a plugin loads from' },
|
|
464
|
+
base: { href: 'sets where every relative URL on the page points' },
|
|
465
|
+
};
|
|
466
|
+
const SAFE_RELS = new Set(['canonical', 'alternate', 'prev', 'next', 'author', 'license', 'help', 'search', 'me', 'bookmark', 'external', 'nofollow', 'noopener', 'noreferrer', 'tag', 'privacy-policy', 'terms-of-service']);
|
|
467
|
+
function loadingUrl(tag, attr, attrs) {
|
|
468
|
+
const t = tag.toLowerCase();
|
|
469
|
+
const name = attr.toLowerCase();
|
|
470
|
+
if (t === 'link' && name === 'href') {
|
|
471
|
+
const relAttrs = attrs.filter((x) => x.name.toLowerCase() === 'rel');
|
|
472
|
+
const rel = relAttrs.length === 1 ? relAttrs[0] : undefined;
|
|
473
|
+
const rels = rel?.kind === 'static' && typeof rel.value === 'string' ? rel.value.toLowerCase().split(/\s+/).filter(Boolean) : [];
|
|
474
|
+
return rels.length && rels.every((r) => SAFE_RELS.has(r)) ? undefined : 'can load a stylesheet, icon or script (only rel="canonical", "alternate" and other rels that load nothing take an href from data)';
|
|
475
|
+
}
|
|
476
|
+
return LOADS[t]?.[name];
|
|
477
|
+
}
|
package/dist/compiler/parse.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An attribute in the syntax tree: static, an event `(x)`, a prop `[x]`, a two-way binding `[(x)]`, or a value with
|
|
3
|
+
* `{{ }}` holes. `start`: its offset in the source.
|
|
4
|
+
* @see https://clarusjs.com/reference/compiler#parse
|
|
5
|
+
*/
|
|
1
6
|
export type Attr = {
|
|
2
7
|
kind: 'static';
|
|
3
8
|
name: string;
|
|
@@ -13,6 +18,11 @@ export type Attr = {
|
|
|
13
18
|
name: string;
|
|
14
19
|
expr: string;
|
|
15
20
|
start: number;
|
|
21
|
+
} | {
|
|
22
|
+
kind: 'model';
|
|
23
|
+
name: string;
|
|
24
|
+
expr: string;
|
|
25
|
+
start: number;
|
|
16
26
|
} | {
|
|
17
27
|
kind: 'interp';
|
|
18
28
|
name: string;
|
|
@@ -22,6 +32,10 @@ export type Attr = {
|
|
|
22
32
|
})[];
|
|
23
33
|
start: number;
|
|
24
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* A node of a template's syntax tree: an element, text, a `{{ }}` hole, an `@if`, an `@for`, a slot fill…
|
|
37
|
+
* @see https://clarusjs.com/reference/compiler#parse
|
|
38
|
+
*/
|
|
25
39
|
export type TemplateNode = {
|
|
26
40
|
type: 'element';
|
|
27
41
|
tag: string;
|
|
@@ -67,6 +81,10 @@ export type TemplateNode = {
|
|
|
67
81
|
empty: TemplateNode[] | null;
|
|
68
82
|
start: number;
|
|
69
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* An `@input` (or `@model`) declaration: its name, type text, default, and offsets.
|
|
86
|
+
* @see https://clarusjs.com/reference/compiler#parse
|
|
87
|
+
*/
|
|
70
88
|
export type InputDecl = {
|
|
71
89
|
name: string;
|
|
72
90
|
type: string;
|
|
@@ -75,6 +93,7 @@ export type InputDecl = {
|
|
|
75
93
|
nameStart: number;
|
|
76
94
|
typeStart: number;
|
|
77
95
|
defaultStart?: number;
|
|
96
|
+
model?: true;
|
|
78
97
|
};
|
|
79
98
|
export type TypeImport = {
|
|
80
99
|
clause: string;
|
|
@@ -88,15 +107,33 @@ export type DataImport = {
|
|
|
88
107
|
start: number;
|
|
89
108
|
clauseStart: number;
|
|
90
109
|
};
|
|
110
|
+
/**
|
|
111
|
+
* A component `@import`: the name it's used by in the template, and where it comes from.
|
|
112
|
+
* @see https://clarusjs.com/reference/compiler#parse
|
|
113
|
+
*/
|
|
91
114
|
export type ImportDecl = {
|
|
92
115
|
name: string;
|
|
93
116
|
from: string;
|
|
94
117
|
start: number;
|
|
95
118
|
};
|
|
119
|
+
export type TypeDecl = {
|
|
120
|
+
kind: 'type' | 'interface';
|
|
121
|
+
name: string;
|
|
122
|
+
params?: string;
|
|
123
|
+
type: string;
|
|
124
|
+
start: number;
|
|
125
|
+
nameStart: number;
|
|
126
|
+
typeStart: number;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* A `.tscl` file's syntax tree, as `parse()` gives it: inputs, imports, the script, styles and template nodes.
|
|
130
|
+
* @see https://clarusjs.com/reference/compiler#parse
|
|
131
|
+
*/
|
|
96
132
|
export type ParsedFile = {
|
|
97
133
|
inputs: InputDecl[];
|
|
98
134
|
imports: ImportDecl[];
|
|
99
135
|
typeImports: TypeImport[];
|
|
136
|
+
types: TypeDecl[];
|
|
100
137
|
dataImports: DataImport[];
|
|
101
138
|
script?: {
|
|
102
139
|
code: string;
|
|
@@ -104,8 +141,9 @@ export type ParsedFile = {
|
|
|
104
141
|
};
|
|
105
142
|
styles: {
|
|
106
143
|
css: string;
|
|
107
|
-
|
|
144
|
+
mode: 'scoped' | 'global' | 'plain';
|
|
108
145
|
start: number;
|
|
146
|
+
tag: number;
|
|
109
147
|
}[];
|
|
110
148
|
cssImports: {
|
|
111
149
|
from: string;
|
|
@@ -113,6 +151,19 @@ export type ParsedFile = {
|
|
|
113
151
|
}[];
|
|
114
152
|
nodes: TemplateNode[];
|
|
115
153
|
};
|
|
154
|
+
/** The names a @for row has besides its item: $index, $count, $first, $last, $even, $odd. */
|
|
155
|
+
export declare const FOR_IMPLICIT: string[];
|
|
156
|
+
/**
|
|
157
|
+
* Parse a `.tscl` file into its syntax tree, without compiling it: for tools (an editor, a linter, a docs
|
|
158
|
+
* generator). Throws a located `ClarusError` for a syntax error.
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* import { parse } from 'clarusjs/compiler'
|
|
162
|
+
* const { inputs, imports } = parse(source, 'src/components/card.tscl')
|
|
163
|
+
* console.log(inputs.map((i) => `${i.name}: ${i.type}`))
|
|
164
|
+
* ```
|
|
165
|
+
* @see https://clarusjs.com/reference/compiler#parse
|
|
166
|
+
*/
|
|
116
167
|
export declare function parse(source: string, file: string): ParsedFile;
|
|
117
168
|
/** Local names an import clause binds: `{ a, b as c, type T }` → a, c; `x` → x. */
|
|
118
169
|
export declare function importNames(clause: string): string[];
|