Skip to content

Commit

Permalink
[Fix rubocop#12646] Handle opposite styles for Layout/SpaceBeforeBloc…
Browse files Browse the repository at this point in the history
…kBraces

The `Layout/SpaceBeforeBlockBraces` cop has two separate style parameters,
`EnforcedStyle` and `EnforcedStyleForEmptyBraces`. The logic for generating
`Enabled: false` when opposite styles are detected during `--auto-gen-config`
was implemented for `EnforcedStyle`, but it was missing for
`EnforcedStyleForEmptyBraces`.
  • Loading branch information
jonas054 committed Feb 25, 2024
1 parent 4c5fce1 commit 975667f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_auto_gen_config_SpaceBeforeBlockBraces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12646](https://github.com/rubocop/rubocop/issues/12646): Fix `--auto-gen-config` bug for `Layout/SpaceBeforeBlockBraces`. ([@jonas054][])
15 changes: 12 additions & 3 deletions lib/rubocop/cop/layout/space_before_block_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ def on_block(node)

private

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def check_empty(left_brace, space_plus_brace, used_style)
return if style_for_empty_braces == used_style

config_to_allow_offenses['EnforcedStyleForEmptyBraces'] = used_style.to_s
if style_for_empty_braces == used_style
if config_to_allow_offenses['EnforcedStyleForEmptyBraces'] &&
config_to_allow_offenses['EnforcedStyleForEmptyBraces'].to_sym != used_style
config_to_allow_offenses.clear
config_to_allow_offenses['Enabled'] = false
end
return
elsif !config_to_allow_offenses.key?('Enabled')
config_to_allow_offenses['EnforcedStyleForEmptyBraces'] = used_style.to_s
end

if style_for_empty_braces == :space
add_offense(left_brace, message: MISSING_MSG) do |corrector|
Expand All @@ -96,6 +104,7 @@ def check_empty(left_brace, space_plus_brace, used_style)
end
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def check_non_empty(left_brace, space_plus_brace, used_style)
case used_style
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/layout/space_before_block_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
^ Space missing to the left of {.
RUBY

expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'no_space')
expect_correction(<<~RUBY)
each { puts }
RUBY
Expand All @@ -26,6 +27,7 @@
each { puts }
RUBY

expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
expect_correction(<<~RUBY)
each { puts }
each { puts }
Expand Down Expand Up @@ -56,6 +58,7 @@
each { _1 }
RUBY

expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
expect_correction(<<~RUBY)
each { _1 }
each { _1 }
Expand Down Expand Up @@ -89,6 +92,7 @@
^ Space detected to the left of {.
RUBY

expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'space')
expect_correction(<<~RUBY)
each{ puts }
RUBY
Expand All @@ -101,6 +105,7 @@
^ Space detected to the left of {.
RUBY

expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
expect_correction(<<~RUBY)
each{ puts }
each{ puts }
Expand All @@ -119,6 +124,7 @@
^ Space detected to the left of {.
RUBY

expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
expect_correction(<<~RUBY)
each{ _1 }
each{ _1 }
Expand Down Expand Up @@ -164,6 +170,7 @@
^ Space detected to the left of {.
RUBY

expect(cop.config_to_allow_offenses).to eq('EnforcedStyleForEmptyBraces' => 'space')
expect_correction(<<~RUBY)
->{}
RUBY
Expand All @@ -188,6 +195,7 @@
^ Space missing to the left of {.
RUBY

expect(cop.config_to_allow_offenses).to eq('EnforcedStyleForEmptyBraces' => 'no_space')
expect_correction(<<~RUBY)
-> {}
RUBY
Expand Down

0 comments on commit 975667f

Please sign in to comment.