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 an incorrect autocorrect for hash transform methods #7903

Merged
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
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