@avi2dg/checks 0.55.0 → 0.56.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/CHANGELOG.md +16 -0
- package/docs/bins.md +2 -3
- package/docs/configs/dependency-rules.md +3 -3
- package/docs/configs/native-settings.md +4 -4
- package/docs/design.md +4 -3
- package/docs/gates/checks-ci-wiring.md +1 -1
- package/docs/gates/checks-copy.md +1 -1
- package/docs/gates/checks-css-size.md +1 -1
- package/docs/gates/checks-effect-scope.md +1 -1
- package/docs/gates/checks-frontend-syntax.md +1 -1
- package/docs/gates/checks-graphwalk.md +126 -0
- package/docs/gates/checks-lint-coverage.md +1 -1
- package/docs/gates/checks-lint.md +15 -6
- package/docs/gates/checks-no-plain-js.md +2 -2
- package/docs/gates/checks-test-layout.md +1 -1
- package/package.json +5 -7
- package/src/complexity/graphwalk.ts +374 -0
- package/src/complexity/knip.ts +2 -17
- package/src/core/gates.ts +2 -4
- package/src/core/lint.ts +40 -4
- package/src/core/main.ts +12 -8
- package/src/core/scope.ts +54 -0
- package/docs/gates/checks-exports.md +0 -104
- package/docs/gates/checks-imports.md +0 -64
- package/docs/gates/checks-unused.md +0 -66
- package/src/complexity/exports.ts +0 -189
- package/src/complexity/unused.ts +0 -49
- package/src/dependencies/imports.ts +0 -94
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
kind: reference
|
|
3
|
-
audience: consumers
|
|
4
|
-
---
|
|
5
|
-
# checks-exports
|
|
6
|
-
|
|
7
|
-
`checks-exports` is the gate that refuses an exported value or type nothing imports.
|
|
8
|
-
|
|
9
|
-
## What it checks
|
|
10
|
-
|
|
11
|
-
It runs Knip with the repository's own configuration and names each exported value and type no file imports.
|
|
12
|
-
It reads only the exports and types issue types, so an unreferenced file or an unused dependency never fails it.
|
|
13
|
-
It asks Knip for those issue types itself, so a configuration that narrows `include` or excludes them still has its exports judged.
|
|
14
|
-
A configuration that turns the exports or types rules off silences those symbols, so keep both rules on.
|
|
15
|
-
It holds each reported symbol against `exports-baseline.json`, and fails on any symbol the baseline does not hold.
|
|
16
|
-
It fails on any baseline entry the range adds unless Knip reports that same symbol unused at the base, so the baseline records debt that already existed and never debt the range creates.
|
|
17
|
-
It fails on any baseline entry Knip no longer reports, so the baseline only shrinks.
|
|
18
|
-
It fails when the repository tracks no TypeScript source, since an empty scan would pass without judging anything.
|
|
19
|
-
It fails when the repository holds no Knip configuration, since Knip's default entries cannot tell a dead export from a public one.
|
|
20
|
-
|
|
21
|
-
## What it reads
|
|
22
|
-
|
|
23
|
-
It runs Knip over the working tree, so an uncommitted file is judged like a committed one.
|
|
24
|
-
It reads the repository's Knip configuration, which names the entries.
|
|
25
|
-
It reads `exports-baseline.json` in the repository root, which holds each accepted unused export as a file, kind and name triple.
|
|
26
|
-
It reads `exports-baseline.json` again at the base of the range, where a commit without the file counts as empty.
|
|
27
|
-
When the range adds baseline entries, it runs Knip again on a temporary checkout of the base and removes the checkout when it ends.
|
|
28
|
-
That checkout links the repository's `node_modules`, so a configuration importing the kit's builder loads there too.
|
|
29
|
-
The repository writes `knip.config.ts` with the kit's builder, which requires `entry`, the files nothing imports:
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
import { defineConfig } from "@avi2dg/checks/knip";
|
|
33
|
-
|
|
34
|
-
export default defineConfig({ entry: ["src/index.ts"] });
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
The builder sets `include` to unreferenced files alone, and adds `tests/**/*.test.ts`, `dependency-cruiser.config.ts` and `oxlint.config.ts` to the entries.
|
|
38
|
-
The gate resolves the Knip binary from the installed kit, so a consumer installs nothing beyond the kit.
|
|
39
|
-
|
|
40
|
-
## Arguments
|
|
41
|
-
|
|
42
|
-
```sh
|
|
43
|
-
checks-exports <base-ref> <head-ref>
|
|
44
|
-
checks-exports <ref>
|
|
45
|
-
checks-exports --write
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
With two arguments the base is where the head branched off, at their merge-base.
|
|
49
|
-
When the checked-out commit is a merge of the head ref and one other commit, as a pull request's default checkout is, the base is that other commit instead.
|
|
50
|
-
So under `checks-lint` a pull request's merge checkout is judged against the base branch tip it merges, and a symbol that branch already baselined does not count as added.
|
|
51
|
-
With one the base is that commit's parent, or the empty tree for a repository's first commit, where nothing counts as already unused.
|
|
52
|
-
With `--write` it records every unused export and type Knip reports into `exports-baseline.json`, the way `oxlint --suppress-all` seeds `oxlint-suppressions.json`.
|
|
53
|
-
|
|
54
|
-
## Seeding a baseline
|
|
55
|
-
|
|
56
|
-
A repository adopting the gate runs `checks-exports --write` on a branch that changes no source and commits the file it writes.
|
|
57
|
-
The gate passes that range, since Knip reports every seeded symbol unused at the base too.
|
|
58
|
-
A seed written after the range adds a dead export, or after it removes the last import of one, fails on that symbol.
|
|
59
|
-
|
|
60
|
-
## Exit codes
|
|
61
|
-
|
|
62
|
-
| Code | When |
|
|
63
|
-
| --- | --- |
|
|
64
|
-
| 0 | no reported symbol is unlisted, no baseline entry is added or stale, or `--write` recorded the baseline |
|
|
65
|
-
| 1 | a reported symbol is unlisted, a baseline entry is added or stale or the repository holds no Knip configuration |
|
|
66
|
-
| 2 | a ref does not resolve, the repository tracks no TypeScript source, Knip cannot run, or a report or baseline does not parse |
|
|
67
|
-
|
|
68
|
-
## Sample output
|
|
69
|
-
|
|
70
|
-
```
|
|
71
|
-
exports: 2 unused export(s) not in exports-baseline.json:
|
|
72
|
-
src/used.ts: unusedExport (export)
|
|
73
|
-
src/used.ts: UnusedOptions (type)
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
A baseline entry the range adds fails when Knip does not report its symbol unused at the base:
|
|
77
|
-
|
|
78
|
-
```
|
|
79
|
-
exports: 1 exports-baseline.json export(s) the range adds that its base did not leave unused, remove the export instead:
|
|
80
|
-
src/used.ts: unusedExport (export)
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
A passing run with a held baseline counts what it holds:
|
|
84
|
-
|
|
85
|
-
```
|
|
86
|
-
exports: 2 unused export(s) in exports-baseline.json, and no new ones
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
A passing run with an empty baseline says so:
|
|
90
|
-
|
|
91
|
-
```
|
|
92
|
-
exports: no unused exports or types
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## When it runs
|
|
96
|
-
|
|
97
|
-
`checks-lint` runs it over each pull request's range when the repository tracks a `.ts` or `.tsx` file.
|
|
98
|
-
Such a repository names its entries in a Knip configuration.
|
|
99
|
-
|
|
100
|
-
## Related topics
|
|
101
|
-
|
|
102
|
-
- [checks-lint](checks-lint.md)
|
|
103
|
-
- [checks-suppressions-ratchet](checks-suppressions-ratchet.md)
|
|
104
|
-
- [checks-unused](checks-unused.md)
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
kind: reference
|
|
3
|
-
audience: consumers
|
|
4
|
-
---
|
|
5
|
-
# checks-imports
|
|
6
|
-
|
|
7
|
-
`checks-imports` is the gate that holds a repository's imports to the dependency rules, with the kit's defaults when the repository writes no config.
|
|
8
|
-
|
|
9
|
-
## What it checks
|
|
10
|
-
|
|
11
|
-
It runs dependency-cruiser over every tracked `.ts`, `.tsx`, `.mts` and `.cts` file and names each violation with its rule, the importing module and the module it reaches.
|
|
12
|
-
It fails on a violation whose rule has `error` severity, and lists a `warn` or `info` violation without failing.
|
|
13
|
-
The rules are those [The dependency rules](../configs/dependency-rules.md) lists, plus each rule the repository's config adds.
|
|
14
|
-
|
|
15
|
-
In a repository whose `package.json` lists `astro` under `dependencies` or `devDependencies`, the kit's defaults leave `no-orphans` out.
|
|
16
|
-
The cruise never reads an `.astro` file, so a `.ts` module only a page imports would read as an orphan.
|
|
17
|
-
[checks-unused](checks-unused.md) already names each file no entry reaches there, and it reads `.astro` imports.
|
|
18
|
-
A repository that wants the rule back lists its own `no-orphans` under `forbidden` in `dependency-cruiser.config.ts`.
|
|
19
|
-
|
|
20
|
-
## What it reads
|
|
21
|
-
|
|
22
|
-
It reads the working tree from the repository root, and leaves out a tracked file the working tree no longer holds.
|
|
23
|
-
It cruises against the first config it finds there:
|
|
24
|
-
|
|
25
|
-
1. `dependency-cruiser.config.ts`, which calls `defineConfig` from `@avi2dg/checks/dependency-cruiser`.
|
|
26
|
-
1. A config under one of dependency-cruiser's own names, such as `.dependency-cruiser.cjs`.
|
|
27
|
-
1. The kit's defaults, which `defineConfig()` returns with no argument.
|
|
28
|
-
|
|
29
|
-
It runs dependency-cruiser from the installed kit under Bun, which loads a TypeScript config wherever it sits.
|
|
30
|
-
|
|
31
|
-
## Arguments
|
|
32
|
-
|
|
33
|
-
It takes none.
|
|
34
|
-
|
|
35
|
-
## Exit codes
|
|
36
|
-
|
|
37
|
-
| Code | When |
|
|
38
|
-
| --- | --- |
|
|
39
|
-
| 0 | no import breaks an `error` rule |
|
|
40
|
-
| 1 | an import breaks an `error` rule |
|
|
41
|
-
| 2 | the repository tracks no TypeScript, or dependency-cruiser cannot load the config or run |
|
|
42
|
-
|
|
43
|
-
## Sample output
|
|
44
|
-
|
|
45
|
-
```
|
|
46
|
-
imports: 2 violation(s) in 36 module(s) cruised against dependency-cruiser.config.ts
|
|
47
|
-
error not-to-dev-dep: src/index.ts → node_modules/effect/dist/index.js
|
|
48
|
-
error no-orphans: src/lonely.ts
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
A passing run counts the modules it cruised:
|
|
52
|
-
|
|
53
|
-
```
|
|
54
|
-
imports: 225 module(s) cruised against dependency-cruiser.config.ts, no violation
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## When it runs
|
|
58
|
-
|
|
59
|
-
`checks-lint` runs it when the repository tracks a `.ts` or `.tsx` file, so a repository with no config of its own still has its imports judged.
|
|
60
|
-
|
|
61
|
-
## Related topics
|
|
62
|
-
|
|
63
|
-
- [The dependency rules](../configs/dependency-rules.md)
|
|
64
|
-
- [checks-lint](checks-lint.md)
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
kind: reference
|
|
3
|
-
audience: consumers
|
|
4
|
-
---
|
|
5
|
-
# checks-unused
|
|
6
|
-
|
|
7
|
-
`checks-unused` is the gate that refuses a TypeScript or Astro file no entry point reaches.
|
|
8
|
-
|
|
9
|
-
## What it checks
|
|
10
|
-
|
|
11
|
-
It runs Knip with the repository's own configuration and names each `.ts`, `.tsx` or `.astro` file no entry reaches.
|
|
12
|
-
It reads only the files issue type, so an unused export or dependency never fails it.
|
|
13
|
-
It asks Knip for that issue type itself, so a configuration that narrows `include`, excludes files or turns the files rule off still has its files judged.
|
|
14
|
-
It fails when the repository tracks no `.ts`, `.tsx` or `.astro` file, since an empty scan would pass without judging anything.
|
|
15
|
-
It fails when the repository holds no Knip configuration, since Knip's default entries cannot tell a dead file from an entry point.
|
|
16
|
-
|
|
17
|
-
## What it reads
|
|
18
|
-
|
|
19
|
-
It reads the working tree, so an uncommitted file is judged like a committed one.
|
|
20
|
-
It reads the repository's Knip configuration, which names the entries.
|
|
21
|
-
The repository writes `knip.config.ts` with the kit's builder, which requires `entry`, the files nothing imports:
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
import { defineConfig } from "@avi2dg/checks/knip";
|
|
25
|
-
|
|
26
|
-
export default defineConfig({ entry: ["src/index.ts"] });
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
The builder sets `include` to unreferenced files alone, and adds `tests/**/*.test.ts`, `dependency-cruiser.config.ts` and `oxlint.config.ts` to the entries.
|
|
30
|
-
In a repository that depends on `astro`, Knip reads `.astro` imports, so a file only an `.astro` entry imports counts as used and an `.astro` file no entry reaches is named.
|
|
31
|
-
The gate resolves the Knip binary from the installed kit, so a consumer installs nothing beyond the kit.
|
|
32
|
-
|
|
33
|
-
## Arguments
|
|
34
|
-
|
|
35
|
-
It takes none.
|
|
36
|
-
|
|
37
|
-
## Exit codes
|
|
38
|
-
|
|
39
|
-
| Code | When |
|
|
40
|
-
| --- | --- |
|
|
41
|
-
| 0 | no tracked file is unreferenced |
|
|
42
|
-
| 1 | a tracked file is unreferenced or the repository holds no Knip configuration |
|
|
43
|
-
| 2 | the repository tracks no TypeScript or Astro source, Knip cannot run, or its configuration does not parse |
|
|
44
|
-
|
|
45
|
-
## Sample output
|
|
46
|
-
|
|
47
|
-
```
|
|
48
|
-
unused: 1 unreferenced file(s):
|
|
49
|
-
src/planted-dead.ts
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
A passing run counts the files it judged:
|
|
53
|
-
|
|
54
|
-
```
|
|
55
|
-
unused: no unreferenced files among 110 tracked .ts/.tsx/.astro file(s)
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## When it runs
|
|
59
|
-
|
|
60
|
-
`checks-lint` runs it when the repository tracks a `.ts`, `.tsx` or `.astro` file.
|
|
61
|
-
Such a repository names its entries in a Knip configuration.
|
|
62
|
-
|
|
63
|
-
## Related topics
|
|
64
|
-
|
|
65
|
-
- [checks-lint](checks-lint.md)
|
|
66
|
-
- [checks-repetition](checks-repetition.md)
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import { Console, Effect, FileSystem, Path, Schema } from "effect";
|
|
3
|
-
import { TYPESCRIPT_SOURCE } from "../core/gates.ts";
|
|
4
|
-
import { checkoutFiles, commitOf, git, pathsAt, rangeEnds, refArgs } from "../core/git.ts";
|
|
5
|
-
import { runMain } from "../core/main.ts";
|
|
6
|
-
import { knipReport, scanTree } from "./knip.ts";
|
|
7
|
-
|
|
8
|
-
const UnusedSymbolSchema = Schema.Struct({
|
|
9
|
-
file: Schema.String,
|
|
10
|
-
kind: Schema.Literals(["export", "type"]),
|
|
11
|
-
name: Schema.String,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
export type UnusedSymbol = typeof UnusedSymbolSchema.Type;
|
|
15
|
-
|
|
16
|
-
export type Baseline = readonly UnusedSymbol[];
|
|
17
|
-
|
|
18
|
-
export type Held = {
|
|
19
|
-
readonly reported: readonly UnusedSymbol[];
|
|
20
|
-
readonly base: Baseline;
|
|
21
|
-
readonly head: Baseline;
|
|
22
|
-
readonly unusedAtBase: readonly UnusedSymbol[];
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type Drift = {
|
|
26
|
-
readonly unlisted: readonly UnusedSymbol[];
|
|
27
|
-
readonly added: readonly UnusedSymbol[];
|
|
28
|
-
readonly stale: readonly UnusedSymbol[];
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const NAME = "exports";
|
|
32
|
-
export const BASELINE_FILE = "exports-baseline.json";
|
|
33
|
-
const INCLUDED = ["--include", "exports,types"];
|
|
34
|
-
const WRITE = "--write";
|
|
35
|
-
const USAGE = `usage: exports.ts <ref> | <base-ref> <head-ref> | ${WRITE}`;
|
|
36
|
-
|
|
37
|
-
class ExportsError extends Schema.TaggedError<ExportsError>()("ExportsError", {
|
|
38
|
-
message: Schema.String,
|
|
39
|
-
}) {}
|
|
40
|
-
|
|
41
|
-
const KnipSymbol = Schema.Struct({ name: Schema.String });
|
|
42
|
-
|
|
43
|
-
const decodeKnipReport = Schema.decodeUnknownEffect(
|
|
44
|
-
Schema.fromJsonString(
|
|
45
|
-
Schema.Struct({
|
|
46
|
-
issues: Schema.Array(
|
|
47
|
-
Schema.Struct({
|
|
48
|
-
file: Schema.String,
|
|
49
|
-
exports: Schema.optional(Schema.Array(KnipSymbol)),
|
|
50
|
-
types: Schema.optional(Schema.Array(KnipSymbol)),
|
|
51
|
-
}),
|
|
52
|
-
),
|
|
53
|
-
}),
|
|
54
|
-
),
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
const decodeBaseline = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Array(UnusedSymbolSchema)));
|
|
58
|
-
|
|
59
|
-
function byPosition(a: UnusedSymbol, b: UnusedSymbol): number {
|
|
60
|
-
return a.file.localeCompare(b.file) || a.kind.localeCompare(b.kind) || a.name.localeCompare(b.name);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export const symbolsOf = Effect.fn("symbolsOf")(function* (stdout: string) {
|
|
64
|
-
const { issues } = yield* decodeKnipReport(stdout).pipe(
|
|
65
|
-
Effect.mapError((cause) => new ExportsError({ message: `knip's JSON report does not decode: ${cause.message}` })),
|
|
66
|
-
);
|
|
67
|
-
return issues
|
|
68
|
-
.flatMap(({ file, exports = [], types = [] }): readonly UnusedSymbol[] => [
|
|
69
|
-
...exports.map(({ name }): UnusedSymbol => ({ file, kind: "export", name })),
|
|
70
|
-
...types.map(({ name }): UnusedSymbol => ({ file, kind: "type", name })),
|
|
71
|
-
])
|
|
72
|
-
.toSorted(byPosition);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
export const baselineOf = Effect.fn("baselineOf")(function* (text: string, where: string) {
|
|
76
|
-
return yield* decodeBaseline(text).pipe(
|
|
77
|
-
Effect.mapError((cause) => new ExportsError({ message: `${where} does not decode: ${cause.message}` })),
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const baselinePath = Effect.fn("baselinePath")(function* (root: string) {
|
|
82
|
-
return (yield* Path.Path).join(root, BASELINE_FILE);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const baselineInTree = Effect.fn("baselineInTree")(function* (root: string) {
|
|
86
|
-
const fs = yield* FileSystem.FileSystem;
|
|
87
|
-
const at = yield* baselinePath(root);
|
|
88
|
-
if (!(yield* fs.exists(at))) return [] satisfies Baseline;
|
|
89
|
-
return yield* fs.readFileString(at).pipe(Effect.flatMap((text) => baselineOf(text, BASELINE_FILE)));
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const baselineAt = Effect.fn("baselineAt")(function* (treeish: string, root: string) {
|
|
93
|
-
const blob = (yield* git(["ls-tree", "--object-only", treeish, "--", BASELINE_FILE], root)).trim();
|
|
94
|
-
if (blob === "") return [] satisfies Baseline;
|
|
95
|
-
return yield* baselineOf(yield* git(["cat-file", "blob", blob], root), `${BASELINE_FILE} at ${treeish}`);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
const keyOf = ({ file, kind, name }: UnusedSymbol): string => `${kind}\0${file}\0${name}`;
|
|
99
|
-
|
|
100
|
-
function outside(symbols: readonly UnusedSymbol[], others: readonly UnusedSymbol[]): readonly UnusedSymbol[] {
|
|
101
|
-
const known = new Set(others.map(keyOf));
|
|
102
|
-
return symbols.filter((symbol) => !known.has(keyOf(symbol))).toSorted(byPosition);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function driftOf({ reported, base, head, unusedAtBase }: Held): Drift {
|
|
106
|
-
return {
|
|
107
|
-
unlisted: outside(reported, head),
|
|
108
|
-
added: outside(head, [...base, ...unusedAtBase]),
|
|
109
|
-
stale: outside(head, reported),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function describe(symbol: UnusedSymbol): string {
|
|
114
|
-
return ` ${symbol.file}: ${symbol.name} (${symbol.kind})`;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function report(head: Baseline, { unlisted, added, stale }: Drift): string {
|
|
118
|
-
const sections = [
|
|
119
|
-
{ symbols: unlisted, what: `unused export(s) not in ${BASELINE_FILE}:` },
|
|
120
|
-
{ symbols: added, what: `${BASELINE_FILE} export(s) the range adds that its base did not leave unused, remove the export instead:` },
|
|
121
|
-
{ symbols: stale, what: `${BASELINE_FILE} export(s) no longer reported, remove them:` },
|
|
122
|
-
].filter(({ symbols }) => symbols.length > 0);
|
|
123
|
-
if (sections.length > 0) {
|
|
124
|
-
return sections.flatMap(({ symbols, what }) => [`${NAME}: ${symbols.length} ${what}`, ...symbols.map(describe)]).join("\n");
|
|
125
|
-
}
|
|
126
|
-
return head.length === 0
|
|
127
|
-
? `${NAME}: no unused exports or types`
|
|
128
|
-
: `${NAME}: ${head.length} unused export(s) in ${BASELINE_FILE}, and no new ones`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const scan = scanTree(NAME, INCLUDED, TYPESCRIPT_SOURCE).pipe(Effect.mapError((cause) => new ExportsError({ message: cause.message })));
|
|
132
|
-
|
|
133
|
-
// Knip loads the configuration's imports, such as the kit's knip builder, from a node_modules the checkout lacks.
|
|
134
|
-
const unusedAt = Effect.fn("unusedAt")(
|
|
135
|
-
function* (rev: string, root: string) {
|
|
136
|
-
const files = yield* pathsAt(rev, [], root);
|
|
137
|
-
if (files.length === 0) return [] satisfies readonly UnusedSymbol[];
|
|
138
|
-
const fs = yield* FileSystem.FileSystem;
|
|
139
|
-
const path = yield* Path.Path;
|
|
140
|
-
const tree = yield* checkoutFiles(rev, files, yield* fs.makeTempDirectoryScoped({ prefix: "checks-exports-" }), root);
|
|
141
|
-
const modules = path.join(root, "node_modules");
|
|
142
|
-
if (yield* fs.exists(modules)) yield* fs.symlink(modules, path.join(tree, "node_modules"));
|
|
143
|
-
const reported = yield* knipReport(tree, INCLUDED).pipe(
|
|
144
|
-
Effect.mapError((cause) => new ExportsError({ message: `at ${rev}: ${cause.message}` })),
|
|
145
|
-
);
|
|
146
|
-
return reported.kind === "unconfigured" ? [] : yield* symbolsOf(reported.stdout);
|
|
147
|
-
},
|
|
148
|
-
Effect.scoped,
|
|
149
|
-
);
|
|
150
|
-
|
|
151
|
-
// Knip scans the checked-out tree, and a pull request's merge checkout holds the base branch tip beside the range head.
|
|
152
|
-
const judgedBase = Effect.fn("judgedBase")(function* (first: string, second: string | undefined, root: string) {
|
|
153
|
-
const { base } = yield* rangeEnds(first, second, root);
|
|
154
|
-
if (second === undefined) return base;
|
|
155
|
-
const head = yield* commitOf(second, root);
|
|
156
|
-
const parents = (yield* git(["rev-parse", "HEAD^@"], root)).split("\n").filter((line) => line !== "");
|
|
157
|
-
const [other, ...rest] = parents.filter((parent) => parent !== head);
|
|
158
|
-
return parents.includes(head) && other !== undefined && rest.length === 0 ? other : base;
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const check = Effect.fn("check")(function* (first: string, second: string | undefined) {
|
|
162
|
-
const { root, reported } = yield* scan;
|
|
163
|
-
if (reported.kind === "unconfigured") return false;
|
|
164
|
-
const baseRev = yield* judgedBase(first, second, root);
|
|
165
|
-
const base = yield* baselineAt(baseRev, root);
|
|
166
|
-
const head = yield* baselineInTree(root);
|
|
167
|
-
const unusedAtBase = outside(head, base).length === 0 ? [] : yield* unusedAt(baseRev, root);
|
|
168
|
-
const drift = driftOf({ reported: yield* symbolsOf(reported.stdout), base, head, unusedAtBase });
|
|
169
|
-
yield* Console.log(report(head, drift));
|
|
170
|
-
return drift.unlisted.length === 0 && drift.added.length === 0 && drift.stale.length === 0;
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
const seed = Effect.gen(function* () {
|
|
174
|
-
const { root, reported } = yield* scan;
|
|
175
|
-
if (reported.kind === "unconfigured") return false;
|
|
176
|
-
const symbols = yield* symbolsOf(reported.stdout);
|
|
177
|
-
yield* (yield* FileSystem.FileSystem).writeFileString(yield* baselinePath(root), `${JSON.stringify(symbols, null, 2)}\n`);
|
|
178
|
-
yield* Console.log(`${NAME}: wrote ${symbols.length} unused export(s) to ${BASELINE_FILE}`);
|
|
179
|
-
return true;
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
const gate = Effect.gen(function* () {
|
|
183
|
-
const args = process.argv.slice(2);
|
|
184
|
-
if (args.length === 1 && args[0] === WRITE) return yield* seed;
|
|
185
|
-
const { first, second } = yield* refArgs(args, USAGE);
|
|
186
|
-
return yield* check(first, second);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
if (import.meta.main) runMain(NAME, gate);
|
package/src/complexity/unused.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import { Console, Effect, Schema } from "effect";
|
|
3
|
-
import { LINTED_SOURCE } from "../core/gates.ts";
|
|
4
|
-
import { runMain } from "../core/main.ts";
|
|
5
|
-
import { scanTree } from "./knip.ts";
|
|
6
|
-
|
|
7
|
-
export type Scan = { readonly tracked: number; readonly files: readonly string[] };
|
|
8
|
-
|
|
9
|
-
const NAME = "unused";
|
|
10
|
-
const JUDGED = /[.]tsx?$|[.]astro$/;
|
|
11
|
-
|
|
12
|
-
class UnusedError extends Schema.TaggedError<UnusedError>()("UnusedError", {
|
|
13
|
-
message: Schema.String,
|
|
14
|
-
}) {}
|
|
15
|
-
|
|
16
|
-
const decodeKnipReport = Schema.decodeUnknownEffect(
|
|
17
|
-
Schema.fromJsonString(
|
|
18
|
-
Schema.Struct({
|
|
19
|
-
issues: Schema.Array(Schema.Struct({ files: Schema.optional(Schema.Array(Schema.Struct({ name: Schema.String }))) })),
|
|
20
|
-
}),
|
|
21
|
-
),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
export const filesOf = Effect.fn("filesOf")(function* (stdout: string) {
|
|
25
|
-
const { issues } = yield* decodeKnipReport(stdout).pipe(
|
|
26
|
-
Effect.mapError((cause) => new UnusedError({ message: `knip's JSON report does not decode: ${cause.message}` })),
|
|
27
|
-
);
|
|
28
|
-
return issues
|
|
29
|
-
.flatMap(({ files = [] }) => files.map(({ name }) => name))
|
|
30
|
-
.filter((file) => JUDGED.test(file))
|
|
31
|
-
.toSorted();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
export function report({ tracked, files }: Scan): string {
|
|
35
|
-
if (files.length === 0) return `${NAME}: no unreferenced files among ${tracked} tracked .ts/.tsx/.astro file(s)`;
|
|
36
|
-
return [`${NAME}: ${files.length} unreferenced file(s):`, ...files.map((file) => ` ${file}`)].join("\n");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const unused = Effect.gen(function* () {
|
|
40
|
-
const { tracked, reported } = yield* scanTree(NAME, ["--files"], LINTED_SOURCE).pipe(
|
|
41
|
-
Effect.mapError((cause) => new UnusedError({ message: cause.message })),
|
|
42
|
-
);
|
|
43
|
-
if (reported.kind === "unconfigured") return false;
|
|
44
|
-
const files = yield* filesOf(reported.stdout);
|
|
45
|
-
yield* Console.log(report({ tracked, files }));
|
|
46
|
-
return files.length === 0;
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
if (import.meta.main) runMain(NAME, unused);
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import { Console, Effect, FileSystem, Path, Schema } from "effect";
|
|
3
|
-
import { collect, git } from "../core/git.ts";
|
|
4
|
-
import { runMain, Usage } from "../core/main.ts";
|
|
5
|
-
|
|
6
|
-
export const PROJECT_CONFIG = "dependency-cruiser.config.ts";
|
|
7
|
-
|
|
8
|
-
export const OWN_CONFIGS = [
|
|
9
|
-
".dependency-cruiser.json",
|
|
10
|
-
".dependency-cruiser.js",
|
|
11
|
-
".dependency-cruiser.cjs",
|
|
12
|
-
".dependency-cruiser.mjs",
|
|
13
|
-
".dependency-cruiser.ts",
|
|
14
|
-
".dependency-cruiser.cts",
|
|
15
|
-
".dependency-cruiser.mts",
|
|
16
|
-
] as const;
|
|
17
|
-
|
|
18
|
-
export const CRUISED = ["*.ts", "*.tsx", "*.mts", "*.cts"] as const;
|
|
19
|
-
|
|
20
|
-
const NAME = "imports";
|
|
21
|
-
const USAGE = "usage: imports.ts";
|
|
22
|
-
const KIT_DEFAULTS = "kit-defaults.ts";
|
|
23
|
-
|
|
24
|
-
class ImportsError extends Schema.TaggedError<ImportsError>()("ImportsError", {
|
|
25
|
-
message: Schema.String,
|
|
26
|
-
}) {}
|
|
27
|
-
|
|
28
|
-
const Violation = Schema.Struct({
|
|
29
|
-
from: Schema.String,
|
|
30
|
-
to: Schema.String,
|
|
31
|
-
rule: Schema.Struct({ name: Schema.String, severity: Schema.String }),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
export type Violation = typeof Violation.Type;
|
|
35
|
-
|
|
36
|
-
const decodeCruise = Schema.decodeUnknownEffect(
|
|
37
|
-
Schema.fromJsonString(
|
|
38
|
-
Schema.Struct({
|
|
39
|
-
summary: Schema.Struct({ violations: Schema.Array(Violation), error: Schema.Int, totalCruised: Schema.Int }),
|
|
40
|
-
}),
|
|
41
|
-
),
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
export function describeViolation({ from, to, rule }: Violation): string {
|
|
45
|
-
return from === to ? ` ${rule.severity} ${rule.name}: ${from}` : ` ${rule.severity} ${rule.name}: ${from} → ${to}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const configOf = Effect.fn("configOf")(function* (root: string) {
|
|
49
|
-
const fs = yield* FileSystem.FileSystem;
|
|
50
|
-
const path = yield* Path.Path;
|
|
51
|
-
for (const name of [PROJECT_CONFIG, ...OWN_CONFIGS]) {
|
|
52
|
-
if (yield* fs.exists(path.join(root, name))) return { file: name, shown: name };
|
|
53
|
-
}
|
|
54
|
-
return { file: path.join(import.meta.dir, KIT_DEFAULTS), shown: "the kit's defaults" };
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
const depcruise = Effect.fn("depcruise")(function* () {
|
|
58
|
-
const path = yield* Path.Path;
|
|
59
|
-
const main = yield* Effect.try({
|
|
60
|
-
try: () => new URL("../../bin/dependency-cruiser.mjs", import.meta.resolve("dependency-cruiser")),
|
|
61
|
-
catch: () => new ImportsError({ message: "cannot resolve dependency-cruiser, a peer dependency of the kit" }),
|
|
62
|
-
});
|
|
63
|
-
return yield* path.fromFileUrl(main);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const trackedInTree = Effect.fn("trackedInTree")(function* (root: string) {
|
|
67
|
-
const fs = yield* FileSystem.FileSystem;
|
|
68
|
-
const path = yield* Path.Path;
|
|
69
|
-
const listed = yield* git(["ls-files", "-z", "--", ...CRUISED], root);
|
|
70
|
-
return yield* Effect.filter(listed.split("\0").filter(Boolean), (file) => fs.exists(path.join(root, file)), { concurrency: "unbounded" });
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const imports = Effect.gen(function* () {
|
|
74
|
-
if (process.argv.length > 2) return yield* new Usage({ message: USAGE });
|
|
75
|
-
const root = (yield* git(["rev-parse", "--show-toplevel"])).trim();
|
|
76
|
-
const files = yield* trackedInTree(root);
|
|
77
|
-
if (files.length === 0) return yield* new ImportsError({ message: "no tracked TypeScript to cruise" });
|
|
78
|
-
const config = yield* configOf(root);
|
|
79
|
-
const run = yield* collect(process.execPath, [yield* depcruise(), "--config", config.file, "--output-type", "json", ...files], root).pipe(
|
|
80
|
-
Effect.mapError((cause) => new ImportsError({ message: `cannot run dependency-cruiser: ${cause.message}` })),
|
|
81
|
-
);
|
|
82
|
-
const { summary } = yield* decodeCruise(run.stdout).pipe(
|
|
83
|
-
Effect.mapError(() => new ImportsError({ message: `dependency-cruiser exits ${run.exitCode} with no report: ${(run.stdout + run.stderr).trim()}` })),
|
|
84
|
-
);
|
|
85
|
-
const cruised = `${summary.totalCruised} module(s) cruised against ${config.shown}`;
|
|
86
|
-
if (summary.violations.length === 0) {
|
|
87
|
-
yield* Console.log(`${NAME}: ${cruised}, no violation`);
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
yield* Console.error([`${NAME}: ${summary.violations.length} violation(s) in ${cruised}`, ...summary.violations.map(describeViolation)].join("\n"));
|
|
91
|
-
return summary.error === 0;
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
if (import.meta.main) runMain(NAME, imports);
|