Remove “join” from the list of trigger methods

This commit is contained in:
Sijawusz Pur Rahnama 2021-04-12 06:22:54 +02:00
parent 9a25214bb5
commit 2694a0c865
2 changed files with 13 additions and 16 deletions

View file

@ -6,30 +6,29 @@ module Ameba::Rule::Performance
describe MapInsteadOfBlock do
it "passes if there is no potential performance improvements" do
source = Source.new %(
(1..3).join(&.to_s)
(1..3).sum(&.*(2))
(1..3).product(&.*(2))
)
subject.catch(source).should be_valid
end
it "reports if there is map followed by join without a block" do
it "reports if there is map followed by sum without a block" do
source = Source.new %(
(1..3).map(&.to_s).join
(1..3).map(&.to_u64).sum
)
subject.catch(source).should_not be_valid
end
it "reports if there is map followed by join without a block (with argument)" do
it "reports if there is map followed by sum without a block (with argument)" do
source = Source.new %(
(1..3).map(&.to_s).join('.')
(1..3).map(&.to_u64).sum(0)
)
subject.catch(source).should_not be_valid
end
it "reports if there is map followed by join with a block" do
it "reports if there is map followed by sum with a block" do
source = Source.new %(
(1..3).map(&.to_s).join(&.itself)
(1..3).map(&.to_u64).sum(&.itself)
)
subject.catch(source).should_not be_valid
end
@ -37,7 +36,7 @@ module Ameba::Rule::Performance
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
{{ [1, 2, 3].map(&.to_s).join }}
{{ [1, 2, 3].map(&.to_u64).sum }}
)
subject.catch(source).should be_valid
end
@ -45,15 +44,15 @@ module Ameba::Rule::Performance
it "reports rule, pos and message" do
s = Source.new %(
(1..3).map(&.to_s).join
(1..3).map(&.to_u64).sum
), "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:1:8"
issue.end_location.to_s.should eq "source.cr:1:24"
issue.message.should eq "Use `join {...}` instead of `map {...}.join`"
issue.end_location.to_s.should eq "source.cr:1:25"
issue.message.should eq "Use `sum {...}` instead of `map {...}.sum`"
end
end
end

View file

@ -1,18 +1,16 @@
module Ameba::Rule::Performance
# This rule is used to identify usage of `join/sum/product` calls
# This rule is used to identify usage of `sum/product` calls
# that follow `map`.
#
# For example, this is considered inefficient:
#
# ```
# (1..3).map(&.to_s).join('.')
# (1..3).map(&.*(2)).sum
# ```
#
# And can be written as this:
#
# ```
# (1..3).join('.', &.to_s)
# (1..3).sum(&.*(2))
# ```
#
@ -25,10 +23,10 @@ module Ameba::Rule::Performance
class MapInsteadOfBlock < Base
properties do
enabled false
description "Identifies usage of `join/sum/product` calls that follow `map`."
description "Identifies usage of `sum/product` calls that follow `map`."
end
CALL_NAMES = %w(join sum product)
CALL_NAMES = %w(sum product)
MAP_NAME = "map"
MSG = "Use `%s {...}` instead of `map {...}.%s`"