File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ class FindBy < Cop
20
20
TARGET_SELECTORS = %i[ first take ] . freeze
21
21
22
22
def_node_matcher :where_first? , <<-PATTERN
23
- (send (send _ :where ...) {:first :take})
23
+ (send ({ send csend} _ :where ...) {:first :take})
24
24
PATTERN
25
25
26
26
def on_send ( node )
@@ -32,6 +32,7 @@ def on_send(node)
32
32
add_offense ( node , location : range ,
33
33
message : format ( MSG , method : node . method_name ) )
34
34
end
35
+ alias on_csend on_send
35
36
36
37
def autocorrect ( node )
37
38
# Don't autocorrect where(...).first, because it can return different
Original file line number Diff line number Diff line change 28
28
it 'does not register an offense when using find_by' do
29
29
expect_no_offenses ( 'User.find_by(id: x)' )
30
30
end
31
+
32
+ it 'autocorrects where.take to find_by' do
33
+ new_source = autocorrect_source ( 'User.where(id: x).take' )
34
+
35
+ expect ( new_source ) . to eq ( 'User.find_by(id: x)' )
36
+ end
37
+
38
+ it 'does not autocorrect where.first' do
39
+ new_source = autocorrect_source ( 'User.where(id: x).first' )
40
+
41
+ expect ( new_source ) . to eq ( 'User.where(id: x).first' )
42
+ end
43
+
44
+ context 'when using safe navigation operator' , :ruby23 do
45
+ it 'registers an offense when using `#first`' do
46
+ expect_offense ( <<-RUBY . strip_indent )
47
+ User&.where(id: x).first
48
+ ^^^^^^^^^^^^^^^^^^ Use `find_by` instead of `where.first`.
49
+ RUBY
50
+ end
51
+ end
31
52
end
You can’t perform that action at this time.
0 commit comments