am-i-done 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/package.json +1 -1
- package/src/config.ts +6 -2
- package/src/engine/engine.ts +39 -10
- package/src/engine/evaluate.ts +6 -1
- package/src/engine/rule.ts +6 -2
- package/templates/done.toml +4 -4
package/README.md
CHANGED
|
@@ -159,7 +159,8 @@ A `check` rule returns `ctx.checked()` when it inspected the file and `ctx.skipp
|
|
|
159
159
|
was out of scope. Its return type is a brand only the engine can mint, so a bare `return` does not
|
|
160
160
|
compile.
|
|
161
161
|
|
|
162
|
-
**Syntax trees.** A check rule that sets `ast: true` can call `ctx.ast()` for the file's tree,
|
|
162
|
+
**Syntax trees.** A check rule that sets `ast: true` can call `ctx.ast()` for the file's tree (or `ctx.ast(path)` for another loaded file's, to follow an
|
|
163
|
+
import or a re-export), in
|
|
163
164
|
[TSESTree](https://typescript-eslint.io/packages/typescript-estree/) form (the ESTree dialect
|
|
164
165
|
typescript-eslint defines), with `range`, `loc` and `parent` on every node and the comments on the
|
|
165
166
|
program. It is parsed once per file and shared by every rule. The parser is TypeScript (through
|
|
@@ -234,6 +235,8 @@ reason = "the public API re-exports its types until the next major (#123)"
|
|
|
234
235
|
```
|
|
235
236
|
|
|
236
237
|
It has the same checks: the reason cites an issue, and a waiver that waives nothing fails the run.
|
|
238
|
+
Leave `match` out to waive the rule for the whole file, for one that is never edited (vendored or
|
|
239
|
+
hash-pinned code).
|
|
237
240
|
|
|
238
241
|
### Built-in rules
|
|
239
242
|
|
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -10,6 +10,9 @@ const ids = z.array(z.string().min(1));
|
|
|
10
10
|
/** A stage or rule left out, and why: `{ id = "lint", reason = "… (#12)" }`. */
|
|
11
11
|
const skips = z.array(z.strictObject({ id: z.string().min(1), reason: z.string().min(1) }));
|
|
12
12
|
|
|
13
|
+
/** `[rules] include` by default: every file Bun runs as a script. The built-in rules' fixtures assume it. */
|
|
14
|
+
export const DEFAULT_INCLUDE = ["**/*.{ts,tsx,mts,cts,js,jsx,mjs,cjs}"] as const;
|
|
15
|
+
|
|
13
16
|
const DEFAULT_CATEGORIES = {
|
|
14
17
|
architecture: "where code lives and what it may import",
|
|
15
18
|
conventions: "house style and banned escape hatches",
|
|
@@ -59,7 +62,7 @@ const schema = z.strictObject({
|
|
|
59
62
|
skip: skips.default([]),
|
|
60
63
|
dirs: z.array(z.string().min(1)).default(["scripts/rules"]),
|
|
61
64
|
/** Files every rule sees unless it names its own `files`. */
|
|
62
|
-
include: z.array(z.string().min(1)).default([
|
|
65
|
+
include: z.array(z.string().min(1)).default([...DEFAULT_INCLUDE]),
|
|
63
66
|
/** Excluded in addition to everything git ignores. */
|
|
64
67
|
exclude: z.array(z.string().min(1)).default([]),
|
|
65
68
|
categories: z.record(z.string().min(1), z.string()).default(DEFAULT_CATEGORIES),
|
|
@@ -81,7 +84,8 @@ const schema = z.strictObject({
|
|
|
81
84
|
z.strictObject({
|
|
82
85
|
rule: z.string().min(1),
|
|
83
86
|
file: z.string().min(1),
|
|
84
|
-
|
|
87
|
+
/** Text on the violating line. Absent: the whole file, for one that is never edited. */
|
|
88
|
+
match: z.string().min(1).optional(),
|
|
85
89
|
reason: z.string().min(1),
|
|
86
90
|
}),
|
|
87
91
|
)
|
package/src/engine/engine.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { join, relative } from "node:path";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import type
|
|
3
|
+
import { type Config, DEFAULT_INCLUDE } from "../config.js";
|
|
4
4
|
import { ID, loadModules } from "../modules.js";
|
|
5
5
|
import { today } from "../today.js";
|
|
6
6
|
import { warnUntilProblems, warnUntilSchema } from "../waivers.js";
|
|
7
|
-
import { chooseParser, loadParser } from "./ast.js";
|
|
7
|
+
import { chooseParser, loadParser, type Parser } from "./ast.js";
|
|
8
8
|
import { evaluate, type Workspace, wantsTests, workspace } from "./evaluate.js";
|
|
9
9
|
import { fileMeta, listFiles, matchesAny } from "./files.js";
|
|
10
10
|
import { checkFixtures, FIXTURE_DIR, loadFixtures } from "./fixtures.js";
|
|
@@ -44,6 +44,8 @@ export interface RuleSet {
|
|
|
44
44
|
dirs: string[];
|
|
45
45
|
/** Every rule id defined, including skipped ones. */
|
|
46
46
|
known: Set<string>;
|
|
47
|
+
/** Ids of the rules that came from the built-in directory. */
|
|
48
|
+
builtin: Set<string>;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
/** The built-in rules (when recommended) and the project's, minus `[rules] skip`. A project rule may not reuse a built-in's id. */
|
|
@@ -63,7 +65,8 @@ export async function loadRules(config: Config, builtinDir: string = BUILTIN_RUL
|
|
|
63
65
|
];
|
|
64
66
|
if (problems.length > 0) throw new Error(problems.join("\n"));
|
|
65
67
|
const rules = loaded.map((m) => m.value).filter((r) => !skipped.has(r.id));
|
|
66
|
-
|
|
68
|
+
const builtin = new Set(loaded.filter((m) => m.path.startsWith(`${builtinDir}/`)).map((m) => m.value.id));
|
|
69
|
+
return { rules: rules.sort((a, b) => a.id.localeCompare(b.id)), dirs: roots, known, builtin };
|
|
67
70
|
}
|
|
68
71
|
|
|
69
72
|
export interface EngineOptions {
|
|
@@ -112,15 +115,36 @@ export async function runEngine(config: Config, set: RuleSet, options: EngineOpt
|
|
|
112
115
|
...scanned.crashes,
|
|
113
116
|
...waived.unused,
|
|
114
117
|
...(full ? liveness(rules, scanned.counts, files) : []),
|
|
115
|
-
...
|
|
116
|
-
requireIssue,
|
|
117
|
-
include: config.rules.include,
|
|
118
|
-
...(parser === undefined ? {} : { parser }),
|
|
119
|
-
}),
|
|
118
|
+
...fixtureProblems(rules, set, config, parser),
|
|
120
119
|
];
|
|
121
120
|
return { violations: waived.violations, warnings: waived.warnings, problems, rules, files: files.size };
|
|
122
121
|
}
|
|
123
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Every rule's fixtures. A built-in rule's were written for the default `[rules] include`, so they
|
|
125
|
+
* are judged by it: a project that narrows its include narrows what the rules see, not what their
|
|
126
|
+
* fixtures may claim.
|
|
127
|
+
*/
|
|
128
|
+
function fixtureProblems(rules: readonly Rule[], set: RuleSet, config: Config, parser: Parser | undefined): string[] {
|
|
129
|
+
const base = { requireIssue: config.suppression.require_issue, ...(parser === undefined ? {} : { parser }) };
|
|
130
|
+
const fixtures = loadFixtures(set.dirs);
|
|
131
|
+
const builtIn = (id: string) => set.builtin.has(id);
|
|
132
|
+
return [
|
|
133
|
+
...checkFixtures(
|
|
134
|
+
rules.filter((r) => builtIn(r.id)),
|
|
135
|
+
fixtures.filter((f) => builtIn(f.ruleId)),
|
|
136
|
+
set.known,
|
|
137
|
+
{ ...base, include: DEFAULT_INCLUDE },
|
|
138
|
+
),
|
|
139
|
+
...checkFixtures(
|
|
140
|
+
rules.filter((r) => !builtIn(r.id)),
|
|
141
|
+
fixtures.filter((f) => !builtIn(f.ruleId)),
|
|
142
|
+
set.known,
|
|
143
|
+
{ ...base, include: config.rules.include },
|
|
144
|
+
),
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
|
|
124
148
|
type Waiver = Config["suppression"]["waive"][number];
|
|
125
149
|
|
|
126
150
|
/**
|
|
@@ -138,7 +162,9 @@ function applyWaivers(
|
|
|
138
162
|
const used = new Set<Waiver>();
|
|
139
163
|
const lineOf = (v: Violation) => files.get(v.file)?.content.split("\n")[v.line - 1] ?? "";
|
|
140
164
|
const keep = (v: Violation) => {
|
|
141
|
-
const waiver = waivers.find(
|
|
165
|
+
const waiver = waivers.find(
|
|
166
|
+
(w) => w.rule === v.rule.id && w.file === v.file && (w.match === undefined || lineOf(v).includes(w.match)),
|
|
167
|
+
);
|
|
142
168
|
if (waiver !== undefined) used.add(waiver);
|
|
143
169
|
return waiver === undefined;
|
|
144
170
|
};
|
|
@@ -146,7 +172,10 @@ function applyWaivers(
|
|
|
146
172
|
const warnings = found.warnings.filter(keep);
|
|
147
173
|
const unused = waivers
|
|
148
174
|
.filter((w) => !used.has(w) && rules.some((r) => r.id === w.rule) && files.has(w.file))
|
|
149
|
-
.map(
|
|
175
|
+
.map(
|
|
176
|
+
(w) =>
|
|
177
|
+
`[[suppression.waive]] ${w.rule} in ${w.file}${w.match === undefined ? "" : ` ("${w.match}")`} waives nothing: delete it`,
|
|
178
|
+
);
|
|
150
179
|
const unknown = waivers
|
|
151
180
|
.filter((w) => full && !files.has(w.file) && rules.some((r) => r.id === w.rule))
|
|
152
181
|
.map((w) => `[[suppression.waive]] ${w.rule}: ${w.file} is not a file the rules check`);
|
package/src/engine/evaluate.ts
CHANGED
|
@@ -125,7 +125,12 @@ export function evaluate(rule: Rule, file: FileMeta, ws: Workspace): Evaluation
|
|
|
125
125
|
violated,
|
|
126
126
|
checked: mintChecked,
|
|
127
127
|
skipped: mintSkipped,
|
|
128
|
-
ast: () =>
|
|
128
|
+
ast: (path?: string) => {
|
|
129
|
+
if (path === undefined) return ws.ast(file);
|
|
130
|
+
const other = ws.files.get(path);
|
|
131
|
+
if (other === undefined) throw new Error(`ctx.ast("${path}"): not a loaded file`);
|
|
132
|
+
return ws.ast(other);
|
|
133
|
+
},
|
|
129
134
|
graph: () => ws.graph(),
|
|
130
135
|
};
|
|
131
136
|
let checked: boolean;
|
package/src/engine/rule.ts
CHANGED
|
@@ -47,8 +47,12 @@ export interface RuleContext {
|
|
|
47
47
|
checked(): Verdict;
|
|
48
48
|
/** This file is out of scope. Claims no work. */
|
|
49
49
|
skipped(): Verdict;
|
|
50
|
-
/**
|
|
51
|
-
|
|
50
|
+
/**
|
|
51
|
+
* A file's syntax tree (TSESTree), parsed once and shared by every rule. Needs `ast: true`. With
|
|
52
|
+
* a path (relative to the root, as in `files`), another loaded file's: for a rule that follows an
|
|
53
|
+
* import or a re-export. Throws for a path that is not loaded.
|
|
54
|
+
*/
|
|
55
|
+
ast(path?: string): Ast;
|
|
52
56
|
/** The project's import graph, built once per run. */
|
|
53
57
|
graph(): ImportGraph;
|
|
54
58
|
}
|
package/templates/done.toml
CHANGED
|
@@ -9,21 +9,21 @@ rerun = "diagnose" # a failure is run again to see if it repeats, and fai
|
|
|
9
9
|
[stages]
|
|
10
10
|
recommended = true # bun-version, typecheck, lint, rules, test: each where its tool is set up
|
|
11
11
|
dirs = ["scripts/stages"] # this project's *.stage.ts
|
|
12
|
-
# skip = [{ id = "lint", reason = "<why>, until
|
|
12
|
+
# skip = [{ id = "lint", reason = "<why>, until #<issue>" }]
|
|
13
13
|
|
|
14
14
|
[rules]
|
|
15
15
|
recommended = true # the built-in rules (doing-it-wrong --list)
|
|
16
16
|
dirs = ["scripts/rules"] # this project's *.rule.ts, with fixtures in scripts/rules/fixtures/
|
|
17
|
-
# skip = [{ id = "no-as-any", reason = "<why>, until
|
|
17
|
+
# skip = [{ id = "no-as-any", reason = "<why>, until #<issue>" }]
|
|
18
18
|
|
|
19
19
|
[suppression]
|
|
20
|
-
require_issue = true # every waiver cites an issue: // doing-it-wrong-ignore <rule>: <reason> (
|
|
20
|
+
require_issue = true # every waiver cites an issue: // doing-it-wrong-ignore <rule>: <reason> (#<issue>)
|
|
21
21
|
# A file that cannot carry that comment (generated, JSON) is waived here instead:
|
|
22
22
|
# [[suppression.waive]]
|
|
23
23
|
# rule = "<id>"
|
|
24
24
|
# file = "<path>"
|
|
25
25
|
# match = "<text on the line>"
|
|
26
|
-
# reason = "<why> (
|
|
26
|
+
# reason = "<why> (#<issue>)"
|
|
27
27
|
|
|
28
28
|
[cache]
|
|
29
29
|
skip = true # a local rerun of an unchanged tree is skipped; never in CI
|