Sha256: 419eaf5f18fb1d6920d6beb974671db3aed2ac52dd4ef38bf47e063842b07975

Contents?: true

Size: 1.54 KB

Versions: 21

Compression:

Stored size: 1.54 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 < Base
        include Duplication

        MSG = 'Duplicate value `%<value>s` found in `%<enum>s` ' \
              'enum declaration.'
        RESTRICT_ON_SEND = %i[enum].freeze

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

        def_node_matcher :enum_values, <<~PATTERN
          (pair $_ ${array hash})
        PATTERN

        def on_send(node)
          enum?(node) do |pairs|
            pairs.each do |pair|
              enum_values(pair) do |key, args|
                items = args.values

                next unless duplicates?(items)

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

        private

        def enum_name(key)
          case key.type
          when :sym, :str
            key.value
          else
            key.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_uniqueness.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/enum_uniqueness.rb