Skip to content

Commit

Permalink
[Fix #8801] Fix Layouts/SpaceAroundEqualsInParameterDefault
Browse files Browse the repository at this point in the history
This cop is only being handled once per line, due to a
mis-use of the 'opposite_style_detected'/'unrecognized_style_detected'
methods.
  • Loading branch information
rdunlop committed Sep 30, 2020
1 parent e8cfbc1 commit 990732c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
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

0 comments on commit 990732c

Please sign in to comment.