mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: Performance/AnyAfterFilter
This commit is contained in:
parent
068733a9d3
commit
a1b34eb7be
3 changed files with 115 additions and 2 deletions
64
spec/ameba/rule/performance/any_after_filter_spec.cr
Normal file
64
spec/ameba/rule/performance/any_after_filter_spec.cr
Normal file
|
@ -0,0 +1,64 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Performance
|
||||
subject = AnyAfterFilter.new
|
||||
|
||||
describe AnyAfterFilter do
|
||||
it "passes if there is no potential performance improvements" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].select { |e| e > 1 }.any?(&.zero?)
|
||||
[1, 2, 3].reject { |e| e > 1 }.any?(&.zero?)
|
||||
[1, 2, 3].select { |e| e > 1 }
|
||||
[1, 2, 3].reject { |e| e > 1 }
|
||||
[1, 2, 3].any? { |e| e > 1 }
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
end
|
||||
|
||||
it "reports if there is select followed by any? without a block" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].select { |e| e > 2 }.any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports if there is reject followed by any? without a block" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].reject { |e| e > 2 }.any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
|
||||
it "does not report if any? calls contains a block" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].select { |e| e > 2 }.any?(&.zero?)
|
||||
[1, 2, 3].reject { |e| e > 2 }.any?(&.zero?)
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
end
|
||||
|
||||
context "properties" do
|
||||
it "allows to configure object_call_names" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].reject { |e| e > 2 }.any?
|
||||
)
|
||||
rule = Rule::Performance::AnyAfterFilter.new
|
||||
rule.filter_names = %w(select)
|
||||
rule.catch(source).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
[1, 2, 3].reject { |e| e > 2 }.any?
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
issue = s.issues.first
|
||||
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:2:19"
|
||||
issue.end_location.to_s.should eq "source.cr:2:44"
|
||||
issue.message.should eq "Use `any? {...}` instead of `reject {...}.any?`"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue