judgelint 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53450ae14a634168662b5a717c4858c80688c39eb7dc497352fa7760bdcca165
4
- data.tar.gz: 05c4a391332c478ab270c30f47d2b5765115b37cd9e378785a1fd61ad06fab04
3
+ metadata.gz: '0901cbb02b6850e8d11760748b1ae734629b06d12ad019af3eab4d255f689761'
4
+ data.tar.gz: 468268468e35d1e9f9274281a3184de02ccfff98080c99c47b0ec6349f9a5762
5
5
  SHA512:
6
- metadata.gz: a7e38d374feea8a3e2df909ffda5ad9ad15ea0e28cea55ad5dde51610d37845b539b79a3b1f2a601b9db9f23182bdcca8480e1fa01c6bd59878d5ce29223f766
7
- data.tar.gz: ddfca2bbb69585e5c21f8b4dddd97aa1bbbc72cefa7bfb0e5d37c962980d96d28bd140eed98779f6e5607b7c07226e8d429e01b9870c71ab22eef3010e4a8c06
6
+ metadata.gz: 85868cd486ebdaf2663607f2539c58541b59150da14eab77ee23fc950878aa9c466eef90cab29ea3241d520d74bebb88803c99b4b31dd0ad979be65187b9da9d
7
+ data.tar.gz: d44aaaf54c4be5f05388d635b9472fee970527dd09aa47ef3ffb429dadd32cdbaa820e1dee9198e53a33df353c151dfcd690d6da91ec79cb6e0a6852e0365611
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.6 (2026-10-26)
4
+
5
+ - `--base` and `--pr` ask about the changed lines, not the 40-line windows
6
+ around them. A finding the model places on a line the change didn't touch
7
+ doesn't count, so editing an old file can't fail it for code the edit
8
+ left alone.
9
+
3
10
  ## 0.1.5 (2026-10-26)
4
11
 
5
12
  - The text report is in color in a terminal: errors in red, warnings and
data/README.md CHANGED
@@ -227,8 +227,11 @@ judgelint --pr # changes in the current pull request
227
227
  judgelint --pr --format github # GitHub annotations on the changed lines (json and sarif work too)
228
228
  ```
229
229
 
230
- These only ask about the 40-line windows that changed, so existing code
231
- doesn't block a new pull request. `--pr` gets the base branch from GitHub
230
+ These only ask about the lines that changed, with the rest of the file as
231
+ context, so existing code doesn't block a new pull request: a finding the
232
+ model places on a line the change didn't touch doesn't count. A new file is
233
+ all changed lines. Rules about a whole file (`scope: file`) still read the
234
+ whole file when any of it changed. `--pr` gets the base branch from GitHub
232
235
  Actions, GitLab, Buildkite, Bitbucket, Azure Pipelines,
233
236
  `JUDGELINT_BASE_BRANCH`, or `gh`. Fetch enough history for git to find the
234
237
  merge base.
@@ -2,7 +2,9 @@
2
2
 
3
3
  module Judgelint
4
4
  # One rule asked about one range of a file. The model sees the whole file;
5
- # the question points at the lines.
5
+ # the question points at the lines. When a change touched only some of a
6
+ # window's lines, it points at those, so code the change didn't touch can't
7
+ # fail it.
6
8
  #
7
9
  # A spec's bullet is a choice between breaking the rule, following it, and
8
10
  # the rule not applying. With the three side by side, a decision model
@@ -19,16 +21,18 @@ module Judgelint
19
21
  EXCUSED = "A judgehint comment is about this rule, and gives a reason the code may break it."
20
22
  NOT_EXCUSED = "No judgehint comment is about this rule. Any judgehint is about a different rule, or gives no reason."
21
23
 
22
- attr_reader :rule, :first, :last, :places
24
+ attr_reader :rule, :first, :last, :places, :lines
23
25
 
24
26
  # +kind+ is :verdict, :where, or :excuse. +places+ are a :where question's
25
27
  # options: line ids (L12) or the directory's files, as the model sees them.
26
- def initialize(rule, first, last, kind: :verdict, places: [])
28
+ # +lines+ are the changed lines in the range, or nil to ask about all of it.
29
+ def initialize(rule, first, last, kind: :verdict, places: [], lines: nil)
27
30
  @rule = rule
28
31
  @first = first
29
32
  @last = last
30
33
  @kind = kind
31
34
  @places = places
35
+ @lines = lines
32
36
  end
33
37
 
34
38
  def excuse?
@@ -72,9 +76,9 @@ module Judgelint
72
76
  def instructions
73
77
  return excuse_instructions if excuse?
74
78
  return where_instructions if where?
75
- return "How #{rule.scope == 'file' ? 'does' : 'do'} #{subject} relate to the following team rule? Rule: #{rule.convention}" if rule.convention
79
+ return "How #{rule.scope == 'file' || lines&.size == 1 ? 'does' : 'do'} #{subject} relate to the following team rule? Rule: #{rule.convention}" if rule.convention
76
80
 
77
- text = rule.window? ? "Look at lines #{first}-#{last} of the file; the rest is context. #{rule.question}" : rule.question
81
+ text = rule.window? ? "Look at #{span} of the file; the rest is context. #{rule.question}" : rule.question
78
82
  return text unless rule.fail_when == "yes"
79
83
 
80
84
  scope = { "window" => "those lines do it", "file" => "the code does it", "directory" => "the files do it" }
@@ -104,8 +108,24 @@ module Judgelint
104
108
  private
105
109
 
106
110
  def subject
107
- { "window" => "lines #{first}-#{last} of the file", "file" => "the file", "directory" => "the files" }
108
- .fetch(rule.scope)
111
+ { "window" => "#{span} of the file", "file" => "the file", "directory" => "the files" }.fetch(rule.scope)
112
+ end
113
+
114
+ # The lines asked about: "lines 41-80", or the changed ones, as "line 7" or
115
+ # "lines 3, 7-9".
116
+ def span
117
+ return "lines #{first}-#{last}" if lines.nil?
118
+ return "line #{lines.first}" if lines.size == 1
119
+
120
+ runs = []
121
+ lines.each do |line|
122
+ if !runs.empty? && runs.last[1] == line - 1
123
+ runs.last[1] = line
124
+ else
125
+ runs << [line, line]
126
+ end
127
+ end
128
+ "lines #{runs.map { |run| run[0] == run[1] ? run[0].to_s : "#{run[0]}-#{run[1]}" }.join(', ')}"
109
129
  end
110
130
 
111
131
  def where_instructions
@@ -72,6 +72,8 @@ module Judgelint
72
72
 
73
73
  # +changes+ ({path => lines or :all}, from Changes.since) limits each file
74
74
  # to the windows a change touched, and directories to those with a change.
75
+ # A window rule is asked about the changed lines alone, with the rest of
76
+ # the file as context.
75
77
  # Rules that read different context files can't share a request, so a
76
78
  # file gets one job per set of context files.
77
79
  def plan(paths, directories: [], changes: nil)
@@ -182,7 +184,7 @@ module Judgelint
182
184
 
183
185
  asked = []
184
186
  ranges.each do |first, last|
185
- asked << Question.new(rule, first, last)
187
+ asked << Question.new(rule, first, last, lines: changed_lines(rule, lines, first, last))
186
188
  places = places(source, rule, first, last)
187
189
  asked << Question.new(rule, first, last, kind: :where, places: places) unless places.empty?
188
190
  asked << Question.new(rule, first, last, kind: :excuse) if source.hinted?(first, last)
@@ -209,6 +211,14 @@ module Judgelint
209
211
  rules.group_by { |rule| [rule.context, rule.prose] }.map { |(globs, _prose), group| yield(Glob.expand(globs), group) }
210
212
  end
211
213
 
214
+ # The lines a change touched in a window rule's range, or nil for all of them.
215
+ def changed_lines(rule, lines, first, last)
216
+ return nil if lines == :all || !rule.window?
217
+
218
+ changed = lines.select { |line| line.between?(first, last) }.uniq.sort
219
+ changed.size == last - first + 1 ? nil : changed
220
+ end
221
+
212
222
  def touched?(lines, first, last)
213
223
  lines == :all || (lines || []).any? { |line| line.between?(first, last) }
214
224
  end
@@ -276,9 +286,11 @@ module Judgelint
276
286
 
277
287
  # A window's probability of a violation is the average of its verdict
278
288
  # over the models, and its place is the line or file the models' "where"
279
- # answers agree on. A rule's verdict is its worst window. A window whose
280
- # judgehint excuses it, by the same average and threshold, is reported as
281
- # excused instead, unless another window breaks the rule.
289
+ # answers agree on. When a change touched only some of its lines, the
290
+ # probability counts only the share of "where" answers on those lines. A
291
+ # rule's verdict is its worst window. A window whose judgehint excuses it,
292
+ # by the same average and threshold, is reported as excused instead,
293
+ # unless another window breaks the rule.
282
294
  def verdict_for(rule, path, questions, job)
283
295
  return Verdict.new(rule: rule, path: path, probability: nil, status: :skipped) if job.skipped
284
296
 
@@ -304,7 +316,10 @@ module Judgelint
304
316
  end
305
317
  next if values.empty?
306
318
 
307
- window = Window.new(first, last, values.sum / values.size, agreed(spread))
319
+ probability = values.sum / values.size
320
+ changed = asked.first.lines # the verdict question comes first
321
+ probability *= changed_share(spread, changed) if changed
322
+ window = Window.new(first, last, probability, agreed(spread))
308
323
  if !excuses.empty? && excuses.sum / excuses.size >= rule.threshold
309
324
  excused << window
310
325
  else
@@ -323,6 +338,18 @@ module Judgelint
323
338
  line: hidden.line, file: hidden.file)
324
339
  end
325
340
 
341
+ # The share of the summed "where" probabilities on +lines+, or all of it
342
+ # when nothing answered where.
343
+ def changed_share(spread, lines)
344
+ total = 0.0
345
+ changed = 0.0
346
+ spread.each do |place, value|
347
+ total += value
348
+ changed += value if lines.include?(place.delete_prefix("L").to_i)
349
+ end
350
+ total.positive? ? changed / total : 1.0
351
+ end
352
+
326
353
  # The place with at least PLACE_AT of the summed "where" probabilities.
327
354
  # A loop rather than max_by, which Spinel's Hash doesn't have yet.
328
355
  def agreed(spread)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Judgelint
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
5
5
  end
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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Camara