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 an incorrect autocorrect for Rails/SelectMap when select has no receiver and method chains are used #1390

Conversation

masato-bkn
Copy link
Contributor

@masato-bkn masato-bkn commented Dec 1, 2024

This PR fixes an incorrect autocorrect for Rails/SelectMap when select has no receiver and method chains are used between select and map.

For example: select(:column_name).where(conditions).map(&:column_name)

Expected Behavior

where(conditions).pluck(:column_name)

Actual Behavior

.where(conditions)pluck(:column_name)

Steps to reproduce the problem

Run bundle ex rubocop -A --only Rails/SelectMap on the code below:

select(:column_name).where(conditions).map(&:column_name)

RuboCop version

1.67.0 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.2, running on ruby 3.2.0) [x86_64-darwin21]
  - rubocop-rails 2.27.0

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

…no receiver and method chains are used

This commit fixes an incorrect autocorrect for `Rails/SelectMap` when `select` has no receiver and method chains are used between `select` and `map`, as follows:

Before autocorrect
```ruby
select(:column_name).where(conditions).map(&:column_name)
```

After autocorrect (syntax error)
```ruby
.where(conditions)pluck(:column_name)
```
@@ -45,7 +45,7 @@
RUBY
end

it 'registers an offense when using `select(:column_name).map(&:column_name)` without receiver model' do
it 'registers an offense when using `select(:column_name).map(&:column_name)` without receiver' do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I added "model" to the test name in L59, Layout/LineLength pointed it out, so I removed it from this test name as well to keep the format consistent.

spec/rubocop/cop/rails/select_map_spec.rb:59:121: C: Layout/LineLength: Line is too long. [124/120]
  it 'registers an offense when using `select(:column_name).where(conditions).map(&:column_name)` without receiver model' do
                                                                                                                        ^^^^

Copy link
Member

Choose a reason for hiding this comment

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

I think keeping git blame intact is more valuable, but it’s unlikely that anything significant will happen here.

@masato-bkn masato-bkn marked this pull request as ready for review December 1, 2024 01:16
@koic koic merged commit e470d18 into rubocop:master Dec 1, 2024
14 checks passed
@koic
Copy link
Member

koic commented Dec 1, 2024

Thanks!

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 this pull request may close these issues.

2 participants