Skip to content

Commit

Permalink
[Fix rubocop#8642] Fix a false negative for `Style/SpaceInsideHashLit…
Browse files Browse the repository at this point in the history
…eralBraces` when a correct empty hash precedes the incorrect hash.
  • Loading branch information
dvandersluis committed Sep 10, 2020
1 parent a60c3d2 commit 6a3fab2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@
* [#8671](https://github.com/rubocop-hq/rubocop/issues/8671): Fix an error for `Style/ExplicitBlockArgument` when using safe navigation method call. ([@koic][])
* [#8682](https://github.com/rubocop-hq/rubocop/pull/8682): Fix a positive for `Style/HashTransformKeys` and `Style/HashTransformValues` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][])
* [#8688](https://github.com/rubocop-hq/rubocop/issues/8688): Mark `Style/GlobalStdStream` as unsafe autocorrection. ([@marcandre][])
* [#8642](https://github.com/rubocop-hq/rubocop/issues/8642): Fix a false negative for `Style/SpaceInsideHashLiteralBraces` when a correct empty hash precedes the incorrect hash. ([@dvandersluis][])

### Changes

Expand Down
5 changes: 2 additions & 3 deletions lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb
Expand Up @@ -126,12 +126,11 @@ 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)

style = expect_space ? :no_space : :space
return unless ambiguous_or_unexpected_style_detected(style, token1.text == token2.text)

add_offense(range, message: message(brace, is_empty_braces, expect_space)) do |corrector|
autocorrect(corrector, range)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/layout/space_inside_hash_literal_braces_spec.rb
Expand Up @@ -216,4 +216,21 @@
expect_no_offenses('{ key: "{" }')
end
end

context 'offending hash following empty hash' do
# regression test; see GH issue 8642
it 'registers an offense on both sides' do
expect_offense(<<~RUBY)
{}
{key: 1}
^ Space inside { missing.
^ Space inside } missing.
RUBY

expect_correction(<<~RUBY)
{}
{ key: 1 }
RUBY
end
end
end

0 comments on commit 6a3fab2

Please sign in to comment.