Class: RuboCop::Cop::RSpec::Focus

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

Overview

Checks if examples are focused.

Examples:

# bad
describe MyClass, focus: true do
end

describe MyClass, :focus do
end

fdescribe MyClass do
end

# good
describe MyClass do
end

Constant Summary collapse

MSG =
'Focused spec found.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

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

Instance Method Details

#focusable_selector?(node) ⇒ Object


29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec/focus.rb', line 29

def_node_matcher :focusable_selector?, <<-PATTERN
  {
    #ExampleGroups.regular
    #ExampleGroups.skipped
    #Examples.regular
    #Examples.skipped
    #Examples.pending
  }
PATTERN

#focused_block?(node) ⇒ Object


46
47
48
49
# File 'lib/rubocop/cop/rspec/focus.rb', line 46

def_node_matcher :focused_block?,
send_pattern(<<~PATTERN)
  {#ExampleGroups.focused #Examples.focused}
PATTERN

#metadata(node) ⇒ Object


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

def_node_matcher :metadata, <<-PATTERN
  {(send #rspec? #focusable_selector? <$(sym :focus) ...>)
   (send #rspec? #focusable_selector? ... (hash <$(pair (sym :focus) true) ...>))}
PATTERN

#on_send(node) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubocop/cop/rspec/focus.rb', line 51

def on_send(node)
  (node) do |focus|
    add_offense(focus) do |corrector|
      if focus.pair_type? || focus.str_type? || focus.sym_type?
        corrector.remove(with_surrounding(focus))
      elsif focus.send_type?
        correct_send(corrector, focus)
      end
    end
  end
end