Class: RuboCop::Cop::RSpec::Output

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

Overview

Checks for the use of output calls like puts and print in specs.

Examples:

# bad
puts 'A debug message'
pp 'A debug message'
print 'A debug message'

Constant Summary collapse

MSG =
'Do not write to stdout in specs.'
RESTRICT_ON_SEND =
(KERNEL_METHODS + IO_METHODS).to_a.freeze

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

#io_output?(node) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/rspec/output.rb', line 50

def_node_matcher :io_output?, "(send\n  {\n    (gvar #match_gvar?)\n    (const {nil? cbase} {:STDOUT :STDERR})\n  }\n  IO_METHODS\n  ...)\n"

#on_send(node) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



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

def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
  return if node.parent&.call_type? || node.block_node
  return if !output?(node) && !io_output?(node)
  return if node.arguments.any? { |arg| arg.type?(:hash, :block_pass) }

  add_offense(node) do |corrector|
    corrector.remove(node)
  end
end

#output?(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/rspec/output.rb', line 45

def_node_matcher :output?, "(send nil? KERNEL_METHODS ...)\n"