Skip to content

Commit cc81598

Browse files
authored
Merge pull request #1204 from koic/make_some_cops_aware_of_safe_navigation_operator
Make some cops aware of safe navigation operator
2 parents a3db656 + 7c672a0 commit cc81598

22 files changed

+301
-62
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1204](https://github.com/rubocop/rubocop-rails/pull/1204): Make `Rails/ActiveSupportAliases`, `Rails/FindBy`, `Rails/FindById`, `Rails/Inquiry`, `Rails/Pick` `Rails/PluckId`, `Rails/PluckInWhere`, `Rails/WhereEquals`, `Rails/WhereExists`, and `Rails/WhereNot` cops aware of safe navigation operator. ([@koic][])

lib/rubocop/cop/mixin/active_record_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def polymorphic?(belongs_to)
9898
end
9999

100100
def in_where?(node)
101-
send_node = node.each_ancestor(:send).first
101+
send_node = node.each_ancestor(:send, :csend).first
102102
return false unless send_node
103103

104104
return true if WHERE_METHODS.include?(send_node.method_name)

lib/rubocop/cop/rails/active_support_aliases.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class ActiveSupportAliases < Base
2727

2828
ALIASES = {
2929
starts_with?: {
30-
original: :start_with?, matcher: '(send str :starts_with? _)'
30+
original: :start_with?, matcher: '(call str :starts_with? _)'
3131
},
3232
ends_with?: {
33-
original: :end_with?, matcher: '(send str :ends_with? _)'
33+
original: :end_with?, matcher: '(call str :ends_with? _)'
3434
},
35-
append: { original: :<<, matcher: '(send array :append _)' },
36-
prepend: { original: :unshift, matcher: '(send array :prepend _)' }
35+
append: { original: :<<, matcher: '(call array :append _)' },
36+
prepend: { original: :unshift, matcher: '(call array :prepend _)' }
3737
}.freeze
3838

3939
ALIASES.each do |aliased_method, options|
@@ -47,13 +47,14 @@ def on_send(node)
4747
preferred_method = ALIASES[aliased_method][:original]
4848
message = format(MSG, prefer: preferred_method, current: aliased_method)
4949

50-
add_offense(node, message: message) do |corrector|
50+
add_offense(node.loc.selector.join(node.source_range.end), message: message) do |corrector|
5151
next if append(node)
5252

5353
corrector.replace(node.loc.selector, preferred_method)
5454
end
5555
end
5656
end
57+
alias on_csend on_send
5758
end
5859
end
5960
end

lib/rubocop/cop/rails/find_by.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FindBy < Base
2828
include RangeHelp
2929
extend AutoCorrector
3030

31-
MSG = 'Use `find_by` instead of `where.%<method>s`.'
31+
MSG = 'Use `find_by` instead of `where%<dot>s%<method>s`.'
3232
RESTRICT_ON_SEND = %i[first take].freeze
3333

3434
def on_send(node)
@@ -37,7 +37,7 @@ def on_send(node)
3737

3838
range = offense_range(node)
3939

40-
add_offense(range, message: format(MSG, method: node.method_name)) do |corrector|
40+
add_offense(range, message: format(MSG, dot: node.loc.dot.source, method: node.method_name)) do |corrector|
4141
autocorrect(corrector, node)
4242
end
4343
end

lib/rubocop/cop/rails/find_by_id.rb

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,39 @@ class FindById < Base
2424
RESTRICT_ON_SEND = %i[take! find_by_id! find_by!].freeze
2525

2626
def_node_matcher :where_take?, <<~PATTERN
27-
(send
28-
$(send _ :where
27+
(call
28+
$(call _ :where
2929
(hash
3030
(pair (sym :id) $_))) :take!)
3131
PATTERN
3232

3333
def_node_matcher :find_by?, <<~PATTERN
3434
{
35-
(send _ :find_by_id! $_)
36-
(send _ :find_by! (hash (pair (sym :id) $_)))
35+
(call _ :find_by_id! $_)
36+
(call _ :find_by! (hash (pair (sym :id) $_)))
3737
}
3838
PATTERN
3939

4040
def on_send(node)
4141
where_take?(node) do |where, id_value|
4242
range = where_take_offense_range(node, where)
43-
bad_method = build_where_take_bad_method(id_value)
4443

45-
register_offense(range, id_value, bad_method)
44+
register_offense(range, id_value)
4645
end
4746

4847
find_by?(node) do |id_value|
4948
range = find_by_offense_range(node)
50-
bad_method = build_find_by_bad_method(node, id_value)
5149

52-
register_offense(range, id_value, bad_method)
50+
register_offense(range, id_value)
5351
end
5452
end
53+
alias on_csend on_send
5554

5655
private
5756

58-
def register_offense(range, id_value, bad_method)
57+
def register_offense(range, id_value)
5958
good_method = build_good_method(id_value)
60-
message = format(MSG, good_method: good_method, bad_method: bad_method)
59+
message = format(MSG, good_method: good_method, bad_method: range.source)
6160

6261
add_offense(range, message: message) do |corrector|
6362
corrector.replace(range, good_method)
@@ -75,19 +74,6 @@ def find_by_offense_range(node)
7574
def build_good_method(id_value)
7675
"find(#{id_value.source})"
7776
end
78-
79-
def build_where_take_bad_method(id_value)
80-
"where(id: #{id_value.source}).take!"
81-
end
82-
83-
def build_find_by_bad_method(node, id_value)
84-
case node.method_name
85-
when :find_by_id!
86-
"find_by_id!(#{id_value.source})"
87-
when :find_by!
88-
"find_by!(id: #{id_value.source})"
89-
end
90-
end
9177
end
9278
end
9379
end

lib/rubocop/cop/rails/inquiry.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def on_send(node)
3333

3434
add_offense(node.loc.selector)
3535
end
36+
alias on_csend on_send
3637
end
3738
end
3839
end

lib/rubocop/cop/rails/pick.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class Pick < Base
2828
extend AutoCorrector
2929
extend TargetRailsVersion
3030

31-
MSG = 'Prefer `pick(%<args>s)` over `pluck(%<args>s).first`.'
31+
MSG = 'Prefer `pick(%<args>s)` over `%<current>s`.'
3232
RESTRICT_ON_SEND = %i[first].freeze
3333

3434
minimum_target_rails_version 6.0
3535

3636
def_node_matcher :pick_candidate?, <<~PATTERN
37-
(send (send _ :pluck ...) :first)
37+
(call (call _ :pluck ...) :first)
3838
PATTERN
3939

4040
def on_send(node)
@@ -44,19 +44,20 @@ def on_send(node)
4444
node_selector = node.loc.selector
4545
range = receiver_selector.join(node_selector)
4646

47-
add_offense(range, message: message(receiver)) do |corrector|
47+
add_offense(range, message: message(receiver, range)) do |corrector|
4848
first_range = receiver.source_range.end.join(node_selector)
4949

5050
corrector.remove(first_range)
5151
corrector.replace(receiver_selector, 'pick')
5252
end
5353
end
5454
end
55+
alias on_csend on_send
5556

5657
private
5758

58-
def message(receiver)
59-
format(MSG, args: receiver.arguments.map(&:source).join(', '))
59+
def message(receiver, current)
60+
format(MSG, args: receiver.arguments.map(&:source).join(', '), current: current.source)
6061
end
6162
end
6263
end

lib/rubocop/cop/rails/pluck_id.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PluckId < Base
3434
RESTRICT_ON_SEND = %i[pluck].freeze
3535

3636
def_node_matcher :pluck_id_call?, <<~PATTERN
37-
(send _ :pluck {(sym :id) (send nil? :primary_key)})
37+
(call _ :pluck {(sym :id) (send nil? :primary_key)})
3838
PATTERN
3939

4040
def on_send(node)
@@ -47,6 +47,7 @@ def on_send(node)
4747
corrector.replace(offense_range(node), 'ids')
4848
end
4949
end
50+
alias on_csend on_send
5051

5152
private
5253

lib/rubocop/cop/rails/pluck_in_where.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ def on_send(node)
6565
corrector.replace(range, replacement)
6666
end
6767
end
68+
alias on_csend on_send
6869

6970
private
7071

7172
def root_receiver(node)
7273
receiver = node.receiver
7374

74-
if receiver&.send_type?
75+
if receiver&.call_type?
7576
root_receiver(receiver)
7677
else
7778
receiver

lib/rubocop/cop/rails/where_equals.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class WhereEquals < Base
3333

3434
def_node_matcher :where_method_call?, <<~PATTERN
3535
{
36-
(send _ :where (array $str_type? $_ ?))
37-
(send _ :where $str_type? $_ ?)
36+
(call _ :where (array $str_type? $_ ?))
37+
(call _ :where $str_type? $_ ?)
3838
}
3939
PATTERN
4040

@@ -55,6 +55,7 @@ def on_send(node)
5555
end
5656
end
5757
end
58+
alias on_csend on_send
5859

5960
EQ_ANONYMOUS_RE = /\A([\w.]+)\s+=\s+\?\z/.freeze # column = ?
6061
IN_ANONYMOUS_RE = /\A([\w.]+)\s+IN\s+\(\?\)\z/i.freeze # column IN (?)

0 commit comments

Comments
 (0)