Class: RuboCop::Cop::RSpec::BeNil

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rspec/be_nil.rb

Overview

Ensures a consistent style is used when matching ‘nil`.

You can either use the more specific ‘be_nil` matcher, or the more generic `be` matcher with a `nil` argument.

This cop can be configured using the ‘EnforcedStyle` option

Examples:

‘EnforcedStyle: be_nil` (default)

# bad
expect(foo).to be(nil)

# good
expect(foo).to be_nil

‘EnforcedStyle: be`

# bad
expect(foo).to be_nil

# good
expect(foo).to be(nil)

Constant Summary collapse

BE_MSG =
'Prefer `be(nil)` over `be_nil`.'
BE_NIL_MSG =
'Prefer `be_nil` over `be(nil)`.'
RESTRICT_ON_SEND =
%i[be be_nil].freeze

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

#be_nil_matcher?(node) ⇒ Object



36
37
38
# File 'lib/rubocop/cop/rspec/be_nil.rb', line 36

def_node_matcher :be_nil_matcher?, <<~PATTERN
  (send nil? :be_nil)
PATTERN

#nil_value_expectation?(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/rspec/be_nil.rb', line 41

def_node_matcher :nil_value_expectation?, <<~PATTERN
  (send nil? :be nil)
PATTERN

#on_send(node) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rspec/be_nil.rb', line 45

def on_send(node)
  case style
  when :be
    check_be_style(node)
  when :be_nil
    check_be_nil_style(node)
  end
end