Class: RuboCop::Cop::Rails::Output

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

Overview

Checks for the use of output calls like puts and print

Examples:

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

# good
Rails.logger.debug 'A debug message'

Constant Summary collapse

MSG =
"Do not write to stdout. Use Rails's logger if you want to log."
RESTRICT_ON_SEND =
%i[ap p pp pretty_print print puts binwrite syswrite write write_nonblock].freeze
ALLOWED_TYPES =
%i[send csend block numblock].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/rails/output.rb', line 42

def on_send(node)
  return if ALLOWED_TYPES.include?(node.parent&.type)
  return if !output?(node) && !io_output?(node)

  range = offense_range(node)

  add_offense(range) do |corrector|
    corrector.replace(range, 'Rails.logger.debug')
  end
end