Skip to content
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

Fix assignment edge-cases in Layout/MultilineAssignmentLayout #7438

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#7439](https://github.com/rubocop-hq/rubocop/issues/7439): Make `Style/FormatStringToken` ignore percent escapes (`%%`). ([@buehmann][])
* [#7438](https://github.com/rubocop-hq/rubocop/issues/7438): Fix assignment edge-cases in `Layout/MultilineAssignmentLayout`. ([@gsamokovarov][])
koic marked this conversation as resolved.
Show resolved Hide resolved

## 0.75.1 (2019-10-14)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/multiline_assignment_layout.rb
Expand Up @@ -43,7 +43,7 @@ class MultilineAssignmentLayout < Cop
'on the same line as the assignment operator `=`.'

def check_assignment(node, rhs)
return if node.send_type?
return if node.send_type? && node.loc.operator&.source != '='
return unless rhs
return unless supported_types.include?(rhs.type)
return if rhs.first_line == rhs.last_line
Expand Down
61 changes: 51 additions & 10 deletions spec/rubocop/cop/layout/multiline_assignment_layout_spec.rb
Expand Up @@ -21,16 +21,37 @@
^^^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`.
end
RUBY

expect_correction(<<~RUBY)
blarg =
if true
end
RUBY
end

it 'auto-corrects offenses' do
new_source = autocorrect_source(<<~RUBY)
blarg = if true
it 'registers an offense when the rhs is on the same line in []=' do
expect_offense(<<~RUBY)
hash[:foo] = if true
^^^^^^^^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`.
end
RUBY

expect(new_source).to eq(<<~RUBY)
blarg =
expect_correction(<<~RUBY)
hash[:foo] =
if true
end
RUBY
end

it 'registers an offense when the rhs is on the same line in setters' do
expect_offense(<<~RUBY)
foo.bar = if true
^^^^^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`.
end
RUBY

expect_correction(<<~RUBY)
foo.bar =
if true
end
gsamokovarov marked this conversation as resolved.
Show resolved Hide resolved
RUBY
Expand Down Expand Up @@ -123,17 +144,37 @@
if true
end
RUBY

expect_correction(<<~RUBY)
blarg = if true
end
RUBY
end

it 'auto-corrects offenses' do
new_source = autocorrect_source(<<~RUBY)
blarg =
it 'registers an offense when the rhs is a different line in []=' do
expect_offense(<<~RUBY)
hash[:foo] =
^^^^^^^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`.
if true
end
RUBY

expect(new_source).to eq(<<~RUBY)
blarg = if true
expect_correction(<<~RUBY)
hash[:foo] = if true
end
RUBY
end

it 'registers an offense when the rhs is a different line in setters' do
expect_offense(<<~RUBY)
foo.bar =
^^^^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`.
if true
end
RUBY

expect_correction(<<~RUBY)
foo.bar = if true
end
gsamokovarov marked this conversation as resolved.
Show resolved Hide resolved
RUBY
end
Expand Down