rubocop-rails 2.16.0 → 2.16.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a928896af6bef8228bfc2530b829a683154c23b22ea547557d40ea1e4d08ae7f
4
- data.tar.gz: 40303c4d80e1e6eaef12ceb0170b64b65b151fc2056957d6c6b6bac1d645d445
3
+ metadata.gz: 531a7f7b4bc04b877c25047dc10ee38e10804ad233b5b2abd425b613d508f0b5
4
+ data.tar.gz: 135eaa3b4ab1c2393f8ab72cd961470c713552d2d6300bd2ea0f981831ea5cba
5
5
  SHA512:
6
- metadata.gz: a87b9c167cf43287207bae3ec38e385cb6213c26ff0f7e10aa3bf392ab28b724794820a459dc6af6d464e48ea70629c443e74acc4539dca6f0c0a57fee65d87c
7
- data.tar.gz: fbb0b7333c6d485e50facadd7170bb2d59e425119134d01f42ac3d24eecc01ad52d588f55de184c9373061ba207cf6845d3e8201fe43ce48e3a8d3b22e992dff
6
+ metadata.gz: ab99c18ad7f0684d03945b48393cadecd20f465ade8f27a9876b874210e2b930300ce5b08d7e6b8d654595bca67ae321b6afef88c05843ddd79ff47cf1b80708
7
+ data.tar.gz: 879fd14dfff79cd8afe60e9f6678d66f61eefe8b91cdbe36c92cbb31541f6735b59d5dc33a8c21f8330dac60f359c4e46ad59f43b259d81ea25c366d3f26ca50
data/config/default.yml CHANGED
@@ -845,6 +845,7 @@ Rails/RootJoinChain:
845
845
  Rails/RootPathnameMethods:
846
846
  Description: 'Use `Rails.root` IO methods instead of passing it to `File`.'
847
847
  Enabled: pending
848
+ SafeAutocorrect: false
848
849
  VersionAdded: '2.16'
849
850
 
850
851
  Rails/RootPublicPath:
@@ -43,10 +43,15 @@ module RuboCop
43
43
  PATTERN
44
44
 
45
45
  def on_send(node)
46
- child_node, method_name = *node.first_argument.children
46
+ child_node, method_name, time_argument = *node.first_argument.children
47
+ return if time_argument || !child_node
47
48
  return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name)
48
49
 
49
- add_offense(node) { |corrector| corrector.replace(node, 'freeze_time') }
50
+ add_offense(node) do |corrector|
51
+ last_argument = node.last_argument
52
+ freeze_time_method = last_argument.block_pass_type? ? "freeze_time(#{last_argument.source})" : 'freeze_time'
53
+ corrector.replace(node, freeze_time_method)
54
+ end
50
55
  end
51
56
 
52
57
  private
@@ -11,6 +11,10 @@ module RuboCop
11
11
  # This cop works best when used together with
12
12
  # `Style/FileRead`, `Style/FileWrite` and `Rails/RootJoinChain`.
13
13
  #
14
+ # @safety
15
+ # This cop is unsafe for autocorrection because `Dir`'s `children`, `each_child`, `entries`, and `glob`
16
+ # methods return string element, but these methods of `Pathname` return `Pathname` element.
17
+ #
14
18
  # @example
15
19
  # # bad
16
20
  # File.open(Rails.root.join('db', 'schema.rb'))
@@ -30,6 +34,7 @@ module RuboCop
30
34
  #
31
35
  class RootPathnameMethods < Base
32
36
  extend AutoCorrector
37
+ include RangeHelp
33
38
 
34
39
  MSG = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'
35
40
 
@@ -138,6 +143,11 @@ module RuboCop
138
143
  }
139
144
  PATTERN
140
145
 
146
+ def_node_matcher :dir_glob?, <<~PATTERN
147
+ (send
148
+ (const {cbase nil?} :Dir) :glob ...)
149
+ PATTERN
150
+
141
151
  def_node_matcher :rails_root_pathname?, <<~PATTERN
142
152
  {
143
153
  $#rails_root?
@@ -153,8 +163,12 @@ module RuboCop
153
163
  def on_send(node)
154
164
  evidence(node) do |method, path, args, rails_root|
155
165
  add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector|
156
- replacement = "#{path.source}.#{method}"
157
- replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?
166
+ if dir_glob?(node)
167
+ replacement = build_path_glob(path, method)
168
+ else
169
+ replacement = "#{path.source}.#{method}"
170
+ replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?
171
+ end
158
172
 
159
173
  corrector.replace(node, replacement)
160
174
  end
@@ -169,6 +183,31 @@ module RuboCop
169
183
 
170
184
  yield(method, path, args, rails_root)
171
185
  end
186
+
187
+ def build_path_glob(path, method)
188
+ receiver = range_between(path.loc.expression.begin_pos, path.children.first.loc.selector.end_pos).source
189
+
190
+ argument = if path.arguments.one?
191
+ path.first_argument.source
192
+ else
193
+ join_arguments(path.arguments)
194
+ end
195
+
196
+ "#{receiver}.#{method}(#{argument})"
197
+ end
198
+
199
+ def include_interpolation?(arguments)
200
+ arguments.any? do |argument|
201
+ argument.children.any? { |child| child.respond_to?(:begin_type?) && child.begin_type? }
202
+ end
203
+ end
204
+
205
+ def join_arguments(arguments)
206
+ quote = include_interpolation?(arguments) ? '"' : "'"
207
+ joined_arguments = arguments.map(&:value).join('/')
208
+
209
+ "#{quote}#{joined_arguments}#{quote}"
210
+ end
172
211
  end
173
212
  end
174
213
  end
@@ -31,6 +31,7 @@ module RuboCop
31
31
  # @param [RuboCop::AST::ConstNode] node
32
32
  def on_const(node)
33
33
  return unless top_level_hash_with_indifferent_access?(node)
34
+ return if node.parent&.class_type? && node.parent.ancestors.any?(&:module_type?)
34
35
 
35
36
  add_offense(node) do |corrector|
36
37
  autocorrect(corrector, node)
@@ -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.16.0'
7
+ STRING = '2.16.1'
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.16.0
4
+ version: 2.16.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-09-09 00:00:00.000000000 Z
13
+ date: 2022-09-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport