sevencop 0.32.0 → 0.32.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: b73ab476e9b75a7c542283153fb21a6e34bc065305f174ad7fc360d8d4fe83ab
4
- data.tar.gz: 36704b1877d0042042c91112699c201566a60bfc84e9bea4eb00824f910d6516
3
+ metadata.gz: f4a4965c23bf272ebe52214f0c8a37c6bd989638560cbf7e079b3af2e9782622
4
+ data.tar.gz: 134934de2b553fabf8b07b63c5e10364178fd007866bb45cea8ff8e357985c77
5
5
  SHA512:
6
- metadata.gz: b6d0f76043cf888387a230e1de66233a0ba68be0b647883bd8dac259f7cd7c97b504a3ce24577a14fd15fc590f146e17ff605223e2616e6c13992ec4e9a855d0
7
- data.tar.gz: ac8f462e97a095a80c612318d9eb69373c0b3c9ce11a3fc49c8143ba92d40adcdfc2279b6ad0edb0eaf07a6c12ac500a8090b12969feee0529ec91c981a1a72f
6
+ metadata.gz: 6bf335fbbf8fc003e83369b98b67c2af236bdfd2cee48b6a142ff13a34cea3d0f633a142f340924c61411755adb771171e1fb87498a70edc0e3c4d241f96ca22
7
+ data.tar.gz: a5592277696b87dc3ec141b8d2155681da27d7d1f8369d919fd5d24dc1da8d1c2d8ca1070315083e334217f1ecf9d7e77e8879efcbcdc86ed9639ec4aeed0500
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.32.0)
4
+ sevencop (0.32.2)
5
5
  activesupport
6
6
  rubocop
7
7
 
data/config/default.yml CHANGED
@@ -99,6 +99,7 @@ Sevencop/RailsOrderFieldInOrderOf:
99
99
  Description: |
100
100
  Prefer `in_order_of` to MySQL `FIELD` function.
101
101
  Enabled: false
102
+ SafeAutoCorrect: false
102
103
  VersionAdded: '0.32'
103
104
 
104
105
  Sevencop/RailsSpecificActionName:
@@ -70,7 +70,7 @@ module RuboCop
70
70
  class_node:
71
71
  )
72
72
  class_node.parent_class&.each_descendant(:cbase)&.any? do |descendant|
73
- descendant.eql?(node)
73
+ descendant.equal?(node)
74
74
  end
75
75
  end
76
76
  end
@@ -5,6 +5,23 @@ module RuboCop
5
5
  module Sevencop
6
6
  # Prefer `in_order_of` to MySQL `FIELD` function.
7
7
  #
8
+ # @safety
9
+ # This cop's autocorrection is unsafe because in the original code the array value is interpolated as
10
+ # literals on SQL query, but after its autocorrection, the value will be now passed directly to in_order_of,
11
+ # which may produce different results.
12
+ #
13
+ # For example, the following code is valid where the id column is a integer column:
14
+ #
15
+ # ids = %w[1 2 3]
16
+ # order("FIELD(id, #{ids.join(', ')})") # "FIELD(id, 1, 2, 3)"
17
+ #
18
+ # But after its autocorrection, it will be like this:
19
+ #
20
+ # ids = %w[1 2 3]
21
+ # in_order_of(:id, ids) # We need to change this code as `ids.map(&:to_i)`.
22
+ #
23
+ # And this code will raise an error because `ids` is not an array of Integer but an array of String.
24
+ #
8
25
  # @example
9
26
  # # bad
10
27
  # order('FIELD(id, 1, 2, 3)')
@@ -37,7 +54,7 @@ module RuboCop
37
54
  \s*
38
55
  field\(
39
56
  \s*
40
- (?<column_name>\w+)
57
+ (?<column_name>[^,]+)
41
58
  ,\s*
42
59
  \z
43
60
  /ix.freeze
@@ -59,7 +76,7 @@ module RuboCop
59
76
  \s*
60
77
  field\(
61
78
  \s*
62
- (?<column_name>\w+)
79
+ (?<column_name>[^,]+)
63
80
  ,\s*
64
81
  (?<values>.+)
65
82
  \s*
@@ -84,7 +101,9 @@ module RuboCop
84
101
  def on_send(node)
85
102
  return unless bad?(node)
86
103
 
87
- add_offense(node) do |corrector|
104
+ add_offense(
105
+ convert_to_autocorrected_range(node)
106
+ ) do |corrector|
88
107
  autocorrect(corrector, node)
89
108
  end
90
109
  end
@@ -148,17 +167,23 @@ module RuboCop
148
167
  node
149
168
  )
150
169
  corrector.replace(
151
- node.location.expression.with(
152
- begin_pos: node.location.selector.begin_pos
153
- ),
170
+ convert_to_autocorrected_range(node),
154
171
  format_in_order_of(node)
155
172
  )
156
173
  end
157
174
 
175
+ # @param node [RuboCop::AST::SendNode]
176
+ # @return [Parser::Source::Range]
177
+ def convert_to_autocorrected_range(node)
178
+ node.location.expression.with(
179
+ begin_pos: node.location.selector.begin_pos
180
+ )
181
+ end
182
+
158
183
  # @param node [RuboCop::AST::SendNode]
159
184
  # @return [String, nil]
160
185
  def extract_column_name(node)
161
- if node.each_descendant(:dstr).any?
186
+ if search_from_arguments(node, type: :dstr).any?
162
187
  extract_column_name_from_dstr(node)
163
188
  else
164
189
  extract_column_name_from_str(node)
@@ -168,25 +193,25 @@ module RuboCop
168
193
  # @param node [RuboCop::AST::SendNode]
169
194
  # @return [String]
170
195
  def extract_column_name_from_dstr(node)
171
- node.each_descendant(:str).first.value[REGEXP_FIELD_DSTR_HEAD, :column_name]
196
+ search_from_arguments(node, type: :str).first.value[REGEXP_FIELD_DSTR_HEAD, :column_name].delete('`')
172
197
  end
173
198
 
174
199
  # @param node [RuboCop::AST::SendNode]
175
200
  # @return [String]
176
201
  def extract_column_name_from_str(node)
177
- node.each_descendant(:str).first.value[REGEXP_FIELD_STR, :column_name]
202
+ search_from_arguments(node, type: :str).first.value[REGEXP_FIELD_STR, :column_name].delete('`')
178
203
  end
179
204
 
180
205
  # @param node [RuboCop::AST::SendNode]
181
206
  # @return [String, nil]
182
207
  def extract_order_from_dstr(node)
183
- node.each_descendant(:str).to_a.last.value[REGEXP_FIELD_DSTR_TAIL, :order]
208
+ search_from_arguments(node, type: :str).to_a.last.value[REGEXP_FIELD_DSTR_TAIL, :order]
184
209
  end
185
210
 
186
211
  # @param node [RuboCop::AST::SendNode]
187
212
  # @return [String, nil]
188
213
  def extract_order_from_str(node)
189
- node.each_descendant(:str).first.value[REGEXP_FIELD_STR, :order]
214
+ search_from_arguments(node, type: :str).first.value[REGEXP_FIELD_STR, :order]
190
215
  end
191
216
 
192
217
  # @param node [RuboCop::AST::SendNode]
@@ -216,7 +241,7 @@ module RuboCop
216
241
  # @param node [RuboCop::AST::SendNode]
217
242
  # @return [String]
218
243
  def extract_values_from_dstr(node)
219
- node.each_descendant.find do |descendant|
244
+ search_from_arguments(node).find do |descendant|
220
245
  match_field_dstr_body?(descendant)
221
246
  end.children.first.receiver.source
222
247
  end
@@ -226,14 +251,14 @@ module RuboCop
226
251
  def extract_values_from_str(node)
227
252
  format(
228
253
  '[%<values>s]',
229
- values: node.each_descendant(:str).first.value[REGEXP_FIELD_STR, :values].split(',').map(&:strip).join(', ')
254
+ values: search_from_arguments(node, type: :str).first.value[REGEXP_FIELD_STR, :values].split(',').map(&:strip).join(', ')
230
255
  )
231
256
  end
232
257
 
233
258
  # @param node [RuboCop::AST::SendNode]
234
259
  # @return [String]
235
260
  def format_in_order_of(node)
236
- if node.each_descendant(:dstr).any?
261
+ if search_from_arguments(node, type: :dstr).any?
237
262
  format_in_order_of_on_dstr(node)
238
263
  else
239
264
  format_in_order_of_on_str(node)
@@ -244,7 +269,7 @@ module RuboCop
244
269
  # @return [String]
245
270
  def format_in_order_of_on_dstr(node)
246
271
  format(
247
- 'in_order_of(:%<column_name>s, %<values>s)%<reverse_order>s%<rest_order>s',
272
+ "in_order_of(:'%<column_name>s', %<values>s)%<reverse_order>s%<rest_order>s",
248
273
  column_name: extract_column_name_from_dstr(node),
249
274
  rest_order: extract_rest_order(node),
250
275
  reverse_order: extract_reverse_order_from_dstr(node),
@@ -256,7 +281,7 @@ module RuboCop
256
281
  # @return [String]
257
282
  def format_in_order_of_on_str(node)
258
283
  format(
259
- 'in_order_of(:%<column_name>s, %<values>s)%<reverse_order>s%<rest_order>s',
284
+ "in_order_of(:'%<column_name>s', %<values>s)%<reverse_order>s%<rest_order>s",
260
285
  column_name: extract_column_name_from_str(node),
261
286
  rest_order: extract_rest_order(node),
262
287
  reverse_order: extract_reverse_order_from_str(node),
@@ -310,6 +335,17 @@ module RuboCop
310
335
  node.str_type? &&
311
336
  node.value.match?(REGEXP_FIELD_STR)
312
337
  end
338
+
339
+ # @param node [RuboCop::AST::SendNode]
340
+ # @param type [Symbol]
341
+ def search_from_arguments(
342
+ node,
343
+ type: nil
344
+ )
345
+ node.arguments.flat_map do |argument|
346
+ argument.each_node(*Array(type)).to_a
347
+ end
348
+ end
313
349
  end
314
350
  end
315
351
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.32.0'
4
+ VERSION = '0.32.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sevencop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.32.0
4
+ version: 0.32.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-20 00:00:00.000000000 Z
11
+ date: 2022-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport