-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require "../../spec_helper" | ||
|
||
module Ameba::Rule | ||
describe UnneededDisableDirective do | ||
subject = UnneededDisableDirective.new | ||
|
||
it "passes if there are no comments" do | ||
s = Source.new %( | ||
a = 1 | ||
) | ||
subject.catch(s).should be_valid | ||
end | ||
|
||
it "passes if there is disable directive" do | ||
s = Source.new %( | ||
a = 1 # my super var | ||
) | ||
subject.catch(s).should be_valid | ||
end | ||
|
||
it "passes if there is disable directive and it is needed" do | ||
s = Source.new %Q( | ||
a = 1 # ameba:disable #{NamedRule.name} | ||
) | ||
s.error NamedRule.new, 2, 1, "Alarm!", :disabled | ||
subject.catch(s).should be_valid | ||
end | ||
|
||
it "ignores commented out disable directive" do | ||
s = Source.new %Q( | ||
# # ameba:disable #{NamedRule.name} | ||
a = 1 | ||
) | ||
s.error NamedRule.new, 3, 1, "Alarm!", :disabled | ||
subject.catch(s).should be_valid | ||
end | ||
|
||
it "failes if there is unneeded directive" do | ||
s = Source.new %Q( | ||
# ameba:disable #{NamedRule.name} | ||
a = 1 | ||
) | ||
subject.catch(s).should_not be_valid | ||
s.errors.first.message.should eq( | ||
"Unnecessary disabling of #{NamedRule.name}" | ||
) | ||
end | ||
|
||
it "fails if there is inline unneeded directive" do | ||
s = Source.new %Q(a = 1 # ameba:disable #{NamedRule.name}) | ||
subject.catch(s).should_not be_valid | ||
s.errors.first.message.should eq( | ||
"Unnecessary disabling of #{NamedRule.name}" | ||
) | ||
end | ||
|
||
it "detects mixed inline directives" do | ||
s = Source.new %Q( | ||
# ameba:disable Rule1, Rule2 | ||
a = 1 # ameba:disable Rule3 | ||
), "source.cr" | ||
subject.catch(s).should_not be_valid | ||
s.errors.size.should eq 2 | ||
s.errors.first.message.should contain "Rule1, Rule2" | ||
s.errors.last.message.should contain "Rule3" | ||
end | ||
|
||
it "reports error, location and message" do | ||
s = Source.new %Q( | ||
# ameba:disable Rule1, Rule2 | ||
a = 1 | ||
), "source.cr" | ||
subject.catch(s).should_not be_valid | ||
error = s.errors.first | ||
error.rule.should_not be_nil | ||
error.location.to_s.should eq "source.cr:2:9" | ||
error.message.should eq "Unnecessary disabling of Rule1, Rule2" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Ameba::Rule | ||
# A rule that reports unneeded disable directives. | ||
# For example, this is considered invalid: | ||
# | ||
# ``` | ||
# # ameba:disable PredicateName | ||
# def comment? | ||
# do_something | ||
# end | ||
# ``` | ||
# | ||
# as the predicate name is correct and comment directive does not | ||
# have any effect, the snippet should be written as the following: | ||
# | ||
# ``` | ||
# def comment? | ||
# do_something | ||
# end | ||
# ``` | ||
# | ||
struct UnneededDisableDirective < Base | ||
properties do | ||
description = "Reports unneeded disable directives in comments" | ||
end | ||
|
||
def test(source) | ||
Tokenizer.new(source).run do |token| | ||
next unless token.type == :COMMENT | ||
next unless directive = source.parse_inline_directive(token.value.to_s) | ||
next unless names = unneeded_disables(source, directive, token.location) | ||
next unless names.any? | ||
|
||
source.error self, token.location, | ||
"Unnecessary disabling of #{names.join(", ")}" | ||
end | ||
end | ||
|
||
private def unneeded_disables(source, directive, location) | ||
return unless directive[:action] == "disable" | ||
|
||
directive[:rules].reject do |rule_name| | ||
any = source.errors.any? do |error| | ||
error.rule.name == rule_name && | ||
error.disabled? && | ||
error.location.try(&.line_number) == location.line_number | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters