Skip to content

Commit

Permalink
Fix an infinite loop error for Layout/EndAlignment
Browse files Browse the repository at this point in the history
Fixes standardrb/standard#598.

This PR fixes an infinite loop error for `Layout/EndAlignment`
when misaligned in singleton class assignments with `EnforcedStyleAlignWith: variable`.
  • Loading branch information
koic committed Dec 29, 2023
1 parent 229f205 commit 81f5138
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_layout_end_alignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12580](https://github.com/rubocop/rubocop/pull/12580): Fix an infinite loop error for `Layout/EndAlignment` when misaligned in singleton class assignments with `EnforcedStyleAlignWith: variable`. ([@koic][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/layout/end_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def on_class(node)
end

def on_sclass(node)
check_other_alignment(node)
if node.parent&.assignment?
check_asgn_alignment(node.parent, node)
else
check_other_alignment(node)
end
end

def on_module(node)
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3134,6 +3134,32 @@ class << self
RUBY
end

it 'corrects `Layout/EndAlignment` when `end` is not aligned with beginning of a singleton class assignment ' \
'and EnforcedStyleAlignWith is set to `variable` style' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
def thing
@thing ||= class << Object.new
end
end
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/EndAlignment:
EnforcedStyleAlignWith: variable
YAML

status = cli.run(%w[--autocorrect --only Layout/EndAlignment])
expect(status).to eq(0)
expect($stderr.string).to eq('')
expect(source_file.read).to eq(<<~RUBY)
def thing
@thing ||= class << Object.new
end
end
RUBY
end

it 'corrects `Layout/EndAlignment` when `end` is not aligned with start of line ' \
'and EnforcedStyleAlignWith is set to `start_of_line` style' do
source_file = Pathname('example.rb')
Expand Down

0 comments on commit 81f5138

Please sign in to comment.