diff --git a/CHANGELOG.md b/CHANGELOG.md index 2da74058908..981b5864727 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -4476,3 +4477,4 @@ [@knu]: https://github.com/knu [@saurabhmaurya15]: https://github.com/saurabhmaurya15 [@DracoAter]: https://github.com/DracoAter +[@diogoosorio]: https://github.com/diogoosorio diff --git a/lib/rubocop/cop/mixin/hash_transform_method.rb b/lib/rubocop/cop/mixin/hash_transform_method.rb index 0ad0c68b579..d2b5e0380c0 100644 --- a/lib/rubocop/cop/mixin/hash_transform_method.rb +++ b/lib/rubocop/cop/mixin/hash_transform_method.rb @@ -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 diff --git a/spec/rubocop/cop/style/hash_transform_keys_spec.rb b/spec/rubocop/cop/style/hash_transform_keys_spec.rb index f75aacaded8..a702eff24d8 100644 --- a/spec/rubocop/cop/style/hash_transform_keys_spec.rb +++ b/spec/rubocop/cop/style/hash_transform_keys_spec.rb @@ -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 diff --git a/spec/rubocop/cop/style/hash_transform_values_spec.rb b/spec/rubocop/cop/style/hash_transform_values_spec.rb index 6a8417ce12e..266ac53c4e4 100644 --- a/spec/rubocop/cop/style/hash_transform_values_spec.rb +++ b/spec/rubocop/cop/style/hash_transform_values_spec.rb @@ -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