Skip to content

Commit 590c2b3

Browse files
committed
Monkey patch class and method length cops
Rubocop `ClassLength` and `MethodLength` cops have a function called `check_code_length` that determines the lines to report for the violation. Rubocop passes in `keyword` as the method to be called on `node` which always returns `1` since it's grabbing the defining keyword, eg: `class` or `def`. This monkey patch passes in the node's `loc` property directly to return the correct lines.
1 parent 485cc0c commit 590c2b3

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

lib/cc/engine/rubocop.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require "pathname"
33
require "rubocop"
44
require "rubocop/cop/method_complexity_patch"
5+
require "rubocop/cop/method_length"
6+
require "rubocop/cop/class_length"
57
require "cc/engine/category_parser"
68
require "cc/engine/file_list_resolver"
79
require "cc/engine/violation_decorator"

lib/rubocop/cop/class_length.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RuboCop::Cop::Metrics::ClassLength.module_eval do
2+
def check_code_length(node, *_)
3+
length = code_length(node)
4+
return unless length > max_length
5+
6+
add_offense(node, node.loc, message(length, max_length)) do
7+
self.max = length
8+
end
9+
end
10+
end

lib/rubocop/cop/method_length.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RuboCop::Cop::Metrics::MethodLength.module_eval do
2+
def check_code_length(node, *_)
3+
length = code_length(node)
4+
return unless length > max_length
5+
6+
add_offense(node, node.loc, message(length, max_length)) do
7+
self.max = length
8+
end
9+
end
10+
end

spec/cc/engine/rubocop_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,39 @@ def method
283283
refute includes_check?(output, "Lint/UselessAssignment")
284284
end
285285

286+
it "shows full source of long methods" do
287+
create_source_file("test.rb", <<-EORUBY)
288+
def method
289+
#{"puts 'hi'\n" * 10}
290+
return false
291+
end
292+
EORUBY
293+
output = run_engine
294+
issues = output.split("\0").map { |issue| JSON.parse(issue) }
295+
violation = issues.find do |issue|
296+
issue["check_name"] == "Rubocop/Metrics/MethodLength"
297+
end
298+
299+
assert_equal 1, violation["location"]["positions"]["begin"]["line"]
300+
assert_equal 14, violation["location"]["positions"]["end"]["line"]
301+
end
302+
303+
it "shows full source of long classes" do
304+
create_source_file("test.rb", <<-EORUBY)
305+
class Awesome
306+
#{"foo = 1\n" * 102}
307+
end
308+
EORUBY
309+
output = run_engine
310+
issues = output.split("\0").map { |issue| JSON.parse(issue) }
311+
violation = issues.find do |issue|
312+
issue["check_name"] == "Rubocop/Metrics/ClassLength"
313+
end
314+
315+
assert_equal 1, violation["location"]["positions"]["begin"]["line"]
316+
assert_equal 105, violation["location"]["positions"]["end"]["line"]
317+
end
318+
286319
def includes_check?(output, cop_name)
287320
issues(output).any? { |i| i["check_name"] =~ /#{cop_name}$/ }
288321
end

0 commit comments

Comments
 (0)