rubocop-rails 2.33.0 → 2.33.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/rails/find_by_or_assignment_memoization.rb +33 -3
- data/lib/rubocop/cop/rails/order_arguments.rb +5 -0
- data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -1
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +2 -1
- data/lib/rubocop/cop/rails/where_exists.rb +3 -3
- data/lib/rubocop/rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dd62ec2504a075e89e8933ab9a8ecc63061d04c75f604d661d21a87b264cdee
|
4
|
+
data.tar.gz: a47a5b0e0d1e2f94b4f5a2976875ce86eb19c2c9fed53a1767ce5c59666d819e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c48f2805329e13493881423a0de8549a7e3d3cfacc99be40b5a9dbd36b4a6c3d376845c5b0bcf3b492023835e8b3f5062d291c8ebe87165d82f96a77dbbc102
|
7
|
+
data.tar.gz: b30b183e23a29af49d9a6709d27318d79d4ff24246ba1b21ac8ae14260acbb64fd46ec54661f3d18867ffb331f71d794a33e5b760545b0743645ce902cac1daf
|
@@ -13,18 +13,32 @@ module RuboCop
|
|
13
13
|
# or the code may have a different purpose than memoization.
|
14
14
|
#
|
15
15
|
# @example
|
16
|
-
# # bad
|
16
|
+
# # bad - exclusively doing memoization
|
17
17
|
# def current_user
|
18
18
|
# @current_user ||= User.find_by(id: session[:user_id])
|
19
19
|
# end
|
20
20
|
#
|
21
21
|
# # good
|
22
22
|
# def current_user
|
23
|
-
# if
|
23
|
+
# return @current_user if defined?(@current_user)
|
24
|
+
#
|
25
|
+
# @current_user = User.find_by(id: session[:user_id])
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# # bad - method contains other code
|
29
|
+
# def current_user
|
30
|
+
# @current_user ||= User.find_by(id: session[:user_id])
|
31
|
+
# @current_user.do_something
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# def current_user
|
36
|
+
# if defined?(@current_user)
|
24
37
|
# @current_user
|
25
38
|
# else
|
26
39
|
# @current_user = User.find_by(id: session[:user_id])
|
27
40
|
# end
|
41
|
+
# @current_user.do_something
|
28
42
|
# end
|
29
43
|
class FindByOrAssignmentMemoization < Base
|
30
44
|
extend AutoCorrector
|
@@ -40,6 +54,22 @@ module RuboCop
|
|
40
54
|
)
|
41
55
|
PATTERN
|
42
56
|
|
57
|
+
# When a method body contains only memoization, the correction can be more succinct.
|
58
|
+
def on_def(node)
|
59
|
+
find_by_or_assignment_memoization(node.body) do |varible_name, find_by|
|
60
|
+
add_offense(node.body) do |corrector|
|
61
|
+
corrector.replace(
|
62
|
+
node.body,
|
63
|
+
<<~RUBY.rstrip
|
64
|
+
return #{varible_name} if defined?(#{varible_name})
|
65
|
+
|
66
|
+
#{varible_name} = #{find_by.source}
|
67
|
+
RUBY
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
43
73
|
def on_send(node)
|
44
74
|
assignment_node = node.parent
|
45
75
|
find_by_or_assignment_memoization(assignment_node) do |varible_name, find_by|
|
@@ -49,7 +79,7 @@ module RuboCop
|
|
49
79
|
corrector.replace(
|
50
80
|
assignment_node,
|
51
81
|
<<~RUBY.rstrip
|
52
|
-
if
|
82
|
+
if defined?(#{varible_name})
|
53
83
|
#{varible_name}
|
54
84
|
else
|
55
85
|
#{varible_name} = #{find_by.source}
|
@@ -52,6 +52,7 @@ module RuboCop
|
|
52
52
|
order_arguments.map! { |arg| extract_column_and_direction(arg.strip) }
|
53
53
|
|
54
54
|
return if order_arguments.any?(&:nil?)
|
55
|
+
return if order_arguments.any? { |column_name, _| positional_column?(column_name) }
|
55
56
|
|
56
57
|
convert_to_preferred_arguments(order_arguments).join(', ')
|
57
58
|
end
|
@@ -68,6 +69,10 @@ module RuboCop
|
|
68
69
|
end
|
69
70
|
end
|
70
71
|
|
72
|
+
def positional_column?(column_name)
|
73
|
+
column_name.match?(/\A\d+\z/)
|
74
|
+
end
|
75
|
+
|
71
76
|
def extract_column_and_direction(order_expression)
|
72
77
|
return unless (column, direction = ORDER_EXPRESSION_REGEX.match(order_expression)&.captures)
|
73
78
|
|
@@ -66,7 +66,7 @@ module RuboCop
|
|
66
66
|
return false unless enclosing_method
|
67
67
|
|
68
68
|
shadowing_method_name = first_arg.value.to_s
|
69
|
-
shadowing_method_name
|
69
|
+
shadowing_method_name += '=' if node.method?(:write_attribute)
|
70
70
|
enclosing_method.method?(shadowing_method_name)
|
71
71
|
end
|
72
72
|
|
@@ -97,7 +97,8 @@ module RuboCop
|
|
97
97
|
|
98
98
|
def in_transaction_block?(node)
|
99
99
|
return false unless transaction_method_name?(node.method_name)
|
100
|
-
return false unless node.parent
|
100
|
+
return false unless (parent = node.parent)
|
101
|
+
return false unless parent.any_block_type? && parent.body
|
101
102
|
|
102
103
|
node.right_siblings.none? do |sibling|
|
103
104
|
sibling.respond_to?(:loop_keyword?) && sibling.loop_keyword?
|
@@ -58,8 +58,8 @@ module RuboCop
|
|
58
58
|
(call (call _ :where $...) :exists?)
|
59
59
|
PATTERN
|
60
60
|
|
61
|
-
def_node_matcher :
|
62
|
-
(call _ :exists?
|
61
|
+
def_node_matcher :exists_with_arg?, <<~PATTERN
|
62
|
+
(call _ :exists? $!splat_type?)
|
63
63
|
PATTERN
|
64
64
|
|
65
65
|
def on_send(node)
|
@@ -91,7 +91,7 @@ module RuboCop
|
|
91
91
|
if exists_style?
|
92
92
|
where_exists_call?(node, &block)
|
93
93
|
elsif where_style?
|
94
|
-
|
94
|
+
exists_with_arg?(node) { |arg| yield([arg]) }
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|