Skip to content

Commit

Permalink
[Fix rubocop#7422] Treat casgn nodes like other assignment nodes in…
Browse files Browse the repository at this point in the history
… `Layout/SpaceAroundOperators`.
  • Loading branch information
dvandersluis committed Aug 10, 2021
1 parent bb5d832 commit cbdd9c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_treat_casgn_nodes_like_other_assignment.md
@@ -0,0 +1 @@
* [#7422](https://github.com/rubocop/rubocop/issues/7422): Treat constant assignment like other assignment in `Layout/SpaceAroundOperators`. ([@dvandersluis][])
9 changes: 8 additions & 1 deletion lib/rubocop/cop/layout/space_around_operators.rb
Expand Up @@ -108,6 +108,14 @@ def on_assignment(node)
check_operator(:assignment, node.loc.operator, rhs.source_range)
end

def on_casgn(node)
_, _, right, = *node

return unless right

check_operator(:assignment, node.loc.operator, right.source_range)
end

def on_binary(node)
_, rhs, = *node

Expand All @@ -134,7 +142,6 @@ def on_match_pattern(node)
alias on_and on_binary
alias on_lvasgn on_assignment
alias on_masgn on_assignment
alias on_casgn on_special_asgn
alias on_ivasgn on_assignment
alias on_cvasgn on_assignment
alias on_gvasgn on_assignment
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/layout/space_around_operators_spec.rb
Expand Up @@ -955,4 +955,28 @@ class Foo < Bar
RUBY
end
end

describe 'when Layout/ExtraSpacing has `ForceEqualSignAlignment` configured to true' do
let(:other_cops) do
{ 'Layout/ExtraSpacing' => { 'Enabled' => true, 'ForceEqualSignAlignment' => true } }
end

it 'allows variables to be aligned' do
expect_no_offenses(<<~RUBY)
first = {
x: y
}.freeze
second = true
RUBY
end

it 'allows constants to be aligned' do
expect_no_offenses(<<~RUBY)
FIRST = {
x: y
}.freeze
SECOND = true
RUBY
end
end
end

0 comments on commit cbdd9c7

Please sign in to comment.