rubocop-rails 2.15.0 → 2.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +82 -1
- data/config/obsoletion.yml +10 -0
- data/lib/rubocop/cop/mixin/active_record_helper.rb +1 -4
- data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +1 -3
- data/lib/rubocop/cop/mixin/index_method.rb +5 -15
- data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +98 -0
- data/lib/rubocop/cop/rails/action_filter.rb +1 -1
- data/lib/rubocop/cop/rails/active_record_aliases.rb +1 -4
- data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
- data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
- data/lib/rubocop/cop/rails/add_column_index.rb +1 -4
- data/lib/rubocop/cop/rails/blank.rb +1 -2
- data/lib/rubocop/cop/rails/bulk_change_table.rb +6 -20
- data/lib/rubocop/cop/rails/compact_blank.rb +5 -1
- data/lib/rubocop/cop/rails/content_tag.rb +1 -3
- data/lib/rubocop/cop/rails/date.rb +4 -9
- data/lib/rubocop/cop/rails/delegate.rb +6 -14
- data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +18 -12
- data/lib/rubocop/cop/rails/dot_separated_keys.rb +2 -2
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +2 -4
- data/lib/rubocop/cop/rails/enum_hash.rb +1 -2
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -5
- data/lib/rubocop/cop/rails/environment_comparison.rb +1 -2
- data/lib/rubocop/cop/rails/expanded_date_range.rb +38 -22
- data/lib/rubocop/cop/rails/file_path.rb +2 -4
- data/lib/rubocop/cop/rails/find_each.rb +8 -2
- data/lib/rubocop/cop/rails/freeze_time.rb +69 -0
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +1 -3
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +4 -9
- data/lib/rubocop/cop/rails/http_status.rb +5 -10
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +3 -10
- data/lib/rubocop/cop/rails/inverse_of.rb +3 -6
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +2 -6
- data/lib/rubocop/cop/rails/link_to_blank.rb +1 -4
- data/lib/rubocop/cop/rails/order_by_id.rb +1 -2
- data/lib/rubocop/cop/rails/output.rb +2 -5
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +1 -2
- data/lib/rubocop/cop/rails/presence.rb +1 -3
- data/lib/rubocop/cop/rails/present.rb +3 -6
- data/lib/rubocop/cop/rails/rake_environment.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +2 -4
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +2 -2
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +28 -26
- data/lib/rubocop/cop/rails/reflection_class_name.rb +17 -0
- data/lib/rubocop/cop/rails/refute_methods.rb +1 -5
- data/lib/rubocop/cop/rails/relative_date_constant.rb +2 -5
- data/lib/rubocop/cop/rails/request_referer.rb +1 -2
- data/lib/rubocop/cop/rails/reversible_migration.rb +10 -33
- data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +1 -2
- data/lib/rubocop/cop/rails/root_pathname_methods.rb +175 -0
- data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +1 -3
- data/lib/rubocop/cop/rails/save_bang.rb +10 -22
- data/lib/rubocop/cop/rails/short_i18n.rb +1 -4
- data/lib/rubocop/cop/rails/skips_model_validations.rb +1 -2
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +1 -5
- data/lib/rubocop/cop/rails/strip_heredoc.rb +1 -1
- data/lib/rubocop/cop/rails/time_zone.rb +8 -19
- data/lib/rubocop/cop/rails/time_zone_assignment.rb +2 -2
- data/lib/rubocop/cop/rails/to_formatted_s.rb +1 -0
- data/lib/rubocop/cop/rails/to_s_with_argument.rb +41 -0
- data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +48 -0
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +1 -1
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -6
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +3 -5
- data/lib/rubocop/cop/rails/unknown_env.rb +2 -4
- data/lib/rubocop/cop/rails/validation.rb +4 -12
- data/lib/rubocop/cop/rails/where_missing.rb +111 -0
- data/lib/rubocop/cop/rails_cops.rb +7 -0
- data/lib/rubocop/rails/schema_loader/schema.rb +5 -5
- data/lib/rubocop/rails/version.rb +1 -1
- metadata +13 -8
- data/bin/console +0 -11
- data/bin/setup +0 -7
@@ -60,15 +60,6 @@ module RuboCop
|
|
60
60
|
|
61
61
|
MSG = 'Redundant receiver in `with_options`.'
|
62
62
|
|
63
|
-
def_node_matcher :with_options?, <<~PATTERN
|
64
|
-
(block
|
65
|
-
(send nil? :with_options
|
66
|
-
(...))
|
67
|
-
(args
|
68
|
-
$_arg)
|
69
|
-
$_body)
|
70
|
-
PATTERN
|
71
|
-
|
72
63
|
def_node_search :all_block_nodes_in, <<~PATTERN
|
73
64
|
(block ...)
|
74
65
|
PATTERN
|
@@ -78,29 +69,40 @@ module RuboCop
|
|
78
69
|
PATTERN
|
79
70
|
|
80
71
|
def on_block(node)
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
end
|
93
|
-
end
|
72
|
+
return unless node.method?(:with_options)
|
73
|
+
return unless (body = node.body)
|
74
|
+
return unless all_block_nodes_in(body).count.zero?
|
75
|
+
|
76
|
+
send_nodes = all_send_nodes_in(body)
|
77
|
+
return unless redundant_receiver?(send_nodes, node)
|
78
|
+
|
79
|
+
send_nodes.each do |send_node|
|
80
|
+
receiver = send_node.receiver
|
81
|
+
add_offense(receiver.source_range) do |corrector|
|
82
|
+
autocorrect(corrector, send_node, node)
|
94
83
|
end
|
95
84
|
end
|
96
85
|
end
|
97
86
|
|
87
|
+
alias on_numblock on_block
|
88
|
+
|
98
89
|
private
|
99
90
|
|
100
|
-
def autocorrect(corrector, node)
|
101
|
-
corrector.remove(
|
102
|
-
corrector.remove(
|
103
|
-
corrector.remove(block_argument_range(
|
91
|
+
def autocorrect(corrector, send_node, node)
|
92
|
+
corrector.remove(send_node.receiver.source_range)
|
93
|
+
corrector.remove(send_node.loc.dot)
|
94
|
+
corrector.remove(block_argument_range(send_node)) unless node.numblock_type?
|
95
|
+
end
|
96
|
+
|
97
|
+
def redundant_receiver?(send_nodes, node)
|
98
|
+
proc = if node.numblock_type?
|
99
|
+
->(n) { n.receiver.lvar_type? && n.receiver.source == '_1' }
|
100
|
+
else
|
101
|
+
arg = node.arguments.first
|
102
|
+
->(n) { same_value?(arg, n.receiver) }
|
103
|
+
end
|
104
|
+
|
105
|
+
send_nodes.all?(&proc)
|
104
106
|
end
|
105
107
|
|
106
108
|
def block_argument_range(node)
|
@@ -34,12 +34,29 @@ module RuboCop
|
|
34
34
|
|
35
35
|
def on_send(node)
|
36
36
|
association_with_reflection(node) do |reflection_class_name|
|
37
|
+
return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
|
38
|
+
return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)
|
39
|
+
|
37
40
|
add_offense(reflection_class_name.loc.expression)
|
38
41
|
end
|
39
42
|
end
|
40
43
|
|
41
44
|
private
|
42
45
|
|
46
|
+
def str_assigned?(reflection_class_name)
|
47
|
+
lvar = reflection_class_name.value.source
|
48
|
+
|
49
|
+
reflection_class_name.ancestors.each do |nodes|
|
50
|
+
return true if nodes.each_child_node(:lvasgn).detect do |node|
|
51
|
+
lhs, rhs = *node
|
52
|
+
|
53
|
+
lhs.to_s == lvar && ALLOWED_REFLECTION_CLASS_TYPES.include?(rhs.type)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
false
|
58
|
+
end
|
59
|
+
|
43
60
|
def reflection_class_value?(class_value)
|
44
61
|
if class_value.send_type?
|
45
62
|
!class_value.method?(:to_s) || class_value.receiver&.const_type?
|
@@ -81,11 +81,7 @@ module RuboCop
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def offense_message(method_name)
|
84
|
-
format(
|
85
|
-
MSG,
|
86
|
-
bad_method: method_name,
|
87
|
-
good_method: convert_good_method(method_name)
|
88
|
-
)
|
84
|
+
format(MSG, bad_method: method_name, good_method: convert_good_method(method_name))
|
89
85
|
end
|
90
86
|
|
91
87
|
def convert_good_method(bad_method)
|
@@ -34,8 +34,7 @@ module RuboCop
|
|
34
34
|
include RangeHelp
|
35
35
|
extend AutoCorrector
|
36
36
|
|
37
|
-
MSG = 'Do not assign `%<method_name>s` to constants as it '
|
38
|
-
'will be evaluated only once.'
|
37
|
+
MSG = 'Do not assign `%<method_name>s` to constants as it will be evaluated only once.'
|
39
38
|
RELATIVE_DATE_METHODS = %i[since from_now after ago until before yesterday tomorrow].to_set.freeze
|
40
39
|
|
41
40
|
def on_casgn(node)
|
@@ -77,9 +76,7 @@ module RuboCop
|
|
77
76
|
return unless scope.nil?
|
78
77
|
|
79
78
|
indent = ' ' * node.loc.column
|
80
|
-
new_code = ["def self.#{const_name.downcase}",
|
81
|
-
"#{indent}#{value.source}",
|
82
|
-
'end'].join("\n#{indent}")
|
79
|
+
new_code = ["def self.#{const_name.downcase}", "#{indent}#{value.source}", 'end'].join("\n#{indent}")
|
83
80
|
|
84
81
|
corrector.replace(node.source_range, new_code)
|
85
82
|
end
|
@@ -23,8 +23,7 @@ module RuboCop
|
|
23
23
|
include ConfigurableEnforcedStyle
|
24
24
|
extend AutoCorrector
|
25
25
|
|
26
|
-
MSG = 'Use `request.%<prefer>s` instead of '
|
27
|
-
'`request.%<current>s`.'
|
26
|
+
MSG = 'Use `request.%<prefer>s` instead of `request.%<current>s`.'
|
28
27
|
RESTRICT_ON_SEND = %i[referer referrer].freeze
|
29
28
|
|
30
29
|
def_node_matcher :referer?, <<~PATTERN
|
@@ -229,6 +229,8 @@ module RuboCop
|
|
229
229
|
check_change_table_node(node.send_node, node.body)
|
230
230
|
end
|
231
231
|
|
232
|
+
alias on_numblock on_block
|
233
|
+
|
232
234
|
private
|
233
235
|
|
234
236
|
def check_irreversible_schema_statement_node(node)
|
@@ -240,10 +242,7 @@ module RuboCop
|
|
240
242
|
def check_drop_table_node(node)
|
241
243
|
drop_table_call(node) do
|
242
244
|
unless node.parent.block_type? || node.last_argument.block_pass_type?
|
243
|
-
add_offense(
|
244
|
-
node,
|
245
|
-
message: format(MSG, action: 'drop_table(without block)')
|
246
|
-
)
|
245
|
+
add_offense(node, message: format(MSG, action: 'drop_table(without block)'))
|
247
246
|
end
|
248
247
|
end
|
249
248
|
end
|
@@ -251,22 +250,12 @@ module RuboCop
|
|
251
250
|
def check_reversible_hash_node(node)
|
252
251
|
return if reversible_change_table_call?(node)
|
253
252
|
|
254
|
-
add_offense(
|
255
|
-
node,
|
256
|
-
message: format(
|
257
|
-
MSG, action: "#{node.method_name}(without :from and :to)"
|
258
|
-
)
|
259
|
-
)
|
253
|
+
add_offense(node, message: format(MSG, action: "#{node.method_name}(without :from and :to)"))
|
260
254
|
end
|
261
255
|
|
262
256
|
def check_remove_column_node(node)
|
263
257
|
remove_column_call(node) do |args|
|
264
|
-
if args.to_a.size < 3
|
265
|
-
add_offense(
|
266
|
-
node,
|
267
|
-
message: format(MSG, action: 'remove_column(without type)')
|
268
|
-
)
|
269
|
-
end
|
258
|
+
add_offense(node, message: format(MSG, action: 'remove_column(without type)')) if args.to_a.size < 3
|
270
259
|
end
|
271
260
|
end
|
272
261
|
|
@@ -295,10 +284,7 @@ module RuboCop
|
|
295
284
|
unless all_hash_key?(args, :type) && target_rails_version >= 6.1
|
296
285
|
action = target_rails_version >= 6.1 ? 'remove_columns(without type)' : 'remove_columns'
|
297
286
|
|
298
|
-
add_offense(
|
299
|
-
node,
|
300
|
-
message: format(MSG, action: action)
|
301
|
-
)
|
287
|
+
add_offense(node, message: format(MSG, action: action))
|
302
288
|
end
|
303
289
|
end
|
304
290
|
end
|
@@ -306,18 +292,14 @@ module RuboCop
|
|
306
292
|
def check_remove_index_node(node)
|
307
293
|
remove_index_call(node) do |args|
|
308
294
|
if args.hash_type? && !all_hash_key?(args, :column)
|
309
|
-
add_offense(
|
310
|
-
node,
|
311
|
-
message: format(MSG, action: 'remove_index(without column)')
|
312
|
-
)
|
295
|
+
add_offense(node, message: format(MSG, action: 'remove_index(without column)'))
|
313
296
|
end
|
314
297
|
end
|
315
298
|
end
|
316
299
|
|
317
300
|
def check_change_table_offense(receiver, node)
|
318
301
|
method_name = node.method_name
|
319
|
-
return if receiver != node.receiver &&
|
320
|
-
reversible_change_table_call?(node)
|
302
|
+
return if receiver != node.receiver && reversible_change_table_call?(node)
|
321
303
|
|
322
304
|
action = if method_name == :remove
|
323
305
|
target_rails_version >= 6.1 ? 't.remove (without type)' : 't.remove'
|
@@ -325,10 +307,7 @@ module RuboCop
|
|
325
307
|
"change_table(with #{method_name})"
|
326
308
|
end
|
327
309
|
|
328
|
-
add_offense(
|
329
|
-
node,
|
330
|
-
message: format(MSG, action: action)
|
331
|
-
)
|
310
|
+
add_offense(node, message: format(MSG, action: action))
|
332
311
|
end
|
333
312
|
|
334
313
|
def reversible_change_table_call?(node)
|
@@ -353,9 +332,7 @@ module RuboCop
|
|
353
332
|
|
354
333
|
def within_reversible_or_up_only_block?(node)
|
355
334
|
node.each_ancestor(:block).any? do |ancestor|
|
356
|
-
(ancestor.block_type? &&
|
357
|
-
ancestor.send_node.method?(:reversible)) ||
|
358
|
-
ancestor.send_node.method?(:up_only)
|
335
|
+
(ancestor.block_type? && ancestor.send_node.method?(:reversible)) || ancestor.send_node.method?(:up_only)
|
359
336
|
end
|
360
337
|
end
|
361
338
|
|
@@ -45,8 +45,7 @@ module RuboCop
|
|
45
45
|
class ReversibleMigrationMethodDefinition < Base
|
46
46
|
include MigrationsHelper
|
47
47
|
|
48
|
-
MSG = 'Migrations must contain either a `change` method, or '
|
49
|
-
'both an `up` and a `down` method.'
|
48
|
+
MSG = 'Migrations must contain either a `change` method, or both an `up` and a `down` method.'
|
50
49
|
|
51
50
|
def_node_matcher :change_method?, <<~PATTERN
|
52
51
|
`(def :change (args) _)
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Use `Rails.root` IO methods instead of passing it to `File`.
|
7
|
+
#
|
8
|
+
# `Rails.root` is an instance of `Pathname`
|
9
|
+
# so we can apply many IO methods directly.
|
10
|
+
#
|
11
|
+
# This cop works best when used together with
|
12
|
+
# `Style/FileRead`, `Style/FileWrite` and `Rails/RootJoinChain`.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# # bad
|
16
|
+
# File.open(Rails.root.join('db', 'schema.rb'))
|
17
|
+
# File.open(Rails.root.join('db', 'schema.rb'), 'w')
|
18
|
+
# File.read(Rails.root.join('db', 'schema.rb'))
|
19
|
+
# File.binread(Rails.root.join('db', 'schema.rb'))
|
20
|
+
# File.write(Rails.root.join('db', 'schema.rb'), content)
|
21
|
+
# File.binwrite(Rails.root.join('db', 'schema.rb'), content)
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# Rails.root.join('db', 'schema.rb').open
|
25
|
+
# Rails.root.join('db', 'schema.rb').open('w')
|
26
|
+
# Rails.root.join('db', 'schema.rb').read
|
27
|
+
# Rails.root.join('db', 'schema.rb').binread
|
28
|
+
# Rails.root.join('db', 'schema.rb').write(content)
|
29
|
+
# Rails.root.join('db', 'schema.rb').binwrite(content)
|
30
|
+
#
|
31
|
+
class RootPathnameMethods < Base
|
32
|
+
extend AutoCorrector
|
33
|
+
|
34
|
+
MSG = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'
|
35
|
+
|
36
|
+
DIR_METHODS = %i[children delete each_child empty? entries exist? glob mkdir open rmdir unlink].to_set.freeze
|
37
|
+
|
38
|
+
FILE_METHODS = %i[
|
39
|
+
atime
|
40
|
+
basename
|
41
|
+
binread
|
42
|
+
binwrite
|
43
|
+
birthtime
|
44
|
+
blockdev?
|
45
|
+
chardev?
|
46
|
+
chmod
|
47
|
+
chown
|
48
|
+
ctime
|
49
|
+
delete
|
50
|
+
directory?
|
51
|
+
dirname
|
52
|
+
empty?
|
53
|
+
executable?
|
54
|
+
executable_real?
|
55
|
+
exist?
|
56
|
+
expand_path
|
57
|
+
extname
|
58
|
+
file?
|
59
|
+
fnmatch
|
60
|
+
fnmatch?
|
61
|
+
ftype
|
62
|
+
grpowned?
|
63
|
+
join
|
64
|
+
lchmod
|
65
|
+
lchown
|
66
|
+
lstat
|
67
|
+
mtime
|
68
|
+
open
|
69
|
+
owned?
|
70
|
+
pipe?
|
71
|
+
read
|
72
|
+
readable?
|
73
|
+
readable_real?
|
74
|
+
readlines
|
75
|
+
readlink
|
76
|
+
realdirpath
|
77
|
+
realpath
|
78
|
+
rename
|
79
|
+
setgid?
|
80
|
+
setuid?
|
81
|
+
size
|
82
|
+
size?
|
83
|
+
socket?
|
84
|
+
split
|
85
|
+
stat
|
86
|
+
sticky?
|
87
|
+
symlink?
|
88
|
+
sysopen
|
89
|
+
truncate
|
90
|
+
unlink
|
91
|
+
utime
|
92
|
+
world_readable?
|
93
|
+
world_writable?
|
94
|
+
writable?
|
95
|
+
writable_real?
|
96
|
+
write
|
97
|
+
zero?
|
98
|
+
].to_set.freeze
|
99
|
+
|
100
|
+
FILE_TEST_METHODS = %i[
|
101
|
+
blockdev?
|
102
|
+
chardev?
|
103
|
+
directory?
|
104
|
+
empty?
|
105
|
+
executable?
|
106
|
+
executable_real?
|
107
|
+
exist?
|
108
|
+
file?
|
109
|
+
grpowned?
|
110
|
+
owned?
|
111
|
+
pipe?
|
112
|
+
readable?
|
113
|
+
readable_real?
|
114
|
+
setgid?
|
115
|
+
setuid?
|
116
|
+
size
|
117
|
+
size?
|
118
|
+
socket?
|
119
|
+
sticky?
|
120
|
+
symlink?
|
121
|
+
world_readable?
|
122
|
+
world_writable?
|
123
|
+
writable?
|
124
|
+
writable_real?
|
125
|
+
zero?
|
126
|
+
].to_set.freeze
|
127
|
+
|
128
|
+
FILE_UTILS_METHODS = %i[chmod chown mkdir mkpath rmdir rmtree].to_set.freeze
|
129
|
+
|
130
|
+
RESTRICT_ON_SEND = (DIR_METHODS + FILE_METHODS + FILE_TEST_METHODS + FILE_UTILS_METHODS).to_set.freeze
|
131
|
+
|
132
|
+
def_node_matcher :pathname_method, <<~PATTERN
|
133
|
+
{
|
134
|
+
(send (const {nil? cbase} :Dir) $DIR_METHODS $_ $...)
|
135
|
+
(send (const {nil? cbase} {:IO :File}) $FILE_METHODS $_ $...)
|
136
|
+
(send (const {nil? cbase} :FileTest) $FILE_TEST_METHODS $_ $...)
|
137
|
+
(send (const {nil? cbase} :FileUtils) $FILE_UTILS_METHODS $_ $...)
|
138
|
+
}
|
139
|
+
PATTERN
|
140
|
+
|
141
|
+
def_node_matcher :rails_root_pathname?, <<~PATTERN
|
142
|
+
{
|
143
|
+
$#rails_root?
|
144
|
+
(send $#rails_root? :join ...)
|
145
|
+
}
|
146
|
+
PATTERN
|
147
|
+
|
148
|
+
# @!method rails_root?(node)
|
149
|
+
def_node_matcher :rails_root?, <<~PATTERN
|
150
|
+
(send (const {nil? cbase} :Rails) {:root :public_path})
|
151
|
+
PATTERN
|
152
|
+
|
153
|
+
def on_send(node)
|
154
|
+
evidence(node) do |method, path, args, rails_root|
|
155
|
+
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?
|
158
|
+
|
159
|
+
corrector.replace(node, replacement)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
|
166
|
+
def evidence(node)
|
167
|
+
return if node.method?(:open) && node.parent&.send_type?
|
168
|
+
return unless (method, path, args = pathname_method(node)) && (rails_root = rails_root_pathname?(path))
|
169
|
+
|
170
|
+
yield(method, path, args, rails_root)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -31,9 +31,7 @@ module RuboCop
|
|
31
31
|
class SafeNavigationWithBlank < Base
|
32
32
|
extend AutoCorrector
|
33
33
|
|
34
|
-
MSG =
|
35
|
-
'Avoid calling `blank?` with the safe navigation operator ' \
|
36
|
-
'in conditionals.'
|
34
|
+
MSG = 'Avoid calling `blank?` with the safe navigation operator in conditionals.'
|
37
35
|
|
38
36
|
def_node_matcher :safe_navigation_blank_in_conditional?, <<~PATTERN
|
39
37
|
(if $(csend ... :blank?) ...)
|
@@ -121,18 +121,12 @@ module RuboCop
|
|
121
121
|
include NegativeConditional
|
122
122
|
extend AutoCorrector
|
123
123
|
|
124
|
-
MSG = 'Use `%<prefer>s` instead of `%<current>s` if the return '
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
'always truthy.'
|
131
|
-
|
132
|
-
CREATE_PERSIST_METHODS = %i[create create_or_find_by
|
133
|
-
first_or_create find_or_create_by].freeze
|
134
|
-
MODIFY_PERSIST_METHODS = %i[save
|
135
|
-
update update_attributes destroy].freeze
|
124
|
+
MSG = 'Use `%<prefer>s` instead of `%<current>s` if the return value is not checked.'
|
125
|
+
CREATE_MSG = "#{MSG} Or check `persisted?` on model returned from `%<current>s`."
|
126
|
+
CREATE_CONDITIONAL_MSG = '`%<current>s` returns a model which is always truthy.'
|
127
|
+
|
128
|
+
CREATE_PERSIST_METHODS = %i[create create_or_find_by first_or_create find_or_create_by].freeze
|
129
|
+
MODIFY_PERSIST_METHODS = %i[save update update_attributes destroy].freeze
|
136
130
|
RESTRICT_ON_SEND = (CREATE_PERSIST_METHODS + MODIFY_PERSIST_METHODS).freeze
|
137
131
|
|
138
132
|
def self.joining_forces
|
@@ -244,8 +238,7 @@ module RuboCop
|
|
244
238
|
parent = node.parent
|
245
239
|
return false unless parent
|
246
240
|
|
247
|
-
operator_or_single_negative?(parent) ||
|
248
|
-
(conditional?(parent) && node == parent.condition)
|
241
|
+
operator_or_single_negative?(parent) || (conditional?(parent) && node == parent.condition)
|
249
242
|
end
|
250
243
|
|
251
244
|
def operator_or_single_negative?(node)
|
@@ -294,9 +287,7 @@ module RuboCop
|
|
294
287
|
# NameSpace::Const != ::Const
|
295
288
|
# Const != NameSpace::Const
|
296
289
|
def const_matches?(const, allowed_const)
|
297
|
-
parts = allowed_const.split('::').reverse.zip(
|
298
|
-
const.split('::').reverse
|
299
|
-
)
|
290
|
+
parts = allowed_const.split('::').reverse.zip(const.split('::').reverse)
|
300
291
|
parts.all? do |(allowed_part, const_part)|
|
301
292
|
allowed_part == const_part.to_s
|
302
293
|
end
|
@@ -335,9 +326,7 @@ module RuboCop
|
|
335
326
|
end
|
336
327
|
|
337
328
|
def persist_method?(node, methods = RESTRICT_ON_SEND)
|
338
|
-
methods.include?(node.method_name) &&
|
339
|
-
expected_signature?(node) &&
|
340
|
-
!allowed_receiver?(node)
|
329
|
+
methods.include?(node.method_name) && expected_signature?(node) && !allowed_receiver?(node)
|
341
330
|
end
|
342
331
|
|
343
332
|
# Check argument signature as no arguments or one hash
|
@@ -345,8 +334,7 @@ module RuboCop
|
|
345
334
|
!node.arguments? ||
|
346
335
|
(node.arguments.one? &&
|
347
336
|
node.method_name != :destroy &&
|
348
|
-
(node.first_argument.hash_type? ||
|
349
|
-
!node.first_argument.literal?))
|
337
|
+
(node.first_argument.hash_type? || !node.first_argument.literal?))
|
350
338
|
end
|
351
339
|
end
|
352
340
|
end
|
@@ -44,10 +44,7 @@ module RuboCop
|
|
44
44
|
|
45
45
|
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
|
46
46
|
|
47
|
-
PREFERRED_METHODS = {
|
48
|
-
translate: :t,
|
49
|
-
localize: :l
|
50
|
-
}.freeze
|
47
|
+
PREFERRED_METHODS = { translate: :t, localize: :l }.freeze
|
51
48
|
|
52
49
|
RESTRICT_ON_SEND = PREFERRED_METHODS.keys.freeze
|
53
50
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
|
44
44
|
def register_offense(node, heredoc)
|
45
45
|
add_offense(node) do |corrector|
|
46
|
-
squiggly_heredoc = heredoc.source.sub(/\A
|
46
|
+
squiggly_heredoc = heredoc.source.sub(/\A<<(-|~)?/, '<<~')
|
47
47
|
|
48
48
|
corrector.replace(heredoc, squiggly_heredoc)
|
49
49
|
corrector.remove(node.loc.dot)
|
@@ -43,21 +43,17 @@ module RuboCop
|
|
43
43
|
include ConfigurableEnforcedStyle
|
44
44
|
extend AutoCorrector
|
45
45
|
|
46
|
-
MSG = 'Do not use `%<current>s` without zone. Use `%<prefer>s` '
|
47
|
-
'instead.'
|
46
|
+
MSG = 'Do not use `%<current>s` without zone. Use `%<prefer>s` instead.'
|
48
47
|
|
49
|
-
MSG_ACCEPTABLE = 'Do not use `%<current>s` without zone. '
|
50
|
-
'Use one of %<prefer>s instead.'
|
48
|
+
MSG_ACCEPTABLE = 'Do not use `%<current>s` without zone. Use one of %<prefer>s instead.'
|
51
49
|
|
52
|
-
MSG_LOCALTIME = 'Do not use `Time.localtime` without '
|
53
|
-
'offset or zone.'
|
50
|
+
MSG_LOCALTIME = 'Do not use `Time.localtime` without offset or zone.'
|
54
51
|
|
55
52
|
GOOD_METHODS = %i[zone zone_default find_zone find_zone!].freeze
|
56
53
|
|
57
54
|
DANGEROUS_METHODS = %i[now local new parse at].freeze
|
58
55
|
|
59
|
-
ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601
|
60
|
-
jisx0301 rfc3339 httpdate to_i to_f].freeze
|
56
|
+
ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601 jisx0301 rfc3339 httpdate to_i to_f].freeze
|
61
57
|
|
62
58
|
TIMEZONE_SPECIFIER = /[A-z]/.freeze
|
63
59
|
|
@@ -100,13 +96,11 @@ module RuboCop
|
|
100
96
|
# remove redundant `.in_time_zone` from `Time.zone.now.in_time_zone`
|
101
97
|
def remove_redundant_in_time_zone(corrector, node)
|
102
98
|
time_methods_called = extract_method_chain(node)
|
103
|
-
return unless time_methods_called.include?(:in_time_zone) ||
|
104
|
-
time_methods_called.include?(:zone)
|
99
|
+
return unless time_methods_called.include?(:in_time_zone) || time_methods_called.include?(:zone)
|
105
100
|
|
106
101
|
while node&.send_type?
|
107
102
|
if node.children.last == :in_time_zone
|
108
|
-
in_time_zone_with_dot =
|
109
|
-
node.loc.selector.adjust(begin_pos: -1)
|
103
|
+
in_time_zone_with_dot = node.loc.selector.adjust(begin_pos: -1)
|
110
104
|
corrector.remove(in_time_zone_with_dot)
|
111
105
|
end
|
112
106
|
node = node.parent
|
@@ -144,9 +138,7 @@ module RuboCop
|
|
144
138
|
)
|
145
139
|
else
|
146
140
|
safe_method_name = safe_method(method_name, node)
|
147
|
-
format(MSG,
|
148
|
-
current: "#{klass}.#{method_name}",
|
149
|
-
prefer: "Time.zone.#{safe_method_name}")
|
141
|
+
format(MSG, current: "#{klass}.#{method_name}", prefer: "Time.zone.#{safe_method_name}")
|
150
142
|
end
|
151
143
|
end
|
152
144
|
|
@@ -227,10 +219,7 @@ module RuboCop
|
|
227
219
|
end
|
228
220
|
|
229
221
|
def acceptable_methods(klass, method_name, node)
|
230
|
-
acceptable = [
|
231
|
-
"`Time.zone.#{safe_method(method_name, node)}`",
|
232
|
-
"`#{klass}.current`"
|
233
|
-
]
|
222
|
+
acceptable = ["`Time.zone.#{safe_method(method_name, node)}`", "`#{klass}.current`"]
|
234
223
|
|
235
224
|
ACCEPTED_METHODS.each do |am|
|
236
225
|
acceptable << "`#{klass}.#{method_name}.#{am}`"
|
@@ -22,12 +22,12 @@ module RuboCop
|
|
22
22
|
MSG = 'Use `Time.use_zone` with block instead of `Time.zone=`.'
|
23
23
|
RESTRICT_ON_SEND = %i[zone=].freeze
|
24
24
|
|
25
|
-
def_node_matcher :
|
25
|
+
def_node_matcher :time_zone_assignment?, <<~PATTERN
|
26
26
|
(send (const nil? :Time) :zone= ...)
|
27
27
|
PATTERN
|
28
28
|
|
29
29
|
def on_send(node)
|
30
|
-
return unless
|
30
|
+
return unless time_zone_assignment?(node)
|
31
31
|
|
32
32
|
add_offense(node)
|
33
33
|
end
|