Sha256: 113bb2623c6ed379fb59b5152fece1cc061114127619ac85caee10a904226ced

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for array literals made up of word-like
      # strings, that are not using the %w() syntax.
      class WordArray < Cop
        MSG = 'Use %w or %W for array of words.'

        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)
            convention(node, :expression)
          end
        end

        private

        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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.14.1 lib/rubocop/cop/style/word_array.rb
rubocop-0.14.0 lib/rubocop/cop/style/word_array.rb
rubocop-0.13.1 lib/rubocop/cop/style/word_array.rb
rubocop-0.13.0 lib/rubocop/cop/style/word_array.rb