diff --git a/changelog/fix_error_for_rescue_ensure_alignment.md b/changelog/fix_error_for_rescue_ensure_alignment.md new file mode 100644 index 00000000000..696466b4ec6 --- /dev/null +++ b/changelog/fix_error_for_rescue_ensure_alignment.md @@ -0,0 +1 @@ +* [#10017](https://github.com/rubocop/rubocop/pull/10017): Fixan error for `Layout/RescueEnsureAlignment` when using zsuper with block. ([@koic][]) diff --git a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb index 276a52d81d1..ff2c1be1165 100644 --- a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +++ b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb @@ -137,17 +137,18 @@ def aligned_with_line_break_method?(ancestor_node, node) send_node_loc = ancestor_node.send_node.loc do_keyword_line = ancestor_node.loc.begin.line rescue_keyword_column = node.loc.keyword.column - selector = send_node_loc.selector + selector = send_node_loc.respond_to?(:selector) ? send_node_loc.selector : send_node_loc - if send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot) && - aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column) + if aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column) return true end do_keyword_line == selector.line && rescue_keyword_column == selector.column end - def aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column) + def aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column) + return false unless send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot) + do_keyword_line == dot.line && rescue_keyword_column == dot.column end diff --git a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb index 0882160c774..f0df363d22c 100644 --- a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb +++ b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb @@ -769,6 +769,37 @@ def foo end end + context 'when using zsuper with block' do + it 'registers and corrects an offense and corrects when incorrect alignment' do + expect_offense(<<~RUBY) + super do + nil + ensure + ^^^^^^ `ensure` at 3, 4 is not aligned with `super do` at 1, 0. + nil + end + RUBY + + expect_correction(<<~RUBY) + super do + nil + ensure + nil + end + RUBY + end + + it 'does not register an offense when correct alignment' do + expect_no_offenses(<<~RUBY) + super do + nil + ensure + nil + end + RUBY + end + end + describe 'excluded file', :config do let(:config) do RuboCop::Config.new('Layout/RescueEnsureAlignment' =>