Sha256: 6d10d5585cebee7727a079de3678d6ee245f605b3f34d919fbbd9dea6f0bd3af

Contents?: true

Size: 1.72 KB

Versions: 28

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Identifier of all cops containing a department and cop name.
    #
    # All cops are identified by their badge. For example, the badge for
    # `RuboCop::Cop::Layout::IndentationStyle` is `Layout/IndentationStyle`.
    # Badges can be parsed as either `Department/CopName` or just `CopName` to
    # allow for badge references in source files that omit the department for
    # RuboCop to infer.
    class Badge
      attr_reader :department, :cop_name

      def self.for(class_name)
        parts = class_name.split('::')
        name_deep_enough = parts.length >= 4
        new(name_deep_enough ? parts[2..] : parts.last(2))
      end

      def self.parse(identifier)
        new(identifier.split('/').map { |i| camel_case(i) })
      end

      def self.camel_case(name_part)
        return 'RSpec' if name_part == 'rspec'

        name_part.gsub(/^\w|_\w/) { |match| match[-1, 1].upcase }
      end

      def initialize(class_name_parts)
        department_parts = class_name_parts[0...-1]
        @department = (department_parts.join('/').to_sym unless department_parts.empty?)
        @cop_name = class_name_parts.last
      end

      def ==(other)
        hash == other.hash
      end
      alias eql? ==

      def hash
        [department, cop_name].hash
      end

      def match?(other)
        cop_name == other.cop_name && (!qualified? || department == other.department)
      end

      def to_s
        @to_s ||= qualified? ? "#{department}/#{cop_name}" : cop_name
      end

      def qualified?
        !department.nil?
      end

      def with_department(department)
        self.class.new([department.to_s.split('/'), cop_name].flatten)
      end
    end
  end
end

Version data entries

28 entries across 24 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/badge.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/badge.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/badge.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/badge.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/badge.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/badge.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/badge.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/badge.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/badge.rb
rubocop-1.40.0 lib/rubocop/cop/badge.rb
rubocop-1.39.0 lib/rubocop/cop/badge.rb
rubocop-1.38.0 lib/rubocop/cop/badge.rb
rubocop-1.37.1 lib/rubocop/cop/badge.rb
rubocop-1.37.0 lib/rubocop/cop/badge.rb
rubocop-1.36.0 lib/rubocop/cop/badge.rb
rubocop-1.35.1 lib/rubocop/cop/badge.rb
rubocop-1.35.0 lib/rubocop/cop/badge.rb
rubocop-1.34.1 lib/rubocop/cop/badge.rb
rubocop-1.34.0 lib/rubocop/cop/badge.rb
rubocop-1.33.0 lib/rubocop/cop/badge.rb