class RuboCop::Cop::Style::UnneededPercentQ
This cop checks for usage of the %q/%Q syntax when ” or “” would do.
def acceptable_capital_q?(node)
def acceptable_capital_q?(node) src = node.source src.include?(QUOTE) && (src =~ STRING_INTERPOLATION_REGEXP || (node.str_type? && double_quotes_acceptable?(node.str_content))) end
def autocorrect(node)
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
def check(node)
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 add_offense(node, :expression) end
def message(node)
def message(node) src = node.source extra = if src.start_with?(PERCENT_CAPITAL_Q) DYNAMIC_MSG else EMPTY end format(MSG, src[0, 2], extra) end
def on_dstr(node)
def on_dstr(node) check(node) end
def on_str(node)
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
def start_with_percent_q_variant?(string)
def start_with_percent_q_variant?(string) string.start_with?(PERCENT_Q, PERCENT_CAPITAL_Q) end
def string_literal?(node)
def string_literal?(node) node.loc.respond_to?(:begin) && node.loc.respond_to?(:end) && node.loc.begin && node.loc.end end