Sha256: 8b7dd6e3f5583ee6b4491c27b5a55d80e74f269f1bd46c8d88b502e2f03ef361

Contents?: true

Size: 1.26 KB

Versions: 18

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

18 entries across 18 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/context_method.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/context_method.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/context_method.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/context_method.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/context_method.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/context_method.rb