@yearofthedan/warp 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/load.js +2 -2
- package/dist/app/unclassified.js +1 -1
- package/dist/builtins/advice.js +1 -1
- package/dist/builtins/dependency-direction/dependencyDirection.js +6 -1
- package/dist/builtins/unused-declaration/grants.js +25 -7
- package/dist/cli.js +23 -6
- package/dist/config/loadConfig.d.ts +1 -1
- package/dist/config/loadConfig.js +39 -17
- package/dist/git/cache.js +5 -1
- package/dist/model/archetype/data.d.ts +1 -0
- package/dist/model/archetype/fixes.js +34 -23
- package/dist/model/archetype/language.js +18 -5
- package/dist/model/archetype/references.js +1 -1
- package/dist/model/archetype/requirements.js +11 -1
- package/dist/model/archetype/resolve.js +3 -2
- package/dist/model/archetype/rulesets.js +8 -7
- package/dist/model/archetype/settings.d.ts +1 -0
- package/dist/model/archetype/settings.js +5 -0
- package/dist/rules/registry.js +1 -1
- package/package.json +2 -1
package/dist/app/load.js
CHANGED
|
@@ -21,8 +21,8 @@ export const loadConfigData = (raw, { builtins, ruleset, file = 'warp.yaml', plu
|
|
|
21
21
|
if (!isRecord(raw))
|
|
22
22
|
throw new ConfigError(`${file}: expected a mapping, root: { modules: ... }`);
|
|
23
23
|
const unknownKeys = Object.keys(raw).filter((key) => !keys.includes(key));
|
|
24
|
-
|
|
25
|
-
fail(`${
|
|
24
|
+
for (const key of unknownKeys)
|
|
25
|
+
fail(`${key}: unknown key; a config takes ${keys.join(', ')}`);
|
|
26
26
|
checkPlugins(raw.plugins, plugins, fail);
|
|
27
27
|
const rules = [...registry([...(builtins ?? []), ...pluginRules(plugins)]).values()];
|
|
28
28
|
const structure = parseStructure(raw.root, raw.archetypes, fail);
|
package/dist/app/unclassified.js
CHANGED
|
@@ -6,7 +6,7 @@ export const unclassifiedFindings = (model, settled, docs) => model.modules.flat
|
|
|
6
6
|
file,
|
|
7
7
|
line: 1,
|
|
8
8
|
value: 1,
|
|
9
|
-
message: `${file} is in no module of ${module.id} (${module.archetype}), so nothing says what it may import. ` +
|
|
9
|
+
message: `${file} is in no module of ${module.parent ? `${module.id} (${module.archetype})` : 'the root'}, so nothing says what it may import. ` +
|
|
10
10
|
`Add a files module to ${module.archetype} that matches it, or move it into a module's folder.`,
|
|
11
11
|
...(docs ? { docs } : {}),
|
|
12
12
|
}))
|
package/dist/builtins/advice.js
CHANGED
|
@@ -17,7 +17,7 @@ const step = (model, fix, refs) => {
|
|
|
17
17
|
case 'test-expose':
|
|
18
18
|
return `add ${sentence(refs.map(refText))} to ${fix.archetype}.test.exposes${instances(model, fix.archetype)}`;
|
|
19
19
|
case 'expose-file':
|
|
20
|
-
return `add '${fix.pattern}' to ${fix.archetype}.exposes
|
|
20
|
+
return `add '${fix.pattern}' to the files of ${fix.archetype}.exposes`;
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
export const fixSteps = (model, fixes) => {
|
|
@@ -49,12 +49,17 @@ const explain = ({ config, model, verdicts }, from, to, refusal) => {
|
|
|
49
49
|
if (refusal.reason === 'test')
|
|
50
50
|
return 'a test file. Production code never imports tests: move what both need out of the test file into the module it tests.';
|
|
51
51
|
const instead = refusal.reason === 'private' ? throughSurface({ config, model, verdicts }, from, to, refusal.hiddenBy) : '';
|
|
52
|
-
|
|
52
|
+
const opened = (instead && fixesFor(config, model, from, to)) || [];
|
|
53
|
+
const exposing = opened.filter((fix) => fix.kind === 'expose' || fix.kind === 'expose-file');
|
|
54
|
+
const open = exposing.length > 0 ? ` Or, if ${to} should be public, ${sentence(fixSteps(model, exposing))}.` : '';
|
|
55
|
+
return `${why(config, model, verdicts, from, to, refusal)}. ${instead ? `${instead}${open}` : advise(config, model, verdicts, from, to)}`;
|
|
53
56
|
};
|
|
54
57
|
const why = (config, model, verdicts, from, to, refusal) => {
|
|
55
58
|
const source = model.moduleOf(from);
|
|
56
59
|
if (refusal.reason === 'private') {
|
|
57
60
|
const { hiddenBy } = refusal;
|
|
61
|
+
if (model.isTest(to))
|
|
62
|
+
return `which ${hiddenBy.id} keeps for its own tests`;
|
|
58
63
|
return `which ${hiddenBy.id} keeps private (it exposes ${exposesText(verdicts.exposes(hiddenBy))})`;
|
|
59
64
|
}
|
|
60
65
|
if (refusal.reason === 'above')
|
|
@@ -1,29 +1,41 @@
|
|
|
1
|
-
import { isTestData } from '../../model/archetype/data.js';
|
|
1
|
+
import { isTestData, } from '../../model/archetype/data.js';
|
|
2
2
|
import { show } from '../../model/archetype/references.js';
|
|
3
3
|
import { listsPackage } from '../packages.js';
|
|
4
4
|
import { childrenNamed, instancesOf, named, unused } from './declarations.js';
|
|
5
5
|
export const unusedGrants = (context) => {
|
|
6
6
|
const used = usedRefs(context);
|
|
7
|
-
return declared(context)
|
|
7
|
+
return declared(context, used)
|
|
8
8
|
.filter(({ ref, holders }) => !used.has(ref) && holders.some((holder) => context.atModule(holder).on))
|
|
9
|
-
.map(({ ref, declaredIn, layer, holders, kit }) => {
|
|
9
|
+
.map(({ ref, declaredIn, layer, holders, ending, kit }) => {
|
|
10
10
|
const using = show(ref);
|
|
11
11
|
const modules = holders.map((holder) => holder.id);
|
|
12
12
|
const names = holders.map(named).join(', ');
|
|
13
13
|
const files = kit ? `the tests of ${names}` : names;
|
|
14
|
-
return unused(context.config, `${declaredIn}->${using}`, `${declaredIn} names ${using}, but nothing in ${files} imports through it.
|
|
14
|
+
return unused(context.config, `${declaredIn}->${using}`, `${declaredIn} names ${using}, but nothing in ${files} imports through it. ${ending}`, { kind: 'grant', declaredIn, layer, using, modules });
|
|
15
15
|
});
|
|
16
16
|
};
|
|
17
|
-
const declared = (context) => Object.values(context.config.archetypes).flatMap((archetype) => {
|
|
17
|
+
const declared = (context, used) => Object.values(context.config.archetypes).flatMap((archetype) => {
|
|
18
18
|
const instances = instancesOf(context, archetype.name);
|
|
19
|
+
const unusedInputs = new Set(archetype.requires.filter((name) => !bindingsOf(context.config, archetype, name).some((ref) => used.has(ref))));
|
|
20
|
+
const requires = [...unusedInputs].map((name) => ({
|
|
21
|
+
ref: { kind: 'required', name },
|
|
22
|
+
declaredIn: `${archetype.name}.requires`,
|
|
23
|
+
layer: 'production',
|
|
24
|
+
holders: instances,
|
|
25
|
+
ending: 'Remove it, with its binding at each use.',
|
|
26
|
+
}));
|
|
19
27
|
const modules = archetype.modules.flatMap((module) => {
|
|
20
28
|
const holders = childrenNamed(context, instances, module.name);
|
|
21
29
|
const layer = isTestData(module) ? 'test' : 'production';
|
|
22
|
-
return module.using
|
|
30
|
+
return module.using
|
|
31
|
+
.filter((ref) => !fills(context.config, module, ref))
|
|
32
|
+
.filter((ref) => ref.kind !== 'required' || !unusedInputs.has(ref.name))
|
|
33
|
+
.map((ref) => ({
|
|
23
34
|
ref,
|
|
24
35
|
declaredIn: `${archetype.name}.${module.name}.dependencies`,
|
|
25
36
|
layer,
|
|
26
37
|
holders,
|
|
38
|
+
ending: 'Remove it.',
|
|
27
39
|
}));
|
|
28
40
|
});
|
|
29
41
|
const kit = (archetype.testUsing ?? []).map((ref) => ({
|
|
@@ -31,10 +43,16 @@ const declared = (context) => Object.values(context.config.archetypes).flatMap((
|
|
|
31
43
|
declaredIn: `${archetype.name}.test.provides`,
|
|
32
44
|
layer: 'test',
|
|
33
45
|
holders: instances,
|
|
46
|
+
ending: 'Remove it.',
|
|
34
47
|
kit: true,
|
|
35
48
|
}));
|
|
36
|
-
return [...modules, ...kit];
|
|
49
|
+
return [...requires, ...modules, ...kit];
|
|
37
50
|
});
|
|
51
|
+
const fills = (config, module, ref) => module.kind === 'dir' &&
|
|
52
|
+
Object.entries(module.inputs ?? {}).some(([name, bound]) => bound === ref && config.archetypes[module.archetype].requires.includes(name));
|
|
53
|
+
const bindingsOf = (config, archetype, name) => Object.values(config.archetypes).flatMap((holder) => holder.modules.flatMap((module) => module.kind === 'dir' && module.archetype === archetype.name && module.inputs?.[name]
|
|
54
|
+
? [module.inputs[name]]
|
|
55
|
+
: []));
|
|
38
56
|
const usedRefs = ({ project, verdicts }) => {
|
|
39
57
|
const used = new Set();
|
|
40
58
|
for (const file of project.files) {
|
package/dist/cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { readFileSync, realpathSync } from 'node:fs';
|
|
3
|
-
import { basename, dirname, resolve } from 'node:path';
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
4
4
|
import { parseArgs } from 'node:util';
|
|
5
5
|
import { diskFolders } from './analysis/folders.js';
|
|
6
6
|
import { typescriptAnalyzer } from './analysis/typescript.js';
|
|
@@ -13,7 +13,8 @@ import { findConfig, loadConfig } from './config/loadConfig.js';
|
|
|
13
13
|
import { toolDigest } from './config/toolDigest.js';
|
|
14
14
|
import { gitDirCache } from './git/cache.js';
|
|
15
15
|
import { gitRepository, projectPathOf } from './git/repository.js';
|
|
16
|
-
import { rulesOf } from './model/archetype/settings.js';
|
|
16
|
+
import { anyRuleOn, rulesOf } from './model/archetype/settings.js';
|
|
17
|
+
import { recommended } from './model/archetype/rulesets.js';
|
|
17
18
|
import { loadConfigData } from './app/load.js';
|
|
18
19
|
import { ConfigError } from './model/configError.js';
|
|
19
20
|
import { reporters } from './reporters/index.js';
|
|
@@ -87,6 +88,8 @@ const resolveConfig = async (options) => {
|
|
|
87
88
|
const file = basename(configFile);
|
|
88
89
|
try {
|
|
89
90
|
const { config, rules } = loadConfigData(raw, { builtins, plugins, ruleset: options.ruleset, file });
|
|
91
|
+
for (const tsconfig of config.tsconfig.filter((path) => !existsSync(join(root, path))))
|
|
92
|
+
throw new ConfigError(`tsconfig: no ${tsconfig} beside ${file}; add it, or list the project's tsconfig files under tsconfig`);
|
|
90
93
|
return { root, config, rules, lines, digest };
|
|
91
94
|
}
|
|
92
95
|
catch (error) {
|
|
@@ -102,7 +105,9 @@ const run = async (options, produce) => {
|
|
|
102
105
|
throw new Error(`no reporter called ${options.reporter}: use one of ${[...reporters.keys()].join(', ')}`);
|
|
103
106
|
try {
|
|
104
107
|
const resolved = await resolveConfig(options);
|
|
105
|
-
|
|
108
|
+
if (!anyRuleOn(resolved.config))
|
|
109
|
+
console.error(nothingOn(resolved.config.file));
|
|
110
|
+
const report = placed(resolved, () => produce(resolved.root, checkOf(resolved), resolved.digest));
|
|
106
111
|
const output = reporter.render(report, { toolVersion: version(), projectPath: projectPathOf(resolved.root) });
|
|
107
112
|
if (output)
|
|
108
113
|
console.log(output);
|
|
@@ -119,6 +124,17 @@ const run = async (options, produce) => {
|
|
|
119
124
|
return 0;
|
|
120
125
|
}
|
|
121
126
|
};
|
|
127
|
+
const placed = ({ lines, config }, run) => {
|
|
128
|
+
try {
|
|
129
|
+
return run();
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (error instanceof ConfigError)
|
|
133
|
+
throw new ConfigError(placeProblems(lines, config.file, error.message));
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const nothingOn = (file) => `No rules are on, so nothing was checked. Turn some on in ${file}: rulesets: { default: { extends: [${recommended}] } }`;
|
|
122
138
|
const failureKey = () => `${toolDigest()}-failure`;
|
|
123
139
|
const failureCache = () => {
|
|
124
140
|
try {
|
|
@@ -142,8 +158,9 @@ const forgetFailure = () => {
|
|
|
142
158
|
};
|
|
143
159
|
const printModel = async (options) => {
|
|
144
160
|
try {
|
|
145
|
-
const
|
|
146
|
-
const {
|
|
161
|
+
const resolved = await resolveConfig(options);
|
|
162
|
+
const { root, config, rules } = resolved;
|
|
163
|
+
const { model } = placed(resolved, () => modelProject(adaptersAt(root), root, config));
|
|
147
164
|
const settings = rulesOf(config, model, rules);
|
|
148
165
|
console.log(options.json ? modelJson(model, settings) : modelText(model, options.rules ? settings : undefined));
|
|
149
166
|
return 0;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const configName = "warp.yaml";
|
|
2
|
-
export declare const findConfig: (
|
|
2
|
+
export declare const findConfig: (start: string, exists?: (path: string) => boolean) => string;
|
|
3
3
|
export interface LoadedConfig {
|
|
4
4
|
raw: unknown;
|
|
5
5
|
plugins: Record<string, unknown>;
|
|
@@ -18,23 +18,29 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
18
18
|
import { createHash } from 'node:crypto';
|
|
19
19
|
import { existsSync, readFileSync } from 'node:fs';
|
|
20
20
|
import { createRequire, registerHooks } from 'node:module';
|
|
21
|
-
import { dirname, join, resolve } from 'node:path';
|
|
21
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
22
22
|
import { pathToFileURL } from 'node:url';
|
|
23
23
|
import { isAlias, isMap, isNode, isPair, isScalar, parseDocument, visit } from 'yaml';
|
|
24
24
|
import { ConfigError } from '../model/configError.js';
|
|
25
25
|
export const configName = 'warp.yaml';
|
|
26
|
-
export const findConfig = (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
export const findConfig = (start, exists = existsSync) => {
|
|
27
|
+
for (let dir = start;; dir = dirname(dir)) {
|
|
28
|
+
const found = join(dir, configName);
|
|
29
|
+
if (exists(found))
|
|
30
|
+
return found;
|
|
31
|
+
if (dirname(dir) === dir)
|
|
32
|
+
throw new ConfigError(`no ${configName} in ${start} or any folder above it`);
|
|
33
|
+
}
|
|
31
34
|
};
|
|
32
35
|
export const parseConfig = (text, file) => {
|
|
33
36
|
const doc = parseDocument(text, { version: '1.2', merge: false, uniqueKeys: true });
|
|
34
37
|
const lineOf = (offset) => text.slice(0, offset).split('\n').length;
|
|
38
|
+
const unquoted = doc.errors.filter((error) => error.code === 'BAD_ALIAS' && text[error.pos[0]] === '*');
|
|
35
39
|
const problems = [
|
|
36
|
-
...doc.errors.map((error) => `${file}:${lineOf(error.pos[0])}: ${
|
|
37
|
-
...anchors(doc)
|
|
40
|
+
...doc.errors.map((error) => `${file}:${lineOf(error.pos[0])}: ${yamlProblem(error, text, unquoted)}`),
|
|
41
|
+
...anchors(doc)
|
|
42
|
+
.filter((offset) => !unquoted.some((error) => error.pos[0] === offset))
|
|
43
|
+
.map((offset) => `${file}:${lineOf(offset)}: anchors and aliases are not part of warp.yaml; write it out`),
|
|
38
44
|
];
|
|
39
45
|
if (problems.length > 0)
|
|
40
46
|
throw new ConfigError(problems.join('\n'));
|
|
@@ -54,6 +60,13 @@ export const parseConfig = (text, file) => {
|
|
|
54
60
|
walk(doc.contents, top);
|
|
55
61
|
return { raw: doc.toJS() ?? {}, lines };
|
|
56
62
|
};
|
|
63
|
+
const yamlProblem = (error, text, unquoted) => {
|
|
64
|
+
if (unquoted.includes(error))
|
|
65
|
+
return "a value that starts with * is quoted, '*'";
|
|
66
|
+
if (error.code !== 'DUPLICATE_KEY')
|
|
67
|
+
return firstLine(error.message);
|
|
68
|
+
return `${firstLine(error.message)} (${/^['"]?([^'":\n]*)/.exec(text.slice(error.pos[0]))[1]})`;
|
|
69
|
+
};
|
|
57
70
|
const firstLine = (message) => message.split('\n')[0].replace(/ at line \d+, column \d+:?$/, '');
|
|
58
71
|
const anchors = (doc) => {
|
|
59
72
|
const offsets = [];
|
|
@@ -69,12 +82,14 @@ const plain = (path) => (key) => ({ path: `${path}.${key}`, inside: plain(`${pat
|
|
|
69
82
|
const moduleAt = (path) => ({ path, inside: moduleKey(path) });
|
|
70
83
|
const moduleKey = (path) => (key) => {
|
|
71
84
|
if (key === 'modules')
|
|
72
|
-
return { inside: (child) => moduleAt(`${path}.${child}`) };
|
|
85
|
+
return { path: `${path}.modules`, inside: (child) => moduleAt(`${path}.${child}`) };
|
|
73
86
|
if (key === 'test')
|
|
74
87
|
return { path: `${path}.test`, inside: testKey(path) };
|
|
75
88
|
return plain(path)(key);
|
|
76
89
|
};
|
|
77
|
-
const testKey = (path) => (key) => key === 'modules'
|
|
90
|
+
const testKey = (path) => (key) => key === 'modules'
|
|
91
|
+
? { path: `${path}.test.modules`, inside: (child) => moduleAt(`${path}.${child}`) }
|
|
92
|
+
: plain(`${path}.test`)(key);
|
|
78
93
|
const top = (key) => {
|
|
79
94
|
if (key === 'root')
|
|
80
95
|
return moduleAt('root');
|
|
@@ -87,7 +102,8 @@ const ownFiles = new URL('../../', import.meta.url).href;
|
|
|
87
102
|
export const loadConfig = async (file) => {
|
|
88
103
|
const text = readFileSync(file, 'utf8');
|
|
89
104
|
const digest = createHash('sha256').update(text);
|
|
90
|
-
const
|
|
105
|
+
const name = basename(file);
|
|
106
|
+
const { raw, lines } = parseConfig(text, name);
|
|
91
107
|
const declared = raw.plugins;
|
|
92
108
|
const specs = declared && typeof declared === 'object' && !Array.isArray(declared) ? Object.entries(declared) : [];
|
|
93
109
|
const hooks = registerHooks({
|
|
@@ -104,10 +120,7 @@ export const loadConfig = async (file) => {
|
|
|
104
120
|
for (const [namespace, spec] of specs) {
|
|
105
121
|
if (typeof spec !== 'string')
|
|
106
122
|
continue;
|
|
107
|
-
|
|
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;
|
|
123
|
+
plugins[namespace] = await importPlugin(file, spec, `${name}:${lines.get(`plugins.${namespace}`) ?? 1}: plugins.${namespace}`);
|
|
111
124
|
}
|
|
112
125
|
return { raw, plugins, lines, digest: digest.digest('hex') };
|
|
113
126
|
}
|
|
@@ -115,10 +128,19 @@ export const loadConfig = async (file) => {
|
|
|
115
128
|
hooks.deregister();
|
|
116
129
|
}
|
|
117
130
|
};
|
|
118
|
-
const
|
|
131
|
+
const importPlugin = async (file, spec, at) => {
|
|
132
|
+
const path = pluginPath(file, spec, at);
|
|
133
|
+
if (!spec.startsWith('npm:') && !existsSync(path))
|
|
134
|
+
throw new ConfigError(`${at}: no file at ${spec}`);
|
|
135
|
+
const module = await import(__rewriteRelativeImportExtension(pathToFileURL(path).href));
|
|
136
|
+
if (module.default === undefined)
|
|
137
|
+
throw new ConfigError(`${at}: ${spec} has no default export; export the plugin as default`);
|
|
138
|
+
return module.default;
|
|
139
|
+
};
|
|
140
|
+
const pluginPath = (file, spec, at) => {
|
|
119
141
|
if (spec.startsWith('npm:'))
|
|
120
142
|
return createRequire(file).resolve(spec.slice(4));
|
|
121
143
|
if (spec.startsWith('./') || spec.startsWith('../'))
|
|
122
144
|
return resolve(dirname(file), spec);
|
|
123
|
-
throw new ConfigError(`${
|
|
145
|
+
throw new ConfigError(`${at}: '${spec}' is a path, ./${spec}, or a package, npm:${spec}`);
|
|
124
146
|
};
|
package/dist/git/cache.js
CHANGED
|
@@ -3,7 +3,11 @@ import { mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, uti
|
|
|
3
3
|
import { join, resolve } from 'node:path';
|
|
4
4
|
export const maxAgeMs = 30 * 24 * 60 * 60 * 1000;
|
|
5
5
|
export const gitDirCache = (projectRoot, now = Date.now) => {
|
|
6
|
-
const gitDir = execFileSync('git', ['rev-parse', '--git-common-dir'], {
|
|
6
|
+
const gitDir = execFileSync('git', ['rev-parse', '--git-common-dir'], {
|
|
7
|
+
cwd: projectRoot,
|
|
8
|
+
encoding: 'utf8',
|
|
9
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
10
|
+
}).trim();
|
|
7
11
|
const dir = join(resolve(projectRoot, gitDir), 'warp-cache');
|
|
8
12
|
const file = (key) => join(dir, `${key}.json`);
|
|
9
13
|
const touch = (key) => {
|
|
@@ -47,10 +47,10 @@ const surfacesOf = ({ exposes, testExposes }, tests) => [
|
|
|
47
47
|
...(tests ? Object.keys(testExposes ?? {}) : []),
|
|
48
48
|
]),
|
|
49
49
|
];
|
|
50
|
-
const moduleRefs = (config, module, tests, up = 0) => {
|
|
50
|
+
const moduleRefs = (config, module, tests, up = 0, top = false) => {
|
|
51
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
|
+
const out = { ...(up > 0 ? { up } : {}), ...(top ? { top: true } : {}) };
|
|
54
54
|
return [
|
|
55
55
|
...surfaces.map((surface) => ({ kind: 'module', module: name, surface, ...out })),
|
|
56
56
|
{ kind: 'module', module: name, ...out },
|
|
@@ -62,6 +62,7 @@ const levels = (config, holder) => {
|
|
|
62
62
|
found.push(at.parent);
|
|
63
63
|
return found;
|
|
64
64
|
};
|
|
65
|
+
const declares = (config, level, name) => config.archetypes[level.archetype].modules.some((module) => module.name === name);
|
|
65
66
|
const inputsOf = (config, site) => {
|
|
66
67
|
const named = config.archetypes[namedArchetype(config, site.holder.archetype)];
|
|
67
68
|
const names = [...named.requires, ...(site.tests ? (named.testRequires ?? []) : [])];
|
|
@@ -78,29 +79,39 @@ const nameable = (site, module) => !(module.parent === site.holder && module.nam
|
|
|
78
79
|
const usingChanges = function* (search, config) {
|
|
79
80
|
const targets = reachable(search);
|
|
80
81
|
for (const site of sitesOf(config, search.model, search.from)) {
|
|
81
|
-
const
|
|
82
|
+
const scopes = levels(config, site.holder);
|
|
83
|
+
const own = scopes.flatMap((level, up) => targets
|
|
82
84
|
.filter((module) => module.parent === level && nameable(site, module))
|
|
83
|
-
.flatMap((module) =>
|
|
85
|
+
.flatMap((module) => {
|
|
86
|
+
const shadowed = scopes.slice(0, up).some((nearer) => declares(config, nearer, module.name));
|
|
87
|
+
if (!shadowed)
|
|
88
|
+
return moduleRefs(config, module, site.tests, up);
|
|
89
|
+
return up === scopes.length - 1 ? moduleRefs(config, module, site.tests, up, true) : [];
|
|
90
|
+
}));
|
|
84
91
|
for (const ref of [...own, ...inputsOf(config, site)])
|
|
85
92
|
yield [{ fix: { kind: 'using', site: site.label, ref }, config: site.add(config, ref) }];
|
|
86
93
|
}
|
|
87
94
|
};
|
|
88
|
-
const bindingAt = (config, chain,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
const bindingAt = (config, chain, { holder }, name) => {
|
|
96
|
+
for (let key = holder, up = 0;; up += 1) {
|
|
97
|
+
if (config.archetypes[key].modules.some((module) => module.name === name))
|
|
98
|
+
return { kind: 'module', module: name, ...(up > 0 ? { up } : {}) };
|
|
99
|
+
if (!config.archetypes[key].inline)
|
|
100
|
+
break;
|
|
101
|
+
key = key.slice(0, key.lastIndexOf('.'));
|
|
102
|
+
}
|
|
103
|
+
return chain.has(namedArchetype(config, holder)) ? { kind: 'required', name } : undefined;
|
|
93
104
|
};
|
|
94
|
-
const
|
|
105
|
+
const usesOf = (config, archetype) => Object.entries(config.archetypes).flatMap(([holder, data]) => data.modules
|
|
106
|
+
.filter((module) => module.kind === 'dir' && module.archetype === archetype)
|
|
107
|
+
.map((module) => ({ holder, name: module.name })));
|
|
95
108
|
const chainTo = (config, holder, target) => {
|
|
109
|
+
const scope = instanceOf({ config }, target.parent);
|
|
96
110
|
const chain = [];
|
|
97
111
|
for (let instance = instanceOf({ config }, holder); instance.parent; instance = instanceOf({ config }, instance.parent)) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (!chain.includes(archetype))
|
|
102
|
-
chain.push(archetype);
|
|
103
|
-
if (instance.parent === target.parent)
|
|
112
|
+
if (!chain.includes(instance.archetype))
|
|
113
|
+
chain.push(instance.archetype);
|
|
114
|
+
if (instanceOf({ config }, instance.parent) === scope)
|
|
104
115
|
return chain;
|
|
105
116
|
}
|
|
106
117
|
return undefined;
|
|
@@ -110,12 +121,12 @@ const requireChanges = function* (search, config) {
|
|
|
110
121
|
for (const site of sitesOf(config, search.model, search.from))
|
|
111
122
|
for (const module of targets) {
|
|
112
123
|
const chain = chainTo(config, site.holder, module);
|
|
113
|
-
const changes = chain && requirement(config,
|
|
124
|
+
const changes = chain && requirement(config, site, chain, module);
|
|
114
125
|
if (changes)
|
|
115
126
|
yield changes;
|
|
116
127
|
}
|
|
117
128
|
};
|
|
118
|
-
const requirement = (config,
|
|
129
|
+
const requirement = (config, site, chain, module) => {
|
|
119
130
|
const name = module.name;
|
|
120
131
|
if (config.archetypes[chain[0]].requires.includes(name))
|
|
121
132
|
return undefined;
|
|
@@ -123,17 +134,17 @@ const requirement = (config, model, site, chain, module) => {
|
|
|
123
134
|
let required = config;
|
|
124
135
|
const fixes = [];
|
|
125
136
|
for (const archetype of chain.filter((at) => !config.archetypes[at].requires.includes(name))) {
|
|
126
|
-
const
|
|
127
|
-
const bindings =
|
|
137
|
+
const uses = usesOf(config, archetype);
|
|
138
|
+
const bindings = uses.map((use) => bindingAt(config, passing, use, name));
|
|
128
139
|
if (bindings.includes(undefined))
|
|
129
140
|
return undefined;
|
|
130
141
|
required = withArchetype(required, archetype, (data) => ({ ...data, requires: [...data.requires, name] }));
|
|
131
|
-
for (const [index,
|
|
132
|
-
required = withModule(required,
|
|
142
|
+
for (const [index, use] of uses.entries())
|
|
143
|
+
required = withModule(required, use.holder, use.name, (declared) => {
|
|
133
144
|
const dir = declared;
|
|
134
145
|
return { ...dir, inputs: { ...dir.inputs, [name]: bindings[index] } };
|
|
135
146
|
});
|
|
136
|
-
const sites =
|
|
147
|
+
const sites = uses.map((use) => `${use.holder}.${use.name}`);
|
|
137
148
|
fixes.push({ fix: { kind: 'require', archetype, name, sites, module: name }, config: required });
|
|
138
149
|
}
|
|
139
150
|
const ref = { kind: 'required', name };
|
|
@@ -143,7 +143,7 @@ const productionModule = (state, scope, name, raw) => {
|
|
|
143
143
|
const where = `${scope.archetype}.${name}`;
|
|
144
144
|
const def = record(state, where, raw);
|
|
145
145
|
if (nameProblem(name))
|
|
146
|
-
state.fail(`${
|
|
146
|
+
state.fail(`${where}: a module's name has no dots, not '${name}'`);
|
|
147
147
|
checkKeys(state, where, def, moduleKeys);
|
|
148
148
|
const inner = { ...scope, own: name };
|
|
149
149
|
if (def.archetype !== undefined)
|
|
@@ -162,7 +162,7 @@ const testModule = (state, scope, name, raw) => {
|
|
|
162
162
|
const where = `${scope.archetype}.${name}`;
|
|
163
163
|
const def = record(state, where, raw);
|
|
164
164
|
if (nameProblem(name))
|
|
165
|
-
state.fail(`${
|
|
165
|
+
state.fail(`${where}: a module's name has no dots, not '${name}'`);
|
|
166
166
|
checkKeys(state, where, def, testModuleKeys);
|
|
167
167
|
const inner = { ...scope, own: name };
|
|
168
168
|
if (def.archetype !== undefined)
|
|
@@ -322,7 +322,10 @@ const resolveName = (state, scope, entry, tests, where) => {
|
|
|
322
322
|
const parts = top ? rest : [head, ...rest];
|
|
323
323
|
if (parts.length === 0 || parts.length > 2 || !parts.every(isName))
|
|
324
324
|
return fail(state, where, entry, 'a name is a module, a module and one of its surfaces, or an input');
|
|
325
|
-
const
|
|
325
|
+
const whole = parts.join('.');
|
|
326
|
+
const [module, surface] = searchedFrom(scope, top).some((level) => level.names.has(whole))
|
|
327
|
+
? [whole, undefined]
|
|
328
|
+
: parts;
|
|
326
329
|
const name = { module, ...(surface === undefined ? {} : { surface }), top, tests, where, entry };
|
|
327
330
|
const named = moduleRef(state, scope, name);
|
|
328
331
|
if (named !== undefined)
|
|
@@ -343,7 +346,7 @@ const moduleRef = (state, scope, name) => {
|
|
|
343
346
|
const found = searchedFrom(scope, name.top).find((level) => level.names.has(name.module));
|
|
344
347
|
if (!found)
|
|
345
348
|
return undefined;
|
|
346
|
-
if (found === scope && name.module === scope.own
|
|
349
|
+
if (found === scope && name.module === scope.own) {
|
|
347
350
|
fail(state, name.where, name.entry, `a module reaches its own files already`);
|
|
348
351
|
return null;
|
|
349
352
|
}
|
|
@@ -353,6 +356,7 @@ const moduleRef = (state, scope, name) => {
|
|
|
353
356
|
module: name.module,
|
|
354
357
|
...(name.surface === undefined ? {} : { surface: name.surface }),
|
|
355
358
|
...(up > 0 ? { up } : {}),
|
|
359
|
+
...(name.top ? { top: true } : {}),
|
|
356
360
|
};
|
|
357
361
|
state.pending.push({ ref, where: name.where, archetype: found.archetype, tests: name.tests });
|
|
358
362
|
return ref;
|
|
@@ -374,7 +378,16 @@ const outOfScope = (state, scope, { module, top, tests, where, entry }) => {
|
|
|
374
378
|
const names = searchedFrom(scope, top).flatMap((level) => [...level.names].filter((name) => level !== scope || name !== scope.own));
|
|
375
379
|
const inScope = [...new Set([...names, ...inputs])];
|
|
376
380
|
const testInput = !tests && scope.testInputs.has(module) ? `; ${module} is a test input, which only test code may name` : '';
|
|
377
|
-
|
|
381
|
+
const named = scope.archetype.split('.')[0];
|
|
382
|
+
const hints = testInput !== ''
|
|
383
|
+
? []
|
|
384
|
+
: named !== rootName && !top
|
|
385
|
+
? [`a module outside ${named} comes in through ${named}.requires`]
|
|
386
|
+
: entry === module && /^[\w-]+$/.test(module) && where.endsWith('dependencies')
|
|
387
|
+
? [`a package is npm:${module}`]
|
|
388
|
+
: [];
|
|
389
|
+
return fail(state, where, entry, `no module or input called ${module} here${testInput}; in scope: ${list(inScope.sort(), 'nothing')}` +
|
|
390
|
+
hints.map((hint) => `. ${hint[0].toUpperCase()}${hint.slice(1)}`).join(''));
|
|
378
391
|
};
|
|
379
392
|
const fail = (state, where, entry, problem) => {
|
|
380
393
|
state.fail(`${where}: '${entry}': ${problem}`);
|
|
@@ -4,7 +4,7 @@ import { isRecord, list } from './shared.js';
|
|
|
4
4
|
export const packageText = (pattern) => pattern.startsWith('node:') || pattern.startsWith('virtual:') ? pattern : `npm:${pattern}`;
|
|
5
5
|
export const show = (ref) => ref.kind === 'package'
|
|
6
6
|
? packageText(ref.pattern)
|
|
7
|
-
: `${ref.kind === 'required' ? ref.name : ref.module}${ref.surface === undefined ? '' : `.${ref.surface}`}`;
|
|
7
|
+
: `${ref.kind === 'module' && ref.top ? 'top.' : ''}${ref.kind === 'required' ? ref.name : ref.module}${ref.surface === undefined ? '' : `.${ref.surface}`}`;
|
|
8
8
|
export const checkReferences = (archetypes, pending, fail) => {
|
|
9
9
|
for (const entry of pending) {
|
|
10
10
|
const problem = refProblem(archetypes, entry);
|
|
@@ -23,8 +23,13 @@ export const checkInputs = (archetypes, pending, fail) => {
|
|
|
23
23
|
const uses = pending.filter((entry) => entry.ref.kind === 'required');
|
|
24
24
|
const usesOf = (archetype) => uses.filter((entry) => namedOf(entry.archetype) === archetype);
|
|
25
25
|
for (const archetype of Object.values(archetypes).filter((candidate) => !candidate.inline)) {
|
|
26
|
+
const inputs = [...archetype.requires, ...(archetype.testRequires ?? [])];
|
|
27
|
+
const shadowed = shadowsOf(archetypes, archetype.name, inputs);
|
|
28
|
+
for (const [module, name] of shadowed)
|
|
29
|
+
fail(`${module} shadows the input ${name}; rename one`);
|
|
26
30
|
const used = new Set(usesOf(archetype.name).map((entry) => entry.ref.name));
|
|
27
|
-
|
|
31
|
+
const names = new Set(shadowed.map(([, name]) => name));
|
|
32
|
+
for (const name of inputs.filter((input) => !used.has(input) && !names.has(input)))
|
|
28
33
|
fail(`${archetype.name} requires ${name}, which none of its modules names in dependencies`);
|
|
29
34
|
}
|
|
30
35
|
for (const site of sitesOf(archetypes)) {
|
|
@@ -33,6 +38,11 @@ export const checkInputs = (archetypes, pending, fail) => {
|
|
|
33
38
|
checkSite(archetypes, site, archetype, usesOf(archetype.name), fail);
|
|
34
39
|
}
|
|
35
40
|
};
|
|
41
|
+
const shadowsOf = (archetypes, named, inputs) => Object.values(archetypes)
|
|
42
|
+
.filter((archetype) => namedOf(archetype.name) === named)
|
|
43
|
+
.flatMap((archetype) => archetype.modules
|
|
44
|
+
.filter((module) => inputs.includes(module.name))
|
|
45
|
+
.map((module) => [`${archetype.name}.${module.name}`, module.name]));
|
|
36
46
|
const checkSite = (archetypes, { holder, module }, archetype, uses, fail) => {
|
|
37
47
|
const where = `${holder.name}.${module.name}`;
|
|
38
48
|
for (const name of archetype.requires.filter((input) => !module.inputs?.[input]))
|
|
@@ -174,7 +174,8 @@ const claimsIn = (resolution, module, archetype) => {
|
|
|
174
174
|
const claimsOf = (resolution, module, declared) => {
|
|
175
175
|
const child = resolution.config.archetypes[declared.archetype];
|
|
176
176
|
return declared.paths.flatMap((path) => matchFolders(resolution.folders, module.dir, path).flatMap((match) => {
|
|
177
|
-
const
|
|
177
|
+
const inherited = child.inline ? { ...module.captures, ...match.captures } : match.captures;
|
|
178
|
+
const captures = identify(resolution, child, match.dir, inherited, module, { declared, path });
|
|
178
179
|
return captures ? [{ module: declared, path, archetype: child, ...match, captures }] : [];
|
|
179
180
|
}));
|
|
180
181
|
};
|
|
@@ -249,7 +250,7 @@ const filesModule = (resolution, module, archetype, candidates, file) => {
|
|
|
249
250
|
for (const pattern of declared.patterns) {
|
|
250
251
|
const filled = fill(pattern, module.captures);
|
|
251
252
|
if ('missing' in filled) {
|
|
252
|
-
resolution.errors.add(`${
|
|
253
|
+
resolution.errors.add(`${archetype.name}.${declared.name}.files: '${pattern}' names {${filled.missing}}, which ${module.id} doesn't capture`);
|
|
253
254
|
}
|
|
254
255
|
else if (globRegex(filled.filled).test(path))
|
|
255
256
|
matches.push({ module: declared.name, pattern, score: specificity(filled.filled) });
|
|
@@ -5,7 +5,7 @@ export const withRuleset = (config, rulesets, name, parse, fail) => {
|
|
|
5
5
|
const named = rulesetsOf(rulesets, fail);
|
|
6
6
|
if (name !== undefined && !named.has(name)) {
|
|
7
7
|
const names = [...named.keys()];
|
|
8
|
-
throw new ConfigError(`no ruleset called ${name}. ${names.length > 0 ? `Rulesets: ${names.join(', ')}` : 'The config declares none'}`);
|
|
8
|
+
throw new ConfigError(`rulesets: no ruleset called ${name}. ${names.length > 0 ? `Rulesets: ${names.join(', ')}` : 'The config declares none'}`);
|
|
9
9
|
}
|
|
10
10
|
const chosen = name ?? 'default';
|
|
11
11
|
if (!named.has(chosen))
|
|
@@ -86,12 +86,13 @@ const testTargets = (config) => Object.values(config.archetypes).flatMap((archet
|
|
|
86
86
|
const targetOf = (config, path) => {
|
|
87
87
|
if (config.archetypes[path])
|
|
88
88
|
return { archetype: path };
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
for (let at = path.lastIndexOf('.'); at > 0; at = path.lastIndexOf('.', at - 1)) {
|
|
90
|
+
const archetype = config.archetypes[path.slice(0, at)];
|
|
91
|
+
const module = path.slice(at + 1);
|
|
92
|
+
if (archetype?.modules.some((declared) => declared.name === module))
|
|
93
|
+
return { archetype: archetype.name, module };
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
95
96
|
};
|
|
96
97
|
const applied = (config, layers, parse, fail) => {
|
|
97
98
|
let rules = config.rules;
|
|
@@ -47,3 +47,8 @@ export const rulesOf = (config, model, rules) => {
|
|
|
47
47
|
})),
|
|
48
48
|
};
|
|
49
49
|
};
|
|
50
|
+
const turnsOn = (rules) => rules !== undefined && rules !== 'off' && Object.values(rules).some((setting) => setting !== false);
|
|
51
|
+
export const anyRuleOn = (config) => config.rules === undefined ||
|
|
52
|
+
(config.rules !== 'off' && config.rules['*'] !== false) ||
|
|
53
|
+
turnsOn(config.rules) ||
|
|
54
|
+
Object.values(config.archetypes).some((archetype) => turnsOn(archetype.rules) || archetype.modules.some((module) => turnsOn(module.rules)));
|
package/dist/rules/registry.js
CHANGED
|
@@ -9,7 +9,7 @@ export const registry = (rules) => {
|
|
|
9
9
|
return byId;
|
|
10
10
|
};
|
|
11
11
|
const name = /^[a-z][a-z0-9-]*$/;
|
|
12
|
-
export const pluginRules = (plugins) => Object.entries(plugins).flatMap(([namespace, plugin]) => namespaced(namespace, plugin, `
|
|
12
|
+
export const pluginRules = (plugins) => Object.entries(plugins).flatMap(([namespace, plugin]) => namespaced(namespace, plugin, `plugins.${namespace}`));
|
|
13
13
|
const namespaced = (namespace, plugin, at) => {
|
|
14
14
|
if (!name.test(namespace))
|
|
15
15
|
throw new ConfigError(`${at}: a namespace is lowercase letters, digits and dashes`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yearofthedan/warp",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Architectural fitness checks for TypeScript codebases",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"@stryker-mutator/core": "10.1.1",
|
|
37
37
|
"@stryker-mutator/vitest-runner": "10.1.1",
|
|
38
38
|
"@types/node": "26.6.6",
|
|
39
|
+
"c8": "10.1.3",
|
|
39
40
|
"secretlint": "14.0.1",
|
|
40
41
|
"vite-plus": "catalog:"
|
|
41
42
|
},
|