-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for variable alignment to Layout/RescueEnsureAlignment #7531
Closed
dylanahsmith
wants to merge
1
commit into
rubocop:master
from
dylanahsmith:rescue-ensure-style-config
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::RescueEnsureAlignment, :config do | ||
let(:cop_config) do | ||
{ 'EnforcedStyleAlignWith' => 'keyword', 'AutoCorrect' => true } | ||
end | ||
|
||
it 'accepts the modifier form' do | ||
expect_no_offenses('test rescue nil') | ||
end | ||
|
||
context 'rescue with begin' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
begin | ||
something | ||
rescue | ||
^^^^^^ `rescue` at 3, 4 is not aligned with `begin` at 1, 0. | ||
error | ||
end | ||
it 'rescue not aligned with begin registers an offense' do | ||
expect_offense(<<~RUBY) | ||
begin | ||
something | ||
rescue | ||
^^^^^^ `rescue` at 3, 4 is not aligned with `begin` at 1, 0. | ||
error | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
begin | ||
something | ||
rescue | ||
error | ||
end | ||
RUBY | ||
end | ||
|
||
context 'on assignment with keyword style' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyleAlignWith' => 'keyword', 'AutoCorrect' => true } | ||
end | ||
|
||
it 'accepts multi-line, aligned with keyword' do | ||
expect_no_offenses(<<~RUBY) | ||
x ||= begin | ||
1 | ||
rescue | ||
2 | ||
end | ||
RUBY | ||
end | ||
|
||
expect_correction(<<~RUBY) | ||
begin | ||
something | ||
it 'accepts multi-line, indented' do | ||
expect_no_offenses(<<~RUBY) | ||
x ||= | ||
begin | ||
1 | ||
rescue | ||
2 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense for incorrect alignment' do | ||
expect_offense(<<~RUBY) | ||
x ||= begin | ||
1 | ||
rescue | ||
error | ||
^^^^^^ `rescue` at 3, 0 is not aligned with `begin` at 1, 6. | ||
2 | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'as RHS of assignment' do | ||
it 'accepts multi-line, aligned' do | ||
expect_no_offenses(<<~RUBY) | ||
x ||= begin | ||
1 | ||
rescue | ||
2 | ||
end | ||
RUBY | ||
end | ||
context 'on assignment with variable style' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyleAlignWith' => 'variable', 'AutoCorrect' => true } | ||
end | ||
|
||
it 'accepts multi-line, indented' do | ||
expect_no_offenses(<<~RUBY) | ||
x ||= | ||
begin | ||
1 | ||
rescue | ||
2 | ||
end | ||
RUBY | ||
end | ||
it 'accepts multi-line, aligned with variable' do | ||
expect_no_offenses(<<~RUBY) | ||
x ||= begin | ||
1 | ||
rescue | ||
2 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense for incorrect alignment' do | ||
expect_offense(<<~RUBY) | ||
x ||= begin | ||
it 'registers offense for multi-line, indented' do | ||
expect_no_offenses(<<~RUBY) | ||
x ||= | ||
begin | ||
1 | ||
rescue | ||
^^^^^^ `rescue` at 3, 0 is not aligned with `begin` at 1, 6. | ||
2 | ||
end | ||
RUBY | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense for keyword alignment' do | ||
expect_offense(<<~RUBY) | ||
x ||= begin | ||
1 | ||
rescue | ||
^^^^^^ `rescue` at 3, 6 is not aligned with `x` at 1, 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
2 | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
|
@@ -286,16 +330,6 @@ def method2 | |
RUBY | ||
end | ||
|
||
it 'accepts correctly aligned rescue in assigned begin-end block' do | ||
expect_no_offenses(<<-RUBY) | ||
foo = begin | ||
bar | ||
rescue BazError | ||
qux | ||
end | ||
RUBY | ||
end | ||
|
||
context '>= Ruby 2.5', :ruby25 do | ||
it 'accepts aligned rescue in do-end block' do | ||
expect_no_offenses(<<~RUBY) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simply
EnforcedStyle
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looked like the alignment cops were using
EnforcedStyleAlignWith
, so I was just trying to be consistent with them. Why not simplyEnforcedStyle
forLayout/BlockAlignment
,Layout/DefEndAlignment
,Layout/EndAlignment
andLayout/RescueEnsureAlignment
? I'm fine either way. Did you want me to change it toEnforcedStyle
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is my experience/expectation as well, FWIW
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I no longer remember why we did it like this in the past, but I definitely don't like it today. Probably it was to avoid styles named
align_with_*
, but I think that's fine.