Sha256: 91ea789843504e31cff3dd879ac1aa6d93b48f6070da3c296363a03615c25b4f

Contents?: true

Size: 1.94 KB

Versions: 29

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check that implicit block expectation syntax is not used.
      #
      # Prefer using explicit block expectations.
      #
      # @example
      #   # bad
      #   subject { -> { do_something } }
      #   it { is_expected.to change(something).to(new_value) }
      #
      #   # good
      #   it 'changes something to a new value' do
      #     expect { do_something }.to change(something).to(new_value)
      #   end
      #
      class ImplicitBlockExpectation < Base
        MSG = 'Avoid implicit block expectations.'
        RESTRICT_ON_SEND = %i[is_expected should should_not].freeze

        # @!method lambda?(node)
        def_node_matcher :lambda?, <<-PATTERN
          {
            (send (const nil? :Proc) :new)
            (send nil? {:proc :lambda})
          }
        PATTERN

        # @!method lambda_subject?(node)
        def_node_matcher :lambda_subject?, '(block #lambda? ...)'

        # @!method implicit_expect(node)
        def_node_matcher :implicit_expect, <<-PATTERN
          $(send nil? {:is_expected :should :should_not} ...)
        PATTERN

        def on_send(node)
          implicit_expect(node) do |implicit_expect|
            subject = nearest_subject(implicit_expect)
            add_offense(implicit_expect) if lambda_subject?(subject&.body)
          end
        end

        private

        def nearest_subject(node)
          node
            .each_ancestor(:block)
            .lazy
            .select { |block_node| multi_statement_example_group?(block_node) }
            .map { |block_node| find_subject(block_node) }
            .find(&:itself)
        end

        def multi_statement_example_group?(node)
          example_group_with_body?(node) && node.body.begin_type?
        end

        def find_subject(block_node)
          block_node.body.child_nodes.find { |send_node| subject?(send_node) }
        end
      end
    end
  end
end

Version data entries

29 entries across 27 versions & 4 rubygems

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