Sha256: 465ac49f6eed66c7e56ce307c10c39427ac51debff5884714b7fdbe02abbe241

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

module RuboCop
  module Cop # rubocop:disable Style/Documentation
    WorkaroundCop = Cop.dup

    # Clone of the the normal RuboCop::Cop::Cop class so we can rewrite
    # the inherited method without breaking functionality
    class WorkaroundCop
      # Overwrite the cop inherited method to be a noop. Our RSpec::Cop
      # class will invoke the inherited hook instead
      def self.inherited(*)
      end

      # Special case `Module#<` so that the rspec support rubocop exports
      # is compatible with our subclass
      def self.<(other)
        other.equal?(RuboCop::Cop::Cop) || super
      end
    end
    private_constant(:WorkaroundCop)

    module RSpec
      # @abstract parent class to rspec cops
      #
      # The criteria for whether rubocop-rspec analyzes a certain ruby file
      # is configured via `AllCops/RSpec`. For example, if you want to
      # customize your project to scan all files within a `test/` directory
      # then you could add this to your configuration:
      #
      # @example configuring analyzed paths
      #
      #   AllCops:
      #     RSpec:
      #       Patterns:
      #       - '_test.rb$'
      #       - '(?:^|/)test/'
      class Cop < WorkaroundCop
        DEFAULT_CONFIGURATION =
          RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')

        include RuboCop::RSpec::Language, RuboCop::RSpec::Language::NodePattern

        # Invoke the original inherited hook so our cops are recognized
        def self.inherited(subclass)
          RuboCop::Cop::Cop.inherited(subclass)
        end

        def relevant_file?(file)
          rspec_pattern =~ file && super
        end

        private

        def rspec_pattern
          Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new)))
        end

        def rspec_pattern_config
          config
            .for_all_cops
            .fetch('RSpec', DEFAULT_CONFIGURATION)
            .fetch('Patterns')
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.9.1 lib/rubocop/cop/rspec/cop.rb
rubocop-rspec-1.9.0 lib/rubocop/cop/rspec/cop.rb