Skip to content

Commit

Permalink
Merge pull request #10840 from koic/fix_an_incorrect_autocorrect_for_…
Browse files Browse the repository at this point in the history
…layout_first_argument_indentation

[Fix #10830] Fix an incorrect autocorrect for `Layout/FirstArgumentIndentation`
  • Loading branch information
koic committed Jul 28, 2022
2 parents cbd4732 + 650a10f commit 7dee99a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
@@ -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

0 comments on commit 7dee99a

Please sign in to comment.