mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Merge pull request #197 from crystal-ameba/feature/any-instead-of-empty
Add Performance/AnyInsteadOfEmpty rule
This commit is contained in:
commit
fac8072ec1
9 changed files with 98 additions and 8 deletions
|
@ -51,7 +51,7 @@ module Ameba::AST
|
|||
it "adds a new variable to the scope" do
|
||||
scope = Scope.new as_node("")
|
||||
scope.add_variable(Crystal::Var.new "foo")
|
||||
scope.variables.any?.should be_true
|
||||
scope.variables.empty?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ module Ameba::AST
|
|||
it "assigns the variable (creates a new assignment)" do
|
||||
variable = Variable.new(var_node, scope)
|
||||
variable.assign(assign_node, scope)
|
||||
variable.assignments.any?.should be_true
|
||||
variable.assignments.empty?.should be_false
|
||||
end
|
||||
|
||||
it "can create multiple assignments" do
|
||||
|
@ -64,7 +64,7 @@ module Ameba::AST
|
|||
variable = Variable.new(var_node, scope)
|
||||
variable.assign(as_node("foo=1"), scope)
|
||||
variable.reference(var_node, scope)
|
||||
variable.references.any?.should be_true
|
||||
variable.references.empty?.should be_false
|
||||
end
|
||||
|
||||
it "adds a reference to the scope" do
|
||||
|
|
46
spec/ameba/rule/performance/any_instead_of_empty_spec.cr
Normal file
46
spec/ameba/rule/performance/any_instead_of_empty_spec.cr
Normal file
|
@ -0,0 +1,46 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Performance
|
||||
subject = AnyInsteadOfEmpty.new
|
||||
|
||||
describe AnyInsteadOfEmpty do
|
||||
it "passes if there is no potential performance improvements" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].any?(&.zero?)
|
||||
[1, 2, 3].any?(String)
|
||||
[1, 2, 3].any?(1..3)
|
||||
[1, 2, 3].any? { |e| e > 1 }
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
end
|
||||
|
||||
it "reports if there is any? call without a block nor argument" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "reports in macro scope" do
|
||||
source = Source.new %(
|
||||
{{ [1, 2, 3].any? }}
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new path: "source.cr", code: %(
|
||||
[1, 2, 3].any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
issue = source.issues.first
|
||||
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:1:11"
|
||||
issue.end_location.to_s.should eq "source.cr:1:15"
|
||||
issue.message.should eq "Use `!{...}.empty?` instead of `{...}.any?`"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue