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.

This cop does not support autocorrection in some cases.

Examples:

# bad
describe MyClass, focus: true do
end

describe MyClass, :focus do
end

fdescribe MyClass do
end

# good
describe MyClass do
end

# bad
fdescribe 'test' do; end

# good
describe 'test' do; end

# bad
fdescribe 'test' do; end

# good
describe 'test' do; end

# bad
shared_examples 'test', focus: true do; end

# good
shared_examples 'test' do; end

# bad
shared_context 'test', focus: true do; end

# good
shared_context 'test' do; end

# bad (does not support autocorrection)
focus 'test' 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

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

Instance Method Details

#focusable_selector?(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/rspec/focus.rb', line 59

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

#focused_block?(node) ⇒ Object



77
78
79
# File 'lib/rubocop/cop/rspec/focus.rb', line 77

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

#metadata(node) ⇒ Object



71
72
73
74
# File 'lib/rubocop/cop/rspec/focus.rb', line 71

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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/rspec/focus.rb', line 81

def on_send(node)
  return if node.chained? || node.each_ancestor(:def, :defs).any?

  (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