Sha256: 74b5ef29262cb96def0f9659e83f85dcf298b2eac3e6b240e2e22d939c6e1c83

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop looks for duplicate values in enum declarations.
      #
      # @example
      #   # bad
      #   enum status: { active: 0, archived: 0 }
      #
      #   # good
      #   enum status: { active: 0, archived: 1 }
      #
      #   # bad
      #   enum status: [:active, :archived, :active]
      #
      #   # good
      #   enum status: [:active, :archived]
      class EnumUniqueness < Cop
        include Duplication

        MSG = 'Duplicate value `%<value>s` found in `%<enum>s` ' \
              'enum declaration.'

        def_node_matcher :enum_declaration, <<-PATTERN
          (send nil? :enum (hash (pair (_ $_) ${array hash})))
        PATTERN

        def on_send(node)
          enum_declaration(node) do |name, args|
            items = args.values

            return unless duplicates?(items)

            consecutive_duplicates(items).each do |item|
              add_offense(item, message: format(MSG, value: item.source,
                                                     enum: name))
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
rubocop-rails-2.2.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.2.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.1.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.0.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.71.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.0.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.70.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.69.0 lib/rubocop/cop/rails/enum_uniqueness.rb