Class: RuboCop::Cop::Vicenzo::RSpec::NestedContextImproperStart

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

Overview

Checks for nested context blocks where the inner context starts with "when", "with", or "without". It suggests replacing it with "and", "but", or "however".

Examples:

# bad
context 'when the product is for sale' do
  context 'when the color pink is not available' do
    it 'does not show the pink option'
  end
end

# good
context 'when the product is for sale' do
  context 'but the color pink is not available' do
    it 'does not show the pink option'
  end
end

Constant Summary collapse

MSG =
'Nested `context` should start with `and`, `but`, or `however`, not `when`, `with`, or `without`.'
FORBIDDEN_PREFIXES =
%w[when with without].freeze

Instance Method Summary collapse

Instance Method Details

#context_block?(node) ⇒ Object



30
31
32
# File 'lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb', line 30

def_node_matcher :context_block?, <<~PATTERN
  (block (send nil? :context ...) ...)
PATTERN

#example_group_block?(node) ⇒ Object



35
36
37
# File 'lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb', line 35

def_node_matcher :example_group_block?, <<~PATTERN
  (block (send nil? {:describe :context :feature :example_group} ...) ...)
PATTERN

#on_block(node) ⇒ Object Also known as: on_numblock



39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb', line 39

def on_block(node)
  return unless context_block?(node)

  parent = find_closest_example_group(node)

  return unless parent && context_block?(parent)

  check_description(node)
end