Sha256: 12913409c4e5ccc32270f5f21981771ee4e1535c31960c129ac0239a2664a5c2

Contents?: true

Size: 1.26 KB

Versions: 24

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # `context` should not be used for specifying methods.
      #
      # @example
      #   # bad
      #   context '#foo_bar' do
      #     # ...
      #   end
      #
      #   context '.foo_bar' do
      #     # ...
      #   end
      #
      #   # good
      #   describe '#foo_bar' do
      #     # ...
      #   end
      #
      #   describe '.foo_bar' do
      #     # ...
      #   end
      #
      class ContextMethod < Base
        extend AutoCorrector

        MSG = 'Use `describe` for testing methods.'

        # @!method context_method(node)
        def_node_matcher :context_method, <<~PATTERN
          (block
            (send #rspec? :context
              ${(str #method_name?) (dstr (str #method_name?) ...)}
            ...)
          ...)
        PATTERN

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          context_method(node) do |context|
            add_offense(context) do |corrector|
              corrector.replace(node.send_node.loc.selector, 'describe')
            end
          end
        end

        private

        def method_name?(description)
          description.start_with?('.', '#')
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/context_method.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/context_method.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/context_method.rb