@yearofthedan/warp 0.3.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.
- package/README.md +20 -22
- package/dist/app/check.d.ts +3 -1
- package/dist/app/check.js +3 -2
- package/dist/app/configLines.d.ts +5 -0
- package/dist/app/configLines.js +24 -0
- package/dist/app/load.d.ts +2 -2
- package/dist/app/load.js +31 -43
- package/dist/builtins/advice.js +9 -9
- package/dist/builtins/dependency-direction/dependencyDirection.js +1 -1
- package/dist/builtins/module-externals/moduleExternals.js +3 -2
- package/dist/builtins/unused-declaration/empty.js +11 -10
- package/dist/builtins/unused-declaration/grants.js +17 -7
- package/dist/builtins/unused-declaration/surfaces.js +16 -6
- package/dist/cli.js +14 -11
- package/dist/config/loadConfig.d.ts +7 -1
- package/dist/config/loadConfig.js +83 -12
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/model/archetype/data.d.ts +13 -23
- package/dist/model/archetype/data.js +1 -1
- package/dist/model/archetype/fixes.d.ts +5 -7
- package/dist/model/archetype/fixes.js +121 -90
- package/dist/model/archetype/language.d.ts +16 -0
- package/dist/model/archetype/language.js +419 -0
- package/dist/model/archetype/reach.d.ts +8 -3
- package/dist/model/archetype/reach.js +122 -56
- package/dist/model/archetype/references.d.ts +11 -5
- package/dist/model/archetype/references.js +59 -118
- package/dist/model/archetype/requirements.d.ts +3 -3
- package/dist/model/archetype/requirements.js +65 -58
- package/dist/model/archetype/resolve.d.ts +1 -0
- package/dist/model/archetype/resolve.js +23 -54
- package/dist/model/archetype/rulesets.d.ts +2 -1
- package/dist/model/archetype/rulesets.js +72 -42
- package/dist/reporters/model.js +4 -3
- package/dist/rules/registry.js +1 -1
- package/package.json +3 -2
- package/dist/model/archetype/builder.d.ts +0 -183
- package/dist/model/archetype/builder.js +0 -78
- package/dist/model/archetype/draft.d.ts +0 -30
- package/dist/model/archetype/draft.js +0 -264
|
@@ -16,22 +16,80 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
16
16
|
return path;
|
|
17
17
|
};
|
|
18
18
|
import { createHash } from 'node:crypto';
|
|
19
|
-
import { existsSync } from 'node:fs';
|
|
20
|
-
import { registerHooks } from 'node:module';
|
|
21
|
-
import { join } from 'node:path';
|
|
19
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
20
|
+
import { createRequire, registerHooks } from 'node:module';
|
|
21
|
+
import { dirname, join, resolve } from 'node:path';
|
|
22
22
|
import { pathToFileURL } from 'node:url';
|
|
23
|
+
import { isAlias, isMap, isNode, isPair, isScalar, parseDocument, visit } from 'yaml';
|
|
23
24
|
import { ConfigError } from '../model/configError.js';
|
|
24
|
-
export const
|
|
25
|
+
export const configName = 'warp.yaml';
|
|
25
26
|
export const findConfig = (dir) => {
|
|
26
|
-
const found =
|
|
27
|
-
if (!found)
|
|
28
|
-
throw new ConfigError(`no config in ${dir}: add
|
|
27
|
+
const found = join(dir, configName);
|
|
28
|
+
if (!existsSync(found))
|
|
29
|
+
throw new ConfigError(`no config in ${dir}: add a ${configName}`);
|
|
29
30
|
return found;
|
|
30
31
|
};
|
|
32
|
+
export const parseConfig = (text, file) => {
|
|
33
|
+
const doc = parseDocument(text, { version: '1.2', merge: false, uniqueKeys: true });
|
|
34
|
+
const lineOf = (offset) => text.slice(0, offset).split('\n').length;
|
|
35
|
+
const problems = [
|
|
36
|
+
...doc.errors.map((error) => `${file}:${lineOf(error.pos[0])}: ${firstLine(error.message)}`),
|
|
37
|
+
...anchors(doc).map((offset) => `${file}:${lineOf(offset)}: anchors and aliases are not part of warp.yaml; write it out`),
|
|
38
|
+
];
|
|
39
|
+
if (problems.length > 0)
|
|
40
|
+
throw new ConfigError(problems.join('\n'));
|
|
41
|
+
const lines = new Map();
|
|
42
|
+
const walk = (node, at) => {
|
|
43
|
+
if (!isMap(node))
|
|
44
|
+
return;
|
|
45
|
+
for (const pair of node.items.filter(isPair)) {
|
|
46
|
+
if (!isScalar(pair.key))
|
|
47
|
+
continue;
|
|
48
|
+
const place = at(String(pair.key.value));
|
|
49
|
+
if (place.path !== undefined)
|
|
50
|
+
lines.set(place.path, lineOf(pair.key.range?.[0] ?? 0));
|
|
51
|
+
walk(pair.value, place.inside);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
walk(doc.contents, top);
|
|
55
|
+
return { raw: doc.toJS() ?? {}, lines };
|
|
56
|
+
};
|
|
57
|
+
const firstLine = (message) => message.split('\n')[0].replace(/ at line \d+, column \d+:?$/, '');
|
|
58
|
+
const anchors = (doc) => {
|
|
59
|
+
const offsets = [];
|
|
60
|
+
visit(doc, {
|
|
61
|
+
Node: (_, node) => {
|
|
62
|
+
if (isAlias(node) || (isNode(node) && node.anchor))
|
|
63
|
+
offsets.push(node.range?.[0] ?? 0);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
return offsets;
|
|
67
|
+
};
|
|
68
|
+
const plain = (path) => (key) => ({ path: `${path}.${key}`, inside: plain(`${path}.${key}`) });
|
|
69
|
+
const moduleAt = (path) => ({ path, inside: moduleKey(path) });
|
|
70
|
+
const moduleKey = (path) => (key) => {
|
|
71
|
+
if (key === 'modules')
|
|
72
|
+
return { inside: (child) => moduleAt(`${path}.${child}`) };
|
|
73
|
+
if (key === 'test')
|
|
74
|
+
return { path: `${path}.test`, inside: testKey(path) };
|
|
75
|
+
return plain(path)(key);
|
|
76
|
+
};
|
|
77
|
+
const testKey = (path) => (key) => key === 'modules' ? { inside: (child) => moduleAt(`${path}.${child}`) } : plain(`${path}.test`)(key);
|
|
78
|
+
const top = (key) => {
|
|
79
|
+
if (key === 'root')
|
|
80
|
+
return moduleAt('root');
|
|
81
|
+
if (key === 'archetypes')
|
|
82
|
+
return { inside: moduleAt };
|
|
83
|
+
return { path: key, inside: plain(key) };
|
|
84
|
+
};
|
|
31
85
|
const entry = new URL(import.meta.url.endsWith('.ts') ? '../index.ts' : '../index.js', import.meta.url).href;
|
|
32
86
|
const ownFiles = new URL('../../', import.meta.url).href;
|
|
33
87
|
export const loadConfig = async (file) => {
|
|
34
|
-
const
|
|
88
|
+
const text = readFileSync(file, 'utf8');
|
|
89
|
+
const digest = createHash('sha256').update(text);
|
|
90
|
+
const { raw, lines } = parseConfig(text, file);
|
|
91
|
+
const declared = raw.plugins;
|
|
92
|
+
const specs = declared && typeof declared === 'object' && !Array.isArray(declared) ? Object.entries(declared) : [];
|
|
35
93
|
const hooks = registerHooks({
|
|
36
94
|
resolve: (specifier, context, nextResolve) => specifier === '@yearofthedan/warp' ? { url: entry, shortCircuit: true } : nextResolve(specifier, context),
|
|
37
95
|
load: (url, context, nextLoad) => {
|
|
@@ -42,12 +100,25 @@ export const loadConfig = async (file) => {
|
|
|
42
100
|
},
|
|
43
101
|
});
|
|
44
102
|
try {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
103
|
+
const plugins = {};
|
|
104
|
+
for (const [namespace, spec] of specs) {
|
|
105
|
+
if (typeof spec !== 'string')
|
|
106
|
+
continue;
|
|
107
|
+
const module = await import(__rewriteRelativeImportExtension(pathToFileURL(pluginPath(file, spec)).href));
|
|
108
|
+
if (module.default === undefined)
|
|
109
|
+
throw new ConfigError(`${file}:${lines.get(`plugins.${namespace}`) ?? 1}: plugins.${namespace}: ${spec} has no default export; export the plugin as default`);
|
|
110
|
+
plugins[namespace] = module.default;
|
|
111
|
+
}
|
|
112
|
+
return { raw, plugins, lines, digest: digest.digest('hex') };
|
|
49
113
|
}
|
|
50
114
|
finally {
|
|
51
115
|
hooks.deregister();
|
|
52
116
|
}
|
|
53
117
|
};
|
|
118
|
+
const pluginPath = (file, spec) => {
|
|
119
|
+
if (spec.startsWith('npm:'))
|
|
120
|
+
return createRequire(file).resolve(spec.slice(4));
|
|
121
|
+
if (spec.startsWith('./') || spec.startsWith('../'))
|
|
122
|
+
return resolve(dirname(file), spec);
|
|
123
|
+
throw new ConfigError(`${file}: plugin '${spec}': name a path, ./rules/x.ts, or a package, npm:x`);
|
|
124
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { all, archetype, contains, defineConfig, dir, exposes, files, mirror, pkg, ruleset, using, type ModuleHandle, type Selector, type Ruleset, type RuleSettings, type WarpConfig, type All, type Archetype, type Exposed, type Using, type TestUsing, type UsingContext, type Package, type Reference, type Shape, } from './model/archetype/builder.ts';
|
|
2
1
|
export { loadConfigData, type LoadedConfigData } from './app/load.ts';
|
|
3
2
|
export type * from './model/archetype/data.ts';
|
|
4
3
|
export type { Complexity, FunctionFacts, ImportEdge, FileFacts, Member, MemberGraph, MemberKind, } from './model/facts.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { all, archetype, contains, defineConfig, dir, exposes, files, mirror, pkg, ruleset, using, } from './model/archetype/builder.js';
|
|
2
1
|
export { loadConfigData } from './app/load.js';
|
|
3
2
|
export { ConfigError } from './model/configError.js';
|
|
4
3
|
export { wholeNumberOptions, } from './rules/rule.js';
|
|
@@ -2,6 +2,7 @@ export interface ModuleRefData {
|
|
|
2
2
|
kind: 'module';
|
|
3
3
|
module: string;
|
|
4
4
|
surface?: string;
|
|
5
|
+
up?: number;
|
|
5
6
|
}
|
|
6
7
|
export interface RequiredRefData {
|
|
7
8
|
kind: 'required';
|
|
@@ -24,14 +25,6 @@ export type ExposesData = {
|
|
|
24
25
|
kind: 'surfaces';
|
|
25
26
|
surfaces: Record<string, RefData[]>;
|
|
26
27
|
};
|
|
27
|
-
export type LocatorData = {
|
|
28
|
-
kind: 'dir' | 'files';
|
|
29
|
-
patterns: string[];
|
|
30
|
-
} | {
|
|
31
|
-
kind: 'mirror';
|
|
32
|
-
from: string;
|
|
33
|
-
to: string;
|
|
34
|
-
};
|
|
35
28
|
export interface FilesModuleData {
|
|
36
29
|
kind: 'files';
|
|
37
30
|
name: string;
|
|
@@ -48,14 +41,18 @@ export interface DirModuleData {
|
|
|
48
41
|
name: string;
|
|
49
42
|
paths: string[];
|
|
50
43
|
archetype: string;
|
|
44
|
+
test?: true;
|
|
51
45
|
using: RefData[];
|
|
46
|
+
inputs?: Record<string, RefData>;
|
|
47
|
+
testInputs?: Record<string, RefData>;
|
|
52
48
|
rules?: RuleSettingsData;
|
|
53
49
|
description?: string;
|
|
54
50
|
}
|
|
55
51
|
export interface TestModuleData {
|
|
56
52
|
kind: 'test';
|
|
57
53
|
name: string;
|
|
58
|
-
|
|
54
|
+
patterns: string[];
|
|
55
|
+
of?: string;
|
|
59
56
|
using: RefData[];
|
|
60
57
|
exposes?: Exclude<ExposesData, {
|
|
61
58
|
kind: 'surfaces';
|
|
@@ -64,28 +61,21 @@ export interface TestModuleData {
|
|
|
64
61
|
description?: string;
|
|
65
62
|
}
|
|
66
63
|
export type ModuleData = FilesModuleData | DirModuleData | TestModuleData;
|
|
67
|
-
export
|
|
68
|
-
|
|
69
|
-
archetype: string;
|
|
70
|
-
} | {
|
|
71
|
-
kind: 'dir';
|
|
72
|
-
} | {
|
|
73
|
-
kind: 'files';
|
|
74
|
-
};
|
|
75
|
-
export type IdentifyData = {
|
|
64
|
+
export declare const isTestData: (module: ModuleData) => boolean;
|
|
65
|
+
export interface IdentifyData {
|
|
76
66
|
marker: string;
|
|
77
|
-
}
|
|
78
|
-
match: (dir: string, chain: readonly unknown[]) => false | Record<string, string>;
|
|
79
|
-
};
|
|
67
|
+
}
|
|
80
68
|
export interface ArchetypeData {
|
|
81
69
|
name: string;
|
|
82
70
|
inline?: true;
|
|
71
|
+
test?: true;
|
|
83
72
|
description?: string;
|
|
84
73
|
identify?: IdentifyData;
|
|
85
|
-
requires:
|
|
74
|
+
requires: string[];
|
|
75
|
+
testRequires?: string[];
|
|
86
76
|
modules: ModuleData[];
|
|
87
77
|
exposes?: ExposesData;
|
|
88
|
-
|
|
78
|
+
testExposes?: Record<string, RefData[]>;
|
|
89
79
|
testUsing?: RefData[];
|
|
90
80
|
rules?: RuleSettingsData;
|
|
91
81
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const isTestData = (module) => module.kind === 'test' || (module.kind === 'dir' && module.test === true);
|
|
@@ -4,7 +4,6 @@ export interface Site {
|
|
|
4
4
|
label: string;
|
|
5
5
|
holder: ModuleView;
|
|
6
6
|
add(config: ConfigData, ref: RefData): ConfigData;
|
|
7
|
-
visible: ReadonlySet<string>;
|
|
8
7
|
self?: string;
|
|
9
8
|
tests: boolean;
|
|
10
9
|
}
|
|
@@ -12,21 +11,20 @@ export type Fix = {
|
|
|
12
11
|
kind: 'using';
|
|
13
12
|
site: string;
|
|
14
13
|
ref: RefData;
|
|
15
|
-
} | {
|
|
16
|
-
kind: 'declare';
|
|
17
|
-
archetype: string;
|
|
18
|
-
module: string;
|
|
19
|
-
above: string;
|
|
20
14
|
} | {
|
|
21
15
|
kind: 'require';
|
|
22
16
|
archetype: string;
|
|
23
17
|
name: string;
|
|
24
|
-
|
|
18
|
+
sites: string[];
|
|
25
19
|
module: string;
|
|
26
20
|
} | {
|
|
27
21
|
kind: 'expose';
|
|
28
22
|
archetype: string;
|
|
29
23
|
ref: RefData;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'test-expose';
|
|
26
|
+
archetype: string;
|
|
27
|
+
ref: RefData;
|
|
30
28
|
} | {
|
|
31
29
|
kind: 'expose-file';
|
|
32
30
|
archetype: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { verdicts } from './reach.js';
|
|
1
|
+
import { instanceOf, verdicts } from './reach.js';
|
|
2
2
|
const ancestors = function* (module) {
|
|
3
3
|
for (let at = module.parent; at; at = at.parent)
|
|
4
4
|
yield at;
|
|
@@ -16,24 +16,20 @@ const withModule = (config, archetype, name, change) => withArchetype(config, ar
|
|
|
16
16
|
const declarationSite = (config, module) => {
|
|
17
17
|
const holder = module.parent;
|
|
18
18
|
const archetype = holder.archetype;
|
|
19
|
-
const declared = config.archetypes[archetype].modules;
|
|
20
|
-
const index = declared.findIndex((data) => data.name === module.name);
|
|
21
19
|
return {
|
|
22
|
-
label:
|
|
20
|
+
label: `${archetype}.${module.name}.dependencies`,
|
|
23
21
|
holder,
|
|
24
22
|
add: (from, ref) => withModule(from, archetype, module.name, (data) => ({ ...data, using: [...data.using, ref] })),
|
|
25
|
-
visible: new Set(declared.slice(0, index).map((data) => data.name)),
|
|
26
23
|
self: module.name,
|
|
27
|
-
tests: module.
|
|
24
|
+
tests: module.test === true,
|
|
28
25
|
};
|
|
29
26
|
};
|
|
30
|
-
const kitSite = (
|
|
27
|
+
const kitSite = (at) => {
|
|
31
28
|
const archetype = at.archetype;
|
|
32
29
|
return {
|
|
33
|
-
label:
|
|
30
|
+
label: `${archetype}.test.provides`,
|
|
34
31
|
holder: at,
|
|
35
32
|
add: (from, ref) => withArchetype(from, archetype, (data) => ({ ...data, testUsing: [...(data.testUsing ?? []), ref] })),
|
|
36
|
-
visible: new Set(config.archetypes[archetype].modules.map((data) => data.name)),
|
|
37
33
|
tests: true,
|
|
38
34
|
};
|
|
39
35
|
};
|
|
@@ -41,23 +37,35 @@ export const sitesOf = (config, model, file) => {
|
|
|
41
37
|
const module = model.moduleOf(file);
|
|
42
38
|
if (!module || module.looseFiles.includes(file))
|
|
43
39
|
return [];
|
|
44
|
-
if (module.
|
|
45
|
-
return
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
52
|
const name = module.name;
|
|
53
|
+
const out = up > 0 ? { up } : {};
|
|
53
54
|
return [
|
|
54
|
-
...surfaces.map((surface) => ({ kind: 'module', module: name, surface })),
|
|
55
|
-
{ kind: 'module', module: name },
|
|
55
|
+
...surfaces.map((surface) => ({ kind: 'module', module: name, surface, ...out })),
|
|
56
|
+
{ kind: 'module', module: name, ...out },
|
|
56
57
|
];
|
|
57
58
|
};
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
|
|
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 }));
|
|
61
69
|
};
|
|
62
70
|
const allowed = ({ model, from, to }, config) => verdicts(config, model).verdict(from, to).allowed;
|
|
63
71
|
const targetLineage = ({ model, to }) => lineage(model.moduleOf(to));
|
|
@@ -66,82 +74,77 @@ const reachable = (search) => {
|
|
|
66
74
|
const importer = lineage(search.model.moduleOf(search.from));
|
|
67
75
|
return targetLineage(search).filter((module) => !importer.some((own) => sameDeclaration(own, module)));
|
|
68
76
|
};
|
|
69
|
-
const nameable = (site, module) => module.parent === site.holder && module.name
|
|
77
|
+
const nameable = (site, module) => !(module.parent === site.holder && module.name === site.self) && (site.tests || !module.test);
|
|
70
78
|
const usingChanges = function* (search, config) {
|
|
71
79
|
const targets = reachable(search);
|
|
72
80
|
for (const site of sitesOf(config, search.model, search.from)) {
|
|
73
|
-
const own =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
for (const ref of [...own
|
|
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)])
|
|
77
85
|
yield [{ fix: { kind: 'using', site: site.label, ref }, config: site.add(config, ref) }];
|
|
78
86
|
}
|
|
79
87
|
};
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (!declared)
|
|
86
|
-
continue;
|
|
87
|
-
for (const ref of moduleRefs(declared.config, module))
|
|
88
|
-
yield [declared, { fix: { kind: 'using', site: site.label, ref }, config: site.add(declared.config, ref) }];
|
|
89
|
-
}
|
|
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;
|
|
90
93
|
};
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
config: withArchetype(config, archetype, (data) => ({
|
|
105
|
-
...data,
|
|
106
|
-
modules: [...rest.slice(0, at), moved, ...rest.slice(at)],
|
|
107
|
-
})),
|
|
108
|
-
};
|
|
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;
|
|
109
107
|
};
|
|
110
|
-
const providedEverywhere = (config, model, archetype, name) => model.modules
|
|
111
|
-
.filter((module) => module.archetype !== undefined && namedArchetype(config, module.archetype) === archetype)
|
|
112
|
-
.every((module) => [...ancestors(module)].some((at) => config.archetypes[at.archetype].provides[name]));
|
|
113
108
|
const requireChanges = function* (search, config) {
|
|
114
|
-
const targets = reachable(search);
|
|
109
|
+
const targets = reachable(search).filter((target) => !target.test);
|
|
115
110
|
for (const site of sitesOf(config, search.model, search.from))
|
|
116
|
-
for (const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
};
|
|
123
|
-
const requirement = (
|
|
124
|
-
const archetype = namedArchetype(config, site.holder.archetype);
|
|
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) => {
|
|
125
119
|
const name = module.name;
|
|
126
|
-
|
|
127
|
-
if (config.archetypes[archetype].requires[name] || (providing && providing.module !== name))
|
|
128
|
-
return undefined;
|
|
129
|
-
const required = withArchetype(withArchetype(config, archetype, (data) => ({
|
|
130
|
-
...data,
|
|
131
|
-
requires: { ...data.requires, [name]: { kind: module.kind === 'dir' ? 'dir' : 'files' } },
|
|
132
|
-
})), provider.archetype, (data) => ({ ...data, provides: { ...data.provides, [name]: { kind: 'module', module: name } } }));
|
|
133
|
-
if (!providedEverywhere(required, search.model, archetype, name))
|
|
120
|
+
if (config.archetypes[chain[0]].requires.includes(name))
|
|
134
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
|
+
}
|
|
135
139
|
const ref = { kind: 'required', name };
|
|
136
|
-
return [
|
|
137
|
-
{ fix: { kind: 'require', archetype, name, provider: provider.archetype, module: name }, config: required },
|
|
138
|
-
{ fix: { kind: 'using', site: site.label, ref }, config: site.add(required, ref) },
|
|
139
|
-
];
|
|
140
|
+
return [...fixes, { fix: { kind: 'using', site: site.label, ref }, config: site.add(required, ref) }];
|
|
140
141
|
};
|
|
141
142
|
const relative = (module, file) => module.dir === '' ? file : file.slice(module.dir.length + 1);
|
|
142
143
|
const exposeChange = (search, config, hiddenBy) => {
|
|
143
144
|
if (hiddenBy.kind !== 'dir')
|
|
144
145
|
return exposeFilesModule(search, config, hiddenBy);
|
|
146
|
+
if (search.model.isTest(search.to) && !hiddenBy.test)
|
|
147
|
+
return testExposeChange(search, config, hiddenBy);
|
|
145
148
|
const archetype = hiddenBy.archetype;
|
|
146
149
|
const exposes = config.archetypes[archetype].exposes;
|
|
147
150
|
const child = targetLineage(search).find((module) => module.parent === hiddenBy);
|
|
@@ -155,7 +158,7 @@ const exposeChange = (search, config, hiddenBy) => {
|
|
|
155
158
|
config: withArchetype(config, archetype, (data) => ({ ...data, exposes: { kind: 'surfaces', surfaces } })),
|
|
156
159
|
};
|
|
157
160
|
}
|
|
158
|
-
if (exposes?.kind === 'all' || exposes?.entry
|
|
161
|
+
if (exposes?.kind === 'all' || exposes?.entry)
|
|
159
162
|
return undefined;
|
|
160
163
|
const pattern = relative(hiddenBy, search.to);
|
|
161
164
|
const opened = { kind: 'files', patterns: [...(exposes?.patterns ?? []), pattern] };
|
|
@@ -164,6 +167,19 @@ const exposeChange = (search, config, hiddenBy) => {
|
|
|
164
167
|
config: withArchetype(config, archetype, (data) => ({ ...data, exposes: opened })),
|
|
165
168
|
};
|
|
166
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
|
+
};
|
|
167
183
|
const exposeFilesModule = (search, config, hiddenBy) => {
|
|
168
184
|
const archetype = hiddenBy.parent.archetype;
|
|
169
185
|
const declared = config.archetypes[archetype].modules.find((data) => data.name === hiddenBy.name);
|
|
@@ -182,13 +198,27 @@ const exposeFilesModule = (search, config, hiddenBy) => {
|
|
|
182
198
|
};
|
|
183
199
|
};
|
|
184
200
|
const subtree = (module) => [module, ...module.children.flatMap(subtree)];
|
|
185
|
-
const
|
|
201
|
+
const uses = (config, model, from, under) => {
|
|
186
202
|
const judge = verdicts(config, model);
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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;
|
|
192
222
|
};
|
|
193
223
|
const topOf = (module, under) => {
|
|
194
224
|
let at = module;
|
|
@@ -204,7 +234,7 @@ const loops = ({ model, from, to }, config) => {
|
|
|
204
234
|
const common = [...lineage(source)].find((module) => lineage(target).includes(module));
|
|
205
235
|
if (!common || common === source || common === target)
|
|
206
236
|
return false;
|
|
207
|
-
return reaches(config, model, topOf(target, common), topOf(source, common));
|
|
237
|
+
return reaches(config, model, topOf(target, common), topOf(source, common), common);
|
|
208
238
|
};
|
|
209
239
|
const search = (state, config, fixes) => {
|
|
210
240
|
const verdict = verdicts(config, state.model).verdict(state.from, state.to);
|
|
@@ -214,7 +244,7 @@ const search = (state, config, fixes) => {
|
|
|
214
244
|
const change = exposeChange(state, config, verdict.hiddenBy);
|
|
215
245
|
return change && search(state, change.config, [...fixes, change.fix]);
|
|
216
246
|
}
|
|
217
|
-
for (const candidates of [usingChanges,
|
|
247
|
+
for (const candidates of [usingChanges, requireChanges])
|
|
218
248
|
for (const changes of candidates(state, config)) {
|
|
219
249
|
const changed = changes.at(-1).config;
|
|
220
250
|
if (allowed(state, changed) && !loops(state, changed))
|
|
@@ -227,8 +257,9 @@ const search = (state, config, fixes) => {
|
|
|
227
257
|
const hiddenOnTheWay = ({ model, from, to }, config) => {
|
|
228
258
|
const judge = verdicts(config, model);
|
|
229
259
|
const above = lineage(model.moduleOf(from));
|
|
260
|
+
const view = model.isTest(from) ? 'test' : 'production';
|
|
230
261
|
return lineage(model.moduleOf(to))
|
|
231
262
|
.filter((module) => !above.includes(module))
|
|
232
|
-
.find((module) => !judge.surface(module).has(to));
|
|
263
|
+
.find((module) => !judge.surface(module, undefined, view).has(to));
|
|
233
264
|
};
|
|
234
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;
|