Skip to content

Commit

Permalink
[Fix #11123 ] Fix auto correction bug for Style/StringLiterals
Browse files Browse the repository at this point in the history
  • Loading branch information
si-lens committed Nov 12, 2022
1 parent 41a8249 commit 45da59a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_auto_correction_bug_for_string_literals.md
@@ -0,0 +1 @@
* [#11123](https://github.com/rubocop/rubocop/issues/11123): Fix autocorrection bug for `Style/StringLiterals` when using multiple escape characters. ([@si-lens][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/quoted_symbols.rb
Expand Up @@ -93,7 +93,7 @@ def correct_quotes(str)
end

# The conversion process doubles escaped slashes, so they have to be reverted
correction.gsub('\\\\', '\\')
correction.gsub('\\\\', '\\').gsub('\"', '"')
end

def style
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/util.rb
Expand Up @@ -122,7 +122,7 @@ def to_string_literal(string)
string.inspect
else
# In a single-quoted strings, double quotes don't need to be escaped
"'#{string.gsub('\"', '"').gsub('\\') { '\\\\' }}'"
"'#{string.gsub('\\') { '\\\\' }.gsub('\"', '"')}'"
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/rubocop/cop/style/string_literals_spec.rb
Expand Up @@ -14,6 +14,8 @@
^^^^^ Prefer single-quoted strings when you don't need string interpolation or special symbols.
z = "a\\"
^^^^^ Prefer single-quoted strings when you don't need string interpolation or special symbols.
t = "{\"[\\\"*\\\"]\""
^^^^^^^^^^^^^^^^^^ Prefer single-quoted strings when you don't need string interpolation or special symbols.
RUBY
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'double_quotes')

Expand All @@ -22,6 +24,7 @@
x = 'a\\b'
y ='\\b'
z = 'a\\'
t = '{"[\"*\"]"'
RUBY
end

Expand Down

0 comments on commit 45da59a

Please sign in to comment.