From 650a10f4cdc0d16984121675fcc441984f92ad47 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 28 Jul 2022 15:13:51 +0900 Subject: [PATCH] [Fix #10830] Fix an incorrect autocorrect for `Layout/FirstArgumentIndentation` Fixes #10830. This PR fixes 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`. --- ...t_for_layout_first_argument_indentation.md | 1 + .../cop/layout/first_argument_indentation.rb | 7 +++- spec/rubocop/cli/autocorrect_spec.rb | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_incorrect_autocorrect_for_layout_first_argument_indentation.md diff --git a/changelog/fix_an_incorrect_autocorrect_for_layout_first_argument_indentation.md b/changelog/fix_an_incorrect_autocorrect_for_layout_first_argument_indentation.md new file mode 100644 index 00000000000..5472cafff7d --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_layout_first_argument_indentation.md @@ -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][]) diff --git a/lib/rubocop/cop/layout/first_argument_indentation.rb b/lib/rubocop/cop/layout/first_argument_indentation.rb index 1d19baf2608..8b6e1b45c71 100644 --- a/lib/rubocop/cop/layout/first_argument_indentation.rb +++ b/lib/rubocop/cop/layout/first_argument_indentation.rb @@ -153,7 +153,8 @@ class FirstArgumentIndentation < Base MSG = 'Indent the first argument one step more than %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 @@ -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 diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index ba499f1b8b9..129286a86ad 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -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)