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 Style/MapToHash with Layout/SingleLineBlockChain #12407

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