forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.rb
52 lines (45 loc) · 1.3 KB
/
output.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks for the use of output calls like puts and print
#
# @example
# # bad
# puts 'A debug message'
# pp 'A debug message'
# print 'A debug message'
#
# # good
# Rails.logger.debug 'A debug message'
class Output < Base
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
def_node_matcher :output?, <<~PATTERN
(send nil? {:ap :p :pp :pretty_print :print :puts} ...)
PATTERN
def_node_matcher :io_output?, <<~PATTERN
(send
{
(gvar #match_gvar?)
{(const nil? :STDOUT) (const nil? :STDERR)}
}
{:binwrite :syswrite :write :write_nonblock}
...)
PATTERN
def on_send(node)
return unless (output?(node) || io_output?(node)) &&
node.arguments?
add_offense(node.loc.selector)
end
private
def match_gvar?(sym)
%i[$stdout $stderr].include?(sym)
end
end
end
end
end