judgelint 0.1.2 → 0.1.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +88 -19
- data/lib/judgelint/cli.rb +26 -15
- data/lib/judgelint/client.rb +5 -1
- data/lib/judgelint/config.rb +3 -3
- data/lib/judgelint/glob.rb +24 -1
- data/lib/judgelint/paint.rb +46 -0
- data/lib/judgelint/question.rb +27 -7
- data/lib/judgelint/reporter.rb +40 -13
- data/lib/judgelint/rule.rb +22 -2
- data/lib/judgelint/runner.rb +54 -13
- data/lib/judgelint/scaffold.rb +44 -45
- data/lib/judgelint/source.rb +3 -2
- data/lib/judgelint/spec.rb +78 -27
- data/lib/judgelint/version.rb +1 -1
- data/lib/judgelint.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '0901cbb02b6850e8d11760748b1ae734629b06d12ad019af3eab4d255f689761'
|
|
4
|
+
data.tar.gz: 468268468e35d1e9f9274281a3184de02ccfff98080c99c47b0ec6349f9a5762
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85868cd486ebdaf2663607f2539c58541b59150da14eab77ee23fc950878aa9c466eef90cab29ea3241d520d74bebb88803c99b4b31dd0ad979be65187b9da9d
|
|
7
|
+
data.tar.gz: d44aaaf54c4be5f05388d635b9472fee970527dd09aa47ef3ffb429dadd32cdbaa820e1dee9198e53a33df353c151dfcd690d6da91ec79cb6e0a6852e0365611
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.6 (2026-10-26)
|
|
4
|
+
|
|
5
|
+
- `--base` and `--pr` ask about the changed lines, not the 40-line windows
|
|
6
|
+
around them. A finding the model places on a line the change didn't touch
|
|
7
|
+
doesn't count, so editing an old file can't fail it for code the edit
|
|
8
|
+
left alone.
|
|
9
|
+
|
|
10
|
+
## 0.1.5 (2026-10-26)
|
|
11
|
+
|
|
12
|
+
- The text report is in color in a terminal: errors in red, warnings and
|
|
13
|
+
uncertain findings in yellow, and file names in bold. `judgelint test`
|
|
14
|
+
marks hits green and misses red. `NO_COLOR` turns color off and
|
|
15
|
+
`FORCE_COLOR` turns it on. Piped output is unchanged.
|
|
16
|
+
|
|
17
|
+
## 0.1.4 (2026-10-24)
|
|
18
|
+
|
|
19
|
+
- `Bad:` and `Good:` code blocks under a bullet are examples. The model reads
|
|
20
|
+
them with the spec, and `judgelint test` judges each one, along with the
|
|
21
|
+
files in `.judgelint/examples/`.
|
|
22
|
+
- `judgelint init` writes a `CONVENTIONS.md` with examples in it.
|
|
23
|
+
- `examples/conventions` has complete specs for Ruby, TypeScript, Python, and
|
|
24
|
+
Postgres migrations.
|
|
25
|
+
|
|
26
|
+
## 0.1.3 (2026-10-24)
|
|
27
|
+
|
|
28
|
+
- No setup beyond `TYPESAFE_API_KEY`. A `CONVENTIONS.md` of bullets is a
|
|
29
|
+
spec with no frontmatter, and it covers the files in its directory and
|
|
30
|
+
below, except Markdown and lockfiles. A README still needs `paths:`.
|
|
31
|
+
- The cost line names the model.
|
|
32
|
+
- Full runs skip files git ignores, and binary files.
|
|
33
|
+
- `judgelint init` no longer writes `.judgelint.yml`.
|
|
34
|
+
- A run the cache answers in full needs no API key.
|
|
35
|
+
|
|
3
36
|
## 0.1.2 (2026-10-24)
|
|
4
37
|
|
|
5
38
|
- Each bullet is one choice between breaking the rule, following it, and the
|
data/README.md
CHANGED
|
@@ -22,14 +22,39 @@ plain Ruby on Windows.
|
|
|
22
22
|
|
|
23
23
|
## Getting started
|
|
24
24
|
|
|
25
|
+
Write your conventions as bullets in a `CONVENTIONS.md`:
|
|
26
|
+
|
|
27
|
+
```markdown
|
|
28
|
+
- **No silent rescues.** A rescue logs the error, re-raises it, or returns a
|
|
29
|
+
failure the caller sees.
|
|
30
|
+
- **Error messages say what to do next.** A raised error names what was wrong
|
|
31
|
+
and what would work instead.
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then set your TypeSafe API key and run it:
|
|
35
|
+
|
|
25
36
|
```sh
|
|
26
|
-
judgelint init # writes .judgelint.yml and an example spec
|
|
27
37
|
export TYPESAFE_API_KEY=...
|
|
28
|
-
judgelint
|
|
29
|
-
judgelint # checks every file a spec covers
|
|
38
|
+
judgelint # checks every file
|
|
30
39
|
judgelint --base main # checks only what changed since main
|
|
31
40
|
```
|
|
32
41
|
|
|
42
|
+
```text
|
|
43
|
+
lib/profile.rb
|
|
44
|
+
x error root/no-silent-rescues 5 0.99 No silent rescues.
|
|
45
|
+
x error root/error-messages-say-what-to-do 9 0.97 Error messages say what to do next.
|
|
46
|
+
|
|
47
|
+
1 file, 2 violations
|
|
48
|
+
judgelint: 1 request, 940 input tokens, $0.0000, typesafe:jev-latest
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
That's all the setup there is. A `CONVENTIONS.md` covers the files in its
|
|
52
|
+
directory and below, except Markdown and lockfiles. In a git repository,
|
|
53
|
+
judgelint skips what git ignores. It also skips binary files.
|
|
54
|
+
|
|
55
|
+
judgelint asks Jev on TypeSafe. To use another provider, set `models` in
|
|
56
|
+
`.judgelint.yml` (see [Configuration](#configuration)).
|
|
57
|
+
|
|
33
58
|
Here's a run on the [demo app](examples/scoop-society), with the messages shortened:
|
|
34
59
|
|
|
35
60
|
```text
|
|
@@ -47,7 +72,7 @@ src/features/flavor-of-day
|
|
|
47
72
|
x error src/features/no-orphaned-files old-rotation.ts 0.79 No orphaned files.
|
|
48
73
|
|
|
49
74
|
2 files, 6 violations, 3 uncertain
|
|
50
|
-
judgelint: 2 requests, 6,437 input tokens, $0.0003
|
|
75
|
+
judgelint: 2 requests, 6,437 input tokens, $0.0003, typesafe:jev-latest
|
|
51
76
|
```
|
|
52
77
|
|
|
53
78
|
A finding names the line that breaks the rule, or the file in a directory,
|
|
@@ -57,10 +82,15 @@ about.
|
|
|
57
82
|
It exits 1 when there are violations, and 2 on a usage, config, or model
|
|
58
83
|
error. Config problems are caught before any request is sent.
|
|
59
84
|
|
|
85
|
+
In a terminal, the report is in color. Set `NO_COLOR` to turn color off, or
|
|
86
|
+
`FORCE_COLOR` to keep it when the output is piped.
|
|
87
|
+
|
|
60
88
|
## Writing specs
|
|
61
89
|
|
|
62
|
-
A spec is a
|
|
63
|
-
its frontmatter. Each top-level bullet is a rule.
|
|
90
|
+
A spec is a `CONVENTIONS.md`, a file ending in `.judgelint.md`, or a README
|
|
91
|
+
with `paths:` in its frontmatter. Each top-level bullet is a rule. Put a spec
|
|
92
|
+
in a subdirectory for conventions that apply only there, or use `paths:` to
|
|
93
|
+
choose the files:
|
|
64
94
|
|
|
65
95
|
```markdown
|
|
66
96
|
---
|
|
@@ -76,7 +106,7 @@ excludes: ["src/services/legacy-import.ts"]
|
|
|
76
106
|
|
|
77
107
|
The model gets the spec and the numbered file. For each bullet, it chooses
|
|
78
108
|
whether the code breaks it, follows it, or isn't covered by it, and which line
|
|
79
|
-
breaks it most clearly. A rule's id comes from the start of its bullet, like
|
|
109
|
+
breaks it most clearly. A rule's id comes from the spec's directory and the start of its bullet, like
|
|
80
110
|
`src/services/no-i-o-except-through-the`. Bullets that say "should" or
|
|
81
111
|
"prefer" are warnings, and the rest are errors.
|
|
82
112
|
|
|
@@ -91,9 +121,36 @@ If the model misreads a bullet, describe breaking and following it:
|
|
|
91
121
|
They make the model more decisive. A loose description can also flag code
|
|
92
122
|
you meant to allow, so check them against examples.
|
|
93
123
|
|
|
124
|
+
### Examples
|
|
125
|
+
|
|
126
|
+
Show the model what breaking and following a rule looks like. Under a bullet,
|
|
127
|
+
put code blocks after a `Bad:` or `Good:` line:
|
|
128
|
+
|
|
129
|
+
````markdown
|
|
130
|
+
- **Jobs take ids, not records.** Arguments to `perform_later` are ids and
|
|
131
|
+
plain values. A record can change or disappear before the job runs.
|
|
132
|
+
|
|
133
|
+
Bad:
|
|
134
|
+
|
|
135
|
+
```ruby
|
|
136
|
+
WelcomeEmailJob.perform_later(user)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Good:
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
WelcomeEmailJob.perform_later(user.id, locale: user.locale)
|
|
143
|
+
```
|
|
144
|
+
````
|
|
145
|
+
|
|
146
|
+
The model reads them with the rest of the spec, and `judgelint test` checks
|
|
147
|
+
that it gets each one right. [examples/conventions](examples/conventions) has
|
|
148
|
+
complete specs for Ruby, TypeScript, Python, and Postgres migrations, with
|
|
149
|
+
code to run them on.
|
|
150
|
+
|
|
94
151
|
| Frontmatter | What it does |
|
|
95
152
|
|---|---|
|
|
96
|
-
| `paths`, `excludes` | Globs for the files the spec covers, relative to the project root. |
|
|
153
|
+
| `paths`, `excludes` | Globs for the files the spec covers, relative to the project root. A README needs `paths`. |
|
|
97
154
|
| `context` | Files the model reads along with each file, like shared types. They're never judged. |
|
|
98
155
|
| `scope` | `window`, the default, asks about every 40 lines so findings have line numbers. `file` asks once per file. `directory` asks once per directory, shows the model every file in it, and names the file that breaks the rule. |
|
|
99
156
|
| `severity`, `threshold` | Set the severity or threshold for every rule in the spec. |
|
|
@@ -138,21 +195,30 @@ with no reason, like "this is intentional", doesn't excuse anything.
|
|
|
138
195
|
|
|
139
196
|
## Testing rules
|
|
140
197
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
`judgelint test` shows which ones the model gets wrong:
|
|
198
|
+
`judgelint test` judges each rule's examples and shows which ones the model
|
|
199
|
+
gets wrong:
|
|
144
200
|
|
|
145
201
|
```text
|
|
146
|
-
|
|
147
|
-
ok
|
|
148
|
-
|
|
149
|
-
|
|
202
|
+
root/jobs-take-ids-not-records 2/2
|
|
203
|
+
ok fail 1.00 CONVENTIONS.md:67
|
|
204
|
+
ok pass 0.00 CONVENTIONS.md:73
|
|
205
|
+
root/no-silent-rescues 2/3
|
|
206
|
+
ok pass 0.03 .judgelint/examples/root/no-silent-rescues/pass/logs_and_reraises.rb
|
|
207
|
+
MISS pass 0.79 .judgelint/examples/root/no-silent-rescues/pass/retries_then_raises.rb
|
|
208
|
+
ok fail 0.94 .judgelint/examples/root/no-silent-rescues/fail/assigns_default.rb
|
|
150
209
|
```
|
|
151
210
|
|
|
152
|
-
|
|
211
|
+
`Bad:` and `Good:` blocks in the spec are the quickest examples to write. The
|
|
212
|
+
model also reads them during every check, so passing them shows it
|
|
213
|
+
understands the rule, not that it gets the rule right on code it hasn't seen.
|
|
214
|
+
For that, and for longer examples, put whole files that follow the rule in
|
|
215
|
+
`.judgelint/examples/<rule id>/pass/` and files that break it in `fail/`. The
|
|
153
216
|
model doesn't see the `pass/` or `fail/` part of the path. For a directory
|
|
154
217
|
rule, each example is a directory.
|
|
155
218
|
|
|
219
|
+
When an example fails, reword the bullet or split it into narrower rules.
|
|
220
|
+
`judgelint init` writes a spec with examples to start from.
|
|
221
|
+
|
|
156
222
|
## CI
|
|
157
223
|
|
|
158
224
|
```sh
|
|
@@ -161,15 +227,18 @@ judgelint --pr # changes in the current pull request
|
|
|
161
227
|
judgelint --pr --format github # GitHub annotations on the changed lines (json and sarif work too)
|
|
162
228
|
```
|
|
163
229
|
|
|
164
|
-
These only ask about the
|
|
165
|
-
doesn't block a new pull request
|
|
230
|
+
These only ask about the lines that changed, with the rest of the file as
|
|
231
|
+
context, so existing code doesn't block a new pull request: a finding the
|
|
232
|
+
model places on a line the change didn't touch doesn't count. A new file is
|
|
233
|
+
all changed lines. Rules about a whole file (`scope: file`) still read the
|
|
234
|
+
whole file when any of it changed. `--pr` gets the base branch from GitHub
|
|
166
235
|
Actions, GitLab, Buildkite, Bitbucket, Azure Pipelines,
|
|
167
236
|
`JUDGELINT_BASE_BRANCH`, or `gh`. Fetch enough history for git to find the
|
|
168
237
|
merge base.
|
|
169
238
|
|
|
170
239
|
## Configuration
|
|
171
240
|
|
|
172
|
-
`.judgelint.yml` has four settings:
|
|
241
|
+
judgelint runs without a config file. `.judgelint.yml` has four settings:
|
|
173
242
|
|
|
174
243
|
```yaml
|
|
175
244
|
models: [typesafe] # provider or provider:model; list two to average them
|
data/lib/judgelint/cli.rb
CHANGED
|
@@ -10,8 +10,9 @@ module Judgelint
|
|
|
10
10
|
HELP = <<~TEXT
|
|
11
11
|
Usage: judgelint [command] [options] [paths...]
|
|
12
12
|
|
|
13
|
-
Checks code against the conventions in your specs
|
|
14
|
-
rule, it asks a decision model (Jev
|
|
13
|
+
Checks code against the conventions in your specs, like a CONVENTIONS.md
|
|
14
|
+
of bullets. For each file and each rule, it asks a decision model (Jev on
|
|
15
|
+
TypeSafe by default) whether the code breaks it.
|
|
15
16
|
|
|
16
17
|
Commands:
|
|
17
18
|
check [paths...] Check the files the specs govern, or these (default).
|
|
@@ -19,7 +20,7 @@ module Judgelint
|
|
|
19
20
|
and fail) and report what the model gets wrong.
|
|
20
21
|
rules List the rules and how many files each governs.
|
|
21
22
|
providers List the decision-model providers judgelint knows.
|
|
22
|
-
init Write .
|
|
23
|
+
init Write a CONVENTIONS.md to start from, with examples.
|
|
23
24
|
|
|
24
25
|
Options:
|
|
25
26
|
--base REF Check only what changed since the merge base with REF,
|
|
@@ -50,6 +51,8 @@ module Judgelint
|
|
|
50
51
|
@overrides = {}
|
|
51
52
|
@usage = nil
|
|
52
53
|
@no_cache = false
|
|
54
|
+
@paint = Paint.for(out)
|
|
55
|
+
@err_paint = Paint.for(err)
|
|
53
56
|
end
|
|
54
57
|
|
|
55
58
|
def run
|
|
@@ -65,13 +68,13 @@ module Judgelint
|
|
|
65
68
|
when "init" then init
|
|
66
69
|
end
|
|
67
70
|
rescue UsageError => e
|
|
68
|
-
@err.puts "judgelint: #{e.message}"
|
|
69
|
-
@err.puts "Run `judgelint --help` for usage."
|
|
71
|
+
@err.puts "#{@err_paint.call('judgelint:', Paint::BOLD_RED)} #{e.message}"
|
|
72
|
+
@err.puts @err_paint.call("Run `judgelint --help` for usage.", Paint::DIM)
|
|
70
73
|
2
|
|
71
74
|
rescue Error => e
|
|
72
|
-
@err.puts "judgelint: #{e.message}"
|
|
75
|
+
@err.puts "#{@err_paint.call('judgelint:', Paint::BOLD_RED)} #{e.message}"
|
|
73
76
|
# Requests that succeeded before the failure are still billed.
|
|
74
|
-
|
|
77
|
+
cost_line if @usage && @usage.requests.positive?
|
|
75
78
|
2
|
|
76
79
|
end
|
|
77
80
|
|
|
@@ -137,10 +140,11 @@ module Judgelint
|
|
|
137
140
|
units = paths.size + directories.size
|
|
138
141
|
runner = runner(config, rules)
|
|
139
142
|
jobs = runner.plan(paths, directories: directories, changes: changes)
|
|
143
|
+
runner.check_keys(jobs)
|
|
140
144
|
guard_cost(config, runner, jobs)
|
|
141
145
|
verdicts = runner.execute(jobs).flatten
|
|
142
|
-
Reporter.new(@out, format: @format).check(verdicts, config: config, file_count: units, usage: @usage)
|
|
143
|
-
|
|
146
|
+
Reporter.new(@out, format: @format, paint: @paint).check(verdicts, config: config, file_count: units, usage: @usage)
|
|
147
|
+
cost_line
|
|
144
148
|
verdicts.any?(&:failing?) ? 1 : 0
|
|
145
149
|
end
|
|
146
150
|
|
|
@@ -154,9 +158,9 @@ module Judgelint
|
|
|
154
158
|
elsif changes
|
|
155
159
|
paths = changes.keys.sort.select { |path| File.file?(path) }
|
|
156
160
|
else
|
|
157
|
-
paths = Glob.
|
|
161
|
+
paths = Glob.files(rules.flat_map(&:paths))
|
|
158
162
|
end
|
|
159
|
-
paths.select { |path| rules.any? { |rule| !rule.directory? && rule.applies_to?(path) } }
|
|
163
|
+
paths.select { |path| rules.any? { |rule| !rule.directory? && rule.applies_to?(path) } }.reject { |path| Glob.binary?(path) }
|
|
160
164
|
end
|
|
161
165
|
|
|
162
166
|
# The directories directory rules govern; with paths named, those the
|
|
@@ -184,10 +188,11 @@ module Judgelint
|
|
|
184
188
|
raise ConfigError, "No rule has examples yet. Add some under #{rules.first.examples_dir}/{pass,fail}/" if jobs.empty?
|
|
185
189
|
|
|
186
190
|
@err.puts "#{untested.size} #{untested.size == 1 ? 'rule has' : 'rules have'} no examples: #{untested.map(&:id).join(', ')}" unless untested.empty?
|
|
191
|
+
runner.check_keys(jobs)
|
|
187
192
|
guard_cost(config, runner, jobs)
|
|
188
193
|
examples = runner.execute(jobs).flatten
|
|
189
|
-
Reporter.new(@out, format: @format).fixtures(examples, usage: @usage)
|
|
190
|
-
|
|
194
|
+
Reporter.new(@out, format: @format, paint: @paint).fixtures(examples, usage: @usage)
|
|
195
|
+
cost_line
|
|
191
196
|
examples.all?(&:correct?) ? 0 : 1
|
|
192
197
|
end
|
|
193
198
|
|
|
@@ -201,10 +206,15 @@ module Judgelint
|
|
|
201
206
|
Usage.delimit(runner.estimated_tokens(jobs)), estimate, config.max_cost_usd)
|
|
202
207
|
end
|
|
203
208
|
|
|
209
|
+
# What the run cost, and which models answered.
|
|
210
|
+
def cost_line
|
|
211
|
+
@err.puts @err_paint.call("#{@usage.line}, #{@models}", Paint::DIM)
|
|
212
|
+
end
|
|
213
|
+
|
|
204
214
|
def list_rules
|
|
205
215
|
_config, rules = load_rules
|
|
206
216
|
rules.each do |rule|
|
|
207
|
-
count = rule.directory? ? "#{rule.directories.size} dirs" : "#{Glob.
|
|
217
|
+
count = rule.directory? ? "#{rule.directories.size} dirs" : "#{Glob.files(rule.paths).count { |path| rule.applies_to?(path) }} files"
|
|
208
218
|
@out.puts format("%-24s %-8s %10s %s", rule.id, rule.severity, count, rule.question || rule.message)
|
|
209
219
|
end
|
|
210
220
|
0
|
|
@@ -222,7 +232,7 @@ module Judgelint
|
|
|
222
232
|
|
|
223
233
|
def init
|
|
224
234
|
Scaffold.write(".", @out)
|
|
225
|
-
@err.puts "Next: set TYPESAFE_API_KEY, then run `judgelint test` to see the
|
|
235
|
+
@err.puts "Next: set TYPESAFE_API_KEY, then run `judgelint test` to see the examples judged."
|
|
226
236
|
0
|
|
227
237
|
end
|
|
228
238
|
|
|
@@ -233,6 +243,7 @@ module Judgelint
|
|
|
233
243
|
|
|
234
244
|
def runner(config, rules)
|
|
235
245
|
@usage = Usage.new
|
|
246
|
+
@models = config.label
|
|
236
247
|
clients = config.models.map { |endpoint| Client.new(endpoint, usage: @usage) }
|
|
237
248
|
Runner.new(config: config, rules: rules, clients: clients, cache: Cache.new(@no_cache ? nil : Config::CACHE))
|
|
238
249
|
end
|
data/lib/judgelint/client.rb
CHANGED
|
@@ -86,6 +86,11 @@ module Judgelint
|
|
|
86
86
|
end
|
|
87
87
|
end
|
|
88
88
|
|
|
89
|
+
# Called before the first request, so a run the cache answers needs no key.
|
|
90
|
+
def check_key
|
|
91
|
+
raise ConfigError, "Set #{@endpoint.api_key_env} to your #{@endpoint.provider} API key" if key_missing?
|
|
92
|
+
end
|
|
93
|
+
|
|
89
94
|
private
|
|
90
95
|
|
|
91
96
|
def check_settings
|
|
@@ -94,7 +99,6 @@ module Judgelint
|
|
|
94
99
|
raise ConfigError, "Provider #{provider} needs #{@endpoint.needs || 'api_base'} in .judgelint.yml"
|
|
95
100
|
end
|
|
96
101
|
raise ConfigError, "Provider #{provider} needs a model in .judgelint.yml" if model_needed?
|
|
97
|
-
raise ConfigError, "Set #{@endpoint.api_key_env} to your #{provider} API key" if key_missing?
|
|
98
102
|
|
|
99
103
|
unless %w[bearer basic none].include?(@endpoint.auth) || @endpoint.auth.start_with?("header:")
|
|
100
104
|
raise ConfigError, "auth is bearer, basic, none, or header:<name>, not #{@endpoint.auth}"
|
data/lib/judgelint/config.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Judgelint
|
|
4
|
-
# .judgelint.yml: which models to ask, the threshold,
|
|
5
|
-
# spending limit. Everything else has a fixed value.
|
|
4
|
+
# .judgelint.yml, which is optional: which models to ask, the threshold,
|
|
5
|
+
# files to ignore, and a spending limit. Everything else has a fixed value.
|
|
6
6
|
#
|
|
7
7
|
# models: [typesafe, cloudflare:clef] # ask both and average
|
|
8
8
|
# threshold: 0.7
|
|
@@ -73,7 +73,7 @@ module Judgelint
|
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
# One model to ask, and how to reach it: a provider preset and model id
|
|
76
|
-
# ("typesafe", "cloudflare:clef", "openrouter:
|
|
76
|
+
# ("typesafe", "cloudflare:clef", "openrouter:typesafe/jev-1.13"), or a
|
|
77
77
|
# mapping for an endpoint judgelint has no preset for.
|
|
78
78
|
class Endpoint
|
|
79
79
|
KEYS = %w[
|
data/lib/judgelint/glob.rb
CHANGED
|
@@ -17,11 +17,34 @@ module Judgelint
|
|
|
17
17
|
patterns.flat_map { |pattern| Dir.glob(pattern) }.select { |path| File.file?(path) }.uniq.sort
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
# The files to check: in a git repository, those git tracks or would add,
|
|
21
|
+
# so nothing it ignores (node_modules, build output) is ever read.
|
|
22
|
+
# Elsewhere, every file the patterns match.
|
|
23
|
+
def self.files(patterns)
|
|
24
|
+
listed = project_files
|
|
25
|
+
return expand(patterns) if listed.nil?
|
|
26
|
+
|
|
27
|
+
listed.select { |path| match_any?(patterns, path) && File.file?(path) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# nil outside a git repository, or without git.
|
|
31
|
+
def self.project_files
|
|
32
|
+
out, _err, status = Open3.capture3("git", "ls-files", "-z", "--cached", "--others", "--exclude-standard")
|
|
33
|
+
status.success? ? out.split("\0").uniq.sort : nil
|
|
34
|
+
rescue SystemCallError
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# A NUL byte in the first 8 KB, as git decides.
|
|
39
|
+
def self.binary?(path)
|
|
40
|
+
File.open(path, "rb") { |file| file.read(8000).to_s.include?("\0") }
|
|
41
|
+
end
|
|
42
|
+
|
|
20
43
|
# Files named on the command line, with directories expanded.
|
|
21
44
|
def self.expand_arguments(arguments)
|
|
22
45
|
arguments.flat_map do |argument|
|
|
23
46
|
if File.directory?(argument)
|
|
24
|
-
|
|
47
|
+
files([File.join(argument, "**", "*").delete_prefix("./")])
|
|
25
48
|
else
|
|
26
49
|
[argument]
|
|
27
50
|
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Judgelint
|
|
4
|
+
# Colors for a terminal. They're on when the stream is a terminal, unless
|
|
5
|
+
# NO_COLOR is set or TERM is dumb. FORCE_COLOR turns them on anywhere, so
|
|
6
|
+
# piped output can be colored too. With colors off, text passes through
|
|
7
|
+
# unchanged.
|
|
8
|
+
class Paint
|
|
9
|
+
BOLD = "1"
|
|
10
|
+
DIM = "2"
|
|
11
|
+
RED = "31"
|
|
12
|
+
GREEN = "32"
|
|
13
|
+
YELLOW = "33"
|
|
14
|
+
CYAN = "36"
|
|
15
|
+
BOLD_RED = "1;31"
|
|
16
|
+
BOLD_GREEN = "1;32"
|
|
17
|
+
|
|
18
|
+
def self.for(io)
|
|
19
|
+
new(color?(io))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.color?(io)
|
|
23
|
+
return false if set?("NO_COLOR") || ENV.fetch("TERM", "") == "dumb"
|
|
24
|
+
return true if set?("FORCE_COLOR") && ENV.fetch("FORCE_COLOR", "") != "0"
|
|
25
|
+
|
|
26
|
+
io.tty?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.set?(name)
|
|
30
|
+
!ENV.fetch(name, "").empty?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def initialize(on)
|
|
34
|
+
@on = on
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def on?
|
|
38
|
+
@on
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# +text+ in the SGR style +code+, such as Paint::RED.
|
|
42
|
+
def call(text, code)
|
|
43
|
+
@on ? "\e[#{code}m#{text}\e[0m" : text
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/judgelint/question.rb
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
module Judgelint
|
|
4
4
|
# One rule asked about one range of a file. The model sees the whole file;
|
|
5
|
-
# the question points at the lines.
|
|
5
|
+
# the question points at the lines. When a change touched only some of a
|
|
6
|
+
# window's lines, it points at those, so code the change didn't touch can't
|
|
7
|
+
# fail it.
|
|
6
8
|
#
|
|
7
9
|
# A spec's bullet is a choice between breaking the rule, following it, and
|
|
8
10
|
# the rule not applying. With the three side by side, a decision model
|
|
@@ -19,16 +21,18 @@ module Judgelint
|
|
|
19
21
|
EXCUSED = "A judgehint comment is about this rule, and gives a reason the code may break it."
|
|
20
22
|
NOT_EXCUSED = "No judgehint comment is about this rule. Any judgehint is about a different rule, or gives no reason."
|
|
21
23
|
|
|
22
|
-
attr_reader :rule, :first, :last, :places
|
|
24
|
+
attr_reader :rule, :first, :last, :places, :lines
|
|
23
25
|
|
|
24
26
|
# +kind+ is :verdict, :where, or :excuse. +places+ are a :where question's
|
|
25
27
|
# options: line ids (L12) or the directory's files, as the model sees them.
|
|
26
|
-
|
|
28
|
+
# +lines+ are the changed lines in the range, or nil to ask about all of it.
|
|
29
|
+
def initialize(rule, first, last, kind: :verdict, places: [], lines: nil)
|
|
27
30
|
@rule = rule
|
|
28
31
|
@first = first
|
|
29
32
|
@last = last
|
|
30
33
|
@kind = kind
|
|
31
34
|
@places = places
|
|
35
|
+
@lines = lines
|
|
32
36
|
end
|
|
33
37
|
|
|
34
38
|
def excuse?
|
|
@@ -72,9 +76,9 @@ module Judgelint
|
|
|
72
76
|
def instructions
|
|
73
77
|
return excuse_instructions if excuse?
|
|
74
78
|
return where_instructions if where?
|
|
75
|
-
return "How #{rule.scope == 'file' ? 'does' : 'do'} #{subject} relate to the following team rule? Rule: #{rule.convention}" if rule.convention
|
|
79
|
+
return "How #{rule.scope == 'file' || lines&.size == 1 ? 'does' : 'do'} #{subject} relate to the following team rule? Rule: #{rule.convention}" if rule.convention
|
|
76
80
|
|
|
77
|
-
text = rule.window? ? "Look at
|
|
81
|
+
text = rule.window? ? "Look at #{span} of the file; the rest is context. #{rule.question}" : rule.question
|
|
78
82
|
return text unless rule.fail_when == "yes"
|
|
79
83
|
|
|
80
84
|
scope = { "window" => "those lines do it", "file" => "the code does it", "directory" => "the files do it" }
|
|
@@ -104,8 +108,24 @@ module Judgelint
|
|
|
104
108
|
private
|
|
105
109
|
|
|
106
110
|
def subject
|
|
107
|
-
{ "window" => "
|
|
108
|
-
|
|
111
|
+
{ "window" => "#{span} of the file", "file" => "the file", "directory" => "the files" }.fetch(rule.scope)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The lines asked about: "lines 41-80", or the changed ones, as "line 7" or
|
|
115
|
+
# "lines 3, 7-9".
|
|
116
|
+
def span
|
|
117
|
+
return "lines #{first}-#{last}" if lines.nil?
|
|
118
|
+
return "line #{lines.first}" if lines.size == 1
|
|
119
|
+
|
|
120
|
+
runs = []
|
|
121
|
+
lines.each do |line|
|
|
122
|
+
if !runs.empty? && runs.last[1] == line - 1
|
|
123
|
+
runs.last[1] = line
|
|
124
|
+
else
|
|
125
|
+
runs << [line, line]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
"lines #{runs.map { |run| run[0] == run[1] ? run[0].to_s : "#{run[0]}-#{run[1]}" }.join(', ')}"
|
|
109
129
|
end
|
|
110
130
|
|
|
111
131
|
def where_instructions
|
data/lib/judgelint/reporter.rb
CHANGED
|
@@ -6,11 +6,15 @@ module Judgelint
|
|
|
6
6
|
class Reporter
|
|
7
7
|
FORMATS = %w[text json github sarif].freeze
|
|
8
8
|
MARKS = { violation: "x", uncertain: "?", skipped: "-", refused: "-", excused: "-" }.freeze
|
|
9
|
+
# In color, a violation's mark is a cross.
|
|
10
|
+
COLOR_MARKS = { violation: "\u2717", uncertain: "?", skipped: "-", refused: "-", excused: "-" }.freeze
|
|
9
11
|
STATUSES = %w[violation uncertain pass skipped refused excused].freeze
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
# +paint+ colors the text format; the others are never colored.
|
|
14
|
+
def initialize(out, format:, paint: Paint.new(false))
|
|
12
15
|
@out = out
|
|
13
16
|
@format = format
|
|
17
|
+
@paint = paint
|
|
14
18
|
end
|
|
15
19
|
|
|
16
20
|
def check(verdicts, config:, file_count:, usage:)
|
|
@@ -27,14 +31,18 @@ module Judgelint
|
|
|
27
31
|
|
|
28
32
|
examples.group_by(&:rule).each do |rule, group|
|
|
29
33
|
correct = group.count(&:correct?)
|
|
30
|
-
@
|
|
34
|
+
tally = @paint.call("#{correct}/#{group.size}", correct == group.size ? Paint::GREEN : Paint::RED)
|
|
35
|
+
@out.puts "#{@paint.call(rule.id, Paint::BOLD)} #{tally}"
|
|
31
36
|
group.each do |example|
|
|
32
|
-
mark = example.correct? ? "ok" : "MISS"
|
|
33
|
-
@out.puts
|
|
37
|
+
mark = example.correct? ? cell("ok", 4, Paint::GREEN) : @paint.call("MISS", Paint::BOLD_RED)
|
|
38
|
+
@out.puts " #{mark} #{cell(example.expected.to_s, 4, Paint::DIM)} " \
|
|
39
|
+
"#{probability(example)} #{@paint.call(example.path, Paint::CYAN)}"
|
|
34
40
|
end
|
|
35
41
|
end
|
|
36
42
|
@out.puts "" unless examples.empty?
|
|
37
|
-
|
|
43
|
+
correct = examples.count(&:correct?)
|
|
44
|
+
@out.puts @paint.call("#{correct}/#{examples.size} examples judged correctly",
|
|
45
|
+
correct == examples.size ? Paint::BOLD_GREEN : Paint::BOLD_RED)
|
|
38
46
|
end
|
|
39
47
|
|
|
40
48
|
private
|
|
@@ -48,17 +56,34 @@ module Judgelint
|
|
|
48
56
|
width = shown.map { |verdict| verdict.rule.id.size }.max
|
|
49
57
|
places = [7, *shown.map { |verdict| range(verdict).size }].max
|
|
50
58
|
shown.group_by(&:path).each do |path, group|
|
|
51
|
-
@out.puts path
|
|
59
|
+
@out.puts @paint.call(path, Paint::BOLD)
|
|
52
60
|
group.each { |verdict| @out.puts " #{line(verdict, width, places)}" }
|
|
53
61
|
end
|
|
54
62
|
@out.puts "" unless shown.empty?
|
|
55
63
|
@out.puts summary(verdicts, file_count)
|
|
56
64
|
end
|
|
57
65
|
|
|
66
|
+
# Columns are padded before they're colored, so they line up either way.
|
|
58
67
|
def line(verdict, width, places)
|
|
59
68
|
label = verdict.status == :violation ? verdict.rule.severity : verdict.status.to_s
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
marks = @paint.on? ? COLOR_MARKS : MARKS
|
|
70
|
+
tone = tone(verdict)
|
|
71
|
+
[cell("#{marks.fetch(verdict.status)} #{label}", 12, tone), cell(verdict.rule.id, width, Paint::DIM),
|
|
72
|
+
" #{cell(range(verdict), places, Paint::CYAN)}", "#{@paint.call(probability(verdict), tone)} ",
|
|
73
|
+
verdict.rule.message].join(" ")
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# +text+ in color, padded to +width+ outside it.
|
|
77
|
+
def cell(text, width, code)
|
|
78
|
+
"#{@paint.call(text, code)}#{' ' * [width - text.size, 0].max}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Errors are red, warnings and uncertain findings yellow, the rest dim.
|
|
82
|
+
def tone(verdict)
|
|
83
|
+
return Paint::YELLOW if verdict.status == :uncertain
|
|
84
|
+
return Paint::DIM unless verdict.status == :violation
|
|
85
|
+
|
|
86
|
+
verdict.rule.error? ? Paint::BOLD_RED : Paint::YELLOW
|
|
62
87
|
end
|
|
63
88
|
|
|
64
89
|
# The line that breaks the rule, or the range asked about; for a
|
|
@@ -78,11 +103,13 @@ module Judgelint
|
|
|
78
103
|
|
|
79
104
|
def summary(verdicts, file_count)
|
|
80
105
|
counts = verdicts.group_by(&:status).transform_values(&:size)
|
|
81
|
-
|
|
82
|
-
parts
|
|
83
|
-
|
|
84
|
-
parts << "#{counts[:
|
|
85
|
-
parts << "#{counts[:
|
|
106
|
+
violations = counts.fetch(:violation, 0)
|
|
107
|
+
parts = ["#{file_count} #{file_count == 1 ? 'file' : 'files'}",
|
|
108
|
+
@paint.call("#{violations} violations", violations.zero? ? Paint::BOLD_GREEN : Paint::BOLD_RED)]
|
|
109
|
+
parts << @paint.call("#{counts[:uncertain]} uncertain", Paint::YELLOW) if counts[:uncertain]
|
|
110
|
+
parts << @paint.call("#{counts[:skipped]} skipped (too large)", Paint::DIM) if counts[:skipped]
|
|
111
|
+
parts << @paint.call("#{counts[:refused]} refused by the model", Paint::DIM) if counts[:refused]
|
|
112
|
+
parts << @paint.call("#{counts[:excused]} excused by judgehints", Paint::DIM) if counts[:excused]
|
|
86
113
|
parts.join(", ")
|
|
87
114
|
end
|
|
88
115
|
|
data/lib/judgelint/rule.rb
CHANGED
|
@@ -31,7 +31,7 @@ module Judgelint
|
|
|
31
31
|
KEYS = %w[id question convention yes_means no_means fail_when scope paths excludes context severity threshold message].freeze
|
|
32
32
|
|
|
33
33
|
attr_reader :id, :question, :convention, :yes_means, :no_means, :fail_when, :scope, :paths, :excludes,
|
|
34
|
-
:context, :severity, :threshold, :message, :source, :examples_dir, :prose
|
|
34
|
+
:context, :severity, :threshold, :message, :source, :examples_dir, :prose, :inline_examples
|
|
35
35
|
|
|
36
36
|
# The rules in +dir+ alone (Spec.discover also finds READMEs).
|
|
37
37
|
def self.load_dir(dir, default_threshold:)
|
|
@@ -41,7 +41,8 @@ module Judgelint
|
|
|
41
41
|
# A rule is a question asked exactly as written, or a convention (a
|
|
42
42
|
# spec's bullet) asked whether the code breaks it.
|
|
43
43
|
# +prose+ is a spec's body, which the model reads alongside the file.
|
|
44
|
-
|
|
44
|
+
# +inline_examples+ are the Bad and Good code blocks under a spec's bullet.
|
|
45
|
+
def initialize(data, source:, body: "", default_threshold: 0.7, examples_dir: nil, prose: nil, inline_examples: [])
|
|
45
46
|
unknown = data.keys - KEYS
|
|
46
47
|
raise ConfigError, "#{source}: unknown keys #{unknown.join(', ')}" unless unknown.empty?
|
|
47
48
|
|
|
@@ -53,6 +54,7 @@ module Judgelint
|
|
|
53
54
|
|
|
54
55
|
@examples_dir = examples_dir || source.delete_suffix(".md")
|
|
55
56
|
@prose = prose
|
|
57
|
+
@inline_examples = inline_examples
|
|
56
58
|
@yes_means = text(data["yes_means"])
|
|
57
59
|
@no_means = text(data["no_means"])
|
|
58
60
|
@fail_when = one_of("fail_when", yes_or_no(data.fetch("fail_when", "yes")), %w[yes no])
|
|
@@ -144,4 +146,22 @@ module Judgelint
|
|
|
144
146
|
paragraph&.gsub(/\s*\n\s*/, " ")
|
|
145
147
|
end
|
|
146
148
|
end
|
|
149
|
+
|
|
150
|
+
# A Bad or Good code block under a spec's bullet. `judgelint test` judges it
|
|
151
|
+
# like a file in the rule's fail/ or pass/ examples.
|
|
152
|
+
class InlineExample
|
|
153
|
+
attr_reader :expected, :code, :line, :language
|
|
154
|
+
|
|
155
|
+
def initialize(label, code, line, language)
|
|
156
|
+
@expected = label == "bad" ? :fail : :pass
|
|
157
|
+
@code = code
|
|
158
|
+
@line = line
|
|
159
|
+
@language = language
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# The file name the model sees.
|
|
163
|
+
def name
|
|
164
|
+
language.empty? ? "example" : "example.#{language}"
|
|
165
|
+
end
|
|
166
|
+
end
|
|
147
167
|
end
|
data/lib/judgelint/runner.rb
CHANGED
|
@@ -72,6 +72,8 @@ module Judgelint
|
|
|
72
72
|
|
|
73
73
|
# +changes+ ({path => lines or :all}, from Changes.since) limits each file
|
|
74
74
|
# to the windows a change touched, and directories to those with a change.
|
|
75
|
+
# A window rule is asked about the changed lines alone, with the rest of
|
|
76
|
+
# the file as context.
|
|
75
77
|
# Rules that read different context files can't share a request, so a
|
|
76
78
|
# file gets one job per set of context files.
|
|
77
79
|
def plan(paths, directories: [], changes: nil)
|
|
@@ -90,25 +92,29 @@ module Judgelint
|
|
|
90
92
|
|
|
91
93
|
attr_reader :untested
|
|
92
94
|
|
|
93
|
-
# Files under <examples>/pass
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
#
|
|
97
|
-
#
|
|
95
|
+
# Files under <examples>/pass and Good blocks under a spec's bullet must
|
|
96
|
+
# not be judged violations; files under <examples>/fail and Bad blocks
|
|
97
|
+
# must. The model sees each file's path relative to its pass/ or fail/
|
|
98
|
+
# directory, and a block as example.<language>, so neither gives the
|
|
99
|
+
# answer away. A rule with examples needs both kinds, checked before
|
|
100
|
+
# anything is sent; rules with none are listed in +untested+.
|
|
98
101
|
def plan_examples
|
|
99
102
|
jobs = []
|
|
100
103
|
missing = []
|
|
101
104
|
@untested = []
|
|
102
105
|
@rules.each do |rule|
|
|
103
106
|
found = %i[pass fail].to_h { |expected| [expected, example_paths(rule, expected)] }
|
|
104
|
-
|
|
107
|
+
inline = rule.directory? ? [] : rule.inline_examples
|
|
108
|
+
if found.values.all?(&:empty?) && inline.empty?
|
|
105
109
|
@untested << rule
|
|
106
110
|
next
|
|
107
111
|
end
|
|
108
112
|
|
|
109
113
|
found.each do |expected, paths|
|
|
110
114
|
dir = File.join(rule.examples_dir, expected.to_s)
|
|
111
|
-
|
|
115
|
+
if paths.empty? && inline.none? { |example| example.expected == expected }
|
|
116
|
+
missing << (inline.empty? ? "#{dir}/" : "a #{expected == :pass ? 'Good' : 'Bad'} block under #{rule.id}")
|
|
117
|
+
end
|
|
112
118
|
paths.each do |path|
|
|
113
119
|
shown = path.delete_prefix("#{dir}/")
|
|
114
120
|
context = rule.context_files
|
|
@@ -117,12 +123,22 @@ module Judgelint
|
|
|
117
123
|
jobs << plan_file(source, [rule], :all, expected: expected)
|
|
118
124
|
end
|
|
119
125
|
end
|
|
126
|
+
inline.each do |example|
|
|
127
|
+
source = Source.new("#{rule.source}:#{example.line}", shown_as: example.name, text: example.code,
|
|
128
|
+
context: rule.context_files)
|
|
129
|
+
jobs << plan_file(source, [rule], :all, expected: example.expected)
|
|
130
|
+
end
|
|
120
131
|
end
|
|
121
|
-
raise ConfigError, "Examples need both kinds: add
|
|
132
|
+
raise ConfigError, "Examples need both kinds: add #{missing.join(', ')}" unless missing.empty?
|
|
122
133
|
|
|
123
134
|
jobs
|
|
124
135
|
end
|
|
125
136
|
|
|
137
|
+
# Every model the jobs will ask needs its key.
|
|
138
|
+
def check_keys(jobs)
|
|
139
|
+
jobs.flat_map { |job| job.requests.map(&:model) }.uniq.each { |model| @clients[model].check_key }
|
|
140
|
+
end
|
|
141
|
+
|
|
126
142
|
def request_count(jobs)
|
|
127
143
|
jobs.sum { |job| job.requests.size }
|
|
128
144
|
end
|
|
@@ -168,7 +184,7 @@ module Judgelint
|
|
|
168
184
|
|
|
169
185
|
asked = []
|
|
170
186
|
ranges.each do |first, last|
|
|
171
|
-
asked << Question.new(rule, first, last)
|
|
187
|
+
asked << Question.new(rule, first, last, lines: changed_lines(rule, lines, first, last))
|
|
172
188
|
places = places(source, rule, first, last)
|
|
173
189
|
asked << Question.new(rule, first, last, kind: :where, places: places) unless places.empty?
|
|
174
190
|
asked << Question.new(rule, first, last, kind: :excuse) if source.hinted?(first, last)
|
|
@@ -195,6 +211,14 @@ module Judgelint
|
|
|
195
211
|
rules.group_by { |rule| [rule.context, rule.prose] }.map { |(globs, _prose), group| yield(Glob.expand(globs), group) }
|
|
196
212
|
end
|
|
197
213
|
|
|
214
|
+
# The lines a change touched in a window rule's range, or nil for all of them.
|
|
215
|
+
def changed_lines(rule, lines, first, last)
|
|
216
|
+
return nil if lines == :all || !rule.window?
|
|
217
|
+
|
|
218
|
+
changed = lines.select { |line| line.between?(first, last) }.uniq.sort
|
|
219
|
+
changed.size == last - first + 1 ? nil : changed
|
|
220
|
+
end
|
|
221
|
+
|
|
198
222
|
def touched?(lines, first, last)
|
|
199
223
|
lines == :all || (lines || []).any? { |line| line.between?(first, last) }
|
|
200
224
|
end
|
|
@@ -262,9 +286,11 @@ module Judgelint
|
|
|
262
286
|
|
|
263
287
|
# A window's probability of a violation is the average of its verdict
|
|
264
288
|
# over the models, and its place is the line or file the models' "where"
|
|
265
|
-
# answers agree on.
|
|
266
|
-
#
|
|
267
|
-
#
|
|
289
|
+
# answers agree on. When a change touched only some of its lines, the
|
|
290
|
+
# probability counts only the share of "where" answers on those lines. A
|
|
291
|
+
# rule's verdict is its worst window. A window whose judgehint excuses it,
|
|
292
|
+
# by the same average and threshold, is reported as excused instead,
|
|
293
|
+
# unless another window breaks the rule.
|
|
268
294
|
def verdict_for(rule, path, questions, job)
|
|
269
295
|
return Verdict.new(rule: rule, path: path, probability: nil, status: :skipped) if job.skipped
|
|
270
296
|
|
|
@@ -290,7 +316,10 @@ module Judgelint
|
|
|
290
316
|
end
|
|
291
317
|
next if values.empty?
|
|
292
318
|
|
|
293
|
-
|
|
319
|
+
probability = values.sum / values.size
|
|
320
|
+
changed = asked.first.lines # the verdict question comes first
|
|
321
|
+
probability *= changed_share(spread, changed) if changed
|
|
322
|
+
window = Window.new(first, last, probability, agreed(spread))
|
|
294
323
|
if !excuses.empty? && excuses.sum / excuses.size >= rule.threshold
|
|
295
324
|
excused << window
|
|
296
325
|
else
|
|
@@ -309,6 +338,18 @@ module Judgelint
|
|
|
309
338
|
line: hidden.line, file: hidden.file)
|
|
310
339
|
end
|
|
311
340
|
|
|
341
|
+
# The share of the summed "where" probabilities on +lines+, or all of it
|
|
342
|
+
# when nothing answered where.
|
|
343
|
+
def changed_share(spread, lines)
|
|
344
|
+
total = 0.0
|
|
345
|
+
changed = 0.0
|
|
346
|
+
spread.each do |place, value|
|
|
347
|
+
total += value
|
|
348
|
+
changed += value if lines.include?(place.delete_prefix("L").to_i)
|
|
349
|
+
end
|
|
350
|
+
total.positive? ? changed / total : 1.0
|
|
351
|
+
end
|
|
352
|
+
|
|
312
353
|
# The place with at least PLACE_AT of the summed "where" probabilities.
|
|
313
354
|
# A loop rather than max_by, which Spinel's Hash doesn't have yet.
|
|
314
355
|
def agreed(spread)
|
data/lib/judgelint/scaffold.rb
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Judgelint
|
|
4
|
-
#
|
|
4
|
+
# The file `judgelint init` writes.
|
|
5
5
|
module Scaffold
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
models: [typesafe] # `judgelint providers` lists the others; list two to average them
|
|
9
|
-
# threshold: 0.7
|
|
10
|
-
# ignore: ["vendor/**"]
|
|
11
|
-
# max_cost_usd: 1.00
|
|
12
|
-
YAML
|
|
13
|
-
|
|
14
|
-
# A spec: each bullet is a convention judgelint checks. A README with
|
|
15
|
-
# paths: frontmatter works the same way, next to the code it governs.
|
|
6
|
+
# A spec: each bullet is a convention judgelint checks, and the Bad and
|
|
7
|
+
# Good blocks under it are examples for `judgelint test`.
|
|
16
8
|
SPEC = <<~MARKDOWN
|
|
17
9
|
---
|
|
18
10
|
paths: ["**/*.rb"]
|
|
@@ -23,44 +15,51 @@ module Judgelint
|
|
|
23
15
|
- **No silent rescues.** A rescue logs the error, re-raises it, or turns it
|
|
24
16
|
into a result the caller sees. Swallowed exceptions hide bugs until they
|
|
25
17
|
cost us data.
|
|
26
|
-
- **Error messages say what to do next.** A raised or returned error names
|
|
27
|
-
what was wrong and what would work instead.
|
|
28
|
-
MARKDOWN
|
|
29
18
|
|
|
30
|
-
|
|
31
|
-
def fetch_profile(id)
|
|
32
|
-
client.profile(id)
|
|
33
|
-
rescue Client::Timeout => e
|
|
34
|
-
logger.warn("profile \#{id} timed out: \#{e.message}")
|
|
35
|
-
raise
|
|
36
|
-
end
|
|
37
|
-
RUBY
|
|
19
|
+
Bad:
|
|
38
20
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
21
|
+
```ruby
|
|
22
|
+
def fetch_profile(id)
|
|
23
|
+
client.profile(id)
|
|
24
|
+
rescue StandardError
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
```
|
|
46
28
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
files.each do |name, content|
|
|
56
|
-
path = root == "." ? name : File.join(root, name)
|
|
57
|
-
if File.exist?(path)
|
|
58
|
-
out.puts "exists #{path}"
|
|
59
|
-
else
|
|
60
|
-
FileUtils.mkdir_p(File.dirname(path))
|
|
61
|
-
File.write(path, content)
|
|
62
|
-
out.puts "create #{path}"
|
|
29
|
+
Good:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
def fetch_profile(id)
|
|
33
|
+
client.profile(id)
|
|
34
|
+
rescue Client::Timeout => e
|
|
35
|
+
logger.warn("profile \#{id} timed out: \#{e.message}")
|
|
36
|
+
raise
|
|
63
37
|
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
- **Error messages say what to do next.** A raised error names what was
|
|
41
|
+
wrong and what would work instead.
|
|
42
|
+
|
|
43
|
+
Bad:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
raise ArgumentError, "invalid plan"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Good:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
raise ArgumentError, "unknown plan \#{plan.inspect}; use one of \#{PLANS.keys.join(', ')}"
|
|
53
|
+
```
|
|
54
|
+
MARKDOWN
|
|
55
|
+
|
|
56
|
+
def self.write(root, out)
|
|
57
|
+
path = root == "." ? "CONVENTIONS.md" : File.join(root, "CONVENTIONS.md")
|
|
58
|
+
if File.exist?(path)
|
|
59
|
+
out.puts "exists #{path}"
|
|
60
|
+
else
|
|
61
|
+
File.write(path, SPEC)
|
|
62
|
+
out.puts "create #{path}"
|
|
64
63
|
end
|
|
65
64
|
end
|
|
66
65
|
end
|
data/lib/judgelint/source.rb
CHANGED
|
@@ -18,11 +18,12 @@ module Judgelint
|
|
|
18
18
|
new(dir, shown_as: shown_as, files: files, context: context, directory: true)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
# +text+ is the file's contents when it isn't on disk, like an example in a spec.
|
|
22
|
+
def initialize(path, shown_as: path, files: [path], context: [], directory: false, text: nil)
|
|
22
23
|
@path = path
|
|
23
24
|
@shown_as = shown_as
|
|
24
25
|
@directory = directory
|
|
25
|
-
@files = files.map { |file| [file, File.read(file).lines] }
|
|
26
|
+
@files = text ? [[path, text.lines]] : files.map { |file| [file, File.read(file).lines] }
|
|
26
27
|
@context = context
|
|
27
28
|
@line_count = @files.sum { |_file, lines| lines.size }
|
|
28
29
|
@hints = []
|
data/lib/judgelint/spec.rb
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Judgelint
|
|
4
|
-
# Finds rules where a team already writes its conventions: a
|
|
5
|
-
# *.judgelint.md
|
|
6
|
-
# convention per top-level bullet
|
|
4
|
+
# Finds rules where a team already writes its conventions: a
|
|
5
|
+
# CONVENTIONS.md or *.judgelint.md, or a README whose frontmatter says which
|
|
6
|
+
# files it governs, with one convention per top-level bullet.
|
|
7
|
+
#
|
|
8
|
+
# A CONVENTIONS.md or *.judgelint.md needs no frontmatter. Without paths:,
|
|
9
|
+
# it governs the files under its own directory, except Markdown and
|
|
10
|
+
# lockfiles. A README is a spec only when it has paths:.
|
|
7
11
|
#
|
|
8
12
|
# ---
|
|
9
13
|
# paths: ["src/services/*.ts"]
|
|
@@ -28,11 +32,14 @@ module Judgelint
|
|
|
28
32
|
# Files in .judgelint/rules/ work the same way, and one there with a
|
|
29
33
|
# question: in its frontmatter is a single rule asked exactly that way.
|
|
30
34
|
module Spec
|
|
31
|
-
PATTERNS = ["**/README.md", "**/*.judgelint.md"].freeze
|
|
35
|
+
PATTERNS = ["**/README.md", "**/CONVENTIONS.md", "**/*.judgelint.md"].freeze
|
|
36
|
+
# What a spec without paths: leaves out of its directory.
|
|
37
|
+
UNJUDGED = ["**/*.md", "**/*.lock", "**/package-lock.json", "**/pnpm-lock.yaml"].freeze
|
|
32
38
|
KEYS = %w[paths excludes context scope cohort type severity threshold].freeze
|
|
33
39
|
COHORTS = { "by_file" => "file", "by_directory" => "directory" }.freeze
|
|
34
40
|
BULLET = /\A(?:[-*+]|\d+[.)])\s+(\S.*)\z/
|
|
35
41
|
CRITERION = /\A(violation|compliant):\s*(\S.*)\z/i
|
|
42
|
+
LABEL = /\A[*_]*(bad|good)[*_]*:?[*_]*\z/i
|
|
36
43
|
LEAD = /\A\*\*(.+?)\*\*/
|
|
37
44
|
|
|
38
45
|
# +readmes+ false looks in +rules_dir+ alone.
|
|
@@ -41,10 +48,10 @@ module Judgelint
|
|
|
41
48
|
found = Dir.glob(File.join(rules_dir, "*.md")).sort.flat_map do |path|
|
|
42
49
|
load(path.dup, default_threshold: default_threshold, required: true)
|
|
43
50
|
end
|
|
44
|
-
specs = readmes ? Glob.
|
|
51
|
+
specs = readmes ? Glob.files(PATTERNS) : []
|
|
45
52
|
specs = specs.reject { |path| path.start_with?("#{rules_dir}/") || Glob.match_any?(ignore, path) }
|
|
46
53
|
found += specs.flat_map { |path| load(path.dup, default_threshold: default_threshold, required: false) }
|
|
47
|
-
raise ConfigError, "No rules:
|
|
54
|
+
raise ConfigError, "No rules: write your conventions as bullets in CONVENTIONS.md, or run `judgelint init`" if found.empty?
|
|
48
55
|
|
|
49
56
|
found.group_by(&:question_key).each_value do |same|
|
|
50
57
|
raise ConfigError, "Rule ids collide: #{same.map(&:id).join(', ')}" if same.size > 1
|
|
@@ -54,35 +61,40 @@ module Judgelint
|
|
|
54
61
|
|
|
55
62
|
# A README without frontmatter, or without paths:, isn't a spec.
|
|
56
63
|
def self.load(path, default_threshold:, required:)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
body = match[2]
|
|
64
|
+
content = File.read(path)
|
|
65
|
+
match = Rule::FRONTMATTER.match(content)
|
|
66
|
+
own = !required && File.basename(path) != "README.md"
|
|
67
|
+
raise ConfigError, "#{path}: rules start with a --- frontmatter block" if match.nil? && required
|
|
68
|
+
return [] if match.nil? && !own
|
|
69
|
+
|
|
70
|
+
data = match ? Judgelint.parse_yaml(match[1], path) : {}
|
|
71
|
+
body = match ? match[2] : content
|
|
66
72
|
return [Rule.new(data, source: path, body: body.strip, default_threshold: default_threshold)] if data.key?("question")
|
|
67
|
-
return [] unless required || data.key?("paths")
|
|
73
|
+
return [] unless required || own || data.key?("paths")
|
|
68
74
|
|
|
69
75
|
unknown = data.keys - KEYS
|
|
70
76
|
raise ConfigError, "#{path}: unknown keys #{unknown.join(', ')}" unless unknown.empty?
|
|
71
77
|
|
|
72
78
|
shared = shared_settings(path, data)
|
|
73
|
-
|
|
79
|
+
unless shared.key?("paths") || required
|
|
80
|
+
dir = File.dirname(path)
|
|
81
|
+
shared["paths"] = [dir == "." ? "**/*" : "#{dir}/**/*"]
|
|
82
|
+
excludes = shared["excludes"] || []
|
|
83
|
+
shared["excludes"] = (excludes.is_a?(Array) ? excludes : [excludes]) + UNJUDGED
|
|
84
|
+
end
|
|
85
|
+
found = bullets(body, content[0, content.size - body.size].count("\n"))
|
|
74
86
|
raise ConfigError, "#{path}: a rule needs a question, or bullets to turn into rules" if found.empty? && required
|
|
75
87
|
|
|
76
88
|
prose = body.strip
|
|
77
89
|
ids = []
|
|
78
|
-
found.map do |text, criteria|
|
|
90
|
+
found.map do |text, criteria, examples|
|
|
79
91
|
id = unique("#{name(path)}/#{slug(text)}", ids)
|
|
80
92
|
rule = shared.merge("id" => id, "convention" => plain(text), "message" => lead(text))
|
|
81
93
|
rule["yes_means"] = plain(criteria["violation"]) if criteria["violation"]
|
|
82
94
|
rule["no_means"] = plain(criteria["compliant"]) if criteria["compliant"]
|
|
83
95
|
rule["severity"] = severity(text) unless data.key?("severity")
|
|
84
96
|
Rule.new(rule, source: path, default_threshold: default_threshold, prose: prose,
|
|
85
|
-
examples_dir: File.join(".judgelint", "examples", id))
|
|
97
|
+
examples_dir: File.join(".judgelint", "examples", id), inline_examples: examples)
|
|
86
98
|
end
|
|
87
99
|
end
|
|
88
100
|
|
|
@@ -100,49 +112,88 @@ module Judgelint
|
|
|
100
112
|
end
|
|
101
113
|
|
|
102
114
|
# The top-level bullets of the body, outside code fences, as [text,
|
|
103
|
-
# criteria]: the text with its continuation lines and nested
|
|
104
|
-
# any indented violation: or compliant: lines
|
|
105
|
-
|
|
115
|
+
# criteria, examples]: the text with its continuation lines and nested
|
|
116
|
+
# bullets, any indented violation: or compliant: lines, and the code blocks
|
|
117
|
+
# under a "Bad:" or "Good:" line. +offset+ is the number of lines before
|
|
118
|
+
# the body, so an example's line number is the spec file's.
|
|
119
|
+
def self.bullets(body, offset = 0)
|
|
106
120
|
found = []
|
|
107
121
|
current = nil
|
|
108
122
|
criteria = {}
|
|
109
123
|
criterion = nil
|
|
124
|
+
examples = []
|
|
125
|
+
label = nil
|
|
110
126
|
fenced = false
|
|
127
|
+
indent = 0
|
|
128
|
+
code = nil
|
|
129
|
+
language = ""
|
|
130
|
+
start = 0
|
|
131
|
+
number = offset
|
|
111
132
|
body.each_line do |raw|
|
|
133
|
+
number += 1
|
|
112
134
|
line = raw.chomp
|
|
113
135
|
if line.lstrip.start_with?("```")
|
|
136
|
+
if fenced
|
|
137
|
+
examples << InlineExample.new(label, code.join, start, language) if code
|
|
138
|
+
code = nil
|
|
139
|
+
else
|
|
140
|
+
indent = line.size - line.lstrip.size
|
|
141
|
+
if current && label && indent.positive?
|
|
142
|
+
code = []
|
|
143
|
+
start = number + 1
|
|
144
|
+
language = line.strip.delete_prefix("```").split(" ").first.to_s
|
|
145
|
+
end
|
|
146
|
+
end
|
|
114
147
|
fenced = !fenced
|
|
115
148
|
next
|
|
116
149
|
end
|
|
117
|
-
|
|
150
|
+
if fenced
|
|
151
|
+
code << deindent(raw, indent) if code
|
|
152
|
+
next
|
|
153
|
+
end
|
|
118
154
|
|
|
119
155
|
bullet = BULLET.match(line)
|
|
120
156
|
if bullet
|
|
121
|
-
found << [current, criteria] if current
|
|
157
|
+
found << [current, criteria, examples] if current
|
|
122
158
|
current = bullet[1]
|
|
123
159
|
criteria = {}
|
|
124
160
|
criterion = nil
|
|
161
|
+
examples = []
|
|
162
|
+
label = nil
|
|
125
163
|
elsif current && line.start_with?(" ", "\t") && !line.strip.empty?
|
|
126
164
|
nested = BULLET.match(line.strip)
|
|
127
165
|
text = nested ? nested[1] : line.strip
|
|
128
166
|
named = CRITERION.match(text)
|
|
167
|
+
labeled = LABEL.match(text)
|
|
129
168
|
if named
|
|
130
169
|
criterion = named[1].downcase
|
|
131
170
|
criteria[criterion] = named[2]
|
|
171
|
+
elsif labeled
|
|
172
|
+
label = labeled[1].downcase
|
|
173
|
+
criterion = nil
|
|
174
|
+
elsif label
|
|
175
|
+
# Commentary on the examples isn't part of the rule.
|
|
132
176
|
elsif criterion
|
|
133
177
|
criteria[criterion] = "#{criteria[criterion]} #{text}"
|
|
134
178
|
else
|
|
135
179
|
current = "#{current} #{text}"
|
|
136
180
|
end
|
|
137
181
|
elsif current && !line.strip.empty?
|
|
138
|
-
found << [current, criteria]
|
|
182
|
+
found << [current, criteria, examples]
|
|
139
183
|
current = nil
|
|
140
184
|
end
|
|
141
185
|
end
|
|
142
|
-
found << [current, criteria] if current
|
|
186
|
+
found << [current, criteria, examples] if current
|
|
143
187
|
found
|
|
144
188
|
end
|
|
145
189
|
|
|
190
|
+
# A code block's line without the indentation its fence had.
|
|
191
|
+
def self.deindent(raw, indent)
|
|
192
|
+
cut = 0
|
|
193
|
+
cut += 1 while cut < indent && [" ", "\t"].include?(raw[cut])
|
|
194
|
+
raw[cut..].to_s
|
|
195
|
+
end
|
|
196
|
+
|
|
146
197
|
# The bullet with its markdown emphasis removed: what the model is asked about.
|
|
147
198
|
def self.plain(text)
|
|
148
199
|
text.gsub("**", "").gsub(/\s+/, " ").strip
|
|
@@ -175,7 +226,7 @@ module Judgelint
|
|
|
175
226
|
# Spec names: a README's directory, a *.judgelint.md's stem, or a rules file's name.
|
|
176
227
|
def self.name(path)
|
|
177
228
|
dir = File.dirname(path)
|
|
178
|
-
return(dir == "." ? "root" : dir) if File.basename(path)
|
|
229
|
+
return(dir == "." ? "root" : dir) if %w[README.md CONVENTIONS.md].include?(File.basename(path))
|
|
179
230
|
|
|
180
231
|
File.basename(path).delete_suffix(".md").delete_suffix(".judgelint")
|
|
181
232
|
end
|
data/lib/judgelint/version.rb
CHANGED
data/lib/judgelint.rb
CHANGED
|
@@ -24,6 +24,7 @@ require_relative "judgelint/verdict"
|
|
|
24
24
|
require_relative "judgelint/protocol"
|
|
25
25
|
require_relative "judgelint/client"
|
|
26
26
|
require_relative "judgelint/runner"
|
|
27
|
+
require_relative "judgelint/paint"
|
|
27
28
|
require_relative "judgelint/reporter"
|
|
28
29
|
require_relative "judgelint/scaffold"
|
|
29
30
|
require_relative "judgelint/cli"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: judgelint
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- JP Camara
|
|
@@ -33,6 +33,7 @@ files:
|
|
|
33
33
|
- lib/judgelint/config.rb
|
|
34
34
|
- lib/judgelint/errors.rb
|
|
35
35
|
- lib/judgelint/glob.rb
|
|
36
|
+
- lib/judgelint/paint.rb
|
|
36
37
|
- lib/judgelint/pool.rb
|
|
37
38
|
- lib/judgelint/protocol.rb
|
|
38
39
|
- lib/judgelint/providers.rb
|