File tree Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change
1
+ * [ #1041 ] ( https://github.com/rubocop/rubocop-rails/issues/1041 ) : Fix a false positive for ` Rails/Output ` when output method is called with block argument. ([ @koic ] [ ] )
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ class Output < Base
23
23
24
24
MSG = "Do not write to stdout. Use Rails's logger if you want to log."
25
25
RESTRICT_ON_SEND = %i[ ap p pp pretty_print print puts binwrite syswrite write write_nonblock ] . freeze
26
+ ALLOWED_TYPES = %i[ send csend block numblock ] . freeze
26
27
27
28
def_node_matcher :output? , <<~PATTERN
28
29
(send nil? {:ap :p :pp :pretty_print :print :puts} ...)
@@ -39,8 +40,8 @@ class Output < Base
39
40
PATTERN
40
41
41
42
def on_send ( node )
42
- return if node . parent &.call_type?
43
- return unless output? ( node ) || io_output? ( node )
43
+ return if ALLOWED_TYPES . include? ( node . parent &.type )
44
+ return if ! output? ( node ) && ! io_output? ( node )
44
45
45
46
range = offense_range ( node )
46
47
Original file line number Diff line number Diff line change 140
140
RUBY
141
141
end
142
142
143
+ it 'does not register an offense when the `p` method is called with block argument' do
144
+ expect_no_offenses ( <<~RUBY )
145
+ # phlex-rails gem.
146
+ div do
147
+ p { 'Some text' }
148
+ end
149
+ RUBY
150
+ end
151
+
152
+ it 'does not register an offense when io method is called with block argument' do
153
+ expect_no_offenses ( <<~RUBY )
154
+ obj.write { do_somethig }
155
+ RUBY
156
+ end
157
+
158
+ it 'does not register an offense when io method is called with numbered block argument' do
159
+ expect_no_offenses ( <<~RUBY )
160
+ obj.write { do_something(_1) }
161
+ RUBY
162
+ end
163
+
143
164
it 'does not register an offense when a method is ' \
144
165
'safe navigation called to a local variable with the same name as a print method' do
145
166
expect_no_offenses ( <<~RUBY )
You can’t perform that action at this time.
0 commit comments