@yearofthedan/warp 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +97 -2
- package/dist/analysis/complexity.d.ts +4 -0
- package/dist/analysis/complexity.js +84 -0
- package/dist/analysis/folders.d.ts +2 -0
- package/dist/analysis/folders.js +15 -0
- package/dist/analysis/imports.d.ts +13 -0
- package/dist/analysis/imports.js +135 -0
- package/dist/analysis/members.d.ts +4 -0
- package/dist/analysis/members.js +190 -0
- package/dist/analysis/source.d.ts +13 -0
- package/dist/analysis/source.js +29 -0
- package/dist/analysis/specifiers.d.ts +2 -0
- package/dist/analysis/specifiers.js +6 -0
- package/dist/analysis/typescript.d.ts +2 -0
- package/dist/analysis/typescript.js +105 -0
- package/dist/app/check.d.ts +23 -0
- package/dist/app/check.js +15 -0
- package/dist/app/checkModel.d.ts +5 -0
- package/dist/app/checkModel.js +30 -0
- package/dist/app/diff.d.ts +25 -0
- package/dist/app/diff.js +53 -0
- package/dist/app/facts.d.ts +7 -0
- package/dist/app/facts.js +55 -0
- package/dist/app/load.d.ts +13 -0
- package/dist/app/load.js +76 -0
- package/dist/app/ports.d.ts +41 -0
- package/dist/app/ports.js +1 -0
- package/dist/app/unclassified.d.ts +5 -0
- package/dist/app/unclassified.js +13 -0
- package/dist/builtins/cognitive-complexity/cognitiveComplexity.d.ts +6 -0
- package/dist/builtins/cognitive-complexity/cognitiveComplexity.js +26 -0
- package/dist/builtins/components.d.ts +2 -0
- package/dist/builtins/components.js +20 -0
- package/dist/builtins/dependency-direction/dependencyDirection.d.ts +2 -0
- package/dist/builtins/dependency-direction/dependencyDirection.js +65 -0
- package/dist/builtins/file-cohesion/fileCohesion.d.ts +6 -0
- package/dist/builtins/file-cohesion/fileCohesion.js +30 -0
- package/dist/builtins/function-cohesion/functionCohesion.d.ts +6 -0
- package/dist/builtins/function-cohesion/functionCohesion.js +30 -0
- package/dist/builtins/index.d.ts +4 -0
- package/dist/builtins/index.js +30 -0
- package/dist/builtins/loops.d.ts +25 -0
- package/dist/builtins/loops.js +64 -0
- package/dist/builtins/module-cycle/moduleCycle.d.ts +3 -0
- package/dist/builtins/module-cycle/moduleCycle.js +61 -0
- package/dist/builtins/module-externals/moduleExternals.d.ts +2 -0
- package/dist/builtins/module-externals/moduleExternals.js +41 -0
- package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +2 -0
- package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.js +47 -0
- package/dist/builtins/no-cycle/noCycle.d.ts +3 -0
- package/dist/builtins/no-cycle/noCycle.js +39 -0
- package/dist/builtins/no-double-export/noDoubleExport.d.ts +2 -0
- package/dist/builtins/no-double-export/noDoubleExport.js +38 -0
- package/dist/builtins/packages.d.ts +2 -0
- package/dist/builtins/packages.js +5 -0
- package/dist/builtins/place.d.ts +2 -0
- package/dist/builtins/place.js +6 -0
- package/dist/builtins/shared.d.ts +10 -0
- package/dist/builtins/shared.js +13 -0
- package/dist/builtins/unused-declaration/declarations.d.ts +32 -0
- package/dist/builtins/unused-declaration/declarations.js +26 -0
- package/dist/builtins/unused-declaration/empty.d.ts +3 -0
- package/dist/builtins/unused-declaration/empty.js +22 -0
- package/dist/builtins/unused-declaration/grants.d.ts +3 -0
- package/dist/builtins/unused-declaration/grants.js +48 -0
- package/dist/builtins/unused-declaration/surfaces.d.ts +3 -0
- package/dist/builtins/unused-declaration/surfaces.js +57 -0
- package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +2 -0
- package/dist/builtins/unused-declaration/unusedDeclaration.js +14 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +168 -0
- package/dist/config/loadConfig.d.ts +7 -0
- package/dist/config/loadConfig.js +53 -0
- package/dist/config/toolDigest.d.ts +1 -0
- package/dist/config/toolDigest.js +16 -0
- package/dist/git/cache.d.ts +3 -0
- package/dist/git/cache.js +41 -0
- package/dist/git/repository.d.ts +3 -0
- package/dist/git/repository.js +81 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/model/archetype/builder.d.ts +183 -0
- package/dist/model/archetype/builder.js +85 -0
- package/dist/model/archetype/data.d.ts +92 -0
- package/dist/model/archetype/data.js +1 -0
- package/dist/model/archetype/draft.d.ts +37 -0
- package/dist/model/archetype/draft.js +275 -0
- package/dist/model/archetype/reach.d.ts +35 -0
- package/dist/model/archetype/reach.js +209 -0
- package/dist/model/archetype/references.d.ts +13 -0
- package/dist/model/archetype/references.js +166 -0
- package/dist/model/archetype/requirements.d.ts +5 -0
- package/dist/model/archetype/requirements.js +69 -0
- package/dist/model/archetype/resolve.d.ts +30 -0
- package/dist/model/archetype/resolve.js +311 -0
- package/dist/model/archetype/rulesets.d.ts +4 -0
- package/dist/model/archetype/rulesets.js +115 -0
- package/dist/model/archetype/settings.d.ts +21 -0
- package/dist/model/archetype/settings.js +64 -0
- package/dist/model/archetype/shared.d.ts +5 -0
- package/dist/model/archetype/shared.js +4 -0
- package/dist/model/configError.d.ts +3 -0
- package/dist/model/configError.js +3 -0
- package/dist/model/facts.d.ts +57 -0
- package/dist/model/facts.js +1 -0
- package/dist/reporters/claudeHook.d.ts +2 -0
- package/dist/reporters/claudeHook.js +10 -0
- package/dist/reporters/index.d.ts +2 -0
- package/dist/reporters/index.js +9 -0
- package/dist/reporters/json.d.ts +2 -0
- package/dist/reporters/json.js +4 -0
- package/dist/reporters/model.d.ts +4 -0
- package/dist/reporters/model.js +60 -0
- package/dist/reporters/omp.d.ts +2 -0
- package/dist/reporters/omp.js +8 -0
- package/dist/reporters/reporter.d.ts +26 -0
- package/dist/reporters/reporter.js +14 -0
- package/dist/reporters/sarif.d.ts +2 -0
- package/dist/reporters/sarif.js +44 -0
- package/dist/reporters/text.d.ts +6 -0
- package/dist/reporters/text.js +35 -0
- package/dist/rules/diff.d.ts +10 -0
- package/dist/rules/diff.js +31 -0
- package/dist/rules/registry.d.ts +5 -0
- package/dist/rules/registry.js +33 -0
- package/dist/rules/rule.d.ts +59 -0
- package/dist/rules/rule.js +18 -0
- package/integrations/omp/warp.ts +40 -0
- package/package.json +44 -4
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { show } from './references.js';
|
|
2
|
+
import { declarationOf, fillPattern, globRegex } from './resolve.js';
|
|
3
|
+
export const grantText = ({ owner, ref }) => (ref === undefined ? owner : `${owner} uses ${ref}`);
|
|
4
|
+
const none = new Set();
|
|
5
|
+
const union = (sets) => {
|
|
6
|
+
const all = new Set();
|
|
7
|
+
for (const set of sets)
|
|
8
|
+
for (const file of set)
|
|
9
|
+
all.add(file);
|
|
10
|
+
return all;
|
|
11
|
+
};
|
|
12
|
+
const ancestors = function* (module) {
|
|
13
|
+
for (let at = module.parent; at; at = at.parent)
|
|
14
|
+
yield at;
|
|
15
|
+
};
|
|
16
|
+
export const verdicts = (config, model) => {
|
|
17
|
+
const state = { config, model, surfaces: new Map(), reaches: new Map() };
|
|
18
|
+
return {
|
|
19
|
+
reach: (file) => reach(state, file),
|
|
20
|
+
exposes: (module) => exposes(state, module),
|
|
21
|
+
surface: (module, name) => surface(state, module, name),
|
|
22
|
+
verdict: (from, to) => verdict(state, from, to),
|
|
23
|
+
files: (module, ref) => resolve(state, module, ref),
|
|
24
|
+
describe: (module, ref) => describe(state, module, ref),
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const exposes = ({ config }, module) => {
|
|
28
|
+
if (module.kind === 'dir')
|
|
29
|
+
return config.archetypes[module.archetype].exposes;
|
|
30
|
+
const declared = declarationOf(config, module);
|
|
31
|
+
return declared?.kind === 'files' ? declared.exposes : undefined;
|
|
32
|
+
};
|
|
33
|
+
const surface = (state, module, name) => {
|
|
34
|
+
const key = `${module.id}\0${name ?? ''}`;
|
|
35
|
+
let files = state.surfaces.get(key);
|
|
36
|
+
if (!files) {
|
|
37
|
+
files = exposed(state, module, name);
|
|
38
|
+
state.surfaces.set(key, files);
|
|
39
|
+
}
|
|
40
|
+
return files;
|
|
41
|
+
};
|
|
42
|
+
const exposed = (state, module, name) => {
|
|
43
|
+
const declared = exposes(state, module);
|
|
44
|
+
if (declared === undefined)
|
|
45
|
+
return none;
|
|
46
|
+
if (declared.kind === 'surfaces') {
|
|
47
|
+
const refs = name === undefined ? Object.values(declared.surfaces).flat() : (declared.surfaces[name] ?? []);
|
|
48
|
+
return union(refs.map((ref) => resolve(state, module, ref)));
|
|
49
|
+
}
|
|
50
|
+
const all = union([new Set(module.files), ...module.children.map((child) => surface(state, child))]);
|
|
51
|
+
if (declared.kind === 'all')
|
|
52
|
+
return all;
|
|
53
|
+
const patterns = declared.patterns.flatMap((pattern) => fillPattern(pattern, module.captures) ?? []);
|
|
54
|
+
const inside = (file) => (module.dir === '' ? file : file.slice(module.dir.length + 1));
|
|
55
|
+
return new Set([...all].filter((file) => patterns.some((pattern) => globRegex(pattern).test(inside(file)))));
|
|
56
|
+
};
|
|
57
|
+
const provider = (state, module, name) => {
|
|
58
|
+
for (const at of ancestors(module)) {
|
|
59
|
+
const ref = state.config.archetypes[at.archetype].provides[name];
|
|
60
|
+
if (ref)
|
|
61
|
+
return { at, ref };
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
};
|
|
65
|
+
const resolve = (state, module, ref) => {
|
|
66
|
+
if (ref.kind === 'package')
|
|
67
|
+
return none;
|
|
68
|
+
if (ref.kind === 'required')
|
|
69
|
+
return resolveRequired(state, module, ref.name, ref.surface);
|
|
70
|
+
return union(module.children.filter((child) => child.name === ref.module).map((child) => surface(state, child, ref.surface)));
|
|
71
|
+
};
|
|
72
|
+
const resolveRequired = (state, module, name, surface) => {
|
|
73
|
+
const found = provider(state, module, name);
|
|
74
|
+
return resolve(state, found.at, { ...found.ref, surface });
|
|
75
|
+
};
|
|
76
|
+
const describe = (state, module, ref) => {
|
|
77
|
+
if (ref.kind === 'module')
|
|
78
|
+
return `m.${show(ref)}`;
|
|
79
|
+
if (ref.kind === 'package')
|
|
80
|
+
return show(ref);
|
|
81
|
+
return `${show(ref)} (from ${provider(state, module, ref.name).at.id})`;
|
|
82
|
+
};
|
|
83
|
+
const grants = (state, holder, owner, refs) => refs.map((ref) => ({
|
|
84
|
+
owner,
|
|
85
|
+
ref: describe(state, holder, ref),
|
|
86
|
+
files: resolve(state, holder, ref),
|
|
87
|
+
...(ref.kind === 'package' ? { package: ref.pattern } : {}),
|
|
88
|
+
declared: ref,
|
|
89
|
+
}));
|
|
90
|
+
const reach = (state, file) => {
|
|
91
|
+
const module = state.model.moduleOf(file);
|
|
92
|
+
if (!module || module.looseFiles.includes(file))
|
|
93
|
+
return undefined;
|
|
94
|
+
if (state.model.isTest(file))
|
|
95
|
+
return testReach(state, module);
|
|
96
|
+
let found = state.reaches.get(module.id);
|
|
97
|
+
if (!found) {
|
|
98
|
+
const granted = [
|
|
99
|
+
{ owner: `own module ${module.id}`, files: new Set(module.files) },
|
|
100
|
+
...module.children
|
|
101
|
+
.filter((child) => !child.test)
|
|
102
|
+
.map((child) => ({ owner: module.id, ref: child.id, files: surface(state, child) })),
|
|
103
|
+
...using(state, module),
|
|
104
|
+
];
|
|
105
|
+
if (module.kind === 'files')
|
|
106
|
+
granted.push(...using(state, module.parent));
|
|
107
|
+
found = granted;
|
|
108
|
+
state.reaches.set(module.id, found);
|
|
109
|
+
}
|
|
110
|
+
return found;
|
|
111
|
+
};
|
|
112
|
+
const testReach = (state, module) => {
|
|
113
|
+
const owner = module.test ? module.parent : module;
|
|
114
|
+
const key = `tests\0${owner.id}`;
|
|
115
|
+
let found = state.reaches.get(key);
|
|
116
|
+
if (!found) {
|
|
117
|
+
found = owner.kind === 'dir' ? folderTests(state, owner) : filesTests(state, owner);
|
|
118
|
+
state.reaches.set(key, found);
|
|
119
|
+
}
|
|
120
|
+
return found;
|
|
121
|
+
};
|
|
122
|
+
const folderTests = (state, owner) => {
|
|
123
|
+
const by = `tests of ${owner.id}`;
|
|
124
|
+
const own = owner.children.filter((child) => child.kind === 'files');
|
|
125
|
+
return [
|
|
126
|
+
{
|
|
127
|
+
owner: by,
|
|
128
|
+
files: union([new Set([...owner.files, ...owner.tests]), ...own.map((child) => new Set(child.files))]),
|
|
129
|
+
},
|
|
130
|
+
...owner.children
|
|
131
|
+
.filter((child) => child.kind === 'dir')
|
|
132
|
+
.map((child) => ({ owner: by, ref: child.id, files: union([surface(state, child), testSurface(state, child)]) })),
|
|
133
|
+
...(owner.parent ? using(state, owner) : []),
|
|
134
|
+
...own.filter((child) => !child.test).flatMap((child) => using(state, child)),
|
|
135
|
+
...layerUsing(state, owner, owner, by),
|
|
136
|
+
...[...ancestors(owner)].flatMap((above) => layer(state, above, by)),
|
|
137
|
+
];
|
|
138
|
+
};
|
|
139
|
+
const filesTests = (state, owner) => {
|
|
140
|
+
const by = `tests of ${owner.id}`;
|
|
141
|
+
const folder = owner.parent;
|
|
142
|
+
const declared = declarationOf(state.config, owner);
|
|
143
|
+
const tests = declared?.kind === 'files' ? declared.tests : undefined;
|
|
144
|
+
return [
|
|
145
|
+
{ owner: by, files: new Set([...owner.files, ...owner.tests]) },
|
|
146
|
+
...using(state, owner),
|
|
147
|
+
...using(state, folder),
|
|
148
|
+
...(tests ? grants(state, folder, by, tests.using) : []),
|
|
149
|
+
...[folder, ...ancestors(folder)].flatMap((above) => layer(state, above, by)),
|
|
150
|
+
];
|
|
151
|
+
};
|
|
152
|
+
const layerOf = ({ config }, module) => module.archetype === undefined ? undefined : config.archetypes[module.archetype].tests;
|
|
153
|
+
const layerUsing = (state, holder, at, by) => grants(state, holder, by, layerOf(state, at)?.using ?? []);
|
|
154
|
+
const layer = (state, above, by) => {
|
|
155
|
+
const tests = above.children.filter((child) => child.test).map((child) => new Set(child.files));
|
|
156
|
+
return [
|
|
157
|
+
...(tests.length > 0 ? [{ owner: by, ref: `${above.id} tests`, files: union(tests) }] : []),
|
|
158
|
+
...layerUsing(state, above, above, by),
|
|
159
|
+
];
|
|
160
|
+
};
|
|
161
|
+
const testSurface = (state, module) => {
|
|
162
|
+
const exposes = layerOf(state, module)?.exposes;
|
|
163
|
+
if (!exposes)
|
|
164
|
+
return none;
|
|
165
|
+
const testModules = module.children.filter((child) => child.test);
|
|
166
|
+
if (exposes.kind !== 'surfaces')
|
|
167
|
+
return union([
|
|
168
|
+
...testModules.map((child) => new Set(child.files)),
|
|
169
|
+
...module.children.filter((child) => child.kind === 'dir').map((child) => testSurface(state, child)),
|
|
170
|
+
]);
|
|
171
|
+
const named = new Set(Object.values(exposes.surfaces)
|
|
172
|
+
.flat()
|
|
173
|
+
.flatMap((ref) => (ref.kind === 'module' ? [`tests.${ref.module}`] : [])));
|
|
174
|
+
return union(testModules.filter((child) => named.has(child.name)).map((child) => new Set(child.files)));
|
|
175
|
+
};
|
|
176
|
+
const using = (state, module) => {
|
|
177
|
+
const declared = declarationOf(state.config, module);
|
|
178
|
+
if (!declared)
|
|
179
|
+
return [];
|
|
180
|
+
const owner = module.kind === 'files' ? declared.name : `${module.parent.archetype}.${declared.name}`;
|
|
181
|
+
return grants(state, module.parent, owner, declared.using);
|
|
182
|
+
};
|
|
183
|
+
const verdict = (state, from, to) => {
|
|
184
|
+
const granted = reach(state, from);
|
|
185
|
+
const target = state.model.moduleOf(to);
|
|
186
|
+
if (!granted || !target)
|
|
187
|
+
return undefined;
|
|
188
|
+
const grant = granted.find((candidate) => candidate.files.has(to));
|
|
189
|
+
if (grant)
|
|
190
|
+
return { allowed: true, grant };
|
|
191
|
+
if (state.model.isTest(to) && !state.model.isTest(from))
|
|
192
|
+
return { allowed: false, reason: 'test' };
|
|
193
|
+
return { allowed: false, ...refusal(state, state.model.moduleOf(from), target, from, to) };
|
|
194
|
+
};
|
|
195
|
+
const refusal = (state, source, target, from, to) => {
|
|
196
|
+
const above = new Set(ancestors(source));
|
|
197
|
+
if (above.has(target))
|
|
198
|
+
return { reason: 'above' };
|
|
199
|
+
if (target.kind === 'files' && target.parent !== source.parent && above.has(target.parent))
|
|
200
|
+
return { reason: 'above' };
|
|
201
|
+
const tested = state.model.isTest(from);
|
|
202
|
+
const start = target.test ? target.parent : target;
|
|
203
|
+
for (let at = start; !above.has(at) && at !== source; at = at.parent) {
|
|
204
|
+
const open = tested ? union([surface(state, at), testSurface(state, at)]) : surface(state, at);
|
|
205
|
+
if (!open.has(to))
|
|
206
|
+
return { reason: 'private', hiddenBy: at };
|
|
207
|
+
}
|
|
208
|
+
return { reason: 'not-in-using' };
|
|
209
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ExposesData, RefData, RuleSettingsData } from './data.ts';
|
|
2
|
+
import type { Draft, DraftExposes } from './draft.ts';
|
|
3
|
+
import { type Fail } from './shared.ts';
|
|
4
|
+
export interface Check {
|
|
5
|
+
fail: Fail;
|
|
6
|
+
ruleIds?: ReadonlySet<string> | undefined;
|
|
7
|
+
options: Readonly<Record<string, (raw: Record<string, unknown>, at: string) => unknown>>;
|
|
8
|
+
exposesOf: (archetype: string) => DraftExposes | undefined;
|
|
9
|
+
}
|
|
10
|
+
export declare const show: (ref: RefData) => string;
|
|
11
|
+
export declare const resolveDraft: (check: Check, { data: archetype, modules, exposes, provides, tests }: Draft) => void;
|
|
12
|
+
export declare const toRules: (check: Check, value: unknown, where: string) => RuleSettingsData | undefined;
|
|
13
|
+
export declare const exposesText: (exposes: DraftExposes | ExposesData | undefined) => string;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { ConfigError } from '../configError.js';
|
|
2
|
+
import { recordedAs } from './builder.js';
|
|
3
|
+
import { isName, isRecord, list } from './shared.js';
|
|
4
|
+
export const show = (ref) => ref.kind === 'package'
|
|
5
|
+
? `pkg('${ref.pattern}')`
|
|
6
|
+
: `${ref.kind === 'required' ? `required.${ref.name}` : ref.module}${ref.surface === undefined ? '' : `.${ref.surface}`}`;
|
|
7
|
+
export const resolveDraft = (check, { data: archetype, modules, exposes, provides, tests }) => {
|
|
8
|
+
for (const [index, module] of modules.entries()) {
|
|
9
|
+
const scope = {
|
|
10
|
+
archetype,
|
|
11
|
+
visible: index,
|
|
12
|
+
kinds: ['module', 'required', 'package'],
|
|
13
|
+
refused: '',
|
|
14
|
+
module: module.data.name,
|
|
15
|
+
};
|
|
16
|
+
const where = `${archetype.name}.${module.data.name}`;
|
|
17
|
+
module.data.using.push(...module.using.map((value) => toRef(check, value, `${where} uses`, scope)));
|
|
18
|
+
if (module.tests && module.data.kind === 'files')
|
|
19
|
+
module.data.tests = testLayer(check, `${where}.tests`, archetype, module.tests, scope);
|
|
20
|
+
}
|
|
21
|
+
const own = {
|
|
22
|
+
archetype,
|
|
23
|
+
visible: archetype.modules.length,
|
|
24
|
+
kinds: ['module'],
|
|
25
|
+
refused: 'exposes and provides name the archetype’s own modules',
|
|
26
|
+
};
|
|
27
|
+
if (exposes)
|
|
28
|
+
archetype.exposes = exposesData(check, archetype.name, exposes, own);
|
|
29
|
+
for (const [name, value] of Object.entries(provides)) {
|
|
30
|
+
const scope = { ...own, whole: true };
|
|
31
|
+
archetype.provides[name] = toRef(check, value, `${archetype.name} provides ${name} as`, scope);
|
|
32
|
+
}
|
|
33
|
+
if (tests)
|
|
34
|
+
archetype.tests = testLayer(check, `${archetype.name}.tests`, archetype, tests, {
|
|
35
|
+
...own,
|
|
36
|
+
kinds: ['module', 'required', 'package'],
|
|
37
|
+
refused: '',
|
|
38
|
+
module: 'tests',
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const testLayer = (check, where, holder, tests, scope) => {
|
|
42
|
+
const modules = tests.modules.map((module) => module.data);
|
|
43
|
+
const layer = { ...holder, name: where, modules, provides: {} };
|
|
44
|
+
const own = {
|
|
45
|
+
archetype: layer,
|
|
46
|
+
visible: modules.length,
|
|
47
|
+
kinds: ['module'],
|
|
48
|
+
refused: 'exposes names the layer’s own test modules',
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
...(tests.locator ? { locator: tests.locator } : {}),
|
|
52
|
+
modules,
|
|
53
|
+
using: tests.using.map((value) => toRef(check, value, `${where} uses`, scope)),
|
|
54
|
+
...(tests.exposes ? { exposes: exposesData(check, where, tests.exposes, own) } : {}),
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const exposesData = (check, name, exposes, scope) => exposes.kind !== 'surfaces'
|
|
58
|
+
? exposes
|
|
59
|
+
: {
|
|
60
|
+
kind: 'surfaces',
|
|
61
|
+
surfaces: Object.fromEntries(Object.entries(exposes.surfaces).map(([surface, refs]) => [
|
|
62
|
+
surface,
|
|
63
|
+
refs.map((value) => toRef(check, value, `${name} exposes ${surface} as`, scope)),
|
|
64
|
+
])),
|
|
65
|
+
};
|
|
66
|
+
const toRef = (check, value, where, scope) => {
|
|
67
|
+
const ref = recordedAs(value, 'ref');
|
|
68
|
+
if (!ref)
|
|
69
|
+
check.fail(`${where} something other than a reference: use s.module, required.name or pkg('name')`);
|
|
70
|
+
else {
|
|
71
|
+
const problem = refProblem(check, ref, scope);
|
|
72
|
+
if (problem)
|
|
73
|
+
check.fail(`${where} ${show(ref)}; ${problem}`);
|
|
74
|
+
}
|
|
75
|
+
return { ...ref };
|
|
76
|
+
};
|
|
77
|
+
export const toRules = (check, value, where) => {
|
|
78
|
+
if (value === undefined || value === 'off')
|
|
79
|
+
return value;
|
|
80
|
+
if (!isRecord(value)) {
|
|
81
|
+
check.fail(`${where}: expected 'off' or rule settings by id`);
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return Object.fromEntries(Object.entries(value).map(([id, setting]) => [id, toSetting(check, id, setting, where)]));
|
|
85
|
+
};
|
|
86
|
+
const toSetting = (check, id, setting, where) => {
|
|
87
|
+
if (id !== '*' && check.ruleIds && !check.ruleIds.has(id))
|
|
88
|
+
check.fail(`${where}: no rule called ${id}`);
|
|
89
|
+
else if (typeof setting === 'boolean')
|
|
90
|
+
return setting;
|
|
91
|
+
else if (id !== '*' && isRecord(setting) && !recordedAs(setting, 'ref'))
|
|
92
|
+
return checkOptions(check, id, setting, where);
|
|
93
|
+
else
|
|
94
|
+
check.fail(`${where}.${id}: expected true, false${id === '*' ? '' : ' or options'}`);
|
|
95
|
+
return undefined;
|
|
96
|
+
};
|
|
97
|
+
const checkOptions = (check, id, options, where) => {
|
|
98
|
+
try {
|
|
99
|
+
check.options[id]?.(options, `${where}.${id}`);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (!(error instanceof ConfigError))
|
|
103
|
+
throw error;
|
|
104
|
+
check.fail(error.message);
|
|
105
|
+
}
|
|
106
|
+
return { ...options };
|
|
107
|
+
};
|
|
108
|
+
const refProblem = (check, ref, scope) => {
|
|
109
|
+
if (!scope.kinds.includes(ref.kind))
|
|
110
|
+
return scope.refused;
|
|
111
|
+
if (ref.kind === 'package')
|
|
112
|
+
return isName(ref.pattern) ? undefined : 'a package pattern is a non-empty string';
|
|
113
|
+
if (ref.kind === 'required')
|
|
114
|
+
return requiredProblem(check, ref.name, ref.surface, scope.archetype);
|
|
115
|
+
return moduleProblem(check, ref.module, ref.surface, scope);
|
|
116
|
+
};
|
|
117
|
+
const requiredProblem = (check, name, surface, archetype) => {
|
|
118
|
+
const requirement = archetype.requires[name];
|
|
119
|
+
if (!requirement)
|
|
120
|
+
return `${archetype.name} requires ${list(Object.keys(archetype.requires), 'nothing')}`;
|
|
121
|
+
if (requirement.kind !== 'archetype')
|
|
122
|
+
return surface === undefined ? undefined : `required.${name} is ${requirement.kind}(), which has no surfaces`;
|
|
123
|
+
return exposedProblem(`required.${name}`, requirement.archetype, check.exposesOf(requirement.archetype), surface);
|
|
124
|
+
};
|
|
125
|
+
const moduleProblem = (check, name, surface, scope) => {
|
|
126
|
+
const index = scope.archetype.modules.findIndex((module) => module.name === name);
|
|
127
|
+
if (index < 0 || index >= scope.visible)
|
|
128
|
+
return visibilityProblem(name, index, scope);
|
|
129
|
+
if (surface !== undefined && scope.whole)
|
|
130
|
+
return 'name the whole module, without a surface';
|
|
131
|
+
const module = scope.archetype.modules[index];
|
|
132
|
+
if (module.kind === 'files')
|
|
133
|
+
return exposedProblem(name, undefined, module.exposes, surface);
|
|
134
|
+
return exposedProblem(name, archetypeText(scope.archetype, module), check.exposesOf(module.archetype), surface);
|
|
135
|
+
};
|
|
136
|
+
const archetypeText = (holder, module) => module.archetype === holder.name || module.archetype.startsWith(`${holder.name}.`) ? undefined : module.archetype;
|
|
137
|
+
const exposedProblem = (name, archetype, exposes, surface) => {
|
|
138
|
+
const subject = archetype === undefined ? name : `${name} (${archetype})`;
|
|
139
|
+
if (exposes === undefined)
|
|
140
|
+
return `${subject} exposes nothing: give ${archetype ?? name} exposes, or drop the reference`;
|
|
141
|
+
if (surface === undefined)
|
|
142
|
+
return undefined;
|
|
143
|
+
if (exposes.kind !== 'surfaces')
|
|
144
|
+
return `${subject} exposes ${exposesText(exposes)}, not named surfaces`;
|
|
145
|
+
const surfaces = Object.keys(exposes.surfaces);
|
|
146
|
+
return surfaces.includes(surface) ? undefined : `${subject} exposes ${list(surfaces, 'nothing')}`;
|
|
147
|
+
};
|
|
148
|
+
export const exposesText = (exposes) => {
|
|
149
|
+
if (exposes === undefined)
|
|
150
|
+
return 'nothing';
|
|
151
|
+
if (exposes.kind === 'all')
|
|
152
|
+
return 'all its files';
|
|
153
|
+
if (exposes.kind === 'files')
|
|
154
|
+
return `files(${exposes.patterns.map((pattern) => `'${pattern}'`).join(', ')})`;
|
|
155
|
+
return list(Object.keys(exposes.surfaces), 'nothing');
|
|
156
|
+
};
|
|
157
|
+
const visibilityProblem = (name, index, { archetype, visible, module }) => {
|
|
158
|
+
const above = list(archetype.modules.slice(0, visible).map((candidate) => candidate.name), 'no modules');
|
|
159
|
+
if (module === undefined)
|
|
160
|
+
return `${archetype.name} has modules ${above}`;
|
|
161
|
+
if (index < 0)
|
|
162
|
+
return `${module} sees ${above}`;
|
|
163
|
+
if (index === visible)
|
|
164
|
+
return `a module reaches its own files already; ${module} sees ${above}`;
|
|
165
|
+
return `${name} is declared below ${module}, which sees ${above}`;
|
|
166
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ArchetypeData } from './data.ts';
|
|
2
|
+
import type { Draft } from './draft.ts';
|
|
3
|
+
import type { Fail } from './shared.ts';
|
|
4
|
+
export declare const checkUnused: (drafts: readonly Draft[], fail: Fail) => void;
|
|
5
|
+
export declare const checkRequirements: (archetypes: Record<string, ArchetypeData>, root: string, fail: Fail) => string[];
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { recordedAs } from './builder.js';
|
|
2
|
+
const usedRequirements = (drafts) => new Set(drafts
|
|
3
|
+
.flatMap((draft) => [
|
|
4
|
+
...draft.modules.flatMap((module) => [...module.using, ...(module.tests?.using ?? [])]),
|
|
5
|
+
...(draft.tests?.using ?? []),
|
|
6
|
+
])
|
|
7
|
+
.map((value) => recordedAs(value, 'ref'))
|
|
8
|
+
.map((ref) => ref?.kind === 'required' && ref.name));
|
|
9
|
+
export const checkUnused = (drafts, fail) => {
|
|
10
|
+
for (const draft of drafts.filter((candidate) => !candidate.data.inline)) {
|
|
11
|
+
const inline = drafts.filter((candidate) => candidate.data.name.startsWith(`${draft.data.name}.`));
|
|
12
|
+
const used = usedRequirements([draft, ...inline]);
|
|
13
|
+
for (const name of Object.keys(draft.data.requires)) {
|
|
14
|
+
if (!used.has(name))
|
|
15
|
+
fail(`${draft.data.name} requires ${name}, which no module lists in using`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export const checkRequirements = (archetypes, root, fail) => {
|
|
20
|
+
const provisions = Object.values(archetypes).flatMap((archetype) => Object.entries(archetype.provides).map(([name, ref]) => ({
|
|
21
|
+
archetype: archetype.name,
|
|
22
|
+
name,
|
|
23
|
+
module: archetype.modules.find((module) => module.name === ref.module),
|
|
24
|
+
used: false,
|
|
25
|
+
})));
|
|
26
|
+
walk({ archetypes, provisions, fail, missing: new Set(), visited: new Set() }, archetypes[root], '', new Map());
|
|
27
|
+
return provisions
|
|
28
|
+
.filter((p) => !p.used)
|
|
29
|
+
.map((p) => `${p.archetype} provides ${p.name}, which nothing below requires`);
|
|
30
|
+
};
|
|
31
|
+
const walk = (state, archetype, path, above) => {
|
|
32
|
+
const at = path === '' ? archetype.name : `${path} (${archetype.name})`;
|
|
33
|
+
if (!archetype.inline) {
|
|
34
|
+
for (const [name, requirement] of Object.entries(archetype.requires)) {
|
|
35
|
+
answer(state, at, archetype.name, name, requirement, above.get(name));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const below = new Map(above);
|
|
39
|
+
for (const provision of state.provisions.filter((p) => p.archetype === archetype.name))
|
|
40
|
+
below.set(provision.name, provision);
|
|
41
|
+
const signature = JSON.stringify([...below].map(([name, provision]) => [name, provision.archetype]));
|
|
42
|
+
for (const module of archetype.modules) {
|
|
43
|
+
if (module.kind !== 'dir' || state.visited.has(`${module.archetype}|${signature}`))
|
|
44
|
+
continue;
|
|
45
|
+
state.visited.add(`${module.archetype}|${signature}`);
|
|
46
|
+
walk(state, state.archetypes[module.archetype], path === '' ? module.name : `${path}.${module.name}`, below);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const answer = (state, at, archetype, name, requirement, provider) => {
|
|
50
|
+
if (!provider) {
|
|
51
|
+
if (!state.missing.has(`${archetype}.${name}`))
|
|
52
|
+
state.fail(`${at} requires ${name}, which nothing above it provides`);
|
|
53
|
+
state.missing.add(`${archetype}.${name}`);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
provider.used = true;
|
|
57
|
+
if (!fits(requirement, provider.module)) {
|
|
58
|
+
state.fail(`${at} requires ${name} as ${requirementText(requirement)}; ${provider.archetype} provides ${name} from ${provider.module.name}, ${moduleText(state, provider.module)}`);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const moduleText = (state, module) => {
|
|
62
|
+
if (module.kind === 'files')
|
|
63
|
+
return 'a files module';
|
|
64
|
+
return state.archetypes[module.archetype].inline ? 'a dir module' : `modules of ${module.archetype}`;
|
|
65
|
+
};
|
|
66
|
+
const requirementText = (requirement) => requirement.kind === 'archetype' ? `modules of ${requirement.archetype}` : `${requirement.kind}()`;
|
|
67
|
+
const fits = (requirement, module) => requirement.kind === 'archetype'
|
|
68
|
+
? module.kind === 'dir' && module.archetype === requirement.archetype
|
|
69
|
+
: module.kind === requirement.kind;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ConfigData, ModuleData } from './data.ts';
|
|
2
|
+
export interface Folders {
|
|
3
|
+
folders(dir: string): string[];
|
|
4
|
+
exists(path: string): boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface ModuleView {
|
|
7
|
+
id: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
kind: 'dir' | 'files';
|
|
10
|
+
dir: string;
|
|
11
|
+
archetype?: string;
|
|
12
|
+
captures: Record<string, string>;
|
|
13
|
+
parent?: ModuleView;
|
|
14
|
+
children: ModuleView[];
|
|
15
|
+
files: string[];
|
|
16
|
+
looseFiles: string[];
|
|
17
|
+
tests: string[];
|
|
18
|
+
test?: true;
|
|
19
|
+
}
|
|
20
|
+
export interface Model {
|
|
21
|
+
root: ModuleView;
|
|
22
|
+
modules: ModuleView[];
|
|
23
|
+
moduleOf(file: string): ModuleView | undefined;
|
|
24
|
+
isTest(file: string): boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare const declarationOf: (config: ConfigData, module: ModuleView) => ModuleData | undefined;
|
|
27
|
+
export declare const fillPattern: (pattern: string, captures: Record<string, string>) => string | undefined;
|
|
28
|
+
export declare const resolveModel: (config: ConfigData, folders: Folders, files: readonly string[]) => Model;
|
|
29
|
+
export declare const specificity: (pattern: string) => number;
|
|
30
|
+
export declare const globRegex: (pattern: string) => RegExp;
|