Skip to content

Commit

Permalink
[Fix rubocop#9490] Fix incorrect auto-correct for `Layout/FirstArgume…
Browse files Browse the repository at this point in the history
…ntIndentation`

Fixes rubocop#9490 and a regression of rubocop#9486.

This PR fixes incorrect auto-correct for `Layout/FirstArgumentIndentation`
when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and
`EnforcedStyle: consistent` of `Layout/FirstArgumentIndentation`.

And this resolves the following Standard gem build error when upgrading to RuboCop 1.9.1.

```console
2) Failure:
Standard::CliTest#test_autocorrectable
[/home/runner/work/standard/standard/test/standard/cli_test.rb:14]:
--- expected
+++ actual
@@ -98,13 +98,13 @@
```

https://github.com/testdouble/standard/runs/1805630903
  • Loading branch information
koic committed Feb 3, 2021
1 parent e193c92 commit 05b41bd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
@@ -0,0 +1 @@
* [#9490](https://github.com/rubocop-hq/rubocop/issues/9490): Fix incorrect auto-correct for `Layout/FirstArgumentIndentation` when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and `EnforcedStyle: consistent` of `Layout/FirstArgumentIndentation`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/first_argument_indentation.rb
Expand Up @@ -152,7 +152,7 @@ class FirstArgumentIndentation < Cop
MSG = 'Indent the first argument one step more than %<base>s.'

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

indent = base_indentation(node) + configured_indentation_width
Expand Down
86 changes: 69 additions & 17 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -1688,35 +1688,87 @@ def self.some_method(foo, bar: 1)
RUBY
end

it 'does not crash `Layout/ArgumentAlignment` and offenses and accepts `Layout/FirstArgumentIndentation` ' \
'when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment`' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
%i[
consistent_relative_to_receiver
special_for_inner_method_call
special_for_inner_method_call_in_parentheses
].each do |style|
it 'does not crash `Layout/ArgumentAlignment` and offenses and accepts `Layout/FirstArgumentIndentation` ' \
'when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` ' \
"and `EnforcedStyle: #{style}` of `Layout/FirstArgumentIndentation`" do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
expect(response).to redirect_to(path(
obj1,
id: obj2.id
))
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation
Layout/FirstArgumentIndentation:
EnforcedStyle: #{style} # Not `EnforcedStyle: consistent`.
YAML

expect(cli.run([
'--auto-correct',
'--only',
'Layout/ArgumentAlignment,Layout/FirstArgumentIndentation'
])).to eq(0)
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
expect(response).to redirect_to(path(
obj1,
id: obj2.id
))
RUBY
end
end

expect(response).to redirect_to(path(
obj1,
id: obj2.id
))
it 'corrects when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and ' \
'`EnforcedStyle: consistent` of `Layout/FirstArgumentIndentation`' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
def do_even_more_stuff
foo = begin
do_stuff(
a: 1,
b: 2,
c: 3
)
rescue StandardError
nil
end
foo
end
RUBY

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

expect(cli.run([
'--auto-correct',
'--only',
'Layout/ArgumentAlignment,Layout/FirstArgumentIndentation'
])).to eq(0)
expect(cli.run(['--auto-correct'])).to eq(0)
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
expect(response).to redirect_to(path(
obj1,
id: obj2.id
))
def do_even_more_stuff
do_stuff(
a: 1,
b: 2,
c: 3
)
rescue StandardError
nil
end
RUBY
end

Expand Down

0 comments on commit 05b41bd

Please sign in to comment.