rubocop-rails 2.8.1 → 2.9.0
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 +4 -4
- data/README.md +16 -0
- data/config/default.yml +33 -1
- data/lib/rubocop/cop/mixin/active_record_helper.rb +4 -3
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
- data/lib/rubocop/cop/mixin/index_method.rb +8 -11
- data/lib/rubocop/cop/rails/action_filter.rb +10 -14
- data/lib/rubocop/cop/rails/active_record_aliases.rb +13 -17
- data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +17 -12
- data/lib/rubocop/cop/rails/active_record_override.rb +1 -1
- data/lib/rubocop/cop/rails/active_support_aliases.rb +12 -21
- data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
- data/lib/rubocop/cop/rails/application_controller.rb +3 -7
- data/lib/rubocop/cop/rails/application_job.rb +2 -1
- data/lib/rubocop/cop/rails/application_mailer.rb +2 -7
- data/lib/rubocop/cop/rails/application_record.rb +2 -7
- data/lib/rubocop/cop/rails/arel_star.rb +41 -0
- data/lib/rubocop/cop/rails/assert_not.rb +8 -10
- data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
- data/lib/rubocop/cop/rails/belongs_to.rb +9 -18
- data/lib/rubocop/cop/rails/blank.rb +27 -27
- data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
- data/lib/rubocop/cop/rails/content_tag.rb +16 -16
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -1
- data/lib/rubocop/cop/rails/date.rb +10 -11
- data/lib/rubocop/cop/rails/default_scope.rb +11 -4
- data/lib/rubocop/cop/rails/delegate.rb +9 -9
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +7 -8
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +12 -11
- data/lib/rubocop/cop/rails/enum_hash.rb +11 -10
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -1
- data/lib/rubocop/cop/rails/environment_comparison.rb +18 -14
- data/lib/rubocop/cop/rails/exit.rb +4 -10
- data/lib/rubocop/cop/rails/file_path.rb +4 -3
- data/lib/rubocop/cop/rails/find_by.rb +13 -13
- data/lib/rubocop/cop/rails/find_by_id.rb +12 -21
- data/lib/rubocop/cop/rails/find_each.rb +16 -14
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +3 -2
- data/lib/rubocop/cop/rails/helper_instance_variable.rb +2 -2
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +19 -21
- data/lib/rubocop/cop/rails/http_status.rb +7 -9
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +8 -6
- data/lib/rubocop/cop/rails/index_by.rb +2 -1
- data/lib/rubocop/cop/rails/index_with.rb +2 -1
- data/lib/rubocop/cop/rails/inquiry.rb +4 -3
- data/lib/rubocop/cop/rails/inverse_of.rb +3 -2
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -15
- data/lib/rubocop/cop/rails/link_to_blank.rb +20 -22
- data/lib/rubocop/cop/rails/mailer_name.rb +19 -13
- data/lib/rubocop/cop/rails/match_route.rb +14 -13
- data/lib/rubocop/cop/rails/negate_include.rb +10 -8
- data/lib/rubocop/cop/rails/not_null_column.rb +2 -1
- data/lib/rubocop/cop/rails/order_by_id.rb +1 -2
- data/lib/rubocop/cop/rails/output.rb +5 -2
- data/lib/rubocop/cop/rails/output_safety.rb +3 -2
- data/lib/rubocop/cop/rails/pick.rb +14 -12
- data/lib/rubocop/cop/rails/pluck.rb +6 -9
- data/lib/rubocop/cop/rails/pluck_id.rb +4 -6
- data/lib/rubocop/cop/rails/pluck_in_where.rb +7 -7
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +10 -14
- data/lib/rubocop/cop/rails/presence.rb +12 -13
- data/lib/rubocop/cop/rails/present.rb +30 -24
- data/lib/rubocop/cop/rails/rake_environment.rb +8 -10
- data/lib/rubocop/cop/rails/read_write_attribute.rb +12 -11
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +29 -31
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +9 -12
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +11 -10
- data/lib/rubocop/cop/rails/reflection_class_name.rb +3 -2
- data/lib/rubocop/cop/rails/refute_methods.rb +9 -10
- data/lib/rubocop/cop/rails/relative_date_constant.rb +16 -8
- data/lib/rubocop/cop/rails/render_inline.rb +2 -1
- data/lib/rubocop/cop/rails/render_plain_text.rb +9 -14
- data/lib/rubocop/cop/rails/request_referer.rb +7 -7
- data/lib/rubocop/cop/rails/reversible_migration.rb +2 -6
- data/lib/rubocop/cop/rails/safe_navigation.rb +11 -10
- data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +5 -10
- data/lib/rubocop/cop/rails/save_bang.rb +17 -20
- data/lib/rubocop/cop/rails/scope_args.rb +2 -1
- data/lib/rubocop/cop/rails/short_i18n.rb +7 -9
- data/lib/rubocop/cop/rails/skips_model_validations.rb +4 -4
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +5 -6
- data/lib/rubocop/cop/rails/time_zone.rb +22 -20
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +4 -6
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +2 -2
- data/lib/rubocop/cop/rails/unknown_env.rb +2 -2
- data/lib/rubocop/cop/rails/validation.rb +15 -14
- data/lib/rubocop/cop/rails/where_equals.rb +94 -0
- data/lib/rubocop/cop/rails/where_exists.rb +8 -13
- data/lib/rubocop/cop/rails/where_not.rb +5 -16
- data/lib/rubocop/cop/rails_cops.rb +4 -0
- data/lib/rubocop/rails/schema_loader.rb +4 -4
- data/lib/rubocop/rails/schema_loader/schema.rb +1 -1
- data/lib/rubocop/rails/version.rb +5 -1
- metadata +19 -9
@@ -43,12 +43,15 @@ module RuboCop
|
|
43
43
|
#
|
44
44
|
# # good
|
45
45
|
# something if foo.present?
|
46
|
-
class Present <
|
46
|
+
class Present < Base
|
47
|
+
extend AutoCorrector
|
48
|
+
|
47
49
|
MSG_NOT_BLANK = 'Use `%<prefer>s` instead of `%<current>s`.'
|
48
50
|
MSG_EXISTS_AND_NOT_EMPTY = 'Use `%<prefer>s` instead of ' \
|
49
51
|
'`%<current>s`.'
|
50
52
|
MSG_UNLESS_BLANK = 'Use `if %<prefer>s` instead of ' \
|
51
53
|
'`%<current>s`.'
|
54
|
+
RESTRICT_ON_SEND = %i[!].freeze
|
52
55
|
|
53
56
|
def_node_matcher :exists_and_not_empty?, <<~PATTERN
|
54
57
|
(and
|
@@ -74,10 +77,11 @@ module RuboCop
|
|
74
77
|
return unless cop_config['NotBlank']
|
75
78
|
|
76
79
|
not_blank?(node) do |receiver|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
80
|
+
message = format(MSG_NOT_BLANK, prefer: replacement(receiver), current: node.source)
|
81
|
+
|
82
|
+
add_offense(node, message: message) do |corrector|
|
83
|
+
autocorrect(corrector, node)
|
84
|
+
end
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
@@ -87,10 +91,11 @@ module RuboCop
|
|
87
91
|
exists_and_not_empty?(node) do |var1, var2|
|
88
92
|
return unless var1 == var2
|
89
93
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
+
message = format(MSG_EXISTS_AND_NOT_EMPTY, prefer: replacement(var1), current: node.source)
|
95
|
+
|
96
|
+
add_offense(node, message: message) do |corrector|
|
97
|
+
autocorrect(corrector, node)
|
98
|
+
end
|
94
99
|
end
|
95
100
|
end
|
96
101
|
|
@@ -100,7 +105,9 @@ module RuboCop
|
|
100
105
|
exists_and_not_empty?(node) do |var1, var2|
|
101
106
|
return unless var1 == var2
|
102
107
|
|
103
|
-
add_offense(node, message: MSG_EXISTS_AND_NOT_EMPTY)
|
108
|
+
add_offense(node, message: MSG_EXISTS_AND_NOT_EMPTY) do |corrector|
|
109
|
+
autocorrect(corrector, node)
|
110
|
+
end
|
104
111
|
end
|
105
112
|
end
|
106
113
|
|
@@ -113,25 +120,24 @@ module RuboCop
|
|
113
120
|
range = unless_condition(node, method_call)
|
114
121
|
msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver),
|
115
122
|
current: range.source)
|
116
|
-
add_offense(
|
123
|
+
add_offense(range, message: msg) do |corrector|
|
124
|
+
autocorrect(corrector, node)
|
125
|
+
end
|
117
126
|
end
|
118
127
|
end
|
119
128
|
|
120
|
-
def autocorrect(node)
|
121
|
-
|
122
|
-
method_call, variable1 = unless_blank?(node)
|
123
|
-
|
124
|
-
if method_call
|
125
|
-
corrector.replace(node.loc.keyword, 'if')
|
126
|
-
range = method_call.loc.expression
|
127
|
-
else
|
128
|
-
variable1, _variable2 =
|
129
|
-
exists_and_not_empty?(node) || not_blank?(node)
|
130
|
-
range = node.loc.expression
|
131
|
-
end
|
129
|
+
def autocorrect(corrector, node)
|
130
|
+
method_call, variable1 = unless_blank?(node)
|
132
131
|
|
133
|
-
|
132
|
+
if method_call
|
133
|
+
corrector.replace(node.loc.keyword, 'if')
|
134
|
+
range = method_call.loc.expression
|
135
|
+
else
|
136
|
+
variable1, _variable2 = exists_and_not_empty?(node) || not_blank?(node)
|
137
|
+
range = node.loc.expression
|
134
138
|
end
|
139
|
+
|
140
|
+
corrector.replace(range, replacement(variable1))
|
135
141
|
end
|
136
142
|
|
137
143
|
private
|
@@ -25,7 +25,9 @@ module RuboCop
|
|
25
25
|
# do_something
|
26
26
|
# end
|
27
27
|
#
|
28
|
-
class RakeEnvironment <
|
28
|
+
class RakeEnvironment < Base
|
29
|
+
extend AutoCorrector
|
30
|
+
|
29
31
|
MSG = 'Include `:environment` task as a dependency for all Rake tasks.'
|
30
32
|
|
31
33
|
def_node_matcher :task_definition?, <<~PATTERN
|
@@ -37,16 +39,12 @@ module RuboCop
|
|
37
39
|
return if task_name(task_method) == :default
|
38
40
|
return if with_dependencies?(task_method)
|
39
41
|
|
40
|
-
add_offense(task_method)
|
41
|
-
|
42
|
-
|
42
|
+
add_offense(task_method) do |corrector|
|
43
|
+
task_name = task_method.arguments[0]
|
44
|
+
task_dependency = correct_task_dependency(task_name)
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
task_name = node.arguments[0]
|
47
|
-
task_dependency = correct_task_dependency(task_name)
|
48
|
-
|
49
|
-
corrector.replace(task_name.loc.expression, task_dependency)
|
46
|
+
corrector.replace(task_name.loc.expression, task_dependency)
|
47
|
+
end
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
@@ -23,8 +23,11 @@ module RuboCop
|
|
23
23
|
# # good
|
24
24
|
# x = self[:attr]
|
25
25
|
# self[:attr] = val
|
26
|
-
class ReadWriteAttribute <
|
26
|
+
class ReadWriteAttribute < Base
|
27
|
+
extend AutoCorrector
|
28
|
+
|
27
29
|
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
|
30
|
+
RESTRICT_ON_SEND = %i[read_attribute write_attribute].freeze
|
28
31
|
|
29
32
|
def_node_matcher :read_write_attribute?, <<~PATTERN
|
30
33
|
{
|
@@ -36,18 +39,16 @@ module RuboCop
|
|
36
39
|
def on_send(node)
|
37
40
|
return unless read_write_attribute?(node)
|
38
41
|
|
39
|
-
add_offense(node,
|
40
|
-
|
42
|
+
add_offense(node.loc.selector, message: message(node)) do |corrector|
|
43
|
+
case node.method_name
|
44
|
+
when :read_attribute
|
45
|
+
replacement = read_attribute_replacement(node)
|
46
|
+
when :write_attribute
|
47
|
+
replacement = write_attribute_replacement(node)
|
48
|
+
end
|
41
49
|
|
42
|
-
|
43
|
-
case node.method_name
|
44
|
-
when :read_attribute
|
45
|
-
replacement = read_attribute_replacement(node)
|
46
|
-
when :write_attribute
|
47
|
-
replacement = write_attribute_replacement(node)
|
50
|
+
corrector.replace(node.source_range, replacement)
|
48
51
|
end
|
49
|
-
|
50
|
-
->(corrector) { corrector.replace(node.source_range, replacement) }
|
51
52
|
end
|
52
53
|
|
53
54
|
private
|
@@ -26,8 +26,9 @@ module RuboCop
|
|
26
26
|
# # Here, `nil` is valid but `''` is not
|
27
27
|
# validates :x, length: { is: 5 }, allow_nil: true, allow_blank: false
|
28
28
|
#
|
29
|
-
class RedundantAllowNil <
|
29
|
+
class RedundantAllowNil < Base
|
30
30
|
include RangeHelp
|
31
|
+
extend AutoCorrector
|
31
32
|
|
32
33
|
MSG_SAME =
|
33
34
|
'`allow_nil` is redundant when `allow_blank` has the same value.'
|
@@ -35,59 +36,56 @@ module RuboCop
|
|
35
36
|
MSG_ALLOW_NIL_FALSE =
|
36
37
|
'`allow_nil: false` is redundant when `allow_blank` is true.'
|
37
38
|
|
38
|
-
|
39
|
-
return unless node.method?(:validates)
|
39
|
+
RESTRICT_ON_SEND = %i[validates].freeze
|
40
40
|
|
41
|
+
def on_send(node)
|
41
42
|
allow_nil, allow_blank = find_allow_nil_and_allow_blank(node)
|
42
43
|
return unless allow_nil && allow_blank
|
43
44
|
|
44
45
|
allow_nil_val = allow_nil.children.last
|
45
46
|
allow_blank_val = allow_blank.children.last
|
46
47
|
|
47
|
-
|
48
|
+
if allow_nil_val.type == allow_blank_val.type
|
49
|
+
register_offense(allow_nil, MSG_SAME)
|
50
|
+
elsif allow_nil_val.false_type? && allow_blank_val.true_type?
|
51
|
+
register_offense(allow_nil, MSG_ALLOW_NIL_FALSE)
|
52
|
+
end
|
48
53
|
end
|
49
54
|
|
50
|
-
|
51
|
-
|
52
|
-
|
55
|
+
private
|
56
|
+
|
57
|
+
def register_offense(allow_nil, message)
|
58
|
+
add_offense(allow_nil, message: message) do |corrector|
|
59
|
+
prv_sib = previous_sibling(allow_nil)
|
60
|
+
nxt_sib = next_sibling(allow_nil)
|
53
61
|
|
54
|
-
lambda do |corrector|
|
55
62
|
if nxt_sib
|
56
|
-
corrector.remove(range_between(node_beg(
|
63
|
+
corrector.remove(range_between(node_beg(allow_nil), node_beg(nxt_sib)))
|
57
64
|
elsif prv_sib
|
58
|
-
corrector.remove(range_between(node_end(prv_sib), node_end(
|
65
|
+
corrector.remove(range_between(node_end(prv_sib), node_end(allow_nil)))
|
59
66
|
else
|
60
|
-
corrector.remove(
|
67
|
+
corrector.remove(allow_nil.loc.expression)
|
61
68
|
end
|
62
69
|
end
|
63
70
|
end
|
64
71
|
|
65
|
-
private
|
66
|
-
|
67
|
-
def offense(allow_nil_val, allow_blank_val, allow_nil)
|
68
|
-
if allow_nil_val.type == allow_blank_val.type
|
69
|
-
add_offense(allow_nil, message: MSG_SAME)
|
70
|
-
elsif allow_nil_val.false_type? && allow_blank_val.true_type?
|
71
|
-
add_offense(allow_nil, message: MSG_ALLOW_NIL_FALSE)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
72
|
def find_allow_nil_and_allow_blank(node)
|
76
|
-
allow_nil = nil
|
77
|
-
allow_blank = nil
|
73
|
+
allow_nil, allow_blank = nil
|
78
74
|
|
79
|
-
node.
|
80
|
-
|
75
|
+
node.each_child_node do |child_node|
|
76
|
+
if child_node.pair_type?
|
77
|
+
key = child_node.children.first.source
|
81
78
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
79
|
+
allow_nil = child_node if key == 'allow_nil'
|
80
|
+
allow_blank = child_node if key == 'allow_blank'
|
81
|
+
end
|
82
|
+
return [allow_nil, allow_blank] if allow_nil && allow_blank
|
86
83
|
|
87
|
-
|
84
|
+
found_in_children_nodes = find_allow_nil_and_allow_blank(child_node)
|
85
|
+
return found_in_children_nodes if found_in_children_nodes
|
88
86
|
end
|
89
87
|
|
90
|
-
|
88
|
+
nil
|
91
89
|
end
|
92
90
|
|
93
91
|
def previous_sibling(node)
|
@@ -24,10 +24,12 @@ module RuboCop
|
|
24
24
|
# class Comment
|
25
25
|
# belongs_to :author, foreign_key: 'user_id'
|
26
26
|
# end
|
27
|
-
class RedundantForeignKey <
|
27
|
+
class RedundantForeignKey < Base
|
28
28
|
include RangeHelp
|
29
|
+
extend AutoCorrector
|
29
30
|
|
30
31
|
MSG = 'Specifying the default value for `foreign_key` is redundant.'
|
32
|
+
RESTRICT_ON_SEND = %i[belongs_to has_one has_many has_and_belongs_to_many].freeze
|
31
33
|
|
32
34
|
def_node_matcher :association_with_foreign_key, <<~PATTERN
|
33
35
|
(send nil? ${:belongs_to :has_one :has_many :has_and_belongs_to_many} ({sym str} $_)
|
@@ -38,18 +40,13 @@ module RuboCop
|
|
38
40
|
def on_send(node)
|
39
41
|
association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key|
|
40
42
|
if redundant?(node, type, name, options, foreign_key)
|
41
|
-
add_offense(
|
42
|
-
|
43
|
-
|
44
|
-
end
|
43
|
+
add_offense(foreign_key_pair.loc.expression) do |corrector|
|
44
|
+
range = range_with_surrounding_space(range: foreign_key_pair.source_range, side: :left)
|
45
|
+
range = range_with_surrounding_comma(range, :left)
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
range = range_with_surrounding_comma(range, :left)
|
50
|
-
|
51
|
-
lambda do |corrector|
|
52
|
-
corrector.remove(range)
|
47
|
+
corrector.remove(range)
|
48
|
+
end
|
49
|
+
end
|
53
50
|
end
|
54
51
|
end
|
55
52
|
|
@@ -54,8 +54,9 @@ module RuboCop
|
|
54
54
|
# merger.invoke(another_receiver)
|
55
55
|
# end
|
56
56
|
# end
|
57
|
-
class RedundantReceiverInWithOptions <
|
57
|
+
class RedundantReceiverInWithOptions < Base
|
58
58
|
include RangeHelp
|
59
|
+
extend AutoCorrector
|
59
60
|
|
60
61
|
MSG = 'Redundant receiver in `with_options`.'
|
61
62
|
|
@@ -86,22 +87,22 @@ module RuboCop
|
|
86
87
|
if send_nodes.all? { |n| same_value?(arg, n.receiver) }
|
87
88
|
send_nodes.each do |send_node|
|
88
89
|
receiver = send_node.receiver
|
89
|
-
add_offense(
|
90
|
+
add_offense(receiver.source_range) do |corrector|
|
91
|
+
autocorrect(corrector, send_node)
|
92
|
+
end
|
90
93
|
end
|
91
94
|
end
|
92
95
|
end
|
93
96
|
end
|
94
97
|
|
95
|
-
def autocorrect(node)
|
96
|
-
lambda do |corrector|
|
97
|
-
corrector.remove(node.receiver.source_range)
|
98
|
-
corrector.remove(node.loc.dot)
|
99
|
-
corrector.remove(block_argument_range(node))
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
98
|
private
|
104
99
|
|
100
|
+
def autocorrect(corrector, node)
|
101
|
+
corrector.remove(node.receiver.source_range)
|
102
|
+
corrector.remove(node.loc.dot)
|
103
|
+
corrector.remove(block_argument_range(node))
|
104
|
+
end
|
105
|
+
|
105
106
|
def block_argument_range(node)
|
106
107
|
block_node = node.each_ancestor(:block).first
|
107
108
|
block_argument = block_node.children[1].source_range
|
@@ -13,8 +13,9 @@ module RuboCop
|
|
13
13
|
#
|
14
14
|
# # good
|
15
15
|
# has_many :accounts, class_name: 'Account'
|
16
|
-
class ReflectionClassName <
|
16
|
+
class ReflectionClassName < Base
|
17
17
|
MSG = 'Use a string value for `class_name`.'
|
18
|
+
RESTRICT_ON_SEND = %i[has_many has_one belongs_to].freeze
|
18
19
|
|
19
20
|
def_node_matcher :association_with_reflection, <<~PATTERN
|
20
21
|
(send nil? {:has_many :has_one :belongs_to} _ _ ?
|
@@ -28,7 +29,7 @@ module RuboCop
|
|
28
29
|
|
29
30
|
def on_send(node)
|
30
31
|
association_with_reflection(node) do |reflection_class_name|
|
31
|
-
add_offense(
|
32
|
+
add_offense(reflection_class_name.loc.expression)
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
@@ -28,8 +28,9 @@ module RuboCop
|
|
28
28
|
# refute_empty [1, 2, 3]
|
29
29
|
# refute_equal true, false
|
30
30
|
#
|
31
|
-
class RefuteMethods <
|
31
|
+
class RefuteMethods < Base
|
32
32
|
include ConfigurableEnforcedStyle
|
33
|
+
extend AutoCorrector
|
33
34
|
|
34
35
|
MSG = 'Prefer `%<good_method>s` over `%<bad_method>s`.'
|
35
36
|
|
@@ -53,21 +54,19 @@ module RuboCop
|
|
53
54
|
REFUTE_METHODS = CORRECTIONS.keys.freeze
|
54
55
|
ASSERT_NOT_METHODS = CORRECTIONS.values.freeze
|
55
56
|
|
57
|
+
RESTRICT_ON_SEND = REFUTE_METHODS + ASSERT_NOT_METHODS
|
58
|
+
|
56
59
|
def_node_matcher :offensive?, '(send nil? #bad_method? ...)'
|
57
60
|
|
58
61
|
def on_send(node)
|
59
62
|
return unless offensive?(node)
|
60
63
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
def autocorrect(node)
|
66
|
-
bad_method = node.method_name
|
67
|
-
good_method = convert_good_method(bad_method)
|
64
|
+
method_name = node.method_name
|
65
|
+
message = offense_message(method_name)
|
66
|
+
range = node.loc.selector
|
68
67
|
|
69
|
-
|
70
|
-
corrector.replace(
|
68
|
+
add_offense(range, message: message) do |corrector|
|
69
|
+
corrector.replace(range, convert_good_method(method_name))
|
71
70
|
end
|
72
71
|
end
|
73
72
|
|
@@ -27,15 +27,18 @@ module RuboCop
|
|
27
27
|
# 1.week.since
|
28
28
|
# end
|
29
29
|
# end
|
30
|
-
class RelativeDateConstant <
|
30
|
+
class RelativeDateConstant < Base
|
31
31
|
include RangeHelp
|
32
|
+
extend AutoCorrector
|
32
33
|
|
33
34
|
MSG = 'Do not assign %<method_name>s to constants as it ' \
|
34
35
|
'will be evaluated only once.'
|
35
36
|
|
36
37
|
def on_casgn(node)
|
37
38
|
relative_date_assignment?(node) do |method_name|
|
38
|
-
add_offense(node, message:
|
39
|
+
add_offense(node, message: message(method_name)) do |corrector|
|
40
|
+
autocorrect(corrector, node)
|
41
|
+
end
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
@@ -48,9 +51,9 @@ module RuboCop
|
|
48
51
|
next unless name.casgn_type?
|
49
52
|
|
50
53
|
relative_date?(value) do |method_name|
|
51
|
-
add_offense(
|
52
|
-
|
53
|
-
|
54
|
+
add_offense(offense_range(name, value), message: message(method_name)) do |corrector|
|
55
|
+
autocorrect(corrector, node)
|
56
|
+
end
|
54
57
|
end
|
55
58
|
end
|
56
59
|
end
|
@@ -61,7 +64,9 @@ module RuboCop
|
|
61
64
|
end
|
62
65
|
end
|
63
66
|
|
64
|
-
|
67
|
+
private
|
68
|
+
|
69
|
+
def autocorrect(corrector, node)
|
65
70
|
return unless node.casgn_type?
|
66
71
|
|
67
72
|
scope, const_name, value = *node
|
@@ -71,10 +76,13 @@ module RuboCop
|
|
71
76
|
new_code = ["def self.#{const_name.downcase}",
|
72
77
|
"#{indent}#{value.source}",
|
73
78
|
'end'].join("\n#{indent}")
|
74
|
-
|
79
|
+
|
80
|
+
corrector.replace(node.source_range, new_code)
|
75
81
|
end
|
76
82
|
|
77
|
-
|
83
|
+
def message(method_name)
|
84
|
+
format(MSG, method_name: method_name)
|
85
|
+
end
|
78
86
|
|
79
87
|
def offense_range(name, value)
|
80
88
|
range_between(name.loc.expression.begin_pos, value.loc.expression.end_pos)
|