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 #6778] Fix a false positive in Style/HashSyntax cop #6948

Merged
merged 1 commit into from Apr 19, 2019
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 @@ -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