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 #10830] Fix an incorrect autocorrect for Layout/FirstArgumentIndentation #10840

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
@@ -0,0 +1 @@
* [#10830](https://github.com/rubocop/rubocop/issues/10830): Fix an incorrect autocorrect for `Layout/FirstArgumentIndentation` when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and `EnforcedStyle: consistent` of `Layout/FirstArgumentIndentation` and enabling `Layout/FirstMethodArgumentLineBreak`. ([@koic][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/layout/first_argument_indentation.rb
Expand Up @@ -153,7 +153,8 @@ class FirstArgumentIndentation < Base
MSG = 'Indent the first argument one step more than %<base>s.'

def on_send(node)
return if style != :consistent && enforce_first_argument_with_fixed_indentation?
return if style != :consistent && enforce_first_argument_with_fixed_indentation? &&
!enable_layout_first_method_argument_line_break?
return if !node.arguments? || bare_operator?(node) || node.setter_method?

indent = base_indentation(node) + configured_indentation_width
Expand Down Expand Up @@ -267,6 +268,10 @@ def enforce_first_argument_with_fixed_indentation?
argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
end

def enable_layout_first_method_argument_line_break?
config.for_cop('Layout/FirstMethodArgumentLineBreak')['Enabled']
end

def argument_alignment_config
config.for_cop('Layout/ArgumentAlignment')
end
Expand Down
41 changes: 41 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -1887,6 +1887,47 @@ def self.some_method(foo, bar: 1)
end
end

it 'registers an offense and corrects when using `Layout/ArgumentAlignment`, `Layout/FirstArgumentIndentation`, and `Layout/FirstMethodArgumentLineBreak` ' \
'and specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` ' \
'and `EnforcedStyle: consistent` of `Layout/FirstArgumentIndentation`' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true

it do
expect(do_something).to eq([
'foo',
'bar'
])
end
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation
Layout/FirstArgumentIndentation:
EnforcedStyle: consistent
Layout/FirstMethodArgumentLineBreak:
Enabled: true
YAML

expect(cli.run(['--autocorrect', '--only', %w[
Layout/ArgumentAlignment Layout/FirstArgumentIndentation Layout/FirstMethodArgumentLineBreak
Layout/IndentationWidth
].join(',')])).to eq(0)
expect($stderr.string).to eq('')
expect(File.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true

it do
expect(do_something).to eq(
[
'foo',
'bar'
])
end
RUBY
end

it 'corrects when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and ' \
'`EnforcedStyle: consistent` of `Layout/FirstArgumentIndentation`' do
create_file('example.rb', <<~RUBY)
Expand Down