@yearofthedan/warp 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.
Files changed (54) hide show
  1. package/README.md +24 -25
  2. package/dist/analysis/imports.d.ts +3 -1
  3. package/dist/analysis/imports.js +5 -1
  4. package/dist/analysis/typescript.js +2 -1
  5. package/dist/app/check.d.ts +3 -1
  6. package/dist/app/check.js +5 -3
  7. package/dist/app/checkModel.js +1 -1
  8. package/dist/app/configLines.d.ts +5 -0
  9. package/dist/app/configLines.js +24 -0
  10. package/dist/app/load.d.ts +2 -2
  11. package/dist/app/load.js +31 -43
  12. package/dist/builtins/advice.d.ts +8 -0
  13. package/dist/builtins/advice.js +42 -0
  14. package/dist/builtins/dependency-direction/dependencyDirection.js +33 -16
  15. package/dist/builtins/index.js +2 -0
  16. package/dist/builtins/module-externals/moduleExternals.js +11 -9
  17. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.js +16 -5
  18. package/dist/builtins/place.js +5 -3
  19. package/dist/builtins/unresolved-import/unresolvedImport.d.ts +2 -0
  20. package/dist/builtins/unresolved-import/unresolvedImport.js +15 -0
  21. package/dist/builtins/unused-declaration/declarations.js +1 -1
  22. package/dist/builtins/unused-declaration/empty.js +19 -11
  23. package/dist/builtins/unused-declaration/grants.js +22 -15
  24. package/dist/builtins/unused-declaration/surfaces.js +24 -22
  25. package/dist/cli.js +14 -11
  26. package/dist/config/loadConfig.d.ts +7 -1
  27. package/dist/config/loadConfig.js +83 -12
  28. package/dist/index.d.ts +0 -1
  29. package/dist/index.js +0 -1
  30. package/dist/model/archetype/data.d.ts +25 -29
  31. package/dist/model/archetype/data.js +1 -1
  32. package/dist/model/archetype/fixes.d.ts +35 -0
  33. package/dist/model/archetype/fixes.js +265 -0
  34. package/dist/model/archetype/language.d.ts +16 -0
  35. package/dist/model/archetype/language.js +419 -0
  36. package/dist/model/archetype/reach.d.ts +8 -3
  37. package/dist/model/archetype/reach.js +142 -122
  38. package/dist/model/archetype/references.d.ts +11 -5
  39. package/dist/model/archetype/references.js +58 -132
  40. package/dist/model/archetype/requirements.d.ts +3 -3
  41. package/dist/model/archetype/requirements.js +65 -61
  42. package/dist/model/archetype/resolve.d.ts +3 -3
  43. package/dist/model/archetype/resolve.js +64 -77
  44. package/dist/model/archetype/rulesets.d.ts +2 -1
  45. package/dist/model/archetype/rulesets.js +72 -49
  46. package/dist/model/archetype/settings.js +4 -19
  47. package/dist/model/facts.d.ts +6 -0
  48. package/dist/reporters/model.js +9 -9
  49. package/dist/rules/registry.js +1 -1
  50. package/package.json +3 -2
  51. package/dist/model/archetype/builder.d.ts +0 -183
  52. package/dist/model/archetype/builder.js +0 -85
  53. package/dist/model/archetype/draft.d.ts +0 -37
  54. package/dist/model/archetype/draft.js +0 -275
@@ -0,0 +1,265 @@
1
+ import { instanceOf, verdicts } from './reach.js';
2
+ const ancestors = function* (module) {
3
+ for (let at = module.parent; at; at = at.parent)
4
+ yield at;
5
+ };
6
+ const lineage = (module) => [module, ...ancestors(module)];
7
+ export const namedArchetype = (config, name) => config.archetypes[name].inline ? namedArchetype(config, name.slice(0, name.lastIndexOf('.'))) : name;
8
+ const withArchetype = (config, name, change) => ({
9
+ ...config,
10
+ archetypes: { ...config.archetypes, [name]: change(config.archetypes[name]) },
11
+ });
12
+ const withModule = (config, archetype, name, change) => withArchetype(config, archetype, (data) => ({
13
+ ...data,
14
+ modules: data.modules.map((module) => (module.name === name ? change(module) : module)),
15
+ }));
16
+ const declarationSite = (config, module) => {
17
+ const holder = module.parent;
18
+ const archetype = holder.archetype;
19
+ return {
20
+ label: `${archetype}.${module.name}.dependencies`,
21
+ holder,
22
+ add: (from, ref) => withModule(from, archetype, module.name, (data) => ({ ...data, using: [...data.using, ref] })),
23
+ self: module.name,
24
+ tests: module.test === true,
25
+ };
26
+ };
27
+ const kitSite = (at) => {
28
+ const archetype = at.archetype;
29
+ return {
30
+ label: `${archetype}.test.provides`,
31
+ holder: at,
32
+ add: (from, ref) => withArchetype(from, archetype, (data) => ({ ...data, testUsing: [...(data.testUsing ?? []), ref] })),
33
+ tests: true,
34
+ };
35
+ };
36
+ export const sitesOf = (config, model, file) => {
37
+ const module = model.moduleOf(file);
38
+ if (!module || module.looseFiles.includes(file))
39
+ return [];
40
+ if (module.test)
41
+ return lineage(module).map((at) => (at.test ? declarationSite(config, at) : kitSite(at)));
42
+ return module.parent ? [declarationSite(config, module)] : [];
43
+ };
44
+ const surfacesOf = ({ exposes, testExposes }, tests) => [
45
+ ...new Set([
46
+ ...(exposes?.kind === 'surfaces' ? Object.keys(exposes.surfaces) : []),
47
+ ...(tests ? Object.keys(testExposes ?? {}) : []),
48
+ ]),
49
+ ];
50
+ const moduleRefs = (config, module, tests, up = 0) => {
51
+ const surfaces = module.kind === 'dir' ? surfacesOf(config.archetypes[module.archetype], tests) : [];
52
+ const name = module.name;
53
+ const out = up > 0 ? { up } : {};
54
+ return [
55
+ ...surfaces.map((surface) => ({ kind: 'module', module: name, surface, ...out })),
56
+ { kind: 'module', module: name, ...out },
57
+ ];
58
+ };
59
+ const levels = (config, holder) => {
60
+ const found = [holder];
61
+ for (let at = holder; config.archetypes[at.archetype].inline && at.parent; at = at.parent)
62
+ found.push(at.parent);
63
+ return found;
64
+ };
65
+ const inputsOf = (config, site) => {
66
+ const named = config.archetypes[namedArchetype(config, site.holder.archetype)];
67
+ const names = [...named.requires, ...(site.tests ? (named.testRequires ?? []) : [])];
68
+ return names.map((name) => ({ kind: 'required', name }));
69
+ };
70
+ const allowed = ({ model, from, to }, config) => verdicts(config, model).verdict(from, to).allowed;
71
+ const targetLineage = ({ model, to }) => lineage(model.moduleOf(to));
72
+ const sameDeclaration = (a, b) => a.parent === b.parent && a.name === b.name;
73
+ const reachable = (search) => {
74
+ const importer = lineage(search.model.moduleOf(search.from));
75
+ return targetLineage(search).filter((module) => !importer.some((own) => sameDeclaration(own, module)));
76
+ };
77
+ const nameable = (site, module) => !(module.parent === site.holder && module.name === site.self) && (site.tests || !module.test);
78
+ const usingChanges = function* (search, config) {
79
+ const targets = reachable(search);
80
+ for (const site of sitesOf(config, search.model, search.from)) {
81
+ const own = levels(config, site.holder).flatMap((level, up) => targets
82
+ .filter((module) => module.parent === level && nameable(site, module))
83
+ .flatMap((module) => moduleRefs(config, module, site.tests, up)));
84
+ for (const ref of [...own, ...inputsOf(config, site)])
85
+ yield [{ fix: { kind: 'using', site: site.label, ref }, config: site.add(config, ref) }];
86
+ }
87
+ };
88
+ const bindingAt = (config, chain, at, name) => {
89
+ const holder = at.parent;
90
+ if (holder.children.some((child) => child.name === name))
91
+ return { kind: 'module', module: name };
92
+ return chain.has(namedArchetype(config, holder.archetype)) ? { kind: 'required', name } : undefined;
93
+ };
94
+ const instancesOf = (config, model, archetype) => model.modules.filter((module) => module.archetype === archetype && !config.archetypes[archetype].inline && module.parent);
95
+ const chainTo = (config, holder, target) => {
96
+ const chain = [];
97
+ for (let instance = instanceOf({ config }, holder); instance.parent; instance = instanceOf({ config }, instance.parent)) {
98
+ const archetype = instance.archetype;
99
+ if (config.archetypes[archetype].inline)
100
+ return undefined;
101
+ if (!chain.includes(archetype))
102
+ chain.push(archetype);
103
+ if (instance.parent === target.parent)
104
+ return chain;
105
+ }
106
+ return undefined;
107
+ };
108
+ const requireChanges = function* (search, config) {
109
+ const targets = reachable(search).filter((target) => !target.test);
110
+ for (const site of sitesOf(config, search.model, search.from))
111
+ for (const module of targets) {
112
+ const chain = chainTo(config, site.holder, module);
113
+ const changes = chain && requirement(config, search.model, site, chain, module);
114
+ if (changes)
115
+ yield changes;
116
+ }
117
+ };
118
+ const requirement = (config, model, site, chain, module) => {
119
+ const name = module.name;
120
+ if (config.archetypes[chain[0]].requires.includes(name))
121
+ return undefined;
122
+ const passing = new Set(chain);
123
+ let required = config;
124
+ const fixes = [];
125
+ for (const archetype of chain.filter((at) => !config.archetypes[at].requires.includes(name))) {
126
+ const instances = instancesOf(config, model, archetype);
127
+ const bindings = instances.map((at) => bindingAt(config, passing, at, name));
128
+ if (bindings.includes(undefined))
129
+ return undefined;
130
+ required = withArchetype(required, archetype, (data) => ({ ...data, requires: [...data.requires, name] }));
131
+ for (const [index, at] of instances.entries())
132
+ required = withModule(required, at.parent.archetype, at.name, (declared) => {
133
+ const dir = declared;
134
+ return { ...dir, inputs: { ...dir.inputs, [name]: bindings[index] } };
135
+ });
136
+ const sites = [...new Set(instances.map((at) => `${at.parent.archetype}.${at.name}`))];
137
+ fixes.push({ fix: { kind: 'require', archetype, name, sites, module: name }, config: required });
138
+ }
139
+ const ref = { kind: 'required', name };
140
+ return [...fixes, { fix: { kind: 'using', site: site.label, ref }, config: site.add(required, ref) }];
141
+ };
142
+ const relative = (module, file) => module.dir === '' ? file : file.slice(module.dir.length + 1);
143
+ const exposeChange = (search, config, hiddenBy) => {
144
+ if (hiddenBy.kind !== 'dir')
145
+ return exposeFilesModule(search, config, hiddenBy);
146
+ if (search.model.isTest(search.to) && !hiddenBy.test)
147
+ return testExposeChange(search, config, hiddenBy);
148
+ const archetype = hiddenBy.archetype;
149
+ const exposes = config.archetypes[archetype].exposes;
150
+ const child = targetLineage(search).find((module) => module.parent === hiddenBy);
151
+ if (exposes?.kind === 'surfaces') {
152
+ if (!child)
153
+ return undefined;
154
+ const ref = { kind: 'module', module: child.name };
155
+ const surfaces = { ...exposes.surfaces, [child.name]: [...(exposes.surfaces[child.name] ?? []), ref] };
156
+ return {
157
+ fix: { kind: 'expose', archetype, ref },
158
+ config: withArchetype(config, archetype, (data) => ({ ...data, exposes: { kind: 'surfaces', surfaces } })),
159
+ };
160
+ }
161
+ if (exposes?.kind === 'all' || exposes?.entry)
162
+ return undefined;
163
+ const pattern = relative(hiddenBy, search.to);
164
+ const opened = { kind: 'files', patterns: [...(exposes?.patterns ?? []), pattern] };
165
+ return {
166
+ fix: { kind: 'expose-file', archetype, pattern },
167
+ config: withArchetype(config, archetype, (data) => ({ ...data, exposes: opened })),
168
+ };
169
+ };
170
+ const testExposeChange = (search, config, hiddenBy) => {
171
+ const child = targetLineage(search).find((module) => module.parent === hiddenBy);
172
+ if (!child)
173
+ return undefined;
174
+ const archetype = hiddenBy.archetype;
175
+ const ref = { kind: 'module', module: child.name };
176
+ const surfaces = config.archetypes[archetype].testExposes ?? {};
177
+ const testExposes = { ...surfaces, [child.name]: [...(surfaces[child.name] ?? []), ref] };
178
+ return {
179
+ fix: { kind: 'test-expose', archetype, ref },
180
+ config: withArchetype(config, archetype, (data) => ({ ...data, testExposes })),
181
+ };
182
+ };
183
+ const exposeFilesModule = (search, config, hiddenBy) => {
184
+ const archetype = hiddenBy.parent.archetype;
185
+ const declared = config.archetypes[archetype].modules.find((data) => data.name === hiddenBy.name);
186
+ if (declared?.kind === 'dir')
187
+ return undefined;
188
+ const exposes = declared.exposes;
189
+ if (exposes?.kind === 'all')
190
+ return undefined;
191
+ const pattern = relative(hiddenBy, search.to);
192
+ return {
193
+ fix: { kind: 'expose-file', archetype: `${archetype}.${hiddenBy.name}`, pattern },
194
+ config: withModule(config, archetype, hiddenBy.name, (data) => ({
195
+ ...data,
196
+ exposes: { kind: 'files', patterns: [...(exposes?.patterns ?? []), pattern] },
197
+ })),
198
+ };
199
+ };
200
+ const subtree = (module) => [module, ...module.children.flatMap(subtree)];
201
+ const uses = (config, model, from, under) => {
202
+ const judge = verdicts(config, model);
203
+ const reached = subtree(from).flatMap((module) => module.files.slice(0, 1).flatMap((file) => (judge.reach(file) ?? []).flatMap((grant) => [...grant.files])));
204
+ const found = new Set();
205
+ for (const to of reached) {
206
+ const target = model.moduleOf(to);
207
+ if (target && lineage(target).includes(under))
208
+ found.add(topOf(target, under));
209
+ }
210
+ found.delete(from);
211
+ return found;
212
+ };
213
+ const reaches = (config, model, from, to, under) => {
214
+ const seen = new Set([from]);
215
+ for (const at of seen) {
216
+ if (at === to)
217
+ return true;
218
+ for (const next of uses(config, model, at, under))
219
+ seen.add(next);
220
+ }
221
+ return false;
222
+ };
223
+ const topOf = (module, under) => {
224
+ let at = module;
225
+ while (at.parent && at.parent !== under)
226
+ at = at.parent;
227
+ return at;
228
+ };
229
+ const loops = ({ model, from, to }, config) => {
230
+ if (model.isTest(from))
231
+ return false;
232
+ const source = model.moduleOf(from);
233
+ const target = model.moduleOf(to);
234
+ const common = [...lineage(source)].find((module) => lineage(target).includes(module));
235
+ if (!common || common === source || common === target)
236
+ return false;
237
+ return reaches(config, model, topOf(target, common), topOf(source, common), common);
238
+ };
239
+ const search = (state, config, fixes) => {
240
+ const verdict = verdicts(config, state.model).verdict(state.from, state.to);
241
+ if (verdict.allowed)
242
+ return fixes;
243
+ if (verdict.reason === 'private') {
244
+ const change = exposeChange(state, config, verdict.hiddenBy);
245
+ return change && search(state, change.config, [...fixes, change.fix]);
246
+ }
247
+ for (const candidates of [usingChanges, requireChanges])
248
+ for (const changes of candidates(state, config)) {
249
+ const changed = changes.at(-1).config;
250
+ if (allowed(state, changed) && !loops(state, changed))
251
+ return [...fixes, ...changes.map((change) => change.fix)];
252
+ }
253
+ const hidden = hiddenOnTheWay(state, config);
254
+ const change = hidden && exposeChange(state, config, hidden);
255
+ return change && search(state, change.config, [...fixes, change.fix]);
256
+ };
257
+ const hiddenOnTheWay = ({ model, from, to }, config) => {
258
+ const judge = verdicts(config, model);
259
+ const above = lineage(model.moduleOf(from));
260
+ const view = model.isTest(from) ? 'test' : 'production';
261
+ return lineage(model.moduleOf(to))
262
+ .filter((module) => !above.includes(module))
263
+ .find((module) => !judge.surface(module, undefined, view).has(to));
264
+ };
265
+ export const fixesFor = (config, model, from, to) => search({ model, from, to }, config, []);
@@ -0,0 +1,16 @@
1
+ import type { ArchetypeData, RefData } from './data.ts';
2
+ import { type Fail } from './shared.ts';
3
+ export interface PendingRef {
4
+ ref: RefData;
5
+ where: string;
6
+ archetype: string;
7
+ tests: boolean;
8
+ surface?: true;
9
+ }
10
+ export interface Structure {
11
+ root: string;
12
+ archetypes: Record<string, ArchetypeData>;
13
+ pending: PendingRef[];
14
+ }
15
+ export declare const rootName = "root";
16
+ export declare const parseStructure: (root: unknown, archetypes: unknown, fail: Fail) => Structure;