Skip to content

Commit

Permalink
[Fix #8938] Fix --auto-gen-config for Style/SpaceInsideBlockBraces
Browse files Browse the repository at this point in the history
The expected use of the API provided by `ConfigurableEnforcedStyle` is
for the cop to call `correct_style_detected` if the cop detects no
offense or to call one of the other `xxx_style_detected` api methods if
it does detect an offense, and it should also call `add_offense` to
register the offense. Doing this updates `config_to_allow_offenses` which
is used during `--auto-gen-config` to decide how to configure the cop for
the current code under review so that regardless of the styles used the
code passes review. Importantly, this API should _only_ be used when the
offense is caused by the configuration handled by `EnforcedStyle`.

Prior to this commit, this cop was inconsistent about when it called
`correct_style_detected` on success and `opposite_style_detected` on
failure. This means it would always output `EnforcedStyle: <some value>`
during `--auto-gen-config` runs. If the code under review actually has a
mix of styles rather than a single consistent style then the generated
config for this cop in `.rubocop_todo.yml` will not produce a passing
review as instead of an exclusion list, or turning the cop off, it will
only have the `EnforcedStyle: <some value>` config.

This commit makes sure we always call `opposite_style_detected` when we
call `add_offense`, and always call `correct_style_detected` when we
don't detect an offense. Note, because this cop has separate style
choices for braces with contents, `EnforcedStyle`, and empty braces
without contents, `EnforcedStyleForEmptyBraces`, we also have to make
sure to only call the `xxx_style_detected` methods when the offenses /
passes are detected for the former as the API provided by
`ConfigurableEnforcedStyle` only applies to `EnforcedStyle` config.
  • Loading branch information
h-lame authored and bbatsov committed Dec 24, 2020
1 parent bd0131c commit 21a97b6
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/rubocop/cop/layout/space_inside_block_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def check_inside(node, left_brace, right_brace)
braces_with_contents_inside(node, inner)
elsif style_for_empty_braces == :no_space
offense(range.begin_pos, range.end_pos,
'Space inside empty braces detected.')
'Space inside empty braces detected.',
'EnforcedStyleForEmptyBraces')
end
end
end
Expand All @@ -120,7 +121,8 @@ def adjacent_braces(left_brace, right_brace)
return if style_for_empty_braces != :space

offense(left_brace.begin_pos, right_brace.end_pos,
'Space missing inside empty braces.')
'Space missing inside empty braces.',
'EnforcedStyleForEmptyBraces')
end

def braces_with_contents_inside(node, inner)
Expand Down Expand Up @@ -164,9 +166,9 @@ def no_space_inside_left_brace(left_brace, args_delimiter)
if left_brace.end_pos == args_delimiter.begin_pos &&
cop_config['SpaceBeforeBlockParameters']
offense(left_brace.begin_pos, args_delimiter.end_pos,
'Space between { and | missing.') do
opposite_style_detected
end
'Space between { and | missing.')
else
correct_style_detected
end
else
# We indicate the position after the left brace. Otherwise it's
Expand All @@ -179,11 +181,11 @@ def no_space_inside_left_brace(left_brace, args_delimiter)

def space_inside_left_brace(left_brace, args_delimiter)
if pipe?(args_delimiter)
unless cop_config['SpaceBeforeBlockParameters']
if cop_config['SpaceBeforeBlockParameters']
correct_style_detected
else
offense(left_brace.end_pos, args_delimiter.begin_pos,
'Space between { and | detected.') do
opposite_style_detected
end
'Space between { and | detected.')
end
else
brace_with_space = range_with_surrounding_space(range: left_brace,
Expand Down Expand Up @@ -220,7 +222,7 @@ def space(begin_pos, end_pos, msg)
end
end

def offense(begin_pos, end_pos, msg)
def offense(begin_pos, end_pos, msg, style_param = 'EnforcedStyle')
range = range_between(begin_pos, end_pos)
add_offense(range, message: msg) do |corrector|
case range.source
Expand All @@ -229,6 +231,7 @@ def offense(begin_pos, end_pos, msg)
when '{|' then corrector.replace(range, '{ |')
else corrector.insert_before(range, ' ')
end
opposite_style_detected if style_param == 'EnforcedStyle'
end
end

Expand Down

0 comments on commit 21a97b6

Please sign in to comment.