class RuboCop::Cop::Style::RedundantPercentQ
Checks for usage of the %q/%Q syntax when '' or “” would do.
@example
# bad name = %q(Bruce Wayne) time = %q(8 o'clock) question = %q("What did you say?") # good name = 'Bruce Wayne' time = "8 o'clock" question = '"What did you say?"'
Constants
- DYNAMIC_MSG
- EMPTY
- ESCAPED_NON_BACKSLASH
- MSG
- PERCENT_CAPITAL_Q
- PERCENT_Q
- QUOTE
- SINGLE_QUOTE
- STRING_INTERPOLATION_REGEXP
Public Instance Methods
on_dstr(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 34 def on_dstr(node) return unless string_literal?(node) check(node) end
on_str(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 40 def on_str(node) # Interpolated strings that contain more than just interpolation # will call `on_dstr` for the entire string and `on_str` for the # non interpolated portion of the string return unless string_literal?(node) check(node) end
Private Instance Methods
acceptable_capital_q?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 99 def acceptable_capital_q?(node) src = node.source src.include?(QUOTE) && (STRING_INTERPOLATION_REGEXP.match?(src) || (node.str_type? && double_quotes_required?(src))) end
acceptable_q?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 91 def acceptable_q?(node) src = node.source return true if STRING_INTERPOLATION_REGEXP.match?(src) src.scan(/\\./).any? { |s| ESCAPED_NON_BACKSLASH.match?(s) } end
allowed_percent_q?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 67 def allowed_percent_q?(node) (node.source.start_with?(PERCENT_Q) && acceptable_q?(node)) || (node.source.start_with?(PERCENT_CAPITAL_Q) && acceptable_capital_q?(node)) end
check(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 51 def check(node) return unless start_with_percent_q_variant?(node) return if interpolated_quotes?(node) || allowed_percent_q?(node) add_offense(node) do |corrector| delimiter = /^%Q[^"]+$|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE corrector.replace(node.loc.begin, delimiter) corrector.replace(node.loc.end, delimiter) end end
interpolated_quotes?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 63 def interpolated_quotes?(node) node.source.include?(SINGLE_QUOTE) && node.source.include?(QUOTE) end
message(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 72 def message(node) src = node.source extra = if src.start_with?(PERCENT_CAPITAL_Q) DYNAMIC_MSG else EMPTY end format(MSG, q_type: src[0, 2], extra: extra) end
start_with_percent_q_variant?(string)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 87 def start_with_percent_q_variant?(string) string.source.start_with?(PERCENT_Q, PERCENT_CAPITAL_Q) end
string_literal?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_percent_q.rb, line 82 def string_literal?(node) node.loc.respond_to?(:begin) && node.loc.respond_to?(:end) && node.loc.begin && node.loc.end end