@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
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ each may use, and warp reports the code that steps outside it:
|
|
|
6
6
|
- imports outside a module's reach, including packages it doesn't list.
|
|
7
7
|
- imports and re-exports that go around a module's surface.
|
|
8
8
|
- import loops between files and between modules.
|
|
9
|
-
- files and functions that are too large or too tangled to follow.
|
|
9
|
+
- files, classes and functions that are too large or too tangled to follow.
|
|
10
10
|
- parts of the config that the code no longer uses.
|
|
11
11
|
|
|
12
12
|
`warp check` judges the whole codebase, for CI. `warp diff` reports only what a change made new or worse, so it can
|
|
@@ -66,5 +66,3 @@ new: src/utils/slug.ts:1 [dependency-direction] src/utils/slug.ts (root.utils sr
|
|
|
66
66
|
- warp follows `import`, `export … from`, `import x = require()`, `import()`, `require()` and `import('…')` types
|
|
67
67
|
when the specifier is a string literal, a template literal without `${}` included. A specifier computed at runtime,
|
|
68
68
|
such as `import(name)` or `` import(`./${name}.ts`) ``, goes unchecked.
|
|
69
|
-
- A namespace import (`import * as x`) points at the module the specifier resolves to, a barrel included.
|
|
70
|
-
- Each class method is checked as a function of its own, and a class is one member of its file.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { SyntaxKind } from 'typescript/ast';
|
|
2
|
+
import { isCallExpression, isHeritageClause, isIdentifier, isNonNullExpression, isParenthesizedExpression, isPropertyAccessExpression, isShorthandPropertyAssignment, } from 'typescript/ast/is';
|
|
3
|
+
import { SymbolFlags } from 'typescript/sync';
|
|
4
|
+
import { walk } from './source.js';
|
|
5
|
+
export const fileChains = (source, resolver) => {
|
|
6
|
+
const declared = declaredIn(source, resolver);
|
|
7
|
+
const chains = [];
|
|
8
|
+
walk(source.sourceFile, (node) => {
|
|
9
|
+
if (skipped(node))
|
|
10
|
+
return false;
|
|
11
|
+
if (isPropertyAccessExpression(node) && !isPropertyAccessExpression(node.parent)) {
|
|
12
|
+
const chain = chainOf(node, source, declared);
|
|
13
|
+
if (chain)
|
|
14
|
+
chains.push(chain);
|
|
15
|
+
}
|
|
16
|
+
if (isIdentifier(node) && !isPropertyAccessExpression(node.parent)) {
|
|
17
|
+
const root = declared.root(node);
|
|
18
|
+
if (root)
|
|
19
|
+
chains.push({ line: source.line(node), root, hops: [], call: isCalled(node) });
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
});
|
|
23
|
+
return chains;
|
|
24
|
+
};
|
|
25
|
+
const typeNodes = (node) => node.kind >= SyntaxKind.FirstTypeNode && node.kind <= SyntaxKind.LastTypeNode;
|
|
26
|
+
const skipped = (node) => typeNodes(node) ||
|
|
27
|
+
node.kind === SyntaxKind.ImportDeclaration ||
|
|
28
|
+
node.kind === SyntaxKind.ImportEqualsDeclaration ||
|
|
29
|
+
node.kind === SyntaxKind.ExportDeclaration ||
|
|
30
|
+
node.kind === SyntaxKind.ExportAssignment ||
|
|
31
|
+
node.kind === SyntaxKind.InterfaceDeclaration ||
|
|
32
|
+
node.kind === SyntaxKind.TypeAliasDeclaration ||
|
|
33
|
+
(isHeritageClause(node) && node.token === SyntaxKind.ImplementsKeyword);
|
|
34
|
+
const unwrap = (expression) => isParenthesizedExpression(expression) || isNonNullExpression(expression) ? unwrap(expression.expression) : expression;
|
|
35
|
+
const isCalled = (node) => isCallExpression(node.parent) && node.parent.expression === node;
|
|
36
|
+
const chainOf = (outer, source, declared) => {
|
|
37
|
+
const accesses = [];
|
|
38
|
+
let at = outer;
|
|
39
|
+
while (isPropertyAccessExpression(at)) {
|
|
40
|
+
accesses.unshift(at);
|
|
41
|
+
at = unwrap(at.expression);
|
|
42
|
+
}
|
|
43
|
+
const root = isIdentifier(at) ? declared.root(at) : undefined;
|
|
44
|
+
const hops = accesses.flatMap((access) => {
|
|
45
|
+
if (!isIdentifier(access.name))
|
|
46
|
+
return [];
|
|
47
|
+
const hop = declared.hop(access.name, root);
|
|
48
|
+
return hop ? [hop] : [];
|
|
49
|
+
});
|
|
50
|
+
if (!root && hops.length === 0)
|
|
51
|
+
return undefined;
|
|
52
|
+
return { line: source.line(outer), ...(root ? { root } : {}), hops, call: isCalled(outer) };
|
|
53
|
+
};
|
|
54
|
+
const declaredIn = (source, resolver) => {
|
|
55
|
+
const lookup = { source, resolver, tops: new Map() };
|
|
56
|
+
return { root: (identifier) => rootOf(lookup, identifier), hop: (name, root) => hopOf(lookup, name, root) };
|
|
57
|
+
};
|
|
58
|
+
const otherFile = ({ source, resolver }, symbol) => {
|
|
59
|
+
const file = symbol.declarations.map((declaration) => resolver.relative(declaration.path)).find(Boolean);
|
|
60
|
+
return file === source.file ? undefined : file;
|
|
61
|
+
};
|
|
62
|
+
const valueSymbol = ({ source, resolver }, identifier) => isShorthandPropertyAssignment(identifier.parent)
|
|
63
|
+
? resolver.checker.getShorthandAssignmentValueSymbol(identifier.parent)
|
|
64
|
+
: source.symbolOf(identifier);
|
|
65
|
+
const rootOf = (lookup, identifier) => {
|
|
66
|
+
const { checker } = lookup.resolver;
|
|
67
|
+
const symbol = valueSymbol(lookup, identifier);
|
|
68
|
+
if (!symbol || !(symbol.flags & SymbolFlags.Alias))
|
|
69
|
+
return undefined;
|
|
70
|
+
const target = checker.getAliasedSymbol(symbol);
|
|
71
|
+
if (checker.isUnknownSymbol(target) || !(target.flags & SymbolFlags.Value))
|
|
72
|
+
return undefined;
|
|
73
|
+
const file = otherFile(lookup, target);
|
|
74
|
+
return file ? { file, name: isFileSymbol(target) ? '*' : target.name } : undefined;
|
|
75
|
+
};
|
|
76
|
+
const hopOf = (lookup, name, root) => {
|
|
77
|
+
const symbol = lookup.source.symbolOf(name);
|
|
78
|
+
const file = symbol && otherFile(lookup, symbol);
|
|
79
|
+
if (!symbol || !file)
|
|
80
|
+
return undefined;
|
|
81
|
+
const top = topOf(lookup, symbol);
|
|
82
|
+
if (top === symbol)
|
|
83
|
+
return { file, name: symbol.name };
|
|
84
|
+
if (top)
|
|
85
|
+
return { file, name: top.name, member: symbol.name };
|
|
86
|
+
return root && root.name !== '*' ? { file: root.file, name: root.name, member: symbol.name } : undefined;
|
|
87
|
+
};
|
|
88
|
+
const topOf = ({ tops }, symbol) => {
|
|
89
|
+
if (!tops.has(symbol)) {
|
|
90
|
+
let top = symbol;
|
|
91
|
+
for (let parent = top.getParent(); parent && !isFileSymbol(parent); parent = parent.getParent())
|
|
92
|
+
top = parent;
|
|
93
|
+
tops.set(symbol, top.name.startsWith('__') ? undefined : top);
|
|
94
|
+
}
|
|
95
|
+
return tops.get(symbol);
|
|
96
|
+
};
|
|
97
|
+
const isFileSymbol = (symbol) => symbol.declarations.some((declaration) => declaration.kind === SyntaxKind.SourceFile);
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { isArrowFunction, isClassDeclaration, isClassLikeDeclaration, isConstructorDeclaration, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isIdentifier, isMethodDeclaration, isPrivateIdentifier, isPropertyAccessExpression, isPropertyDeclaration, isSetAccessorDeclaration, isThisExpression, isVariableDeclaration, } from 'typescript/ast/is';
|
|
2
|
+
import { getCombinedModifierFlags } from 'typescript/ast/utils';
|
|
3
|
+
import { ModifierFlags, SymbolFlags } from 'typescript/sync';
|
|
4
|
+
import { symbolKey, walk } from './source.js';
|
|
5
|
+
export const classFacts = (source) => {
|
|
6
|
+
const facts = [];
|
|
7
|
+
walk(source.sourceFile, (node) => {
|
|
8
|
+
if (isClassLikeDeclaration(node))
|
|
9
|
+
facts.push(classOf(node, source));
|
|
10
|
+
});
|
|
11
|
+
return facts;
|
|
12
|
+
};
|
|
13
|
+
const classOf = (node, source) => {
|
|
14
|
+
const line = source.line(node);
|
|
15
|
+
const endLine = source.endLine(node);
|
|
16
|
+
return { name: className(node, source), line, endLine, lines: endLine - line + 1, members: classGraph(node, source) };
|
|
17
|
+
};
|
|
18
|
+
const className = (node, source) => {
|
|
19
|
+
if (node.name)
|
|
20
|
+
return node.name.text;
|
|
21
|
+
if (isVariableDeclaration(node.parent))
|
|
22
|
+
return node.parent.name.getText(source.sourceFile);
|
|
23
|
+
return isClassDeclaration(node) ? 'default' : '<anonymous class>';
|
|
24
|
+
};
|
|
25
|
+
const classGraph = (node, source) => {
|
|
26
|
+
const self = selfKey(node, source);
|
|
27
|
+
const parts = byKey(node.members.flatMap((element) => partsOf(element, source)));
|
|
28
|
+
const members = parts.map((part) => part.member);
|
|
29
|
+
const owner = new Map(parts.flatMap((part, index) => (part.key ? [[part.key, index]] : [])));
|
|
30
|
+
const links = [];
|
|
31
|
+
parts.forEach((part, index) => {
|
|
32
|
+
for (const touch of part.bodies.flatMap((body) => touches(body, self, source))) {
|
|
33
|
+
let other = owner.get(touch.key);
|
|
34
|
+
if (other === undefined && isField(touch.symbol)) {
|
|
35
|
+
other = members.push({ name: touch.symbol.name, kind: 'state', line: touch.line, endLine: touch.line }) - 1;
|
|
36
|
+
owner.set(touch.key, other);
|
|
37
|
+
}
|
|
38
|
+
if (other !== undefined && other !== index)
|
|
39
|
+
links.push([index, other]);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return { members, links };
|
|
43
|
+
};
|
|
44
|
+
const partsOf = (element, source) => {
|
|
45
|
+
if (isConstructorDeclaration(element)) {
|
|
46
|
+
return element.parameters.filter(isParameterProperty).map((parameter) => part(parameter, 'state', [], source));
|
|
47
|
+
}
|
|
48
|
+
if (isMethodDeclaration(element))
|
|
49
|
+
return element.body ? [part(element, 'function', [element.body], source)] : [];
|
|
50
|
+
if (isPropertyDeclaration(element)) {
|
|
51
|
+
const value = element.initializer ? [element.initializer] : [];
|
|
52
|
+
return [part(element, isFunctionValue(element.initializer) ? 'function' : 'state', value, source)];
|
|
53
|
+
}
|
|
54
|
+
if (isGetAccessorDeclaration(element) || isSetAccessorDeclaration(element)) {
|
|
55
|
+
return [part(element, 'state', element.body ? [element.body] : [], source)];
|
|
56
|
+
}
|
|
57
|
+
return [];
|
|
58
|
+
};
|
|
59
|
+
const part = (node, kind, bodies, source) => {
|
|
60
|
+
const name = node.name;
|
|
61
|
+
const key = isIdentifier(name) || isPrivateIdentifier(name) ? symbolKey(source.symbolOf(name)) : undefined;
|
|
62
|
+
return {
|
|
63
|
+
member: {
|
|
64
|
+
name: name.getText(source.sourceFile),
|
|
65
|
+
kind,
|
|
66
|
+
line: source.line(node),
|
|
67
|
+
endLine: source.endLine(node),
|
|
68
|
+
},
|
|
69
|
+
key,
|
|
70
|
+
bodies,
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const byKey = (parts) => {
|
|
74
|
+
const kept = new Map();
|
|
75
|
+
return parts.filter((candidate) => {
|
|
76
|
+
const first = candidate.key ? kept.get(candidate.key) : undefined;
|
|
77
|
+
if (!first) {
|
|
78
|
+
if (candidate.key)
|
|
79
|
+
kept.set(candidate.key, candidate);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
first.bodies.push(...candidate.bodies);
|
|
83
|
+
return false;
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
const touches = (body, self, source) => {
|
|
87
|
+
const found = [];
|
|
88
|
+
walk(body, (node) => {
|
|
89
|
+
if (node !== body && rebindsThis(node))
|
|
90
|
+
return false;
|
|
91
|
+
if (!isPropertyAccessExpression(node) || !isSelf(node.expression, self, source))
|
|
92
|
+
return;
|
|
93
|
+
const symbol = source.symbolOf(node.name);
|
|
94
|
+
const key = symbolKey(symbol);
|
|
95
|
+
if (symbol && key)
|
|
96
|
+
found.push({ key, symbol, line: source.line(node) });
|
|
97
|
+
});
|
|
98
|
+
return found;
|
|
99
|
+
};
|
|
100
|
+
const rebindsThis = (node) => isFunctionDeclaration(node) || isFunctionExpression(node) || isClassLikeDeclaration(node);
|
|
101
|
+
const isSelf = (expression, self, source) => isThisExpression(expression) ||
|
|
102
|
+
(self !== undefined && isIdentifier(expression) && symbolKey(source.symbolOf(expression)) === self);
|
|
103
|
+
const selfKey = (node, source) => {
|
|
104
|
+
const name = node.name ?? (isVariableDeclaration(node.parent) ? node.parent.name : undefined);
|
|
105
|
+
return name && isIdentifier(name) ? symbolKey(source.symbolOf(name)) : undefined;
|
|
106
|
+
};
|
|
107
|
+
const isField = (symbol) => (symbol.flags & SymbolFlags.PropertyOrAccessor) !== 0 && (symbol.flags & SymbolFlags.Method) === 0;
|
|
108
|
+
const isParameterProperty = (parameter) => (getCombinedModifierFlags(parameter) & ModifierFlags.ParameterPropertyModifier) !== 0;
|
|
109
|
+
const isFunctionValue = (value) => value !== undefined && (isArrowFunction(value) || isFunctionExpression(value));
|
|
@@ -62,10 +62,10 @@ const referenceEdges = (declaration, via, source, resolver) => {
|
|
|
62
62
|
const byTarget = new Map();
|
|
63
63
|
for (const binding of declaration.bindings) {
|
|
64
64
|
const target = resolveBinding(binding, source, resolver) ?? { file: via, typeOnly: false };
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
entry.names
|
|
68
|
-
entry.
|
|
65
|
+
const typeOnly = declaration.typeOnly || binding.typeOnly || target.typeOnly;
|
|
66
|
+
const key = `${target.file}\0${binding.reExport}\0${typeOnly}`;
|
|
67
|
+
const entry = byTarget.get(key) ?? { to: target.file, names: [], typeOnly, reExport: binding.reExport };
|
|
68
|
+
entry.names.push(target.name ?? binding.identifier.text);
|
|
69
69
|
byTarget.set(key, entry);
|
|
70
70
|
}
|
|
71
71
|
return [...byTarget.values()].map(({ to, names, typeOnly, reExport }) => edge(to, names, typeOnly, reExport));
|
|
@@ -169,5 +169,8 @@ const resolveBinding = (binding, source, resolver) => {
|
|
|
169
169
|
.find((path) => path !== undefined);
|
|
170
170
|
if (!file)
|
|
171
171
|
return undefined;
|
|
172
|
-
|
|
172
|
+
const name = target.declarations.some((declaration) => declaration.kind === SyntaxKind.SourceFile)
|
|
173
|
+
? '*'
|
|
174
|
+
: target.name;
|
|
175
|
+
return { file, typeOnly: (target.flags & SymbolFlags.Value) === 0, name };
|
|
173
176
|
};
|
|
@@ -87,7 +87,7 @@ const references = (node, source) => {
|
|
|
87
87
|
if (key)
|
|
88
88
|
keys.add(key);
|
|
89
89
|
}
|
|
90
|
-
if (isPropertyAccessExpression(child) && isThisExpression(child.expression)
|
|
90
|
+
if (isPropertyAccessExpression(child) && isThisExpression(child.expression)) {
|
|
91
91
|
keys.add(`this.${child.name.text}`);
|
|
92
92
|
}
|
|
93
93
|
});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Identifier, Node, SourceFile } from 'typescript/ast';
|
|
1
|
+
import type { Identifier, Node, PrivateIdentifier, SourceFile } from 'typescript/ast';
|
|
2
2
|
import type { Checker, Symbol } from 'typescript/sync';
|
|
3
3
|
export interface Source {
|
|
4
4
|
file: string;
|
|
5
5
|
sourceFile: SourceFile;
|
|
6
|
-
symbolOf: (identifier: Identifier) => Symbol | undefined;
|
|
6
|
+
symbolOf: (identifier: Identifier | PrivateIdentifier) => Symbol | undefined;
|
|
7
7
|
line: (node: Node) => number;
|
|
8
8
|
endLine: (node: Node) => number;
|
|
9
9
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isIdentifier } from 'typescript/ast/is';
|
|
1
|
+
import { isIdentifier, isPrivateIdentifier } from 'typescript/ast/is';
|
|
2
2
|
export const readSource = (checker, sourceFile, file) => {
|
|
3
3
|
const identifiers = [];
|
|
4
4
|
walk(sourceFile, (node) => {
|
|
5
|
-
if (isIdentifier(node))
|
|
5
|
+
if (isIdentifier(node) || isPrivateIdentifier(node))
|
|
6
6
|
identifiers.push(node);
|
|
7
7
|
});
|
|
8
8
|
const symbols = identifiers.length > 0 ? checker.getSymbolAtLocation(identifiers) : [];
|
|
@@ -6,6 +6,8 @@ import { freshFacts } from './freshFacts.js';
|
|
|
6
6
|
import { fileEdges } from './imports.js';
|
|
7
7
|
import { moduleSpecifiers } from './specifiers.js';
|
|
8
8
|
import { countLines, readSource } from './source.js';
|
|
9
|
+
import { fileChains } from './chains.js';
|
|
10
|
+
import { classFacts } from './classes.js';
|
|
9
11
|
import { functionFacts, fileMembers } from './members.js';
|
|
10
12
|
export const typescriptAnalyzer = {
|
|
11
13
|
analyze(root, tsconfig, store) {
|
|
@@ -101,6 +103,8 @@ const analyzeProgram = (api, { program, checker }, files, fresh, done) => {
|
|
|
101
103
|
...fileEdges(source, resolver),
|
|
102
104
|
members: fileMembers(source),
|
|
103
105
|
functions: functionFacts(source),
|
|
106
|
+
classes: classFacts(source),
|
|
107
|
+
chains: fileChains(source, resolver),
|
|
104
108
|
};
|
|
105
109
|
analyzed.set(file, { specifiers: moduleSpecifiers(sourceFile), facts });
|
|
106
110
|
}
|
|
@@ -21,7 +21,7 @@ import { createRequire, registerHooks } from 'node:module';
|
|
|
21
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
|
-
import { ConfigError } from '../../domain/
|
|
24
|
+
import { ConfigError } from '../../domain/errors/configError.js';
|
|
25
25
|
export const configName = 'warp.yaml';
|
|
26
26
|
export const findConfig = (start, exists = existsSync) => {
|
|
27
27
|
for (let dir = start;; dir = dirname(dir)) {
|
package/dist/app/checkModel.js
CHANGED
|
@@ -19,11 +19,11 @@ const run = (rule, shared, settled) => {
|
|
|
19
19
|
if (rule.scope === 'project')
|
|
20
20
|
return rule.check({
|
|
21
21
|
...shared,
|
|
22
|
-
atFile: (file) => settled.atFile(file, rule.id, rule.defaults),
|
|
23
|
-
atModule: (module) => settled.atModule(module, rule.id, rule.defaults),
|
|
22
|
+
atFile: (file) => settled.atFile(file, rule.id, rule.defaults, rule.moduleOptions),
|
|
23
|
+
atModule: (module) => settled.atModule(module, rule.id, rule.defaults, rule.moduleOptions),
|
|
24
24
|
});
|
|
25
25
|
return shared.project.files.flatMap((file) => {
|
|
26
|
-
const { on, options } = settled.atFile(file.path, rule.id, rule.defaults);
|
|
26
|
+
const { on, options } = settled.atFile(file.path, rule.id, rule.defaults, rule.moduleOptions);
|
|
27
27
|
return on ? rule.check({ file, options }) : [];
|
|
28
28
|
});
|
|
29
29
|
};
|
package/dist/app/load.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { compileConfig } from '../domain/language/compile.js';
|
|
2
|
-
import { ConfigError } from '../domain/
|
|
2
|
+
import { ConfigError } from '../domain/errors/configError.js';
|
|
3
3
|
import { pluginRules, pluginRulesets, registry } from '../domain/rules/registry.js';
|
|
4
4
|
import { noOptions } from '../domain/rules/rule.js';
|
|
5
5
|
import { placeProblems } from './configLines.js';
|
|
@@ -11,6 +11,7 @@ export const loadConfigData = (raw, { builtins, file, plugins = {} } = {}) => {
|
|
|
11
11
|
rulesets: pluginRulesets(builtins ? { ...plugins, warp: builtins } : plugins),
|
|
12
12
|
ruleIds: builtins && new Set(rules.map((rule) => rule.id)),
|
|
13
13
|
options: Object.fromEntries(rules.map((rule) => [rule.id, rule.parseOptions ?? noOptions])),
|
|
14
|
+
moduleOptions: Object.fromEntries(rules.flatMap((rule) => (rule.moduleOptions ? [[rule.id, rule.moduleOptions]] : []))),
|
|
14
15
|
});
|
|
15
16
|
return { config, rules };
|
|
16
17
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FileFacts, ImportEdge } from '../domain/facts/facts.ts';
|
|
2
|
+
import type { Model, ModuleView } from '../domain/model/resolve.ts';
|
|
3
|
+
export declare const within: (module: ModuleView | undefined, holder: ModuleView) => boolean;
|
|
4
|
+
export declare const crossed: (from: ModuleView, to: ModuleView) => ModuleView | undefined;
|
|
5
|
+
export interface Use {
|
|
6
|
+
file: string;
|
|
7
|
+
edge: ImportEdge;
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const valueUses: (model: Model, files: readonly FileFacts[]) => Use[];
|
|
11
|
+
export interface LimitOptions {
|
|
12
|
+
max: number;
|
|
13
|
+
ignore: string[];
|
|
14
|
+
}
|
|
15
|
+
export declare const limitOptions: (defaults: LimitOptions) => (raw: Record<string, unknown>, at: string) => Partial<LimitOptions>;
|
|
16
|
+
export declare const ignored: (model: Model, ids: readonly string[]) => ModuleView[];
|
|
17
|
+
export declare const listed: (items: readonly string[]) => string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ConfigError } from '../domain/errors/configError.js';
|
|
2
|
+
import { wholeNumberOptions } from '../domain/rules/rule.js';
|
|
3
|
+
const chain = (module) => (module.parent ? [...chain(module.parent), module] : [module]);
|
|
4
|
+
export const within = (module, holder) => {
|
|
5
|
+
for (let at = module; at; at = at.parent)
|
|
6
|
+
if (at === holder)
|
|
7
|
+
return true;
|
|
8
|
+
return false;
|
|
9
|
+
};
|
|
10
|
+
export const crossed = (from, to) => {
|
|
11
|
+
const [source, target] = [chain(from), chain(to)];
|
|
12
|
+
const shared = target.findIndex((module, at) => module !== source[at]);
|
|
13
|
+
if (shared !== -1)
|
|
14
|
+
return target[shared];
|
|
15
|
+
return source.length > target.length ? to : undefined;
|
|
16
|
+
};
|
|
17
|
+
export const valueUses = (model, files) => {
|
|
18
|
+
const production = new Set(files.map((file) => file.path).filter((path) => !model.isTest(path)));
|
|
19
|
+
return files.flatMap(({ path, imports }) => production.has(path)
|
|
20
|
+
? imports
|
|
21
|
+
.filter((edge) => !edge.typeOnly && !edge.reExport && production.has(edge.to))
|
|
22
|
+
.flatMap((edge) => edge.names.filter((name) => name !== '*').map((name) => ({ file: path, edge, name })))
|
|
23
|
+
: []);
|
|
24
|
+
};
|
|
25
|
+
export const limitOptions = (defaults) => (raw, at) => {
|
|
26
|
+
const { ignore, ...rest } = raw;
|
|
27
|
+
const max = wholeNumberOptions({ max: defaults.max })(rest, at);
|
|
28
|
+
if (ignore === undefined)
|
|
29
|
+
return max;
|
|
30
|
+
if (!Array.isArray(ignore) || ignore.some((entry) => typeof entry !== 'string'))
|
|
31
|
+
throw new ConfigError(`${at}.ignore: expected a list of module names`);
|
|
32
|
+
return { ...max, ignore: ignore };
|
|
33
|
+
};
|
|
34
|
+
export const ignored = (model, ids) => model.modules.filter((module) => ids.includes(module.id));
|
|
35
|
+
export const listed = (items) => items.length === 1 ? items[0] : `${items.slice(0, -1).join(', ')} and ${items.at(-1)}`;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { components } from '../components.js';
|
|
2
|
+
import { wholeNumberOptions } from '../../domain/rules/rule.js';
|
|
3
|
+
import { group, withOrdinals } from '../shared.js';
|
|
4
|
+
const defaults = { maxLines: 100 };
|
|
5
|
+
export const classCohesion = {
|
|
6
|
+
id: 'class-cohesion',
|
|
7
|
+
defaults,
|
|
8
|
+
parseOptions: wholeNumberOptions(defaults),
|
|
9
|
+
check: ({ file, options }) => withOrdinals(file.classes).flatMap(([cls, name]) => {
|
|
10
|
+
const methods = statefulMethods(cls);
|
|
11
|
+
const judged = methods.flat().reduce((sum, member) => sum + member.endLine - member.line + 1, 0);
|
|
12
|
+
if (judged <= options.maxLines || methods.length <= 1)
|
|
13
|
+
return [];
|
|
14
|
+
const groups = methods.map(group);
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
rule: 'class-cohesion',
|
|
18
|
+
key: `class-cohesion:${file.path}:${name}`,
|
|
19
|
+
file: file.path,
|
|
20
|
+
line: cls.line,
|
|
21
|
+
value: groups.length,
|
|
22
|
+
limit: 1,
|
|
23
|
+
message: `Class ${cls.name} in ${file.path}:${cls.line} is ${cls.lines} lines, ${judged} of them in methods that use its state ` +
|
|
24
|
+
`(tripwire ${options.maxLines}), and its methods fall into ${groups.length} groups that share no fields or calls: ` +
|
|
25
|
+
`${groups.map(describe).join('; ')}. Each group can become its own class.`,
|
|
26
|
+
details: { lines: cls.lines, judgedLines: judged, tripwire: options.maxLines, groups },
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
const statefulMethods = (cls) => components(cls.members, ['function'])
|
|
32
|
+
.filter((members) => members.some((member) => member.kind === 'state'))
|
|
33
|
+
.map((members) => members.filter((member) => member.kind === 'function'));
|
|
34
|
+
const describe = (g) => `[${g.names.join(', ')}] (lines ${g.lines[0]}-${g.lines[1]})`;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { wholeNumberOptions } from '../../domain/rules/rule.js';
|
|
2
|
+
import { crossed, listed } from '../boundaries.js';
|
|
3
|
+
const id = 'export-consumers';
|
|
4
|
+
const defaults = { max: 3 };
|
|
5
|
+
export const exportConsumers = {
|
|
6
|
+
id,
|
|
7
|
+
scope: 'project',
|
|
8
|
+
defaults,
|
|
9
|
+
parseOptions: wholeNumberOptions(defaults),
|
|
10
|
+
check: ({ model, project, atModule }) => {
|
|
11
|
+
const exports = new Map();
|
|
12
|
+
for (const { file, line, target } of uses(model, project.files)) {
|
|
13
|
+
const consumer = crossed(model.moduleOf(target.file), model.moduleOf(file));
|
|
14
|
+
if (!consumer)
|
|
15
|
+
continue;
|
|
16
|
+
const name = target.member === undefined ? target.name : `${target.name}#${target.member}`;
|
|
17
|
+
const key = `${target.file}#${name}`;
|
|
18
|
+
const found = exports.get(key) ?? { file: target.file, declared: target.name, name, consumers: new Map() };
|
|
19
|
+
exports.set(key, found);
|
|
20
|
+
found.consumers.set(consumer, [...(found.consumers.get(consumer) ?? []), { file, line }]);
|
|
21
|
+
}
|
|
22
|
+
const lines = new Map(project.files.map((file) => [file.path, file.members.members]));
|
|
23
|
+
return [...exports.values()].flatMap((found) => {
|
|
24
|
+
const module = model.moduleOf(found.file);
|
|
25
|
+
const { on, options } = atModule(module);
|
|
26
|
+
if (!on || found.consumers.size <= options.max)
|
|
27
|
+
return [];
|
|
28
|
+
const line = lines.get(found.file).find((member) => member.name === found.declared)?.line ?? 1;
|
|
29
|
+
return [finding(found, module, line, options.max)];
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
const uses = (model, files) => {
|
|
34
|
+
const production = new Set(files.map((file) => file.path).filter((path) => !model.isTest(path)));
|
|
35
|
+
return files
|
|
36
|
+
.filter(({ path }) => production.has(path))
|
|
37
|
+
.flatMap(({ path, chains }) => chains.flatMap(({ line, root, hops }) => {
|
|
38
|
+
const target = hops[0] ?? (root?.name === '*' ? undefined : root);
|
|
39
|
+
return target && production.has(target.file) ? [{ file: path, line, target }] : [];
|
|
40
|
+
}));
|
|
41
|
+
};
|
|
42
|
+
const finding = ({ file, name, consumers }, module, line, max) => {
|
|
43
|
+
const boundaries = [...consumers.keys()].map((consumer) => consumer.id).sort();
|
|
44
|
+
return {
|
|
45
|
+
rule: id,
|
|
46
|
+
key: `${id}:${file}#${name}`,
|
|
47
|
+
file,
|
|
48
|
+
line,
|
|
49
|
+
value: consumers.size,
|
|
50
|
+
limit: max,
|
|
51
|
+
message: `${name}, exported by ${module.id}, is used across ${consumers.size} boundaries: ${listed(boundaries)}. ` +
|
|
52
|
+
`Its limit here is ${max}. Give its consumers one way in, or move it to a module meant to be shared and ` +
|
|
53
|
+
`turn ${id} off there.`,
|
|
54
|
+
sites: [...consumers.values()].map(([first]) => first).toSorted((a, b) => a.file.localeCompare(b.file)),
|
|
55
|
+
parts: boundaries,
|
|
56
|
+
details: {
|
|
57
|
+
module: module.id,
|
|
58
|
+
name,
|
|
59
|
+
consumers: [...consumers].map(([consumer, sites]) => ({
|
|
60
|
+
module: consumer.id,
|
|
61
|
+
files: [...new Set(sites.map((site) => site.file))],
|
|
62
|
+
})),
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ignored, limitOptions, listed, valueUses, within } from '../boundaries.js';
|
|
2
|
+
const id = 'export-spread';
|
|
3
|
+
const defaults = { max: 2, ignore: [] };
|
|
4
|
+
export const exportSpread = {
|
|
5
|
+
id,
|
|
6
|
+
scope: 'project',
|
|
7
|
+
defaults,
|
|
8
|
+
moduleOptions: ['ignore'],
|
|
9
|
+
parseOptions: limitOptions(defaults),
|
|
10
|
+
check: ({ model, project, atModule }) => {
|
|
11
|
+
const uses = valueUses(model, project.files);
|
|
12
|
+
return model.modules.flatMap((module) => {
|
|
13
|
+
const { on, options } = atModule(module);
|
|
14
|
+
if (!on)
|
|
15
|
+
return [];
|
|
16
|
+
const skip = ignored(model, options.ignore);
|
|
17
|
+
return [...spreads(model, module, uses, skip).values()]
|
|
18
|
+
.filter((spread) => spread.children.size > options.max)
|
|
19
|
+
.map((spread) => finding(spread, module, model.moduleOf(spread.file), options.max));
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
const outside = (target, module, skip) => !within(target, module) && !skip.some((holder) => within(target, holder));
|
|
24
|
+
const spreads = (model, module, uses, skip) => {
|
|
25
|
+
const found = new Map();
|
|
26
|
+
for (const { file, edge, name } of uses) {
|
|
27
|
+
if (!outside(model.moduleOf(edge.to), module, skip))
|
|
28
|
+
continue;
|
|
29
|
+
const child = module.children.find((candidate) => !candidate.test && within(model.moduleOf(file), candidate));
|
|
30
|
+
if (!child)
|
|
31
|
+
continue;
|
|
32
|
+
const key = `${edge.to}#${name}`;
|
|
33
|
+
const spread = found.get(key) ?? { file: edge.to, name, children: new Map() };
|
|
34
|
+
found.set(key, spread);
|
|
35
|
+
spread.children.set(child, [...(spread.children.get(child) ?? []), { file, line: edge.line }]);
|
|
36
|
+
}
|
|
37
|
+
return found;
|
|
38
|
+
};
|
|
39
|
+
const finding = ({ file, name, children }, module, exporter, max) => {
|
|
40
|
+
const named = [...children.keys()].map((child) => child.id).sort();
|
|
41
|
+
const sites = [...children.values()].map(([first]) => first).toSorted((a, b) => a.file.localeCompare(b.file));
|
|
42
|
+
return {
|
|
43
|
+
rule: id,
|
|
44
|
+
key: `${id}:${module.id}:${file}#${name}`,
|
|
45
|
+
file: sites[0].file,
|
|
46
|
+
line: sites[0].line,
|
|
47
|
+
value: children.size,
|
|
48
|
+
limit: max,
|
|
49
|
+
message: `${children.size} of ${module.id}'s modules use ${name} from ${exporter.id}: ${listed(named)}. ` +
|
|
50
|
+
`Its limit here is ${max}. Give the use to the one module that should own it and let the others go through ` +
|
|
51
|
+
`that module, or name it in ${id}'s ignore on ${module.id} if it is meant to be shared.`,
|
|
52
|
+
sites,
|
|
53
|
+
parts: named,
|
|
54
|
+
details: {
|
|
55
|
+
module: module.id,
|
|
56
|
+
exporter: exporter.id,
|
|
57
|
+
file,
|
|
58
|
+
name,
|
|
59
|
+
children: [...children].map(([child, uses]) => ({
|
|
60
|
+
module: child.id,
|
|
61
|
+
files: [...new Set(uses.map((use) => use.file))],
|
|
62
|
+
})),
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|