Skip to content

Commit

Permalink
Change Lint/SymbolConversion to only quote with double quotes, sinc…
Browse files Browse the repository at this point in the history
…e `Style/QuotedSymbols` can now correct those to the correct quotes as per configuration.
  • Loading branch information
dvandersluis committed May 17, 2021
1 parent 2fabcd8 commit 3ef1e32
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 63 deletions.
1 change: 1 addition & 0 deletions changelog/change_change_lintsymbolconversion_to_only.md
@@ -0,0 +1 @@
* [#9809](https://github.com/rubocop/rubocop/pull/9809): Change `Lint/SymbolConversion` to only quote with double quotes, since `Style/QuotedSymbols` can now correct those to the correct quotes as per configuration. ([@dvandersluis][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -2092,6 +2092,7 @@ Lint/SymbolConversion:
Description: 'Checks for unnecessary symbol conversions.'
Enabled: pending
VersionAdded: '1.9'
VersionChanged: '<<next>>'
EnforcedStyle: strict
SupportedStyles:
- strict
Expand Down
9 changes: 1 addition & 8 deletions lib/rubocop/cop/lint/symbol_conversion.rb
Expand Up @@ -164,21 +164,14 @@ def correct_inconsistent_hash_keys(keys)
next if requires_quotes?(key)
next if properly_quoted?(key.source, %("#{key.value}"))

correction = "#{quote_type}#{key.value}#{quote_type}"
correction = %("#{key.value}")
register_offense(
key,
correction: correction,
message: format(MSG_CONSISTENCY, correction: "#{correction}:")
)
end
end

def quote_type
# Use the `Style/StringLiterals` configuration for quoting symbols
return '"' unless config.for_cop('Style/StringLiterals')['Enabled']

config.for_cop('Style/StringLiterals')['EnforcedStyle'] == 'single_quotes' ? "'" : '"'
end
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -2019,4 +2019,33 @@ def my_func
end
RUBY
end

it 'consistently quotes symbol keys in a hash using `Lint/SymbolConversion` ' \
'with `EnforcedStyle: consistent` and `Style/QuotedSymbols`' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
{
a: 1,
b: 2,
'c-d': 3
}
RUBY

create_file('.rubocop.yml', <<~YAML)
Lint/SymbolConversion:
EnforcedStyle: consistent
Style/QuotedSymbols:
EnforcedStyle: double_quotes
YAML

status = cli.run(['--auto-correct', '--only', 'Lint/SymbolConversion,Style/QuotedSymbols'])
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
{
"a": 1,
"b": 2,
"c-d": 3
}
RUBY
end
end
61 changes: 6 additions & 55 deletions spec/rubocop/cop/lint/symbol_conversion_spec.rb
Expand Up @@ -170,18 +170,7 @@
end

context 'EnforcedStyle: consistent' do
let(:string_literals_enabled) { true }
let(:string_literals_style) { 'single_quotes' }

let(:cop_config) { { 'EnforcedStyle' => 'consistent' } }
let(:other_cops) do
{
'Style/StringLiterals' => {
'Enabled' => string_literals_enabled,
'EnforcedStyle' => string_literals_style
}
}
end

context 'hash where no keys need to be quoted' do
it 'does not register an offense' do
Expand Down Expand Up @@ -218,18 +207,18 @@
expect_offense(<<~RUBY)
{
a: 1,
^ Symbol hash key should be quoted for consistency; use `'a':` instead.
^ Symbol hash key should be quoted for consistency; use `"a":` instead.
b: 2,
^ Symbol hash key should be quoted for consistency; use `'b':` instead.
^ Symbol hash key should be quoted for consistency; use `"b":` instead.
'c': 3,
'd-e': 4
}
RUBY

expect_correction(<<~RUBY)
{
'a': 1,
'b': 2,
"a": 1,
"b": 2,
'c': 3,
'd-e': 4
}
Expand All @@ -243,14 +232,14 @@
{
'a=': 1,
b: 2
^ Symbol hash key should be quoted for consistency; use `'b':` instead.
^ Symbol hash key should be quoted for consistency; use `"b":` instead.
}
RUBY

expect_correction(<<~RUBY)
{
'a=': 1,
'b': 2
"b": 2
}
RUBY
end
Expand Down Expand Up @@ -279,43 +268,5 @@
RUBY
end
end

context 'quote style' do
let(:source) do
<<~RUBY
{ a: 1, 'b-c': 2 }
RUBY
end

context 'when Style/StringLiterals is not enabled' do
let(:string_literals_enabled) { false }

it 'uses double quotes to correct' do
expect_correction(<<~RUBY, source: source)
{ "a": 1, 'b-c': 2 }
RUBY
end
end

context 'when Style/StringLiterals uses single_quotes style' do
let(:string_literals_style) { 'single_quotes' }

it 'uses double quotes to correct' do
expect_correction(<<~RUBY, source: source)
{ 'a': 1, 'b-c': 2 }
RUBY
end
end

context 'when Style/StringLiterals uses double_quotes style' do
let(:string_literals_style) { 'double_quotes' }

it 'uses double quotes to correct' do
expect_correction(<<~RUBY, source: source)
{ "a": 1, 'b-c': 2 }
RUBY
end
end
end
end
end

0 comments on commit 3ef1e32

Please sign in to comment.