class RubbyCop::Cop::Style::WordArray

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

Alternatively, it can check for uses of the %w() syntax, in projects which do not want to include that syntax.

Configuration option: MinSize If set, arrays with fewer elements than this value will not trigger the cop. For example, a `MinSize of `3` will not enforce a style on an array of 2 or fewer elements.

@example

EnforcedStyle: percent (default)

# good
%w[foo bar baz]

# bad
['foo', 'bar', 'baz']

@example

EnforcedStyle: brackets

# good
['foo', 'bar', 'baz']

# bad
%w[foo bar baz]

Constants

ARRAY_MSG
PERCENT_MSG
QUESTION_MARK_SIZE

Attributes

largest_brackets[RW]

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 56
def autocorrect(node)
  if style == :percent
    correct_percent(node, 'w')
  else
    correct_bracketed(node)
  end
end
on_array(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 48
def on_array(node)
  if bracketed_array_of?(:str, node)
    check_bracketed_array(node)
  elsif node.percent_literal?(:string)
    check_percent_array(node)
  end
end

Private Instance Methods

check_bracketed_array(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 66
def check_bracketed_array(node)
  return if complex_content?(node.values) ||
            comments_in_array?(node) ||
            below_array_length?(node)

  array_style_detected(:brackets, node.values.size)
  add_offense(node, :expression, PERCENT_MSG) if style == :percent
end
check_percent_array(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 75
def check_percent_array(node)
  array_style_detected(:percent, node.values.size)
  add_offense(node, :expression, ARRAY_MSG) if style == :brackets
end
comments_in_array?(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 84
def comments_in_array?(node)
  comments = processed_source.comments
  array_range = node.source_range.to_a

  comments.any? do |comment|
    !(comment.loc.expression.to_a & array_range).empty?
  end
end
complex_content?(strings) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 93
def complex_content?(strings)
  strings.any? do |s|
    string = s.str_content
    !string.valid_encoding? || string !~ word_regex || string =~ / /
  end
end
correct_bracketed(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 104
def correct_bracketed(node)
  words = node.children.map { |w| to_string_literal(w.children[0]) }

  lambda do |corrector|
    corrector.replace(node.source_range, "[#{words.join(', ')}]")
  end
end
percent_syntax?(node) click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 80
def percent_syntax?(node)
  node.loc.begin && node.loc.begin.source =~ /\A%[wW]/
end
word_regex() click to toggle source
# File lib/rubbycop/cop/style/word_array.rb, line 100
def word_regex
  Regexp.new(cop_config['WordRegex'])
end