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

Incorrect autocorrect for Rails/FindBy cop #1234

Closed
ymap opened this issue Feb 1, 2024 · 0 comments · Fixed by #1235
Closed

Incorrect autocorrect for Rails/FindBy cop #1234

ymap opened this issue Feb 1, 2024 · 0 comments · Fixed by #1235

Comments

@ymap
Copy link
Contributor

ymap commented Feb 1, 2024

I've noticed an issue with the Rails/FindBy cop where it incorrectly leaves a redundant newline when autocorrecting an invocation of take or first. This occurs when these methods are called on a separate line with a leading dot. This issue results in a syntax error if there's another method call following it, as illustrated below.

User
  .where(id: x)
  .take
  .nil?

Expected behavior

After autocorrection, the code should appear as follows, without any additional newlines:

User
  .find_by(id: x)
  .nil?

Actual behavior

However, the autocorrection currently results in an unnecessary newline, causing a syntax error:

User
  .find_by(id: x)
  
  .nil?

Steps to reproduce the problem

  1. Save the initial code snippet into a file named example.rb.
  2. Execute bundle exec rubocop --only Rails/FindBy -a example.rb.

RuboCop version

$ bundle exec rubocop -V
1.60.2 (using Parser [3.3.0.5](http://3.3.0.5/), rubocop-ast 1.30.0, running on ruby 3.0.2) [x86_64-linux]
  - rubocop-performance 1.20.2
  - rubocop-rails 2.23.1
  - rubocop-rspec 2.26.1
ymap added a commit to ymap/rubocop-rails that referenced this issue Feb 1, 2024
Fixes rubocop#1234.

This PR fixes an incorrect autocorrect for `Rails/FindBy`
when using multi-line leading dot method calls.
ymap added a commit to ymap/rubocop-rails that referenced this issue Feb 1, 2024
Fixes rubocop#1234.

This PR fixes an incorrect autocorrect for `Rails/FindBy`
when using multi-line leading dot method calls.
@koic koic closed this as completed in #1235 Feb 1, 2024
koic added a commit that referenced this issue Feb 1, 2024
…ails_find_by

[Fix #1234] Fix an incorrect autocorrect for `Rails/FindBy`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant