Prevent disabling of UnneededDisableDirective rule

This commit is contained in:
Vitalii Elenhaupt 2018-02-02 10:52:21 +02:00 committed by V. Elenhaupt
parent 8075c39aa9
commit 6fb483a2dd
4 changed files with 18 additions and 3 deletions

View file

@ -65,6 +65,15 @@ module Ameba::Rule
s.errors.last.message.should contain "Rule3" s.errors.last.message.should contain "Rule3"
end end
it "fails if there is disabled UnneededDisableDirective" do
s = Source.new %Q(
# ameba:disable #{UnneededDisableDirective.class_name}
a = 1
), "source.cr"
s.error UnneededDisableDirective.new, 3, 1, "Alarm!", :disabled
subject.catch(s).should_not be_valid
end
it "reports error, location and message" do it "reports error, location and message" do
s = Source.new %Q( s = Source.new %Q(
# ameba:disable Rule1, Rule2 # ameba:disable Rule1, Rule2

View file

@ -32,6 +32,7 @@ module Ameba
# ``` # ```
# #
def location_disabled?(location, rule) def location_disabled?(location, rule)
return false if Rule::SPECIAL.includes?(rule)
return false unless line_number = location.try &.line_number.try &.- 1 return false unless line_number = location.try &.line_number.try &.- 1
return false unless line = lines[line_number]? return false unless line = lines[line_number]?

View file

@ -1,4 +1,9 @@
module Ameba::Rule module Ameba::Rule
SPECIAL = [
Syntax.class_name,
UnneededDisableDirective.class_name,
]
# Represents a base of all rules. In other words, all rules # Represents a base of all rules. In other words, all rules
# inherits from this struct: # inherits from this struct:
# #

View file

@ -9,7 +9,7 @@ module Ameba::Rule
# end # end
# ``` # ```
# #
# as the predicate name is correct and comment directive does not # as the predicate name is correct and the comment directive does not
# have any effect, the snippet should be written as the following: # have any effect, the snippet should be written as the following:
# #
# ``` # ```
@ -39,11 +39,11 @@ module Ameba::Rule
return unless directive[:action] == "disable" return unless directive[:action] == "disable"
directive[:rules].reject do |rule_name| directive[:rules].reject do |rule_name|
any = 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.location.try(&.line_number) == location.line_number
end end && rule_name != self.name
end end
end end
end end