CyclomaticCompexity: report onle def name

This commit is contained in:
Vitalii Elenhaupt 2019-07-12 23:33:08 +03:00
parent 54706192d2
commit b868d88a85
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
2 changed files with 14 additions and 4 deletions

View file

@ -33,8 +33,8 @@ module Ameba::Rule::Metrics
issue = source.issues.first
issue.rule.should eq subject
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:12:3"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:10"
issue.message.should eq "Cyclomatic complexity too high [8/5]"
end

View file

@ -24,9 +24,19 @@ module Ameba::Rule::Metrics
def test(source, node : Crystal::Def)
complexity = AST::CountingVisitor.new(node).count
if complexity > max_complexity
issue_for(node, MSG % {complexity, max_complexity})
if complexity > max_complexity && (location = node.name_location)
issue_for(
location,
def_name_end_location(node),
MSG % {complexity, max_complexity}
)
end
end
private def def_name_end_location(node)
return unless location = node.name_location
line_number, column_number = location.line_number, location.column_number
Crystal::Location.new(location.filename, line_number, column_number + node.name.size)
end
end
end