Sha256: f71b129baffd6f2ccf92ccf098da01b2aac11980ce5856c3b9c72ac46551c683

Contents?: true

Size: 827 Bytes

Versions: 4

Compression:

Stored size: 827 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class WordArray < Cop
      MSG = 'Use %w or %W for array of words.'

      def on_array(node)
        return unless node.loc.begin && node.loc.begin.source == '['

        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.line, MSG)
        end

        super
      end

      private

      def complex_content?(arr_sexp)
        arr_sexp.each do |s|
          str_content = Util.strip_quotes(s.loc.expression.source)
          return true unless str_content =~ /\A[\w-]+\z/
        end

        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/word_array.rb
rubocop-0.8.2 lib/rubocop/cop/word_array.rb
rubocop-0.8.1 lib/rubocop/cop/word_array.rb
rubocop-0.8.0 lib/rubocop/cop/word_array.rb