@yearofthedan/warp 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -69
- package/dist/analysis/imports.d.ts +3 -1
- package/dist/analysis/imports.js +5 -1
- package/dist/analysis/typescript.js +2 -1
- package/dist/app/check.js +2 -1
- package/dist/app/checkModel.js +1 -1
- package/dist/app/load.js +12 -12
- package/dist/builtins/advice.d.ts +8 -0
- package/dist/builtins/advice.js +42 -0
- package/dist/builtins/dependency-direction/dependencyDirection.js +36 -16
- package/dist/builtins/index.js +2 -0
- package/dist/builtins/module-externals/moduleExternals.js +10 -9
- package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.js +16 -5
- package/dist/builtins/place.js +5 -3
- package/dist/builtins/unresolved-import/unresolvedImport.d.ts +2 -0
- package/dist/builtins/unresolved-import/unresolvedImport.js +15 -0
- package/dist/builtins/unused-declaration/declarations.js +1 -1
- package/dist/builtins/unused-declaration/empty.js +16 -9
- package/dist/builtins/unused-declaration/grants.js +8 -11
- package/dist/builtins/unused-declaration/surfaces.js +11 -19
- package/dist/cli.js +3 -3
- package/dist/git/repository.js +11 -16
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/model/archetype/builder.d.ts +11 -11
- package/dist/model/archetype/builder.js +14 -21
- package/dist/model/archetype/data.d.ts +19 -13
- package/dist/model/archetype/draft.d.ts +2 -9
- package/dist/model/archetype/draft.js +48 -59
- package/dist/model/archetype/fixes.d.ts +37 -0
- package/dist/model/archetype/fixes.js +234 -0
- package/dist/model/archetype/reach.js +34 -80
- package/dist/model/archetype/references.d.ts +1 -1
- package/dist/model/archetype/references.js +13 -28
- package/dist/model/archetype/requirements.js +3 -6
- package/dist/model/archetype/resolve.d.ts +2 -3
- package/dist/model/archetype/resolve.js +93 -71
- package/dist/model/archetype/rulesets.js +0 -7
- package/dist/model/archetype/settings.js +4 -19
- package/dist/model/facts.d.ts +6 -0
- package/dist/reporters/agent.d.ts +4 -0
- package/dist/reporters/agent.js +24 -0
- package/dist/reporters/claudeHook.js +3 -3
- package/dist/reporters/model.js +8 -9
- package/dist/reporters/omp.js +3 -5
- package/dist/reporters/reporter.d.ts +0 -4
- package/dist/reporters/reporter.js +0 -9
- package/dist/reporters/text.d.ts +2 -0
- package/dist/reporters/text.js +12 -8
- package/package.json +2 -3
- package/integrations/omp/warp.ts +0 -40
package/README.md
CHANGED
|
@@ -1,98 +1,74 @@
|
|
|
1
1
|
# warp
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
warp checks a TypeScript codebase against a declared model of its modules. A `warp.config.ts` names the modules and
|
|
4
|
+
what each may use, and warp reports the code that steps outside it:
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
- imports outside a module's reach, including packages it doesn't list.
|
|
7
|
+
- imports and re-exports that go around a module's surface.
|
|
8
|
+
- import loops between files and between modules.
|
|
9
|
+
- files and functions that are too large or too tangled to follow.
|
|
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
|
|
13
|
+
run after every edit of a coding agent.
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
## Quick start
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
pnpm add -D @yearofthedan/warp
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
Add a `warp.config.ts` next to the project's `tsconfig.json`, then:
|
|
17
|
+
warp needs Node 26 or later. Add it to the project as a dev dependency:
|
|
21
18
|
|
|
22
19
|
```sh
|
|
23
|
-
pnpm
|
|
24
|
-
pnpm exec warp diff HEAD # new or worse findings in the working tree since HEAD
|
|
25
|
-
pnpm exec warp diff main..feature # new or worse findings between two refs
|
|
26
|
-
pnpm exec warp diff main... # new or worse findings since the working tree's branch left main
|
|
27
|
-
pnpm exec warp model # the modules the config finds, and the module each file is in
|
|
20
|
+
pnpm add -D @yearofthedan/warp
|
|
28
21
|
```
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
| `--config <file>` | Config path. The project root is its directory. Default: the first of `warp.config.ts`, `.mts`, `.js` and `.mjs` in the current directory. |
|
|
33
|
-
| `--ruleset <name>` | Check with one of the config's [rulesets](docs/config.md#rulesets). Default: the one named `default`, if any. |
|
|
34
|
-
| `--since-last-run` | With `diff` and no head, compare the working tree with its findings at the last run with this flag. The first run, and the first after `HEAD` moves, compares with the base. |
|
|
35
|
-
| `--rules` | With `model`, list the rule settings each module changes. |
|
|
36
|
-
| `--json` | With `model`, print the model as JSON, with every module's resolved rule settings. |
|
|
37
|
-
| `--reporter <name>` | Output format, default `text`. |
|
|
38
|
-
| `--fail` | Exit 1 when there are findings (`check`) or new or worse ones (`diff`). Without it, both exit 0. |
|
|
39
|
-
|
|
40
|
-
| Reporter | Output |
|
|
41
|
-
| --- | --- |
|
|
42
|
-
| `text` | One line per finding, then for `check` the findings counted by rule and module, and a total. For `diff`, findings are marked `new` or `worse (was N)`. |
|
|
43
|
-
| `json` | `{ findings }` for `check`; `{ added, worsened, resolved }` for `diff`. Each finding has the same `message`. |
|
|
44
|
-
| `sarif` | SARIF 2.1.0 for GitHub code scanning: the findings to act on, with paths from the repository root. |
|
|
45
|
-
| `claude-hook` | A Claude Code PostToolUse hook response, or nothing when there is nothing to act on. |
|
|
46
|
-
| `omp` | An omp `tool_result` handler result, or nothing when there is nothing to act on. |
|
|
47
|
-
|
|
48
|
-
"To act on" means every finding for `check` and the new and worse ones for `diff`.
|
|
49
|
-
|
|
50
|
-
## A starter config
|
|
51
|
-
|
|
52
|
-
A starter for a hexagonal layout, to adjust to the project's folders:
|
|
23
|
+
Describe the codebase as it is, one module per folder under `src/`, in a `warp.config.ts` next to the project's
|
|
24
|
+
`tsconfig.json`:
|
|
53
25
|
|
|
54
26
|
```ts
|
|
55
27
|
// warp.config.ts
|
|
56
|
-
import { all, archetype, defineConfig, exposes, files, pkg, ruleset, using } from '@yearofthedan/warp';
|
|
28
|
+
import { all, archetype, defineConfig, dir, exposes, files, pkg, ruleset, using } from '@yearofthedan/warp';
|
|
57
29
|
|
|
58
30
|
const app = archetype('app')
|
|
59
|
-
.contains((
|
|
60
|
-
|
|
61
|
-
.module('
|
|
62
|
-
.module('
|
|
63
|
-
.module('
|
|
64
|
-
.
|
|
65
|
-
.module('entry', files('src/main.ts', 'src/index.ts'), using((m) => [m.domain, m.application, m.adapters, m.lib])),
|
|
31
|
+
.contains((c) =>
|
|
32
|
+
c
|
|
33
|
+
.module('utils', dir('src/utils'), exposes(all()))
|
|
34
|
+
.module('db', dir('src/db'), exposes(all()))
|
|
35
|
+
.module('api', dir('src/api'), exposes(all()).using((m) => [m.db, m.utils, pkg('*')]))
|
|
36
|
+
.testModule('tests', files('src/**/*.test.ts')),
|
|
66
37
|
)
|
|
67
|
-
.
|
|
38
|
+
.entry(files('src/index.ts'))
|
|
39
|
+
.testUsing([pkg('vitest')]);
|
|
68
40
|
|
|
69
41
|
const base = ruleset().on(app.modules.tests, { 'function-cohesion': false });
|
|
70
42
|
|
|
71
|
-
export default defineConfig({ root: app,
|
|
43
|
+
export default defineConfig({ root: app, rulesets: { default: base } });
|
|
72
44
|
```
|
|
73
45
|
|
|
74
|
-
|
|
75
|
-
`function-cohesion` is off for them, because a `describe` callback holds `it` calls that share no locals, which the
|
|
76
|
-
rule would ask to split.
|
|
46
|
+
Then check it, and after an edit, see what the edit broke:
|
|
77
47
|
|
|
78
|
-
|
|
79
|
-
|
|
48
|
+
```sh
|
|
49
|
+
pnpm exec warp check # every finding in the working tree, for CI
|
|
50
|
+
pnpm exec warp diff HEAD # what changed since the last commit made new or worse, for an edit hook
|
|
51
|
+
```
|
|
80
52
|
|
|
81
|
-
|
|
82
|
-
-
|
|
83
|
-
|
|
84
|
-
scanning.
|
|
85
|
-
- [integrations/omp/README.md](integrations/omp/README.md): running warp from omp.
|
|
53
|
+
```text
|
|
54
|
+
new: src/utils/slug.ts:1 [dependency-direction] src/utils/slug.ts (app.utils src/utils) imports src/db/client.ts (app.db src/db), which is outside utils's reach. Declare db above utils in app and add m.db to the using of app.utils, or move the code.
|
|
55
|
+
```
|
|
86
56
|
|
|
87
|
-
##
|
|
57
|
+
## Read next
|
|
88
58
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
59
|
+
- [Getting started](docs/getting-started.md): from this config to a clean `warp check` and a first `diff`, step by
|
|
60
|
+
step.
|
|
61
|
+
- [Fixing findings](docs/fixing-findings.md): how to read a finding and decide between changing the code and changing
|
|
62
|
+
the config.
|
|
63
|
+
- [Concepts](docs/concepts.md): what each of warp's terms means.
|
|
64
|
+
- [Config](docs/config.md) and [CLI](docs/cli.md): the reference for each.
|
|
65
|
+
- [Rules](docs/rules.md): every rule warp checks, each with its page.
|
|
66
|
+
- [Agents and CI](docs/agents-and-ci.md): running warp after each edit of Claude Code or omp, and on pull requests.
|
|
67
|
+
- [Hexagonal layout](docs/examples/hexagonal.md): a stricter config, for ports and adapters.
|
|
93
68
|
|
|
94
69
|
## Limits
|
|
95
70
|
|
|
96
|
-
-
|
|
71
|
+
- warp follows static `import` and `export … from` declarations. Dependencies through `import()`, `require()` and
|
|
72
|
+
`import x = require()` go unchecked.
|
|
97
73
|
- A namespace import (`import * as x`) points at the module the specifier resolves to, a barrel included.
|
|
98
|
-
-
|
|
74
|
+
- Each class method is checked as a function of its own, and a class is one member of its file.
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { type Checker, type Program } from 'typescript/sync';
|
|
2
|
-
import type { ExternalImport, ImportEdge } from '../model/facts.ts';
|
|
2
|
+
import type { ExternalImport, ImportEdge, UnresolvedImport } from '../model/facts.ts';
|
|
3
3
|
import type { Source } from './source.ts';
|
|
4
4
|
export interface Resolver {
|
|
5
5
|
checker: Checker;
|
|
6
6
|
program: Program;
|
|
7
7
|
relative: (pathOrFileName: string) => string | undefined;
|
|
8
|
+
exists: (fileName: string) => boolean;
|
|
8
9
|
}
|
|
9
10
|
export interface FileEdges {
|
|
10
11
|
imports: ImportEdge[];
|
|
11
12
|
externals: ExternalImport[];
|
|
13
|
+
unresolved: UnresolvedImport[];
|
|
12
14
|
}
|
|
13
15
|
export declare const fileEdges: (source: Source, resolver: Resolver) => FileEdges;
|
package/dist/analysis/imports.js
CHANGED
|
@@ -2,9 +2,10 @@ import { SyntaxKind } from 'typescript/ast';
|
|
|
2
2
|
import { isExportAssignment, isExportDeclaration, isIdentifier, isImportDeclaration, isNamedExports, isNamedImports, isNamespaceImport, isStringLiteralLikeNode, } from 'typescript/ast/is';
|
|
3
3
|
import { SymbolFlags } from 'typescript/sync';
|
|
4
4
|
import { isBuiltin } from 'node:module';
|
|
5
|
+
import { dirname, resolve } from 'node:path';
|
|
5
6
|
export const fileEdges = (source, resolver) => {
|
|
6
7
|
const exported = locallyExportedNames(source.sourceFile);
|
|
7
|
-
const edges = { imports: [], externals: [] };
|
|
8
|
+
const edges = { imports: [], externals: [], unresolved: [] };
|
|
8
9
|
for (const statement of source.sourceFile.statements) {
|
|
9
10
|
const declaration = moduleDeclaration(statement, exported);
|
|
10
11
|
if (!declaration)
|
|
@@ -15,9 +16,12 @@ export const fileEdges = (source, resolver) => {
|
|
|
15
16
|
edges.imports.push(...statementEdges(statement, declaration, via, source, resolver));
|
|
16
17
|
else if (isBare(declaration.specifier.text))
|
|
17
18
|
edges.externals.push(externalImport(statement, declaration, source));
|
|
19
|
+
else if (!resolved?.resolvedFileName && !namesFileOnDisk(declaration.specifier.text, source, resolver))
|
|
20
|
+
edges.unresolved.push({ from: source.file, specifier: declaration.specifier.text, line: source.line(statement) });
|
|
18
21
|
}
|
|
19
22
|
return edges;
|
|
20
23
|
};
|
|
24
|
+
const namesFileOnDisk = (specifier, source, resolver) => resolver.exists(resolve(dirname(source.sourceFile.fileName), specifier));
|
|
21
25
|
const isBare = (specifier) => !specifier.startsWith('.') && !specifier.startsWith('/');
|
|
22
26
|
const packageOf = (specifier) => {
|
|
23
27
|
const segments = specifier.split('/');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
3
3
|
import { join, relative, sep } from 'node:path';
|
|
4
4
|
import { API } from 'typescript/sync';
|
|
5
5
|
import { freshFacts } from '../app/facts.js';
|
|
@@ -74,6 +74,7 @@ const analyzeProgram = ({ program, checker }, files, fresh, done) => {
|
|
|
74
74
|
program,
|
|
75
75
|
checker,
|
|
76
76
|
relative: (path) => byPath.get(path) ?? byPath.get(path.toLowerCase()),
|
|
77
|
+
exists: existsSync,
|
|
77
78
|
};
|
|
78
79
|
const analyzed = new Map();
|
|
79
80
|
for (const [fileName, file] of files) {
|
package/dist/app/check.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { checkModel } from './checkModel.js';
|
|
2
2
|
import { resolveModel } from '../model/archetype/resolve.js';
|
|
3
3
|
export const modelProject = ({ analyzer, foldersAt, facts }, root, config) => {
|
|
4
|
-
const
|
|
4
|
+
const analyzed = analyzer.analyze(root, config.tsconfig, facts);
|
|
5
|
+
const project = { files: analyzed.files.filter((file) => file.path !== config.file) };
|
|
5
6
|
const files = project.files.map((file) => file.path);
|
|
6
7
|
return { model: resolveModel(config, foldersAt(root), files), project };
|
|
7
8
|
};
|
package/dist/app/checkModel.js
CHANGED
|
@@ -14,7 +14,7 @@ const placed = (model, finding) => {
|
|
|
14
14
|
const module = model.moduleOf(finding.file);
|
|
15
15
|
if (!module)
|
|
16
16
|
return finding;
|
|
17
|
-
return { ...finding, module:
|
|
17
|
+
return { ...finding, module: module.id };
|
|
18
18
|
};
|
|
19
19
|
const run = (rule, shared, settled) => {
|
|
20
20
|
if (rule.scope === 'project')
|
package/dist/app/load.js
CHANGED
|
@@ -20,7 +20,7 @@ export const loadConfigData = (raw, { builtins, ruleset, file = 'warp.config.ts'
|
|
|
20
20
|
};
|
|
21
21
|
if (!isRecord(raw))
|
|
22
22
|
throw new ConfigError('config: expected an object, defineConfig({ root })');
|
|
23
|
-
const unknownKeys = Object.keys(raw).filter((key) => !['root', 'tsconfig', '
|
|
23
|
+
const unknownKeys = Object.keys(raw).filter((key) => !['root', 'tsconfig', 'plugins', 'rulesets'].includes(key));
|
|
24
24
|
if (unknownKeys.length > 0)
|
|
25
25
|
fail(`config: unknown key ${unknownKeys.join(', ')}`);
|
|
26
26
|
const root = recordedAs(raw.root, 'archetype');
|
|
@@ -41,7 +41,6 @@ export const loadConfigData = (raw, { builtins, ruleset, file = 'warp.config.ts'
|
|
|
41
41
|
root: root.definition.name,
|
|
42
42
|
archetypes: Object.fromEntries(drafts.map(({ data }) => [data.name, archetypeData(data)])),
|
|
43
43
|
tsconfig: tsconfigs(raw.tsconfig, fail),
|
|
44
|
-
...testPatterns(raw.tests, fail),
|
|
45
44
|
};
|
|
46
45
|
const config = withRuleset(structure, raw.rulesets, ruleset, (value, at) => toRules(check, value, at), fail);
|
|
47
46
|
checkUnused(drafts, fail);
|
|
@@ -50,7 +49,17 @@ export const loadConfigData = (raw, { builtins, ruleset, file = 'warp.config.ts'
|
|
|
50
49
|
failed();
|
|
51
50
|
return { config, rules, warnings };
|
|
52
51
|
};
|
|
53
|
-
const archetypeData = ({ name, inline, description, identify, requires, modules, exposes, provides,
|
|
52
|
+
const archetypeData = ({ name, inline, description, identify, requires, modules, exposes, provides, testUsing, }) => defined({
|
|
53
|
+
name,
|
|
54
|
+
inline,
|
|
55
|
+
description,
|
|
56
|
+
identify,
|
|
57
|
+
requires,
|
|
58
|
+
modules: modules.map(defined),
|
|
59
|
+
exposes,
|
|
60
|
+
provides,
|
|
61
|
+
testUsing,
|
|
62
|
+
});
|
|
54
63
|
const plugins = (value, fail) => {
|
|
55
64
|
if (value === undefined)
|
|
56
65
|
return {};
|
|
@@ -59,15 +68,6 @@ const plugins = (value, fail) => {
|
|
|
59
68
|
fail('config.plugins: expected plugins by namespace');
|
|
60
69
|
return {};
|
|
61
70
|
};
|
|
62
|
-
const testPatterns = (value, fail) => {
|
|
63
|
-
if (value === undefined)
|
|
64
|
-
return {};
|
|
65
|
-
const selector = recordedAs(value, 'selector');
|
|
66
|
-
if (selector?.kind === 'files' && selector.patterns.every(isName))
|
|
67
|
-
return { tests: [...selector.patterns] };
|
|
68
|
-
fail("config.tests: expected the test entry files, files('**/*.test.ts', ...)");
|
|
69
|
-
return {};
|
|
70
|
-
};
|
|
71
71
|
const tsconfigs = (value, fail) => {
|
|
72
72
|
const paths = [value ?? 'tsconfig.json'].flat();
|
|
73
73
|
if (paths.length === 0 || !paths.every(isName))
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RefData } from '../model/archetype/data.ts';
|
|
2
|
+
import type { Fix } from '../model/archetype/fixes.ts';
|
|
3
|
+
import type { Verdicts } from '../model/archetype/reach.ts';
|
|
4
|
+
import type { Model } from '../model/archetype/resolve.ts';
|
|
5
|
+
export declare const refText: (ref: RefData) => string;
|
|
6
|
+
export declare const sentence: (parts: readonly string[]) => string;
|
|
7
|
+
export declare const reachSize: (verdicts: Verdicts, file: string) => number;
|
|
8
|
+
export declare const fixSteps: (model: Model, fixes: readonly Fix[]) => string[];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { show } from '../model/archetype/references.js';
|
|
2
|
+
export const refText = (ref) => (ref.kind === 'module' ? `m.${show(ref)}` : show(ref));
|
|
3
|
+
export const sentence = (parts) => parts.length < 2 ? (parts[0] ?? '') : `${parts.slice(0, -1).join(', ')} and ${parts.at(-1)}`;
|
|
4
|
+
export const reachSize = (verdicts, file) => new Set((verdicts.reach(file) ?? []).flatMap((grant) => (grant.ref === undefined ? [] : [grant.ref]))).size;
|
|
5
|
+
const instances = (model, archetype) => {
|
|
6
|
+
const count = model.modules.filter((module) => module.archetype === archetype).length;
|
|
7
|
+
return count > 1 ? ` (all ${count} ${archetype} modules)` : '';
|
|
8
|
+
};
|
|
9
|
+
const step = (model, fix, refs) => {
|
|
10
|
+
switch (fix.kind) {
|
|
11
|
+
case 'using':
|
|
12
|
+
return `add ${refText(fix.ref)} to ${fix.site}`;
|
|
13
|
+
case 'declare':
|
|
14
|
+
return `declare ${fix.module} above ${fix.above} in ${fix.archetype}`;
|
|
15
|
+
case 'require':
|
|
16
|
+
return `require ${fix.name} in ${fix.archetype}, have ${fix.provider} provide m.${fix.module} as ${fix.name}`;
|
|
17
|
+
case 'expose':
|
|
18
|
+
return `expose ${sentence(refs.map(refText))} from ${fix.archetype}${instances(model, fix.archetype)}`;
|
|
19
|
+
case 'expose-file':
|
|
20
|
+
return `add '${fix.pattern}' to what ${fix.archetype} exposes`;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export const fixSteps = (model, fixes) => {
|
|
24
|
+
const steps = [];
|
|
25
|
+
for (const [at, fix] of fixes.entries()) {
|
|
26
|
+
if (fix.kind !== 'expose') {
|
|
27
|
+
steps.push(step(model, fix, []));
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const previous = fixes[at - 1];
|
|
31
|
+
if (previous?.kind === 'expose' && previous.archetype === fix.archetype)
|
|
32
|
+
continue;
|
|
33
|
+
const refs = [];
|
|
34
|
+
for (const next of fixes.slice(at)) {
|
|
35
|
+
if (next.kind !== 'expose' || next.archetype !== fix.archetype)
|
|
36
|
+
break;
|
|
37
|
+
refs.push(next.ref);
|
|
38
|
+
}
|
|
39
|
+
steps.push(step(model, fix, refs));
|
|
40
|
+
}
|
|
41
|
+
return steps;
|
|
42
|
+
};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { exposesText } from '../../model/archetype/references.js';
|
|
2
|
+
import { fixSteps, reachSize, sentence } from '../advice.js';
|
|
2
3
|
import { place } from '../place.js';
|
|
4
|
+
import { title } from '../shared.js';
|
|
5
|
+
import { fixesFor } from '../../model/archetype/fixes.js';
|
|
3
6
|
import { noOptions } from '../../rules/rule.js';
|
|
4
7
|
const id = 'dependency-direction';
|
|
5
8
|
export const dependencyDirection = {
|
|
@@ -34,32 +37,49 @@ const refused = (context, from, to, refusal, edges) => {
|
|
|
34
37
|
details: {
|
|
35
38
|
from: source.id,
|
|
36
39
|
to: target.id,
|
|
37
|
-
reason: refusal.reason,
|
|
40
|
+
reason: isLoose(model, to) ? 'loose' : refusal.reason,
|
|
38
41
|
...(refusal.reason === 'private' ? { hiddenBy: refusal.hiddenBy.id } : {}),
|
|
39
42
|
edges,
|
|
40
43
|
},
|
|
41
44
|
};
|
|
42
45
|
};
|
|
43
46
|
const explain = ({ config, model, verdicts }, from, to, refusal) => {
|
|
44
|
-
|
|
47
|
+
if (isLoose(model, to))
|
|
48
|
+
return `which is in no module yet. Add a module that holds ${to}, then add it to a using if the dependency is intended.`;
|
|
45
49
|
if (refusal.reason === 'test')
|
|
46
50
|
return 'a test file. Production code never imports tests: move what both need out of the test file into the module it tests.';
|
|
51
|
+
const instead = refusal.reason === 'private' ? throughSurface({ config, model, verdicts }, from, to, refusal.hiddenBy) : '';
|
|
52
|
+
return `${why(config, model, verdicts, from, to, refusal)}. ${instead || advise(config, model, verdicts, from, to)}`;
|
|
53
|
+
};
|
|
54
|
+
const why = (config, model, verdicts, from, to, refusal) => {
|
|
55
|
+
const source = model.moduleOf(from);
|
|
47
56
|
if (refusal.reason === 'private') {
|
|
48
57
|
const { hiddenBy } = refusal;
|
|
49
|
-
return
|
|
50
|
-
`Import what ${hiddenBy.id} exposes, or expose ${to} from ${hiddenBy.archetype ?? hiddenBy.name} if other modules should reach it.`);
|
|
58
|
+
return `which ${hiddenBy.id} keeps private (it exposes ${exposesText(verdicts.exposes(hiddenBy))})`;
|
|
51
59
|
}
|
|
52
|
-
if (refusal.reason === 'above')
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
60
|
+
if (refusal.reason === 'above')
|
|
61
|
+
return `which is above ${(source.kind === 'dir' ? source : source.parent).id}`;
|
|
62
|
+
return `which is outside ${source.name ?? source.id}'s reach`;
|
|
63
|
+
};
|
|
64
|
+
const throughSurface = ({ config, model, verdicts }, from, to, hiddenBy) => {
|
|
65
|
+
if (model.isTest(to))
|
|
66
|
+
return '';
|
|
67
|
+
for (const file of verdicts.surface(hiddenBy)) {
|
|
68
|
+
const fixes = fixesFor(config, model, from, file);
|
|
69
|
+
if (!fixes)
|
|
70
|
+
continue;
|
|
71
|
+
const once = fixes.length > 0 ? `, once you ${sentence(fixSteps(model, fixes))}` : '';
|
|
72
|
+
return `Import what ${hiddenBy.id} exposes instead${once}.`;
|
|
56
73
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
74
|
+
return '';
|
|
75
|
+
};
|
|
76
|
+
const advise = (config, model, verdicts, from, to) => {
|
|
77
|
+
const fixes = fixesFor(config, model, from, to);
|
|
78
|
+
if (!fixes)
|
|
79
|
+
return 'No declared path allows this: either the import or the structure is wrong.';
|
|
80
|
+
const steps = fixSteps(model, fixes);
|
|
81
|
+
const size = reachSize(verdicts, from);
|
|
82
|
+
const more = size > 0 && fixes.some((fix) => fix.kind === 'using') ? ` (+${size} more in reach)` : '';
|
|
83
|
+
return `${title(sentence(steps))}, or move the code.${more}`;
|
|
64
84
|
};
|
|
65
|
-
const
|
|
85
|
+
const isLoose = (model, file) => model.moduleOf(file).looseFiles.includes(file);
|
package/dist/builtins/index.js
CHANGED
|
@@ -8,8 +8,10 @@ import { moduleExternals } from './module-externals/moduleExternals.js';
|
|
|
8
8
|
import { noCrossModuleReexport } from './no-cross-module-reexport/noCrossModuleReexport.js';
|
|
9
9
|
import { noCycle } from './no-cycle/noCycle.js';
|
|
10
10
|
import { noDoubleExport } from './no-double-export/noDoubleExport.js';
|
|
11
|
+
import { unresolvedImport } from './unresolved-import/unresolvedImport.js';
|
|
11
12
|
import { unusedDeclaration } from './unused-declaration/unusedDeclaration.js';
|
|
12
13
|
export const builtinRules = [
|
|
14
|
+
unresolvedImport,
|
|
13
15
|
dependencyDirection,
|
|
14
16
|
moduleExternals,
|
|
15
17
|
noDoubleExport,
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { listsPackage } from '../packages.js';
|
|
2
2
|
import { place } from '../place.js';
|
|
3
|
+
import { sitesOf } from '../../model/archetype/fixes.js';
|
|
3
4
|
import { noOptions } from '../../rules/rule.js';
|
|
4
5
|
const id = 'module-externals';
|
|
5
6
|
export const moduleExternals = {
|
|
6
7
|
id,
|
|
7
8
|
scope: 'project',
|
|
8
9
|
parseOptions: noOptions,
|
|
9
|
-
check: ({ model, verdicts, project, atFile }) => project.files.flatMap((file) => {
|
|
10
|
+
check: ({ config, model, verdicts, project, atFile }) => project.files.flatMap((file) => {
|
|
10
11
|
const reach = verdicts.reach(file.path);
|
|
11
12
|
if (!reach || !atFile(file.path).on)
|
|
12
13
|
return [];
|
|
@@ -14,28 +15,28 @@ export const moduleExternals = {
|
|
|
14
15
|
const byPackage = Map.groupBy(file.externals, (external) => external.package);
|
|
15
16
|
return [...byPackage].flatMap(([name, edges]) => {
|
|
16
17
|
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
|
+
return outside.length === 0 ? [] : [refused(config, model, file.path, name, outside, packages)];
|
|
18
19
|
});
|
|
19
20
|
}),
|
|
20
21
|
};
|
|
21
|
-
const refused = (model, from, name, edges, packages) => {
|
|
22
|
+
const refused = (config, model, from, name, edges, packages) => {
|
|
22
23
|
const module = model.moduleOf(from);
|
|
23
24
|
const owner = module.name ?? module.id;
|
|
24
25
|
const names = [...new Set(edges.flatMap((edge) => edge.names))];
|
|
25
26
|
const specifiers = [...new Set(edges.map((edge) => edge.specifier))];
|
|
26
27
|
const imports = edges.every((edge) => edge.typeOnly) ? 'imports types from' : 'imports';
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const site = sitesOf(config, model, from)[0];
|
|
29
|
+
const where = site?.label ?? 'a using';
|
|
30
|
+
const listed = new Set(packages.map((grant) => grant.ref)).size;
|
|
31
|
+
const more = listed > 0 ? ` (+${listed} more in reach)` : '';
|
|
31
32
|
return {
|
|
32
33
|
rule: id,
|
|
33
34
|
key: `${id}:${from}->${name}`,
|
|
34
35
|
file: from,
|
|
35
36
|
line: edges[0].line,
|
|
36
37
|
value: Math.max(1, names.length),
|
|
37
|
-
message: `${from} (${place(model, from, module)}) ${imports} ${specifiers.join(', ')}, a package outside ${owner}'s reach
|
|
38
|
-
`Add pkg('${name}') to
|
|
38
|
+
message: `${from} (${place(model, from, module)}) ${imports} ${specifiers.join(', ')}, a package outside ${owner}'s reach. ` +
|
|
39
|
+
`Add pkg('${name}') to ${where}, or move the code.${more}`,
|
|
39
40
|
details: { from: module.id, package: name, edges },
|
|
40
41
|
};
|
|
41
42
|
};
|
|
@@ -5,7 +5,7 @@ export const noCrossModuleReexport = {
|
|
|
5
5
|
id,
|
|
6
6
|
scope: 'project',
|
|
7
7
|
parseOptions: noOptions,
|
|
8
|
-
check: ({ model, project, atFile }) => project.files.flatMap((file) => {
|
|
8
|
+
check: ({ model, verdicts, project, atFile }) => project.files.flatMap((file) => {
|
|
9
9
|
const module = model.moduleOf(file.path);
|
|
10
10
|
if (!module || !atFile(file.path).on)
|
|
11
11
|
return [];
|
|
@@ -14,7 +14,8 @@ export const noCrossModuleReexport = {
|
|
|
14
14
|
const target = model.moduleOf(to);
|
|
15
15
|
if (!target || owns(module, target))
|
|
16
16
|
return [];
|
|
17
|
-
|
|
17
|
+
const blocked = importersOf(project, file.path, to).filter((importer) => !owns(target, model.moduleOf(importer)) && !verdicts.verdict(importer, to)?.allowed);
|
|
18
|
+
return [passedThrough(model, module, target, to, edges, blocked)];
|
|
18
19
|
});
|
|
19
20
|
}),
|
|
20
21
|
};
|
|
@@ -24,7 +25,10 @@ const owns = (module, target) => {
|
|
|
24
25
|
return true;
|
|
25
26
|
return false;
|
|
26
27
|
};
|
|
27
|
-
const
|
|
28
|
+
const importersOf = (project, file, to) => project.files
|
|
29
|
+
.filter((other) => other.imports.some((edge) => edge.via === file && edge.to === to && !edge.reExport))
|
|
30
|
+
.map((other) => other.path);
|
|
31
|
+
const passedThrough = (model, module, target, to, edges, blocked) => {
|
|
28
32
|
const from = edges[0].from;
|
|
29
33
|
const names = [...new Set(edges.flatMap((edge) => edge.names))];
|
|
30
34
|
const what = names.length > 0 ? names.join(', ') : 'everything';
|
|
@@ -35,8 +39,8 @@ const passedThrough = (model, module, target, to, edges) => {
|
|
|
35
39
|
line: edges[0].line,
|
|
36
40
|
value: Math.max(1, names.length),
|
|
37
41
|
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}.
|
|
39
|
-
|
|
42
|
+
`importers reach ${target.id} through ${module.id}. ${direct(target, blocked)}give ${module.id} its own type that ` +
|
|
43
|
+
`hides it, or move it into ${module.id} if ${module.id} owns it.`,
|
|
40
44
|
details: {
|
|
41
45
|
from: module.id,
|
|
42
46
|
to: target.id,
|
|
@@ -45,3 +49,10 @@ const passedThrough = (model, module, target, to, edges) => {
|
|
|
45
49
|
},
|
|
46
50
|
};
|
|
47
51
|
};
|
|
52
|
+
const direct = (target, blocked) => {
|
|
53
|
+
if (blocked.length === 0)
|
|
54
|
+
return `Have importers that need it use ${target.id} directly, `;
|
|
55
|
+
const count = blocked.length - 1;
|
|
56
|
+
const others = count > 0 ? ` and ${count} other importer${count > 1 ? 's' : ''}` : '';
|
|
57
|
+
return `${blocked[0]}${others} may not use ${target.id} directly, so `;
|
|
58
|
+
};
|
package/dist/builtins/place.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export const place = (model, file, module = model.moduleOf(file)) => {
|
|
2
|
-
if (module.kind
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
if (module.kind !== 'dir') {
|
|
3
|
+
const role = module.kind === 'test' ? 'test module ' : '';
|
|
4
|
+
return `${role}${module.name} of ${module.parent.archetype} ${module.parent.id}`;
|
|
5
|
+
}
|
|
6
|
+
const role = module.looseFiles.includes(file) ? 'loose in ' : '';
|
|
5
7
|
return `${role}${module.archetype} ${module.id}`;
|
|
6
8
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { noOptions } from '../../rules/rule.js';
|
|
2
|
+
const id = 'unresolved-import';
|
|
3
|
+
export const unresolvedImport = {
|
|
4
|
+
id,
|
|
5
|
+
parseOptions: noOptions,
|
|
6
|
+
check: ({ file }) => file.unresolved.map(({ from, specifier, line }) => ({
|
|
7
|
+
rule: id,
|
|
8
|
+
key: `${id}:${from}->${specifier}`,
|
|
9
|
+
file: from,
|
|
10
|
+
line,
|
|
11
|
+
value: 1,
|
|
12
|
+
message: `${specifier} from ${from} resolves to no file. Fix the path, or add the file it names.`,
|
|
13
|
+
details: { from, specifier },
|
|
14
|
+
})),
|
|
15
|
+
};
|
|
@@ -8,7 +8,7 @@ export const holding = ({ model, project }) => {
|
|
|
8
8
|
};
|
|
9
9
|
export const instancesOf = ({ model, held }, archetype) => model.modules.filter((module) => module.archetype === archetype && held.has(module));
|
|
10
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.
|
|
11
|
+
export const named = (module) => (module.parent === undefined ? 'the root' : module.id);
|
|
12
12
|
export const within = (module, holder) => {
|
|
13
13
|
for (let at = module; at; at = at.parent)
|
|
14
14
|
if (at === holder)
|
|
@@ -2,21 +2,28 @@ import { childrenNamed, instancesOf, named, unused } from './declarations.js';
|
|
|
2
2
|
export const emptyModules = (context) => Object.values(context.config.archetypes).flatMap((archetype) => {
|
|
3
3
|
const instances = instancesOf(context, archetype.name);
|
|
4
4
|
const on = instances.some((instance) => context.atModule(instance).on);
|
|
5
|
-
const declared =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
layer: 'test',
|
|
11
|
-
})),
|
|
12
|
-
];
|
|
5
|
+
const declared = archetype.modules.map((module) => ({
|
|
6
|
+
module,
|
|
7
|
+
name: module.name,
|
|
8
|
+
layer: module.kind === 'test' ? 'test' : 'production',
|
|
9
|
+
}));
|
|
13
10
|
return declared
|
|
14
11
|
.filter(({ name }) => on && childrenNamed(context, instances, name).length === 0)
|
|
15
12
|
.map(({ module, name, layer }) => empty(context, `${archetype.name}.${name}`, module, layer, instances));
|
|
16
13
|
});
|
|
17
14
|
const empty = (context, declaredIn, module, layer, instances) => {
|
|
18
|
-
const selector =
|
|
15
|
+
const selector = selectorText(module);
|
|
19
16
|
const where = instances.map(named).join(', ');
|
|
20
17
|
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
18
|
};
|
|
22
19
|
const quoted = (patterns) => patterns.map((pattern) => `'${pattern}'`).join(', ');
|
|
20
|
+
const selectorText = (module) => {
|
|
21
|
+
if (module.kind === 'dir')
|
|
22
|
+
return `dir(${quoted(module.paths)})`;
|
|
23
|
+
if (module.kind === 'files')
|
|
24
|
+
return `files(${quoted(module.patterns)})`;
|
|
25
|
+
const { locator } = module;
|
|
26
|
+
if (locator.kind === 'mirror')
|
|
27
|
+
return `mirror('${locator.from}', '${locator.to}')`;
|
|
28
|
+
return `${locator.kind}(${quoted(locator.patterns)})`;
|
|
29
|
+
};
|