@ai-ecoverse/slicc-shared-web 1.16.0 → 1.17.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 +1 -0
- package/package.json +1 -1
- package/unit/exports.mjs +167 -0
- package/unit/graph.mjs +100 -92
package/README.md
CHANGED
|
@@ -34,6 +34,7 @@ The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover
|
|
|
34
34
|
|
|
35
35
|
- **What counts as changed:** the staged files (`git diff --cached --name-only`). Since `test/unit/` is gitignored, files in it count as changed when they were modified after HEAD last moved (the last commit, checkout or pull).
|
|
36
36
|
- **Which tests run:** every `*.test.*` file in `test/unit/` that is changed, or whose static imports reach a changed file. The import graph is parsed with [acorn](https://github.com/acornjs/acorn), TypeScript after Node's type stripping. It follows `import`, `export … from`, `import()`, `require()` and `new URL(…, import.meta.url)` with literal paths, relative or through the `paths` of `tsconfig.json` (and the files it extends). A path may leave out its extension or name `index`, and `.js` also finds `.ts`. Type-only imports don't count, and nothing is followed into `node_modules/`.
|
|
37
|
+
- **Barrels are followed by name:** a test that imports `{ a, b }` from a module follows only what provides `a` and `b`: `export { a } from`, `export { default as a } from`, an imported binding exported again, and `export * from`, by the target's own exports. `const { a, b } = await import('…')` with a literal path is followed by name too. A name the module declares itself follows all of that module's imports. A namespace import (`import * as`), any other `import()` (bound to a namespace, with `.then`, or destructured with a computed key or a rest element), a `require()`, and an `export *` that leads outside the repo or into CommonJS (any `.cjs` or `.cts` file) all count as the whole module.
|
|
37
38
|
- **No code staged** (only docs, YAML, JSON or lockfiles): no unit tests and no diff coverage; the hook passes.
|
|
38
39
|
- **A staged file no unit test reaches:** no tests run, its lines show as uncovered, and diff coverage fails as before.
|
|
39
40
|
- **A file that won't parse:** one warning line, then every unit test.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ecoverse/slicc-shared-web",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "Shared Renovate, Biome, TypeScript, lefthook and GitHub Actions configuration, plus the CDP integration-test harness, for SLICC's web repos",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
package/unit/exports.mjs
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
export const ALL = '*';
|
|
2
|
+
export const BODY = '\0body';
|
|
3
|
+
|
|
4
|
+
const literal = (node) => {
|
|
5
|
+
if (node?.type === 'Literal' && typeof node.value === 'string') return node.value;
|
|
6
|
+
if (node?.type === 'TemplateLiteral' && node.expressions.length === 0) {
|
|
7
|
+
return node.quasis[0].value.cooked;
|
|
8
|
+
}
|
|
9
|
+
return undefined;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const named = (node) => (node.type === 'Literal' ? String(node.value) : node.name);
|
|
13
|
+
|
|
14
|
+
const metaUrl = (node) =>
|
|
15
|
+
node?.type === 'MemberExpression' &&
|
|
16
|
+
node.object.type === 'MetaProperty' &&
|
|
17
|
+
node.property.name === 'url';
|
|
18
|
+
|
|
19
|
+
function dynamic(node) {
|
|
20
|
+
switch (node.type) {
|
|
21
|
+
case 'ImportExpression':
|
|
22
|
+
return literal(node.source);
|
|
23
|
+
case 'CallExpression':
|
|
24
|
+
return node.callee.type === 'Identifier' && node.callee.name === 'require'
|
|
25
|
+
? literal(node.arguments[0])
|
|
26
|
+
: undefined;
|
|
27
|
+
case 'NewExpression':
|
|
28
|
+
return node.callee.type === 'Identifier' &&
|
|
29
|
+
node.callee.name === 'URL' &&
|
|
30
|
+
metaUrl(node.arguments[1])
|
|
31
|
+
? literal(node.arguments[0])
|
|
32
|
+
: undefined;
|
|
33
|
+
default:
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function picked(node) {
|
|
39
|
+
if (node.type !== 'VariableDeclarator' || node.id.type !== 'ObjectPattern') return undefined;
|
|
40
|
+
const awaited = node.init?.type === 'AwaitExpression' ? node.init.argument : undefined;
|
|
41
|
+
if (awaited?.type !== 'ImportExpression') return undefined;
|
|
42
|
+
const spec = literal(awaited.source);
|
|
43
|
+
const props = node.id.properties;
|
|
44
|
+
if (spec === undefined || props.some((prop) => prop.type !== 'Property' || prop.computed)) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return { spec, names: props.length > 0 ? props.map((prop) => named(prop.key)) : [BODY] };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function dynamics(ast) {
|
|
51
|
+
const found = [];
|
|
52
|
+
const stack = [ast];
|
|
53
|
+
while (stack.length > 0) {
|
|
54
|
+
const node = stack.pop();
|
|
55
|
+
const pick = picked(node);
|
|
56
|
+
if (pick) {
|
|
57
|
+
found.push(pick);
|
|
58
|
+
stack.push(node.id);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const value = dynamic(node);
|
|
62
|
+
if (value !== undefined) found.push({ spec: value, names: [ALL] });
|
|
63
|
+
for (const key of Object.keys(node)) {
|
|
64
|
+
const child = node[key];
|
|
65
|
+
if (Array.isArray(child)) {
|
|
66
|
+
for (const item of child) if (item && typeof item.type === 'string') stack.push(item);
|
|
67
|
+
} else if (child && typeof child.type === 'string') {
|
|
68
|
+
stack.push(child);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return found;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function bound(pattern, out = []) {
|
|
76
|
+
if (!pattern) return out;
|
|
77
|
+
switch (pattern.type) {
|
|
78
|
+
case 'Identifier':
|
|
79
|
+
out.push(pattern.name);
|
|
80
|
+
break;
|
|
81
|
+
case 'ObjectPattern':
|
|
82
|
+
for (const property of pattern.properties) bound(property.value ?? property.argument, out);
|
|
83
|
+
break;
|
|
84
|
+
case 'ArrayPattern':
|
|
85
|
+
for (const element of pattern.elements) bound(element, out);
|
|
86
|
+
break;
|
|
87
|
+
case 'RestElement':
|
|
88
|
+
bound(pattern.argument, out);
|
|
89
|
+
break;
|
|
90
|
+
case 'AssignmentPattern':
|
|
91
|
+
bound(pattern.left, out);
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function declared(declaration) {
|
|
100
|
+
if (declaration.type === 'VariableDeclaration') {
|
|
101
|
+
return declaration.declarations.flatMap((item) => bound(item.id));
|
|
102
|
+
}
|
|
103
|
+
return declaration.id ? [declaration.id.name] : [];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function imported(node, info) {
|
|
107
|
+
const names = new Set();
|
|
108
|
+
for (const item of node.specifiers) {
|
|
109
|
+
let name = ALL;
|
|
110
|
+
if (item.type === 'ImportDefaultSpecifier') name = 'default';
|
|
111
|
+
else if (item.type === 'ImportSpecifier') name = named(item.imported);
|
|
112
|
+
names.add(name);
|
|
113
|
+
info.bindings.set(item.local.name, { spec: node.source.value, name });
|
|
114
|
+
}
|
|
115
|
+
info.imports.push({ spec: node.source.value, names: names.size > 0 ? [...names] : [BODY] });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function exported(node, info) {
|
|
119
|
+
if (node.declaration) {
|
|
120
|
+
for (const name of declared(node.declaration)) info.locals.add(name);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
for (const item of node.specifiers) {
|
|
124
|
+
const local = named(item.local);
|
|
125
|
+
const name = named(item.exported);
|
|
126
|
+
if (node.source) info.reexports.set(name, { spec: node.source.value, name: local });
|
|
127
|
+
else info.aliases.set(name, local);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function exportedDefault(node, info) {
|
|
132
|
+
const { declaration } = node;
|
|
133
|
+
if (declaration.type === 'Identifier') info.aliases.set('default', declaration.name);
|
|
134
|
+
else info.locals.add('default');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const handlers = {
|
|
138
|
+
ImportDeclaration: imported,
|
|
139
|
+
ExportNamedDeclaration: exported,
|
|
140
|
+
ExportDefaultDeclaration: exportedDefault,
|
|
141
|
+
ExportAllDeclaration: (node, info) => {
|
|
142
|
+
if (node.exported) {
|
|
143
|
+
info.reexports.set(named(node.exported), { spec: node.source.value, name: ALL });
|
|
144
|
+
} else {
|
|
145
|
+
info.stars.push(node.source.value);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export function describe(ast, commonjs = false) {
|
|
151
|
+
const info = {
|
|
152
|
+
whole: commonjs || ast.sourceType !== 'module',
|
|
153
|
+
imports: [],
|
|
154
|
+
bindings: new Map(),
|
|
155
|
+
aliases: new Map(),
|
|
156
|
+
locals: new Set(),
|
|
157
|
+
reexports: new Map(),
|
|
158
|
+
stars: [],
|
|
159
|
+
dynamic: dynamics(ast),
|
|
160
|
+
};
|
|
161
|
+
for (const node of ast.body) handlers[node.type]?.(node, info);
|
|
162
|
+
for (const [name, local] of info.aliases) {
|
|
163
|
+
if (info.bindings.has(local)) info.reexports.set(name, info.bindings.get(local));
|
|
164
|
+
else info.locals.add(name);
|
|
165
|
+
}
|
|
166
|
+
return info;
|
|
167
|
+
}
|
package/unit/graph.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
|
2
2
|
import { stripTypeScriptTypes } from 'node:module';
|
|
3
3
|
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
4
|
import { parse } from 'acorn';
|
|
5
|
+
import { ALL, BODY, describe } from './exports.mjs';
|
|
5
6
|
|
|
6
7
|
export const code = /\.(m|c)?(j|t)sx?$/;
|
|
7
8
|
const tests = /\.test\.(m|c)?(j|t)sx?$/;
|
|
@@ -67,60 +68,6 @@ function program(file) {
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
const literal = (node) => {
|
|
71
|
-
if (node?.type === 'Literal' && typeof node.value === 'string') return node.value;
|
|
72
|
-
if (node?.type === 'TemplateLiteral' && node.expressions.length === 0) {
|
|
73
|
-
return node.quasis[0].value.cooked;
|
|
74
|
-
}
|
|
75
|
-
return undefined;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const metaUrl = (node) =>
|
|
79
|
-
node?.type === 'MemberExpression' &&
|
|
80
|
-
node.object.type === 'MetaProperty' &&
|
|
81
|
-
node.property.name === 'url';
|
|
82
|
-
|
|
83
|
-
function specifier(node) {
|
|
84
|
-
switch (node.type) {
|
|
85
|
-
case 'ImportDeclaration':
|
|
86
|
-
case 'ExportAllDeclaration':
|
|
87
|
-
case 'ExportNamedDeclaration':
|
|
88
|
-
case 'ImportExpression':
|
|
89
|
-
return literal(node.source);
|
|
90
|
-
case 'CallExpression':
|
|
91
|
-
return node.callee.type === 'Identifier' && node.callee.name === 'require'
|
|
92
|
-
? literal(node.arguments[0])
|
|
93
|
-
: undefined;
|
|
94
|
-
case 'NewExpression':
|
|
95
|
-
return node.callee.type === 'Identifier' &&
|
|
96
|
-
node.callee.name === 'URL' &&
|
|
97
|
-
metaUrl(node.arguments[1])
|
|
98
|
-
? literal(node.arguments[0])
|
|
99
|
-
: undefined;
|
|
100
|
-
default:
|
|
101
|
-
return undefined;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function specifiers(ast) {
|
|
106
|
-
const found = [];
|
|
107
|
-
const stack = [ast];
|
|
108
|
-
while (stack.length > 0) {
|
|
109
|
-
const node = stack.pop();
|
|
110
|
-
const value = specifier(node);
|
|
111
|
-
if (value !== undefined) found.push(value);
|
|
112
|
-
for (const key of Object.keys(node)) {
|
|
113
|
-
const child = node[key];
|
|
114
|
-
if (Array.isArray(child)) {
|
|
115
|
-
for (const item of child) if (item && typeof item.type === 'string') stack.push(item);
|
|
116
|
-
} else if (child && typeof child.type === 'string') {
|
|
117
|
-
stack.push(child);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return found;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
71
|
function candidates(target) {
|
|
125
72
|
const ext = extname(target);
|
|
126
73
|
const base = target.slice(0, target.length - ext.length);
|
|
@@ -243,52 +190,112 @@ function resolver(root) {
|
|
|
243
190
|
};
|
|
244
191
|
}
|
|
245
192
|
|
|
246
|
-
function
|
|
193
|
+
function modules(root) {
|
|
247
194
|
const find = resolver(root);
|
|
248
|
-
const
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
throw new ParseError(`${relative(root, file)}: ${error.message}`);
|
|
195
|
+
const cache = new Map();
|
|
196
|
+
const load = (file) => {
|
|
197
|
+
if (!cache.has(file)) {
|
|
198
|
+
let info;
|
|
199
|
+
if (code.test(file)) {
|
|
200
|
+
try {
|
|
201
|
+
info = describe(program(file), /\.c(j|t)s$/.test(file));
|
|
202
|
+
} catch (error) {
|
|
203
|
+
throw new ParseError(`${relative(root, file)}: ${error.message}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
cache.set(file, info);
|
|
261
207
|
}
|
|
262
|
-
|
|
208
|
+
return cache.get(file);
|
|
209
|
+
};
|
|
210
|
+
return { find, load };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function provider({ find, load }) {
|
|
214
|
+
const memo = new Map();
|
|
215
|
+
const provides = (file, name, stack = new Set()) => {
|
|
216
|
+
const key = `${file}\0${name}`;
|
|
217
|
+
if (memo.has(key)) return memo.get(key);
|
|
218
|
+
if (stack.has(key)) return false;
|
|
219
|
+
stack.add(key);
|
|
220
|
+
const info = load(file);
|
|
221
|
+
let answer;
|
|
222
|
+
if (!info || info.whole) answer = undefined;
|
|
223
|
+
else if (info.locals.has(name) || info.reexports.has(name)) answer = true;
|
|
224
|
+
else answer = starred(file, info, name, stack);
|
|
225
|
+
memo.set(key, answer);
|
|
226
|
+
return answer;
|
|
227
|
+
};
|
|
228
|
+
const starred = (file, info, name, stack) => {
|
|
229
|
+
let answer = false;
|
|
230
|
+
for (const spec of info.stars) {
|
|
231
|
+
const hit = find(file, spec)?.hit;
|
|
232
|
+
const own = hit ? provides(hit, name, stack) : undefined;
|
|
233
|
+
if (own === undefined) return undefined;
|
|
234
|
+
answer ||= own;
|
|
235
|
+
}
|
|
236
|
+
return answer;
|
|
237
|
+
};
|
|
238
|
+
return provides;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function reacher(graph) {
|
|
242
|
+
const { find } = graph;
|
|
243
|
+
const context = { find, provides: provider(graph) };
|
|
244
|
+
return (start) => {
|
|
245
|
+
const touched = new Set();
|
|
246
|
+
const seen = new Set();
|
|
247
|
+
const queue = [[start, BODY]];
|
|
248
|
+
const go = (file, spec, name) => {
|
|
263
249
|
const found = find(file, spec);
|
|
264
|
-
if (found?.hit)
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
250
|
+
if (found?.hit) queue.push([found.hit, name]);
|
|
251
|
+
else if (found) for (const path of found.missing) touched.add(path);
|
|
252
|
+
};
|
|
253
|
+
while (queue.length > 0) {
|
|
254
|
+
const [file, name] = queue.pop();
|
|
255
|
+
const key = `${file}\0${name}`;
|
|
256
|
+
if (seen.has(key)) continue;
|
|
257
|
+
seen.add(key);
|
|
258
|
+
touched.add(file);
|
|
259
|
+
const info = graph.load(file);
|
|
260
|
+
if (!info) continue;
|
|
261
|
+
for (const [next, wanted, spec] of step(info, name, file, context)) {
|
|
262
|
+
if (spec === undefined) queue.push([next, wanted]);
|
|
263
|
+
else go(next, spec, wanted);
|
|
269
264
|
}
|
|
270
265
|
}
|
|
271
|
-
|
|
272
|
-
|
|
266
|
+
return touched;
|
|
267
|
+
};
|
|
273
268
|
}
|
|
274
269
|
|
|
275
|
-
function
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
270
|
+
function starred(file, info, name, { find, provides }) {
|
|
271
|
+
const targets = info.stars.map((spec) => find(file, spec)?.hit);
|
|
272
|
+
if (targets.some((target) => !target)) return [[file, ALL]];
|
|
273
|
+
const answers = targets.map((target) => provides(target, name));
|
|
274
|
+
if (answers.some((answer) => answer === undefined)) return [[file, ALL]];
|
|
275
|
+
const owners = targets.filter((_, index) => answers[index]);
|
|
276
|
+
return owners.length > 0 ? owners.map((target) => [target, name]) : [[file, ALL]];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function step(info, name, file, context) {
|
|
280
|
+
if (name === BODY) {
|
|
281
|
+
return [
|
|
282
|
+
...info.imports.flatMap(({ spec, names }) => names.map((wanted) => [file, wanted, spec])),
|
|
283
|
+
...info.dynamic.flatMap(({ spec, names }) => names.map((wanted) => [file, wanted, spec])),
|
|
284
|
+
];
|
|
282
285
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
286
|
+
if (info.whole) return [[file, BODY]];
|
|
287
|
+
if (name === ALL) {
|
|
288
|
+
return [
|
|
289
|
+
[file, BODY],
|
|
290
|
+
...[...info.reexports.values()].map(({ spec, name: wanted }) => [file, wanted, spec]),
|
|
291
|
+
...info.stars.map((spec) => [file, ALL, spec]),
|
|
292
|
+
];
|
|
290
293
|
}
|
|
291
|
-
return
|
|
294
|
+
if (info.locals.has(name)) return [[file, BODY]];
|
|
295
|
+
const again = info.reexports.get(name);
|
|
296
|
+
if (again) return [[file, again.name, again.spec]];
|
|
297
|
+
if (name === 'default' || info.stars.length === 0) return [[file, ALL]];
|
|
298
|
+
return starred(file, info, name, context);
|
|
292
299
|
}
|
|
293
300
|
|
|
294
301
|
export function select({ root, changed, since }) {
|
|
@@ -297,7 +304,8 @@ export function select({ root, changed, since }) {
|
|
|
297
304
|
const all = files.filter((file) => tests.test(file));
|
|
298
305
|
const fresh = files.filter((file) => since === undefined || statSync(file).mtimeMs > since);
|
|
299
306
|
const touched = new Set([...changed.map((file) => resolve(root, file)), ...fresh]);
|
|
300
|
-
const
|
|
307
|
+
const reach = reacher(modules(root));
|
|
308
|
+
const reached = new Set(all.filter((test) => [...reach(test)].some((file) => touched.has(file))));
|
|
301
309
|
const posix = (file) => relative(root, file).split(sep).join('/');
|
|
302
310
|
return {
|
|
303
311
|
tests: all
|