Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework inline directives #265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 138 additions & 93 deletions spec/ameba/inline_comments_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ module Ameba
result = subject.match("# ameba:enable Group/RuleName")
result = result.should_not be_nil
result["action"].should eq "enable"
result["rules"].should eq "Group/RuleName"
result["names"].should eq "Group/RuleName"
end

it "parses multiple rules" do
result = subject.match("# ameba:enable Group/RuleName, OtherRule, Foo/Bar")
result = result.should_not be_nil
result["action"].should eq "enable"
result["rules"].should eq "Group/RuleName, OtherRule, Foo/Bar"
result["names"].should eq "Group/RuleName, OtherRule, Foo/Bar"
end

it "fails to parse directives with spaces" do
Expand All @@ -25,135 +25,180 @@ module Ameba
end
end

it "disables a rule with a comment directive" do
s = Source.new %Q(
# ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
s.should be_valid
end
context "ameba:disable_next_line" do
it "disables a rule with a comment directive" do
s = Source.new %Q(
# ameba:disable_next_line #{NamedRule.name}
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "Error!")
s.should be_valid
end

it "disables a rule with a line that ends with a comment directive" do
s = Source.new %Q(
Time.epoch(1483859302) # ameba:disable #{NamedRule.name}
)
s.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
s.should be_valid
end
it "disables a rule if multiple rule names are separated by comma" do
s = Source.new %Q(
# ameba:disable_next_line SomeRule, LargeNumbers, #{NamedRule.name}, SomeOtherRule
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should be_valid
end

it "does not disable a rule of a different name" do
s = Source.new %Q(
# ameba:disable WrongName
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "Error!")
s.should_not be_valid
end
it "does not disable if multiple rule names used without required one" do
s = Source.new %(
# ameba:disable_next_line SomeRule, SomeOtherRule LargeNumbers
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
end

it "disables a rule if multiple rule names provided" do
s = Source.new %Q(
# ameba:disable SomeRule LargeNumbers #{NamedRule.name} SomeOtherRule
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should be_valid
end
it "does not disable if comment directive has wrong place" do
s = Source.new %Q(
# ameba:disable_next_line #{NamedRule.name}
#
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.should_not be_valid
end

it "disables a rule if multiple rule names are separated by comma" do
s = Source.new %Q(
# ameba:disable SomeRule, LargeNumbers, #{NamedRule.name}, SomeOtherRule
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should be_valid
end
it "does not disable if that is not a comment directive" do
s = Source.new %Q(
"ameba:disable_next_line #{NamedRule.name}"
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
end

it "does not disable if multiple rule names used without required one" do
s = Source.new %(
# ameba:disable SomeRule, SomeOtherRule LargeNumbers
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
it "does not disable if that is a commented out directive" do
s = Source.new %Q(
# # ameba:disable_next_line #{NamedRule.name}
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
end
end

it "does not disable if comment directive has wrong place" do
s = Source.new %Q(
# ameba:disable #{NamedRule.name}
#
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.should_not be_valid
end
context "ameba:disable_line" do
it "disables a rule with a line that ends with a comment directive" do
s = Source.new %Q(
Time.epoch(1483859302) # ameba:disable_line #{NamedRule.name}
)
s.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
s.should be_valid
end

it "does not disable if comment directive added to the wrong line" do
s = Source.new %Q(
if use_epoch? # ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
end
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.should_not be_valid
end
it "does not disable a rule of a different name" do
s = Source.new %Q(
Time.epoch(1483859302) # ameba:disable_line WrongName
)
s.add_issue(NamedRule.new, location: {1, 1}, message: "Error!")
s.should_not be_valid
end

it "does not disable if that is not a comment directive" do
s = Source.new %Q(
"ameba:disable #{NamedRule.name}"
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.should_not be_valid
end
it "disables a rule if multiple rule names provided" do
s = Source.new %Q(
Time.epoch(1483859302) # ameba:disable_line SomeRule LargeNumbers #{NamedRule.name} SomeOtherRule
)
s.add_issue(NamedRule.new, location: {1, 1}, message: "")
s.should be_valid
end

it "does not disable if that is a commented out directive" do
s = Source.new %Q(
# # ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.should_not be_valid
it "does not disable if comment directive added to the wrong line" do
s = Source.new %Q(
if use_epoch? # ameba:disable_line #{NamedRule.name}
Time.epoch(1483859302)
end
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
end

it "does not disable if that is an inline commented out directive" do
s = Source.new %Q(
a = 1 # Disable it: # ameba:disable_line #{NamedRule.name}
)
s.add_issue(NamedRule.new, location: {1, 1}, message: "")
s.should_not be_valid
end
end

it "does not disable if that is an inline commented out directive" do
s = Source.new %Q(
a = 1 # Disable it: # ameba:disable #{NamedRule.name}
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
context "ameba:disable/enable" do
it "disables region below" do
s = Source.new %Q(
# ameba:disable #{NamedRule.name}
# something goes here
# something else goes here
a = 1
)
s.add_issue(NamedRule.new, location: {4, 1}, message: "")
s.should be_valid
end

it "disables the line the region starts at" do
s = Source.new %Q(
a = 1 # ameba:disable #{NamedRule.name}
)
s.add_issue(NamedRule.new, location: {1, 1}, message: "")
s.should be_valid
end

it "enables disabled region" do
s = Source.new %Q(
# ameba:disable #{NamedRule.name}
# something goes here
# ameba:enable #{NamedRule.name}
# something else goes here
a = 1
)
s.add_issue(NamedRule.new, location: {5, 1}, message: "")
s.should_not be_valid
end

it "disables the rule if disable is not followed by enable directive" do
s = Source.new %Q(
# ameba:enable #{NamedRule.name}
# ameba:disable #{NamedRule.name}
a = 1
)
s.add_issue(NamedRule.new, location: {3, 1}, message: "")
s.should 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}
a = 1 # ameba:disable_line #{DummyRule.rule_name}
)
s.add_issue(DummyRule.new, location: {1, 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}
a = 1 # ameba:disable_line #{DummyRule.rule_name}
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.add_issue(NamedRule.new, location: {1, 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}
a = 1 # ameba:disable_line #{DummyRule.group_name}
)
s.add_issue(DummyRule.new, location: {1, 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
a = 1 # ameba:disable_line Lint
)
s.add_issue(DummyRule.new, location: {2, 12}, message: "")
s.add_issue(DummyRule.new, location: {1, 12}, message: "")
s.should_not be_valid
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/bad_directive_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Ameba::Rule::Lint
it "reports if there is incorrect action" do
expect_issue subject, <<-CRYSTAL
# ameba:foo Lint/BadDirective
# ^{} error: Bad action in comment directive: 'foo'. Possible values: disable, enable
# ^{} error: Bad action in comment directive: 'foo'. Possible values: enable, disable, disable_line, disable_next_line
CRYSTAL
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/runner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ module Ameba
rules = [NamedRule.new] of Rule::Base
source = Source.new %(
def foo
bar = 1 # ameba:disable #{NamedRule.name}
bar = 1 # ameba:disable_line #{NamedRule.name}
end
)
source.add_issue NamedRule.new, location: {2, 1},
Expand Down
Loading