rubocop-rails 2.35.0 → 2.35.4

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: e4946e79ccf9661248df014d69703665df3b0a72aa800e8c456afc59594ad944
4
- data.tar.gz: 7575ea946eb4e6985d6c0c365a0f39345fc6d32b0634c74949526f987d1ac38d
3
+ metadata.gz: 45b000a9e63ce4154fb6b1acbb97c84253448ead8bac7539aa773dbc1a7e3f8b
4
+ data.tar.gz: 26646956ab4da49c53a4d9105771e83658b07c65912799e5eea7f4d88e5f399f
5
5
  SHA512:
6
- metadata.gz: 7a660eb16537b1dafe092cb100229fd9f039f8d98b51e1408b9438997176c8fc0fde61494ae462bfa8ea15e65524f5ec99e4d2efac42d0461395da65bfe8cb8b
7
- data.tar.gz: ad601bb6b3296dceb196159604975b5e2efecaca5c66f715523368344f8a77e62d93cf8e22c4b3d53ce67d849a34cf9a1f4d184d6b655ecea0f12da23490bb35
6
+ metadata.gz: f8e1594a6edae31940c736c45a37966d09d02ada59afad3146b8941c4668b500c288e30da7293ae34d6ee38d1e2fc47b69900c8afa8f7e598da92e91d55f4d46
7
+ data.tar.gz: 15994ecea386276d46a19c17ec5bc962138a1476b4508f36353a9fb10e20f6fb30a1b1d78f837d9beef9980103a9786160d50b1fc610f06a092bc874b9621050
@@ -12,7 +12,7 @@ module RuboCop
12
12
  # which can affect their behavior.
13
13
  #
14
14
  # @safety
15
- # This cop is unsafe for autocorrection if the receiver for `all` is not an Active Record object.
15
+ # This cop is unsafe because false positives will occur if the receiver is not an Active Record object.
16
16
  #
17
17
  # @example
18
18
  # # bad
@@ -8,7 +8,10 @@ module RuboCop
8
8
  # In the following cases, `params[:key]` is treated as a key that is expected to be passed from the HTTP client,
9
9
  # and the cop detects it using the `expect` method.
10
10
  #
11
- # - Method calls on `params[:key]` without comparison methods
11
+ # - Method calls on `params[:key]` without comparison methods, methods that are safe to call
12
+ # on `nil` (such as `to_i`, `to_s`, or `is_a?`), key-check methods such as `key?`,
13
+ # collection methods such as `keys`, `merge`, or `slice`, or block-style calls such as
14
+ # `params[:key].each { ... }` or `params[:key].map(&:to_s)`
12
15
  # - Passing `params[:key]` as an argument to finder methods that raise on missing records
13
16
  # - Strong parameter methods using `require` or `permit`
14
17
  #
@@ -20,6 +23,18 @@ module RuboCop
20
23
  # incompatibility introduced for valid reasons by the `expect` method, which aligns better with
21
24
  # strong parameter conventions.
22
25
  #
26
+ # It is also unsafe because `expect` is stricter about the structure of the parameters than
27
+ # `require`/`permit`. Nested attributes that hold an array of records need an extra array wrapper,
28
+ # such as `expect(user: [{ pets_attributes: [[:name]] }])`. The cop cannot tell a single nested hash
29
+ # from an array of nested hashes, so it always generates the single-hash form, which can turn
30
+ # a previously successful request into a failure.
31
+ #
32
+ # It is also unsafe when `params[:key]` is passed to a finder method such as `find`, because
33
+ # `find` accepts an array of IDs. `Model.find(params[:id])` loads every record for an array of IDs,
34
+ # but the corrected `Model.find(params.expect(:id))` raises `ActionController::ParameterMissing`
35
+ # for an array value, since `expect` requires a scalar. The cop cannot tell a scalar ID from
36
+ # an array of IDs, so the autocorrection can turn a previously successful request into a failure.
37
+ #
23
38
  # @example
24
39
  #
25
40
  # # bad
@@ -53,7 +68,18 @@ module RuboCop
53
68
 
54
69
  MSG = 'Use `%<prefer>s` instead.'
55
70
  RESTRICT_ON_SEND = %i[[] require permit].freeze
56
- PRESENCE_CHECK_METHODS = %i[nil? blank? present? presence].freeze
71
+ # Method calls on `params[:key]` that should not be rewritten with `expect(:key)`.
72
+ # Covers presence/nil checks, nil-safe conversions and type checks, key-check methods,
73
+ # and collection methods that imply `params[:key]` is a Hash/Array.
74
+ IGNORED_METHODS = %i[
75
+ ! blank? compact compact! compact_blank compact_blank! deep_merge deep_merge!
76
+ delete delete_if dig each except exclude? extract! fetch has_key? has_value?
77
+ include? inspect instance_of? is_a? keep_if key? keys kind_of? member? merge merge!
78
+ nil? presence present? reverse_merge reverse_merge! slice stringify_keys
79
+ to_a to_f to_h to_hash to_i to_s to_unsafe_h to_unsafe_hash
80
+ transform_keys transform_keys! transform_values transform_values! try try!
81
+ value? values values_at with_defaults with_defaults! without
82
+ ].freeze
57
83
  RAISING_FINDER_METHODS = %i[find find_by! find_sole_by].freeze
58
84
 
59
85
  minimum_target_rails_version 8.0
@@ -62,10 +88,18 @@ module RuboCop
62
88
  (send (send nil? :params) :[] $_)
63
89
  PATTERN
64
90
 
91
+ # `require` with an array literal expects multiple top-level keys and has no single `expect` equivalent,
92
+ # so such calls are excluded to avoid generating broken code.
93
+ # A single dynamic argument to `permit` (such as a method call or variable that may return an array)
94
+ # has no safe `expect` rewrite, because the cop cannot tell whether the value is a list of attributes
95
+ # or a nested hash. Such calls are excluded to avoid generating broken code.
65
96
  def_node_matcher :params_require_permit, <<~PATTERN
66
- $(call
97
+ [
67
98
  $(call
68
- (send nil? :params) :require _) :permit _+)
99
+ $(call
100
+ (send nil? :params) :require !array) :permit _+)
101
+ !(call _ :permit {call lvar ivar cvar gvar const})
102
+ ]
69
103
  PATTERN
70
104
 
71
105
  def_node_matcher :params_permit_require, <<~PATTERN
@@ -127,13 +161,15 @@ module RuboCop
127
161
  def offensive_bracket_access?(node)
128
162
  return false unless (parent = node.parent)
129
163
  return false if parent.or_type?
164
+ return false if parent.csend_type? && parent.receiver == node
130
165
  return true if parent.each_ancestor(:call).any? { |node| raising_finder_method?(node) }
131
166
  return false unless parent.call_type?
132
167
 
133
168
  if parent.receiver == node
134
- return false if parent.comparison_method?
169
+ return false if parent.comparison_method? || parent.method?(:[])
170
+ return false if block_call?(parent)
135
171
 
136
- !parent.method?(:[]) && !PRESENCE_CHECK_METHODS.include?(parent.method_name)
172
+ !IGNORED_METHODS.include?(parent.method_name)
137
173
  else
138
174
  raising_finder_method?(parent)
139
175
  end
@@ -144,6 +180,10 @@ module RuboCop
144
180
  RAISING_FINDER_METHODS.include?(node.method_name)
145
181
  end
146
182
 
183
+ def block_call?(send_node)
184
+ send_node.block_literal? || send_node.last_argument&.block_pass_type?
185
+ end
186
+
147
187
  def offense_range(method_node, node)
148
188
  method_node.loc.selector.join(node.source_range.end)
149
189
  end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.35.0'
7
+ STRING = '2.35.4'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.35.0
4
+ version: 2.35.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov