class RubbyCop::Cop::Lint::PercentStringArray
This cop checks for quotes and commas in %w, e.g. `%w('foo', “bar”)`
It is more likely that the additional characters are unintended (for example, mistranslating an array of literals to percent string notation) rather than meant to be part of the resulting strings.
@example
# bad %w('foo', "bar")
@example
# good %w(foo bar)
Constants
- LEADING_QUOTE
- MSG
- QUOTES_AND_COMMAS
- TRAILING_QUOTE
Public Instance Methods
on_array(node)
click to toggle source
# File lib/rubbycop/cop/lint/percent_string_array.rb, line 33 def on_array(node) process(node, '%w', '%W') end
on_percent_literal(node)
click to toggle source
# File lib/rubbycop/cop/lint/percent_string_array.rb, line 37 def on_percent_literal(node) return unless contains_quotes_or_commas?(node) add_offense(node, :expression, MSG) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubbycop/cop/lint/percent_string_array.rb, line 56 def autocorrect(node) lambda do |corrector| node.values.each do |value| range = value.loc.expression match = range.source.match(TRAILING_QUOTE) corrector.remove_trailing(range, match[0].length) if match if range.source =~ LEADING_QUOTE corrector.remove_leading(range, 1) end end end end
contains_quotes_or_commas?(node)
click to toggle source
# File lib/rubbycop/cop/lint/percent_string_array.rb, line 45 def contains_quotes_or_commas?(node) node.values.any? do |value| literal = scrub_string(value.children.first.to_s) # To avoid likely false positives (e.g. a single ' or ") next if literal.gsub(/[^\p{Alnum}]/, '').empty? QUOTES_AND_COMMAS.any? { |pat| literal =~ pat } end end
scrub_string(string)
click to toggle source
# File lib/rubbycop/cop/lint/percent_string_array.rb, line 71 def scrub_string(string) if string.respond_to?(:scrub) string.scrub else string .encode('UTF-16BE', 'UTF-8', invalid: :replace, undef: :replace, replace: '?') .encode('UTF-8') end end