diff --git a/CHANGELOG.md b/CHANGELOG.md index b3680a64bc1..0764ad22d67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb b/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb index 4ff43b9b884..b2cc8d45393 100644 --- a/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +++ b/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb @@ -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 diff --git a/spec/rubocop/cop/layout/space_around_equals_in_parameter_default_spec.rb b/spec/rubocop/cop/layout/space_around_equals_in_parameter_default_spec.rb index 8a3c4d9377c..8370f84c05d 100644 --- a/spec/rubocop/cop/layout/space_around_equals_in_parameter_default_spec.rb +++ b/spec/rubocop/cop/layout/space_around_equals_in_parameter_default_spec.rb @@ -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)