From a7ef72aa8c35336b169c5d78e2925fc27d739fcb Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 1 Oct 2021 13:07:32 -0400 Subject: [PATCH] [Fix #10148] Fix `Style/QuotedSymbols` handling escaped characters incorrectly. Two cases are improved here: 1. Escaped double quotes (`\"`) within a double-quoted string are handled correctly (replaced with an unescaped double quote) when converting to a single-quoted string. 2. Escaped backslashes (`\\`) are treated properly and not doubled for both single- and double-quoted strings. --- ...fix_stylequotedsymbols_handling_escaped.md | 1 + lib/rubocop/cop/mixin/string_literals_help.rb | 6 +- lib/rubocop/cop/style/quoted_symbols.rb | 13 +++-- lib/rubocop/cop/util.rb | 3 +- spec/rubocop/cop/style/quoted_symbols_spec.rb | 56 +++++++++++++++++++ .../rubocop/cop/style/string_literals_spec.rb | 11 ++++ 6 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 changelog/fix_fix_stylequotedsymbols_handling_escaped.md diff --git a/changelog/fix_fix_stylequotedsymbols_handling_escaped.md b/changelog/fix_fix_stylequotedsymbols_handling_escaped.md new file mode 100644 index 00000000000..2b2272faa43 --- /dev/null +++ b/changelog/fix_fix_stylequotedsymbols_handling_escaped.md @@ -0,0 +1 @@ +* [#10148](https://github.com/rubocop/rubocop/issues/10148): Fix `Style/QuotedSymbols` handling escaped characters incorrectly. ([@dvandersluis][]) diff --git a/lib/rubocop/cop/mixin/string_literals_help.rb b/lib/rubocop/cop/mixin/string_literals_help.rb index c43c08dc176..b9d7677dec7 100644 --- a/lib/rubocop/cop/mixin/string_literals_help.rb +++ b/lib/rubocop/cop/mixin/string_literals_help.rb @@ -13,7 +13,11 @@ def wrong_quotes?(src_or_node) if style == :single_quotes !double_quotes_required?(src) else - !/" | \\[^'\\] | \#[@{$]/x.match?(src) + # The string needs single quotes if: + # 1. It contains a double quote + # 2. It contains text that would become an escape sequence with double quotes + # 3. It contains text that would become an interpolation with double quotes + !/" | (?