Skip to content

Commit

Permalink
[Fix #8938] Fix --auto-gen-config for `Style/SpaceInsideHashLiteral…
Browse files Browse the repository at this point in the history
…Braces`

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.

Prior to this commit, this cop called `correct_style_detected` correctly
on success, but made several mistakes with calling
`ambiguous_style_detected` or `unexpected_style_detected` on error. It
treated these methods as predicates and wouldn't call `add_offense` if
they returned `true` so the detected offenses would not be registered. It
also passed the wrong `style` parameter to these methods - it passed the
configured style from `EnforcedStyle` rather than the one detected by the
offense. This means:

1. During a normal run the cop would not report all offenses detected in
   the whole code base under review.
2. During a `--auto-gen-config` run the cop would only ever output
   `EnforcedStyle: <some value>`. 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 corrects both the errors: it passes the detected incorrect
style when calling `ambiguous_style_detected` or
`unexpected_style_detected` and always calls `add_offense` instead of
treating the return value of `ambiguous_style_detected` or
`unexpected_style_detected` as a predicate to decide if it should or not.
  • Loading branch information
h-lame authored and bbatsov committed Dec 24, 2020
1 parent 10c698b commit bd0131c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def expect_space?(token1, token2)

def incorrect_style_detected(token1, token2,
expect_space, is_empty_braces)
return unless ambiguous_or_unexpected_style_detected(style, token1.text == token2.text)

brace = (token1.text == '{' ? token1 : token2).pos
range = expect_space ? brace : space_range(brace)
detected_style = expect_space ? 'no_space' : 'space'

add_offense(range, message: message(brace, is_empty_braces, expect_space)) do |corrector|
autocorrect(corrector, range)
ambiguous_or_unexpected_style_detected(detected_style, token1.text == token2.text)
end
end

Expand Down

0 comments on commit bd0131c

Please sign in to comment.