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 #10462] Fix an incorrect autocorrect for Lint/SymbolConversion #10463

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
@@ -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