From 2b8b5173158e0c04ec79d9f30135f9f29232431e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 30 Jul 2021 00:47:34 +0900 Subject: [PATCH] [Fix #9953] Fix a false auto-correction behavior for `Layout/EndAlignment` Fixes #9953. Fix an infinite loop error and a false auto-correction behavior for `Layout/EndAlignment` when using a conditional statement in a method argument. --- ...autocorrection_for_layout_end_alignment.md | 1 + lib/rubocop/cop/layout/end_alignment.rb | 3 ++- spec/rubocop/cop/layout/end_alignment_spec.rb | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_a_false_autocorrection_for_layout_end_alignment.md diff --git a/changelog/fix_a_false_autocorrection_for_layout_end_alignment.md b/changelog/fix_a_false_autocorrection_for_layout_end_alignment.md new file mode 100644 index 00000000000..81ee0881766 --- /dev/null +++ b/changelog/fix_a_false_autocorrection_for_layout_end_alignment.md @@ -0,0 +1 @@ +* [#9953](https://github.com/rubocop/rubocop/issues/9953): Fix an infinite loop error and a false auto-correction behavior for `Layout/EndAlignment` when using a conditional statement in a method argument. ([@koic][]) diff --git a/lib/rubocop/cop/layout/end_alignment.rb b/lib/rubocop/cop/layout/end_alignment.rb index 07c58af1df4..598563a15d1 100644 --- a/lib/rubocop/cop/layout/end_alignment.rb +++ b/lib/rubocop/cop/layout/end_alignment.rb @@ -165,7 +165,8 @@ def alignment_node(node) end def alignment_node_for_variable_style(node) - return node.parent if node.case_type? && node.argument? + return node.parent if node.case_type? && node.argument? && + node.loc.line == node.parent.loc.line assignment = assignment_or_operator_method(node) diff --git a/spec/rubocop/cop/layout/end_alignment_spec.rb b/spec/rubocop/cop/layout/end_alignment_spec.rb index a7d3d5f8285..7b3892c7844 100644 --- a/spec/rubocop/cop/layout/end_alignment_spec.rb +++ b/spec/rubocop/cop/layout/end_alignment_spec.rb @@ -339,6 +339,31 @@ module Test end RUBY end + + it 'register an offense when using a conditional statement in a method argument and `end` is not aligned' do + expect_offense(<<~RUBY) + format( + case condition + when foo + bar + else + baz + end, qux + ^^^ `end` at 7, 0 is not aligned with `case` at 2, 2. + ) + RUBY + + expect_correction(<<~RUBY) + format( + case condition + when foo + bar + else + baz + end, qux + ) + RUBY + end end context 'correct + opposite' do