Skip to content

Commit

Permalink
[Fix #10462] Fix an incorrect autocorrect for Lint/SymbolConversion
Browse files Browse the repository at this point in the history
Fixes #10462.

This PR fixes an incorrect autocorrect for `Lint/SymbolConversion`
when using a quoted symbol key with hash rocket.
  • Loading branch information
koic authored and bbatsov committed Mar 22, 2022
1 parent 3112ce7 commit 5234ad4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
@@ -0,0 +1 @@
* [#10462](https://github.com/rubocop/rubocop/issues/10462): Fix an incorrect autocorrect for `Lint/SymbolConversion` when using a quoted symbol key with hash rocket. ([@koic][])
5 changes: 3 additions & 2 deletions lib/rubocop/cop/lint/symbol_conversion.rb
Expand Up @@ -147,13 +147,14 @@ def correct_hash_key(node)
# will be ignored.
return unless node.value.to_s.match?(/\A[a-z0-9_]/i)

correction = node.value.inspect.delete_prefix(':')
correction = node.value.inspect
correction = correction.delete_prefix(':') if node.parent.colon?
return if properly_quoted?(node.source, correction)

register_offense(
node,
correction: correction,
message: format(MSG, correction: "#{correction}:")
message: format(MSG, correction: node.parent.colon? ? "#{correction}:" : correction)
)
end

Expand Down
13 changes: 12 additions & 1 deletion spec/rubocop/cop/lint/symbol_conversion_spec.rb
Expand Up @@ -121,7 +121,18 @@
RUBY
end

it 'registers an offense for a quoted symbol' do
it 'registers an offense for a quoted symbol key' do
expect_offense(<<~RUBY)
{ :'foo' => :bar }
^^^^^^ Unnecessary symbol conversion; use `:foo` instead.
RUBY

expect_correction(<<~RUBY)
{ :foo => :bar }
RUBY
end

it 'registers an offense for a quoted symbol value' do
expect_offense(<<~RUBY)
{ foo: :'bar' }
^^^^^^ Unnecessary symbol conversion; use `:bar` instead.
Expand Down

0 comments on commit 5234ad4

Please sign in to comment.