class RuboCop::Cop::Style::UnneededPercentQ

This cop 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

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 48
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
on_dstr(node) click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 33
def on_dstr(node)
  return unless string_literal?(node)

  check(node)
end
on_str(node) click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 39
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/unneeded_percent_q.rb, line 103
def acceptable_capital_q?(node)
  src = node.source
  src.include?(QUOTE) &&
    (src =~ STRING_INTERPOLATION_REGEXP ||
    (node.str_type? && double_quotes_required?(src)))
end
acceptable_q?(node) click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 95
def acceptable_q?(node)
  src = node.source

  return true if src =~ STRING_INTERPOLATION_REGEXP

  src.scan(/\\./).any? { |s| s =~ ESCAPED_NON_BACKSLASH }
end
allowed_percent_q?(node) click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 70
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/unneeded_percent_q.rb, line 59
def check(node)
  return unless start_with_percent_q_variant?(node)
  return if interpolated_quotes?(node) || allowed_percent_q?(node)

  add_offense(node)
end
interpolated_quotes?(node) click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 66
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/unneeded_percent_q.rb, line 76
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/unneeded_percent_q.rb, line 91
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/unneeded_percent_q.rb, line 86
def string_literal?(node)
  node.loc.respond_to?(:begin) && node.loc.respond_to?(:end) &&
    node.loc.begin && node.loc.end
end