sloplint 0.8.1 → 0.10.0
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 +230 -1
- data/README.md +181 -63
- data/docs/SPEC.md +32 -7
- data/exe/sloplint +2 -1
- data/lib/sloplint/cli.rb +242 -42
- data/lib/sloplint/engine.rb +39 -4
- data/lib/sloplint/output.rb +10 -6
- data/lib/sloplint/rules.rb +215 -4
- data/lib/sloplint/split.rb +375 -0
- data/lib/sloplint/version.rb +1 -1
- metadata +2 -1
data/lib/sloplint/cli.rb
CHANGED
|
@@ -56,6 +56,10 @@ module Sloplint
|
|
|
56
56
|
strict = false
|
|
57
57
|
select = nil
|
|
58
58
|
ignore = nil
|
|
59
|
+
judge = false
|
|
60
|
+
register = nil
|
|
61
|
+
backend = nil
|
|
62
|
+
help = false
|
|
59
63
|
p = OptionParser.new do |o|
|
|
60
64
|
o.banner = "usage: sloplint check [options] [paths...] (\"-\" or no paths = stdin)"
|
|
61
65
|
o.on("-o", "--output-format FORMAT", %w[full json],
|
|
@@ -63,21 +67,149 @@ module Sloplint
|
|
|
63
67
|
o.on("--markdown", "Skip fenced/inline code spans, HTML comments, and URLs before scanning.") { markdown = true }
|
|
64
68
|
o.on("--select IDS", "Only run these rules (comma-separated rule ids or category names).") { |v| select = v.split(",").map(&:strip) }
|
|
65
69
|
o.on("--ignore IDS", "Skip these rules (comma-separated rule ids or category names).") { |v| ignore = v.split(",").map(&:strip) }
|
|
66
|
-
o.on("--strict", "Run every rule, including the ones that are off by default
|
|
70
|
+
o.on("--strict", "Run every rule, including the ones that are off by default;",
|
|
71
|
+
"with --judge, also ask the sentence rules about every sentence, about three times the requests.",
|
|
72
|
+
"It also reports low-confidence notes and answers: places to look, not findings.") { strict = true }
|
|
73
|
+
o.on("--judge", "Also run sloplint-judge's rules, which ask a model (needs the gem and a key).",
|
|
74
|
+
"`sloplint explain ID` explains their notes too.") { judge = true }
|
|
75
|
+
o.on("--register TEXT", "With --judge: who reads the document. The judge asks every question",
|
|
76
|
+
"on that reader's behalf, so the same sentence is news to one reader",
|
|
77
|
+
"and filler to another. Default: #{DEFAULT_REGISTER},",
|
|
78
|
+
"which leaves the model to guess. Name the reader when you know it, e.g.",
|
|
79
|
+
"\"a developer deciding whether to install this tool, reading its README\".") { |v| register = v }
|
|
80
|
+
o.on("--backend NAME", "With --judge: which model adapter to use.") { |v| backend = v }
|
|
81
|
+
# OptionParser answers -h itself when nobody else does, and it answers
|
|
82
|
+
# it on the real stdout and ends the process. This command prints to
|
|
83
|
+
# the out it was given and returns, as every other one does.
|
|
84
|
+
o.on("-h", "--help", "Show this help.") { out.puts(o.help); help = true }
|
|
67
85
|
end
|
|
68
|
-
|
|
86
|
+
# permute!, so a flag written after the path is a flag: `sloplint check
|
|
87
|
+
# draft.md --judge` reads the way anyone would write it.
|
|
88
|
+
p.permute!(argv)
|
|
89
|
+
return 0 if help
|
|
69
90
|
|
|
70
|
-
|
|
91
|
+
# The judge is a separate gem that depends on this one. The bare require
|
|
92
|
+
# goes through the load path: exe/sloplint puts this checkout's lib/ at
|
|
93
|
+
# the front, so from the plugin tree the judge files beside this one win,
|
|
94
|
+
# and an installed sloplint-judge gem is found otherwise. Only a missing
|
|
95
|
+
# judge is the install hint; any other LoadError is a real one.
|
|
96
|
+
if judge
|
|
97
|
+
begin
|
|
98
|
+
require "sloplint/judge"
|
|
99
|
+
# A Gem::LoadError is an installed sloplint-judge that RubyGems will
|
|
100
|
+
# not activate beside this sloplint: a conflict, or no version that
|
|
101
|
+
# fits. It carries no path, and to the reader it is the same thing as
|
|
102
|
+
# no judge at all, which is how `--help` already reports it.
|
|
103
|
+
rescue LoadError => e
|
|
104
|
+
raise unless e.path == "sloplint/judge" || e.is_a?(Gem::LoadError)
|
|
105
|
+
|
|
106
|
+
err.puts("sloplint: --judge needs the sloplint-judge gem: gem install sloplint-judge")
|
|
107
|
+
# An installed judge that RubyGems will not activate is a version
|
|
108
|
+
# conflict, and the hint above tells the reader to install what
|
|
109
|
+
# they already have. RubyGems says which versions fell out, so
|
|
110
|
+
# print that too rather than throwing it away.
|
|
111
|
+
err.puts("sloplint: #{e.message}") if e.is_a?(Gem::LoadError)
|
|
112
|
+
return 2
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
# Accepted and then dropped, these two read as a judge run that was
|
|
116
|
+
# never asked for: `sloplint check --register "a lawyer" brief.md` runs
|
|
117
|
+
# the regex rules and says nothing about the reader it was given.
|
|
118
|
+
unless judge
|
|
119
|
+
{ "--register" => register, "--backend" => backend }.each do |flag, value|
|
|
120
|
+
next if value.nil?
|
|
121
|
+
|
|
122
|
+
err.puts("sloplint: #{flag} needs --judge: without it no model is asked anything.")
|
|
123
|
+
return 2
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
catalog = judge ? RULES + Judge::RULES : RULES
|
|
127
|
+
|
|
128
|
+
unknown = unknown_rule_refs(select, catalog) + unknown_rule_refs(ignore, catalog)
|
|
71
129
|
unless unknown.empty?
|
|
72
130
|
err.puts("sloplint: unknown rule or category: #{unknown.join(", ")}")
|
|
73
|
-
|
|
131
|
+
# Under --judge the selection is read against both catalogs, and
|
|
132
|
+
# `sloplint rules` lists only one of them: a judge rule id typed with
|
|
133
|
+
# a letter wrong is not in the list the hint would send you to.
|
|
134
|
+
lists = judge ? "`sloplint rules` and `sloplint-judge rules`" : "`sloplint rules`"
|
|
135
|
+
err.puts("run #{lists} to list them.")
|
|
74
136
|
return 2
|
|
75
137
|
end
|
|
76
138
|
|
|
77
|
-
rules = select_rules(select, ignore, strict)
|
|
139
|
+
rules = select_rules(select, ignore, strict, catalog)
|
|
78
140
|
paths = argv.empty? ? ["-"] : argv
|
|
79
141
|
by_path = paths.reject { |x| x == "-" }.size > 1
|
|
80
142
|
|
|
143
|
+
# Only the read is invalid input. An ArgumentError out of a scan or a
|
|
144
|
+
# judge run is a bug in this code, and it raises like one instead of
|
|
145
|
+
# sending the reader to look for bad bytes in a file that has none.
|
|
146
|
+
sources = begin
|
|
147
|
+
read_sources(paths, err:, stdin:)
|
|
148
|
+
rescue ArgumentError, Encoding::CompatibilityError => e
|
|
149
|
+
err.puts("sloplint: invalid input: #{e.message}")
|
|
150
|
+
return 2
|
|
151
|
+
end
|
|
152
|
+
return 2 unless sources
|
|
153
|
+
|
|
154
|
+
regex_rules, judge_rules = rules.partition { |r| r.is_a?(Rule) }
|
|
155
|
+
|
|
156
|
+
judge_usage = nil
|
|
157
|
+
# Under --judge the output is the {"notes", "judge"} object whether or
|
|
158
|
+
# not a judge rule survived selection: a caller that asked for the judge
|
|
159
|
+
# reads the wrapper, and an empty selection is 0 requests, not a
|
|
160
|
+
# different output shape. 0 requests also means no key: `--judge
|
|
161
|
+
# --select em-dash` must run on a machine that has none.
|
|
162
|
+
#
|
|
163
|
+
# Before the regex scan, not after it: a missing key or a bad
|
|
164
|
+
# SYSTEMONE_URL is known without asking anything, and finding it out
|
|
165
|
+
# after every file has been scanned throws that work away to print the
|
|
166
|
+
# same message.
|
|
167
|
+
if judge
|
|
168
|
+
# With nothing to ask, no key is read: the backend is named and never
|
|
169
|
+
# built, and `sloplint-judge check` reports an empty selection through
|
|
170
|
+
# the same helper, so the two commands print one line. Either way an
|
|
171
|
+
# unknown --backend or a key that is not there is the same usage
|
|
172
|
+
# error, so one rescue answers for both.
|
|
173
|
+
begin
|
|
174
|
+
if judge_rules.empty?
|
|
175
|
+
judge_usage = Judge::Engine.nothing_asked(backend)
|
|
176
|
+
else
|
|
177
|
+
judge_backend = Judge::Backend.load(backend)
|
|
178
|
+
end
|
|
179
|
+
rescue ArgumentError => e
|
|
180
|
+
err.puts("sloplint: --judge: #{e.message}")
|
|
181
|
+
return 2
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
all_notes = sources.flat_map do |label, text|
|
|
186
|
+
Engine.scan(text, rules: regex_rules, markdown:, path: label)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
if judge_usage
|
|
190
|
+
err.puts("sloplint: judge #{Judge::Engine.usage_line(judge_usage)}")
|
|
191
|
+
elsif judge_backend
|
|
192
|
+
begin
|
|
193
|
+
judged = Judge::Engine.scan_sources(sources, name: "sloplint: judge", err:, rules: judge_rules, backend: judge_backend,
|
|
194
|
+
markdown:, register: register || DEFAULT_REGISTER, strict:)
|
|
195
|
+
# Exit 3 withholds the regex notes too: a caller that asked for both
|
|
196
|
+
# and got one would read it as a clean judge run.
|
|
197
|
+
rescue Judge::BackendError => e
|
|
198
|
+
err.puts("sloplint: judge backend failure: #{e.message}")
|
|
199
|
+
return 3
|
|
200
|
+
end
|
|
201
|
+
order = sources.each_with_index.to_h { |(label, _), i| [label, i] }
|
|
202
|
+
all_notes = (all_notes + judged.notes).sort_by.with_index { |n, i| [order[n.path], n.line, n.column, i] }
|
|
203
|
+
judge_usage = judged.usage
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
emit(all_notes, opts[:format], out:, by_path:, judge: judge_usage, err:, strict:)
|
|
207
|
+
all_notes.empty? ? 0 : 1
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Read every path (or stdin for "-") as UTF-8. Returns [[label, text], ...]
|
|
211
|
+
# or nil after writing the error, so the caller exits 2.
|
|
212
|
+
def read_sources(paths, err:, stdin: $stdin, name: "sloplint")
|
|
81
213
|
sources = []
|
|
82
214
|
paths.each do |path|
|
|
83
215
|
# Read as UTF-8 whatever the locale says. A sandbox with no LANG set
|
|
@@ -90,11 +222,20 @@ module Sloplint
|
|
|
90
222
|
stdin.read.force_encoding(Encoding::UTF_8)
|
|
91
223
|
else
|
|
92
224
|
unless File.file?(path)
|
|
93
|
-
err.puts("
|
|
94
|
-
return
|
|
225
|
+
err.puts("#{name}: no such file: #{path}")
|
|
226
|
+
return nil
|
|
95
227
|
end
|
|
96
228
|
File.read(path, encoding: Encoding::UTF_8)
|
|
97
229
|
end
|
|
230
|
+
# Prose that is not valid UTF-8 is an invalid input, said here rather
|
|
231
|
+
# than left to whatever reads the text first: a scan raises on the
|
|
232
|
+
# first regex, and the judge sends the text to a backend, where it
|
|
233
|
+
# would be a JSON error after the request was paid for.
|
|
234
|
+
unless text.valid_encoding?
|
|
235
|
+
err.puts("#{name}: invalid input: #{path == "-" ? "stdin" : path} is not valid UTF-8")
|
|
236
|
+
return nil
|
|
237
|
+
end
|
|
238
|
+
|
|
98
239
|
sources << [path == "-" ? "-" : path, text]
|
|
99
240
|
end
|
|
100
241
|
|
|
@@ -104,29 +245,27 @@ module Sloplint
|
|
|
104
245
|
# mistyped rule id sets, and it exits 2 for the same reason.
|
|
105
246
|
if sources.all? { |_, text| text.strip.empty? }
|
|
106
247
|
names = sources.map { |label, _| label == "-" ? "stdin" : label }
|
|
107
|
-
err.puts("
|
|
108
|
-
return
|
|
248
|
+
err.puts("#{name}: empty input: nothing to check in #{names.join(", ")}")
|
|
249
|
+
return nil
|
|
109
250
|
end
|
|
251
|
+
sources
|
|
252
|
+
end
|
|
110
253
|
|
|
111
|
-
|
|
112
|
-
|
|
254
|
+
def emit(notes, format, out:, by_path:, judge: nil, err: nil, strict: false)
|
|
255
|
+
# An agent reads --help once and the notes one at a time, so the
|
|
256
|
+
# warning goes where it acts: at the top of the run, on stderr so JSON
|
|
257
|
+
# on stdout stays parseable.
|
|
258
|
+
low = notes.count { |n| n.confidence == "low" }
|
|
259
|
+
if strict && err && low.positive?
|
|
260
|
+
err.puts("#{low} of #{notes.size} notes are low confidence (--strict). They are places to look, " \
|
|
261
|
+
"not findings: do not rewrite on their word alone.")
|
|
113
262
|
end
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
when "json"
|
|
117
|
-
out.puts(Output.format_json(all_notes, by_path:))
|
|
263
|
+
if format == "json"
|
|
264
|
+
out.puts(Output.format_json(notes, by_path:, judge:))
|
|
118
265
|
else
|
|
119
|
-
text = Output.format_human(
|
|
266
|
+
text = Output.format_human(notes)
|
|
120
267
|
out.puts(text) unless text.empty?
|
|
121
268
|
end
|
|
122
|
-
|
|
123
|
-
all_notes.empty? ? 0 : 1
|
|
124
|
-
# Invalid UTF-8 reaches this two ways: String#strip in the empty check
|
|
125
|
-
# raises Encoding::CompatibilityError, the engine's regexes raise
|
|
126
|
-
# ArgumentError. Both are the same thing to the reader.
|
|
127
|
-
rescue ArgumentError, Encoding::CompatibilityError => e
|
|
128
|
-
err.puts("sloplint: invalid input: #{e.message}")
|
|
129
|
-
2
|
|
130
269
|
end
|
|
131
270
|
|
|
132
271
|
# ── rules ───────────────────────────────────────────────────────────────
|
|
@@ -137,19 +276,26 @@ module Sloplint
|
|
|
137
276
|
o.on("--json", "Emit the catalog as JSON for machine enumeration.") { as_json = true }
|
|
138
277
|
end.order!(argv)
|
|
139
278
|
|
|
140
|
-
|
|
141
|
-
|
|
279
|
+
render_rules(RULES, json: as_json, out:)
|
|
280
|
+
0
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# The catalog as a table or as JSON. Shared with sloplint-judge, whose
|
|
284
|
+
# rules also carry a unit.
|
|
285
|
+
def render_rules(catalog, json:, out:)
|
|
286
|
+
if json
|
|
287
|
+
payload = catalog.map do |r|
|
|
142
288
|
{ id: r.id, category: r.category, severity: r.severity, confidence: r.confidence,
|
|
143
289
|
message: r.message, rationale: r.rationale, suggestion: r.suggestion }
|
|
290
|
+
.merge(r.respond_to?(:unit) ? { unit: r.unit } : {})
|
|
144
291
|
end
|
|
145
292
|
out.puts(JSON.pretty_generate(payload))
|
|
146
293
|
else
|
|
147
|
-
|
|
294
|
+
catalog.each do |r|
|
|
148
295
|
off = r.confidence == "low" ? " [off by default]" : ""
|
|
149
296
|
out.puts("#{r.id.ljust(24)} #{r.category.ljust(18)} #{r.severity.ljust(8)} #{r.confidence.ljust(7)} #{r.message}#{off}")
|
|
150
297
|
end
|
|
151
298
|
end
|
|
152
|
-
0
|
|
153
299
|
end
|
|
154
300
|
|
|
155
301
|
# ── explain ID ────────────────────────────────────────────────────────
|
|
@@ -161,8 +307,13 @@ module Sloplint
|
|
|
161
307
|
end
|
|
162
308
|
rule = RULES.find { |r| r.id == id }
|
|
163
309
|
unless rule
|
|
310
|
+
# `check --judge` prints the judge's notes too, so their ids have to
|
|
311
|
+
# explain from here as well, or a reader holding one is sent to a
|
|
312
|
+
# catalog that does not list it.
|
|
313
|
+
return Judge::CLI.run(["explain", id], out:, err:) if judge_rule?(id)
|
|
314
|
+
|
|
164
315
|
err.puts("sloplint: no such rule: #{id}")
|
|
165
|
-
err.puts("run `sloplint rules` to list them.")
|
|
316
|
+
err.puts("run `sloplint rules` to list them, and `sloplint-judge rules` for the judge's.")
|
|
166
317
|
return 2
|
|
167
318
|
end
|
|
168
319
|
out.puts(<<~TXT)
|
|
@@ -187,13 +338,22 @@ module Sloplint
|
|
|
187
338
|
examples.map { |e| e.gsub("\r", '\r').gsub("\n", '\n') }.join("\n ")
|
|
188
339
|
end
|
|
189
340
|
|
|
341
|
+
# True when the judge is installed and owns this id. A missing judge is
|
|
342
|
+
# not an error here: the id is then simply unknown.
|
|
343
|
+
def judge_rule?(id)
|
|
344
|
+
require "sloplint/judge/cli"
|
|
345
|
+
Judge::RULES.any? { |r| r.id == id }
|
|
346
|
+
rescue LoadError
|
|
347
|
+
false
|
|
348
|
+
end
|
|
349
|
+
|
|
190
350
|
# ── helpers ─────────────────────────────────────────────────────────────
|
|
191
351
|
# Ids/categories in refs that match no rule in the catalog. nil (no --select
|
|
192
352
|
# or --ignore given) passes through as no unknowns.
|
|
193
|
-
def unknown_rule_refs(refs)
|
|
353
|
+
def unknown_rule_refs(refs, catalog = RULES)
|
|
194
354
|
return [] unless refs
|
|
195
355
|
|
|
196
|
-
known =
|
|
356
|
+
known = catalog.flat_map { |r| [r.id, r.category] }.uniq
|
|
197
357
|
refs - known
|
|
198
358
|
end
|
|
199
359
|
|
|
@@ -201,15 +361,15 @@ module Sloplint
|
|
|
201
361
|
# excludes low-confidence rules unless they are explicitly selected. A
|
|
202
362
|
# category ref selects only that category's non-low rules unless --strict
|
|
203
363
|
# is set; naming a rule by its own id still selects it whatever its
|
|
204
|
-
# confidence.
|
|
205
|
-
def select_rules(select, ignore, strict = false)
|
|
364
|
+
# confidence. catalog is RULES, or RULES plus the judge's under --judge.
|
|
365
|
+
def select_rules(select, ignore, strict = false, catalog = RULES)
|
|
206
366
|
runs_by_default = ->(r) { r.confidence != "low" }
|
|
207
367
|
rules = if select
|
|
208
|
-
|
|
368
|
+
catalog.select { |r| select.include?(r.id) || (select.include?(r.category) && (runs_by_default.call(r) || strict)) }
|
|
209
369
|
elsif strict
|
|
210
|
-
|
|
370
|
+
catalog
|
|
211
371
|
else
|
|
212
|
-
|
|
372
|
+
catalog.select(&runs_by_default)
|
|
213
373
|
end
|
|
214
374
|
if ignore
|
|
215
375
|
rules = rules.reject { |r| ignore.include?(r.id) || ignore.include?(r.category) }
|
|
@@ -217,9 +377,39 @@ module Sloplint
|
|
|
217
377
|
rules
|
|
218
378
|
end
|
|
219
379
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
380
|
+
# The judge's part of the agent recipe, which depends on whether the gem
|
|
381
|
+
# is here. Looked up on the load path without loading it: help must not
|
|
382
|
+
# pull a model adapter in. The text says the one thing an agent must know
|
|
383
|
+
# before the flag: the judge sends the text to an API, so ask the person.
|
|
384
|
+
def judge_recipe
|
|
385
|
+
# Installed as a gem, the judge is not on the load path until RubyGems
|
|
386
|
+
# activates it, so the load path alone reports a missing gem. A gem on
|
|
387
|
+
# disk that asks for a different sloplint cannot be activated beside
|
|
388
|
+
# this one, and advertising it would send the agent into a conflict.
|
|
389
|
+
installed = $LOAD_PATH.resolve_feature_path("sloplint/judge") ||
|
|
390
|
+
Gem::Specification.find_all_by_name("sloplint-judge").any? { |spec|
|
|
391
|
+
spec.dependencies.find { |d| d.name == "sloplint" }&.match?("sloplint", Sloplint::VERSION)
|
|
392
|
+
}
|
|
393
|
+
unless installed
|
|
394
|
+
return "# sloplint-judge (not installed here) adds model-backed rules for what a regex cannot see: gem install sloplint-judge\n"
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
<<~RECIPE
|
|
398
|
+
# With the judge (sloplint-judge is installed here). It sends the text to api.typesafe.ai,
|
|
399
|
+
# so an agent asks the person before running it. Check first, ask, then run:
|
|
400
|
+
sloplint-judge status # exit 0 = a key is set up (reads no key), 2 = not
|
|
401
|
+
sloplint check --judge --markdown -o json --register "READER" FILE # output becomes {"notes": [...], "judge": {requests, tokens, cost_usd}}
|
|
402
|
+
# READER names who the document is for. Without it the judge guesses the reader from the text;
|
|
403
|
+
# naming one keeps the judge from guessing.
|
|
404
|
+
# exit 3 = the model could not be reached; nothing was checked, not even the regex rules
|
|
405
|
+
RECIPE
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# The banner names whether the judge gem is here, which costs a scan of
|
|
409
|
+
# the installed gems. Only --help reads it, so it is built when it is
|
|
410
|
+
# read and never on the way to a scan.
|
|
411
|
+
def global_banner
|
|
412
|
+
<<~BANNER
|
|
223
413
|
sloplint — flag the rhetorical tics and puffery that mark AI-generated prose.
|
|
224
414
|
|
|
225
415
|
# Recommended for agents:
|
|
@@ -228,17 +418,25 @@ module Sloplint
|
|
|
228
418
|
# each note: {path,line,column,severity,confidence,rule,category,message,excerpt,context,rationale,suggestion,count}
|
|
229
419
|
# (count is present only for the rules that tally items)
|
|
230
420
|
|
|
421
|
+
#{judge_recipe}
|
|
231
422
|
usage: sloplint [-o full|json] [command] [args]
|
|
232
423
|
|
|
233
424
|
commands:
|
|
234
425
|
check scan paths (or stdin) for AI-slop tells and report notes [default]
|
|
235
426
|
a first argument that is not a command name is taken as a path
|
|
236
|
-
|
|
237
|
-
|
|
427
|
+
--judge adds sloplint-judge's model-backed rules when that gem is installed
|
|
428
|
+
rules list the rule catalog (add --json for the machine-readable form);
|
|
429
|
+
`sloplint-judge rules` lists the --judge rules
|
|
430
|
+
explain ID print one rule's message, rationale, and a bad/ok example;
|
|
431
|
+
works for any rule id in a note, --judge rules included
|
|
238
432
|
version print the sloplint version
|
|
239
433
|
|
|
240
434
|
global options:
|
|
241
|
-
|
|
435
|
+
BANNER
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
def global_parser(opts, out:)
|
|
439
|
+
parser = OptionParser.new do |o|
|
|
242
440
|
o.on("-o", "--output-format FORMAT", %w[full json],
|
|
243
441
|
"Output format: 'full' (human-readable text) or 'json' (default: full).") do |v|
|
|
244
442
|
opts[:format] = v
|
|
@@ -254,6 +452,8 @@ module Sloplint
|
|
|
254
452
|
o.separator ""
|
|
255
453
|
o.separator "See `sloplint explain <id>` for any rule, or docs/SPEC.md for the JSON contract."
|
|
256
454
|
end
|
|
455
|
+
parser.define_singleton_method(:banner) { CLI.global_banner }
|
|
456
|
+
parser
|
|
257
457
|
end
|
|
258
458
|
end
|
|
259
459
|
end
|
data/lib/sloplint/engine.rb
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
require_relative "rules"
|
|
4
4
|
|
|
5
5
|
module Sloplint
|
|
6
|
+
# The reader sloplint-judge asks its questions for when --register names
|
|
7
|
+
# none. It names no audience, so the model reads the audience off the
|
|
8
|
+
# document, the way calibration matches each register to its own reader;
|
|
9
|
+
# a named default reverses rules on documents written for someone else.
|
|
10
|
+
# It lives here, not in the judge, because `sloplint check --help` quotes
|
|
11
|
+
# it on a machine without the judge gem.
|
|
12
|
+
DEFAULT_REGISTER = "the reader this document is written for"
|
|
13
|
+
|
|
6
14
|
# One match = one Note. See docs/SPEC.md "Note".
|
|
7
15
|
Note = Data.define(
|
|
8
16
|
:path, :line, :column, :severity, :confidence, :rule, :category,
|
|
@@ -59,14 +67,21 @@ module Sloplint
|
|
|
59
67
|
# over blanked text shows code and URLs as a run of spaces. Offsets here are
|
|
60
68
|
# character offsets (MatchData#begin), matching the char-based line_starts_for.
|
|
61
69
|
def context_for(source, match)
|
|
62
|
-
|
|
70
|
+
context_window(source, match.begin(0), match.end(0))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The same window for any span [b, e) of source, so a tool that locates a
|
|
74
|
+
# sentence rather than a regex match (the judge) draws its context the
|
|
75
|
+
# way sloplint does.
|
|
76
|
+
def context_window(source, b, e)
|
|
77
|
+
span = source[b...e]
|
|
78
|
+
return "[#{span.gsub(/\s+/, " ").strip}]" if span.length >= CONTEXT_CHARS
|
|
63
79
|
|
|
64
|
-
b, e = match.begin(0), match.end(0)
|
|
65
80
|
pre = source[[b - CONTEXT_CHARS, 0].max...b]
|
|
66
81
|
post = source[e, CONTEXT_CHARS].to_s
|
|
67
82
|
pre = "…#{pre.sub(/\A\S*\s+/, "")}" if b > CONTEXT_CHARS
|
|
68
83
|
post = "#{post.sub(/\s+\S*\z/, "")}…" if e + CONTEXT_CHARS < source.length
|
|
69
|
-
"#{pre}[#{
|
|
84
|
+
"#{pre}[#{span}]#{post}".gsub(/\s+/, " ").strip
|
|
70
85
|
end
|
|
71
86
|
|
|
72
87
|
# 1-indexed line and column for a char offset into text. Binary-searches a
|
|
@@ -101,8 +116,28 @@ module Sloplint
|
|
|
101
116
|
# with one alternation, so whichever construct opens first is the one that
|
|
102
117
|
# gets consumed: a `<!--` quoted inside backticks is inline code, and a
|
|
103
118
|
# backtick inside a comment is part of the comment.
|
|
119
|
+
# A URL ends before the punctuation that ends the sentence it sits in:
|
|
120
|
+
# "See https://example.com. Then do X." is two sentences, and swallowing
|
|
121
|
+
# the first period would make it one. A URL that really ends in one of
|
|
122
|
+
# these, a Wikipedia link closing on a bracket, loses that character to
|
|
123
|
+
# the sentence instead; the text is only being blanked, so what it costs
|
|
124
|
+
# is one visible character, not a broken link.
|
|
125
|
+
# A fence opens and closes at the start of a line, with three or more
|
|
126
|
+
# backticks; the opener may carry an info string and the closer nothing
|
|
127
|
+
# but whitespace. The line start is the shape that matters: a fence
|
|
128
|
+
# quoted inside a code span, which is how a document explains fences,
|
|
129
|
+
# opened a block in the middle of a sentence and every fence after it in
|
|
130
|
+
# the file paired with the wrong one.
|
|
131
|
+
#
|
|
132
|
+
# Any indent is allowed, because CommonMark measures a fence's indent
|
|
133
|
+
# from its container and a fence under "10. " or a nested bullet stands
|
|
134
|
+
# further in than three spaces. What that costs is an indented code block
|
|
135
|
+
# whose own content has a line of backticks in it, which is rare, and the
|
|
136
|
+
# only thing it costs there is more blanking.
|
|
137
|
+
MARKDOWN_NOISE = /(?<block>^[ \t]*`{3,}[^\n]*\n.*?^[ \t]*`{3,}[ \t]*$|<!--.*?-->)|(?<inline>`[^`\n]*`|https?:\/\/\S*[^\s.,;:!?)\]])/m
|
|
138
|
+
|
|
104
139
|
def blank_markdown(text)
|
|
105
|
-
text.gsub(
|
|
140
|
+
text.gsub(MARKDOWN_NOISE) { |s| s.gsub(/[^\n]/, " ") }
|
|
106
141
|
end
|
|
107
142
|
end
|
|
108
143
|
end
|
data/lib/sloplint/output.rb
CHANGED
|
@@ -11,7 +11,7 @@ module Sloplint
|
|
|
11
11
|
return "" if notes.empty?
|
|
12
12
|
|
|
13
13
|
notes.map do |n|
|
|
14
|
-
head = "#{n.path}:#{n.line}:#{n.column}: #{n.severity} #{n.rule} #{n.message}"
|
|
14
|
+
head = "#{n.path}:#{n.line}:#{n.column}: #{n.severity} (#{n.confidence} confidence) #{n.rule} #{n.message}"
|
|
15
15
|
excerpt = " excerpt: #{n.context}"
|
|
16
16
|
why = " why: #{n.rationale}"
|
|
17
17
|
fix = " fix: #{n.suggestion}"
|
|
@@ -20,13 +20,17 @@ module Sloplint
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# JSON: an array of notes, or an object keyed by path when >1 file was scanned.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
# judge: when the judge ran, its backend name and usage; the notes then
|
|
24
|
+
# sit under "notes" and the usage under "judge", so a caller who asked for
|
|
25
|
+
# the model's opinion also gets what it cost.
|
|
26
|
+
def format_json(notes, by_path: false, judge: nil)
|
|
27
|
+
payload = if by_path
|
|
28
|
+
notes.group_by(&:path).transform_values { |ns| ns.map { |n| note_hash(n) } }
|
|
27
29
|
else
|
|
28
|
-
|
|
30
|
+
notes.map { |n| note_hash(n) }
|
|
29
31
|
end
|
|
32
|
+
payload = { "notes" => payload, "judge" => judge } if judge
|
|
33
|
+
JSON.pretty_generate(payload)
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
def note_hash(note)
|