Skip to content

Commit

Permalink
Don't autocorrect global variable interpolation
Browse files Browse the repository at this point in the history
Global variable interpolation is autocorrected, adding escaping:

'#$foo' is autocorrected to "\#$foo"
  • Loading branch information
etiennebarrie authored and bbatsov committed May 3, 2021
1 parent c5f7dd6 commit c165e90
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
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
4 changes: 3 additions & 1 deletion spec/rubocop/cop/style/string_literals_spec.rb
Expand Up @@ -264,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

0 comments on commit c165e90

Please sign in to comment.