Sha256: f5d82764462a2358104affd73900d4b7ef8e184beb6b9e2ddae0f272cf73df9d

Contents?: true

Size: 1.84 KB

Versions: 25

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # 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, "{#{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

25 entries across 24 versions & 6 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/enum_hash.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/enum_hash.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/enum_hash.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.21.0 lib/rubocop/cop/rails/enum_hash.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/enum_hash.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/enum_hash.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/enum_hash.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/enum_hash.rb
rubocop-rails-2.20.2 lib/rubocop/cop/rails/enum_hash.rb