class RuboCop::Cop::Style::UnneededPercentQ
This cop checks for usage of the %q/%Q syntax when '' or “” would do.
Constants
- DYNAMIC_MSG
- EMPTY
- 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/unneeded_percent_q.rb, line 19 def on_dstr(node) check(node) end
on_str(node)
click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 23 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 75 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
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 57 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
check(node)
click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 33 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
message(node)
click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 47 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
start_with_percent_q_variant?(string)
click to toggle source
# File lib/rubocop/cop/style/unneeded_percent_q.rb, line 71 def start_with_percent_q_variant?(string) string.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 66 def string_literal?(node) node.loc.respond_to?(:begin) && node.loc.respond_to?(:end) && node.loc.begin && node.loc.end end