From 98f0aa71e944658f4531236493378264d98a796f Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sat, 23 Jun 2018 16:52:48 +0300 Subject: [PATCH] Allow to disable group of rules using inline directives --- README.md | 6 ++++-- spec/ameba/inline_comments_spec.cr | 34 ++++++++++++++++++++++++++++++ src/ameba/inline_comments.cr | 13 +++++++++--- src/ameba/reportable.cr | 2 +- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4083d15d..28205b00 100644 --- a/README.md +++ b/README.md @@ -111,13 +111,15 @@ $ ameba --except Style,Lint # runs all rules except rules in Style and Lint gro ### Inline disabling -One or more rules can be disabled using inline directives: +One or more rules or one or more group of rules can be disabled using inline directives: ```crystal # ameba:disable Style/LargeNumbers time = Time.epoch(1483859302) -time = Time.epoch(1483859302) # ameba:disable Style/LargeNumbers +time = Time.epoch(1483859302) # ameba:disable Style/LargeNumbers, Lint/UselessAssign + +time = Time.epoch(1483859302) # ameba:disable Style, Lint ``` ## Editor integration diff --git a/spec/ameba/inline_comments_spec.cr b/spec/ameba/inline_comments_spec.cr index 95971c75..86fdd27b 100644 --- a/spec/ameba/inline_comments_spec.cr +++ b/spec/ameba/inline_comments_spec.cr @@ -100,5 +100,39 @@ module Ameba s.add_issue(NamedRule.new, location: {2, 12}, message: "") s.should_not be_valid end + + context "with group name" do + it "disables one rule with a group" do + s = Source.new %Q( + a = 1 # ameba:disable #{DummyRule.rule_name} + ) + s.add_issue(DummyRule.new, location: {2, 12}, message: "") + s.should be_valid + end + + it "doesn't disable others rules" do + s = Source.new %Q( + a = 1 # ameba:disable #{DummyRule.rule_name} + ) + s.add_issue(NamedRule.new, location: {2, 12}, message: "") + s.should_not be_valid + end + + it "disables a hole group of rules" do + s = Source.new %Q( + a = 1 # ameba:disable #{DummyRule.group_name} + ) + s.add_issue(DummyRule.new, location: {2, 12}, message: "") + s.should be_valid + end + + it "does not disable rules which do not belong to the group" do + s = Source.new %Q( + a = 1 # ameba:disable Lint + ) + s.add_issue(DummyRule.new, location: {2, 12}, message: "") + s.should_not be_valid + end + end end end diff --git a/src/ameba/inline_comments.cr b/src/ameba/inline_comments.cr index a4ea0fa9..5aea7cfc 100644 --- a/src/ameba/inline_comments.cr +++ b/src/ameba/inline_comments.cr @@ -1,7 +1,13 @@ module Ameba # A module that utilizes inline comments parsing and processing logic. module InlineComments - COMMENT_DIRECTIVE_REGEX = Regex.new "# ameba : (\\w+) ([\\w, ]+)".gsub(" ", "\\s*") + COMMENT_DIRECTIVE_REGEX = Regex.new "# ameba : (\\w+) ([\\w\/, ]+)".gsub(" ", "\\s*") + + # Available actions in the inline comments + enum Action + Disable + Enable + end # Returns true if current location is disabled for a particular rule, # false otherwise. @@ -32,7 +38,7 @@ module Ameba # ``` # def location_disabled?(location, rule) - return false if Rule::SPECIAL.includes?(rule) + return false if Rule::SPECIAL.includes?(rule.name) return false unless line_number = location.try &.line_number.try &.- 1 return false unless line = lines[line_number]? @@ -83,7 +89,8 @@ module Ameba private def line_disabled?(line, rule) return false unless directive = parse_inline_directive(line) - directive[:action] == "disable" && directive[:rules].includes?(rule) + Action.parse?(directive[:action]).try(&.disable?) && + (directive[:rules].includes?(rule.name) || directive[:rules].includes?(rule.group)) end private def commented_out?(line) diff --git a/src/ameba/reportable.cr b/src/ameba/reportable.cr index 8cd13491..2ad7872a 100644 --- a/src/ameba/reportable.cr +++ b/src/ameba/reportable.cr @@ -6,7 +6,7 @@ module Ameba # Adds a new issue to the list of issues. def add_issue(rule, location, end_location, message, status = nil) - status ||= :disabled if location_disabled?(location, rule.name) + status ||= :disabled if location_disabled?(location, rule) issues << Issue.new rule, location, end_location, message, status end