class RuboCop::Cop::Style::PercentQLiterals

This cop checks for usage of the %Q() syntax when %q() would do.

Public Instance Methods

on_str(node) click to toggle source
# File lib/rubocop/cop/style/percent_q_literals.rb, line 11
def on_str(node)
  process(node, '%Q', '%q')
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/percent_q_literals.rb, line 37
def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.source_range, corrected(node.source))
  end
end
check(node, msg) click to toggle source
# File lib/rubocop/cop/style/percent_q_literals.rb, line 29
def check(node, msg)
  # Report offense only if changing case doesn't change semantics,
  # i.e., if the string would become dynamic or has special characters.
  return if node.children != parse(corrected(node.source)).ast.children

  add_offense(node, :begin, msg)
end
corrected(src) click to toggle source
# File lib/rubocop/cop/style/percent_q_literals.rb, line 43
def corrected(src)
  src.sub(src[1], src[1].swapcase)
end
on_percent_literal(node) click to toggle source
# File lib/rubocop/cop/style/percent_q_literals.rb, line 17
def on_percent_literal(node)
  if style == :lower_case_q
    if type(node) == '%Q'
      check(node,
            'Do not use `%Q` unless interpolation is needed.  '                      'Use `%q`.')
    end
  elsif type(node) == '%q'
    check(node, 'Use `%Q` instead of `%q`.')
  end
end