Skip to content

Commit

Permalink
Merge pull request #134 from fatkodima/support-function-calls-in-size…
Browse files Browse the repository at this point in the history
…_cop

Support `Array()` and `Hash()` methods for `Performance/Size` cop
  • Loading branch information
koic authored Jun 9, 2020
2 parents 5a15ec9 + 99e1696 commit 41e4b8f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#125](https://github.com/rubocop-hq/rubocop-performance/pull/125): Support `Array()` and `Hash()` methods for `Performance/Size` cop. ([@fatkodima][])
* [#124](https://github.com/rubocop-hq/rubocop-performance/pull/124): Add new `Performance/Squeeze` cop. ([@fatkodima][])
* [#129](https://github.com/rubocop-hq/rubocop-performance/pull/129): Add new `Performance/BigDecimalWithNumericArgument` cop. ([@fatkodima][])
* [#130](https://github.com/rubocop-hq/rubocop-performance/pull/130): Add new `Performance/SortReverse` cop. ([@fatkodima][])
Expand Down
12 changes: 12 additions & 0 deletions docs/modules/ROOT/pages/cops_performance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1179,15 +1179,27 @@ have been assigned to an array or a hash.
----
# bad
[1, 2, 3].count
(1..3).to_a.count
Array[*1..3].count
Array(1..3).count
# bad
{a: 1, b: 2, c: 3}.count
[[:foo, :bar], [1, 2]].to_h.count
Hash[*('a'..'z')].count
Hash(key: :value).count
# good
[1, 2, 3].size
(1..3).to_a.size
Array[*1..3].size
Array(1..3).size
# good
{a: 1, b: 2, c: 3}.size
[[:foo, :bar], [1, 2]].to_h.size
Hash[*('a'..'z')].size
Hash(key: :value).size
# good
[1, 2, 3].count { |e| e > 2 }
Expand Down
14 changes: 14 additions & 0 deletions lib/rubocop/cop/performance/size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ module Performance
# @example
# # bad
# [1, 2, 3].count
# (1..3).to_a.count
# Array[*1..3].count
# Array(1..3).count
#
# # bad
# {a: 1, b: 2, c: 3}.count
# [[:foo, :bar], [1, 2]].to_h.count
# Hash[*('a'..'z')].count
# Hash(key: :value).count
#
# # good
# [1, 2, 3].size
# (1..3).to_a.size
# Array[*1..3].size
# Array(1..3).size
#
# # good
# {a: 1, b: 2, c: 3}.size
# [[:foo, :bar], [1, 2]].to_h.size
# Hash[*('a'..'z')].size
# Hash(key: :value).size
#
# # good
# [1, 2, 3].count { |e| e > 2 }
Expand All @@ -31,6 +43,7 @@ class Size < Cop
[!nil? array_type?]
(send _ :to_a)
(send (const nil? :Array) :[] _)
(send nil? :Array _)
}
PATTERN

Expand All @@ -39,6 +52,7 @@ class Size < Cop
[!nil? hash_type?]
(send _ :to_h)
(send (const nil? :Hash) :[] _)
(send nil? :Hash _)
}
PATTERN

Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/performance/size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
RUBY
end

it 'registers an offense when calling count on Array()' do
expect_offense(<<~RUBY)
Array(1..5).count
^^^^^ Use `size` instead of `count`.
RUBY
end

it 'does not register an offense when calling size' do
expect_no_offenses('[1, 2, 3].size')
end
Expand Down Expand Up @@ -72,6 +79,12 @@

expect(new_source).to eq('Array[*1..5].size')
end

it 'corrects count to size on Array()' do
new_source = autocorrect_source('Array(1..5).count')

expect(new_source).to eq('Array(1..5).size')
end
end

describe 'on hash' do
Expand All @@ -96,6 +109,13 @@
RUBY
end

it 'registers an offense when calling count on Hash()' do
expect_offense(<<~RUBY)
Hash(key: :value).count
^^^^^ Use `size` instead of `count`.
RUBY
end

it 'does not register an offense when calling size' do
expect_no_offenses('{a: 1, b: 2, c: 3}.size')
end
Expand Down Expand Up @@ -133,5 +153,11 @@

expect(new_source).to eq("Hash[*('a'..'z')].size")
end

it 'corrects count to size on Hash()' do
new_source = autocorrect_source('Hash(key: :value).count')

expect(new_source).to eq('Hash(key: :value).size')
end
end
end

0 comments on commit 41e4b8f

Please sign in to comment.