Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Rails/Presence when a right-hand s…
Browse files Browse the repository at this point in the history
…ide of the relational operator

This PR fixes an incorrect autocorrect when there is right-hand side of the relational operator as follows:
```ruby
a < if b.present?
  b
else
  c
end
```

Expect autocorrect
```ruby
a < (b.presence || c)
```

Actual autocorrect
```ruby
a < b.presence || c
```
  • Loading branch information
ydah committed Oct 19, 2022
1 parent 7220b5c commit c8fd04d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#816](https://github.com/rubocop/rubocop-rails/pull/816): Fix an incorrect autocorrect for `Rails/Presence` when a right-hand side of the relational operator. ([@ydah][])
9 changes: 5 additions & 4 deletions lib/rubocop/cop/rails/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def on_if(node)

def register_offense(node, receiver, other)
add_offense(node, message: message(node, receiver, other)) do |corrector|
corrector.replace(node.source_range, replacement(receiver, other))
corrector.replace(node.source_range, replacement(receiver, other, node.left_sibling))
end
end

Expand All @@ -106,10 +106,10 @@ def ignore_other_node?(node)
end

def message(node, receiver, other)
format(MSG, prefer: replacement(receiver, other), current: node.source)
format(MSG, prefer: replacement(receiver, other, node.left_sibling), current: node.source)
end

def replacement(receiver, other)
def replacement(receiver, other, left_sibling)
or_source = if other&.send_type?
build_source_for_or_method(other)
elsif other.nil? || other.nil_type?
Expand All @@ -118,7 +118,8 @@ def replacement(receiver, other)
" || #{other.source}"
end

"#{receiver.source}.presence" + or_source
replaced = "#{receiver.source}.presence#{or_source}"
left_sibling ? "(#{replaced})" : replaced
end

def build_source_for_or_method(other)
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/rails/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@
CORRECTION
end

context 'when a right-hand side of the relational operator' do
%w[< > <= >= == !=].each do |operator|
it "registers an offense and corrects when `#{operator}`" do
source = <<~SOURCE.chomp
a #{operator} if b.present?
b
else
c
end
SOURCE
current = <<~CURRENT.chomp
if b.present?
b
else
c
end
CURRENT
offenses = inspect_source(source)
expect(offenses.count).to eq 1
expect(offenses).to all(have_attributes(first_line: 1, last_line: 5))
expect(offenses).to all(have_attributes(message: "Use `(b.presence || c)` instead of `#{current}`."))
expect(autocorrect_source(source)).to eq("a #{operator} (b.presence || c)")
end
end
end

it 'does not register an offense when using `#presence`' do
expect_no_offenses(<<~RUBY)
a.presence
Expand Down

0 comments on commit c8fd04d

Please sign in to comment.