Skip to content

Commit

Permalink
Merge pull request #7794 from koic/fix_a_false_positive_for_end_keywo…
Browse files Browse the repository at this point in the history
…rd_alignment

[Fix #7778] Fix a false positive for `Layout/EndAlignment`
  • Loading branch information
koic committed Mar 19, 2020
2 parents 69bc623 + 4bfe82e commit 8de00a9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
* [#7733](https://github.com/rubocop-hq/rubocop/issues/7733): Fix rubocop-junit-formatter imcompatibility XML for JUnit formatter. ([@koic][])
* [#7767](https://github.com/rubocop-hq/rubocop/issues/7767): Skip array literals in `Style/HashTransformValues` and `Style/HashTransformKeys`. ([@tejasbubane][])
* [#7791](https://github.com/rubocop-hq/rubocop/issues/7791): Fix an error on auto-correction for `Layout/BlockEndNewline` when `}` of multiline block without processing is not on its own line. ([@koic][])
* [#7778](https://github.com/rubocop-hq/rubocop/issues/7778): Fix a false positive for `Layout/EndAlignment` when a non-whitespace is used before the `end` keyword. ([@koic][])

### Changes

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/mixin/end_keyword_alignment.rb
Expand Up @@ -20,7 +20,7 @@ def check_end_kw_alignment(node, align_ranges)
return if ignored_node?(node)

end_loc = node.loc.end
return unless end_loc # Discard modifier forms of if/while/until.
return if accept_end_kw_alignment?(end_loc)

matching = matching_ranges(end_loc, align_ranges)

Expand Down Expand Up @@ -49,6 +49,11 @@ def add_offense_for_misalignment(node, align_with)
add_offense(node, location: end_loc, message: msg)
end

def accept_end_kw_alignment?(end_loc)
end_loc.nil? || # Discard modifier forms of if/while/until.
processed_source.lines[end_loc.line - 1] !~ /\A[ \t]*end/
end

def style_parameter_name
'EnforcedStyleAlignWith'
end
Expand Down
10 changes: 2 additions & 8 deletions spec/rubocop/cop/layout/end_alignment_spec.rb
Expand Up @@ -277,14 +277,8 @@ module Test
end

context 'when end is preceded by something else than whitespace' do
it 'registers an offense and does not correct' do
expect_offense(<<~RUBY)
module A
puts a end
^^^ `end` at 2, 7 is not aligned with `module` at 1, 0.
RUBY

expect_correction(<<~RUBY)
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
module A
puts a end
RUBY
Expand Down

0 comments on commit 8de00a9

Please sign in to comment.