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

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
1 change: 1 addition & 0 deletions changelog/fix_fix_an_incorrect_autocorrect_for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1390](https://github.com/rubocop/rubocop-rails/pull/1390): Fix an incorrect autocorrect for `Rails/SelectMap` when `select` has no receiver and method chains are used. ([@masato-bkn][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/select_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def find_select_node(node, column_name)

# rubocop:disable Metrics/AbcSize
def autocorrect(corrector, select_node, node, preferred_method)
corrector.remove(select_node.loc.dot || node.loc.dot)
corrector.remove(select_node.parent.loc.dot)
corrector.remove(select_node.loc.selector.begin.join(select_node.source_range.end))
corrector.replace(node.loc.selector.begin.join(node.source_range.end), preferred_method)
end
Expand Down
13 changes: 12 additions & 1 deletion spec/rubocop/cop/rails/select_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

expect_offense(<<~RUBY)
select(:column_name).map(&:column_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `pluck(:column_name)` instead of `select` with `map`.
Expand All @@ -56,6 +56,17 @@
RUBY
end

it 'registers an offense when using `select(:column_name).where(conditions).map(&:column_name)` without receiver' do
expect_offense(<<~RUBY)
select(:column_name).where(conditions).map(&:column_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `pluck(:column_name)` instead of `select` with `map`.
RUBY

expect_correction(<<~RUBY)
where(conditions).pluck(:column_name)
RUBY
end

it 'handles safe navigation chain' do
expect_offense(<<~RUBY)
relation&.select(:column_name)&.map(&:column_name)
Expand Down