Skip to content

Commit

Permalink
Merge pull request #12407 from koic/fix_incorrect_autocorrect_for_lay…
Browse files Browse the repository at this point in the history
…out_single_line_block_chain_with_style_map_to_hash

Fix an incorrect autocorrect for `Style/MapToHash` with `Layout/SingleLineBlockChain`
  • Loading branch information
koic committed Nov 24, 2023
2 parents 5c81b56 + 84da432 commit 09cba51
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12407](https://github.com/rubocop/rubocop/pull/12407): Fix an incorrect autocorrect for `Style/MapToHash` with `Layout/SingleLineBlockChain`. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/layout/single_line_block_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class SingleLineBlockChain < Base

MSG = 'Put method call on a separate line if chained to a single line block.'

def self.autocorrect_incompatible_with
[Style::MapToHash]
end

def on_send(node)
range = offending_range(node)
add_offense(range) { |corrector| corrector.insert_before(range, "\n") } if range
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/map_to_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class MapToHash < Base
}
PATTERN

def self.autocorrect_incompatible_with
[Layout::SingleLineBlockChain]
end

def on_send(node)
return unless (to_h_node, map_node = map_to_h?(node))

Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2706,6 +2706,21 @@ def foo(a)
RUBY
end

it 'corrects `Style/MapToHash` and `Layout/SingleLineBlockChain` offenses' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
obj.map { |i| foo(i) }.to_h
.bar
RUBY

expect(cli.run(['-A', '--only', 'Style/MapToHash,Layout/SingleLineBlockChain'])).to eq(0)

expect(source_file.read).to eq(<<~RUBY)
obj.to_h { |i| foo(i) }
.bar
RUBY
end

it 'does not crash when using `Layout/CaseIndentation` and `Layout/ElseAlignment`' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
Expand Down

0 comments on commit 09cba51

Please sign in to comment.