From 4c5f485bec514e10b29277f6b1b61dd45cecdd39 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 6 Oct 2021 11:10:26 +0200 Subject: [PATCH] Style/HashTransform*: Fix incorrect auto-correction when inside block body The special case was checking for a block *being passed to* our matched `node` (i.e. the `to_h` call), without excluding the case of the `node` being *wrapped by* a block. --- .../fix_stylehashtransform_fix_incorrect.md | 3 +++ .../cop/mixin/hash_transform_method.rb | 6 +++--- .../cop/style/hash_transform_keys_spec.rb | 19 +++++++++++++++++++ .../cop/style/hash_transform_values_spec.rb | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_stylehashtransform_fix_incorrect.md diff --git a/changelog/fix_stylehashtransform_fix_incorrect.md b/changelog/fix_stylehashtransform_fix_incorrect.md new file mode 100644 index 00000000000..f466a2b31b1 --- /dev/null +++ b/changelog/fix_stylehashtransform_fix_incorrect.md @@ -0,0 +1,3 @@ +* [#10171](https://github.com/rubocop/rubocop/pull/10171): Fix `Style/HashTransformKeys` and `Style/HashTransformValues` incorrect auto-correction when inside block body. ([@franzliedke][]) + +[@franzliedke]: https://github.com/franzliedke diff --git a/lib/rubocop/cop/mixin/hash_transform_method.rb b/lib/rubocop/cop/mixin/hash_transform_method.rb index 68df99b76ba..c449ad2b07e 100644 --- a/lib/rubocop/cop/mixin/hash_transform_method.rb +++ b/lib/rubocop/cop/mixin/hash_transform_method.rb @@ -139,9 +139,9 @@ def self.from_hash_brackets_map(node, match) end def self.from_map_to_h(node, match) - strip_trailing_chars = 0 - - unless node.parent&.block_type? + if node.parent&.block_type? && node.parent.send_node == node + strip_trailing_chars = 0 + else map_range = node.children.first.source_range node_range = node.source_range strip_trailing_chars = node_range.end_pos - map_range.end_pos diff --git a/spec/rubocop/cop/style/hash_transform_keys_spec.rb b/spec/rubocop/cop/style/hash_transform_keys_spec.rb index 7e7017a5e38..6a538e56807 100644 --- a/spec/rubocop/cop/style/hash_transform_keys_spec.rb +++ b/spec/rubocop/cop/style/hash_transform_keys_spec.rb @@ -117,6 +117,25 @@ RUBY end + it 'flags _.map {...}.to_h when transform_keys could be used when wrapped in another block' do + expect_offense(<<~RUBY) + wrapping do + x.map do |k, v| + ^^^^^^^^^^^^^^^ Prefer `transform_keys` over `map {...}.to_h`. + [k.to_sym, v] + end.to_h + end + RUBY + + expect_correction(<<~RUBY) + wrapping do + x.transform_keys do |k| + k.to_sym + end + end + 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 0f3ec021226..cb366e7987d 100644 --- a/spec/rubocop/cop/style/hash_transform_values_spec.rb +++ b/spec/rubocop/cop/style/hash_transform_values_spec.rb @@ -117,6 +117,25 @@ RUBY end + it 'flags _.map {...}.to_h when transform_values could be used when wrapped in another block' do + expect_offense(<<~RUBY) + wrapping do + x.map do |k, v| + ^^^^^^^^^^^^^^^ Prefer `transform_values` over `map {...}.to_h`. + [k, v.to_s] + end.to_h + end + RUBY + + expect_correction(<<~RUBY) + wrapping do + x.transform_values do |v| + v.to_s + end + end + 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