clarusjs 0.11.0 → 0.12.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/compiler/ir.js +11 -5
- package/dist/compiler/script.d.ts +10 -0
- package/dist/compiler/script.js +61 -0
- package/dist/forms/index.js +4 -1
- package/dist/vite/plugin.js +8 -1
- package/package.json +1 -1
package/dist/compiler/ir.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parseExpressionAt } from 'acorn';
|
|
2
2
|
import { ClarusError } from './errors.js';
|
|
3
3
|
import { FOR_IMPLICIT, importNames } from './parse.js';
|
|
4
|
-
import { analyzeScript, writesTo, checkExpr, checkHandler, referencesNames, scriptNames, splitImports } from './script.js';
|
|
4
|
+
import { analyzeScript, mutationsOf, writesTo, checkExpr, checkHandler, referencesNames, scriptNames, splitImports } from './script.js';
|
|
5
5
|
const ATTACH_MODES = ['load', 'idle', 'visible', 'interaction'];
|
|
6
6
|
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr']);
|
|
7
7
|
export function lower(parsed, file, source, options = {}) {
|
|
@@ -49,6 +49,10 @@ export function lower(parsed, file, source, options = {}) {
|
|
|
49
49
|
if (parsed.script && inputNames.size)
|
|
50
50
|
for (const w of writesTo(parsed.script.code, inputNames))
|
|
51
51
|
readOnly(w.name, parsed.script.start + w.start);
|
|
52
|
+
const stateNames = new Set(signals.keys());
|
|
53
|
+
if (parsed.script)
|
|
54
|
+
for (const m of mutationsOf(parsed.script.code, stateNames))
|
|
55
|
+
warnings.push(new ClarusError(m.message, file, source, parsed.script.start + m.start));
|
|
52
56
|
for (const d of parsed.dataImports) {
|
|
53
57
|
if (stateful && d.from.startsWith('node:'))
|
|
54
58
|
fail(`\`${d.from}\` can't be imported in an island: its imports are bundled for the browser`, d.start);
|
|
@@ -326,12 +330,14 @@ export function lower(parsed, file, source, options = {}) {
|
|
|
326
330
|
if (!stateful)
|
|
327
331
|
fail('event bindings need a <script> with state; this component is static', a.start);
|
|
328
332
|
checkHandler(a.expr, file, source, a.start);
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
333
|
+
const found = source.indexOf(a.expr, a.start);
|
|
334
|
+
const exprStart = found === -1 ? a.start : found;
|
|
335
|
+
if (inputNames.size)
|
|
332
336
|
for (const w of writesTo(a.expr, inputNames))
|
|
333
337
|
readOnly(w.name, exprStart + w.start);
|
|
334
|
-
|
|
338
|
+
if (found !== -1)
|
|
339
|
+
for (const m of mutationsOf(a.expr, stateNames))
|
|
340
|
+
warnings.push(new ClarusError(m.message, file, source, exprStart + m.start));
|
|
335
341
|
}
|
|
336
342
|
else if (a.kind === 'prop' && a.name === 'engine') {
|
|
337
343
|
if (!stateful)
|
|
@@ -46,6 +46,16 @@ export declare function rewriteClient(code: string, signals: Signals, options?:
|
|
|
46
46
|
export declare function rewriteClientExpr(expr: string, signals: Signals): string;
|
|
47
47
|
export declare function rewriteServer(code: string): string;
|
|
48
48
|
export declare function referencesSignal(expr: string, signals: Signals): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* State changed in place (#350): a mutating method called on one of `names` (`items.push(x)`), or one of its properties
|
|
51
|
+
* written, incremented or deleted (`form.a = 1`). Nothing notifies, so the page doesn't update. Skips places where a
|
|
52
|
+
* local declaration shadows the name. Each with where it starts in `code` and what to write instead.
|
|
53
|
+
*/
|
|
54
|
+
export declare function mutationsOf(code: string, names: Set<string>): {
|
|
55
|
+
name: string;
|
|
56
|
+
start: number;
|
|
57
|
+
message: string;
|
|
58
|
+
}[];
|
|
49
59
|
/** Does `expr` read any of `names`? */
|
|
50
60
|
/**
|
|
51
61
|
* Assignments and ++/-- to any of `names` in a script or event handler, skipping places where a local declaration
|
package/dist/compiler/script.js
CHANGED
|
@@ -367,6 +367,67 @@ export function rewriteServer(code) {
|
|
|
367
367
|
export function referencesSignal(expr, signals) {
|
|
368
368
|
return referencesNames(expr, new Set(signals.keys()));
|
|
369
369
|
}
|
|
370
|
+
const MUTATORS = {
|
|
371
|
+
push: (n) => `${n} = [...${n}, …]`,
|
|
372
|
+
unshift: (n) => `${n} = […, ...${n}]`,
|
|
373
|
+
pop: (n) => `${n} = ${n}.slice(0, -1)`,
|
|
374
|
+
shift: (n) => `${n} = ${n}.slice(1)`,
|
|
375
|
+
splice: (n) => `${n} = ${n}.toSpliced(…)`,
|
|
376
|
+
sort: (n) => `${n} = ${n}.toSorted(…)`,
|
|
377
|
+
reverse: (n) => `${n} = ${n}.toReversed()`,
|
|
378
|
+
fill: (n) => `${n} = ${n}.map(…)`,
|
|
379
|
+
copyWithin: (n) => `${n} = [...${n}]`,
|
|
380
|
+
set: (n) => `${n} = new Map(${n}).set(…)`,
|
|
381
|
+
add: (n) => `${n} = new Set(${n}).add(…)`,
|
|
382
|
+
delete: (n) => `a copy without it: ${n} = new Map(${n}), then delete from that`,
|
|
383
|
+
clear: (n) => `${n} = new Map() or new Set()`,
|
|
384
|
+
};
|
|
385
|
+
function rootOf(node) {
|
|
386
|
+
let n = node;
|
|
387
|
+
while (n.type === 'MemberExpression')
|
|
388
|
+
n = n.object;
|
|
389
|
+
return n.type === 'Identifier' ? n : undefined;
|
|
390
|
+
}
|
|
391
|
+
function chain(node) {
|
|
392
|
+
if (node.type === 'Identifier')
|
|
393
|
+
return node.name;
|
|
394
|
+
if (node.type !== 'MemberExpression')
|
|
395
|
+
return '…';
|
|
396
|
+
return node.computed ? `${chain(node.object)}[…]` : `${chain(node.object)}.${node.property.name}`;
|
|
397
|
+
}
|
|
398
|
+
export function mutationsOf(code, names) {
|
|
399
|
+
const found = [];
|
|
400
|
+
const say = (name, start, what, instead) => found.push({ name, start, message: `${what} changes ${name} in place, so the page won't update: assign a new value instead (${instead})` });
|
|
401
|
+
const visit = (active, entered) => (node, parent, key) => {
|
|
402
|
+
if (!entered.has(node)) {
|
|
403
|
+
const shadowed = [...scopeNames(node)].filter((n) => active.has(n));
|
|
404
|
+
if (shadowed.length) {
|
|
405
|
+
entered.add(node);
|
|
406
|
+
print(node, code, visit(new Set([...active].filter((n) => !shadowed.includes(n))), entered), parent, key);
|
|
407
|
+
return '';
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && !node.callee.computed) {
|
|
411
|
+
const method = node.callee.property.name;
|
|
412
|
+
const root = rootOf(node.callee.object);
|
|
413
|
+
if (root && active.has(root.name) && Object.hasOwn(MUTATORS, method)) {
|
|
414
|
+
say(root.name, node.start, `${chain(node.callee.object)}.${method}()`, MUTATORS[method](root.name));
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const target = node.type === 'AssignmentExpression' ? node.left : node.type === 'UpdateExpression' ? node.argument : node.type === 'UnaryExpression' && node.operator === 'delete' ? node.argument : null;
|
|
418
|
+
if (target?.type === 'MemberExpression') {
|
|
419
|
+
const root = rootOf(target);
|
|
420
|
+
if (root && active.has(root.name)) {
|
|
421
|
+
const what = node.type === 'AssignmentExpression' ? `${chain(target)} ${node.operator} …` : node.type === 'UpdateExpression' ? `${chain(target)}${node.operator}` : `delete ${chain(target)}`;
|
|
422
|
+
const direct = target.object.type === 'Identifier' && !target.computed;
|
|
423
|
+
say(root.name, node.start, what, direct ? `${root.name} = { ...${root.name}, ${target.property.name}: … }` : `${root.name} = { ...${root.name}, … }`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return undefined;
|
|
427
|
+
};
|
|
428
|
+
print(parseScript(code), code, visit(names, new WeakSet()));
|
|
429
|
+
return found;
|
|
430
|
+
}
|
|
370
431
|
export function writesTo(code, names) {
|
|
371
432
|
const found = [];
|
|
372
433
|
const visit = (active, entered) => (node, parent, key) => {
|
package/dist/forms/index.js
CHANGED
|
@@ -154,7 +154,10 @@ export function form(values, options = {}) {
|
|
|
154
154
|
step.toggleAttribute('data-cl-current', step.id === id);
|
|
155
155
|
}
|
|
156
156
|
if (push && location.hash !== `#${id}`)
|
|
157
|
-
|
|
157
|
+
try {
|
|
158
|
+
history.pushState(null, '', `#${id}`);
|
|
159
|
+
}
|
|
160
|
+
catch { }
|
|
158
161
|
refresh();
|
|
159
162
|
}
|
|
160
163
|
const hashStep = () => {
|
package/dist/vite/plugin.js
CHANGED
|
@@ -158,6 +158,11 @@ function clientPages(appRoot, modes, files) {
|
|
|
158
158
|
}
|
|
159
159
|
return pages;
|
|
160
160
|
}
|
|
161
|
+
function pluginsOf(list) {
|
|
162
|
+
if (Array.isArray(list))
|
|
163
|
+
return list.flatMap(pluginsOf);
|
|
164
|
+
return list && typeof list === 'object' && !(list instanceof Promise) ? [list] : [];
|
|
165
|
+
}
|
|
161
166
|
export default function clarusjs(options = {}) {
|
|
162
167
|
const modes = options.routes ?? {};
|
|
163
168
|
checkModes(modes);
|
|
@@ -172,9 +177,10 @@ export default function clarusjs(options = {}) {
|
|
|
172
177
|
}
|
|
173
178
|
const extensions = ['.tscl', ...sources.map((src) => src.extension)];
|
|
174
179
|
let aliases = [];
|
|
180
|
+
let extra = () => [];
|
|
175
181
|
const files = {
|
|
176
182
|
find: (dir) => findClarusFiles(dir, extensions).filter((f) => f.endsWith('.tscl') || f.includes(`${sep}src${sep}pages${sep}`)),
|
|
177
|
-
components: (appRoot) => reachableComponents(appRoot, files.find(resolve(appRoot, 'src')), (file) => files.read(file, appRoot), extensions, aliases),
|
|
183
|
+
components: (appRoot) => reachableComponents(appRoot, [...files.find(resolve(appRoot, 'src')), ...extra(appRoot).filter((f) => existsSync(f))], (file) => files.read(file, appRoot), extensions, aliases),
|
|
178
184
|
read: (file, appRoot) => {
|
|
179
185
|
const code = readFileSync(file, 'utf8');
|
|
180
186
|
const src = sources.find((x) => file.endsWith(x.extension));
|
|
@@ -219,6 +225,7 @@ export default function clarusjs(options = {}) {
|
|
|
219
225
|
name: 'clarusjs',
|
|
220
226
|
api: { sources, lint: options.lint, lintRules: options.lintRules },
|
|
221
227
|
config(userConfig, env) {
|
|
228
|
+
extra = (appRoot) => pluginsOf(userConfig.plugins).flatMap((p) => p.api?.clarusComponents?.(appRoot) ?? []);
|
|
222
229
|
const projectRoot = resolve(userConfig.root ?? process.cwd());
|
|
223
230
|
aliases = projectAliases(projectRoot, userConfig.resolve?.alias);
|
|
224
231
|
const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clarusjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Clear, compiled views: templates compiled to server HTML, with JavaScript only for interactive islands. SSR, SSG and CSR from one component model, built on Vite.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|