class RuboCop::Cop::Lint::PercentSymbolArray

This cop checks for colons and commas in %i, e.g. `%i(: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 symbols.

@example

# bad

%i(:foo, :bar)

@example

# good

%i(foo bar)

Constants

MSG

Public Instance Methods

on_array(node) click to toggle source
# File lib/rubocop/cop/lint/percent_symbol_array.rb, line 28
def on_array(node)
  process(node, '%i', '%I')
end
on_percent_literal(node) click to toggle source
# File lib/rubocop/cop/lint/percent_symbol_array.rb, line 32
def on_percent_literal(node)
  return unless contains_colons_or_commas?(node)

  add_offense(node)
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/lint/percent_symbol_array.rb, line 52
def autocorrect(node)
  lambda do |corrector|
    node.children.each do |child|
      range = child.loc.expression

      corrector.remove_trailing(range, 1) if /,$/ =~ range.source
      corrector.remove_leading(range, 1) if /^:/ =~ range.source
    end
  end
end
contains_colons_or_commas?(node) click to toggle source
# File lib/rubocop/cop/lint/percent_symbol_array.rb, line 40
def contains_colons_or_commas?(node)
  patterns = [/^:/, /,$/]
  node.children.any? do |child|
    literal = child.children.first

    # To avoid likely false positives (e.g. a single ' or ")
    next if literal.to_s.gsub(/[^\p{Alnum}]/, '').empty?

    patterns.any? { |pat| literal =~ pat }
  end
end