rubocop-rails 2.21.2 → 2.23.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -10
- data/config/default.yml +11 -3
- data/lib/rubocop/cop/mixin/active_record_helper.rb +9 -2
- data/lib/rubocop/cop/mixin/database_type_resolvable.rb +66 -0
- data/lib/rubocop/cop/rails/action_filter.rb +3 -0
- data/lib/rubocop/cop/rails/active_record_aliases.rb +2 -2
- data/lib/rubocop/cop/rails/active_support_aliases.rb +6 -5
- data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
- data/lib/rubocop/cop/rails/bulk_change_table.rb +6 -56
- data/lib/rubocop/cop/rails/content_tag.rb +1 -1
- data/lib/rubocop/cop/rails/dangerous_column_names.rb +10 -2
- data/lib/rubocop/cop/rails/duplicate_association.rb +66 -12
- data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +2 -2
- data/lib/rubocop/cop/rails/env_local.rb +46 -0
- data/lib/rubocop/cop/rails/file_path.rb +5 -5
- data/lib/rubocop/cop/rails/find_by.rb +2 -2
- data/lib/rubocop/cop/rails/find_by_id.rb +9 -23
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +1 -1
- data/lib/rubocop/cop/rails/helper_instance_variable.rb +1 -1
- data/lib/rubocop/cop/rails/inquiry.rb +1 -0
- data/lib/rubocop/cop/rails/inverse_of.rb +1 -1
- data/lib/rubocop/cop/rails/not_null_column.rb +13 -3
- data/lib/rubocop/cop/rails/output.rb +3 -2
- data/lib/rubocop/cop/rails/pick.rb +6 -5
- data/lib/rubocop/cop/rails/pluck.rb +1 -1
- data/lib/rubocop/cop/rails/pluck_id.rb +2 -1
- data/lib/rubocop/cop/rails/pluck_in_where.rb +18 -5
- data/lib/rubocop/cop/rails/rake_environment.rb +2 -2
- data/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +53 -2
- data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +7 -0
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -1
- data/lib/rubocop/cop/rails/response_parsed_body.rb +52 -10
- data/lib/rubocop/cop/rails/reversible_migration.rb +2 -2
- data/lib/rubocop/cop/rails/save_bang.rb +11 -6
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +1 -1
- data/lib/rubocop/cop/rails/unknown_env.rb +5 -1
- data/lib/rubocop/cop/rails/validation.rb +2 -2
- data/lib/rubocop/cop/rails/where_equals.rb +3 -2
- data/lib/rubocop/cop/rails/where_exists.rb +9 -8
- data/lib/rubocop/cop/rails/where_missing.rb +1 -1
- data/lib/rubocop/cop/rails/where_not.rb +8 -6
- data/lib/rubocop/cop/rails_cops.rb +2 -0
- data/lib/rubocop/rails/schema_loader/schema.rb +2 -2
- data/lib/rubocop/rails/version.rb +1 -1
- metadata +26 -4
@@ -24,40 +24,39 @@ module RuboCop
|
|
24
24
|
RESTRICT_ON_SEND = %i[take! find_by_id! find_by!].freeze
|
25
25
|
|
26
26
|
def_node_matcher :where_take?, <<~PATTERN
|
27
|
-
(
|
28
|
-
$(
|
27
|
+
(call
|
28
|
+
$(call _ :where
|
29
29
|
(hash
|
30
30
|
(pair (sym :id) $_))) :take!)
|
31
31
|
PATTERN
|
32
32
|
|
33
33
|
def_node_matcher :find_by?, <<~PATTERN
|
34
34
|
{
|
35
|
-
(
|
36
|
-
(
|
35
|
+
(call _ :find_by_id! $_)
|
36
|
+
(call _ :find_by! (hash (pair (sym :id) $_)))
|
37
37
|
}
|
38
38
|
PATTERN
|
39
39
|
|
40
40
|
def on_send(node)
|
41
41
|
where_take?(node) do |where, id_value|
|
42
42
|
range = where_take_offense_range(node, where)
|
43
|
-
bad_method = build_where_take_bad_method(id_value)
|
44
43
|
|
45
|
-
register_offense(range, id_value
|
44
|
+
register_offense(range, id_value)
|
46
45
|
end
|
47
46
|
|
48
47
|
find_by?(node) do |id_value|
|
49
48
|
range = find_by_offense_range(node)
|
50
|
-
bad_method = build_find_by_bad_method(node, id_value)
|
51
49
|
|
52
|
-
register_offense(range, id_value
|
50
|
+
register_offense(range, id_value)
|
53
51
|
end
|
54
52
|
end
|
53
|
+
alias on_csend on_send
|
55
54
|
|
56
55
|
private
|
57
56
|
|
58
|
-
def register_offense(range, id_value
|
57
|
+
def register_offense(range, id_value)
|
59
58
|
good_method = build_good_method(id_value)
|
60
|
-
message = format(MSG, good_method: good_method, bad_method:
|
59
|
+
message = format(MSG, good_method: good_method, bad_method: range.source)
|
61
60
|
|
62
61
|
add_offense(range, message: message) do |corrector|
|
63
62
|
corrector.replace(range, good_method)
|
@@ -75,19 +74,6 @@ module RuboCop
|
|
75
74
|
def build_good_method(id_value)
|
76
75
|
"find(#{id_value.source})"
|
77
76
|
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
|
91
77
|
end
|
92
78
|
end
|
93
79
|
end
|
@@ -6,7 +6,7 @@ module RuboCop
|
|
6
6
|
# Checks for use of the helper methods which reference
|
7
7
|
# instance variables.
|
8
8
|
#
|
9
|
-
# Relying on instance variables makes it difficult to
|
9
|
+
# Relying on instance variables makes it difficult to reuse helper
|
10
10
|
# methods.
|
11
11
|
#
|
12
12
|
# If it seems awkward to explicitly pass in each dependent
|
@@ -222,7 +222,7 @@ module RuboCop
|
|
222
222
|
|
223
223
|
def with_options_arguments(recv, node)
|
224
224
|
blocks = node.each_ancestor(:block).select do |block|
|
225
|
-
block.send_node.command?(:with_options) && same_context_in_with_options?(block.
|
225
|
+
block.send_node.command?(:with_options) && same_context_in_with_options?(block.first_argument, recv)
|
226
226
|
end
|
227
227
|
blocks.flat_map { |n| n.send_node.arguments }
|
228
228
|
end
|
@@ -3,8 +3,13 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
# Checks for add_column call with NOT NULL constraint
|
7
|
-
#
|
6
|
+
# Checks for add_column call with NOT NULL constraint in migration file.
|
7
|
+
#
|
8
|
+
# `TEXT` can have default values in PostgreSQL, but not in MySQL.
|
9
|
+
# It will automatically detect an adapter from `development` environment
|
10
|
+
# in `config/database.yml` or the environment variable `DATABASE_URL`
|
11
|
+
# when the `Database` option is not set. If the database is MySQL,
|
12
|
+
# this cop ignores offenses for the `TEXT`.
|
8
13
|
#
|
9
14
|
# @example
|
10
15
|
# # bad
|
@@ -17,6 +22,8 @@ module RuboCop
|
|
17
22
|
# add_reference :products, :category
|
18
23
|
# add_reference :products, :category, null: false, default: 1
|
19
24
|
class NotNullColumn < Base
|
25
|
+
include DatabaseTypeResolvable
|
26
|
+
|
20
27
|
MSG = 'Do not add a NOT NULL column without a default value.'
|
21
28
|
RESTRICT_ON_SEND = %i[add_column add_reference].freeze
|
22
29
|
|
@@ -45,7 +52,10 @@ module RuboCop
|
|
45
52
|
|
46
53
|
def check_add_column(node)
|
47
54
|
add_not_null_column?(node) do |type, pairs|
|
48
|
-
|
55
|
+
if type.respond_to?(:value)
|
56
|
+
return if type.value == :virtual || type.value == 'virtual'
|
57
|
+
return if (type.value == :text || type.value == 'text') && database == MYSQL
|
58
|
+
end
|
49
59
|
|
50
60
|
check_pairs(pairs)
|
51
61
|
end
|
@@ -23,6 +23,7 @@ module RuboCop
|
|
23
23
|
|
24
24
|
MSG = "Do not write to stdout. Use Rails's logger if you want to log."
|
25
25
|
RESTRICT_ON_SEND = %i[ap p pp pretty_print print puts binwrite syswrite write write_nonblock].freeze
|
26
|
+
ALLOWED_TYPES = %i[send csend block numblock].freeze
|
26
27
|
|
27
28
|
def_node_matcher :output?, <<~PATTERN
|
28
29
|
(send nil? {:ap :p :pp :pretty_print :print :puts} ...)
|
@@ -39,8 +40,8 @@ module RuboCop
|
|
39
40
|
PATTERN
|
40
41
|
|
41
42
|
def on_send(node)
|
42
|
-
return if node.parent&.
|
43
|
-
return
|
43
|
+
return if ALLOWED_TYPES.include?(node.parent&.type)
|
44
|
+
return if !output?(node) && !io_output?(node)
|
44
45
|
|
45
46
|
range = offense_range(node)
|
46
47
|
|
@@ -28,13 +28,13 @@ module RuboCop
|
|
28
28
|
extend AutoCorrector
|
29
29
|
extend TargetRailsVersion
|
30
30
|
|
31
|
-
MSG = 'Prefer `pick(%<args>s)` over
|
31
|
+
MSG = 'Prefer `pick(%<args>s)` over `%<current>s`.'
|
32
32
|
RESTRICT_ON_SEND = %i[first].freeze
|
33
33
|
|
34
34
|
minimum_target_rails_version 6.0
|
35
35
|
|
36
36
|
def_node_matcher :pick_candidate?, <<~PATTERN
|
37
|
-
(
|
37
|
+
(call (call _ :pluck ...) :first)
|
38
38
|
PATTERN
|
39
39
|
|
40
40
|
def on_send(node)
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
node_selector = node.loc.selector
|
45
45
|
range = receiver_selector.join(node_selector)
|
46
46
|
|
47
|
-
add_offense(range, message: message(receiver)) do |corrector|
|
47
|
+
add_offense(range, message: message(receiver, range)) do |corrector|
|
48
48
|
first_range = receiver.source_range.end.join(node_selector)
|
49
49
|
|
50
50
|
corrector.remove(first_range)
|
@@ -52,11 +52,12 @@ module RuboCop
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
|
+
alias on_csend on_send
|
55
56
|
|
56
57
|
private
|
57
58
|
|
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)
|
60
61
|
end
|
61
62
|
end
|
62
63
|
end
|
@@ -38,7 +38,7 @@ module RuboCop
|
|
38
38
|
minimum_target_rails_version 5.0
|
39
39
|
|
40
40
|
def_node_matcher :pluck_candidate?, <<~PATTERN
|
41
|
-
({block numblock} (
|
41
|
+
({block numblock} (call _ {:map :collect}) $_argument (send lvar :[] $_key))
|
42
42
|
PATTERN
|
43
43
|
|
44
44
|
def on_block(node)
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
34
34
|
RESTRICT_ON_SEND = %i[pluck].freeze
|
35
35
|
|
36
36
|
def_node_matcher :pluck_id_call?, <<~PATTERN
|
37
|
-
(
|
37
|
+
(call _ :pluck {(sym :id) (send nil? :primary_key)})
|
38
38
|
PATTERN
|
39
39
|
|
40
40
|
def on_send(node)
|
@@ -47,6 +47,7 @@ module RuboCop
|
|
47
47
|
corrector.replace(offense_range(node), 'ids')
|
48
48
|
end
|
49
49
|
end
|
50
|
+
alias on_csend on_send
|
50
51
|
|
51
52
|
private
|
52
53
|
|
@@ -22,10 +22,13 @@ module RuboCop
|
|
22
22
|
# @example
|
23
23
|
# # bad
|
24
24
|
# Post.where(user_id: User.active.pluck(:id))
|
25
|
+
# Post.where(user_id: User.active.ids)
|
26
|
+
# Post.where.not(user_id: User.active.pluck(:id))
|
25
27
|
#
|
26
28
|
# # good
|
27
29
|
# Post.where(user_id: User.active.select(:id))
|
28
30
|
# Post.where(user_id: active_users.select(:id))
|
31
|
+
# Post.where.not(user_id: active_users.select(:id))
|
29
32
|
#
|
30
33
|
# @example EnforcedStyle: conservative (default)
|
31
34
|
# # good
|
@@ -40,8 +43,9 @@ module RuboCop
|
|
40
43
|
include ConfigurableEnforcedStyle
|
41
44
|
extend AutoCorrector
|
42
45
|
|
43
|
-
|
44
|
-
|
46
|
+
MSG_SELECT = 'Use `select` instead of `pluck` within `where` query method.'
|
47
|
+
MSG_IDS = 'Use `select(:id)` instead of `ids` within `where` query method.'
|
48
|
+
RESTRICT_ON_SEND = %i[pluck ids].freeze
|
45
49
|
|
46
50
|
def on_send(node)
|
47
51
|
return unless in_where?(node)
|
@@ -49,17 +53,26 @@ module RuboCop
|
|
49
53
|
|
50
54
|
range = node.loc.selector
|
51
55
|
|
52
|
-
|
53
|
-
|
56
|
+
if node.method?(:ids)
|
57
|
+
replacement = 'select(:id)'
|
58
|
+
message = MSG_IDS
|
59
|
+
else
|
60
|
+
replacement = 'select'
|
61
|
+
message = MSG_SELECT
|
62
|
+
end
|
63
|
+
|
64
|
+
add_offense(range, message: message) do |corrector|
|
65
|
+
corrector.replace(range, replacement)
|
54
66
|
end
|
55
67
|
end
|
68
|
+
alias on_csend on_send
|
56
69
|
|
57
70
|
private
|
58
71
|
|
59
72
|
def root_receiver(node)
|
60
73
|
receiver = node.receiver
|
61
74
|
|
62
|
-
if receiver&.
|
75
|
+
if receiver&.call_type?
|
63
76
|
root_receiver(receiver)
|
64
77
|
else
|
65
78
|
receiver
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def task_name(node)
|
75
|
-
first_arg = node.
|
75
|
+
first_arg = node.first_argument
|
76
76
|
case first_arg&.type
|
77
77
|
when :sym, :str
|
78
78
|
first_arg.value.to_sym
|
@@ -97,7 +97,7 @@ module RuboCop
|
|
97
97
|
end
|
98
98
|
|
99
99
|
def with_dependencies?(node)
|
100
|
-
first_arg = node.
|
100
|
+
first_arg = node.first_argument
|
101
101
|
return false unless first_arg
|
102
102
|
|
103
103
|
if first_arg.hash_type?
|
@@ -3,8 +3,43 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
+
# TODO: In the future, please support only RuboCop 1.52+ and use `RuboCop::Cop::AllowedReceivers`:
|
7
|
+
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/cop/mixin/allowed_receivers.rb
|
8
|
+
# At that time, this duplicated module implementation can be removed.
|
9
|
+
module AllowedReceivers
|
10
|
+
def allowed_receiver?(receiver)
|
11
|
+
receiver_name = receiver_name(receiver)
|
12
|
+
|
13
|
+
allowed_receivers.include?(receiver_name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def receiver_name(receiver)
|
17
|
+
return receiver_name(receiver.receiver) if receiver.receiver && !receiver.receiver.const_type?
|
18
|
+
|
19
|
+
if receiver.send_type?
|
20
|
+
if receiver.receiver
|
21
|
+
"#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
|
22
|
+
else
|
23
|
+
receiver.method_name.to_s
|
24
|
+
end
|
25
|
+
else
|
26
|
+
receiver.source
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def allowed_receivers
|
31
|
+
cop_config.fetch('AllowedReceivers', [])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
6
35
|
# Detect redundant `all` used as a receiver for Active Record query methods.
|
7
36
|
#
|
37
|
+
# For the methods `delete_all` and `destroy_all`, this cop will only check cases where the receiver is a model.
|
38
|
+
# It will ignore cases where the receiver is an association (e.g., `user.articles.all.delete_all`).
|
39
|
+
# This is because omitting `all` from an association changes the methods
|
40
|
+
# from `ActiveRecord::Relation` to `ActiveRecord::Associations::CollectionProxy`,
|
41
|
+
# which can affect their behavior.
|
42
|
+
#
|
8
43
|
# @safety
|
9
44
|
# This cop is unsafe for autocorrection if the receiver for `all` is not an Active Record object.
|
10
45
|
#
|
@@ -35,11 +70,19 @@ module RuboCop
|
|
35
70
|
|
36
71
|
RESTRICT_ON_SEND = [:all].freeze
|
37
72
|
|
38
|
-
# Defined methods in `ActiveRecord::Querying::QUERYING_METHODS` on activerecord 7.0.
|
73
|
+
# Defined methods in `ActiveRecord::Querying::QUERYING_METHODS` on activerecord 7.1.0.
|
39
74
|
QUERYING_METHODS = %i[
|
40
75
|
and
|
41
76
|
annotate
|
42
77
|
any?
|
78
|
+
async_average
|
79
|
+
async_count
|
80
|
+
async_ids
|
81
|
+
async_maximum
|
82
|
+
async_minimum
|
83
|
+
async_pick
|
84
|
+
async_pluck
|
85
|
+
async_sum
|
43
86
|
average
|
44
87
|
calculate
|
45
88
|
count
|
@@ -109,6 +152,7 @@ module RuboCop
|
|
109
152
|
preload
|
110
153
|
readonly
|
111
154
|
references
|
155
|
+
regroup
|
112
156
|
reorder
|
113
157
|
reselect
|
114
158
|
rewhere
|
@@ -130,17 +174,20 @@ module RuboCop
|
|
130
174
|
unscope
|
131
175
|
update_all
|
132
176
|
where
|
177
|
+
with
|
133
178
|
without
|
134
179
|
].to_set.freeze
|
135
180
|
|
136
181
|
POSSIBLE_ENUMERABLE_BLOCK_METHODS = %i[any? count find none? one? select sum].freeze
|
182
|
+
SENSITIVE_METHODS_ON_ASSOCIATION = %i[delete_all destroy_all].freeze
|
137
183
|
|
138
184
|
def_node_matcher :followed_by_query_method?, <<~PATTERN
|
139
185
|
(send (send _ :all) QUERYING_METHODS ...)
|
140
186
|
PATTERN
|
141
187
|
|
142
188
|
def on_send(node)
|
143
|
-
return
|
189
|
+
return unless followed_by_query_method?(node.parent)
|
190
|
+
return if possible_enumerable_block_method?(node) || sensitive_association_method?(node)
|
144
191
|
return if node.receiver ? allowed_receiver?(node.receiver) : !inherit_active_record_base?(node)
|
145
192
|
|
146
193
|
range_of_all_method = offense_range(node)
|
@@ -159,6 +206,10 @@ module RuboCop
|
|
159
206
|
parent.parent&.block_type? || parent.parent&.numblock_type? || parent.first_argument&.block_pass_type?
|
160
207
|
end
|
161
208
|
|
209
|
+
def sensitive_association_method?(node)
|
210
|
+
!node.receiver&.const_type? && SENSITIVE_METHODS_ON_ASSOCIATION.include?(node.parent.method_name)
|
211
|
+
end
|
212
|
+
|
162
213
|
def offense_range(node)
|
163
214
|
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
|
164
215
|
end
|
@@ -53,6 +53,12 @@ module RuboCop
|
|
53
53
|
# @example source that matches - by a foreign key
|
54
54
|
# validates :user_id, presence: true
|
55
55
|
#
|
56
|
+
# @example source that DOES NOT match - if condition
|
57
|
+
# validates :user_id, presence: true, if: condition
|
58
|
+
#
|
59
|
+
# @example source that DOES NOT match - unless condition
|
60
|
+
# validates :user_id, presence: true, unless: condition
|
61
|
+
#
|
56
62
|
# @example source that DOES NOT match - strict validation
|
57
63
|
# validates :user_id, presence: true, strict: true
|
58
64
|
#
|
@@ -65,6 +71,7 @@ module RuboCop
|
|
65
71
|
$[
|
66
72
|
(hash <$(pair (sym :presence) true) ...>) # presence: true
|
67
73
|
!(hash <$(pair (sym :strict) {true const}) ...>) # strict: true
|
74
|
+
!(hash <$(pair (sym {:if :unless}) _) ...>) # if: some_condition or unless: some_condition
|
68
75
|
]
|
69
76
|
)
|
70
77
|
PATTERN
|
@@ -3,25 +3,30 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
# Prefer `response.parsed_body` to `
|
6
|
+
# Prefer `response.parsed_body` to custom parsing logic for `response.body`.
|
7
7
|
#
|
8
8
|
# @safety
|
9
|
-
# This cop is unsafe because Content-Type may not be `application/json
|
10
|
-
# Content-Type provided by corporate entities such as
|
11
|
-
# `
|
9
|
+
# This cop is unsafe because Content-Type may not be `application/json` or `text/html`.
|
10
|
+
# For example, the proprietary Content-Type provided by corporate entities such as
|
11
|
+
# `application/vnd.github+json` is not supported at `response.parsed_body` by default,
|
12
|
+
# so you still have to use `JSON.parse(response.body)` there.
|
12
13
|
#
|
13
14
|
# @example
|
14
15
|
# # bad
|
15
16
|
# JSON.parse(response.body)
|
16
17
|
#
|
18
|
+
# # bad
|
19
|
+
# Nokogiri::HTML.parse(response.body)
|
20
|
+
#
|
21
|
+
# # bad
|
22
|
+
# Nokogiri::HTML5.parse(response.body)
|
23
|
+
#
|
17
24
|
# # good
|
18
25
|
# response.parsed_body
|
19
26
|
class ResponseParsedBody < Base
|
20
27
|
extend AutoCorrector
|
21
28
|
extend TargetRailsVersion
|
22
29
|
|
23
|
-
MSG = 'Prefer `response.parsed_body` to `JSON.parse(response.body)`.'
|
24
|
-
|
25
30
|
RESTRICT_ON_SEND = %i[parse].freeze
|
26
31
|
|
27
32
|
minimum_target_rails_version 5.0
|
@@ -38,12 +43,27 @@ module RuboCop
|
|
38
43
|
)
|
39
44
|
PATTERN
|
40
45
|
|
46
|
+
# @!method nokogiri_html_parse_response_body(node)
|
47
|
+
def_node_matcher :nokogiri_html_parse_response_body, <<~PATTERN
|
48
|
+
(send
|
49
|
+
(const
|
50
|
+
(const {nil? cbase} :Nokogiri)
|
51
|
+
${:HTML :HTML5}
|
52
|
+
)
|
53
|
+
:parse
|
54
|
+
(send
|
55
|
+
(send nil? :response)
|
56
|
+
:body
|
57
|
+
)
|
58
|
+
)
|
59
|
+
PATTERN
|
60
|
+
|
41
61
|
def on_send(node)
|
42
|
-
|
62
|
+
check_json_parse_response_body(node)
|
43
63
|
|
44
|
-
|
45
|
-
|
46
|
-
|
64
|
+
return unless target_rails_version >= 7.1
|
65
|
+
|
66
|
+
check_nokogiri_html_parse_response_body(node)
|
47
67
|
end
|
48
68
|
|
49
69
|
private
|
@@ -51,6 +71,28 @@ module RuboCop
|
|
51
71
|
def autocorrect(corrector, node)
|
52
72
|
corrector.replace(node, 'response.parsed_body')
|
53
73
|
end
|
74
|
+
|
75
|
+
def check_json_parse_response_body(node)
|
76
|
+
return unless json_parse_response_body?(node)
|
77
|
+
|
78
|
+
add_offense(
|
79
|
+
node,
|
80
|
+
message: 'Prefer `response.parsed_body` to `JSON.parse(response.body)`.'
|
81
|
+
) do |corrector|
|
82
|
+
autocorrect(corrector, node)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def check_nokogiri_html_parse_response_body(node)
|
87
|
+
return unless (const = nokogiri_html_parse_response_body(node))
|
88
|
+
|
89
|
+
add_offense(
|
90
|
+
node,
|
91
|
+
message: "Prefer `response.parsed_body` to `Nokogiri::#{const}.parse(response.body)`."
|
92
|
+
) do |corrector|
|
93
|
+
autocorrect(corrector, node)
|
94
|
+
end
|
95
|
+
end
|
54
96
|
end
|
55
97
|
end
|
56
98
|
end
|
@@ -290,10 +290,10 @@ module RuboCop
|
|
290
290
|
when :change
|
291
291
|
false
|
292
292
|
when :remove
|
293
|
-
target_rails_version >= 6.1 && all_hash_key?(node.
|
293
|
+
target_rails_version >= 6.1 && all_hash_key?(node.last_argument, :type)
|
294
294
|
when :change_default, :change_column_default, :change_table_comment,
|
295
295
|
:change_column_comment
|
296
|
-
all_hash_key?(node.
|
296
|
+
all_hash_key?(node.last_argument, :from, :to)
|
297
297
|
else
|
298
298
|
true
|
299
299
|
end
|
@@ -235,10 +235,10 @@ module RuboCop
|
|
235
235
|
|
236
236
|
def in_condition_or_compound_boolean?(node)
|
237
237
|
node = node.block_node || node
|
238
|
-
parent = node.
|
238
|
+
parent = node.each_ancestor.find { |ancestor| !ancestor.begin_type? }
|
239
239
|
return false unless parent
|
240
240
|
|
241
|
-
operator_or_single_negative?(parent) || (conditional?(parent) && node == parent.condition)
|
241
|
+
operator_or_single_negative?(parent) || (conditional?(parent) && node == deparenthesize(parent.condition))
|
242
242
|
end
|
243
243
|
|
244
244
|
def operator_or_single_negative?(node)
|
@@ -249,6 +249,11 @@ module RuboCop
|
|
249
249
|
parent.if_type? || parent.case_type?
|
250
250
|
end
|
251
251
|
|
252
|
+
def deparenthesize(node)
|
253
|
+
node = node.children.last while node.begin_type?
|
254
|
+
node
|
255
|
+
end
|
256
|
+
|
252
257
|
def checked_immediately?(node)
|
253
258
|
node.parent && call_to_persisted?(node.parent)
|
254
259
|
end
|
@@ -331,10 +336,10 @@ module RuboCop
|
|
331
336
|
|
332
337
|
# Check argument signature as no arguments or one hash
|
333
338
|
def expected_signature?(node)
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
339
|
+
return true unless node.arguments?
|
340
|
+
return false if !node.arguments.one? || node.method?(:destroy)
|
341
|
+
|
342
|
+
node.first_argument.hash_type? || !node.first_argument.literal?
|
338
343
|
end
|
339
344
|
end
|
340
345
|
end
|
@@ -86,7 +86,11 @@ module RuboCop
|
|
86
86
|
end
|
87
87
|
|
88
88
|
def environments
|
89
|
-
|
89
|
+
@environments ||= begin
|
90
|
+
environments = cop_config['Environments'] || []
|
91
|
+
environments << 'local' if target_rails_version >= 7.1
|
92
|
+
environments
|
93
|
+
end
|
90
94
|
end
|
91
95
|
end
|
92
96
|
end
|
@@ -51,7 +51,7 @@ module RuboCop
|
|
51
51
|
uniqueness
|
52
52
|
].freeze
|
53
53
|
|
54
|
-
RESTRICT_ON_SEND = TYPES.map { |p| "validates_#{p}_of"
|
54
|
+
RESTRICT_ON_SEND = TYPES.map { |p| :"validates_#{p}_of" }.freeze
|
55
55
|
ALLOWLIST = TYPES.map { |p| "validates :column, #{p}: value" }.freeze
|
56
56
|
|
57
57
|
def on_send(node)
|
@@ -60,7 +60,7 @@ module RuboCop
|
|
60
60
|
range = node.loc.selector
|
61
61
|
|
62
62
|
add_offense(range, message: message(node)) do |corrector|
|
63
|
-
last_argument = node.
|
63
|
+
last_argument = node.last_argument
|
64
64
|
return if !last_argument.literal? && !last_argument.splat_type? && !frozen_array_argument?(last_argument)
|
65
65
|
|
66
66
|
corrector.replace(range, 'validates')
|
@@ -33,8 +33,8 @@ module RuboCop
|
|
33
33
|
|
34
34
|
def_node_matcher :where_method_call?, <<~PATTERN
|
35
35
|
{
|
36
|
-
(
|
37
|
-
(
|
36
|
+
(call _ :where (array $str_type? $_ ?))
|
37
|
+
(call _ :where $str_type? $_ ?)
|
38
38
|
}
|
39
39
|
PATTERN
|
40
40
|
|
@@ -55,6 +55,7 @@ module RuboCop
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
|
+
alias on_csend on_send
|
58
59
|
|
59
60
|
EQ_ANONYMOUS_RE = /\A([\w.]+)\s+=\s+\?\z/.freeze # column = ?
|
60
61
|
IN_ANONYMOUS_RE = /\A([\w.]+)\s+IN\s+\(\?\)\z/i.freeze # column IN (?)
|