File tree Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Original file line number Diff line number Diff line change
1
+ * [ #12711 ] ( https://github.com/rubocop/rubocop/pull/12711 ) : Handle implicit receivers in ` Style/InvertibleUnlessCondition ` . ([ @sambostock ] [ ] )
Original file line number Diff line number Diff line change @@ -99,23 +99,23 @@ def preferred_condition(node)
99
99
end
100
100
end
101
101
102
- def preferred_send_condition ( node )
103
- receiver_source = node . receiver . source
102
+ def preferred_send_condition ( node ) # rubocop:disable Metrics/CyclomaticComplexity
103
+ receiver_source = node . receiver & .source
104
104
return receiver_source if node . method? ( :! )
105
105
106
+ receive = receiver_source ? "#{ receiver_source } ." : '' # receiver may be implicit (self)
107
+
106
108
inverse_method_name = inverse_methods [ node . method_name ]
107
- return "#{ receiver_source } . #{ inverse_method_name } " unless node . arguments?
109
+ return "#{ receive } #{ inverse_method_name } " unless node . arguments?
108
110
109
111
argument_list = node . arguments . map ( &:source ) . join ( ', ' )
110
112
if node . operator_method?
111
113
return "#{ receiver_source } #{ inverse_method_name } #{ argument_list } "
112
114
end
113
115
114
- if node . parenthesized?
115
- return "#{ receiver_source } .#{ inverse_method_name } (#{ argument_list } )"
116
- end
116
+ return "#{ receive } #{ inverse_method_name } (#{ argument_list } )" if node . parenthesized?
117
117
118
- "#{ receiver_source } . #{ inverse_method_name } #{ argument_list } "
118
+ "#{ receive } #{ inverse_method_name } #{ argument_list } "
119
119
end
120
120
121
121
def preferred_logical_condition ( node )
Original file line number Diff line number Diff line change 91
91
end
92
92
end
93
93
94
+ it 'registers an offense and corrects methods without arguments called with implicit receivers' do
95
+ expect_offense ( <<~RUBY )
96
+ foo unless odd?
97
+ ^^^^^^^^^^^^^^^ Prefer `if even?` over `unless odd?`.
98
+ RUBY
99
+
100
+ expect_correction ( <<~RUBY )
101
+ foo if even?
102
+ RUBY
103
+ end
104
+
105
+ it 'registers an offense and corrects parenthesized methods with arguments called with implicit receivers' do
106
+ expect_offense ( <<~RUBY )
107
+ foo unless include?(value)
108
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `if exclude?(value)` over `unless include?(value)`.
109
+ RUBY
110
+
111
+ expect_correction ( <<~RUBY )
112
+ foo if exclude?(value)
113
+ RUBY
114
+ end
115
+
116
+ it 'registers an offense and corrects unparenthesized methods with arguments called with implicit receivers' do
117
+ expect_offense ( <<~RUBY )
118
+ foo unless include? value
119
+ ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `if exclude? value` over `unless include? value`.
120
+ RUBY
121
+
122
+ expect_correction ( <<~RUBY )
123
+ foo if exclude? value
124
+ RUBY
125
+ end
126
+
94
127
it 'does not register an offense when using explicit begin condition' do
95
128
expect_no_offenses ( <<~RUBY )
96
129
foo unless begin x != y end
You can’t perform that action at this time.
0 commit comments