@drkmttr/adhere 0.6.1 → 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 +77 -31
- package/package.json +7 -7
- package/presets/effect/basics/external-calls-are-resilient.md +6 -2
- package/presets/effect/basics/fn-for-named-effects.md +10 -2
- package/presets/effect/basics/gen-for-sequencing.md +10 -2
- package/presets/effect/basics/instrument-with-pipe.md +9 -2
- package/presets/effect/cli/handlers-delegate.md +13 -2
- package/presets/effect/config/business-logic-depends-on-config-service.md +9 -2
- package/presets/effect/config/secrets-are-redacted.md +6 -2
- package/presets/effect/config/tests-provide-values-directly.md +8 -2
- package/presets/effect/config/validate-with-schema.md +9 -2
- package/presets/effect/data/brand-meaningful-primitives.md +11 -2
- package/presets/effect/data/decode-json-with-schema.md +6 -2
- package/presets/effect/data/records-are-schema-classes.md +9 -2
- package/presets/effect/data/variants-are-tagged-unions.md +9 -2
- package/presets/effect/errors/catch-defects-at-boundaries-only.md +6 -2
- package/presets/effect/errors/defects-for-bugs.md +13 -2
- package/presets/effect/errors/domain-errors-are-tagged.md +6 -2
- package/presets/effect/errors/wrap-external-errors.md +7 -2
- package/presets/effect/platform/os-access-through-platform-services.md +8 -2
- package/presets/effect/services/fresh-layer-per-test.md +9 -2
- package/presets/effect/services/memoize-parameterized-layers.md +9 -2
- package/presets/effect/services/methods-have-no-requirements.md +9 -2
- package/presets/effect/services/no-mutable-state.md +9 -2
- package/presets/effect/services/provide-at-entry.md +7 -2
- package/presets/effect/services/test-layers-are-in-memory.md +12 -2
- package/presets/effect/testing/test-clock-for-time.md +11 -2
- package/presets/effect/testing/test-random.md +9 -2
- package/skills/adhere/SKILL.md +24 -17
- package/src/cli.ts +71 -36
- package/src/config.ts +72 -13
- package/src/init.ts +10 -8
- package/src/markdown.ts +59 -25
- package/src/services/Jev.http.ts +27 -5
- package/src/services/Jev.ts +45 -31
- package/src/services/SourceWalker.ts +47 -23
- package/src/workflows/audit.ts +108 -61
- package/src/workflows/format.ts +51 -8
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: The result of a parameterized layer constructor
|
|
2
|
+
description: The result of a parameterized layer constructor must be stored in a module constant before it is used in more than one place, never constructed again for each use.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
const postgresLayer = Postgres.layer({ url: "postgres://localhost/mydb", poolSize: 10 });
|
|
7
7
|
|
|
8
8
|
const goodAppLayer = Layer.merge(
|
|
@@ -10,3 +10,10 @@ const goodAppLayer = Layer.merge(
|
|
|
10
10
|
OrderRepo.layer.pipe(Layer.provide(postgresLayer)),
|
|
11
11
|
);
|
|
12
12
|
```
|
|
13
|
+
|
|
14
|
+
```ts never
|
|
15
|
+
const Live = Layer.merge(
|
|
16
|
+
Sessions.layer.pipe(Layer.provide(Redis.layer({ host: "localhost" }))),
|
|
17
|
+
RateLimits.layer.pipe(Layer.provide(Redis.layer({ host: "localhost" }))),
|
|
18
|
+
);
|
|
19
|
+
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: Service method signatures have no requirements. Dependencies
|
|
2
|
+
description: Service method signatures must have no requirements. Dependencies must be acquired inside the layer, never declared on the method.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
class Database extends Context.Service<
|
|
7
7
|
Database,
|
|
8
8
|
{
|
|
@@ -11,3 +11,10 @@ class Database extends Context.Service<
|
|
|
11
11
|
}
|
|
12
12
|
>()("@app/Database") {}
|
|
13
13
|
```
|
|
14
|
+
|
|
15
|
+
```ts never
|
|
16
|
+
class Orders extends Context.Service<
|
|
17
|
+
Orders,
|
|
18
|
+
{ readonly save: (order: Order) => Effect.Effect<void, SaveError, Database> }
|
|
19
|
+
>()("@app/Orders") {}
|
|
20
|
+
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: A service
|
|
2
|
+
description: A service must expose only readonly members, never mutable state.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
class Logger extends Context.Service<
|
|
7
7
|
Logger,
|
|
8
8
|
{
|
|
@@ -10,3 +10,10 @@ class Logger extends Context.Service<
|
|
|
10
10
|
}
|
|
11
11
|
>()("@app/Logger") {}
|
|
12
12
|
```
|
|
13
|
+
|
|
14
|
+
```ts never
|
|
15
|
+
class Counter extends Context.Service<
|
|
16
|
+
Counter,
|
|
17
|
+
{ count: number; readonly increment: () => Effect.Effect<void> }
|
|
18
|
+
>()("@app/Counter") {}
|
|
19
|
+
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: Layers
|
|
2
|
+
description: Layers must be provided once at the program entry. A module that is not an entry point must never call Effect.provide.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
const appLayer = userServiceLayer.pipe(
|
|
7
7
|
Layer.provideMerge(databaseLayer),
|
|
8
8
|
Layer.provideMerge(loggerLayer),
|
|
@@ -16,3 +16,8 @@ const program = Effect.gen(function* () {
|
|
|
16
16
|
|
|
17
17
|
const main = program.pipe(Effect.provide(appLayer));
|
|
18
18
|
```
|
|
19
|
+
|
|
20
|
+
```ts never
|
|
21
|
+
export const getUser = (id: string) =>
|
|
22
|
+
UserRepo.find(id).pipe(Effect.provide(UserRepo.layer));
|
|
23
|
+
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: A test implementation of a service
|
|
2
|
+
description: A test implementation of a service must be built with Layer.sync or Layer.succeed over in-memory state, never over a real database, network, or file.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
static readonly testLayer = Layer.sync(Cache, () => {
|
|
7
7
|
const store = new Map<string, string>()
|
|
8
8
|
|
|
@@ -12,3 +12,13 @@ static readonly testLayer = Layer.sync(Cache, () => {
|
|
|
12
12
|
return { get, set }
|
|
13
13
|
})
|
|
14
14
|
```
|
|
15
|
+
|
|
16
|
+
```ts never
|
|
17
|
+
const EmailTest = Layer.effect(
|
|
18
|
+
Email,
|
|
19
|
+
Effect.gen(function* () {
|
|
20
|
+
const smtp = yield* SmtpClient;
|
|
21
|
+
return Email.of({ send: (message) => smtp.deliver(message) });
|
|
22
|
+
}),
|
|
23
|
+
).pipe(Layer.provide(SmtpClient.layer({ host: "localhost", port: 1025 })));
|
|
24
|
+
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: A test that depends on time
|
|
2
|
+
description: A test that depends on time must use TestClock. Real sleeps and it.live must be used only when real time is required.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
it.effect("time-based test", () =>
|
|
7
7
|
Effect.gen(function* () {
|
|
8
8
|
const fiber = yield* Effect.delay(Effect.succeed("done"), "10 seconds").pipe(Effect.forkChild);
|
|
@@ -12,3 +12,12 @@ it.effect("time-based test", () =>
|
|
|
12
12
|
}),
|
|
13
13
|
);
|
|
14
14
|
```
|
|
15
|
+
|
|
16
|
+
```ts never
|
|
17
|
+
it.live("retries after a delay", () =>
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
yield* Effect.sleep("2 seconds");
|
|
20
|
+
expect(yield* attempts).toBe(2);
|
|
21
|
+
}),
|
|
22
|
+
);
|
|
23
|
+
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: A test that depends on randomness
|
|
2
|
+
description: A test that depends on randomness must control it with a fixed seed through Random.withSeed, never with Math.random or an unseeded Random.
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
```ts
|
|
5
|
+
```ts must
|
|
6
6
|
const program = Effect.gen(function* () {
|
|
7
7
|
const value1 = yield* Random.next;
|
|
8
8
|
const value2 = yield* Random.next;
|
|
@@ -13,3 +13,10 @@ await Effect.runPromise(
|
|
|
13
13
|
Effect.all([program.pipe(Random.withSeed("my-seed")), program.pipe(Random.withSeed("my-seed"))]),
|
|
14
14
|
); // => [[0.018368576514773527, 0.4010840628128671], [0.018368576514773527, 0.4010840628128671]]
|
|
15
15
|
```
|
|
16
|
+
|
|
17
|
+
```ts never
|
|
18
|
+
it("creates unique ids", () => {
|
|
19
|
+
const id = Math.random().toString(36).slice(2);
|
|
20
|
+
expect(id).toHaveLength(11);
|
|
21
|
+
});
|
|
22
|
+
```
|
package/skills/adhere/SKILL.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: adhere
|
|
3
|
-
description: Gather a repository's non-deterministic coding conventions into adhere rules (Markdown files with a description and
|
|
3
|
+
description: Gather a repository's non-deterministic coding conventions into adhere rules (Markdown files with a description and code that must, or must never, be written, judged by Jev), configure adhere, run it, and calibrate thresholds. Use when setting up adhere in a repo, when the user asks to turn conventions, AGENTS.md guidance, ADRs, or review feedback into lint rules a normal linter cannot express, or when tuning adhere findings.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Gather adhere rules from a repo
|
|
7
7
|
|
|
8
8
|
adhere is a linter for rules a normal linter cannot check. Each rule is one
|
|
9
|
-
sentence
|
|
10
|
-
and returns a probability. Your job is to find the conventions a repo already
|
|
9
|
+
sentence in RFC 2119's words plus code that must be written and code that must
|
|
10
|
+
never be; Jev judges every source file against it and returns a probability. Your job is to find the conventions a repo already
|
|
11
11
|
has, write the ones that need judgment as rule files, and tune until the
|
|
12
12
|
report is trustworthy.
|
|
13
13
|
|
|
@@ -52,32 +52,39 @@ The path without `.md` is the rule id.
|
|
|
52
52
|
|
|
53
53
|
````md
|
|
54
54
|
---
|
|
55
|
-
description: One sentence
|
|
55
|
+
description: One sentence saying what code must be, then what it must never be.
|
|
56
56
|
threshold: 0.8
|
|
57
57
|
---
|
|
58
58
|
|
|
59
59
|
Optional prose for readers on GitHub. adhere ignores it.
|
|
60
60
|
|
|
61
|
-
```ts
|
|
61
|
+
```ts must
|
|
62
62
|
// Correct code lifted from this repo, trimmed to the pattern.
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
```ts
|
|
65
|
+
```ts never
|
|
66
66
|
// Optional: the incorrect form this rule catches, as it appeared in the repo.
|
|
67
67
|
```
|
|
68
68
|
````
|
|
69
69
|
|
|
70
70
|
Rules for the rule:
|
|
71
71
|
|
|
72
|
-
- `description` is one sentence,
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
- `description` is one sentence, specific, in the same words as the fences:
|
|
73
|
+
what code must be, and what it must never be. Jev reads the description
|
|
74
|
+
with the code, and "must" in both is one demand; vague words ("properly",
|
|
75
|
+
"correctly") give it nothing to judge.
|
|
76
|
+
- The `must` block is real code from the repo, not invented, and only correct
|
|
75
77
|
code: Jev compares files to it, so incorrect code there teaches the wrong
|
|
76
|
-
thing. Incorrect code goes in a fence tagged `
|
|
77
|
-
a violation looks like. Add one
|
|
78
|
-
ideally one found in the repo's history
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
thing. Incorrect code goes in a fence tagged `never`, which Jev reads as what
|
|
79
|
+
a violation looks like. Add one whenever violations have a recognizable
|
|
80
|
+
shape, ideally one found in the repo's history: over the effect preset, a
|
|
81
|
+
`never` block raised how well Jev told violations from compliant code under
|
|
82
|
+
every wording tried. A rule with no single correct form can be only a
|
|
83
|
+
`never` block.
|
|
84
|
+
- A convention that is a guideline rather than a requirement says "should" in
|
|
85
|
+
its description and tags its fences `should` and `should not`. A rule is one
|
|
86
|
+
or the other; it cannot mix the two.
|
|
87
|
+
- One pattern per file. Two patterns in one `must` block blur the probability.
|
|
81
88
|
- `threshold` is optional. Start without it; set it in step 5 if needed.
|
|
82
89
|
|
|
83
90
|
## 4. Configure
|
|
@@ -110,11 +117,11 @@ adhere lint --threshold 0.9 # re-reads the cache, sends nothing
|
|
|
110
117
|
|
|
111
118
|
Then, per rule, read the findings and decide:
|
|
112
119
|
|
|
113
|
-
- Many hits at 0.
|
|
120
|
+
- Many hits at 0.80 to 0.90, mostly not violations: the description is too
|
|
114
121
|
broad. Narrow it (say what is out of scope) or raise that rule's
|
|
115
122
|
`threshold`. Prefer narrowing; a threshold hides, a sentence explains.
|
|
116
|
-
- Hits on code that follows the pattern through a different API: the
|
|
117
|
-
|
|
123
|
+
- Hits on code that follows the pattern through a different API: the `must`
|
|
124
|
+
block is too specific. Use the repo's most general correct example.
|
|
118
125
|
- Zero hits: plant a deliberate violation in a scratch file, run, confirm the
|
|
119
126
|
rule fires, remove the file. A rule that cannot fire is not a rule.
|
|
120
127
|
- Hits at 0.9 and above: read them first. They are usually real.
|
package/src/cli.ts
CHANGED
|
@@ -68,6 +68,33 @@ const threshold = Flag.float("threshold").pipe(
|
|
|
68
68
|
),
|
|
69
69
|
);
|
|
70
70
|
|
|
71
|
+
const limit = Flag.integer("limit").pipe(
|
|
72
|
+
Flag.filter(
|
|
73
|
+
(checks) => checks >= 0,
|
|
74
|
+
(checks) => `--limit is a number of checks, 0 or more, not ${checks}.`,
|
|
75
|
+
),
|
|
76
|
+
Flag.optional,
|
|
77
|
+
Flag.withDescription(
|
|
78
|
+
"Judge at most this many checks. The rest wait for a later run, which picks up where this one stopped, since judgments are cached. --limit 0 shows the plan and judges nothing.",
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const rpm = Flag.integer("rpm").pipe(
|
|
83
|
+
Flag.filter(
|
|
84
|
+
(requests) => requests > 0,
|
|
85
|
+
(requests) => `--rpm is a number of requests a minute, more than 0, not ${requests}.`,
|
|
86
|
+
),
|
|
87
|
+
Flag.optional,
|
|
88
|
+
Flag.withDescription("Send at most this many requests to Jev a minute, evenly spaced."),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const filter = Flag.string("filter").pipe(
|
|
92
|
+
Flag.atLeast(0),
|
|
93
|
+
Flag.withDescription(
|
|
94
|
+
"Read only files whose path from the working directory matches this glob, such as 'src/**' or '**/*.service.ts'. Repeat it for more; a pattern starting with ! leaves out what it matches.",
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
|
|
71
98
|
const yes = Flag.boolean("yes").pipe(
|
|
72
99
|
Flag.withAlias("y"),
|
|
73
100
|
Flag.withDefault(false),
|
|
@@ -114,53 +141,61 @@ const force = Flag.boolean("force").pipe(
|
|
|
114
141
|
const chosenPresets = (flag: Option.Option<PresetName>): ReadonlyArray<PresetName> =>
|
|
115
142
|
Option.isSome(flag) ? [flag.value] : [];
|
|
116
143
|
|
|
117
|
-
export const auditLayer = (overrides: Overrides) =>
|
|
144
|
+
export const auditLayer = (overrides: Overrides, filter: ReadonlyArray<string> = []) =>
|
|
118
145
|
Layer.mergeAll(
|
|
119
|
-
SourceWalkerLive,
|
|
146
|
+
SourceWalkerLive(filter),
|
|
120
147
|
AuditCacheLive,
|
|
121
148
|
JevLive.pipe(Layer.provide([FetchHttpClient.layer, CredentialsLive])),
|
|
122
149
|
).pipe(Layer.provideMerge(AdhereConfigLive(overrides)));
|
|
123
150
|
|
|
124
151
|
/** `adhere lint`: the audit. */
|
|
125
|
-
export const lintCommand = Command.make(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
152
|
+
export const lintCommand = Command.make(
|
|
153
|
+
"lint",
|
|
154
|
+
{ preset, threshold, yes, limit, rpm, filter },
|
|
155
|
+
(input) =>
|
|
156
|
+
Effect.gen(function* () {
|
|
157
|
+
const stdio = yield* Stdio.Stdio;
|
|
158
|
+
const plan = yield* planAudit({ limit: Option.getOrUndefined(input.limit) });
|
|
159
|
+
const limits = { filter: input.filter, rpm: Option.getOrUndefined(input.rpm) };
|
|
160
|
+
yield* toStderr(`${describePlan(plan, limits).join("\n")}\n`);
|
|
161
|
+
// A prompt draws on stdout, so it needs a terminal at both ends: none when
|
|
162
|
+
// the report is redirected, or in CI.
|
|
163
|
+
const interactive = (yield* stdio.stdinIsTerminal) && (yield* stdio.stdoutIsTerminal);
|
|
164
|
+
if (plan.requests > 0 && interactive && !input.yes) {
|
|
165
|
+
const send = yield* Prompt.run(
|
|
166
|
+
Prompt.confirm({ message: sendQuestion(plan), initial: true }),
|
|
167
|
+
);
|
|
168
|
+
if (!send) {
|
|
169
|
+
yield* toStderr("Nothing sent.\n");
|
|
170
|
+
return yield* Cancelled.make({});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const counter = counterFor(plan);
|
|
174
|
+
yield* counter.start;
|
|
175
|
+
const result = yield* executeAudit(plan, counter.update).pipe(
|
|
176
|
+
Effect.onExit((exit) => counter.finish(Exit.isFailure(exit))),
|
|
136
177
|
);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
178
|
+
const color = yield* stdio.stdoutIsTerminal;
|
|
179
|
+
const path = yield* Path.Path;
|
|
180
|
+
// One write: separate Console.log calls have interleaved out of order here.
|
|
181
|
+
yield* Console.log(render(result, { color, root: path.resolve() }).join("\n"));
|
|
182
|
+
if (result.findings.length > 0) {
|
|
183
|
+
yield* FindingsReported.make({ count: result.findings.length });
|
|
140
184
|
}
|
|
141
|
-
}
|
|
142
|
-
const counter = counterFor(plan);
|
|
143
|
-
yield* counter.start;
|
|
144
|
-
const result = yield* executeAudit(plan, counter.update).pipe(
|
|
145
|
-
Effect.onExit((exit) => counter.finish(Exit.isFailure(exit))),
|
|
146
|
-
);
|
|
147
|
-
const color = yield* stdio.stdoutIsTerminal;
|
|
148
|
-
const path = yield* Path.Path;
|
|
149
|
-
// One write: separate Console.log calls have interleaved out of order here.
|
|
150
|
-
yield* Console.log(render(result, { color, root: path.resolve() }).join("\n"));
|
|
151
|
-
if (result.findings.length > 0) {
|
|
152
|
-
yield* FindingsReported.make({ count: result.findings.length });
|
|
153
|
-
}
|
|
154
|
-
}),
|
|
185
|
+
}),
|
|
155
186
|
).pipe(
|
|
156
187
|
Command.withDescription(
|
|
157
|
-
"Audit the working directory against
|
|
188
|
+
"Audit the working directory against the rules in .adhere/ or a preset, judged by Jev.",
|
|
158
189
|
),
|
|
159
190
|
Command.provide((input) =>
|
|
160
|
-
auditLayer(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
191
|
+
auditLayer(
|
|
192
|
+
{
|
|
193
|
+
presets: chosenPresets(input.preset),
|
|
194
|
+
threshold: Option.getOrUndefined(input.threshold),
|
|
195
|
+
rpm: Option.getOrUndefined(input.rpm),
|
|
196
|
+
},
|
|
197
|
+
input.filter,
|
|
198
|
+
),
|
|
164
199
|
),
|
|
165
200
|
);
|
|
166
201
|
|
|
@@ -274,7 +309,7 @@ export const skillCommand = Command.make("skill", {}, () => Console.log(skill.tr
|
|
|
274
309
|
|
|
275
310
|
/** The whole CLI. Bare `adhere` prints its help. */
|
|
276
311
|
export const cli = Command.make("adhere").pipe(
|
|
277
|
-
Command.withDescription("Lint a repository against
|
|
312
|
+
Command.withDescription("Lint a repository against the rules in .adhere/, judged by Jev."),
|
|
278
313
|
Command.withExamples([
|
|
279
314
|
{ command: "adhere init", description: "Scaffold .adhere/config.ts and two example rules" },
|
|
280
315
|
{ command: "adhere validate", description: "Check the config and rules without calling Jev" },
|
package/src/config.ts
CHANGED
|
@@ -1,25 +1,79 @@
|
|
|
1
1
|
import { type PresetName, presetNames } from "#presets.ts";
|
|
2
2
|
import type { RuleSet } from "#rules.ts";
|
|
3
|
-
import { Effect, Schema } from "effect";
|
|
3
|
+
import { Effect, Schema, SchemaTransformation } from "effect";
|
|
4
4
|
|
|
5
5
|
export type RuleId = string;
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* A rule's examples, named in RFC 2119's words, which Jev reads as written. A
|
|
9
|
+
* requirement shows code that `must` be written and code that must `never`
|
|
10
|
+
* be. A guideline, which asks for less, shows code that `should` be written
|
|
11
|
+
* and code that `shouldNot` be.
|
|
12
|
+
*/
|
|
13
|
+
const RuleFields = Schema.Struct({
|
|
8
14
|
description: Schema.String,
|
|
9
|
-
|
|
15
|
+
must: Schema.optionalKey(Schema.String),
|
|
16
|
+
never: Schema.optionalKey(Schema.String),
|
|
17
|
+
should: Schema.optionalKey(Schema.String),
|
|
18
|
+
shouldNot: Schema.optionalKey(Schema.String),
|
|
19
|
+
threshold: Schema.optionalKey(Schema.Finite),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/** Before 0.7, a rule's examples were `reference` and `avoid`: read as `must` and `never`. */
|
|
23
|
+
export const Rule = Schema.Struct({
|
|
24
|
+
...RuleFields.fields,
|
|
10
25
|
reference: Schema.optionalKey(Schema.String),
|
|
11
|
-
/** Incorrect code: what a violation looks like. */
|
|
12
26
|
avoid: Schema.optionalKey(Schema.String),
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
)
|
|
27
|
+
})
|
|
28
|
+
.pipe(
|
|
29
|
+
Schema.decodeTo(
|
|
30
|
+
RuleFields,
|
|
31
|
+
SchemaTransformation.transform({
|
|
32
|
+
decode: ({ reference, avoid, ...rule }) => ({
|
|
33
|
+
...rule,
|
|
34
|
+
...(rule.must === undefined && reference !== undefined ? { must: reference } : {}),
|
|
35
|
+
...(rule.never === undefined && avoid !== undefined ? { never: avoid } : {}),
|
|
36
|
+
}),
|
|
37
|
+
encode: (rule) => rule,
|
|
38
|
+
}),
|
|
39
|
+
),
|
|
40
|
+
)
|
|
41
|
+
.check(
|
|
42
|
+
Schema.makeFilter((rule) => {
|
|
43
|
+
const requirement = rule.must !== undefined || rule.never !== undefined;
|
|
44
|
+
const guideline = rule.should !== undefined || rule.shouldNot !== undefined;
|
|
45
|
+
return requirement && guideline
|
|
46
|
+
? "a rule is a requirement (must, never) or a guideline (should, shouldNot), not both"
|
|
47
|
+
: requirement || guideline
|
|
48
|
+
? undefined
|
|
49
|
+
: "a rule needs an example: must, never, should, or shouldNot";
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
21
52
|
export interface Rule extends Schema.Schema.Type<typeof Rule> {}
|
|
22
53
|
|
|
54
|
+
/** One of a rule's examples, under the word it is written with. */
|
|
55
|
+
export interface Example {
|
|
56
|
+
readonly word: "must" | "never" | "should" | "should not";
|
|
57
|
+
readonly code: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A rule's code to write and code not to write, each when the rule has it. */
|
|
61
|
+
export interface Examples {
|
|
62
|
+
readonly good?: Example;
|
|
63
|
+
readonly bad?: Example;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A guideline's examples are `should` and `should not`; any other rule's, `must` and `never`. */
|
|
67
|
+
export const examplesOf = (rule: Rule): Examples => {
|
|
68
|
+
const guideline = rule.should !== undefined || rule.shouldNot !== undefined;
|
|
69
|
+
const good = guideline ? rule.should : rule.must;
|
|
70
|
+
const bad = guideline ? rule.shouldNot : rule.never;
|
|
71
|
+
return {
|
|
72
|
+
...(good === undefined ? {} : { good: { word: guideline ? "should" : "must", code: good } }),
|
|
73
|
+
...(bad === undefined ? {} : { bad: { word: guideline ? "should not" : "never", code: bad } }),
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
23
77
|
export type Rules = Readonly<Record<RuleId, Rule>>;
|
|
24
78
|
|
|
25
79
|
/** Rules inline, or a directory of Markdown rule files (see `markdown.ts`). */
|
|
@@ -86,7 +140,7 @@ export type Loaded<T extends { readonly rules?: unknown }> = Omit<T, "rules"> &
|
|
|
86
140
|
};
|
|
87
141
|
|
|
88
142
|
export const DEFAULT_MODEL = "jev-latest";
|
|
89
|
-
export const DEFAULT_THRESHOLD = 0.
|
|
143
|
+
export const DEFAULT_THRESHOLD = 0.8;
|
|
90
144
|
|
|
91
145
|
/** A config with its presets folded in and every default applied. */
|
|
92
146
|
export interface ResolvedConfig {
|
|
@@ -94,6 +148,8 @@ export interface ResolvedConfig {
|
|
|
94
148
|
readonly threshold: number;
|
|
95
149
|
readonly rules: Rules;
|
|
96
150
|
readonly scopedRules?: RuleSet;
|
|
151
|
+
/** Requests to Jev a minute, at most. Unset, requests go out as fast as they are made. */
|
|
152
|
+
readonly rpm?: number;
|
|
97
153
|
}
|
|
98
154
|
|
|
99
155
|
/** Command-line values that apply on top of the config file. */
|
|
@@ -101,6 +157,8 @@ export interface Overrides {
|
|
|
101
157
|
readonly presets?: ReadonlyArray<PresetName>;
|
|
102
158
|
/** Replaces the config's global threshold. Per-rule thresholds still win. */
|
|
103
159
|
readonly threshold?: number;
|
|
160
|
+
/** Throttles requests to Jev to at most this many a minute. */
|
|
161
|
+
readonly rpm?: number;
|
|
104
162
|
}
|
|
105
163
|
|
|
106
164
|
/** The presets a run applies, command-line ones first. */
|
|
@@ -129,6 +187,7 @@ export const resolveConfig = (
|
|
|
129
187
|
model: config.model ?? last("model") ?? DEFAULT_MODEL,
|
|
130
188
|
threshold: overrides.threshold ?? config.threshold ?? last("threshold") ?? DEFAULT_THRESHOLD,
|
|
131
189
|
rules: Object.assign({}, ...applied.map((preset) => preset.rules), config.rules),
|
|
190
|
+
...(overrides.rpm === undefined ? {} : { rpm: overrides.rpm }),
|
|
132
191
|
};
|
|
133
192
|
};
|
|
134
193
|
|
package/src/init.ts
CHANGED
|
@@ -15,39 +15,41 @@ export interface InitOptions {
|
|
|
15
15
|
const CONFIG = `import type { Config } from "@drkmttr/adhere";
|
|
16
16
|
|
|
17
17
|
export default {
|
|
18
|
-
threshold: 0.
|
|
18
|
+
threshold: 0.8,
|
|
19
19
|
} satisfies Config;
|
|
20
20
|
`;
|
|
21
21
|
|
|
22
22
|
const SMALL_FILES = `---
|
|
23
|
-
description:
|
|
23
|
+
description: A file should be small and focused, with one primary responsibility.
|
|
24
24
|
threshold: 0.8
|
|
25
25
|
---
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
A guideline, so its description and its code say should. A rule that must
|
|
28
|
+
hold says must and never instead, as name-domain-actions does. Use this rule
|
|
29
|
+
as a template for repository-specific conventions.
|
|
28
30
|
|
|
29
|
-
\`\`\`ts
|
|
31
|
+
\`\`\`ts should
|
|
30
32
|
export const parsePort = (value: string) =>
|
|
31
33
|
Port.make(Number.parseInt(value, 10));
|
|
32
34
|
\`\`\`
|
|
33
35
|
`;
|
|
34
36
|
|
|
35
37
|
const NAME_EFFECTS = `---
|
|
36
|
-
description:
|
|
38
|
+
description: A function must be named after the domain action it performs, never after a generic verb like handle.
|
|
37
39
|
threshold: 0.8
|
|
38
40
|
---
|
|
39
41
|
|
|
40
|
-
\`\`\`ts
|
|
42
|
+
\`\`\`ts must
|
|
41
43
|
export const loadCustomerProfile = (customerId: CustomerId) =>
|
|
42
44
|
Effect.gen(function* () {
|
|
43
45
|
return yield* CustomerStore.find(customerId);
|
|
44
46
|
});
|
|
45
47
|
\`\`\`
|
|
46
48
|
|
|
47
|
-
A fence tagged \`
|
|
49
|
+
A fence tagged \`never\` shows what a violation looks like. It is optional, and
|
|
48
50
|
a rule can have one without the block above.
|
|
49
51
|
|
|
50
|
-
\`\`\`ts
|
|
52
|
+
\`\`\`ts never
|
|
51
53
|
export const handle = (id: string) =>
|
|
52
54
|
Effect.gen(function* () {
|
|
53
55
|
return yield* CustomerStore.find(id);
|