lib/rubocop/cop/style/unneeded_percent_q.rb in rubocop-0.41.2 vs lib/rubocop/cop/style/unneeded_percent_q.rb in rubocop-0.42.0
- old
+ new
@@ -14,10 +14,11 @@
QUOTE = '"'.freeze
EMPTY = ''.freeze
PERCENT_Q = '%q'.freeze
PERCENT_CAPITAL_Q = '%Q'.freeze
STRING_INTERPOLATION_REGEXP = /#\{.+}/
+ ESCAPED_NON_BACKSLASH = /\\[^\\]/
def on_dstr(node)
check(node)
end
@@ -33,16 +34,13 @@
def check(node)
src = node.source
return unless start_with_percent_q_variant?(src)
return if src.include?(SINGLE_QUOTE) && src.include?(QUOTE)
- if src.start_with?(PERCENT_Q) && src =~ STRING_INTERPOLATION_REGEXP
- return
- end
- if src.start_with?(PERCENT_CAPITAL_Q) && acceptable_capital_q?(node)
- return
- end
+ return if src.start_with?(PERCENT_Q) && acceptable_q?(node)
+ return if src.start_with?(PERCENT_CAPITAL_Q) &&
+ acceptable_capital_q?(node)
add_offense(node, :expression)
end
def message(node)
@@ -69,9 +67,17 @@
node.loc.begin && node.loc.end
end
def start_with_percent_q_variant?(string)
string.start_with?(PERCENT_Q, PERCENT_CAPITAL_Q)
+ end
+
+ def acceptable_q?(node)
+ src = node.source
+
+ return true if src =~ STRING_INTERPOLATION_REGEXP
+
+ src.scan(/\\./).any? { |s| s =~ ESCAPED_NON_BACKSLASH }
end
def acceptable_capital_q?(node)
src = node.source
src.include?(QUOTE) &&