@yearofthedan/warp 0.5.0 → 0.5.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/README.md +1 -3
- package/dist/adapters/analysis/chains.d.ts +4 -0
- package/dist/adapters/analysis/chains.js +97 -0
- package/dist/adapters/analysis/classes.d.ts +3 -0
- package/dist/adapters/analysis/classes.js +109 -0
- package/dist/adapters/analysis/imports.js +8 -5
- package/dist/adapters/analysis/members.js +1 -1
- package/dist/adapters/analysis/source.d.ts +2 -2
- package/dist/adapters/analysis/source.js +2 -2
- package/dist/adapters/analysis/typescript.js +4 -0
- package/dist/adapters/config/loadConfig.js +1 -1
- package/dist/app/checkModel.js +3 -3
- package/dist/app/load.js +2 -1
- package/dist/builtins/boundaries.d.ts +17 -0
- package/dist/builtins/boundaries.js +35 -0
- package/dist/builtins/class-cohesion/rule.d.ts +6 -0
- package/dist/builtins/class-cohesion/rule.js +34 -0
- package/dist/builtins/export-consumers/rule.d.ts +6 -0
- package/dist/builtins/export-consumers/rule.js +65 -0
- package/dist/builtins/export-spread/rule.d.ts +3 -0
- package/dist/builtins/export-spread/rule.js +65 -0
- package/dist/builtins/export-surface/rule.d.ts +6 -0
- package/dist/builtins/export-surface/rule.js +60 -0
- package/dist/builtins/index.js +16 -1
- package/dist/builtins/loops.js +1 -1
- package/dist/builtins/module-dependencies/rule.d.ts +3 -0
- package/dist/builtins/module-dependencies/rule.js +67 -0
- package/dist/builtins/module-size/rule.d.ts +6 -0
- package/dist/builtins/module-size/rule.js +36 -0
- package/dist/cli.js +1 -1
- package/dist/domain/facts/facts.d.ts +20 -0
- package/dist/domain/language/compile.d.ts +2 -1
- package/dist/domain/language/compile.js +4 -4
- package/dist/domain/language/references.d.ts +3 -1
- package/dist/domain/language/references.js +28 -8
- package/dist/domain/language/rulesets.d.ts +4 -3
- package/dist/domain/language/rulesets.js +3 -3
- package/dist/domain/language/structure.js +8 -7
- package/dist/domain/model/reach.d.ts +1 -0
- package/dist/domain/model/reach.js +7 -0
- package/dist/domain/model/resolve.js +1 -1
- package/dist/domain/model/settings.d.ts +3 -2
- package/dist/domain/model/settings.js +23 -22
- package/dist/domain/rules/registry.js +1 -1
- package/dist/domain/rules/rule.d.ts +1 -0
- package/dist/domain/rules/rule.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
- /package/dist/domain/{model → errors}/configError.d.ts +0 -0
- /package/dist/domain/{model → errors}/configError.js +0 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { wholeNumberOptions } from '../../domain/rules/rule.js';
|
|
2
|
+
import { within } from '../boundaries.js';
|
|
3
|
+
const id = 'export-surface';
|
|
4
|
+
const defaults = { max: 20 };
|
|
5
|
+
export const exportSurface = {
|
|
6
|
+
id,
|
|
7
|
+
scope: 'project',
|
|
8
|
+
defaults,
|
|
9
|
+
parseOptions: wholeNumberOptions(defaults),
|
|
10
|
+
check: ({ model, project, atModule }) => {
|
|
11
|
+
const production = new Set(project.files.map((file) => file.path).filter((path) => !model.isTest(path)));
|
|
12
|
+
const uses = project.files.flatMap(({ path, imports }) => production.has(path)
|
|
13
|
+
? imports
|
|
14
|
+
.filter((edge) => !edge.reExport && production.has(edge.to))
|
|
15
|
+
.flatMap((edge) => edge.names
|
|
16
|
+
.filter((name) => name !== '*')
|
|
17
|
+
.map((name) => ({ from: path, file: edge.to, name, value: !edge.typeOnly })))
|
|
18
|
+
: []);
|
|
19
|
+
return model.modules.flatMap((module) => {
|
|
20
|
+
const { on, options } = atModule(module);
|
|
21
|
+
if (!on)
|
|
22
|
+
return [];
|
|
23
|
+
const names = surface(module, uses, (file) => model.moduleOf(file));
|
|
24
|
+
return names.length > options.max ? [finding(module, names, options.max)] : [];
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const surface = (module, uses, moduleOf) => {
|
|
29
|
+
const names = new Map();
|
|
30
|
+
for (const { from, file, name, value } of uses) {
|
|
31
|
+
if (!within(moduleOf(file), module) || within(moduleOf(from), module))
|
|
32
|
+
continue;
|
|
33
|
+
const key = `${file}#${name}`;
|
|
34
|
+
const known = names.get(key);
|
|
35
|
+
names.set(key, { file, name, value: value || (known?.value ?? false) });
|
|
36
|
+
}
|
|
37
|
+
return [...names.values()].toSorted((a, b) => a.file.localeCompare(b.file) || a.name.localeCompare(b.name));
|
|
38
|
+
};
|
|
39
|
+
const finding = (module, names, max) => {
|
|
40
|
+
const values = names.filter((name) => name.value).length;
|
|
41
|
+
const byFile = Map.groupBy(names, (name) => name.file);
|
|
42
|
+
const [widest] = [...byFile].toSorted(([, a], [, b]) => b.length - a.length);
|
|
43
|
+
return {
|
|
44
|
+
rule: id,
|
|
45
|
+
key: `${id}:${module.id}`,
|
|
46
|
+
file: widest[0],
|
|
47
|
+
line: 1,
|
|
48
|
+
value: names.length,
|
|
49
|
+
limit: max,
|
|
50
|
+
message: `${module.id} offers ${names.length} names that other modules use (${counted(values, 'value')}, ${counted(names.length - values, 'type')}). ` +
|
|
51
|
+
`Its limit here is ${max}. Split it, or give it a smaller front.`,
|
|
52
|
+
details: {
|
|
53
|
+
module: module.id,
|
|
54
|
+
values,
|
|
55
|
+
types: names.length - values,
|
|
56
|
+
files: [...byFile].map(([file, found]) => ({ file, names: found.map((name) => name.name) })),
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const counted = (count, noun) => `${count} ${noun}${count === 1 ? '' : 's'}`;
|
package/dist/builtins/index.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { registry } from '../domain/rules/registry.js';
|
|
2
|
+
import { classCohesion } from './class-cohesion/rule.js';
|
|
2
3
|
import { cognitiveComplexity } from './cognitive-complexity/rule.js';
|
|
4
|
+
import { exportConsumers } from './export-consumers/rule.js';
|
|
5
|
+
import { exportSpread } from './export-spread/rule.js';
|
|
6
|
+
import { exportSurface } from './export-surface/rule.js';
|
|
3
7
|
import { dependencyDirection } from './dependency-direction/rule.js';
|
|
4
8
|
import { fileCohesion } from './file-cohesion/rule.js';
|
|
5
9
|
import { functionCohesion } from './function-cohesion/rule.js';
|
|
10
|
+
import { moduleDependencies } from './module-dependencies/rule.js';
|
|
11
|
+
import { moduleSize } from './module-size/rule.js';
|
|
6
12
|
import { moduleCycle } from './module-cycle/rule.js';
|
|
7
13
|
import { moduleExternals } from './module-externals/rule.js';
|
|
8
14
|
import { noCrossModuleReexport } from './no-cross-module-reexport/rule.js';
|
|
@@ -21,9 +27,15 @@ export const builtinRules = [
|
|
|
21
27
|
noCycle,
|
|
22
28
|
moduleCycle,
|
|
23
29
|
fileCohesion,
|
|
30
|
+
classCohesion,
|
|
24
31
|
functionCohesion,
|
|
25
32
|
cognitiveComplexity,
|
|
26
33
|
unusedDeclaration,
|
|
34
|
+
exportConsumers,
|
|
35
|
+
exportSpread,
|
|
36
|
+
exportSurface,
|
|
37
|
+
moduleDependencies,
|
|
38
|
+
moduleSize,
|
|
27
39
|
];
|
|
28
40
|
const source = 'https://github.com/yearofthedan/warp/blob/main';
|
|
29
41
|
const documented = (rule) => ({
|
|
@@ -33,5 +45,8 @@ const documented = (rule) => ({
|
|
|
33
45
|
export const builtinRegistry = registry(builtinRules.map(documented));
|
|
34
46
|
export const warp = {
|
|
35
47
|
rules: [...builtinRegistry.values()],
|
|
36
|
-
rulesets: {
|
|
48
|
+
rulesets: {
|
|
49
|
+
recommended: { rules: Object.fromEntries(builtinRules.map((rule) => [rule.id, 'on'])) },
|
|
50
|
+
utility: { rules: { 'export-consumers': 'off', 'export-surface': 'off' } },
|
|
51
|
+
},
|
|
37
52
|
};
|
package/dist/builtins/loops.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConfigError } from '../domain/
|
|
1
|
+
import { ConfigError } from '../domain/errors/configError.js';
|
|
2
2
|
export const cycleDefaults = { types: 'count' };
|
|
3
3
|
export const cycleOptions = (raw, at) => {
|
|
4
4
|
const unknown = Object.keys(raw).filter((key) => key !== 'types');
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { crossed, ignored, limitOptions, listed, within } from '../boundaries.js';
|
|
2
|
+
const id = 'module-dependencies';
|
|
3
|
+
const defaults = { max: 4, ignore: [] };
|
|
4
|
+
export const moduleDependencies = {
|
|
5
|
+
id,
|
|
6
|
+
scope: 'project',
|
|
7
|
+
defaults,
|
|
8
|
+
moduleOptions: ['ignore'],
|
|
9
|
+
parseOptions: limitOptions(defaults),
|
|
10
|
+
check: ({ model, project, atModule }) => {
|
|
11
|
+
const imports = productionImports(model, project.files);
|
|
12
|
+
return model.modules.flatMap((module) => {
|
|
13
|
+
const { on, options } = atModule(module);
|
|
14
|
+
if (!on)
|
|
15
|
+
return [];
|
|
16
|
+
const boundaries = crossings(model, module, imports, ignored(model, options.ignore));
|
|
17
|
+
return boundaries.size > options.max ? [finding(module, boundaries, options.max)] : [];
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
const crossings = (model, module, imports, skip) => {
|
|
22
|
+
const boundaries = new Map();
|
|
23
|
+
for (const { file, to, line } of imports) {
|
|
24
|
+
const target = model.moduleOf(to);
|
|
25
|
+
if (!within(model.moduleOf(file), module) || within(target, module))
|
|
26
|
+
continue;
|
|
27
|
+
if (skip.some((holder) => within(target, holder)))
|
|
28
|
+
continue;
|
|
29
|
+
const boundary = crossed(module, target);
|
|
30
|
+
boundaries.set(boundary, [...(boundaries.get(boundary) ?? []), { file, line }]);
|
|
31
|
+
}
|
|
32
|
+
return boundaries;
|
|
33
|
+
};
|
|
34
|
+
const productionImports = (model, files) => {
|
|
35
|
+
const production = new Set(files.map((file) => file.path).filter((path) => !model.isTest(path)));
|
|
36
|
+
return files
|
|
37
|
+
.filter(({ path }) => production.has(path))
|
|
38
|
+
.flatMap(({ path, imports }) => imports
|
|
39
|
+
.map((edge) => ({ file: path, to: edge.via ?? edge.to, line: edge.line }))
|
|
40
|
+
.filter(({ to }) => production.has(to)));
|
|
41
|
+
};
|
|
42
|
+
const finding = (module, boundaries, max) => {
|
|
43
|
+
const named = [...boundaries.keys()].map((boundary) => boundary.id).sort();
|
|
44
|
+
const sites = [...boundaries.values()]
|
|
45
|
+
.map(([first]) => first)
|
|
46
|
+
.toSorted((a, b) => a.file.localeCompare(b.file) || a.line - b.line);
|
|
47
|
+
return {
|
|
48
|
+
rule: id,
|
|
49
|
+
key: `${id}:${module.id}`,
|
|
50
|
+
file: sites[0].file,
|
|
51
|
+
line: sites[0].line,
|
|
52
|
+
value: boundaries.size,
|
|
53
|
+
limit: max,
|
|
54
|
+
message: `${module.id} imports across ${boundaries.size} boundaries: ${listed(named)}. Its limit here is ${max}. ` +
|
|
55
|
+
'Split it along what each part depends on, or reach some of them through one module. A module meant to wire ' +
|
|
56
|
+
`everything together, such as a composition root, turns ${id} off or raises its max.`,
|
|
57
|
+
sites,
|
|
58
|
+
parts: named,
|
|
59
|
+
details: {
|
|
60
|
+
module: module.id,
|
|
61
|
+
boundaries: [...boundaries].map(([boundary, uses]) => ({
|
|
62
|
+
module: boundary.id,
|
|
63
|
+
files: [...new Set(uses.map((use) => use.file))],
|
|
64
|
+
})),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { wholeNumberOptions } from '../../domain/rules/rule.js';
|
|
2
|
+
const id = 'module-size';
|
|
3
|
+
const defaults = { max: 1000 };
|
|
4
|
+
export const moduleSize = {
|
|
5
|
+
id,
|
|
6
|
+
scope: 'project',
|
|
7
|
+
defaults,
|
|
8
|
+
parseOptions: wholeNumberOptions(defaults),
|
|
9
|
+
check: ({ model, project, atModule }) => {
|
|
10
|
+
return model.modules.flatMap((module) => {
|
|
11
|
+
if (module.test)
|
|
12
|
+
return [];
|
|
13
|
+
const { on, options } = atModule(module);
|
|
14
|
+
if (!on)
|
|
15
|
+
return [];
|
|
16
|
+
const files = project.files
|
|
17
|
+
.filter(({ path }) => model.moduleOf(path) === module)
|
|
18
|
+
.map(({ path, lines }) => ({ file: path, lines }))
|
|
19
|
+
.toSorted((a, b) => b.lines - a.lines || a.file.localeCompare(b.file));
|
|
20
|
+
const total = files.reduce((sum, file) => sum + file.lines, 0);
|
|
21
|
+
return total > options.max ? [finding(module, files, total, options.max)] : [];
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
const finding = (module, files, total, max) => ({
|
|
26
|
+
rule: id,
|
|
27
|
+
key: `${id}:${module.id}`,
|
|
28
|
+
file: files[0].file,
|
|
29
|
+
line: 1,
|
|
30
|
+
value: total,
|
|
31
|
+
limit: max,
|
|
32
|
+
message: `${module.id} holds ${total} lines in its own files (limit ${max}): ` +
|
|
33
|
+
`${files.map(({ file, lines }) => `${file} ${lines}`).join(', ')}. ` +
|
|
34
|
+
'Split it into modules that each hold one responsibility.',
|
|
35
|
+
details: { module: module.id, files },
|
|
36
|
+
});
|
package/dist/cli.js
CHANGED
|
@@ -17,7 +17,7 @@ import { gitRepository, projectPathOf } from './adapters/git/repository.js';
|
|
|
17
17
|
import { anyRuleOn, rulesOf } from './domain/model/settings.js';
|
|
18
18
|
import { recommended } from './domain/language/compile.js';
|
|
19
19
|
import { loadConfig } from './app/load.js';
|
|
20
|
-
import { ConfigError } from './domain/
|
|
20
|
+
import { ConfigError } from './domain/errors/configError.js';
|
|
21
21
|
import { reporters } from './adapters/reporters/index.js';
|
|
22
22
|
import { modelJson, modelText } from './adapters/reporters/model.js';
|
|
23
23
|
import { actionable } from './adapters/reporters/reporter.js';
|
|
@@ -9,6 +9,8 @@ export interface FileFacts {
|
|
|
9
9
|
unresolved: UnresolvedImport[];
|
|
10
10
|
members: MemberGraph;
|
|
11
11
|
functions: FunctionFacts[];
|
|
12
|
+
classes: ClassFacts[];
|
|
13
|
+
chains: Chain[];
|
|
12
14
|
}
|
|
13
15
|
export interface ImportEdge {
|
|
14
16
|
from: string;
|
|
@@ -61,3 +63,21 @@ export interface Complexity {
|
|
|
61
63
|
reason: string;
|
|
62
64
|
}[];
|
|
63
65
|
}
|
|
66
|
+
export interface ClassFacts {
|
|
67
|
+
name: string;
|
|
68
|
+
line: number;
|
|
69
|
+
endLine: number;
|
|
70
|
+
lines: number;
|
|
71
|
+
members: MemberGraph;
|
|
72
|
+
}
|
|
73
|
+
export interface Chain {
|
|
74
|
+
line: number;
|
|
75
|
+
root?: Declared;
|
|
76
|
+
hops: Declared[];
|
|
77
|
+
call: boolean;
|
|
78
|
+
}
|
|
79
|
+
export interface Declared {
|
|
80
|
+
file: string;
|
|
81
|
+
name: string;
|
|
82
|
+
member?: string;
|
|
83
|
+
}
|
|
@@ -7,5 +7,6 @@ export interface CompileOptions {
|
|
|
7
7
|
rulesets?: Readonly<Record<string, unknown>>;
|
|
8
8
|
ruleIds?: ReadonlySet<string> | undefined;
|
|
9
9
|
options?: Check['options'];
|
|
10
|
+
moduleOptions?: Check['moduleOptions'];
|
|
10
11
|
}
|
|
11
|
-
export declare const compileConfig: (raw: unknown, { file, plugins, rulesets, ruleIds, options }?: CompileOptions) => ConfigData;
|
|
12
|
+
export declare const compileConfig: (raw: unknown, { file, plugins, rulesets, ruleIds, options, moduleOptions }?: CompileOptions) => ConfigData;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConfigError } from '../
|
|
1
|
+
import { ConfigError } from '../errors/configError.js';
|
|
2
2
|
import { defined, isName, isRecord } from '../model/shared.js';
|
|
3
3
|
import { checkReferences, toRules } from './references.js';
|
|
4
4
|
import { checkInputs, markTests } from './requirements.js';
|
|
@@ -6,7 +6,7 @@ import { rulesets as namedRulesets } from './rulesets.js';
|
|
|
6
6
|
import { parseStructure } from './structure.js';
|
|
7
7
|
export { recommended } from './rulesets.js';
|
|
8
8
|
const keys = ['root', 'archetypes', 'rulesets', 'plugins', 'tsconfig'];
|
|
9
|
-
export const compileConfig = (raw, { file = 'warp.yaml', plugins = [], rulesets = {}, ruleIds, options = {} } = {}) => {
|
|
9
|
+
export const compileConfig = (raw, { file = 'warp.yaml', plugins = [], rulesets = {}, ruleIds, options = {}, moduleOptions = {} } = {}) => {
|
|
10
10
|
const errors = [];
|
|
11
11
|
const fail = (message) => {
|
|
12
12
|
if (!errors.includes(message))
|
|
@@ -22,8 +22,8 @@ export const compileConfig = (raw, { file = 'warp.yaml', plugins = [], rulesets
|
|
|
22
22
|
for (const key of unknownKeys)
|
|
23
23
|
fail(`${key}: unknown key; a config takes ${keys.join(', ')}`);
|
|
24
24
|
checkPlugins(raw.plugins, plugins, fail);
|
|
25
|
-
const check = { fail, ruleIds, options };
|
|
26
|
-
const named = namedRulesets(raw.rulesets, rulesets, (value, at) => toRules(check, value, at), fail);
|
|
25
|
+
const check = { fail, ruleIds, options, moduleOptions };
|
|
26
|
+
const named = namedRulesets(raw.rulesets, rulesets, (value, at, names) => toRules(check, value, at, names), fail);
|
|
27
27
|
const structure = parseStructure(raw.root, raw.archetypes, fail, named.parse);
|
|
28
28
|
named.checkUsed();
|
|
29
29
|
failed();
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { type ArchetypeData, type ExposesData, type RuleSettingsData } from '../model/data.ts';
|
|
2
|
+
import type { Names } from './rulesets.ts';
|
|
2
3
|
import type { PendingRef } from './structure.ts';
|
|
3
4
|
import { type Fail } from '../model/shared.ts';
|
|
4
5
|
export interface Check {
|
|
5
6
|
fail: Fail;
|
|
6
7
|
ruleIds?: ReadonlySet<string> | undefined;
|
|
7
8
|
options: Readonly<Record<string, (raw: Record<string, unknown>, at: string) => unknown>>;
|
|
9
|
+
moduleOptions: Readonly<Record<string, readonly string[]>>;
|
|
8
10
|
}
|
|
9
11
|
export declare const checkReferences: (archetypes: Record<string, ArchetypeData>, pending: readonly PendingRef[], fail: Fail) => void;
|
|
10
12
|
interface Surfaces {
|
|
@@ -12,5 +14,5 @@ interface Surfaces {
|
|
|
12
14
|
testExposes: readonly string[];
|
|
13
15
|
}
|
|
14
16
|
export declare const exposedProblem: (subject: string, { exposes, testExposes }: Surfaces, surface: string | undefined, tests: boolean) => string | undefined;
|
|
15
|
-
export declare const toRules: (check: Check, value: unknown, where: string) => RuleSettingsData | undefined;
|
|
17
|
+
export declare const toRules: (check: Check, value: unknown, where: string, names?: Names) => RuleSettingsData | undefined;
|
|
16
18
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ConfigError } from '../
|
|
2
|
-
import { isTestData } from '../model/data.js';
|
|
1
|
+
import { ConfigError } from '../errors/configError.js';
|
|
2
|
+
import { isTestData, } from '../model/data.js';
|
|
3
3
|
import { isRecord, list } from '../model/shared.js';
|
|
4
4
|
import { exposesText, show } from '../model/text.js';
|
|
5
5
|
export const checkReferences = (archetypes, pending, fail) => {
|
|
@@ -45,7 +45,7 @@ export const exposedProblem = (subject, { exposes, testExposes }, surface, tests
|
|
|
45
45
|
return `${subject} exposes ${list(named, 'nothing')}`;
|
|
46
46
|
return `${subject} exposes ${exposesText(exposes)}, not named surfaces`;
|
|
47
47
|
};
|
|
48
|
-
export const toRules = (check, value, where) => {
|
|
48
|
+
export const toRules = (check, value, where, names) => {
|
|
49
49
|
if (value === undefined)
|
|
50
50
|
return undefined;
|
|
51
51
|
if (value === 'off')
|
|
@@ -55,11 +55,11 @@ export const toRules = (check, value, where) => {
|
|
|
55
55
|
return undefined;
|
|
56
56
|
}
|
|
57
57
|
return Object.fromEntries(Object.entries(value).flatMap(([id, setting]) => {
|
|
58
|
-
const parsed = toSetting(check, id, setting, where);
|
|
58
|
+
const parsed = toSetting(check, id, setting, where, names);
|
|
59
59
|
return parsed === undefined ? [] : [[id, parsed]];
|
|
60
60
|
}));
|
|
61
61
|
};
|
|
62
|
-
const toSetting = (check, id, setting, where) => {
|
|
62
|
+
const toSetting = (check, id, setting, where, names) => {
|
|
63
63
|
if (id !== '*' && check.ruleIds && !check.ruleIds.has(id))
|
|
64
64
|
check.fail(`${where}: no rule called ${id}`);
|
|
65
65
|
else if (id === '*' && setting !== 'off' && setting !== false)
|
|
@@ -69,12 +69,12 @@ const toSetting = (check, id, setting, where) => {
|
|
|
69
69
|
else if (typeof setting === 'boolean')
|
|
70
70
|
return setting;
|
|
71
71
|
else if (isRecord(setting))
|
|
72
|
-
return checkOptions(check, id, setting, where);
|
|
72
|
+
return checkOptions(check, id, setting, where, names);
|
|
73
73
|
else
|
|
74
74
|
check.fail(`${where}.${id}: expected on, off or options`);
|
|
75
75
|
return undefined;
|
|
76
76
|
};
|
|
77
|
-
const checkOptions = (check, id, options, where) => {
|
|
77
|
+
const checkOptions = (check, id, options, where, names) => {
|
|
78
78
|
try {
|
|
79
79
|
check.options[id]?.(options, `${where}.${id}`);
|
|
80
80
|
}
|
|
@@ -82,6 +82,26 @@ const checkOptions = (check, id, options, where) => {
|
|
|
82
82
|
if (!(error instanceof ConfigError))
|
|
83
83
|
throw error;
|
|
84
84
|
check.fail(error.message);
|
|
85
|
+
return { ...options };
|
|
85
86
|
}
|
|
86
|
-
|
|
87
|
+
const resolved = (check.moduleOptions[id] ?? [])
|
|
88
|
+
.filter((key) => options[key] !== undefined)
|
|
89
|
+
.map((key) => [key, modules(check, options[key], `${where}.${id}.${key}`, names)]);
|
|
90
|
+
return { ...options, ...Object.fromEntries(resolved) };
|
|
91
|
+
};
|
|
92
|
+
const modules = (check, entries, where, names) => {
|
|
93
|
+
if (!names) {
|
|
94
|
+
check.fail(`${where}: names modules, so it goes in a ruleset on the root, a module or an archetype`);
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
return entries.flatMap((entry) => {
|
|
98
|
+
const ref = names(entry, where);
|
|
99
|
+
if (ref === undefined)
|
|
100
|
+
return [];
|
|
101
|
+
if (ref.kind === 'package' || ref.surface !== undefined) {
|
|
102
|
+
check.fail(`${where}: '${entry}': name a module, not a package or a surface`);
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
return [ref];
|
|
106
|
+
});
|
|
87
107
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { RuleSettingsData, RulesetData } from '../model/data.ts';
|
|
1
|
+
import type { RefData, RuleSettingsData, RulesetData } from '../model/data.ts';
|
|
2
2
|
import { type Fail } from '../model/shared.ts';
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
3
|
+
export type Names = (entry: string, where: string) => RefData | undefined;
|
|
4
|
+
export type ParseRules = (value: unknown, at: string, names?: Names) => RuleSettingsData | undefined;
|
|
5
|
+
export type ParseRuleset = (value: unknown, at: string, names?: Names) => RulesetData | undefined;
|
|
5
6
|
export interface Rulesets {
|
|
6
7
|
parse: ParseRuleset;
|
|
7
8
|
checkUsed(): void;
|
|
@@ -10,7 +10,7 @@ export const rulesets = (named, shared, parseRules, fail) => {
|
|
|
10
10
|
extended: new Set(),
|
|
11
11
|
};
|
|
12
12
|
return {
|
|
13
|
-
parse: (value, at) => (value === undefined ? undefined : flatten(state, value, at, [])),
|
|
13
|
+
parse: (value, at, names) => (value === undefined ? undefined : flatten(state, value, at, [], names)),
|
|
14
14
|
checkUsed: () => {
|
|
15
15
|
for (const name of [...state.local.keys()].filter((name) => !state.extended.has(name)))
|
|
16
16
|
fail(`rulesets.${name}: nothing extends it; a named ruleset applies through a ruleset's extends`);
|
|
@@ -28,7 +28,7 @@ const localRulesets = (value, fail) => {
|
|
|
28
28
|
fail(`rulesets.${name}: a name has no ':', which marks a plugin's ruleset`);
|
|
29
29
|
return new Map(Object.entries(value).filter(([name]) => !name.includes(':')));
|
|
30
30
|
};
|
|
31
|
-
const flatten = (state, value, at, seen) => {
|
|
31
|
+
const flatten = (state, value, at, seen, names) => {
|
|
32
32
|
const def = Array.isArray(value) ? { extends: value } : value === 'off' ? { rules: 'off' } : value;
|
|
33
33
|
if (!isRecord(def)) {
|
|
34
34
|
state.fail(`${at}: expected a ruleset, off, [${recommended}] or { extends: [${recommended}], rules: {...} }`);
|
|
@@ -37,7 +37,7 @@ const flatten = (state, value, at, seen) => {
|
|
|
37
37
|
for (const key of Object.keys(def).filter((key) => !rulesetKeys.includes(key)))
|
|
38
38
|
state.fail(`${at}: unknown key ${key}; a ruleset takes ${rulesetKeys.join(', ')}`);
|
|
39
39
|
const bases = [def.extends ?? []].flat().flatMap((base) => baseOf(state, base, `${at}.extends`, seen));
|
|
40
|
-
const own = state.parseRules(def.rules, `${at}.rules
|
|
40
|
+
const own = state.parseRules(def.rules, `${at}.rules`, names);
|
|
41
41
|
return own ? [...bases, own] : bases;
|
|
42
42
|
};
|
|
43
43
|
const baseOf = (state, base, at, seen) => {
|
|
@@ -74,7 +74,6 @@ const archetype = (state, name, def, options) => {
|
|
|
74
74
|
const data = options.parent ? inlineData(state, name, where, test) : namedData(state, name, def, test);
|
|
75
75
|
if (options.test)
|
|
76
76
|
data.test = true;
|
|
77
|
-
Object.assign(data, rulesetsAt(state, where, def.ruleset, test.ruleset));
|
|
78
77
|
const scope = {
|
|
79
78
|
archetype: name,
|
|
80
79
|
names: new Set([...Object.keys(modules), ...Object.keys(testModules), ...filesTestNames(modules)]),
|
|
@@ -82,6 +81,7 @@ const archetype = (state, name, def, options) => {
|
|
|
82
81
|
inputs: options.parent?.inputs ?? new Set(data.requires),
|
|
83
82
|
testInputs: options.parent?.testInputs ?? new Set(data.testRequires),
|
|
84
83
|
};
|
|
84
|
+
Object.assign(data, rulesetsAt(state, scope, where, def.ruleset, test.ruleset));
|
|
85
85
|
state.archetypes[name] = data;
|
|
86
86
|
const build = options.test ? testModule : productionModule;
|
|
87
87
|
for (const [module, raw] of Object.entries(modules))
|
|
@@ -110,9 +110,10 @@ const inlineData = (state, name, where, test) => {
|
|
|
110
110
|
state.fail(`${where}.test.requires: only an archetype requires; its modules see what it requires`);
|
|
111
111
|
return { name, inline: true, requires: [], modules: [] };
|
|
112
112
|
};
|
|
113
|
-
const rulesetsAt = (state, where, ruleset, testRuleset) => {
|
|
114
|
-
const
|
|
115
|
-
const
|
|
113
|
+
const rulesetsAt = (state, scope, where, ruleset, testRuleset) => {
|
|
114
|
+
const names = (entry, at) => resolveName(state, scope, entry, false, at);
|
|
115
|
+
const own = state.ruleset(ruleset, `${where}.ruleset`, names);
|
|
116
|
+
const tests = state.ruleset(testRuleset, `${where}.test.ruleset`, names);
|
|
116
117
|
return { ...(own ? { ruleset: own } : {}), ...(tests ? { testRuleset: tests } : {}) };
|
|
117
118
|
};
|
|
118
119
|
const testBlock = (state, where, value) => {
|
|
@@ -202,7 +203,7 @@ const testFiles = (state, scope, name, def, of) => {
|
|
|
202
203
|
...(of === undefined ? {} : { of }),
|
|
203
204
|
using: references(state, scope, `${where}.dependencies`, def.dependencies, true),
|
|
204
205
|
...(exposed && exposed.kind !== 'surfaces' ? { exposes: exposed } : {}),
|
|
205
|
-
...rulesetsAt(state, where, def.ruleset, undefined),
|
|
206
|
+
...rulesetsAt(state, scope, where, def.ruleset, undefined),
|
|
206
207
|
...description(state, where, def.description),
|
|
207
208
|
};
|
|
208
209
|
};
|
|
@@ -243,7 +244,7 @@ const instance = (state, scope, name, def, test) => {
|
|
|
243
244
|
using: Object.values(own),
|
|
244
245
|
...(Object.keys(own).length > 0 ? { inputs: own } : {}),
|
|
245
246
|
...(Object.keys(tests).length > 0 ? { testInputs: tests } : {}),
|
|
246
|
-
...rulesetsAt(state, where, def.ruleset, testBlock.ruleset),
|
|
247
|
+
...rulesetsAt(state, scope, where, def.ruleset, testBlock.ruleset),
|
|
247
248
|
...description(state, where, def.description),
|
|
248
249
|
};
|
|
249
250
|
};
|
|
@@ -275,7 +276,7 @@ const filesModule = (state, scope, name, def) => {
|
|
|
275
276
|
patterns: patterns(state, `${where}.files`, def.files),
|
|
276
277
|
using: references(state, scope, `${where}.dependencies`, def.dependencies, false),
|
|
277
278
|
...(exposed && exposed.kind !== 'surfaces' ? { exposes: exposed } : {}),
|
|
278
|
-
...rulesetsAt(state, where, def.ruleset, test.ruleset),
|
|
279
|
+
...rulesetsAt(state, scope, where, def.ruleset, test.ruleset),
|
|
279
280
|
...description(state, where, def.description),
|
|
280
281
|
};
|
|
281
282
|
for (const key of Object.keys(test).filter((key) => key !== 'modules' && key !== 'ruleset'))
|
|
@@ -39,3 +39,4 @@ export declare const verdicts: (config: ConfigData, model: Model) => Verdicts;
|
|
|
39
39
|
export declare const instanceOf: (state: {
|
|
40
40
|
config: ConfigData;
|
|
41
41
|
}, holder: ModuleView) => ModuleView;
|
|
42
|
+
export declare const modulesNamed: (config: ConfigData, holder: ModuleView, ref: RefData) => ModuleView[];
|
|
@@ -102,6 +102,13 @@ const concrete = (state, holder, ref, view) => {
|
|
|
102
102
|
const found = concrete(state, instance.parent, bound.kind === 'package' ? bound : { ...bound, surface }, view);
|
|
103
103
|
return found && { ...found, via: [bound, ...found.via] };
|
|
104
104
|
};
|
|
105
|
+
export const modulesNamed = (config, holder, ref) => {
|
|
106
|
+
const found = concrete({ config }, holder, ref, 'production');
|
|
107
|
+
if (!found || found.ref.kind !== 'module')
|
|
108
|
+
return [];
|
|
109
|
+
const { module } = found.ref;
|
|
110
|
+
return found.holder.children.filter((child) => child.name === module);
|
|
111
|
+
};
|
|
105
112
|
const resolve = (state, module, ref, view) => {
|
|
106
113
|
const found = concrete(state, module, ref, view);
|
|
107
114
|
if (!found || found.ref.kind !== 'module')
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConfigError } from '
|
|
1
|
+
import { ConfigError } from '../errors/configError.js';
|
|
2
2
|
export const declarationOf = (config, module) => module.parent && config.archetypes[module.parent.archetype].modules.find((data) => data.name === module.name);
|
|
3
3
|
export const rootId = '(root)';
|
|
4
4
|
const placeholders = /\{(\w+)\}/g;
|
|
@@ -5,8 +5,8 @@ export interface RuleAt {
|
|
|
5
5
|
options: Record<string, unknown>;
|
|
6
6
|
}
|
|
7
7
|
export interface Settings {
|
|
8
|
-
atFile(file: string, id: string, defaults?: object): RuleAt;
|
|
9
|
-
atModule(module: ModuleView, id: string, defaults?: object): RuleAt;
|
|
8
|
+
atFile(file: string, id: string, defaults?: object, moduleOptions?: readonly string[]): RuleAt;
|
|
9
|
+
atModule(module: ModuleView, id: string, defaults?: object, moduleOptions?: readonly string[]): RuleAt;
|
|
10
10
|
}
|
|
11
11
|
export declare const settings: (config: ConfigData, model: Model) => Settings;
|
|
12
12
|
export type ResolvedRules = Record<string, false | Record<string, unknown>>;
|
|
@@ -17,5 +17,6 @@ export interface RulesOf {
|
|
|
17
17
|
export declare const rulesOf: (config: ConfigData, model: Model, rules: readonly {
|
|
18
18
|
id: string;
|
|
19
19
|
defaults?: object;
|
|
20
|
+
moduleOptions?: readonly string[];
|
|
20
21
|
}[]) => RulesOf;
|
|
21
22
|
export declare const anyRuleOn: (config: ConfigData) => boolean;
|
|
@@ -1,21 +1,19 @@
|
|
|
1
|
+
import { modulesNamed } from './reach.js';
|
|
1
2
|
import { declarationOf } from './resolve.js';
|
|
2
3
|
import { isRecord } from './shared.js';
|
|
3
4
|
const archetypeOf = (config, module) => module.archetype === undefined ? undefined : config.archetypes[module.archetype];
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
];
|
|
8
|
-
|
|
9
|
-
archetypeOf(config, module)?.testRuleset,
|
|
10
|
-
declarationOf(config, module)?.testRuleset,
|
|
11
|
-
];
|
|
5
|
+
const layered = (config, module, test) => {
|
|
6
|
+
const pick = (place) => test ? place?.testRuleset : place?.ruleset;
|
|
7
|
+
const inside = { ruleset: pick(archetypeOf(config, module)), holder: module };
|
|
8
|
+
return module.parent ? [inside, { ruleset: pick(declarationOf(config, module)), holder: module.parent }] : [inside];
|
|
9
|
+
};
|
|
12
10
|
const production = (config, module) => [
|
|
13
11
|
...(module.parent ? production(config, module.parent) : []),
|
|
14
|
-
...
|
|
12
|
+
...layered(config, module, false),
|
|
15
13
|
];
|
|
16
14
|
const tests = (config, module) => [
|
|
17
15
|
...(module.parent ? tests(config, module.parent) : []),
|
|
18
|
-
...
|
|
16
|
+
...layered(config, module, true),
|
|
19
17
|
];
|
|
20
18
|
const filesModuleOf = (config, holder, test) => {
|
|
21
19
|
const of = declarationOf(config, test)?.of;
|
|
@@ -31,21 +29,24 @@ const layersOf = (config, module) => {
|
|
|
31
29
|
const files = filesModuleOf(config, holder, chain[0]);
|
|
32
30
|
return [
|
|
33
31
|
...production(config, holder),
|
|
34
|
-
files?.ruleset,
|
|
32
|
+
{ ruleset: files?.ruleset, holder },
|
|
35
33
|
...tests(config, holder),
|
|
36
|
-
files?.testRuleset,
|
|
37
|
-
...chain.flatMap((test) =>
|
|
34
|
+
{ ruleset: files?.testRuleset, holder },
|
|
35
|
+
...chain.flatMap((test) => layered(config, test, false)),
|
|
38
36
|
];
|
|
39
37
|
};
|
|
40
|
-
const
|
|
38
|
+
const named = (config, { holder }, refs) => refs.flatMap((ref) => modulesNamed(config, holder, ref).map((module) => module.id));
|
|
39
|
+
const ruleAt = (config, layers, id, defaults = {}, moduleOptions = []) => {
|
|
41
40
|
const at = { on: false, options: { ...defaults } };
|
|
42
41
|
for (const layer of layers) {
|
|
43
|
-
const setting = Object.hasOwn(layer, id) ? layer[id] : layer['*'];
|
|
42
|
+
const setting = Object.hasOwn(layer.rules, id) ? layer.rules[id] : layer.rules['*'];
|
|
44
43
|
if (setting === undefined)
|
|
45
44
|
continue;
|
|
46
45
|
at.on = setting !== false;
|
|
47
|
-
if (isRecord(setting))
|
|
48
|
-
|
|
46
|
+
if (!isRecord(setting))
|
|
47
|
+
continue;
|
|
48
|
+
const modules = moduleOptions.filter((key) => Array.isArray(setting[key]));
|
|
49
|
+
Object.assign(at.options, setting, Object.fromEntries(modules.map((key) => [key, named(config, layer, setting[key])])));
|
|
49
50
|
}
|
|
50
51
|
return at;
|
|
51
52
|
};
|
|
@@ -56,20 +57,20 @@ export const settings = (config, model) => {
|
|
|
56
57
|
return [];
|
|
57
58
|
let found = cache.get(module);
|
|
58
59
|
if (!found)
|
|
59
|
-
cache.set(module, (found = layersOf(config, module).flatMap((ruleset) => ruleset ?? [])));
|
|
60
|
+
cache.set(module, (found = layersOf(config, module).flatMap(({ ruleset, holder }) => (ruleset ?? []).map((rules) => ({ rules, holder })))));
|
|
60
61
|
return found;
|
|
61
62
|
};
|
|
62
63
|
return {
|
|
63
|
-
atFile: (file, id, defaults) => ruleAt(at(model.moduleOf(file)), id, defaults),
|
|
64
|
-
atModule: (module, id, defaults) => ruleAt(at(module), id, defaults),
|
|
64
|
+
atFile: (file, id, defaults, moduleOptions) => ruleAt(config, at(model.moduleOf(file)), id, defaults, moduleOptions),
|
|
65
|
+
atModule: (module, id, defaults, moduleOptions) => ruleAt(config, at(module), id, defaults, moduleOptions),
|
|
65
66
|
};
|
|
66
67
|
};
|
|
67
68
|
export const rulesOf = (config, model, rules) => {
|
|
68
69
|
const settled = settings(config, model);
|
|
69
70
|
return {
|
|
70
71
|
defaults: Object.fromEntries(rules.map(({ id, defaults }) => [id, { ...defaults }])),
|
|
71
|
-
module: (module) => Object.fromEntries(rules.map(({ id, defaults }) => {
|
|
72
|
-
const { on, options } = settled.atModule(module, id, defaults);
|
|
72
|
+
module: (module) => Object.fromEntries(rules.map(({ id, defaults, moduleOptions }) => {
|
|
73
|
+
const { on, options } = settled.atModule(module, id, defaults, moduleOptions);
|
|
73
74
|
return [id, on ? options : false];
|
|
74
75
|
})),
|
|
75
76
|
};
|
|
@@ -40,6 +40,7 @@ interface RuleBase<Options extends object> {
|
|
|
40
40
|
id: string;
|
|
41
41
|
docs?: string;
|
|
42
42
|
defaults?: Options;
|
|
43
|
+
moduleOptions?: readonly string[];
|
|
43
44
|
parseOptions?: (raw: Record<string, unknown>, at: string) => Partial<Options>;
|
|
44
45
|
}
|
|
45
46
|
export interface FileRule<Options extends object = object> extends RuleBase<Options> {
|