Sha256: 811ffeda7e5a66545fc5a3306fdaff318974eef38896d46e9b4e6c1398d208bf

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 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 `%s` found in `%s` enum declaration.'.freeze

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

        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, :expression, format(MSG, item.source, name))
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.49.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.48.1 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.48.0 lib/rubocop/cop/rails/enum_uniqueness.rb
rubocop-0.47.1 lib/rubocop/cop/rails/enum_uniqueness.rb