Skip to content

Commit

Permalink
[Fix rubocop#7778] Fix a false positive for Layout/EndAlignment
Browse files Browse the repository at this point in the history
Resolves rubocop#7778.

This PR resolves a false positive for `Layout/EndAlignment`
when a non-whitespace is used before the `end` keyword.

```console
% cat example.rb
if cond
else 'foo' end

% bundle exec rubocop -a --only Layout/EndAlignment
(snip)

Inspecting 1 file
W

Offenses:

example.rb:2:12: W: Layout/EndAlignment: end at 2, 11 is not aligned
with if at 1, 0.
else 'foo' end
           ^^^

1 file inspected, 1 offense detected
```

This PR changes to not offense it.

I think the responsibility will be clear if different (maybe new) cop like
`Layout/BlockEndNewline` cop handle the offense case.
So I think this role is preferably separated by `Layout/EndAlignment` cop.
  • Loading branch information
koic committed Mar 19, 2020
1 parent 69bc623 commit 4bfe82e
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 4bfe82e

Please sign in to comment.