forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant_presence_validation_on_belongs_to.rb
264 lines (241 loc) · 9.45 KB
/
redundant_presence_validation_on_belongs_to.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Since Rails 5.0 the default for `belongs_to` is `optional: false`
# unless `config.active_record.belongs_to_required_by_default` is
# explicitly set to `false`. The presence validator is added
# automatically, and explicit presence validation is redundant.
#
# @safety
# This cop's autocorrection is unsafe because it changes the default error message
# from "can't be blank" to "must exist".
#
# @example
# # bad
# belongs_to :user
# validates :user, presence: true
#
# # bad
# belongs_to :user
# validates :user_id, presence: true
#
# # bad
# belongs_to :author, foreign_key: :user_id
# validates :user_id, presence: true
#
# # good
# belongs_to :user
#
# # good
# belongs_to :author, foreign_key: :user_id
#
class RedundantPresenceValidationOnBelongsTo < Base
include RangeHelp
extend AutoCorrector
extend TargetRailsVersion
MSG = 'Remove explicit presence validation for %<association>s.'
RESTRICT_ON_SEND = %i[validates].freeze
minimum_target_rails_version 5.0
# @!method presence_validation?(node)
# Match a `validates` statement with a presence check
#
# @example source that matches - by association
# validates :user, presence: true
#
# @example source that matches - by association
# validates :name, :user, presence: true
#
# @example source that matches - by a foreign key
# validates :user_id, presence: true
#
# @example source that DOES NOT match - if condition
# validates :user_id, presence: true, if: condition
#
# @example source that DOES NOT match - unless condition
# validates :user_id, presence: true, unless: condition
#
# @example source that DOES NOT match - strict validation
# validates :user_id, presence: true, strict: true
#
# @example source that DOES NOT match - custom strict validation
# validates :user_id, presence: true, strict: MissingUserError
def_node_matcher :presence_validation?, <<~PATTERN
(
send nil? :validates
(sym $_)+
$[
(hash <$(pair (sym :presence) true) ...>) # presence: true
!(hash <$(pair (sym :strict) {true const}) ...>) # strict: true
!(hash <$(pair (sym {:if :unless}) _) ...>) # if: some_condition or unless: some_condition
]
)
PATTERN
# @!method optional?(node)
# Match a `belongs_to` association with an optional option in a hash
def_node_matcher :optional?, <<~PATTERN
(send nil? :belongs_to _ ... #optional_option?)
PATTERN
# @!method optional_option?(node)
# Match an optional option in a hash
def_node_matcher :optional_option?, <<~PATTERN
{
(hash <(pair (sym :optional) true) ...>) # optional: true
(hash <(pair (sym :required) false) ...>) # required: false
}
PATTERN
# @!method any_belongs_to?(node, association:)
# Match a class with `belongs_to` with no regard to `foreign_key` option
#
# @example source that matches
# belongs_to :user
#
# @example source that matches - regardless of `foreign_key`
# belongs_to :author, foreign_key: :user_id
#
# @param node [RuboCop::AST::Node]
# @param association [Symbol]
# @return [Array<RuboCop::AST::Node>, nil] matching node
def_node_matcher :any_belongs_to?, <<~PATTERN
(begin
<
$(send nil? :belongs_to (sym %association) ...)
...
>
)
PATTERN
# @!method belongs_to?(node, key:, fk:)
# Match a class with a matching association, either by name or an explicit
# `foreign_key` option
#
# @example source that matches - fk matches `foreign_key` option
# belongs_to :author, foreign_key: :user_id
#
# @example source that matches - key matches association name
# belongs_to :user
#
# @example source that does not match - explicit `foreign_key` does not match
# belongs_to :user, foreign_key: :account_id
#
# @param node [RuboCop::AST::Node]
# @param key [Symbol] e.g. `:user`
# @param fk [Symbol] e.g. `:user_id`
# @return [Array<RuboCop::AST::Node>] matching nodes
def_node_matcher :belongs_to?, <<~PATTERN
(begin
<
${
#belongs_to_without_fk?(%key) # belongs_to :user
#belongs_to_with_a_matching_fk?(%fk) # belongs_to :author, foreign_key: :user_id
}
...
>
)
PATTERN
# @!method belongs_to_without_fk?(node, key)
# Match a matching `belongs_to` association, without an explicit `foreign_key` option
#
# @param node [RuboCop::AST::Node]
# @param key [Symbol] e.g. `:user`
# @return [Array<RuboCop::AST::Node>] matching nodes
def_node_matcher :belongs_to_without_fk?, <<~PATTERN
{
(send nil? :belongs_to (sym %1)) # belongs_to :user
(send nil? :belongs_to (sym %1) !hash ...) # belongs_to :user, -> { not_deleted }
(send nil? :belongs_to (sym %1) !(hash <(pair (sym :foreign_key) _) ...>))
}
PATTERN
# @!method belongs_to_with_a_matching_fk?(node, fk)
# Match a matching `belongs_to` association with a matching explicit `foreign_key` option
#
# @example source that matches
# belongs_to :author, foreign_key: :user_id
#
# @param node [RuboCop::AST::Node]
# @param fk [Symbol] e.g. `:user_id`
# @return [Array<RuboCop::AST::Node>] matching nodes
def_node_matcher :belongs_to_with_a_matching_fk?, <<~PATTERN
(send nil? :belongs_to ... (hash <(pair (sym :foreign_key) (sym %1)) ...>))
PATTERN
def on_send(node)
presence_validation?(node) do |all_keys, options, presence|
keys = non_optional_belongs_to(node.parent, all_keys)
return if keys.none?
add_offense_and_correct(node, all_keys, keys, options, presence)
end
end
private
def add_offense_and_correct(node, all_keys, keys, options, presence)
add_offense(presence, message: message_for(keys)) do |corrector|
if options.children.one? # `presence: true` is the only option
if keys == all_keys
remove_validation(corrector, node)
else
remove_keys_from_validation(corrector, node, keys)
end
elsif keys == all_keys
remove_presence_option(corrector, presence)
else
extract_validation_for_keys(corrector, node, keys, options)
end
end
end
def message_for(keys)
display_keys = keys.map { |key| "`#{key}`" }.join('/')
format(MSG, association: display_keys)
end
def non_optional_belongs_to(node, keys)
keys.select do |key|
belongs_to = belongs_to_for(node, key)
belongs_to && !optional?(belongs_to)
end
end
def belongs_to_for(model_class_node, key)
if key.to_s.end_with?('_id')
normalized_key = key.to_s.delete_suffix('_id').to_sym
belongs_to?(model_class_node, key: normalized_key, fk: key)
else
any_belongs_to?(model_class_node, association: key)
end
end
def remove_validation(corrector, node)
corrector.remove(validation_range(node))
end
def remove_keys_from_validation(corrector, node, keys)
keys.each do |key|
key_node = node.arguments.find { |arg| arg.value == key }
key_range = range_with_surrounding_space(
range_with_surrounding_comma(key_node.source_range, :right),
side: :right
)
corrector.remove(key_range)
end
end
def remove_presence_option(corrector, presence)
range = range_with_surrounding_comma(
range_with_surrounding_space(presence.source_range, side: :left),
:left
)
corrector.remove(range)
end
def extract_validation_for_keys(corrector, node, keys, options)
indentation = ' ' * node.source_range.column
options_without_presence = options.children.reject { |pair| pair.key.value == :presence }
source = [
indentation,
'validates ',
keys.map(&:inspect).join(', '),
', ',
options_without_presence.map(&:source).join(', '),
"\n"
].join
remove_keys_from_validation(corrector, node, keys)
corrector.insert_after(validation_range(node), source)
end
def validation_range(node)
range_by_whole_lines(node.source_range, include_final_newline: true)
end
end
end
end
end