Skip to content

Commit 490e9e8

Browse files
authored
Merge pull request #1370 from fatkodima/select_map-safe-navigation
Change `Rails/SelectMap` to handle safe navigation operators
2 parents 1b7292e + 84d4b25 commit 490e9e8

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1195](https://github.com/rubocop/rubocop-rails/issues/1195): Change `Rails/SelectMap` to handle safe navigation operators. ([@fatkodima][])

lib/rubocop/cop/rails/select_map.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ def on_send(node)
4040
autocorrect(corrector, select_node, node, preferred_method)
4141
end
4242
end
43+
alias on_csend on_send
4344

4445
private
4546

4647
def find_select_node(node, column_name)
4748
node.descendants.detect do |select_candidate|
48-
next if !select_candidate.send_type? || !select_candidate.method?(:select)
49+
next if !select_candidate.call_type? || !select_candidate.method?(:select)
4950

5051
match_column_name?(select_candidate, column_name)
5152
end

spec/rubocop/cop/rails/select_map_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@
5656
RUBY
5757
end
5858

59+
it 'handles safe navigation chain' do
60+
expect_offense(<<~RUBY)
61+
relation&.select(:column_name)&.map(&:column_name)
62+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `pluck(:column_name)` instead of `select` with `map`.
63+
RUBY
64+
65+
expect_correction(<<~RUBY)
66+
relation&.pluck(:column_name)
67+
RUBY
68+
end
69+
5970
it 'does not register an offense when using `select(:mismatch_column_name).map(&:column_name)`' do
6071
expect_no_offenses(<<~RUBY)
6172
Model.select(:mismatch_column_name).map(&:column_name)

0 commit comments

Comments
 (0)