From 05b41bd85fd88f8554d02be1510b2cf6302f4497 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 3 Feb 2021 02:31:19 +0900 Subject: [PATCH] [Fix #9490] Fix incorrect auto-correct for `Layout/FirstArgumentIndentation` Fixes #9490 and a regression of #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 --- ...ocorrect_for_first_argument_indentation.md | 1 + .../cop/layout/first_argument_indentation.rb | 2 +- spec/rubocop/cli/cli_autocorrect_spec.rb | 86 +++++++++++++++---- 3 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 changelog/fix_incorrect_autocorrect_for_first_argument_indentation.md diff --git a/changelog/fix_incorrect_autocorrect_for_first_argument_indentation.md b/changelog/fix_incorrect_autocorrect_for_first_argument_indentation.md new file mode 100644 index 000000000000..ad4a2c63b40c --- /dev/null +++ b/changelog/fix_incorrect_autocorrect_for_first_argument_indentation.md @@ -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][]) diff --git a/lib/rubocop/cop/layout/first_argument_indentation.rb b/lib/rubocop/cop/layout/first_argument_indentation.rb index c924187ff970..82d1246b9677 100644 --- a/lib/rubocop/cop/layout/first_argument_indentation.rb +++ b/lib/rubocop/cop/layout/first_argument_indentation.rb @@ -152,7 +152,7 @@ class FirstArgumentIndentation < Cop MSG = 'Indent the first argument one step more than %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 diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index 21680ee149a5..039582f56b5a 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -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