Skip to content

Commit

Permalink
Merge pull request #7787 from tejasbubane/fix-7767
Browse files Browse the repository at this point in the history
[Fix #7767] Skip array literals in `Style/HashTransformValues` & `Style/HashTransformKeys`
  • Loading branch information
koic committed Mar 10, 2020
2 parents e9e0482 + 2a57508 commit 03a58c9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@

* [#7779](https://github.com/rubocop-hq/rubocop/issues/7779): Fix a false positive for `Style/MultilineMethodCallIndentation` when using Ruby 2.7's numbered parameter. ([@koic][])
* [#7733](https://github.com/rubocop-hq/rubocop/issues/7733): Fix rubocop-junit-formatter imcompatibility XML for JUnit formatter. ([@koic][])
* [#7767](https://github.com/rubocop-hq/rubocop/issues/7767): Skip array literals in `Style/HashTransformValues` and `Style/HashTransformKeys`. ([@tejasbubane][])

## 0.80.1 (2020-02-29)

Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/style/hash_transform_keys.rb
Expand Up @@ -31,7 +31,9 @@ class HashTransformKeys < Cop

def_node_matcher :on_bad_each_with_object, <<~PATTERN
(block
({send csend} !(send _ :each_with_index) :each_with_object (hash))
({send csend}
!{(send _ :each_with_index) (array ...)}
:each_with_object (hash))
(args
(mlhs
(arg $_)
Expand All @@ -55,7 +57,9 @@ class HashTransformKeys < Cop
def_node_matcher :on_bad_map_to_h, <<~PATTERN
({send csend}
(block
({send csend} !(send _ :each_with_index) {:map :collect})
({send csend}
!{(send _ :each_with_index) (array ...)}
{:map :collect})
(args
(arg $_)
(arg _val))
Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/style/hash_transform_values.rb
Expand Up @@ -31,7 +31,9 @@ class HashTransformValues < Cop

def_node_matcher :on_bad_each_with_object, <<~PATTERN
(block
({send csend} !(send _ :each_with_index) :each_with_object (hash))
({send csend}
!{(send _ :each_with_index) (array ...)}
:each_with_object (hash))
(args
(mlhs
(arg _key)
Expand All @@ -55,7 +57,9 @@ class HashTransformValues < Cop
def_node_matcher :on_bad_map_to_h, <<~PATTERN
({send csend}
(block
({send csend} !(send _ :each_with_index) {:map :collect})
({send csend}
!{(send _ :each_with_index) (array ...)}
{:map :collect})
(args
(arg _key)
(arg $_))
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/hash_transform_keys_spec.rb
Expand Up @@ -53,6 +53,12 @@
RUBY
end

it 'does not flag each_with_object when its receiver is array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].each_with_object({}) {|(k, v), h| h[foo(k)] = v}
RUBY
end

it 'flags _.map{...}.to_h when transform_keys could be used' do
expect_offense(<<~RUBY)
x.map {|k, v| [k.to_sym, v]}.to_h
Expand All @@ -79,6 +85,12 @@
expect_no_offenses('x.map {|k, v| [k.to_sym, v]}')
end

it 'does not flag key transformation when receiver is array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].map {|k, v| [k.to_sym, v]}.to_h
RUBY
end

it 'correctly autocorrects each_with_object' do
corrected = autocorrect_source(<<~RUBY)
{a: 1, b: 2}.each_with_object({}) do |(k, v), h|
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/hash_transform_values_spec.rb
Expand Up @@ -53,6 +53,12 @@
RUBY
end

it 'does not flag each_with_object when receiver is array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].each_with_object({}) {|(k, v), h| h[k] = foo(v)}
RUBY
end

it 'flags _.map {...}.to_h when transform_values could be used' do
expect_offense(<<~RUBY)
x.map {|k, v| [k, foo(v)]}.to_h
Expand All @@ -79,6 +85,12 @@
expect_no_offenses('x.map {|k, v| [k, foo(v)]}')
end

it 'does not flag value transformation when receiver is array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].map {|k, v| [k, foo(v)]}.to_h
RUBY
end

it 'correctly autocorrects each_with_object' do
corrected = autocorrect_source(<<~RUBY)
{a: 1, b: 2}.each_with_object({}) {|(k, v), h| h[k] = foo(v)}
Expand Down

0 comments on commit 03a58c9

Please sign in to comment.