diff --git a/spec/ameba/rule/style/method_names_spec.cr b/spec/ameba/rule/style/method_names_spec.cr index 008806bfe..4f828b583 100644 --- a/spec/ameba/rule/style/method_names_spec.cr +++ b/spec/ameba/rule/style/method_names_spec.cr @@ -40,14 +40,14 @@ module Ameba it "reports rule, pos and message" do s = Source.new %( - def bad_Name + def bad_Name(a) end ), "source.cr" subject.catch(s).should_not be_valid issue = s.issues.first issue.rule.should_not be_nil - issue.location.to_s.should eq "source.cr:1:1" - issue.end_location.to_s.should eq "source.cr:2:3" + issue.location.to_s.should eq "source.cr:1:5" + issue.end_location.to_s.should eq "source.cr:1:12" issue.message.should eq( "Method name should be underscore-cased: bad_name, not bad_Name" ) diff --git a/src/ameba/reportable.cr b/src/ameba/reportable.cr index 751d89757..354539506 100644 --- a/src/ameba/reportable.cr +++ b/src/ameba/reportable.cr @@ -26,6 +26,13 @@ module Ameba add_issue rule, location, nil, message, **args end + # Adds a new issue for *location* and *end_location* defined by line and column numbers. + def add_issue(rule, location : Tuple(Int32, Int32), end_location : Tuple(Int32, Int32), message, **args) + location = Crystal::Location.new path, *location + end_location = Crystal::Location.new path, *end_location + add_issue rule, location, end_location, message, **args + end + # Returns true if the list of not disabled issues is empty, false otherwise. def valid? issues.reject(&.disabled?).empty? diff --git a/src/ameba/rule/style/method_names.cr b/src/ameba/rule/style/method_names.cr index 220c191cf..5d424c4bd 100644 --- a/src/ameba/rule/style/method_names.cr +++ b/src/ameba/rule/style/method_names.cr @@ -52,7 +52,16 @@ module Ameba::Rule::Style def test(source, node : Crystal::Def) return if (expected = node.name.underscore) == node.name - issue_for node, MSG % {expected, node.name} + line_number = node.location.try &.line_number + column_number = node.name_column_number + + return unless line_number + + issue_for( + {line_number, column_number}, + {line_number, column_number + node.name.size - 1}, + MSG % {expected, node.name} + ) end end end