class Rubocop::Cop::Style::WordArray

This cop checks for array literals made up of word-like strings, that are not using the %w() syntax.

Constants

MSG

Public Instance Methods

on_array(node) click to toggle source
Calls superclass method
# File lib/rubocop/cop/style/word_array.rb, line 11
def on_array(node)
  return unless node.loc.begin && node.loc.begin.is?('[')

  array_elems = node.children

  # no need to check empty arrays
  return unless array_elems && array_elems.size > 1

  string_array = array_elems.all? { |e| e.type == :str }

  if string_array && !complex_content?(array_elems)
    add_offence(:convention, node.loc.expression, MSG)
  end

  super
end

Private Instance Methods

complex_content?(arr_sexp) click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 30
def complex_content?(arr_sexp)
  arr_sexp.each do |s|
    source = s.loc.expression.source
    unless source.start_with?('?') # %W(\r \n) can replace [?\r, ?\n]
      str_content = Util.strip_quotes(source)
      return true unless str_content =~ /\A[\w-]+\z/
    end
  end

  false
end