rubocop-rails 2.34.0 → 2.34.2
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0ba3287dd87a87e513f027365ed1e2f726a812b513f92c70e63e1e1792d2ce45
|
|
4
|
+
data.tar.gz: 375105175c67eebfbf7f82469ca3a8b8540779199e2d9542990fc219c6c57412
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bd06a6c9f0cc892c20473c640ae9bc27d2fd02b2e7ccb1c763c6d4ab0008cc9b3699f9ef8d32aadecc4f5d0eb89066a14495852e3b7fabedc4e0702dd3c2b132
|
|
7
|
+
data.tar.gz: 2353dca7cb758e658688f8fed9343a20f16355d5f3a3170d4e3e1472adb03b4466404067966124c55101e586b5e1009efc96ef4b95d0982ab3c9ed0f7fac2d3e
|
|
@@ -45,7 +45,7 @@ module RuboCop
|
|
|
45
45
|
return unless node.receiver&.const_name == 'Rails'
|
|
46
46
|
|
|
47
47
|
parent = node.parent
|
|
48
|
-
return unless parent
|
|
48
|
+
return unless parent.respond_to?(:predicate_method?) && parent.predicate_method?
|
|
49
49
|
|
|
50
50
|
return if ALLOWED_LIST.include?(parent.method_name)
|
|
51
51
|
|
|
@@ -53,11 +53,22 @@ module RuboCop
|
|
|
53
53
|
#
|
|
54
54
|
# # good
|
|
55
55
|
# a.presence&.foo
|
|
56
|
+
#
|
|
57
|
+
# # good
|
|
58
|
+
# a.present? ? a[1] : nil
|
|
59
|
+
#
|
|
60
|
+
# # good
|
|
61
|
+
# a[:key] = value if a.present?
|
|
62
|
+
#
|
|
63
|
+
# # good
|
|
64
|
+
# a.present? ? a > 1 : nil
|
|
65
|
+
# a <= 0 if a.present?
|
|
56
66
|
class Presence < Base
|
|
57
67
|
include RangeHelp
|
|
58
68
|
extend AutoCorrector
|
|
59
69
|
|
|
60
70
|
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
|
|
71
|
+
INDEX_ACCESS_METHODS = %i[[] []=].freeze
|
|
61
72
|
|
|
62
73
|
def_node_matcher :redundant_receiver_and_other, <<~PATTERN
|
|
63
74
|
{
|
|
@@ -130,7 +141,7 @@ module RuboCop
|
|
|
130
141
|
end
|
|
131
142
|
|
|
132
143
|
def ignore_chain_node?(node)
|
|
133
|
-
|
|
144
|
+
index_access_method?(node) || node.assignment? || node.arithmetic_operation? || node.comparison_method?
|
|
134
145
|
end
|
|
135
146
|
|
|
136
147
|
def message(node, replacement)
|
|
@@ -180,6 +191,10 @@ module RuboCop
|
|
|
180
191
|
replaced += "(#{chain.arguments.map(&:source).join(', ')})" if chain.arguments?
|
|
181
192
|
left_sibling ? "(#{replaced})" : replaced
|
|
182
193
|
end
|
|
194
|
+
|
|
195
|
+
def index_access_method?(node)
|
|
196
|
+
INDEX_ACCESS_METHODS.include?(node.method_name)
|
|
197
|
+
end
|
|
183
198
|
end
|
|
184
199
|
end
|
|
185
200
|
end
|
|
@@ -33,33 +33,38 @@ module RuboCop
|
|
|
33
33
|
|
|
34
34
|
def_node_matcher :redirect_back_with_fallback_location, <<~PATTERN
|
|
35
35
|
(send nil? :redirect_back
|
|
36
|
-
(hash <$(pair (sym :fallback_location) $_)
|
|
36
|
+
(hash <$(pair (sym :fallback_location) $_) $...>)
|
|
37
37
|
)
|
|
38
38
|
PATTERN
|
|
39
39
|
|
|
40
40
|
def on_send(node)
|
|
41
|
-
redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value|
|
|
41
|
+
redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value, options|
|
|
42
42
|
add_offense(node.loc.selector) do |corrector|
|
|
43
|
-
correct_redirect_back(corrector, node, fallback_pair, fallback_value)
|
|
43
|
+
correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
private
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
# rubocop:disable Metrics/AbcSize
|
|
51
|
+
def correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
|
|
51
52
|
corrector.replace(node.loc.selector, 'redirect_back_or_to')
|
|
52
53
|
|
|
53
54
|
hash_arg = node.first_argument
|
|
54
55
|
|
|
55
56
|
if hash_arg.pairs.one?
|
|
56
|
-
|
|
57
|
+
arguments = [fallback_value.source] + options.map(&:source)
|
|
58
|
+
corrector.replace(hash_arg, arguments.join(', '))
|
|
57
59
|
else
|
|
58
60
|
remove_fallback_location_pair(corrector, hash_arg, fallback_pair)
|
|
59
61
|
first_pair = hash_arg.pairs.find { |pair| pair != fallback_pair }
|
|
60
62
|
corrector.insert_before(first_pair, "#{fallback_value.source}, ")
|
|
61
63
|
end
|
|
64
|
+
|
|
65
|
+
wrap_with_parentheses(node, corrector) unless node.parenthesized?
|
|
62
66
|
end
|
|
67
|
+
# rubocop:enable Metrics/AbcSize
|
|
63
68
|
|
|
64
69
|
def remove_fallback_location_pair(corrector, hash_node, fallback_pair)
|
|
65
70
|
pairs = hash_node.pairs
|
|
@@ -74,6 +79,11 @@ module RuboCop
|
|
|
74
79
|
end
|
|
75
80
|
end
|
|
76
81
|
|
|
82
|
+
def wrap_with_parentheses(node, corrector)
|
|
83
|
+
corrector.replace(node.loc.selector.end.join(node.first_argument.source_range.begin), '(')
|
|
84
|
+
corrector.insert_after(node, ')')
|
|
85
|
+
end
|
|
86
|
+
|
|
77
87
|
def remove_first_pair(corrector, fallback_pair, next_pair)
|
|
78
88
|
range = fallback_pair.source_range.join(next_pair.source_range.begin)
|
|
79
89
|
corrector.remove(range)
|