From 6fb483a2dd9486eccec25de33e35874f604ffb3d Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Fri, 2 Feb 2018 10:52:21 +0200 Subject: [PATCH] Prevent disabling of UnneededDisableDirective rule --- spec/ameba/rule/unneded_disable_directive_spec.cr | 9 +++++++++ src/ameba/inline_comments.cr | 1 + src/ameba/rule/base.cr | 5 +++++ src/ameba/rule/unneeded_disable_directive.cr | 6 +++--- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/spec/ameba/rule/unneded_disable_directive_spec.cr b/spec/ameba/rule/unneded_disable_directive_spec.cr index c83e061f..5737a28e 100644 --- a/spec/ameba/rule/unneded_disable_directive_spec.cr +++ b/spec/ameba/rule/unneded_disable_directive_spec.cr @@ -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 diff --git a/src/ameba/inline_comments.cr b/src/ameba/inline_comments.cr index 7c090d95..1182d871 100644 --- a/src/ameba/inline_comments.cr +++ b/src/ameba/inline_comments.cr @@ -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]? diff --git a/src/ameba/rule/base.cr b/src/ameba/rule/base.cr index c10c6a30..df609438 100644 --- a/src/ameba/rule/base.cr +++ b/src/ameba/rule/base.cr @@ -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: # diff --git a/src/ameba/rule/unneeded_disable_directive.cr b/src/ameba/rule/unneeded_disable_directive.cr index 46aadf11..57b084e4 100644 --- a/src/ameba/rule/unneeded_disable_directive.cr +++ b/src/ameba/rule/unneeded_disable_directive.cr @@ -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: # # ``` @@ -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