Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #1234] Fix an incorrect autocorrect for Rails/FindBy #1235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1234](https://github.com/rubocop/rubocop-rails/issues/1234): Fix an incorrect autocorrect for `Rails/FindBy` when using multi-line leading dot method calls. ([@ymap][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/find_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def autocorrect(corrector, node)
return if node.method?(:first)

where_loc = node.receiver.loc.selector
first_loc = range_between(node.loc.dot.begin_pos, node.loc.selector.end_pos)
first_loc = range_between(node.receiver.source_range.end_pos, node.loc.selector.end_pos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep the intentional blank line?

Suggested change
first_loc = range_between(node.receiver.source_range.end_pos, node.loc.selector.end_pos)
first_loc = range_between(node.receiver.source_range.end_pos, node.loc.selector.end_pos)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've fixed.


corrector.replace(where_loc, 'find_by')
corrector.replace(first_loc, '')
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/rails/find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@
RUBY
end

it 'registers and corrects an offense when using multi-line leading dot method calls' do
expect_offense(<<~RUBY)
User
.where(id: x)
^^^^^^^^^^^^ Use `find_by` instead of `where.take`.
.take
RUBY

expect_correction(<<~RUBY)
User
.find_by(id: x)
RUBY
end

it 'registers and corrects an offense when using multi-line trailing dot method calls' do
expect_offense(<<~RUBY)
User.
where(id: x).
^^^^^^^^^^^^^ Use `find_by` instead of `where.take`.
take
RUBY

expect_correction(<<~RUBY)
User.
find_by(id: x)
RUBY
end

context 'when using safe navigation operator' do
it 'registers an offense when using `#take`' do
expect_offense(<<~RUBY)
Expand Down