Skip to content

Commit

Permalink
[Fix rubocop#6778] Fix a false positive in Style/HashSyntax cop
Browse files Browse the repository at this point in the history
The `Style/HashSyntax` cop would register an offense when `EnforcedStyle` was
`ruby19_no_mixed_keys` and a hash key was an interpolated string
like the following code:

```
{"#{foo}": 1}
```

This change fixes that.
  • Loading branch information
tatsuyafw committed Apr 19, 2019
1 parent 79a1a1c commit 74d282f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
* [#6941](https://github.com/rubocop-hq/rubocop/issues/6941): Add missing absence validations to `Rails/Validation`. ([@jmanian][])
* [#6926](https://github.com/rubocop-hq/rubocop/issues/6926): [Fix #6926] Allow nil values to unset config defaults. ([@dduugg][])
* [#6946](https://github.com/rubocop-hq/rubocop/pull/6946): Allow `Rails/ReflectionClassName` to use string interpolation for `class_name`. ([@r7kamura][])
* [#6778](https://github.com/rubocop-hq/rubocop/issues/6778): Fix a false positive in `Style/HashSyntax` cop when a hash key is an interpolated string and EnforcedStyle is ruby19_no_mixed_keys. ([@tatsuyafw][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_syntax.rb
Expand Up @@ -132,7 +132,7 @@ def sym_indices?(pairs)
end

def word_symbol_pair?(pair)
return false unless pair.key.sym_type?
return false unless pair.key.sym_type? || pair.key.dsym_type?

acceptable_19_syntax_symbol?(pair.key.source)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Expand Up @@ -387,6 +387,10 @@
RUBY
end

it 'accepts new syntax when keys are interpolated string' do
expect_no_offenses('{"#{foo}": 1, "#{@foo}": 2, "#@foo": 3}')
end

it 'auto-corrects old to new style' do
new_source = autocorrect_source('{ :a => 1, :b => 2 }')
expect(new_source).to eq('{ a: 1, b: 2 }')
Expand Down Expand Up @@ -504,6 +508,10 @@
RUBY
end

it 'accepts new syntax when keys are interpolated string' do
expect_no_offenses('{"#{foo}": 1, "#{@foo}": 2, "#@foo": 3}')
end

it 'auto-corrects old to new style' do
new_source = autocorrect_source('{ :a => 1, :b => 2 }')
expect(new_source).to eq('{ a: 1, b: 2 }')
Expand Down

0 comments on commit 74d282f

Please sign in to comment.