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 string literal autocorrect #9751

Merged
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
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][])
1 change: 1 addition & 0 deletions changelog/fix_dont_autocorrect_global_variable.md
@@ -0,0 +1 @@
* [#9751](https://github.com/rubocop/rubocop/pull/9751): `Style/StringLiteral` doesn't autocorrect global variable interpolation. ([@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
7 changes: 6 additions & 1 deletion 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 Expand Up @@ -261,10 +264,12 @@
a = '\n'
b = '"'
c = '#{x}'
d = '#@x'
e = '#$x'
RUBY
end

it 'flags single quotes with plain # (not #@var or #{interpolation}' do
it 'flags single quotes with plain # (not #@var or #{interpolation} or #$global' do
expect_offense(<<~RUBY)
a = 'blah #'
^^^^^^^^ Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Expand Down