Skip to content

Commit

Permalink
Merge pull request #7438 from gsamokovarov/multiline-assignment-layou…
Browse files Browse the repository at this point in the history
…t-edge-case

Fix assignment edge-cases in Layout/MultilineAssignmentLayout
  • Loading branch information
koic committed Oct 18, 2019
2 parents 6ce4f5a + d80771d commit 8b8141b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
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][])

## 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
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
RUBY
end
Expand Down

0 comments on commit 8b8141b

Please sign in to comment.