Skip to content

Commit

Permalink
Prevent disabling of UnneededDisableDirective rule
Browse files Browse the repository at this point in the history
  • Loading branch information
veelenga committed Feb 2, 2018
1 parent 8075c39 commit 6fb483a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions spec/ameba/rule/unneded_disable_directive_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ module Ameba::Rule
s.errors.last.message.should contain "Rule3"
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
s = Source.new %Q(
# ameba:disable Rule1, Rule2
Expand Down
1 change: 1 addition & 0 deletions src/ameba/inline_comments.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Ameba
# ```
#
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 = lines[line_number]?

Expand Down
5 changes: 5 additions & 0 deletions src/ameba/rule/base.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module Ameba::Rule
SPECIAL = [
Syntax.class_name,
UnneededDisableDirective.class_name,
]

# Represents a base of all rules. In other words, all rules
# inherits from this struct:
#
Expand Down
6 changes: 3 additions & 3 deletions src/ameba/rule/unneeded_disable_directive.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Ameba::Rule
# 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:
#
# ```
Expand Down Expand Up @@ -39,11 +39,11 @@ module Ameba::Rule
return unless directive[:action] == "disable"

directive[:rules].reject do |rule_name|
any = source.errors.any? do |error|
source.errors.any? do |error|
error.rule.name == rule_name &&
error.disabled? &&
error.location.try(&.line_number) == location.line_number
end
end && rule_name != self.name
end
end
end
Expand Down

0 comments on commit 6fb483a

Please sign in to comment.