Class: RuboCop::Cop::RSpec::OverwritingSetup

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/overwriting_setup.rb

Overview

Checks if there is a let/subject that overwrites an existing one.

Examples:

# 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 }

Constant Summary collapse

MSG =
'`%<name>s` is already defined.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#first_argument_name(node) ⇒ Object



34
# File 'lib/rubocop/cop/rspec/overwriting_setup.rb', line 34

def_node_matcher :first_argument_name, '(send _ _ ({str sym} $_))'

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/rspec/overwriting_setup.rb', line 36

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

#setup?(node) ⇒ Object



29
30
31
# File 'lib/rubocop/cop/rspec/overwriting_setup.rb', line 29

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