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 #8801] Fix Layouts/SpaceAroundEqualsInParameterDefault #8802

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@

* [#8810](https://github.com/rubocop-hq/rubocop/pull/8810): Fix multiple offense detection for Style/RaiseArgs. ([@pbernays][])
* [#8809](https://github.com/rubocop-hq/rubocop/pull/8809): Fix multiple offense detection for Style/For. ([@pbernays][])
* [#8801](https://github.com/rubocop-hq/rubocop/pull/8801): Fix `Layout/SpaceAroundEqualsInParameterDefault` only registered once in a line. ([@rdunlop][])

### Changes

Expand Down Expand Up @@ -4938,3 +4939,4 @@
[@em-gazelle]: https://github.com/em-gazelle
[@tleish]: https://github.com/tleish
[@pbernays]: https://github.com/pbernays
[@rdunlop]: https://github.com/rdunlop
13 changes: 2 additions & 11 deletions lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb
Expand Up @@ -51,22 +51,13 @@ def check_optarg(arg, equals, value)
style == :no_space && no_surrounding_space
correct_style_detected
else
incorrect_style_detected(arg, value, space_on_both_sides,
no_surrounding_space)
incorrect_style_detected(arg, value)
end
end

def incorrect_style_detected(arg, value, space_on_both_sides,
no_surrounding_space)
def incorrect_style_detected(arg, value)
range = range_between(arg.end_pos, value.begin_pos)

if style == :space && no_surrounding_space ||
style == :no_space && space_on_both_sides
return unless opposite_style_detected
else
return unless unrecognized_style_detected
end

add_offense(range) do |corrector|
autocorrect(corrector, range)
end
Expand Down
Expand Up @@ -19,6 +19,21 @@ def f(x, y = 0, z = 1)
RUBY
end

it 'registers an offense and corrects default value assignment where first is partially right ' \
'without space' do
expect_offense(<<~RUBY)
def f(x, y= 0, z=1)
^^ Surrounding space missing in default value assignment.
^ Surrounding space missing in default value assignment.
end
RUBY

expect_correction(<<~RUBY)
def f(x, y = 0, z = 1)
end
RUBY
end

it 'registers an offense and corrects assigning empty string ' \
'without space' do
expect_offense(<<~RUBY)
Expand Down