Skip to content

Commit 6e5c599

Browse files
authored
Merge pull request #14474 from lovro-bikic/false-negative-end-alignment
Fix false negative for `Layout/EndAlignment` when `end` is not on a separate line
2 parents 103f3ab + 49b7248 commit 6e5c599

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14474](https://github.com/rubocop/rubocop/pull/14474): Fix false negative for `Layout/EndAlignment` when `end` is not on a separate line. ([@lovro-bikic][])

lib/rubocop/cop/correctors/alignment_corrector.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ def correct(corrector, processed_source, node, column_delta)
2929
def align_end(corrector, processed_source, node, align_to)
3030
@processed_source = processed_source
3131
whitespace = whitespace_range(node)
32-
return false unless whitespace.source.strip.empty?
33-
3432
column = alignment_column(align_to)
35-
corrector.replace(whitespace, ' ' * column)
33+
34+
if whitespace.source.strip.empty?
35+
corrector.replace(whitespace, ' ' * column)
36+
else
37+
corrector.insert_after(whitespace, "\n#{' ' * column}")
38+
end
3639
end
3740

3841
private

lib/rubocop/cop/mixin/end_keyword_alignment.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def check_end_kw_in_node(node)
1919
def check_end_kw_alignment(node, align_ranges)
2020
return if ignored_node?(node)
2121

22-
end_loc = node.loc.end
23-
return if accept_end_kw_alignment?(end_loc)
22+
return unless (end_loc = node.loc.end)
2423

2524
matching = matching_ranges(end_loc, align_ranges)
2625

@@ -57,11 +56,6 @@ def add_offense_for_misalignment(node, align_with)
5756
add_offense(end_loc, message: msg) { |corrector| autocorrect(corrector, node) }
5857
end
5958

60-
def accept_end_kw_alignment?(end_loc)
61-
end_loc.nil? || # Discard modifier forms of if/while/until.
62-
!/\A[ \t]*end/.match?(processed_source.lines[end_loc.line - 1])
63-
end
64-
6559
def style_parameter_name
6660
'EnforcedStyleAlignWith'
6761
end

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,8 @@ module A module B
16271627
16281628
module A
16291629
module B
1630-
end end
1630+
end
1631+
end
16311632
RUBY
16321633
uncorrected = $stdout.string.split($RS).select do |line|
16331634
line.include?('example.rb:') && !line.include?('[Corrected]')

spec/rubocop/cop/layout/end_alignment_spec.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,37 @@ class << self
503503
end
504504

505505
context 'when end is preceded by something else than whitespace' do
506-
it 'does not register an offense' do
507-
expect_no_offenses(<<~RUBY)
506+
it 'registers an offense and corrects' do
507+
expect_offense(<<~RUBY)
508508
module A
509509
puts a end
510+
^^^ `end` at 2, 7 is not aligned with `module` at 1, 0.
511+
RUBY
512+
513+
expect_correction(<<~RUBY)
514+
module A
515+
puts a#{trailing_whitespace}
516+
end
517+
RUBY
518+
end
519+
end
520+
521+
context 'when end is on the same line as `else` body' do
522+
it 'registers an offense and corrects' do
523+
expect_offense(<<~RUBY)
524+
if test
525+
foo
526+
else
527+
bar end
528+
^^^ `end` at 4, 6 is not aligned with `if` at 1, 0.
529+
RUBY
530+
531+
expect_correction(<<~RUBY)
532+
if test
533+
foo
534+
else
535+
bar#{trailing_whitespace}
536+
end
510537
RUBY
511538
end
512539
end

0 commit comments

Comments
 (0)