Do not report unneeded disable directive if directive is used one line above

This commit is contained in:
Vitalii Elenhaupt 2018-05-09 00:14:46 +03:00
parent c7fc905413
commit 1fc0c525bd
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
4 changed files with 28 additions and 3 deletions

View file

@ -18,7 +18,16 @@ module Ameba::Rule
subject.catch(s).should be_valid
end
it "passes if there is disable directive and it is needed" do
it "doesn't report if there is disable directive and it is needed" do
s = Source.new %Q(
# ameba:disable #{NamedRule.name}
a = 1
)
s.error NamedRule.new, 3, 9, "Useless assignment", :disabled
subject.catch(s).should be_valid
end
it "passes if there is inline disable directive and it is needed" do
s = Source.new %Q(
a = 1 # ameba:disable #{NamedRule.name}
)

View file

@ -72,7 +72,7 @@ module Ameba
def failure_message(source)
String.build do |str|
str << "Source expected to be valid, but there are errors:\n\n"
source.errors.each do |e|
source.errors.reject(&.disabled?).each do |e|
str << " * #{e.rule.name}: #{e.message}\n"
end
end

View file

@ -70,6 +70,13 @@ module Ameba
end
end
# Returns true if the line at the given `line_number` is a comment.
def comment?(line_number : Int32)
if line = lines[line_number]?
comment?(line)
end
end
private def comment?(line)
return true if line.lstrip.starts_with? '#'
end

View file

@ -43,9 +43,18 @@ module Ameba::Rule
source.errors.any? do |error|
error.rule.name == rule_name &&
error.disabled? &&
error.location.try(&.line_number) == location.line_number
error_at_location?(source, error, location)
end && rule_name != self.name
end
end
private def error_at_location?(source, error, location)
return false unless error_line_number = error.location.try(&.line_number)
error_line_number == location.line_number ||
((prev_line_number = error_line_number - 1) &&
prev_line_number == location.line_number &&
source.comment?(prev_line_number - 1))
end
end
end