Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8642] Fix a false negative for Style/SpaceInsideHashLiteralBraces when a correct empty hash precedes the incorrect hash #8649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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