Sha256: 57bc7261060177f675d41a8705f668e252456af5e2beb874854131fcf87256df

Contents?: true

Size: 1.88 KB

Versions: 21

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop looks for enums written with array syntax.
      #
      # When using array syntax, adding an element in a
      # position other than the last causes all previous
      # definitions to shift. Explicitly specifying the
      # value for each key prevents this from happening.
      #
      # @example
      #   # bad
      #   enum status: [:active, :archived]
      #
      #   # good
      #   enum status: { active: 0, archived: 1 }
      #
      class EnumHash < Base
        extend AutoCorrector

        MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '\
              'Use hash syntax instead.'
        RESTRICT_ON_SEND = %i[enum].freeze

        def_node_matcher :enum?, <<~PATTERN
          (send nil? :enum (hash $...))
        PATTERN

        def_node_matcher :array_pair?, <<~PATTERN
          (pair $_ $array)
        PATTERN

        def on_send(node)
          enum?(node) do |pairs|
            pairs.each do |pair|
              key, array = array_pair?(pair)
              next unless key

              add_offense(array, message: format(MSG, enum: enum_name(key))) do |corrector|
                hash = array.children.each_with_index.map do |elem, index|
                  "#{source(elem)} => #{index}"
                end.join(', ')

                corrector.replace(array.loc.expression, "{#{hash}}")
              end
            end
          end
        end

        private

        def enum_name(key)
          case key.type
          when :sym, :str
            key.value
          else
            key.source
          end
        end

        def source(elem)
          case elem.type
          when :str
            elem.value.dump
          when :sym
            elem.value.inspect
          else
            elem.source
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/enum_hash.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/enum_hash.rb