Skip to content

Commit

Permalink
Autocorrect '\\' when double-quotes are preferred
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebarrie committed Apr 29, 2021
1 parent 74377b0 commit f4bdb3d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_autocorrect_when_doublequotes_are.md
@@ -0,0 +1 @@
* [#9751](https://github.com/rubocop/rubocop/pull/9751): `Style/StringLiteral` autocorrects `'\\'` into `"\\"`. ([@etiennebarrie][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/string_literals_help.rb
Expand Up @@ -15,7 +15,7 @@ def wrong_quotes?(node)
if style == :single_quotes
!double_quotes_required?(src)
else
!/" | \\[^'] | \#(@|\{)/x.match?(src)
!/" | \\[^'\\] | \#(@|\{)/x.match?(src)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/rubocop/cop/style/string_literals_spec.rb
Expand Up @@ -206,10 +206,13 @@
expect_offense(<<~'RUBY')
'\''
^^^^ Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
'\\'
^^^^ Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
RUBY

expect_correction(<<~'RUBY')
"'"
"\\"
RUBY
end

Expand Down

0 comments on commit f4bdb3d

Please sign in to comment.