Skip to content

Commit 5da1905

Browse files
sambostockbbatsov
authored andcommitted
Handle implicit receivers in Style/InvertibleUnlessCondition
Previously, we would attempt to call `node.receiver.source`, but `node.receiver` could be `nil`. This teaches the cop to handle implicit receivers.
1 parent 18004e9 commit 5da1905

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#12711](https://github.com/rubocop/rubocop/pull/12711): Handle implicit receivers in `Style/InvertibleUnlessCondition`. ([@sambostock][])

lib/rubocop/cop/style/invertible_unless_condition.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,23 @@ def preferred_condition(node)
9999
end
100100
end
101101

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
104104
return receiver_source if node.method?(:!)
105105

106+
receive = receiver_source ? "#{receiver_source}." : '' # receiver may be implicit (self)
107+
106108
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?
108110

109111
argument_list = node.arguments.map(&:source).join(', ')
110112
if node.operator_method?
111113
return "#{receiver_source} #{inverse_method_name} #{argument_list}"
112114
end
113115

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?
117117

118-
"#{receiver_source}.#{inverse_method_name} #{argument_list}"
118+
"#{receive}#{inverse_method_name} #{argument_list}"
119119
end
120120

121121
def preferred_logical_condition(node)

spec/rubocop/cop/style/invertible_unless_condition_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,39 @@
9191
end
9292
end
9393

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+
94127
it 'does not register an offense when using explicit begin condition' do
95128
expect_no_offenses(<<~RUBY)
96129
foo unless begin x != y end

0 commit comments

Comments
 (0)