Class: RuboCop::Cop::RSpec::LetBeforeExamples

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

Overview

Checks for ‘let` definitions that come after an example.

Examples:

# bad
let(:foo) { bar }

it 'checks what foo does' do
  expect(foo).to be
end

let(:some) { other }

it 'checks what some does' do
  expect(some).to be
end

# good
let(:foo) { bar }
let(:some) { other }

it 'checks what foo does' do
  expect(foo).to be
end

it 'checks what some does' do
  expect(some).to be
end

Constant Summary collapse

MSG =
'Move `let` before the examples in the group.'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

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

Class Method Details

.autocorrect_incompatible_withObject



54
55
56
# File 'lib/rubocop/cop/rspec/let_before_examples.rb', line 54

def self.autocorrect_incompatible_with
  [RSpec::ScatteredLet]
end

Instance Method Details

#example_or_group?(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/rubocop/cop/rspec/let_before_examples.rb', line 39

def_node_matcher :example_or_group?, <<~PATTERN
  {
    (block (send nil? {#ExampleGroups.all #Examples.all} ...) ...)
    (send nil? #Includes.examples ...)
  }
PATTERN

#include_examples?(node) ⇒ Object



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

def_node_matcher :include_examples?, <<~PATTERN
  {
    (block (send nil? :include_examples ...) ...)
    (send nil? :include_examples ...)
  }
PATTERN

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



58
59
60
61
62
# File 'lib/rubocop/cop/rspec/let_before_examples.rb', line 58

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

  check_let_declarations(node.body) if multiline_block?(node.body)
end