From 2694a0c8654778e4ddf421846201c848b2051b14 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 12 Apr 2021 06:22:54 +0200 Subject: [PATCH] =?UTF-8?q?Remove=20=E2=80=9Cjoin=E2=80=9D=20from=20the=20?= =?UTF-8?q?list=20of=20trigger=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../performance/map_instead_of_block_spec.cr | 21 +++++++++---------- .../rule/performance/map_instead_of_block.cr | 8 +++---- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/spec/ameba/rule/performance/map_instead_of_block_spec.cr b/spec/ameba/rule/performance/map_instead_of_block_spec.cr index 6a8c468a..b3a14632 100644 --- a/spec/ameba/rule/performance/map_instead_of_block_spec.cr +++ b/spec/ameba/rule/performance/map_instead_of_block_spec.cr @@ -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 diff --git a/src/ameba/rule/performance/map_instead_of_block.cr b/src/ameba/rule/performance/map_instead_of_block.cr index a00a4698..cef1eed7 100644 --- a/src/ameba/rule/performance/map_instead_of_block.cr +++ b/src/ameba/rule/performance/map_instead_of_block.cr @@ -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`"