From fc5acdeaa671e7504eb90304bff3bb16818ac8d2 Mon Sep 17 00:00:00 2001 From: Tatsuya Hoshino Date: Fri, 19 Apr 2019 16:07:30 +0900 Subject: [PATCH] [Fix #6778] Fix a false positive in `Style/HashSyntax` cop 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. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/hash_syntax.rb | 2 +- spec/rubocop/cop/style/hash_syntax_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f256ed38b5c..4fc272d8ef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index 6f7cb1b3343..54bb3975190 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -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 diff --git a/spec/rubocop/cop/style/hash_syntax_spec.rb b/spec/rubocop/cop/style/hash_syntax_spec.rb index ff086384488..8495f54e3d3 100644 --- a/spec/rubocop/cop/style/hash_syntax_spec.rb +++ b/spec/rubocop/cop/style/hash_syntax_spec.rb @@ -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 }') @@ -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 }')