@yearofthedan/warp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +97 -2
- package/dist/analysis/complexity.d.ts +4 -0
- package/dist/analysis/complexity.js +84 -0
- package/dist/analysis/folders.d.ts +2 -0
- package/dist/analysis/folders.js +15 -0
- package/dist/analysis/imports.d.ts +13 -0
- package/dist/analysis/imports.js +135 -0
- package/dist/analysis/members.d.ts +4 -0
- package/dist/analysis/members.js +190 -0
- package/dist/analysis/source.d.ts +13 -0
- package/dist/analysis/source.js +29 -0
- package/dist/analysis/specifiers.d.ts +2 -0
- package/dist/analysis/specifiers.js +6 -0
- package/dist/analysis/typescript.d.ts +2 -0
- package/dist/analysis/typescript.js +105 -0
- package/dist/app/check.d.ts +23 -0
- package/dist/app/check.js +15 -0
- package/dist/app/checkModel.d.ts +5 -0
- package/dist/app/checkModel.js +30 -0
- package/dist/app/diff.d.ts +25 -0
- package/dist/app/diff.js +53 -0
- package/dist/app/facts.d.ts +7 -0
- package/dist/app/facts.js +55 -0
- package/dist/app/load.d.ts +13 -0
- package/dist/app/load.js +76 -0
- package/dist/app/ports.d.ts +41 -0
- package/dist/app/ports.js +1 -0
- package/dist/app/unclassified.d.ts +5 -0
- package/dist/app/unclassified.js +13 -0
- package/dist/builtins/cognitive-complexity/cognitiveComplexity.d.ts +6 -0
- package/dist/builtins/cognitive-complexity/cognitiveComplexity.js +26 -0
- package/dist/builtins/components.d.ts +2 -0
- package/dist/builtins/components.js +20 -0
- package/dist/builtins/dependency-direction/dependencyDirection.d.ts +2 -0
- package/dist/builtins/dependency-direction/dependencyDirection.js +65 -0
- package/dist/builtins/file-cohesion/fileCohesion.d.ts +6 -0
- package/dist/builtins/file-cohesion/fileCohesion.js +30 -0
- package/dist/builtins/function-cohesion/functionCohesion.d.ts +6 -0
- package/dist/builtins/function-cohesion/functionCohesion.js +30 -0
- package/dist/builtins/index.d.ts +4 -0
- package/dist/builtins/index.js +30 -0
- package/dist/builtins/loops.d.ts +25 -0
- package/dist/builtins/loops.js +64 -0
- package/dist/builtins/module-cycle/moduleCycle.d.ts +3 -0
- package/dist/builtins/module-cycle/moduleCycle.js +61 -0
- package/dist/builtins/module-externals/moduleExternals.d.ts +2 -0
- package/dist/builtins/module-externals/moduleExternals.js +41 -0
- package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +2 -0
- package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.js +47 -0
- package/dist/builtins/no-cycle/noCycle.d.ts +3 -0
- package/dist/builtins/no-cycle/noCycle.js +39 -0
- package/dist/builtins/no-double-export/noDoubleExport.d.ts +2 -0
- package/dist/builtins/no-double-export/noDoubleExport.js +38 -0
- package/dist/builtins/packages.d.ts +2 -0
- package/dist/builtins/packages.js +5 -0
- package/dist/builtins/place.d.ts +2 -0
- package/dist/builtins/place.js +6 -0
- package/dist/builtins/shared.d.ts +10 -0
- package/dist/builtins/shared.js +13 -0
- package/dist/builtins/unused-declaration/declarations.d.ts +32 -0
- package/dist/builtins/unused-declaration/declarations.js +26 -0
- package/dist/builtins/unused-declaration/empty.d.ts +3 -0
- package/dist/builtins/unused-declaration/empty.js +22 -0
- package/dist/builtins/unused-declaration/grants.d.ts +3 -0
- package/dist/builtins/unused-declaration/grants.js +48 -0
- package/dist/builtins/unused-declaration/surfaces.d.ts +3 -0
- package/dist/builtins/unused-declaration/surfaces.js +57 -0
- package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +2 -0
- package/dist/builtins/unused-declaration/unusedDeclaration.js +14 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +168 -0
- package/dist/config/loadConfig.d.ts +7 -0
- package/dist/config/loadConfig.js +53 -0
- package/dist/config/toolDigest.d.ts +1 -0
- package/dist/config/toolDigest.js +16 -0
- package/dist/git/cache.d.ts +3 -0
- package/dist/git/cache.js +41 -0
- package/dist/git/repository.d.ts +3 -0
- package/dist/git/repository.js +81 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/model/archetype/builder.d.ts +183 -0
- package/dist/model/archetype/builder.js +85 -0
- package/dist/model/archetype/data.d.ts +92 -0
- package/dist/model/archetype/data.js +1 -0
- package/dist/model/archetype/draft.d.ts +37 -0
- package/dist/model/archetype/draft.js +275 -0
- package/dist/model/archetype/reach.d.ts +35 -0
- package/dist/model/archetype/reach.js +209 -0
- package/dist/model/archetype/references.d.ts +13 -0
- package/dist/model/archetype/references.js +166 -0
- package/dist/model/archetype/requirements.d.ts +5 -0
- package/dist/model/archetype/requirements.js +69 -0
- package/dist/model/archetype/resolve.d.ts +30 -0
- package/dist/model/archetype/resolve.js +311 -0
- package/dist/model/archetype/rulesets.d.ts +4 -0
- package/dist/model/archetype/rulesets.js +115 -0
- package/dist/model/archetype/settings.d.ts +21 -0
- package/dist/model/archetype/settings.js +64 -0
- package/dist/model/archetype/shared.d.ts +5 -0
- package/dist/model/archetype/shared.js +4 -0
- package/dist/model/configError.d.ts +3 -0
- package/dist/model/configError.js +3 -0
- package/dist/model/facts.d.ts +57 -0
- package/dist/model/facts.js +1 -0
- package/dist/reporters/claudeHook.d.ts +2 -0
- package/dist/reporters/claudeHook.js +10 -0
- package/dist/reporters/index.d.ts +2 -0
- package/dist/reporters/index.js +9 -0
- package/dist/reporters/json.d.ts +2 -0
- package/dist/reporters/json.js +4 -0
- package/dist/reporters/model.d.ts +4 -0
- package/dist/reporters/model.js +60 -0
- package/dist/reporters/omp.d.ts +2 -0
- package/dist/reporters/omp.js +8 -0
- package/dist/reporters/reporter.d.ts +26 -0
- package/dist/reporters/reporter.js +14 -0
- package/dist/reporters/sarif.d.ts +2 -0
- package/dist/reporters/sarif.js +44 -0
- package/dist/reporters/text.d.ts +6 -0
- package/dist/reporters/text.js +35 -0
- package/dist/rules/diff.d.ts +10 -0
- package/dist/rules/diff.js +31 -0
- package/dist/rules/registry.d.ts +5 -0
- package/dist/rules/registry.js +33 -0
- package/dist/rules/rule.d.ts +59 -0
- package/dist/rules/rule.js +18 -0
- package/integrations/omp/warp.ts +40 -0
- package/package.json +44 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { components } from '../components.js';
|
|
2
|
+
import { wholeNumberOptions } from '../../rules/rule.js';
|
|
3
|
+
import { group, withOrdinals } from '../shared.js';
|
|
4
|
+
const defaults = { maxLines: 40 };
|
|
5
|
+
export const functionCohesion = {
|
|
6
|
+
id: 'function-cohesion',
|
|
7
|
+
defaults,
|
|
8
|
+
parseOptions: wholeNumberOptions(defaults),
|
|
9
|
+
check: ({ file, options }) => withOrdinals(file.functions).flatMap(([fn, name]) => {
|
|
10
|
+
if (fn.lines <= options.maxLines)
|
|
11
|
+
return [];
|
|
12
|
+
const groups = components(fn.statements, ['code']).map(group);
|
|
13
|
+
if (groups.length <= 1)
|
|
14
|
+
return [];
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
rule: 'function-cohesion',
|
|
18
|
+
key: `function-cohesion:${file.path}:${name}`,
|
|
19
|
+
file: file.path,
|
|
20
|
+
line: fn.line,
|
|
21
|
+
value: groups.length,
|
|
22
|
+
limit: 1,
|
|
23
|
+
message: `Function ${fn.name} in ${file.path}:${fn.line} is ${fn.lines} lines (tripwire ${options.maxLines}) and its statements fall into ` +
|
|
24
|
+
`${groups.length} groups that share no locals: ${groups.map((g) => `lines ${g.lines[0]}-${g.lines[1]}`).join('; ')}. ` +
|
|
25
|
+
'Each group can become its own function.',
|
|
26
|
+
details: { lines: fn.lines, tripwire: options.maxLines, groups },
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Rule } from '../rules/rule.ts';
|
|
2
|
+
export declare const builtinRules: readonly Rule[];
|
|
3
|
+
export declare const builtinRegistry: ReadonlyMap<string, Rule>;
|
|
4
|
+
export declare const unclassifiedDocs = "https://github.com/yearofthedan/warp/blob/main/src/builtins/unclassified/README.md";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { registry } from '../rules/registry.js';
|
|
2
|
+
import { cognitiveComplexity } from './cognitive-complexity/cognitiveComplexity.js';
|
|
3
|
+
import { dependencyDirection } from './dependency-direction/dependencyDirection.js';
|
|
4
|
+
import { fileCohesion } from './file-cohesion/fileCohesion.js';
|
|
5
|
+
import { functionCohesion } from './function-cohesion/functionCohesion.js';
|
|
6
|
+
import { moduleCycle } from './module-cycle/moduleCycle.js';
|
|
7
|
+
import { moduleExternals } from './module-externals/moduleExternals.js';
|
|
8
|
+
import { noCrossModuleReexport } from './no-cross-module-reexport/noCrossModuleReexport.js';
|
|
9
|
+
import { noCycle } from './no-cycle/noCycle.js';
|
|
10
|
+
import { noDoubleExport } from './no-double-export/noDoubleExport.js';
|
|
11
|
+
import { unusedDeclaration } from './unused-declaration/unusedDeclaration.js';
|
|
12
|
+
export const builtinRules = [
|
|
13
|
+
dependencyDirection,
|
|
14
|
+
moduleExternals,
|
|
15
|
+
noDoubleExport,
|
|
16
|
+
noCrossModuleReexport,
|
|
17
|
+
noCycle,
|
|
18
|
+
moduleCycle,
|
|
19
|
+
fileCohesion,
|
|
20
|
+
functionCohesion,
|
|
21
|
+
cognitiveComplexity,
|
|
22
|
+
unusedDeclaration,
|
|
23
|
+
];
|
|
24
|
+
const source = 'https://github.com/yearofthedan/warp/blob/main';
|
|
25
|
+
const documented = (rule) => ({
|
|
26
|
+
...rule,
|
|
27
|
+
docs: `${source}/src/builtins/${rule.id}/README.md`,
|
|
28
|
+
});
|
|
29
|
+
export const builtinRegistry = registry(builtinRules.map(documented));
|
|
30
|
+
export const unclassifiedDocs = `${source}/src/builtins/unclassified/README.md`;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ImportEdge } from '../model/facts.ts';
|
|
2
|
+
export interface CycleOptions {
|
|
3
|
+
types: 'count' | 'ignore';
|
|
4
|
+
}
|
|
5
|
+
export declare const cycleDefaults: CycleOptions;
|
|
6
|
+
export declare const cycleOptions: (raw: Record<string, unknown>, at: string) => Partial<CycleOptions>;
|
|
7
|
+
export type Graph<T> = Map<string, Map<string, T[]>>;
|
|
8
|
+
export declare const link: <T>(graph: Graph<T>, from: string, to: string, values: readonly T[]) => void;
|
|
9
|
+
export interface Loop<T> {
|
|
10
|
+
members: string[];
|
|
11
|
+
edges: {
|
|
12
|
+
from: string;
|
|
13
|
+
to: string;
|
|
14
|
+
links: T[];
|
|
15
|
+
}[];
|
|
16
|
+
}
|
|
17
|
+
export declare const loops: <T>(graph: Graph<T>) => Loop<T>[];
|
|
18
|
+
export declare const named: (items: readonly string[]) => string;
|
|
19
|
+
export declare const uniqueSites: <S extends {
|
|
20
|
+
file: string;
|
|
21
|
+
line: number;
|
|
22
|
+
}>(sites: readonly S[]) => S[];
|
|
23
|
+
export declare const listed: (items: readonly string[], most?: number) => string;
|
|
24
|
+
export declare const importsByTarget: (file: string, imports: readonly ImportEdge[]) => [string, ImportEdge[]][];
|
|
25
|
+
export declare const counts: (edges: readonly ImportEdge[], options: CycleOptions) => boolean;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ConfigError } from '../model/configError.js';
|
|
2
|
+
export const cycleDefaults = { types: 'count' };
|
|
3
|
+
export const cycleOptions = (raw, at) => {
|
|
4
|
+
const unknown = Object.keys(raw).filter((key) => key !== 'types');
|
|
5
|
+
if (unknown.length > 0)
|
|
6
|
+
throw new ConfigError(`${at}: unknown key ${unknown.join(', ')}`);
|
|
7
|
+
if (raw.types !== undefined && raw.types !== 'count' && raw.types !== 'ignore')
|
|
8
|
+
throw new ConfigError(`${at}.types: expected 'count' or 'ignore'`);
|
|
9
|
+
return raw;
|
|
10
|
+
};
|
|
11
|
+
export const link = (graph, from, to, values) => {
|
|
12
|
+
const targets = graph.get(from) ?? new Map();
|
|
13
|
+
graph.set(from, targets);
|
|
14
|
+
targets.set(to, [...(targets.get(to) ?? []), ...values]);
|
|
15
|
+
if (!graph.has(to))
|
|
16
|
+
graph.set(to, new Map());
|
|
17
|
+
};
|
|
18
|
+
const visit = (walk, node) => {
|
|
19
|
+
const { graph, index, low, component, stack } = walk;
|
|
20
|
+
index.set(node, index.size);
|
|
21
|
+
low.set(node, index.get(node));
|
|
22
|
+
stack.push(node);
|
|
23
|
+
for (const next of graph.get(node).keys()) {
|
|
24
|
+
if (!index.has(next))
|
|
25
|
+
visit(walk, next);
|
|
26
|
+
if (!component.has(next))
|
|
27
|
+
low.set(node, Math.min(low.get(node), low.get(next)));
|
|
28
|
+
}
|
|
29
|
+
if (low.get(node) !== index.get(node))
|
|
30
|
+
return;
|
|
31
|
+
for (let member = stack.pop();; member = stack.pop()) {
|
|
32
|
+
component.set(member, index.get(node));
|
|
33
|
+
if (member === node)
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const components = (graph) => {
|
|
38
|
+
const walk = { graph, index: new Map(), low: new Map(), component: new Map(), stack: [] };
|
|
39
|
+
for (const node of graph.keys())
|
|
40
|
+
if (!walk.index.has(node))
|
|
41
|
+
visit(walk, node);
|
|
42
|
+
return walk.component;
|
|
43
|
+
};
|
|
44
|
+
export const loops = (graph) => {
|
|
45
|
+
const component = components(graph);
|
|
46
|
+
const found = new Map();
|
|
47
|
+
for (const [from, targets] of graph)
|
|
48
|
+
for (const [to, links] of targets) {
|
|
49
|
+
const at = component.get(from);
|
|
50
|
+
if (component.get(to) === at)
|
|
51
|
+
found.set(at, [...(found.get(at) ?? []), { from, to, links }]);
|
|
52
|
+
}
|
|
53
|
+
return [...found.values()].map((edges) => ({
|
|
54
|
+
members: [...new Set(edges.map(({ from }) => from))].sort(),
|
|
55
|
+
edges: edges.toSorted((a, b) => a.from.localeCompare(b.from) || a.to.localeCompare(b.to)),
|
|
56
|
+
}));
|
|
57
|
+
};
|
|
58
|
+
export const named = (items) => `${items.slice(0, -1).join(', ')} and ${items.at(-1)}`;
|
|
59
|
+
export const uniqueSites = (sites) => [
|
|
60
|
+
...new Map(sites.map((site) => [`${site.file}:${site.line}`, site])).values(),
|
|
61
|
+
];
|
|
62
|
+
export const listed = (items, most = 5) => items.length <= most ? items.join(', ') : `${items.slice(0, most).join(', ')}, and ${items.length - most} more`;
|
|
63
|
+
export const importsByTarget = (file, imports) => [...Map.groupBy(imports, (edge) => edge.via ?? edge.to)].filter(([to]) => to !== file);
|
|
64
|
+
export const counts = (edges, options) => options.types === 'count' || edges.some((edge) => !edge.typeOnly);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { counts, cycleDefaults, cycleOptions, importsByTarget, link, listed, loops, named, uniqueSites, } from '../loops.js';
|
|
2
|
+
const id = 'module-cycle';
|
|
3
|
+
const chain = (module) => (module.parent ? [...chain(module.parent), module] : [module]);
|
|
4
|
+
const siblings = (model, from, to) => {
|
|
5
|
+
const source = chain(model.moduleOf(from));
|
|
6
|
+
const target = chain(model.moduleOf(to));
|
|
7
|
+
const split = source.findIndex((module, at) => module !== target[at]);
|
|
8
|
+
const [importer, imported] = [source[split], target[split]];
|
|
9
|
+
return importer && imported ? [importer, imported] : undefined;
|
|
10
|
+
};
|
|
11
|
+
export const moduleCycle = {
|
|
12
|
+
id,
|
|
13
|
+
scope: 'project',
|
|
14
|
+
defaults: cycleDefaults,
|
|
15
|
+
parseOptions: cycleOptions,
|
|
16
|
+
check: ({ model, project, atModule }) => {
|
|
17
|
+
const files = new Set(project.files.map((file) => file.path));
|
|
18
|
+
const modules = new Map(model.modules.map((module) => [module.id, module]));
|
|
19
|
+
const graph = new Map();
|
|
20
|
+
for (const { path: file, imports } of project.files) {
|
|
21
|
+
if (model.isTest(file))
|
|
22
|
+
continue;
|
|
23
|
+
for (const [to, edges] of importsByTarget(file, imports)) {
|
|
24
|
+
const crossed = files.has(to) ? siblings(model, file, to) : undefined;
|
|
25
|
+
if (crossed && counts(edges, atModule(crossed[0]).options))
|
|
26
|
+
link(graph, crossed[0].id, crossed[1].id, [{ file, to, edges }]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return loops(graph).flatMap((loop) => {
|
|
30
|
+
const reported = loop.edges.filter(({ from }) => atModule(modules.get(from)).on);
|
|
31
|
+
if (reported.length === 0)
|
|
32
|
+
return [];
|
|
33
|
+
return [loopFinding(loop.members, reported, modules.get(loop.members[0]).parent.id)];
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const loopFinding = (members, reported, parent) => {
|
|
38
|
+
const links = reported.flatMap(({ links }) => links);
|
|
39
|
+
const sites = uniqueSites(links.map(({ file, edges: [first] }) => ({ file, line: first.line }))).toSorted((a, b) => a.file.localeCompare(b.file) || a.line - b.line);
|
|
40
|
+
const crossings = reported.map(({ from, to, links: [first, ...rest] }) => {
|
|
41
|
+
const more = rest.length === 0 ? '' : `, and ${rest.length} other import${rest.length === 1 ? '' : 's'}`;
|
|
42
|
+
return `${from} -> ${to} (${first.file}:${first.edges[0].line} imports ${first.to}${more})`;
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
rule: id,
|
|
46
|
+
key: `${id}:${members.join('+')}`,
|
|
47
|
+
file: sites[0].file,
|
|
48
|
+
line: sites[0].line,
|
|
49
|
+
value: links.length,
|
|
50
|
+
message: `${named(members)} import each other in a loop: ${listed(crossings)}. ` +
|
|
51
|
+
'Break it at one of these imports: move what both sides need into a module they both use, or turn ' +
|
|
52
|
+
'module-cycle off on the module that holds them if they are meant to use each other.',
|
|
53
|
+
sites,
|
|
54
|
+
parts: reported.map(({ from, to }) => `${from}->${to}`),
|
|
55
|
+
details: {
|
|
56
|
+
module: parent,
|
|
57
|
+
modules: members,
|
|
58
|
+
imports: links.map(({ file, to, edges }) => ({ from: file, to, edges })),
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { listsPackage } from '../packages.js';
|
|
2
|
+
import { place } from '../place.js';
|
|
3
|
+
import { noOptions } from '../../rules/rule.js';
|
|
4
|
+
const id = 'module-externals';
|
|
5
|
+
export const moduleExternals = {
|
|
6
|
+
id,
|
|
7
|
+
scope: 'project',
|
|
8
|
+
parseOptions: noOptions,
|
|
9
|
+
check: ({ model, verdicts, project, atFile }) => project.files.flatMap((file) => {
|
|
10
|
+
const reach = verdicts.reach(file.path);
|
|
11
|
+
if (!reach || !atFile(file.path).on)
|
|
12
|
+
return [];
|
|
13
|
+
const packages = reach.filter((grant) => grant.package !== undefined);
|
|
14
|
+
const byPackage = Map.groupBy(file.externals, (external) => external.package);
|
|
15
|
+
return [...byPackage].flatMap(([name, edges]) => {
|
|
16
|
+
const outside = edges.filter((edge) => !packages.some((grant) => listsPackage(grant.package, edge)));
|
|
17
|
+
return outside.length === 0 ? [] : [refused(model, file.path, name, outside, packages)];
|
|
18
|
+
});
|
|
19
|
+
}),
|
|
20
|
+
};
|
|
21
|
+
const refused = (model, from, name, edges, packages) => {
|
|
22
|
+
const module = model.moduleOf(from);
|
|
23
|
+
const owner = module.name ?? module.id;
|
|
24
|
+
const names = [...new Set(edges.flatMap((edge) => edge.names))];
|
|
25
|
+
const specifiers = [...new Set(edges.map((edge) => edge.specifier))];
|
|
26
|
+
const imports = edges.every((edge) => edge.typeOnly) ? 'imports types from' : 'imports';
|
|
27
|
+
const uses = Map.groupBy(packages, (grant) => grant.owner);
|
|
28
|
+
const lists = uses.size > 0
|
|
29
|
+
? `: ${[...uses].map(([by, grants]) => `${by} uses ${grants.map((grant) => grant.ref).join(', ')}`).join('; ')}`
|
|
30
|
+
: ': no using in its reach lists a package';
|
|
31
|
+
return {
|
|
32
|
+
rule: id,
|
|
33
|
+
key: `${id}:${from}->${name}`,
|
|
34
|
+
file: from,
|
|
35
|
+
line: edges[0].line,
|
|
36
|
+
value: Math.max(1, names.length),
|
|
37
|
+
message: `${from} (${place(model, from, module)}) ${imports} ${specifiers.join(', ')}, a package outside ${owner}'s reach${lists}. ` +
|
|
38
|
+
`Add pkg('${name}') to a using if the dependency is intended, or move the code.`,
|
|
39
|
+
details: { from: module.id, package: name, edges },
|
|
40
|
+
};
|
|
41
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { place } from '../place.js';
|
|
2
|
+
import { noOptions } from '../../rules/rule.js';
|
|
3
|
+
const id = 'no-cross-module-reexport';
|
|
4
|
+
export const noCrossModuleReexport = {
|
|
5
|
+
id,
|
|
6
|
+
scope: 'project',
|
|
7
|
+
parseOptions: noOptions,
|
|
8
|
+
check: ({ model, project, atFile }) => project.files.flatMap((file) => {
|
|
9
|
+
const module = model.moduleOf(file.path);
|
|
10
|
+
if (!module || !atFile(file.path).on)
|
|
11
|
+
return [];
|
|
12
|
+
const reExports = file.imports.filter((edge) => edge.reExport);
|
|
13
|
+
return [...Map.groupBy(reExports, (edge) => edge.via ?? edge.to)].flatMap(([to, edges]) => {
|
|
14
|
+
const target = model.moduleOf(to);
|
|
15
|
+
if (!target || owns(module, target))
|
|
16
|
+
return [];
|
|
17
|
+
return [passedThrough(model, module, target, to, edges)];
|
|
18
|
+
});
|
|
19
|
+
}),
|
|
20
|
+
};
|
|
21
|
+
const owns = (module, target) => {
|
|
22
|
+
for (let at = target; at; at = at.parent)
|
|
23
|
+
if (at === module)
|
|
24
|
+
return true;
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
27
|
+
const passedThrough = (model, module, target, to, edges) => {
|
|
28
|
+
const from = edges[0].from;
|
|
29
|
+
const names = [...new Set(edges.flatMap((edge) => edge.names))];
|
|
30
|
+
const what = names.length > 0 ? names.join(', ') : 'everything';
|
|
31
|
+
return {
|
|
32
|
+
rule: id,
|
|
33
|
+
key: `${id}:${from}->${to}`,
|
|
34
|
+
file: from,
|
|
35
|
+
line: edges[0].line,
|
|
36
|
+
value: Math.max(1, names.length),
|
|
37
|
+
message: `${from} (${place(model, from, module)}) re-exports ${what} from ${to} (${place(model, to, target)}), so its ` +
|
|
38
|
+
`importers reach ${target.id} through ${module.id}. Have importers that need it use ${target.id} directly, give ` +
|
|
39
|
+
`${module.id} its own type that hides it, or move it into ${module.id} if ${module.id} owns it.`,
|
|
40
|
+
details: {
|
|
41
|
+
from: module.id,
|
|
42
|
+
to: target.id,
|
|
43
|
+
names,
|
|
44
|
+
edges,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { counts, cycleDefaults, cycleOptions, importsByTarget, link, listed, loops, named, uniqueSites, } from '../loops.js';
|
|
2
|
+
const id = 'no-cycle';
|
|
3
|
+
export const noCycle = {
|
|
4
|
+
id,
|
|
5
|
+
scope: 'project',
|
|
6
|
+
defaults: cycleDefaults,
|
|
7
|
+
parseOptions: cycleOptions,
|
|
8
|
+
check: ({ project, atFile }) => {
|
|
9
|
+
const files = new Set(project.files.map((file) => file.path));
|
|
10
|
+
const graph = new Map();
|
|
11
|
+
for (const { path: file, imports } of project.files) {
|
|
12
|
+
const { options } = atFile(file);
|
|
13
|
+
for (const [to, edges] of importsByTarget(file, imports))
|
|
14
|
+
if (files.has(to) && counts(edges, options))
|
|
15
|
+
link(graph, file, to, edges);
|
|
16
|
+
}
|
|
17
|
+
return loops(graph).flatMap(({ members, edges }) => {
|
|
18
|
+
const reported = edges.filter(({ from }) => atFile(from).on);
|
|
19
|
+
if (reported.length === 0)
|
|
20
|
+
return [];
|
|
21
|
+
const sites = reported.map(({ from, links: [first] }) => ({ file: from, line: first.line }));
|
|
22
|
+
const who = members.length <= 3 ? named(members) : `${members.length} files`;
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
rule: id,
|
|
26
|
+
key: `${id}:${members.join('+')}`,
|
|
27
|
+
file: sites[0].file,
|
|
28
|
+
line: sites[0].line,
|
|
29
|
+
value: reported.length,
|
|
30
|
+
message: `${who} import each other in a loop: ${listed(sites.map(({ file, line }, at) => `${file}:${line} imports ${reported[at].to}`))}. ` +
|
|
31
|
+
'Break it at one of these imports: move what both need into a file they both import, or invert one import.',
|
|
32
|
+
sites: uniqueSites(sites),
|
|
33
|
+
parts: reported.map(({ from, to }) => `${from}->${to}`),
|
|
34
|
+
details: { files: members, edges: reported.flatMap(({ links }) => links) },
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { place } from '../place.js';
|
|
2
|
+
import { noOptions } from '../../rules/rule.js';
|
|
3
|
+
const id = 'no-double-export';
|
|
4
|
+
const exposingBoth = (verdicts, module, from, to) => {
|
|
5
|
+
for (let at = module; at; at = at.parent)
|
|
6
|
+
if (verdicts.surface(at).has(from) && verdicts.surface(at).has(to))
|
|
7
|
+
return at;
|
|
8
|
+
return undefined;
|
|
9
|
+
};
|
|
10
|
+
export const noDoubleExport = {
|
|
11
|
+
id,
|
|
12
|
+
scope: 'project',
|
|
13
|
+
parseOptions: noOptions,
|
|
14
|
+
check: ({ model, verdicts, project, atFile }) => project.files.flatMap((file) => {
|
|
15
|
+
const module = model.moduleOf(file.path);
|
|
16
|
+
if (!module || !atFile(file.path).on)
|
|
17
|
+
return [];
|
|
18
|
+
const reExports = file.imports.filter((edge) => edge.reExport);
|
|
19
|
+
return [...Map.groupBy(reExports, (edge) => edge.via ?? edge.to)].flatMap(([to, edges]) => {
|
|
20
|
+
const surface = to === file.path ? undefined : exposingBoth(verdicts, module, file.path, to);
|
|
21
|
+
return surface ? [doubled(model, surface, to, edges)] : [];
|
|
22
|
+
});
|
|
23
|
+
}),
|
|
24
|
+
};
|
|
25
|
+
const doubled = (model, module, to, edges) => {
|
|
26
|
+
const from = edges[0].from;
|
|
27
|
+
const names = [...new Set(edges.flatMap((edge) => edge.names))];
|
|
28
|
+
return {
|
|
29
|
+
rule: id,
|
|
30
|
+
key: `${id}:${from}->${to}`,
|
|
31
|
+
file: from,
|
|
32
|
+
line: edges[0].line,
|
|
33
|
+
value: Math.max(1, names.length),
|
|
34
|
+
message: `${from} (${place(model, from)}) re-exports ${to} (${place(model, to)}), and ${module.id} exposes both, ` +
|
|
35
|
+
`so importers have two ways to the same names. Keep one: stop re-exporting it, or stop exposing it.`,
|
|
36
|
+
details: { module: module.id, to: model.moduleOf(to).id, edges },
|
|
37
|
+
};
|
|
38
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
const glob = (pattern) => new RegExp(`^${pattern.replaceAll(/[.+?^${}()|[\]\\]/g, '\\$&').replaceAll('*', '.*')}$`);
|
|
2
|
+
export const listsPackage = (pattern, external) => {
|
|
3
|
+
const matches = glob(pattern);
|
|
4
|
+
return matches.test(external.package) || matches.test(external.specifier);
|
|
5
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const place = (model, file, module = model.moduleOf(file)) => {
|
|
2
|
+
if (module.kind === 'files')
|
|
3
|
+
return `${module.name} of ${module.parent.archetype} ${module.parent.id}`;
|
|
4
|
+
const role = module.looseFiles.includes(file) ? 'loose in ' : model.isTest(file) ? 'a test of ' : '';
|
|
5
|
+
return `${role}${module.archetype} ${module.id}`;
|
|
6
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Member } from '../model/facts.ts';
|
|
2
|
+
export interface Group {
|
|
3
|
+
names: string[];
|
|
4
|
+
lines: [number, number];
|
|
5
|
+
}
|
|
6
|
+
export declare const group: (members: Member[]) => Group;
|
|
7
|
+
export declare const withOrdinals: <T extends {
|
|
8
|
+
name: string;
|
|
9
|
+
}>(functions: T[]) => [T, string][];
|
|
10
|
+
export declare const title: (name: string) => string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const group = (members) => ({
|
|
2
|
+
names: members.map((member) => member.name),
|
|
3
|
+
lines: [Math.min(...members.map((member) => member.line)), Math.max(...members.map((member) => member.endLine))],
|
|
4
|
+
});
|
|
5
|
+
export const withOrdinals = (functions) => {
|
|
6
|
+
const seen = new Map();
|
|
7
|
+
return functions.map((fn) => {
|
|
8
|
+
const count = (seen.get(fn.name) ?? 0) + 1;
|
|
9
|
+
seen.set(fn.name, count);
|
|
10
|
+
return [fn, count === 1 ? fn.name : `${fn.name}#${count}`];
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
export const title = (name) => name.charAt(0).toUpperCase() + name.slice(1);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ConfigData } from '../../model/archetype/data.ts';
|
|
2
|
+
import type { ModuleView } from '../../model/archetype/resolve.ts';
|
|
3
|
+
import type { Finding, ProjectContext } from '../../rules/rule.ts';
|
|
4
|
+
export declare const id = "unused-declaration";
|
|
5
|
+
export type Layer = 'production' | 'test';
|
|
6
|
+
export type UnusedDeclaration = {
|
|
7
|
+
kind: 'grant';
|
|
8
|
+
declaredIn: string;
|
|
9
|
+
layer: Layer;
|
|
10
|
+
using: string;
|
|
11
|
+
modules: string[];
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'surface';
|
|
14
|
+
declaredIn: string;
|
|
15
|
+
layer: Layer;
|
|
16
|
+
exposes: string;
|
|
17
|
+
modules: string[];
|
|
18
|
+
} | {
|
|
19
|
+
kind: 'empty';
|
|
20
|
+
declaredIn: string;
|
|
21
|
+
layer: Layer;
|
|
22
|
+
selector: string;
|
|
23
|
+
};
|
|
24
|
+
export type Context = Pick<ProjectContext<object>, 'config' | 'model' | 'verdicts' | 'project' | 'atModule'> & {
|
|
25
|
+
held: ReadonlySet<ModuleView>;
|
|
26
|
+
};
|
|
27
|
+
export declare const holding: ({ model, project }: Omit<Context, 'held'>) => ReadonlySet<ModuleView>;
|
|
28
|
+
export declare const instancesOf: ({ model, held }: Context, archetype: string) => ModuleView[];
|
|
29
|
+
export declare const childrenNamed: ({ held }: Context, instances: readonly ModuleView[], name: string) => ModuleView[];
|
|
30
|
+
export declare const named: (module: ModuleView) => string;
|
|
31
|
+
export declare const within: (module: ModuleView | undefined, holder: ModuleView) => boolean;
|
|
32
|
+
export declare const unused: (config: ConfigData, key: string, message: string, details: UnusedDeclaration) => Finding;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const id = 'unused-declaration';
|
|
2
|
+
export const holding = ({ model, project }) => {
|
|
3
|
+
const held = new Set();
|
|
4
|
+
for (const file of project.files)
|
|
5
|
+
for (let at = model.moduleOf(file.path); at; at = at.parent)
|
|
6
|
+
held.add(at);
|
|
7
|
+
return held;
|
|
8
|
+
};
|
|
9
|
+
export const instancesOf = ({ model, held }, archetype) => model.modules.filter((module) => module.archetype === archetype && held.has(module));
|
|
10
|
+
export const childrenNamed = ({ held }, instances, name) => instances.flatMap((instance) => instance.children.filter((child) => child.name === name && held.has(child)));
|
|
11
|
+
export const named = (module) => (module.id === '.' ? 'the root' : module.id);
|
|
12
|
+
export const within = (module, holder) => {
|
|
13
|
+
for (let at = module; at; at = at.parent)
|
|
14
|
+
if (at === holder)
|
|
15
|
+
return true;
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
export const unused = (config, key, message, details) => ({
|
|
19
|
+
rule: id,
|
|
20
|
+
key: `${id}:${details.kind}:${key}`,
|
|
21
|
+
file: config.file,
|
|
22
|
+
line: 1,
|
|
23
|
+
value: 1,
|
|
24
|
+
message,
|
|
25
|
+
details,
|
|
26
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { childrenNamed, instancesOf, named, unused } from './declarations.js';
|
|
2
|
+
export const emptyModules = (context) => Object.values(context.config.archetypes).flatMap((archetype) => {
|
|
3
|
+
const instances = instancesOf(context, archetype.name);
|
|
4
|
+
const on = instances.some((instance) => context.atModule(instance).on);
|
|
5
|
+
const declared = [
|
|
6
|
+
...archetype.modules.map((module) => ({ module, name: module.name, layer: 'production' })),
|
|
7
|
+
...(archetype.tests?.modules ?? []).map((module) => ({
|
|
8
|
+
module,
|
|
9
|
+
name: `tests.${module.name}`,
|
|
10
|
+
layer: 'test',
|
|
11
|
+
})),
|
|
12
|
+
];
|
|
13
|
+
return declared
|
|
14
|
+
.filter(({ name }) => on && childrenNamed(context, instances, name).length === 0)
|
|
15
|
+
.map(({ module, name, layer }) => empty(context, `${archetype.name}.${name}`, module, layer, instances));
|
|
16
|
+
});
|
|
17
|
+
const empty = (context, declaredIn, module, layer, instances) => {
|
|
18
|
+
const selector = module.kind === 'dir' ? `dir(${quoted(module.paths)})` : `files(${quoted(module.patterns)})`;
|
|
19
|
+
const where = instances.map(named).join(', ');
|
|
20
|
+
return unused(context.config, declaredIn, `${declaredIn} matches no files: ${selector} finds nothing in ${where}. Fix the selector, or remove the module.`, { kind: 'empty', declaredIn, layer, selector });
|
|
21
|
+
};
|
|
22
|
+
const quoted = (patterns) => patterns.map((pattern) => `'${pattern}'`).join(', ');
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { show } from '../../model/archetype/references.js';
|
|
2
|
+
import { listsPackage } from '../packages.js';
|
|
3
|
+
import { childrenNamed, instancesOf, named, unused } from './declarations.js';
|
|
4
|
+
export const unusedGrants = (context) => {
|
|
5
|
+
const used = usedRefs(context);
|
|
6
|
+
return declared(context)
|
|
7
|
+
.filter(({ ref, holders }) => !used.has(ref) && holders.some((holder) => context.atModule(holder).on))
|
|
8
|
+
.map(({ ref, declaredIn, layer, holders }) => {
|
|
9
|
+
const using = show(ref);
|
|
10
|
+
const modules = holders.map((holder) => holder.id);
|
|
11
|
+
const names = holders.map(named).join(', ');
|
|
12
|
+
const files = layer === 'test' ? `tests of ${names}` : names;
|
|
13
|
+
return unused(context.config, `${declaredIn}->${using}`, `${declaredIn} uses ${using}, but nothing in ${files} imports through it. Remove it from the using.`, { kind: 'grant', declaredIn, layer, using, modules });
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
const declared = (context) => Object.values(context.config.archetypes).flatMap((archetype) => {
|
|
17
|
+
const instances = instancesOf(context, archetype.name);
|
|
18
|
+
const modules = archetype.modules.flatMap((module) => {
|
|
19
|
+
const holders = childrenNamed(context, instances, module.name);
|
|
20
|
+
const declaredIn = `${archetype.name}.${module.name}`;
|
|
21
|
+
const tests = module.kind === 'files' ? (module.tests?.using ?? []) : [];
|
|
22
|
+
return [
|
|
23
|
+
...module.using.map((ref) => ({ ref, declaredIn, layer: 'production', holders })),
|
|
24
|
+
...tests.map((ref) => ({ ref, declaredIn: `${declaredIn}.tests`, layer: 'test', holders })),
|
|
25
|
+
];
|
|
26
|
+
});
|
|
27
|
+
const layer = (archetype.tests?.using ?? []).map((ref) => ({
|
|
28
|
+
ref,
|
|
29
|
+
declaredIn: `${archetype.name}.tests`,
|
|
30
|
+
layer: 'test',
|
|
31
|
+
holders: instances,
|
|
32
|
+
}));
|
|
33
|
+
return [...modules, ...layer];
|
|
34
|
+
});
|
|
35
|
+
const usedRefs = ({ project, verdicts }) => {
|
|
36
|
+
const used = new Set();
|
|
37
|
+
for (const file of project.files) {
|
|
38
|
+
const reach = verdicts.reach(file.path) ?? [];
|
|
39
|
+
const targets = file.imports.map((edge) => edge.via ?? edge.to);
|
|
40
|
+
for (const grant of reach)
|
|
41
|
+
if (grant.declared && permits(grant, targets, file.externals))
|
|
42
|
+
used.add(grant.declared);
|
|
43
|
+
}
|
|
44
|
+
return used;
|
|
45
|
+
};
|
|
46
|
+
const permits = (grant, targets, externals) => grant.package === undefined
|
|
47
|
+
? targets.some((to) => grant.files.has(to))
|
|
48
|
+
: externals.some((external) => listsPackage(grant.package, external));
|