Skip to content

Commit 00b63ed

Browse files
author
Will Fleming
committed
Monkey Patch Rubocop to fix complexity report location
This is a horrible no good very bad take at fixing the issue with being unable to see method source in reports for cyclomatic complexity issues, but the alternative is a major pain, so I decided to at least try this first to confirm it really fixes the issue. So, the underlying reason that cyclomatic complexity violations repoorted by RuboCop weren't coming through with the full method def (I think) is because for complexity violations RuboCop reports the `location` of the violation as just the method definition line, not the whole method definition. Getting the full method location is pretty easy if you have the AST node. Unfortunately, RuboCop doesn't actually keep the AST `node` attached to the `Offense` instances: they get dropped when `Cop` instances generate their `Offense` instances. So by the time Rubocop's results come back to our code, there is no AST `node` for us to look at: just a string buffer of file contents & a location pointing at a method name. So this monkey patch basically just overrides the `Offense` creation on complexity cops to pass along the location as the whole method def instead of just the method def line. The alternative, to do it in our own code would, I think be: 1. Parse the string buffer ourself ourself to get an AST 2. Descend into AST to find node corresponding to the location RuboCop gave us 3. Map that node back to a location of the entire method.
1 parent 2813326 commit 00b63ed

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/cc/engine/rubocop.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "json"
22
require "pathname"
3+
require "rubocop/cop/patch_method_complexity"
34
require "rubocop"
45
require "cc/engine/category_parser"
56

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Monkey patching! Here be dragons.
2+
module RuboCop
3+
module Cop
4+
module MethodComplexity
5+
def add_offense(node, loc, message = nil, severity = nil)
6+
super(node, node.loc, message, severity)
7+
end
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)