From c165e90a388f663d01fecd4b588d42aff3474be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Thu, 29 Apr 2021 16:47:21 -0400 Subject: [PATCH] Don't autocorrect global variable interpolation Global variable interpolation is autocorrected, adding escaping: '#$foo' is autocorrected to "\#$foo" --- changelog/fix_dont_autocorrect_global_variable.md | 1 + lib/rubocop/cop/mixin/string_literals_help.rb | 2 +- spec/rubocop/cop/style/string_literals_spec.rb | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_dont_autocorrect_global_variable.md diff --git a/changelog/fix_dont_autocorrect_global_variable.md b/changelog/fix_dont_autocorrect_global_variable.md new file mode 100644 index 00000000000..f7a3ff8c518 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/mixin/string_literals_help.rb b/lib/rubocop/cop/mixin/string_literals_help.rb index 455e93939d2..5dc088d371e 100644 --- a/lib/rubocop/cop/mixin/string_literals_help.rb +++ b/lib/rubocop/cop/mixin/string_literals_help.rb @@ -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 diff --git a/spec/rubocop/cop/style/string_literals_spec.rb b/spec/rubocop/cop/style/string_literals_spec.rb index bbd276bcef1..b6dd68f36b0 100644 --- a/spec/rubocop/cop/style/string_literals_spec.rb +++ b/spec/rubocop/cop/style/string_literals_spec.rb @@ -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.