mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Extend JoinAfterMap to check also calls to sum/product
and rename it to MapInsteadOfBlock (#190)
This commit is contained in:
parent
e9ec91654c
commit
d2fa75280f
2 changed files with 18 additions and 13 deletions
|
@ -1,13 +1,14 @@
|
||||||
require "../../../spec_helper"
|
require "../../../spec_helper"
|
||||||
|
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
subject = JoinAfterMap.new
|
subject = MapInsteadOfBlock.new
|
||||||
|
|
||||||
describe JoinAfterMap do
|
describe MapInsteadOfBlock do
|
||||||
it "passes if there is no potential performance improvements" do
|
it "passes if there is no potential performance improvements" do
|
||||||
source = Source.new %(
|
source = Source.new %(
|
||||||
(1..3).join(&.to_s)
|
(1..3).join(&.to_s)
|
||||||
(1..3).join('.', &.to_s)
|
(1..3).sum(&.*(2))
|
||||||
|
(1..3).product(&.*(2))
|
||||||
)
|
)
|
||||||
subject.catch(source).should be_valid
|
subject.catch(source).should be_valid
|
||||||
end
|
end
|
||||||
|
@ -52,7 +53,7 @@ module Ameba::Rule::Performance
|
||||||
issue.rule.should_not be_nil
|
issue.rule.should_not be_nil
|
||||||
issue.location.to_s.should eq "source.cr:1:8"
|
issue.location.to_s.should eq "source.cr:1:8"
|
||||||
issue.end_location.to_s.should eq "source.cr:1:24"
|
issue.end_location.to_s.should eq "source.cr:1:24"
|
||||||
issue.message.should eq "Use `join(separator) {...}` instead of `map {...}.join(separator)`"
|
issue.message.should eq "Use `join {...}` instead of `map {...}.join`"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -1,32 +1,35 @@
|
||||||
module Ameba::Rule::Performance
|
module Ameba::Rule::Performance
|
||||||
# This rule is used to identify usage of `join` calls that follow `map`.
|
# This rule is used to identify usage of `join/sum/product` calls
|
||||||
|
# that follow `map`.
|
||||||
#
|
#
|
||||||
# For example, this is considered inefficient:
|
# For example, this is considered inefficient:
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
# (1..3).map(&.to_s).join('.')
|
# (1..3).map(&.to_s).join('.')
|
||||||
|
# (1..3).map(&.*(2)).sum
|
||||||
# ```
|
# ```
|
||||||
#
|
#
|
||||||
# And can be written as this:
|
# And can be written as this:
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
# (1..3).join('.', &.to_s)
|
# (1..3).join('.', &.to_s)
|
||||||
|
# (1..3).sum(&.*(2))
|
||||||
# ```
|
# ```
|
||||||
#
|
#
|
||||||
# YAML configuration example:
|
# YAML configuration example:
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
# Performance/JoinAfterMap
|
# Performance/MapInsteadOfBlock
|
||||||
# Enabled: true
|
# Enabled: true
|
||||||
# ```
|
# ```
|
||||||
struct JoinAfterMap < Base
|
struct MapInsteadOfBlock < Base
|
||||||
properties do
|
properties do
|
||||||
description "Identifies usage of `join` calls that follow `map`."
|
description "Identifies usage of `join/sum/product` calls that follow `map`."
|
||||||
end
|
end
|
||||||
|
|
||||||
MAP_NAME = "map"
|
CALL_NAMES = %w(join sum product)
|
||||||
JOIN_NAME = "join"
|
MAP_NAME = "map"
|
||||||
MSG = "Use `join(separator) {...}` instead of `map {...}.join(separator)`"
|
MSG = "Use `%s {...}` instead of `map {...}.%s`"
|
||||||
|
|
||||||
def test(source)
|
def test(source)
|
||||||
AST::NodeVisitor.new self, source, skip: [
|
AST::NodeVisitor.new self, source, skip: [
|
||||||
|
@ -38,11 +41,12 @@ module Ameba::Rule::Performance
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source, node : Crystal::Call)
|
def test(source, node : Crystal::Call)
|
||||||
return unless node.name == JOIN_NAME && (obj = node.obj)
|
return unless node.name.in?(CALL_NAMES) && (obj = node.obj)
|
||||||
return unless obj.is_a?(Crystal::Call) && obj.block
|
return unless obj.is_a?(Crystal::Call) && obj.block
|
||||||
return unless obj.name == MAP_NAME
|
return unless obj.name == MAP_NAME
|
||||||
|
|
||||||
issue_for obj.name_location, node.name_end_location, MSG
|
issue_for obj.name_location, node.name_end_location,
|
||||||
|
MSG % {node.name, node.name}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue