diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index 1669c030680d..57c11510f165 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -100,7 +100,6 @@ def word_symbol_pair?(pair) end def valid_19_syntax_symbol?(sym_name) - return false if sym_name =~ /\A:["']/ sym_name.sub!(/\A:/, '') RuboCop::ProcessedSource.new("{ #{sym_name}: :foo }").valid_syntax? end diff --git a/spec/rubocop/cop/style/hash_syntax_spec.rb b/spec/rubocop/cop/style/hash_syntax_spec.rb index 211d51b22611..273213c7b076 100644 --- a/spec/rubocop/cop/style/hash_syntax_spec.rb +++ b/spec/rubocop/cop/style/hash_syntax_spec.rb @@ -41,9 +41,18 @@ expect(cop.messages).to be_empty end - it 'accepts hash rockets when symbol keys have string in them' do - inspect_source(cop, 'x = { :"string" => 0 }') - expect(cop.messages).to be_empty + context 'ruby < 2.2', ruby_less_than: 2.2 do + it 'accepts hash rockets when symbol keys have string in them' do + inspect_source(cop, 'x = { :"string" => 0 }') + expect(cop.messages).to be_empty + end + end + + context 'ruby >= 2.2', ruby_greater_than_or_equal: 2.2 do + it 'registers an offense when symbol keys have strings in them' do + inspect_source(cop, 'x = { :"string" => 0 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end end it 'accepts hash rockets when symbol keys end with =' do @@ -283,37 +292,57 @@ expect(cop.config_to_allow_offenses).to eq('Enabled' => false) end - it 'accepts hash rockets when keys have whitespaces in them' do - inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }') - expect(cop.messages).to be_empty - end + context 'ruby < 2.2', ruby_less_than: 2.2 do + it 'accepts hash rockets when keys have whitespaces in them' do + inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }') + expect(cop.messages).to be_empty + end - it 'registers an offense when keys have whitespaces and mix styles' do - inspect_source(cop, 'x = { :"t o" => 0, b: 1 }') - expect(cop.messages).to eq(["Don't mix styles in the same hash."]) - expect(cop.config_to_allow_offenses).to eq('Enabled' => false) - end + it 'registers an offense when keys have whitespaces and mix styles' do + inspect_source(cop, 'x = { :"t o" => 0, b: 1 }') + expect(cop.messages).to eq(["Don't mix styles in the same hash."]) + expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + end - it 'accepts hash rockets when keys have special symbols in them' do - inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }') - expect(cop.messages).to be_empty - end + it 'accepts hash rockets when keys have special symbols in them' do + inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }') + expect(cop.messages).to be_empty + end - it 'registers an offense when keys have special symbols and mix styles' do - inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }') - expect(cop.messages).to eq(["Don't mix styles in the same hash."]) - expect(cop.config_to_allow_offenses).to eq('Enabled' => false) - end + it 'registers an offense when keys have special symbols and '\ + 'mix styles' do + inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }') + expect(cop.messages).to eq(["Don't mix styles in the same hash."]) + expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + end - it 'accepts hash rockets when keys start with a digit' do - inspect_source(cop, 'x = { :"1" => 1, :b => 1 }') - expect(cop.messages).to be_empty + it 'accepts hash rockets when keys start with a digit' do + inspect_source(cop, 'x = { :"1" => 1, :b => 1 }') + expect(cop.messages).to be_empty + end + + it 'registers an offense when keys start with a digit and mix styles' do + inspect_source(cop, 'x = { :"1" => 1, b: 1 }') + expect(cop.messages).to eq(["Don't mix styles in the same hash."]) + expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + end end - it 'registers an offense when keys start with a digit and mix styles' do - inspect_source(cop, 'x = { :"1" => 1, b: 1 }') - expect(cop.messages).to eq(["Don't mix styles in the same hash."]) - expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + context 'ruby >= 2.2', ruby_greater_than_or_equal: 2.2 do + it 'registers an offense when keys have whitespaces in them' do + inspect_source(cop, 'x = { :"t o" => 0 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end + + it 'registers an offense when keys have special symbols in them' do + inspect_source(cop, 'x = { :"\tab" => 1 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end + + it 'accepts hash rockets when keys start with a digit' do + inspect_source(cop, 'x = { :"1" => 1 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end end it 'auto-corrects old to new style' do @@ -399,37 +428,57 @@ expect(cop.config_to_allow_offenses).to eq('Enabled' => false) end - it 'accepts hash rockets when keys have whitespaces in them' do - inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }') - expect(cop.messages).to be_empty - end - - it 'registers an offense when keys have whitespaces and mix styles' do - inspect_source(cop, 'x = { :"t o" => 0, b: 1 }') - expect(cop.messages).to eq(["Don't mix styles in the same hash."]) - expect(cop.config_to_allow_offenses).to eq('Enabled' => false) - end - - it 'accepts hash rockets when keys have special symbols in them' do - inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }') - expect(cop.messages).to be_empty - end - - it 'registers an offense when keys have special symbols and mix styles' do - inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }') - expect(cop.messages).to eq(["Don't mix styles in the same hash."]) - expect(cop.config_to_allow_offenses).to eq('Enabled' => false) - end - - it 'accepts hash rockets when keys start with a digit' do - inspect_source(cop, 'x = { :"1" => 1, :b => 1 }') - expect(cop.messages).to be_empty - end - - it 'registers an offense when keys start with a digit and mix styles' do - inspect_source(cop, 'x = { :"1" => 1, b: 1 }') - expect(cop.messages).to eq(["Don't mix styles in the same hash."]) - expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + context 'ruby < 2.2', ruby_less_than: 2.2 do + it 'accepts hash rockets when keys have whitespaces in them' do + inspect_source(cop, 'x = { :"t o" => 0, :b => 1 }') + expect(cop.messages).to be_empty + end + + it 'registers an offense when keys have whitespaces and mix styles' do + inspect_source(cop, 'x = { :"t o" => 0, b: 1 }') + expect(cop.messages).to eq(["Don't mix styles in the same hash."]) + expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + end + + it 'accepts hash rockets when keys have special symbols in them' do + inspect_source(cop, 'x = { :"\tab" => 1, :b => 1 }') + expect(cop.messages).to be_empty + end + + it 'registers an offense when keys have special symbols and ' \ + 'mix styles' do + inspect_source(cop, 'x = { :"\tab" => 1, b: 1 }') + expect(cop.messages).to eq(["Don't mix styles in the same hash."]) + expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + end + + it 'accepts hash rockets when keys start with a digit' do + inspect_source(cop, 'x = { :"1" => 1, :b => 1 }') + expect(cop.messages).to be_empty + end + + it 'registers an offense when keys start with a digit and mix styles' do + inspect_source(cop, 'x = { :"1" => 1, b: 1 }') + expect(cop.messages).to eq(["Don't mix styles in the same hash."]) + expect(cop.config_to_allow_offenses).to eq('Enabled' => false) + end + end + + context 'ruby >= 2.2', ruby_greater_than_or_equal: 2.2 do + it 'registers an offense when keys have whitespaces in them' do + inspect_source(cop, 'x = { :"t o" => 0 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end + + it 'registers an offense when keys have special symbols in them' do + inspect_source(cop, 'x = { :"\tab" => 1 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end + + it 'accepts hash rockets when keys start with a digit' do + inspect_source(cop, 'x = { :"1" => 1 }') + expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.']) + end end it 'auto-corrects old to new style' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7f5ac13cf685..f2e3ad0c47a5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,6 +21,20 @@ config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) } + ruby_version = Gem::Version.new(RUBY_VERSION) + config.filter_run_excluding ruby_less_than: (lambda do |v| + ruby_version >= Gem::Version.new(v) + end) + config.filter_run_excluding ruby_less_than: (lambda do |v| + ruby_version >= Gem::Version.new(v) + end) + config.filter_run_excluding ruby_greater_than_or_equal: (lambda do |v| + ruby_version < Gem::Version.new(v) + end) + config.filter_run_excluding ruby_greater_than_or_equal: (lambda do |v| + ruby_version < Gem::Version.new(v) + end) + broken_filter = lambda do |v| v.is_a?(Symbol) ? RUBY_ENGINE == v.to_s : v end