judgelint 0.1.9 → 0.2.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 +33 -0
- data/README.md +9 -11
- data/exe/judgelint +9 -9
- metadata +3 -25
- data/lib/judgelint/cache.rb +0 -53
- data/lib/judgelint/changes.rb +0 -87
- data/lib/judgelint/cli.rb +0 -251
- data/lib/judgelint/client.rb +0 -186
- data/lib/judgelint/config.rb +0 -162
- data/lib/judgelint/errors.rb +0 -14
- data/lib/judgelint/glob.rb +0 -74
- data/lib/judgelint/paint.rb +0 -46
- data/lib/judgelint/pool.rb +0 -41
- data/lib/judgelint/protocol.rb +0 -122
- data/lib/judgelint/providers.rb +0 -78
- data/lib/judgelint/question.rb +0 -148
- data/lib/judgelint/reporter.rb +0 -188
- data/lib/judgelint/rule.rb +0 -167
- data/lib/judgelint/runner.rb +0 -400
- data/lib/judgelint/scaffold.rb +0 -66
- data/lib/judgelint/source.rb +0 -82
- data/lib/judgelint/spec.rb +0 -240
- data/lib/judgelint/usage.rb +0 -53
- data/lib/judgelint/verdict.rb +0 -65
- data/lib/judgelint/version.rb +0 -5
- data/lib/judgelint.rb +0 -61
data/lib/judgelint/runner.rb
DELETED
|
@@ -1,400 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# Judges files against the rules that govern them, and rules against their
|
|
5
|
-
# examples, in two steps: plan every request first, so a run can be priced
|
|
6
|
-
# and refused before anything is sent, then send them.
|
|
7
|
-
#
|
|
8
|
-
# Each file's requests carry the whole numbered file as state and one
|
|
9
|
-
# question per rule and window, so a file's verdicts never depend on which
|
|
10
|
-
# other files ran alongside it. Every model is asked every question, all of a
|
|
11
|
-
# file's requests go out at once, and a question's answer is the models'
|
|
12
|
-
# average. A rule's verdict is its worst window.
|
|
13
|
-
class Runner
|
|
14
|
-
# One file's work: the questions to ask, each model's answers (from the
|
|
15
|
-
# cache or the model), and the requests still to send.
|
|
16
|
-
class Job
|
|
17
|
-
attr_reader :source, :state_digest, :questions, :answers, :requests, :expected
|
|
18
|
-
attr_accessor :skipped
|
|
19
|
-
|
|
20
|
-
# The state is the whole file, and a run plans every file before it
|
|
21
|
-
# sends one, so it's built when needed rather than kept. Its hash, which
|
|
22
|
-
# every question's cache key needs, is kept.
|
|
23
|
-
def initialize(source, questions, prose: nil, expected: nil)
|
|
24
|
-
@source = source
|
|
25
|
-
@prose = prose
|
|
26
|
-
@state_digest = Digest::SHA256.hexdigest(JSON.generate(state))
|
|
27
|
-
@questions = questions
|
|
28
|
-
@answers = {} # model index => {question key => probability or nil}
|
|
29
|
-
@requests = []
|
|
30
|
-
@expected = expected
|
|
31
|
-
@skipped = false
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# Rules from a spec read its prose alongside the file, as team_rules.
|
|
35
|
-
def state
|
|
36
|
-
@prose ? { "team_rules" => @prose, "file" => @source.state } : @source.state
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Questions sent together to one model, and their estimated input tokens.
|
|
41
|
-
class Request
|
|
42
|
-
attr_reader :model, :questions, :tokens
|
|
43
|
-
|
|
44
|
-
def initialize(model, questions, tokens)
|
|
45
|
-
@model = model
|
|
46
|
-
@questions = questions
|
|
47
|
-
@tokens = tokens
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# One range's averaged answers: the probability it breaks the rule, and
|
|
52
|
-
# where, when the model is sure enough of one line or file.
|
|
53
|
-
class Window
|
|
54
|
-
attr_reader :first, :last, :probability, :place
|
|
55
|
-
|
|
56
|
-
def initialize(first, last, probability, place)
|
|
57
|
-
@first = first
|
|
58
|
-
@last = last
|
|
59
|
-
@probability = probability
|
|
60
|
-
@place = place
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
# A finding names a line or file when it gets at least this share of the
|
|
65
|
-
# "where" answer.
|
|
66
|
-
PLACE_AT = 0.5
|
|
67
|
-
# A choice takes at most this many options.
|
|
68
|
-
MAX_OPTIONS = 255
|
|
69
|
-
|
|
70
|
-
# +clients+ has one Client per model in config.models.
|
|
71
|
-
def initialize(config:, rules:, clients:, cache:)
|
|
72
|
-
@config = config
|
|
73
|
-
@rules = rules
|
|
74
|
-
@clients = clients
|
|
75
|
-
@cache = cache
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
# +changes+ ({path => lines or :all}, from Changes.since) limits each file
|
|
79
|
-
# to the windows a change touched, and directories to those with a change.
|
|
80
|
-
# A window rule is asked about the changed lines alone, with the rest of
|
|
81
|
-
# the file as context.
|
|
82
|
-
# Rules that read different context files can't share a request, so a
|
|
83
|
-
# file gets one job per set of context files.
|
|
84
|
-
def plan(paths, directories: [], changes: nil)
|
|
85
|
-
files = paths.flat_map do |path|
|
|
86
|
-
rules = @rules.select { |rule| !rule.directory? && rule.applies_to?(path) }
|
|
87
|
-
by_context(rules) { |context, group| plan_file(Source.new(path, context: context), group, changes ? changes[path] : :all) }
|
|
88
|
-
end
|
|
89
|
-
units = directories.flat_map do |dir|
|
|
90
|
-
next [] if changes && changes.keys.none? { |path| path.start_with?("#{dir}/") }
|
|
91
|
-
|
|
92
|
-
rules = @rules.select { |rule| rule.directory? && rule.applies_to?(dir) }
|
|
93
|
-
by_context(rules) { |context, group| plan_file(Source.directory(dir, context: context), group, :all) }
|
|
94
|
-
end
|
|
95
|
-
files + units
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
attr_reader :untested
|
|
99
|
-
|
|
100
|
-
# Files under <examples>/pass and Good blocks under a spec's bullet must
|
|
101
|
-
# not be judged violations; files under <examples>/fail and Bad blocks
|
|
102
|
-
# must. The model sees each file's path relative to its pass/ or fail/
|
|
103
|
-
# directory, and a block as example.<language>, so neither gives the
|
|
104
|
-
# answer away. A rule with examples needs both kinds, checked before
|
|
105
|
-
# anything is sent; rules with none are listed in +untested+.
|
|
106
|
-
def plan_examples
|
|
107
|
-
jobs = []
|
|
108
|
-
missing = []
|
|
109
|
-
@untested = []
|
|
110
|
-
@rules.each do |rule|
|
|
111
|
-
found = %i[pass fail].to_h { |expected| [expected, example_paths(rule, expected)] }
|
|
112
|
-
inline = rule.directory? ? [] : rule.inline_examples
|
|
113
|
-
if found.values.all?(&:empty?) && inline.empty?
|
|
114
|
-
@untested << rule
|
|
115
|
-
next
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
found.each do |expected, paths|
|
|
119
|
-
dir = File.join(rule.examples_dir, expected.to_s)
|
|
120
|
-
if paths.empty? && inline.none? { |example| example.expected == expected }
|
|
121
|
-
missing << (inline.empty? ? "#{dir}/" : "a #{expected == :pass ? 'Good' : 'Bad'} block under #{rule.id}")
|
|
122
|
-
end
|
|
123
|
-
paths.each do |path|
|
|
124
|
-
shown = path.delete_prefix("#{dir}/")
|
|
125
|
-
context = rule.context_files
|
|
126
|
-
source = rule.directory? ? Source.directory(path, shown_as: shown, context: context)
|
|
127
|
-
: Source.new(path, shown_as: shown, context: context)
|
|
128
|
-
jobs << plan_file(source, [rule], :all, expected: expected)
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
inline.each do |example|
|
|
132
|
-
source = Source.new("#{rule.source}:#{example.line}", shown_as: example.name, text: example.code,
|
|
133
|
-
context: rule.context_files)
|
|
134
|
-
jobs << plan_file(source, [rule], :all, expected: example.expected)
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
raise ConfigError, "Examples need both kinds: add #{missing.join(', ')}" unless missing.empty?
|
|
138
|
-
|
|
139
|
-
jobs
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# Every model the jobs will ask needs its key.
|
|
143
|
-
def check_keys(jobs)
|
|
144
|
-
jobs.flat_map { |job| job.requests.map(&:model) }.uniq.each { |model| @clients[model].check_key }
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
def request_count(jobs)
|
|
148
|
-
jobs.sum { |job| job.requests.size }
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
def estimated_tokens(jobs)
|
|
152
|
-
jobs.sum { |job| job.requests.sum(&:tokens) }
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
# Nil when a model has no price.
|
|
156
|
-
def estimated_cost(jobs)
|
|
157
|
-
requests = jobs.flat_map(&:requests)
|
|
158
|
-
prices = requests.map { |request| @clients[request.model].endpoint.price_per_million }
|
|
159
|
-
return nil if prices.include?(nil)
|
|
160
|
-
|
|
161
|
-
requests.each_with_index.sum { |request, index| request.tokens * prices[index] / 1_000_000.0 }
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
# Returns each job's verdicts, in the jobs' order.
|
|
165
|
-
def execute(jobs)
|
|
166
|
-
Pool.map(jobs, Config::THREADS) do |job|
|
|
167
|
-
ask(job)
|
|
168
|
-
verdicts(job)
|
|
169
|
-
end
|
|
170
|
-
ensure
|
|
171
|
-
@cache.save
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
private
|
|
175
|
-
|
|
176
|
-
# A directory rule's examples are the directories under pass/ and fail/.
|
|
177
|
-
def example_paths(rule, expected)
|
|
178
|
-
dir = File.join(rule.examples_dir, expected.to_s)
|
|
179
|
-
return Dir.glob(File.join(dir, "*")).select { |path| File.directory?(path) }.sort if rule.directory?
|
|
180
|
-
|
|
181
|
-
Glob.expand([File.join(dir, "**", "*")])
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
def plan_file(source, rules, lines, expected: nil)
|
|
185
|
-
questions = {}
|
|
186
|
-
rules.each do |rule|
|
|
187
|
-
ranges = rule.ranges(source, Config::WINDOW_LINES).select { |first, last| touched?(lines, first, last) }
|
|
188
|
-
next if ranges.empty?
|
|
189
|
-
|
|
190
|
-
asked = []
|
|
191
|
-
ranges.each do |first, last|
|
|
192
|
-
asked << Question.new(rule, first, last, lines: changed_lines(rule, lines, first, last))
|
|
193
|
-
asked << where(source, rule, first, last) if where?(source, rule, first, last)
|
|
194
|
-
asked << Question.new(rule, first, last, kind: :excuse) if source.hinted?(first, last)
|
|
195
|
-
end
|
|
196
|
-
questions[rule] = asked
|
|
197
|
-
end
|
|
198
|
-
job = Job.new(source, questions, prose: rules.first&.prose, expected: expected)
|
|
199
|
-
pack(job)
|
|
200
|
-
job
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
# Where a bullet is broken is asked over the range's lines or the
|
|
204
|
-
# directory's files, but not for a rule written as a question, or past
|
|
205
|
-
# what a choice can hold.
|
|
206
|
-
def where?(source, rule, first, last)
|
|
207
|
-
rule.convention && (rule.directory? ? source.members.size : last - first + 1) <= MAX_OPTIONS
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
# A file's line ids are made when the question is sent, not kept.
|
|
211
|
-
def where(source, rule, first, last)
|
|
212
|
-
places = rule.directory? ? source.members.map { |_path, shown| shown } : nil
|
|
213
|
-
Question.new(rule, first, last, kind: :where, places: places)
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
# Rules that read different context files or spec prose can't share a request.
|
|
217
|
-
def by_context(rules)
|
|
218
|
-
rules.group_by { |rule| [rule.context, rule.prose] }.map { |(globs, _prose), group| yield(Glob.expand(globs), group) }
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
# The lines a change touched in a window rule's range, or nil for all of them.
|
|
222
|
-
def changed_lines(rule, lines, first, last)
|
|
223
|
-
return nil if lines == :all || !rule.window?
|
|
224
|
-
|
|
225
|
-
changed = lines.select { |line| line.between?(first, last) }.uniq.sort
|
|
226
|
-
changed.size == last - first + 1 ? nil : changed
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
def touched?(lines, first, last)
|
|
230
|
-
lines == :all || (lines || []).any? { |line| line.between?(first, last) }
|
|
231
|
-
end
|
|
232
|
-
|
|
233
|
-
# For each model, packs the questions its cache can't answer into as few
|
|
234
|
-
# requests as fit its limits. A file whose state and widest question are
|
|
235
|
-
# over a model's limit is skipped: it's reported, never sent.
|
|
236
|
-
def pack(job)
|
|
237
|
-
@clients.each_with_index do |client, model|
|
|
238
|
-
answers = job.answers[model] = {}
|
|
239
|
-
pending = []
|
|
240
|
-
job.questions.each_value do |questions|
|
|
241
|
-
questions.each do |question|
|
|
242
|
-
cached = @cache.fetch(cache_key(question, job, client))
|
|
243
|
-
cached.nil? ? pending << question : answers[question.key] = cached
|
|
244
|
-
end
|
|
245
|
-
end
|
|
246
|
-
next if pending.empty?
|
|
247
|
-
|
|
248
|
-
limits = client.endpoint
|
|
249
|
-
base = client.tokens(job.state, [])
|
|
250
|
-
sizes = pending.map { |question| client.question_tokens(question) }
|
|
251
|
-
if base + sizes.max >= limits.max_state_tokens
|
|
252
|
-
job.skipped = true
|
|
253
|
-
job.requests.clear
|
|
254
|
-
return
|
|
255
|
-
end
|
|
256
|
-
|
|
257
|
-
batch = []
|
|
258
|
-
tokens = base
|
|
259
|
-
pending.each_with_index do |question, index|
|
|
260
|
-
if !batch.empty? && (tokens + sizes[index] > limits.max_request_tokens || batch.size >= limits.batch_size)
|
|
261
|
-
job.requests << Request.new(model, batch, tokens)
|
|
262
|
-
batch = []
|
|
263
|
-
tokens = base
|
|
264
|
-
end
|
|
265
|
-
batch << question
|
|
266
|
-
tokens += sizes[index]
|
|
267
|
-
end
|
|
268
|
-
job.requests << Request.new(model, batch, tokens)
|
|
269
|
-
end
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
# Sends every request at once: a file's packed requests and every model's.
|
|
273
|
-
def ask(job)
|
|
274
|
-
return if job.requests.empty?
|
|
275
|
-
|
|
276
|
-
state = job.state
|
|
277
|
-
results = Pool.map(job.requests, job.requests.size) do |request|
|
|
278
|
-
@clients[request.model].judge(state, request.questions)
|
|
279
|
-
end
|
|
280
|
-
job.requests.each_with_index do |request, index|
|
|
281
|
-
client = @clients[request.model]
|
|
282
|
-
request.questions.each do |question|
|
|
283
|
-
probability = results[index][question.key]
|
|
284
|
-
@cache.store(cache_key(question, job, client), probability) unless probability.nil?
|
|
285
|
-
job.answers[request.model][question.key] = probability
|
|
286
|
-
end
|
|
287
|
-
end
|
|
288
|
-
end
|
|
289
|
-
|
|
290
|
-
def verdicts(job)
|
|
291
|
-
path = job.source.path
|
|
292
|
-
found = job.questions.map { |rule, questions| verdict_for(rule, path, questions, job) }
|
|
293
|
-
job.expected ? found.map { |verdict| verdict.as_example(job.expected) } : found
|
|
294
|
-
end
|
|
295
|
-
|
|
296
|
-
# A window's probability of a violation is the average of its verdict
|
|
297
|
-
# over the models, and its place is the line or file the models' "where"
|
|
298
|
-
# answers agree on. When a change touched only some of its lines, the
|
|
299
|
-
# probability counts only the share of "where" answers on those lines. A
|
|
300
|
-
# rule's verdict is its worst window. A window whose judgehint excuses it,
|
|
301
|
-
# by the same average and threshold, is reported as excused instead,
|
|
302
|
-
# unless another window breaks the rule.
|
|
303
|
-
def verdict_for(rule, path, questions, job)
|
|
304
|
-
return Verdict.new(rule: rule, path: path, probability: nil, status: :skipped) if job.skipped
|
|
305
|
-
|
|
306
|
-
open = []
|
|
307
|
-
excused = []
|
|
308
|
-
questions.group_by { |question| [question.first, question.last] }.each do |(first, last), asked|
|
|
309
|
-
values = []
|
|
310
|
-
excuses = []
|
|
311
|
-
spread = {}
|
|
312
|
-
asked.each do |question|
|
|
313
|
-
job.answers.each_value do |answers|
|
|
314
|
-
answer = answers[question.key]
|
|
315
|
-
next if answer.nil?
|
|
316
|
-
|
|
317
|
-
if question.excuse?
|
|
318
|
-
excuses << answer
|
|
319
|
-
elsif question.where?
|
|
320
|
-
answer.each { |place, probability| spread[place] = (spread[place] || 0.0) + probability }
|
|
321
|
-
else
|
|
322
|
-
values << question.violation(answer)
|
|
323
|
-
end
|
|
324
|
-
end
|
|
325
|
-
end
|
|
326
|
-
next if values.empty?
|
|
327
|
-
|
|
328
|
-
probability = values.sum / values.size
|
|
329
|
-
changed = asked.first.lines # the verdict question comes first
|
|
330
|
-
probability *= changed_share(spread, changed) if changed
|
|
331
|
-
window = Window.new(first, last, probability, agreed(spread))
|
|
332
|
-
if !excuses.empty? && excuses.sum / excuses.size >= rule.threshold
|
|
333
|
-
excused << window
|
|
334
|
-
else
|
|
335
|
-
open << window
|
|
336
|
-
end
|
|
337
|
-
end
|
|
338
|
-
return Verdict.new(rule: rule, path: path, probability: nil, status: :refused) if open.empty? && excused.empty?
|
|
339
|
-
|
|
340
|
-
found = worst(rule, path, open, job.source)
|
|
341
|
-
return found if found && found.status != :pass
|
|
342
|
-
|
|
343
|
-
hidden = worst(rule, path, excused, job.source)
|
|
344
|
-
return found || hidden if hidden.nil? || hidden.status == :pass
|
|
345
|
-
|
|
346
|
-
Verdict.new(rule: rule, path: path, probability: hidden.probability, status: :excused, lines: hidden.lines,
|
|
347
|
-
line: hidden.line, file: hidden.file)
|
|
348
|
-
end
|
|
349
|
-
|
|
350
|
-
# The share of the summed "where" probabilities on +lines+, or all of it
|
|
351
|
-
# when nothing answered where.
|
|
352
|
-
def changed_share(spread, lines)
|
|
353
|
-
total = 0.0
|
|
354
|
-
changed = 0.0
|
|
355
|
-
spread.each do |place, value|
|
|
356
|
-
total += value
|
|
357
|
-
changed += value if lines.include?(place.delete_prefix("L").to_i)
|
|
358
|
-
end
|
|
359
|
-
total.positive? ? changed / total : 1.0
|
|
360
|
-
end
|
|
361
|
-
|
|
362
|
-
# The place with at least PLACE_AT of the summed "where" probabilities.
|
|
363
|
-
# A loop rather than max_by, which Spinel's Hash doesn't have yet.
|
|
364
|
-
def agreed(spread)
|
|
365
|
-
total = 0.0
|
|
366
|
-
best = nil
|
|
367
|
-
spread.each do |place, value|
|
|
368
|
-
total += value
|
|
369
|
-
best = place if best.nil? || value > spread[best]
|
|
370
|
-
end
|
|
371
|
-
return nil if total <= 0
|
|
372
|
-
|
|
373
|
-
spread[best] / total >= PLACE_AT ? best : nil
|
|
374
|
-
end
|
|
375
|
-
|
|
376
|
-
# The worst of +windows+, or nil if there are none.
|
|
377
|
-
def worst(rule, path, windows, source)
|
|
378
|
-
return nil if windows.empty?
|
|
379
|
-
|
|
380
|
-
window = windows.max_by(&:probability)
|
|
381
|
-
line = nil
|
|
382
|
-
file = nil
|
|
383
|
-
if rule.directory?
|
|
384
|
-
member = source.members.find { |_member, shown| shown == window.place }
|
|
385
|
-
file = member[0] if member
|
|
386
|
-
elsif window.place
|
|
387
|
-
line = window.place.delete_prefix("L").to_i
|
|
388
|
-
end
|
|
389
|
-
lines = rule.directory? ? nil : [window.first, window.last]
|
|
390
|
-
Verdict.classify(rule, path, window.probability, warn_at: @config.warn_at, lines: lines, line: line, file: file)
|
|
391
|
-
end
|
|
392
|
-
|
|
393
|
-
# Each model's answers are cached on their own, so adding a model asks
|
|
394
|
-
# only the new one.
|
|
395
|
-
def cache_key(question, job, client)
|
|
396
|
-
endpoint = client.endpoint
|
|
397
|
-
Cache.key(question.digest, job.state_digest, "#{endpoint.provider}/#{endpoint.model}")
|
|
398
|
-
end
|
|
399
|
-
end
|
|
400
|
-
end
|
data/lib/judgelint/scaffold.rb
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# The file `judgelint init` writes.
|
|
5
|
-
module Scaffold
|
|
6
|
-
# A spec: each bullet is a convention judgelint checks, and the Bad and
|
|
7
|
-
# Good blocks under it are examples for `judgelint test`.
|
|
8
|
-
SPEC = <<~MARKDOWN
|
|
9
|
-
---
|
|
10
|
-
paths: ["**/*.rb"]
|
|
11
|
-
excludes: ["test/**", "spec/**"]
|
|
12
|
-
---
|
|
13
|
-
# Ruby conventions
|
|
14
|
-
|
|
15
|
-
- **No silent rescues.** A rescue logs the error, re-raises it, or turns it
|
|
16
|
-
into a result the caller sees. Swallowed exceptions hide bugs until they
|
|
17
|
-
cost us data.
|
|
18
|
-
|
|
19
|
-
Bad:
|
|
20
|
-
|
|
21
|
-
```ruby
|
|
22
|
-
def fetch_profile(id)
|
|
23
|
-
client.profile(id)
|
|
24
|
-
rescue StandardError
|
|
25
|
-
nil
|
|
26
|
-
end
|
|
27
|
-
```
|
|
28
|
-
|
|
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
|
|
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}"
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
end
|
data/lib/judgelint/source.rb
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# What the model is shown: one file, or every file in a directory, with each
|
|
5
|
-
# line numbered so a question can point at a range, followed by any context
|
|
6
|
-
# files the rule names, which are there to read and never judged.
|
|
7
|
-
#
|
|
8
|
-
# A judgehint is a comment (#, //, --, /*, <!--, or ;) starting with
|
|
9
|
-
# `judgehint:` that says why the code is the way it is. The model weighs it
|
|
10
|
-
# for the window it falls in, or for the whole directory.
|
|
11
|
-
class Source
|
|
12
|
-
HINT = %r{(?:#|//|--|/\*|<!--|;)\s*judgehint:}
|
|
13
|
-
|
|
14
|
-
attr_reader :path, :shown_as, :line_count
|
|
15
|
-
|
|
16
|
-
def self.directory(dir, shown_as: dir, context: [])
|
|
17
|
-
files = Glob.expand([File.join(dir, "**", "*")])
|
|
18
|
-
new(dir, shown_as: shown_as, files: files, context: context, directory: true)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# +text+ is the file's contents when it isn't on disk, like an example in a spec.
|
|
22
|
-
# Each file is kept as one string, since a run holds every file it plans.
|
|
23
|
-
def initialize(path, shown_as: path, files: [path], context: [], directory: false, text: nil)
|
|
24
|
-
@path = path
|
|
25
|
-
@shown_as = shown_as
|
|
26
|
-
@directory = directory
|
|
27
|
-
@files = text ? [[path, text]] : files.map { |file| [file, File.read(file)] }
|
|
28
|
-
@context = context
|
|
29
|
-
@line_count = 0
|
|
30
|
-
@hints = []
|
|
31
|
-
@files.each do |_file, contents|
|
|
32
|
-
contents.each_line do |line|
|
|
33
|
-
@line_count += 1
|
|
34
|
-
@hints << @line_count if HINT.match?(line)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def directory?
|
|
40
|
-
@directory
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def state
|
|
44
|
-
body =
|
|
45
|
-
if directory?
|
|
46
|
-
sections = @files.map { |file, contents| "Path: #{shown(file)}\n\n#{numbered(contents)}" }
|
|
47
|
-
"Directory: #{shown_as}\n\n#{sections.join("\n")}"
|
|
48
|
-
else
|
|
49
|
-
"Path: #{shown_as}\n\n#{numbered(@files.first[1])}"
|
|
50
|
-
end
|
|
51
|
-
return body if @context.empty?
|
|
52
|
-
|
|
53
|
-
references = @context.map { |file| "Path: #{file}\n\n#{File.read(file)}" }
|
|
54
|
-
"#{body}\nContext, for reference only (not under review):\n\n#{references.join("\n")}"
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# [first, last] ranges of +size+ lines that cover the file.
|
|
58
|
-
def windows(size)
|
|
59
|
-
(1..line_count).step(size).map { |first| [first, [first + size - 1, line_count].min] }
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# The directory's files as [path, the path the model sees].
|
|
63
|
-
def members
|
|
64
|
-
@files.map { |file, _contents| [file, shown(file)] }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def hinted?(first, last)
|
|
68
|
-
directory? ? !@hints.empty? : @hints.any? { |line| line.between?(first, last) }
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
private
|
|
72
|
-
|
|
73
|
-
def numbered(contents)
|
|
74
|
-
contents.lines.each_with_index.map { |line, index| "#{index + 1}: #{line.chomp}\n" }.join
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# A file's path inside the directory, as the model sees it.
|
|
78
|
-
def shown(file)
|
|
79
|
-
"#{shown_as}/#{file.delete_prefix("#{path}/")}"
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
end
|