Sha256: 75e3cba461d2c35b44dabf1d76808b52e9a60faebd88638113aa01ecfe6c15c5

Contents?: true

Size: 1.95 KB

Versions: 39

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if there is a let/subject that overwrites an existing one.
      #
      # @example
      #   # bad
      #   let(:foo) { bar }
      #   let(:foo) { baz }
      #
      #   subject(:foo) { bar }
      #   let(:foo) { baz }
      #
      #   let(:foo) { bar }
      #   let!(:foo) { baz }
      #
      #   # good
      #   subject(:test) { something }
      #   let(:foo) { bar }
      #   let(:baz) { baz }
      #   let!(:other) { other }
      #
      class OverwritingSetup < Base
        MSG = '`%<name>s` is already defined.'

        # @!method setup?(node)
        def_node_matcher :setup?, <<~PATTERN
          (block (send nil? {#Helpers.all #Subjects.all} ...) ...)
        PATTERN

        # @!method first_argument_name(node)
        def_node_matcher :first_argument_name, '(send _ _ ({str sym} $_))'

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless example_group_with_body?(node)

          find_duplicates(node.body) do |duplicate, name|
            add_offense(
              duplicate,
              message: format(MSG, name: name)
            )
          end
        end

        private

        def find_duplicates(node)
          setup_expressions = Set.new
          node.each_child_node(:block) do |child|
            next unless common_setup?(child)

            name = if child.send_node.arguments?
                     first_argument_name(child.send_node).to_sym
                   else
                     :subject
                   end

            yield child, name unless setup_expressions.add?(name)
          end
        end

        def common_setup?(node)
          return false unless setup?(node)

          # Search only for setup with basic_literal arguments (e.g. :sym, :str)
          # or no arguments at all.
          node.send_node.arguments.all?(&:basic_literal?)
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 7 rubygems

Version Path
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/overwriting_setup.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/overwriting_setup.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/overwriting_setup.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/overwriting_setup.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/overwriting_setup.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/overwriting_setup.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/overwriting_setup.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/overwriting_setup.rb