-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2583 from DNNX/times-map
New Performance/TimesMap cop
- Loading branch information
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# encoding: utf-8 | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop checks for .times.map calls. | ||
# In most cases such calls can be replaced | ||
# with an explicit array creation. | ||
# | ||
# @example | ||
# | ||
# @bad | ||
# 9.times.map do |i| | ||
# i.to_s | ||
# end | ||
# | ||
# @good | ||
# Array.new(9) do |i| | ||
# i.to_s | ||
# end | ||
class TimesMap < Cop | ||
MSG = 'Use `Array.new` with a block instead of `.times.%s`.' | ||
|
||
def on_send(node) | ||
check(node) | ||
end | ||
|
||
def on_block(node) | ||
check(node) | ||
end | ||
|
||
private | ||
|
||
def check(node) | ||
map_or_collect = times_map_call(node) | ||
if map_or_collect | ||
add_offense(node, :expression, format(MSG, map_or_collect)) | ||
end | ||
end | ||
|
||
def_node_matcher :times_map_call, <<-END | ||
{(block (send (send _ :times) ${:map :collect}) ...) | ||
(send (send _ :times) ${:map :collect} (block_pass ...))} | ||
END | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Performance::TimesMap do | ||
subject(:cop) { described_class.new } | ||
|
||
before do | ||
inspect_source(cop, source) | ||
end | ||
|
||
context '.times.map' do | ||
context 'with a block' do | ||
let(:source) { '4.times.map { |i| i.to_s }' } | ||
|
||
it 'registers an offense' do | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.first.message).to eq( | ||
'Use `Array.new` with a block instead of `.times.map`.' | ||
) | ||
expect(cop.highlights).to eq(['4.times.map { |i| i.to_s }']) | ||
end | ||
end | ||
|
||
context 'with an explicitly passed block' do | ||
let(:source) { '4.times.map(&method(:foo))' } | ||
|
||
it 'registers an offense' do | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.first.message).to eq( | ||
'Use `Array.new` with a block instead of `.times.map`.' | ||
) | ||
expect(cop.highlights).to eq(['4.times.map(&method(:foo))']) | ||
end | ||
end | ||
|
||
context 'without a block' do | ||
let(:source) { '4.times.map' } | ||
|
||
it "doesn't register an offense" do | ||
expect(cop.offenses).to be_empty | ||
end | ||
end | ||
end | ||
|
||
context '.times.collect' do | ||
context 'with a block' do | ||
let(:source) { '4.times.collect { |i| i.to_s }' } | ||
|
||
it 'registers an offense' do | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.first.message).to eq( | ||
'Use `Array.new` with a block instead of `.times.collect`.' | ||
) | ||
expect(cop.highlights).to eq(['4.times.collect { |i| i.to_s }']) | ||
end | ||
end | ||
|
||
context 'with an explicitly passed block' do | ||
let(:source) { '4.times.collect(&method(:foo))' } | ||
|
||
it 'registers an offense' do | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.first.message).to eq( | ||
'Use `Array.new` with a block instead of `.times.collect`.' | ||
) | ||
expect(cop.highlights).to eq(['4.times.collect(&method(:foo))']) | ||
end | ||
end | ||
|
||
context 'without a block' do | ||
let(:source) { '4.times.collect' } | ||
|
||
it "doesn't register an offense" do | ||
expect(cop.offenses).to be_empty | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters