Skip to content

Commit

Permalink
Ignore symbols ending with ?/! in Style/HashSyntax (rubocop#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
owst authored and Neodelf committed Oct 15, 2016
1 parent 86993d7 commit 572ced6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* [#3140](https://github.com/bbatsov/rubocop/pull/3140): `Style/FrozenStringLiteralComment` works with file doesn't have any tokens. ([@pocke][])
* [#3154](https://github.com/bbatsov/rubocop/issues/3154): Fix handling of `()` in `Style/RedundantParentheses`. ([@lumeet][])

### Changes

* [#3149](https://github.com/bbatsov/rubocop/pull/3149): Make `Style/HashSyntax` configurable to not report hash rocket syntax for symbols ending with ? or ! when using ruby19 style. ([@owst][])

## 0.40.0 (2016-05-09)

### New features
Expand Down
4 changes: 3 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,10 @@ Style/HashSyntax:
- ruby19
- ruby19_no_mixed_keys
- hash_rockets
# Force hashes that have a symbol value to use hash rockets
# Force hashes that have a symbol value to use hash rockets
UseHashRocketsWithSymbolValues: false
# Do not suggest { a?: 1 } over { :a? => 1 } in ruby19 style
PreferHashRocketsForNonAlnumEndingSymbols: false

Style/IfUnlessModifier:
MaxLineLength: 80
Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/cop/style/hash_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ def word_symbol_pair?(pair)

return false unless key.sym_type?

valid_19_syntax_symbol?(key.source)
acceptable_19_syntax_symbol?(key.source)
end

def valid_19_syntax_symbol?(sym_name)
def acceptable_19_syntax_symbol?(sym_name)
sym_name.sub!(/\A:/, '')

if cop_config['PreferHashRocketsForNonAlnumEndingSymbols']
# Prefer { :production? => false } over { production?: false } and
# similarly for other non-alnum final characters (except quotes,
# to prefer { "x y": 1 } over { :"x y" => 1 }).
return false unless sym_name =~ /[\p{Alnum}"']\z/
end

# Most hash keys can be matched against a simple regex.
return true if sym_name =~ /\A[_a-z]\w*[?!]?\z/i

Expand Down
47 changes: 42 additions & 5 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@
RuboCop::Config.new('AllCops' => {
'TargetRubyVersion' => ruby_version
},
'Style/HashSyntax' => {
'EnforcedStyle' => 'ruby19',
'SupportedStyles' => %w(ruby19 hash_rockets),
'UseHashRocketsWithSymbolValues' => false
},
'Style/HashSyntax' => cop_config,
'Style/SpaceAroundOperators' => {
'Enabled' => true
})
end

let(:cop_config) do
{
'EnforcedStyle' => 'ruby19',
'SupportedStyles' => %w(ruby19 hash_rockets),
'UseHashRocketsWithSymbolValues' => false,
'PreferHashRocketsForNonAlnumEndingSymbols' => false
}.merge(cop_config_overrides)
end

let(:cop_config_overrides) { {} }

it 'registers offense for hash rocket syntax when new is possible' do
inspect_source(cop, 'x = { :a => 0 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
Expand Down Expand Up @@ -59,6 +66,36 @@
end
end

context 'if PreferHashRocketsForNonAlnumEndingSymbols is false' do
it 'registers an offense for hash rockets when symbols end with ?' do
inspect_source(cop, 'x = { :a? => 0 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end

it 'registers an offense for hash rockets when symbols end with !' do
inspect_source(cop, 'x = { :a! => 0 }')
expect(cop.messages).to eq(['Use the new Ruby 1.9 hash syntax.'])
end
end

context 'if PreferHashRocketsForNonAlnumEndingSymbols is true' do
let(:cop_config_overrides) do
{
'PreferHashRocketsForNonAlnumEndingSymbols' => true
}
end

it 'accepts hash rockets when symbols end with ?' do
inspect_source(cop, 'x = { :a? => 0 }')
expect(cop.messages).to be_empty
end

it 'accepts hash rockets when symbols end with !' do
inspect_source(cop, 'x = { :a! => 0 }')
expect(cop.messages).to be_empty
end
end

it 'accepts hash rockets when symbol keys end with =' do
inspect_source(cop, 'x = { :a= => 0 }')
expect(cop.messages).to be_empty
Expand Down

0 comments on commit 572ced6

Please sign in to comment.