lib/rubocop/cop/style/unneeded_percent_q.rb in rubocop-0.51.0 vs lib/rubocop/cop/style/unneeded_percent_q.rb in rubocop-0.52.0

- old
+ new

@@ -3,12 +3,12 @@ module RuboCop module Cop module Style # This cop checks for usage of the %q/%Q syntax when '' or "" would do. class UnneededPercentQ < Cop - MSG = 'Use `%s` only for strings that contain both single quotes and ' \ - 'double quotes%s.'.freeze + MSG = 'Use `%<q_type>s` only for strings that contain both ' \ + 'single quotes and double quotes%<extra>s.'.freeze DYNAMIC_MSG = ', or for dynamic strings that contain ' \ 'double quotes'.freeze SINGLE_QUOTE = "'".freeze QUOTE = '"'.freeze EMPTY = ''.freeze @@ -16,10 +16,11 @@ PERCENT_CAPITAL_Q = '%Q'.freeze STRING_INTERPOLATION_REGEXP = /#\{.+}/ ESCAPED_NON_BACKSLASH = /\\[^\\]/ def on_dstr(node) + return unless string_literal?(node) check(node) end def on_str(node) # Interpolated strings that contain more than just interpolation @@ -27,10 +28,19 @@ # non interpolated portion of the string return unless string_literal?(node) check(node) end + def autocorrect(node) + delimiter = + node.source =~ /^%Q[^"]+$|'/ ? QUOTE : SINGLE_QUOTE + lambda do |corrector| + corrector.replace(node.loc.begin, delimiter) + corrector.replace(node.loc.end, delimiter) + end + end + private def check(node) return unless start_with_percent_q_variant?(node) return if interpolated_quotes?(node) || allowed_percent_q?(node) @@ -53,19 +63,10 @@ extra = if src.start_with?(PERCENT_CAPITAL_Q) DYNAMIC_MSG else EMPTY end - format(MSG, src[0, 2], extra) - end - - def autocorrect(node) - delimiter = - node.source =~ /^%Q[^"]+$|'/ ? QUOTE : SINGLE_QUOTE - lambda do |corrector| - corrector.replace(node.loc.begin, delimiter) - corrector.replace(node.loc.end, delimiter) - end + format(MSG, q_type: src[0, 2], extra: extra) end def string_literal?(node) node.loc.respond_to?(:begin) && node.loc.respond_to?(:end) && node.loc.begin && node.loc.end