judgelint 0.1.2 → 0.1.5
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 +26 -0
- data/README.md +83 -17
- 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/reporter.rb +40 -13
- data/lib/judgelint/rule.rb +22 -2
- data/lib/judgelint/runner.rb +22 -8
- 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: 53450ae14a634168662b5a717c4858c80688c39eb7dc497352fa7760bdcca165
|
|
4
|
+
data.tar.gz: 05c4a391332c478ab270c30f47d2b5765115b37cd9e378785a1fd61ad06fab04
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a7e38d374feea8a3e2df909ffda5ad9ad15ea0e28cea55ad5dde51610d37845b539b79a3b1f2a601b9db9f23182bdcca8480e1fa01c6bd59878d5ce29223f766
|
|
7
|
+
data.tar.gz: ddfca2bbb69585e5c21f8b4dddd97aa1bbbc72cefa7bfb0e5d37c962980d96d28bd140eed98779f6e5607b7c07226e8d429e01b9870c71ab22eef3010e4a8c06
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.5 (2026-10-26)
|
|
4
|
+
|
|
5
|
+
- The text report is in color in a terminal: errors in red, warnings and
|
|
6
|
+
uncertain findings in yellow, and file names in bold. `judgelint test`
|
|
7
|
+
marks hits green and misses red. `NO_COLOR` turns color off and
|
|
8
|
+
`FORCE_COLOR` turns it on. Piped output is unchanged.
|
|
9
|
+
|
|
10
|
+
## 0.1.4 (2026-10-24)
|
|
11
|
+
|
|
12
|
+
- `Bad:` and `Good:` code blocks under a bullet are examples. The model reads
|
|
13
|
+
them with the spec, and `judgelint test` judges each one, along with the
|
|
14
|
+
files in `.judgelint/examples/`.
|
|
15
|
+
- `judgelint init` writes a `CONVENTIONS.md` with examples in it.
|
|
16
|
+
- `examples/conventions` has complete specs for Ruby, TypeScript, Python, and
|
|
17
|
+
Postgres migrations.
|
|
18
|
+
|
|
19
|
+
## 0.1.3 (2026-10-24)
|
|
20
|
+
|
|
21
|
+
- No setup beyond `TYPESAFE_API_KEY`. A `CONVENTIONS.md` of bullets is a
|
|
22
|
+
spec with no frontmatter, and it covers the files in its directory and
|
|
23
|
+
below, except Markdown and lockfiles. A README still needs `paths:`.
|
|
24
|
+
- The cost line names the model.
|
|
25
|
+
- Full runs skip files git ignores, and binary files.
|
|
26
|
+
- `judgelint init` no longer writes `.judgelint.yml`.
|
|
27
|
+
- A run the cache answers in full needs no API key.
|
|
28
|
+
|
|
3
29
|
## 0.1.2 (2026-10-24)
|
|
4
30
|
|
|
5
31
|
- 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
|
|
@@ -169,7 +235,7 @@ merge base.
|
|
|
169
235
|
|
|
170
236
|
## Configuration
|
|
171
237
|
|
|
172
|
-
`.judgelint.yml` has four settings:
|
|
238
|
+
judgelint runs without a config file. `.judgelint.yml` has four settings:
|
|
173
239
|
|
|
174
240
|
```yaml
|
|
175
241
|
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/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
|
@@ -90,25 +90,29 @@ module Judgelint
|
|
|
90
90
|
|
|
91
91
|
attr_reader :untested
|
|
92
92
|
|
|
93
|
-
# Files under <examples>/pass
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
#
|
|
97
|
-
#
|
|
93
|
+
# Files under <examples>/pass and Good blocks under a spec's bullet must
|
|
94
|
+
# not be judged violations; files under <examples>/fail and Bad blocks
|
|
95
|
+
# must. The model sees each file's path relative to its pass/ or fail/
|
|
96
|
+
# directory, and a block as example.<language>, so neither gives the
|
|
97
|
+
# answer away. A rule with examples needs both kinds, checked before
|
|
98
|
+
# anything is sent; rules with none are listed in +untested+.
|
|
98
99
|
def plan_examples
|
|
99
100
|
jobs = []
|
|
100
101
|
missing = []
|
|
101
102
|
@untested = []
|
|
102
103
|
@rules.each do |rule|
|
|
103
104
|
found = %i[pass fail].to_h { |expected| [expected, example_paths(rule, expected)] }
|
|
104
|
-
|
|
105
|
+
inline = rule.directory? ? [] : rule.inline_examples
|
|
106
|
+
if found.values.all?(&:empty?) && inline.empty?
|
|
105
107
|
@untested << rule
|
|
106
108
|
next
|
|
107
109
|
end
|
|
108
110
|
|
|
109
111
|
found.each do |expected, paths|
|
|
110
112
|
dir = File.join(rule.examples_dir, expected.to_s)
|
|
111
|
-
|
|
113
|
+
if paths.empty? && inline.none? { |example| example.expected == expected }
|
|
114
|
+
missing << (inline.empty? ? "#{dir}/" : "a #{expected == :pass ? 'Good' : 'Bad'} block under #{rule.id}")
|
|
115
|
+
end
|
|
112
116
|
paths.each do |path|
|
|
113
117
|
shown = path.delete_prefix("#{dir}/")
|
|
114
118
|
context = rule.context_files
|
|
@@ -117,12 +121,22 @@ module Judgelint
|
|
|
117
121
|
jobs << plan_file(source, [rule], :all, expected: expected)
|
|
118
122
|
end
|
|
119
123
|
end
|
|
124
|
+
inline.each do |example|
|
|
125
|
+
source = Source.new("#{rule.source}:#{example.line}", shown_as: example.name, text: example.code,
|
|
126
|
+
context: rule.context_files)
|
|
127
|
+
jobs << plan_file(source, [rule], :all, expected: example.expected)
|
|
128
|
+
end
|
|
120
129
|
end
|
|
121
|
-
raise ConfigError, "Examples need both kinds: add
|
|
130
|
+
raise ConfigError, "Examples need both kinds: add #{missing.join(', ')}" unless missing.empty?
|
|
122
131
|
|
|
123
132
|
jobs
|
|
124
133
|
end
|
|
125
134
|
|
|
135
|
+
# Every model the jobs will ask needs its key.
|
|
136
|
+
def check_keys(jobs)
|
|
137
|
+
jobs.flat_map { |job| job.requests.map(&:model) }.uniq.each { |model| @clients[model].check_key }
|
|
138
|
+
end
|
|
139
|
+
|
|
126
140
|
def request_count(jobs)
|
|
127
141
|
jobs.sum { |job| job.requests.size }
|
|
128
142
|
end
|
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.5
|
|
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
|