Skip to content

Commit

Permalink
Merge pull request #8719 from koic/fix_false_positive_for_rescue_ensu…
Browse files Browse the repository at this point in the history
…re_alignment

[Fix #8710] Fix a false positive for `Layout/RescueEnsureAlignment`
  • Loading branch information
koic committed Sep 15, 2020
2 parents 0b3c840 + 5b98cfb commit 07c21d3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#8720](https://github.com/rubocop-hq/rubocop/issues/8720): Fix an error for `Lint/IdentityComparison` when calling `object_id` method without receiver in LHS or RHS. ([@koic][])
* [#8710](https://github.com/rubocop-hq/rubocop/issues/8710): Fix a false positive for `Layout/RescueEnsureAlignment` when `Layout/BeginEndAlignment` cop is not enabled status. ([@koic][])

## 0.91.0 (2020-09-15)

Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Expand Up @@ -191,7 +191,16 @@ def alignment_location(alignment_node)
end

def begin_end_alignment_style
config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
# FIXME: Workaround for pending status for `Layout/BeginEndAlignment` cop
# When RuboCop 1.0 is released, please replace it with the following condition.
#
# config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
# config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
if config.for_all_cops['NewCops'] == 'enable' ||
config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
config.for_cop('Layout/BeginEndAlignment')['Enabled'] != 'pending'
config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
end
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Expand Up @@ -30,6 +30,58 @@
{ 'EnforcedStyle' => 'require_parentheses' }
end

context '`Layout/BeginEndAlignment` cop is not enabled' do
let(:other_cops) do
{
'Layout/BeginEndAlignment' => {
'Enabled' => false,
'EnforcedStyleAlignWith' => 'start_of_line'
}
}
end

it 'accepts multi-line, aligned' do
expect_no_offenses(<<~RUBY)
x ||= begin
1
rescue
2
end
RUBY
end

it 'accepts multi-line, indented' do
expect_no_offenses(<<~RUBY)
x ||=
begin
1
rescue
2
end
RUBY
end

it 'registers an offense and corrects for incorrect alignment' do
expect_offense(<<~RUBY)
x ||= begin
1
rescue
^^^^^^ `rescue` at 3, 0 is not aligned with `begin` at 1, 6.
2
end
RUBY

# Except for `rescue`, it will be aligned by `Layout/BeginEndAlignment` auto-correction.
expect_correction(<<~RUBY)
x ||= begin
1
rescue
2
end
RUBY
end
end

context 'when `EnforcedStyleAlignWith: start_of_line` of `Layout/BeginEndAlignment` cop' do
let(:other_cops) do
{
Expand Down

0 comments on commit 07c21d3

Please sign in to comment.