Allow to disable group of rules using inline directives

This commit is contained in:
Vitalii Elenhaupt 2018-06-23 16:52:48 +03:00
parent 04c7300964
commit 98f0aa71e9
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
4 changed files with 49 additions and 6 deletions

View file

@ -111,13 +111,15 @@ $ ameba --except Style,Lint # runs all rules except rules in Style and Lint gro
### Inline disabling ### 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 ```crystal
# ameba:disable Style/LargeNumbers # ameba:disable Style/LargeNumbers
time = Time.epoch(1483859302) 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 ## Editor integration

View file

@ -100,5 +100,39 @@ module Ameba
s.add_issue(NamedRule.new, location: {2, 12}, message: "") s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid s.should_not be_valid
end 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
end end

View file

@ -1,7 +1,13 @@
module Ameba module Ameba
# A module that utilizes inline comments parsing and processing logic. # A module that utilizes inline comments parsing and processing logic.
module InlineComments 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, # Returns true if current location is disabled for a particular rule,
# false otherwise. # false otherwise.
@ -32,7 +38,7 @@ module Ameba
# ``` # ```
# #
def location_disabled?(location, rule) 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_number = location.try &.line_number.try &.- 1
return false unless line = lines[line_number]? return false unless line = lines[line_number]?
@ -83,7 +89,8 @@ module Ameba
private def line_disabled?(line, rule) private def line_disabled?(line, rule)
return false unless directive = parse_inline_directive(line) 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 end
private def commented_out?(line) private def commented_out?(line)

View file

@ -6,7 +6,7 @@ module Ameba
# Adds a new issue to the list of issues. # Adds a new issue to the list of issues.
def add_issue(rule, location, end_location, message, status = nil) 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 issues << Issue.new rule, location, end_location, message, status
end end