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 #7422] Treat casgn nodes like other assignment nodes in Layout/SpaceAroundOperators #10001

Merged
merged 1 commit into from Aug 11, 2021
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/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