Skip to content

Commit 5d304d0

Browse files
koicbbatsov
authored andcommitted
Fix an incorrect auto-correct for Style/InverseMethods
This PR fixes an incorrect auto-correct for `Style/InverseMethods` when using `BasicObject#!`. The following is a reproduction step. ```ruby # example.rb foo.reject { |e| e.bar?.! } ``` ```console % rubocop example.rb --only Style/InverseMethods -a Inspecting 1 file E Offenses: example.rb:1:1: C: [Corrected] Style/InverseMethods: Use select instead of inverting reject. foo.reject { |e| e.bar?.! } ^^^^^^^^^^^^^^^^^^^^^^^^^^^ example.rb:1:26: E: Lint/Syntax: unexpected token tRCURLY (Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops) foo.select { |e| e.bar?. } ^ 1 file inspected, 2 offenses detected, 1 offense corrected ``` ## Before The following is an invalid syntax. ```diff % g diff diff --git a/example.rb b/example.rb index 3efc6bf..cc6cf14 100644 --- a/example.rb +++ b/example.rb @@ -1 +1 @@ -foo.reject { |e| e.bar?.! } +foo.select { |e| e.bar?. } ``` ## After The following si a valid syntax. ```diff % g diff diff --git a/example.rb b/example.rb index 3efc6bf..cc6cf14 100644 --- a/example.rb +++ b/example.rb @@ -1 +1 @@ -foo.reject { |e| e.bar?.! } +foo.select { |e| e.bar? } ```
1 parent 666696b commit 5d304d0

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* [#6915](https://github.com/rubocop-hq/rubocop/issues/6915): Fix false positive in `Style/SafeNavigation` when a modifier if is safe guarding a method call being passed to `break`, `fail`, `next`, `raise`, `return`, `throw`, and `yield`. ([@rrosenblum][])
4141
* [#6822](https://github.com/rubocop-hq/rubocop/issues/6822): Fix Lint/LiteralInInterpolation autocorrection for single quotes. ([@hoshinotsuyoshi][])
4242
* [#6985](https://github.com/rubocop-hq/rubocop/issues/6985): Fix an incorrect auto-correct for `Lint/LiteralInInterpolation` if contains array percent literal. ([@yakout][])
43+
* [#7003](https://github.com/rubocop-hq/rubocop/pull/7003): Fix an incorrect auto-correct for `Style/InverseMethods` when using `BasicObject#!`. ([@koic][])
4344

4445
### Changes
4546

lib/rubocop/cop/style/inverse_methods.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Style
3030
# !(foo.class < Numeric) # Checking class hierarchy is allowed
3131
class InverseMethods < Cop
3232
include IgnoredNode
33+
include RangeHelp
3334

3435
MSG = 'Use `%<inverse>s` instead of inverting `%<method>s`.'.freeze
3536
CLASS_COMPARISON_METHODS = %i[<= >= < >].freeze
@@ -121,6 +122,11 @@ def correct_inverse_selector(block, corrector)
121122
selector[0] = '='
122123
corrector.replace(block.loc.selector, selector)
123124
else
125+
if block.loc.dot
126+
range = dot_range(block.loc)
127+
corrector.remove(range)
128+
end
129+
124130
corrector.remove(block.loc.selector)
125131
end
126132
end
@@ -165,6 +171,10 @@ def possible_class_hierarchy_check?(lhs, rhs, method)
165171
def camel_case_constant?(node)
166172
node.const_type? && node.source =~ CAMEL_CASE
167173
end
174+
175+
def dot_range(loc)
176+
range_between(loc.dot.begin_pos, loc.expression.end_pos)
177+
end
168178
end
169179
end
170180
end

spec/rubocop/cop/style/inverse_methods_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@
254254
expect(new_source).to eq("foo.#{inverse} { |e| e.bar? }")
255255
end
256256

257+
it 'corrects an inverted method call when using `BasicObject#!`' do
258+
new_source = autocorrect_source("foo.#{method} { |e| e.bar?.! }")
259+
260+
expect(new_source).to eq("foo.#{inverse} { |e| e.bar? }")
261+
end
262+
263+
it 'corrects an inverted method call when using `BasicObject# !`' do
264+
new_source = autocorrect_source("foo.#{method} { |e| e.bar?. ! }")
265+
266+
expect(new_source).to eq("foo.#{inverse} { |e| e.bar? }")
267+
end
268+
257269
it 'corrects a complex inverted method call' do
258270
source = "puts 1 if !foo.#{method} { |e| !e.bar? }"
259271
new_source = autocorrect_source(source)

0 commit comments

Comments
 (0)