Merge pull request #186 from crystal-ameba/feature/join-after-map

Add Performance/JoinAfterMap rule
This commit is contained in:
Sijawusz Pur Rahnama 2021-01-17 16:59:51 +01:00 committed by GitHub
commit 66a4b283d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,58 @@
require "../../../spec_helper"
module Ameba::Rule::Performance
subject = JoinAfterMap.new
describe JoinAfterMap do
it "passes if there is no potential performance improvements" do
source = Source.new %(
(1..3).join(&.to_s)
(1..3).join('.', &.to_s)
)
subject.catch(source).should be_valid
end
it "reports if there is map followed by join without a block" do
source = Source.new %(
(1..3).map(&.to_s).join
)
subject.catch(source).should_not be_valid
end
it "reports if there is map followed by join without a block (with argument)" do
source = Source.new %(
(1..3).map(&.to_s).join('.')
)
subject.catch(source).should_not be_valid
end
it "reports if there is map followed by join with a block" do
source = Source.new %(
(1..3).map(&.to_s).join(&.itself)
)
subject.catch(source).should_not be_valid
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
{{ [1, 2, 3].map(&.to_s).join }}
)
subject.catch(source).should be_valid
end
end
it "reports rule, pos and message" do
s = Source.new %(
(1..3).map(&.to_s).join
), "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(separator) {...}` instead of `map {...}.join(separator)`"
end
end
end

View file

@ -0,0 +1,48 @@
module Ameba::Rule::Performance
# This rule is used to identify usage of `join` calls that follow `map`.
#
# For example, this is considered inefficient:
#
# ```
# (1..3).map(&.to_s).join('.')
# ```
#
# And can be written as this:
#
# ```
# (1..3).join('.', &.to_s)
# ```
#
# YAML configuration example:
#
# ```
# Performance/JoinAfterMap
# Enabled: true
# ```
struct JoinAfterMap < Base
MAP_NAME = "map"
JOIN_NAME = "join"
MSG = "Use `join(separator) {...}` instead of `map {...}.join(separator)`"
properties do
description "Identifies usage of `join` calls that follow `map`."
end
def test(source)
AST::NodeVisitor.new self, source, skip: [
Crystal::Macro,
Crystal::MacroExpression,
Crystal::MacroIf,
Crystal::MacroFor,
]
end
def test(source, node : Crystal::Call)
return unless node.name == JOIN_NAME && (obj = node.obj)
return unless obj.is_a?(Crystal::Call) && obj.block
return unless obj.name == MAP_NAME
issue_for obj.name_location, node.name_end_location, MSG
end
end
end