mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Do not report unneeded disable directive if directive is used one line above
This commit is contained in:
parent
c7fc905413
commit
1fc0c525bd
4 changed files with 28 additions and 3 deletions
|
@ -18,7 +18,16 @@ module Ameba::Rule
|
||||||
subject.catch(s).should be_valid
|
subject.catch(s).should be_valid
|
||||||
end
|
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(
|
s = Source.new %Q(
|
||||||
a = 1 # ameba:disable #{NamedRule.name}
|
a = 1 # ameba:disable #{NamedRule.name}
|
||||||
)
|
)
|
||||||
|
|
|
@ -72,7 +72,7 @@ module Ameba
|
||||||
def failure_message(source)
|
def failure_message(source)
|
||||||
String.build do |str|
|
String.build do |str|
|
||||||
str << "Source expected to be valid, but there are errors:\n\n"
|
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"
|
str << " * #{e.rule.name}: #{e.message}\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -70,6 +70,13 @@ module Ameba
|
||||||
end
|
end
|
||||||
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)
|
private def comment?(line)
|
||||||
return true if line.lstrip.starts_with? '#'
|
return true if line.lstrip.starts_with? '#'
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,9 +43,18 @@ module Ameba::Rule
|
||||||
source.errors.any? do |error|
|
source.errors.any? do |error|
|
||||||
error.rule.name == rule_name &&
|
error.rule.name == rule_name &&
|
||||||
error.disabled? &&
|
error.disabled? &&
|
||||||
error.location.try(&.line_number) == location.line_number
|
error_at_location?(source, error, location)
|
||||||
end && rule_name != self.name
|
end && rule_name != self.name
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue