Skip to content

Commit

Permalink
Fix an incorrect autocorrect for hash transform methods
Browse files Browse the repository at this point in the history
Follow up to rubocop/rubocop-rails#239.

This PR fixes an incorrect autocorrect for `Style/HashTransformKeys` and
`Style/HashTransformValues` cops when line break before `to_h` method.

In the future, duplicate code in RuboCop Rails may be removed,
making it dependent on RuboCop core code. It centralizes the resolution
of problems.

Co-authored-by: Diogo Osório <diogo.osorio@marleyspoon.com>
  • Loading branch information
koic and Diogo Osório committed Apr 24, 2020
1 parent 9d51e4f commit 41eae31
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#7881](https://github.com/rubocop-hq/rubocop/issues/7881): Fix `--parallel` and `--force-default-config` combination. ([@jonas054][])
* [#7635](https://github.com/rubocop-hq/rubocop/issues/7635): Fix a false positive for `Style/MultilineWhenThen` when `then` required for a body of `when` is used. ([@koic][])
* [#7905](https://github.com/rubocop-hq/rubocop/pull/7905): Fix an error when running `rubocop --only` or `rubocop --except` options without cop name argument. ([@koic][])
* [#7903](https://github.com/rubocop-hq/rubocop/pull/7903): Fix an incorrect autocorrect for `Style/HashTransformKeys` and `Style/HashTransformValues` cops when line break before `to_h` method. ([@diogoosorio][], [@koic][])

### Changes

Expand Down Expand Up @@ -4476,3 +4477,4 @@
[@knu]: https://github.com/knu
[@saurabhmaurya15]: https://github.com/saurabhmaurya15
[@DracoAter]: https://github.com/DracoAter
[@diogoosorio]: https://github.com/diogoosorio
9 changes: 8 additions & 1 deletion lib/rubocop/cop/mixin/hash_transform_method.rb
Expand Up @@ -132,7 +132,14 @@ def self.from_hash_brackets_map(node, match)
end

def self.from_map_to_h(node, match)
strip_trailing_chars = node.parent&.block_type? ? 0 : '.to_h'.length
strip_trailing_chars = 0

unless node.parent&.block_type?
map_range = node.children.first.source_range
node_range = node.source_range
strip_trailing_chars = node_range.end_pos - map_range.end_pos
end

new(match, node.children.first, 0, strip_trailing_chars)
end

Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/hash_transform_keys_spec.rb
Expand Up @@ -66,6 +66,19 @@
RUBY
end

it 'flags _.map{...}.to_h when transform_keys could be used ' \
'when line break before `to_h`' do
expect_offense(<<~RUBY)
x.map {|k, v| [k.to_sym, v]}.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `transform_keys` over `map {...}.to_h`.
to_h
RUBY

expect_correction(<<~RUBY)
x.transform_keys {|k| k.to_sym}
RUBY
end

it 'does not flag _.map{...}.to_h when both key & value are transformed' do
expect_no_offenses('x.map {|k, v| [k.to_sym, foo(v)]}.to_h')
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/hash_transform_values_spec.rb
Expand Up @@ -65,6 +65,19 @@
RUBY
end

it 'flags _.map {...}.to_h when transform_values could be used ' \
'when line break before `to_h`' do
expect_offense(<<~RUBY)
x.map {|k, v| [k, foo(v)]}.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `transform_values` over `map {...}.to_h`.
to_h
RUBY

expect_correction(<<~RUBY)
x.transform_values {|v| foo(v)}
RUBY
end

it 'does not flag _.map{...}.to_h when both key & value are transformed' do
expect_no_offenses('x.map {|k, v| [k.to_sym, foo(v)]}.to_h')
end
Expand Down

0 comments on commit 41eae31

Please sign in to comment.