Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #7767] Skip array literals in Style/HashTransformValues & Style/HashTransformKeys #7787

Merged
merged 1 commit into from Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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