Sha256: de4f5fd2b7de286696833f5ba3f8fd6db774dc43d15dee190723d1923b6c8b4b

Contents?: true

Size: 809 Bytes

Versions: 2

Compression:

Stored size: 809 Bytes

Contents

module RuboCop
  module Cop
    module Ipepe
      class AlphabeticalArrayOfStrings < ::RuboCop::Cop::Base
        extend AutoCorrector
        MSG = "Ensure that strings in array are in alphabetical order".freeze

        def on_array(node)
          str_type_hash = {}
          node.children.each do |n|
            str_type_hash[n.str_type?] ||= 0
            str_type_hash[n.str_type?] += 1
          end

          return if str_type_hash.size != 1 || str_type_hash[true].nil?

          strings = node.children.map(&:value)
          sorted_strings = strings.sort

          return if strings == sorted_strings

          add_offense(node) do |corrector|
            corrector.replace(node, "[#{sorted_strings.map { |s| "\"#{s}\"" }.join(', ')}]")
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-ipepe-0.2.5 lib/rubocop/cop/ipepe/alphabetical_array_of_strings.rb
rubocop-ipepe-0.2.0 lib/rubocop/cop/ipepe/alphabetical_array_of_strings.rb