Skip to content

Commit

Permalink
Merge pull request #11568 from j-miyake/fix_end_alignment
Browse files Browse the repository at this point in the history
[Fix #11567] Fix `Layout/EndAlignment` false negative
  • Loading branch information
koic committed Feb 12, 2023
2 parents 3507b51 + e09f89e commit 84ed5d9
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 38 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_layout_end_alignment_false_negative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11567](https://github.com/rubocop/rubocop/issues/11567): Fix `Layout/EndAlignment` false negative. ([@j-miyake][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/layout/end_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def on_class(node)
check_other_alignment(node)
end

def on_sclass(node)
check_other_alignment(node)
end

def on_module(node)
check_other_alignment(node)
end
Expand Down
75 changes: 75 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2823,4 +2823,79 @@ def bar; end
end
RUBY
end

it 'corrects `Layout/EndAlignment` when `end` is not aligned with beginning of a singleton class definition ' \
'and EnforcedStyleAlignWith is set to `keyword` style' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
class << self
end
puts 1; class << self
end
RUBY

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

status = cli.run(%w[--autocorrect --only Layout/EndAlignment])
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
class << self
end
puts 1; class << self
end
RUBY
end

it 'corrects `Layout/EndAlignment` when `end` is not aligned with beginning of a singleton class definition ' \
'and EnforcedStyleAlignWith is set to `variable` style' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
class << self
end
puts 1; class << self
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(source_file.read).to eq(<<~RUBY)
class << self
end
puts 1; class << self
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')
create_file(source_file, <<~RUBY)
class << self
end
puts 1; class << self
end
RUBY

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

status = cli.run(%w[--autocorrect --only Layout/EndAlignment])
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
class << self
end
puts 1; class << self
end
RUBY
end
end
97 changes: 59 additions & 38 deletions spec/rubocop/cop/layout/end_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@

include_examples 'aligned', "\xef\xbb\xbfclass", 'Test', 'end'

include_examples 'aligned', 'class', 'Test', 'end'
include_examples 'aligned', 'module', 'Test', 'end'
include_examples 'aligned', 'if', 'test', 'end'
include_examples 'aligned', 'unless', 'test', 'end'
include_examples 'aligned', 'while', 'test', 'end'
include_examples 'aligned', 'until', 'test', 'end'
include_examples 'aligned', 'case', 'a when b', 'end'
include_examples 'aligned', 'class', 'Test', 'end'
include_examples 'aligned', 'class << self;', 'Test', 'end'
include_examples 'aligned', 'module', 'Test', 'end'
include_examples 'aligned', 'if', 'test', 'end'
include_examples 'aligned', 'unless', 'test', 'end'
include_examples 'aligned', 'while', 'test', 'end'
include_examples 'aligned', 'until', 'test', 'end'
include_examples 'aligned', 'case', 'a when b', 'end'

include_examples 'misaligned', <<~RUBY, false
puts 1; class Test
end
^^^ `end` at 2, 2 is not aligned with `class` at 1, 8.
module Test
class Test
end
^^^ `end` at 2, 2 is not aligned with `module` at 1, 0.
^^^ `end` at 2, 2 is not aligned with `class` at 1, 0.
puts 1; class Test
puts 1; class << self
end
^^^ `end` at 2, 2 is not aligned with `class` at 1, 8.
class << self
end
^^^ `end` at 2, 2 is not aligned with `class` at 1, 0.
puts 1; module Test
end
^^^ `end` at 2, 2 is not aligned with `module` at 1, 8.
module Test
end
^^^ `end` at 2, 2 is not aligned with `module` at 1, 0.
Expand Down Expand Up @@ -71,13 +80,14 @@ module Test
^^^ `end` at 2, 2 is not aligned with `case` at 1, 0.
RUBY

include_examples 'aligned', 'puts 1; class', 'Test', ' end'
include_examples 'aligned', 'puts 1; module', 'Test', ' end'
include_examples 'aligned', 'puts 1; if', 'Test', ' end'
include_examples 'aligned', 'puts 1; unless', 'Test', ' end'
include_examples 'aligned', 'puts 1; while', 'Test', ' end'
include_examples 'aligned', 'puts 1; until', 'Test', ' end'
include_examples 'aligned', 'puts 1; case', 'a when b', ' end'
include_examples 'aligned', 'puts 1; class', 'Test', ' end'
include_examples 'aligned', 'puts 1; class << self;', 'Test', ' end'
include_examples 'aligned', 'puts 1; module', 'Test', ' end'
include_examples 'aligned', 'puts 1; if', 'Test', ' end'
include_examples 'aligned', 'puts 1; unless', 'Test', ' end'
include_examples 'aligned', 'puts 1; while', 'Test', ' end'
include_examples 'aligned', 'puts 1; until', 'Test', ' end'
include_examples 'aligned', 'puts 1; case', 'a when b', ' end'

it 'can handle ternary if' do
expect_no_offenses('a = cond ? x : y')
Expand All @@ -90,13 +100,14 @@ module Test
context 'when EnforcedStyleAlignWith is start_of_line' do
let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line', 'AutoCorrect' => true } }

include_examples 'aligned', 'puts 1; class', 'Test', 'end'
include_examples 'aligned', 'puts 1; module', 'Test', 'end'
include_examples 'aligned', 'puts 1; if', 'test', 'end'
include_examples 'aligned', 'puts 1; unless', 'test', 'end'
include_examples 'aligned', 'puts 1; while', 'test', 'end'
include_examples 'aligned', 'puts 1; until', 'test', 'end'
include_examples 'aligned', 'puts 1; case', 'a when b', 'end'
include_examples 'aligned', 'puts 1; class', 'Test', 'end'
include_examples 'aligned', 'puts 1; class << self;', 'Test', 'end'
include_examples 'aligned', 'puts 1; module', 'Test', 'end'
include_examples 'aligned', 'puts 1; if', 'test', 'end'
include_examples 'aligned', 'puts 1; unless', 'test', 'end'
include_examples 'aligned', 'puts 1; while', 'test', 'end'
include_examples 'aligned', 'puts 1; until', 'test', 'end'
include_examples 'aligned', 'puts 1; case', 'a when b', 'end'

include_examples 'misaligned', <<~RUBY, false
puts 1; class Test
Expand Down Expand Up @@ -235,6 +246,10 @@ module Test
end
^^^ `end` at 2, 7 is not aligned with `module` at 1, 0.
class << self
end
^^^ `end` at 2, 2 is not aligned with `class` at 1, 0.
if test
end
^^^ `end` at 2, 2 is not aligned with `if` at 1, 0.
Expand All @@ -256,13 +271,14 @@ module Test
^^^ `end` at 2, 2 is not aligned with `case` at 1, 0.
RUBY

include_examples 'aligned', 'class', 'Test', 'end'
include_examples 'aligned', 'module', 'Test', 'end'
include_examples 'aligned', 'if', 'test', 'end'
include_examples 'aligned', 'unless', 'test', 'end'
include_examples 'aligned', 'while', 'test', 'end'
include_examples 'aligned', 'until', 'test', 'end'
include_examples 'aligned', 'case', 'a when b', 'end'
include_examples 'aligned', 'class', 'Test', 'end'
include_examples 'aligned', 'class << self;', 'Test', 'end'
include_examples 'aligned', 'module', 'Test', 'end'
include_examples 'aligned', 'if', 'test', 'end'
include_examples 'aligned', 'unless', 'test', 'end'
include_examples 'aligned', 'while', 'test', 'end'
include_examples 'aligned', 'until', 'test', 'end'
include_examples 'aligned', 'case', 'a when b', 'end'

include_examples 'misaligned', <<~RUBY, :start_of_line
puts 1; class Test
Expand All @@ -273,6 +289,10 @@ module Test
end
^^^ `end` at 2, 0 is not aligned with `module` at 1, 8.
puts 1; class << self
end
^^^ `end` at 2, 0 is not aligned with `class` at 1, 8.
puts 1; if test
end
^^^ `end` at 2, 0 is not aligned with `if` at 1, 8.
Expand All @@ -294,13 +314,14 @@ module Test
^^^ `end` at 2, 0 is not aligned with `case` at 1, 8.
RUBY

include_examples 'aligned', 'puts 1; class', 'Test', ' end'
include_examples 'aligned', 'puts 1; module', 'Test', ' end'
include_examples 'aligned', 'puts 1; if', 'Test', ' end'
include_examples 'aligned', 'puts 1; unless', 'Test', ' end'
include_examples 'aligned', 'puts 1; while', 'Test', ' end'
include_examples 'aligned', 'puts 1; until', 'Test', ' end'
include_examples 'aligned', 'puts 1; case', 'a when b', ' end'
include_examples 'aligned', 'puts 1; class', 'Test', ' end'
include_examples 'aligned', 'puts 1; class << self;', 'Test', ' end'
include_examples 'aligned', 'puts 1; module', 'Test', ' end'
include_examples 'aligned', 'puts 1; if', 'Test', ' end'
include_examples 'aligned', 'puts 1; unless', 'Test', ' end'
include_examples 'aligned', 'puts 1; while', 'Test', ' end'
include_examples 'aligned', 'puts 1; until', 'Test', ' end'
include_examples 'aligned', 'puts 1; case', 'a when b', ' end'

it 'register an offense when using `+` operator method and `end` is not aligned' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 84ed5d9

Please sign in to comment.