rubocop-rails 2.15.2 → 2.20.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +23 -2
- data/config/default.yml +181 -13
- data/config/obsoletion.yml +10 -0
- data/lib/rubocop/cop/mixin/active_record_helper.rb +3 -6
- data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +1 -3
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +1 -1
- data/lib/rubocop/cop/mixin/index_method.rb +7 -17
- data/lib/rubocop/cop/mixin/migrations_helper.rb +1 -1
- data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +112 -0
- data/lib/rubocop/cop/rails/action_controller_test_case.rb +2 -2
- data/lib/rubocop/cop/rails/action_filter.rb +1 -1
- data/lib/rubocop/cop/rails/action_order.rb +116 -0
- data/lib/rubocop/cop/rails/active_record_aliases.rb +3 -4
- data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +6 -3
- 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 +2 -5
- data/lib/rubocop/cop/rails/application_controller.rb +1 -1
- data/lib/rubocop/cop/rails/application_job.rb +2 -2
- data/lib/rubocop/cop/rails/application_mailer.rb +1 -1
- data/lib/rubocop/cop/rails/application_record.rb +1 -1
- data/lib/rubocop/cop/rails/arel_star.rb +1 -1
- data/lib/rubocop/cop/rails/assert_not.rb +1 -2
- data/lib/rubocop/cop/rails/belongs_to.rb +1 -4
- data/lib/rubocop/cop/rails/blank.rb +6 -7
- data/lib/rubocop/cop/rails/bulk_change_table.rb +7 -24
- data/lib/rubocop/cop/rails/compact_blank.rb +5 -1
- data/lib/rubocop/cop/rails/content_tag.rb +5 -6
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -3
- data/lib/rubocop/cop/rails/date.rb +15 -11
- data/lib/rubocop/cop/rails/delegate.rb +19 -8
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
- data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +18 -14
- data/lib/rubocop/cop/rails/dot_separated_keys.rb +2 -2
- data/lib/rubocop/cop/rails/duration_arithmetic.rb +3 -3
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +25 -13
- data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +5 -1
- data/lib/rubocop/cop/rails/enum_hash.rb +1 -1
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -5
- data/lib/rubocop/cop/rails/environment_comparison.rb +2 -3
- data/lib/rubocop/cop/rails/file_path.rb +154 -27
- data/lib/rubocop/cop/rails/find_by_id.rb +2 -2
- data/lib/rubocop/cop/rails/find_each.rb +15 -5
- data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +4 -6
- data/lib/rubocop/cop/rails/helper_instance_variable.rb +1 -1
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +22 -11
- data/lib/rubocop/cop/rails/http_status.rb +6 -11
- data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +2 -0
- data/lib/rubocop/cop/rails/i18n_locale_texts.rb +7 -3
- data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +23 -12
- data/lib/rubocop/cop/rails/index_by.rb +1 -1
- data/lib/rubocop/cop/rails/index_with.rb +1 -1
- data/lib/rubocop/cop/rails/inverse_of.rb +3 -9
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +21 -15
- data/lib/rubocop/cop/rails/link_to_blank.rb +1 -4
- data/lib/rubocop/cop/rails/mailer_name.rb +4 -4
- data/lib/rubocop/cop/rails/migration_class_name.rb +1 -1
- data/lib/rubocop/cop/rails/negate_include.rb +1 -1
- data/lib/rubocop/cop/rails/not_null_column.rb +9 -6
- data/lib/rubocop/cop/rails/output.rb +6 -8
- data/lib/rubocop/cop/rails/output_safety.rb +5 -1
- data/lib/rubocop/cop/rails/pluck.rb +44 -12
- data/lib/rubocop/cop/rails/pluck_id.rb +1 -1
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +1 -2
- data/lib/rubocop/cop/rails/presence.rb +21 -12
- data/lib/rubocop/cop/rails/present.rb +8 -11
- data/lib/rubocop/cop/rails/rake_environment.rb +2 -2
- data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +5 -7
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +2 -2
- data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +3 -3
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +30 -26
- data/lib/rubocop/cop/rails/reflection_class_name.rb +34 -1
- data/lib/rubocop/cop/rails/refute_methods.rb +1 -6
- data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -7
- data/lib/rubocop/cop/rails/request_referer.rb +1 -2
- data/lib/rubocop/cop/rails/require_dependency.rb +1 -1
- data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
- data/lib/rubocop/cop/rails/reversible_migration.rb +14 -62
- data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +1 -2
- data/lib/rubocop/cop/rails/root_join_chain.rb +1 -1
- data/lib/rubocop/cop/rails/root_pathname_methods.rb +238 -0
- data/lib/rubocop/cop/rails/safe_navigation.rb +1 -1
- 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 +2 -5
- data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -3
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +9 -7
- data/lib/rubocop/cop/rails/three_state_boolean_column.rb +71 -0
- data/lib/rubocop/cop/rails/time_zone.rb +27 -25
- data/lib/rubocop/cop/rails/time_zone_assignment.rb +1 -1
- data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
- data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +8 -3
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -6
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +22 -19
- data/lib/rubocop/cop/rails/unknown_env.rb +2 -4
- data/lib/rubocop/cop/rails/unused_ignored_columns.rb +6 -1
- data/lib/rubocop/cop/rails/validation.rb +4 -12
- data/lib/rubocop/cop/rails/where_equals.rb +1 -1
- data/lib/rubocop/cop/rails/where_exists.rb +1 -1
- data/lib/rubocop/cop/rails/where_missing.rb +118 -0
- data/lib/rubocop/cop/rails/where_not.rb +1 -1
- data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
- data/lib/rubocop/cop/rails_cops.rb +12 -0
- data/lib/rubocop/rails/schema_loader/schema.rb +4 -4
- data/lib/rubocop/rails/version.rb +1 -1
- data/lib/rubocop/rails.rb +1 -1
- data/lib/rubocop-rails.rb +11 -0
- metadata +19 -9
- data/bin/console +0 -11
- data/bin/setup +0 -7
@@ -35,19 +35,16 @@ module RuboCop
|
|
35
35
|
# skip_before_action :login_required,
|
36
36
|
# if: -> { trusted_origin? && action_name != "admin" }
|
37
37
|
# end
|
38
|
-
#
|
39
|
-
# @see https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options
|
40
38
|
class IgnoredSkipActionFilterOption < Base
|
39
|
+
extend AutoCorrector
|
40
|
+
|
41
|
+
include RangeHelp
|
42
|
+
|
41
43
|
MSG = <<~MSG.chomp.freeze
|
42
44
|
`%<ignore>s` option will be ignored when `%<prefer>s` and `%<ignore>s` are used together.
|
43
45
|
MSG
|
44
46
|
|
45
|
-
RESTRICT_ON_SEND = %i[
|
46
|
-
skip_after_action
|
47
|
-
skip_around_action
|
48
|
-
skip_before_action
|
49
|
-
skip_action_callback
|
50
|
-
].freeze
|
47
|
+
RESTRICT_ON_SEND = %i[skip_after_action skip_around_action skip_before_action skip_action_callback].freeze
|
51
48
|
|
52
49
|
FILTERS = RESTRICT_ON_SEND.map { |method_name| ":#{method_name}" }
|
53
50
|
|
@@ -67,11 +64,13 @@ module RuboCop
|
|
67
64
|
options = options_hash(options)
|
68
65
|
|
69
66
|
if if_and_only?(options)
|
70
|
-
add_offense(options[:if],
|
71
|
-
|
67
|
+
add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if)) do |corrector|
|
68
|
+
remove_node_with_left_space_and_comma(corrector, options[:if])
|
69
|
+
end
|
72
70
|
elsif if_and_except?(options)
|
73
|
-
add_offense(options[:except],
|
74
|
-
|
71
|
+
add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except)) do |corrector|
|
72
|
+
remove_node_with_left_space_and_comma(corrector, options[:except])
|
73
|
+
end
|
75
74
|
end
|
76
75
|
end
|
77
76
|
|
@@ -90,6 +89,18 @@ module RuboCop
|
|
90
89
|
def if_and_except?(options)
|
91
90
|
options.key?(:if) && options.key?(:except)
|
92
91
|
end
|
92
|
+
|
93
|
+
def remove_node_with_left_space_and_comma(corrector, node)
|
94
|
+
corrector.remove(
|
95
|
+
range_with_surrounding_comma(
|
96
|
+
range_with_surrounding_space(
|
97
|
+
node.source_range,
|
98
|
+
side: :left
|
99
|
+
),
|
100
|
+
:left
|
101
|
+
)
|
102
|
+
)
|
103
|
+
end
|
93
104
|
end
|
94
105
|
end
|
95
106
|
end
|
@@ -137,9 +137,6 @@ module RuboCop
|
|
137
137
|
# class Blog < ApplicationRecord
|
138
138
|
# has_many :posts, -> { order(published_at: :desc) }
|
139
139
|
# end
|
140
|
-
#
|
141
|
-
# @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations
|
142
|
-
# @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
|
143
140
|
class InverseOf < Base
|
144
141
|
SPECIFY_MSG = 'Specify an `:inverse_of` option.'
|
145
142
|
NIL_MSG = 'You specified `inverse_of: nil`, you probably meant to use `inverse_of: false`.'
|
@@ -192,8 +189,7 @@ module RuboCop
|
|
192
189
|
end
|
193
190
|
return if options_ignoring_inverse_of?(options)
|
194
191
|
|
195
|
-
return unless scope?(arguments) ||
|
196
|
-
options_requiring_inverse_of?(options)
|
192
|
+
return unless scope?(arguments) || options_requiring_inverse_of?(options)
|
197
193
|
|
198
194
|
return if options_contain_inverse_of?(options)
|
199
195
|
|
@@ -206,8 +202,7 @@ module RuboCop
|
|
206
202
|
|
207
203
|
def options_requiring_inverse_of?(options)
|
208
204
|
required = options.any? do |opt|
|
209
|
-
conditions_option?(opt) ||
|
210
|
-
foreign_key_option?(opt)
|
205
|
+
conditions_option?(opt) || foreign_key_option?(opt)
|
211
206
|
end
|
212
207
|
|
213
208
|
return required if target_rails_version >= 5.2
|
@@ -227,8 +222,7 @@ module RuboCop
|
|
227
222
|
|
228
223
|
def with_options_arguments(recv, node)
|
229
224
|
blocks = node.each_ancestor(:block).select do |block|
|
230
|
-
block.send_node.command?(:with_options) &&
|
231
|
-
same_context_in_with_options?(block.arguments.first, recv)
|
225
|
+
block.send_node.command?(:with_options) && same_context_in_with_options?(block.arguments.first, recv)
|
232
226
|
end
|
233
227
|
blocks.flat_map { |n| n.send_node.arguments }
|
234
228
|
end
|
@@ -144,19 +144,29 @@ module RuboCop
|
|
144
144
|
end
|
145
145
|
|
146
146
|
def aliased_action_methods(node, defined_methods)
|
147
|
-
alias_methods = node
|
148
|
-
|
149
|
-
hash_of_alias_methods = alias_methods.each_with_object({}) do |alias_method, result|
|
150
|
-
result[alias_method.last_argument.value] = alias_method.first_argument.value
|
151
|
-
end
|
152
|
-
|
147
|
+
alias_methods = alias_methods(node)
|
153
148
|
defined_methods.each_with_object([]) do |defined_method, aliased_method|
|
154
|
-
if (new_method_name =
|
149
|
+
if (new_method_name = alias_methods[defined_method])
|
155
150
|
aliased_method << new_method_name
|
156
151
|
end
|
157
152
|
end
|
158
153
|
end
|
159
154
|
|
155
|
+
def alias_methods(node)
|
156
|
+
result = {}
|
157
|
+
node.each_child_node(:send, :alias) do |child_node|
|
158
|
+
case child_node.type
|
159
|
+
when :send
|
160
|
+
if child_node.method?(:alias_method)
|
161
|
+
result[child_node.last_argument.value] = child_node.first_argument.value
|
162
|
+
end
|
163
|
+
when :alias
|
164
|
+
result[child_node.old_identifier.value] = child_node.new_identifier.value
|
165
|
+
end
|
166
|
+
end
|
167
|
+
result
|
168
|
+
end
|
169
|
+
|
160
170
|
# @param node [RuboCop::AST::Node]
|
161
171
|
# @return [Array<Symbol>]
|
162
172
|
def array_values(node) # rubocop:disable Metrics/MethodLength
|
@@ -166,14 +176,14 @@ module RuboCop
|
|
166
176
|
when :sym
|
167
177
|
[node.value]
|
168
178
|
when :array
|
169
|
-
node.values.
|
179
|
+
node.values.filter_map do |v|
|
170
180
|
case v.type
|
171
181
|
when :str
|
172
182
|
v.str_content.to_sym
|
173
183
|
when :sym
|
174
184
|
v.value
|
175
185
|
end
|
176
|
-
end
|
186
|
+
end
|
177
187
|
else
|
178
188
|
[]
|
179
189
|
end
|
@@ -184,13 +194,9 @@ module RuboCop
|
|
184
194
|
# @return [String]
|
185
195
|
def message(methods, parent)
|
186
196
|
if methods.size == 1
|
187
|
-
format(MSG,
|
188
|
-
action: "`#{methods[0]}` is",
|
189
|
-
type: parent.type)
|
197
|
+
format(MSG, action: "`#{methods[0]}` is", type: parent.type)
|
190
198
|
else
|
191
|
-
format(MSG,
|
192
|
-
action: "`#{methods.join('`, `')}` are",
|
193
|
-
type: parent.type)
|
199
|
+
format(MSG, action: "`#{methods.join('`, `')}` are", type: parent.type)
|
194
200
|
end
|
195
201
|
end
|
196
202
|
end
|
@@ -68,10 +68,7 @@ module RuboCop
|
|
68
68
|
|
69
69
|
def append_to_rel(rel_node, corrector)
|
70
70
|
existing_rel = rel_node.children.last.value
|
71
|
-
str_range = rel_node.children.last.
|
72
|
-
begin_pos: 1,
|
73
|
-
end_pos: -1
|
74
|
-
)
|
71
|
+
str_range = rel_node.children.last.source_range.adjust(begin_pos: 1, end_pos: -1)
|
75
72
|
corrector.replace(str_range, "#{existing_rel} noopener")
|
76
73
|
end
|
77
74
|
|
@@ -34,8 +34,8 @@ module RuboCop
|
|
34
34
|
|
35
35
|
def_node_matcher :mailer_base_class?, <<~PATTERN
|
36
36
|
{
|
37
|
-
(const (const nil? :ActionMailer) :Base)
|
38
|
-
(const nil? :ApplicationMailer)
|
37
|
+
(const (const {nil? cbase} :ActionMailer) :Base)
|
38
|
+
(const {nil? cbase} :ApplicationMailer)
|
39
39
|
}
|
40
40
|
PATTERN
|
41
41
|
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
PATTERN
|
45
45
|
|
46
46
|
def_node_matcher :class_new_definition?, <<~PATTERN
|
47
|
-
(send (const nil? :Class) :new #mailer_base_class?)
|
47
|
+
(send (const {nil? cbase} :Class) :new #mailer_base_class?)
|
48
48
|
PATTERN
|
49
49
|
|
50
50
|
def on_class(node)
|
@@ -77,7 +77,7 @@ module RuboCop
|
|
77
77
|
corrector.replace(node.loc.name, "#{name}Mailer")
|
78
78
|
else
|
79
79
|
name = node.children.last
|
80
|
-
corrector.replace(node
|
80
|
+
corrector.replace(node, "#{name}Mailer")
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -29,7 +29,7 @@ module RuboCop
|
|
29
29
|
|
30
30
|
basename = basename_without_timestamp_and_suffix(processed_source.file_path)
|
31
31
|
|
32
|
-
class_identifier = node.identifier
|
32
|
+
class_identifier = node.identifier.location.name
|
33
33
|
camelized_basename = camelize(basename)
|
34
34
|
return if class_identifier.source.casecmp(camelized_basename).zero?
|
35
35
|
|
@@ -21,7 +21,7 @@ module RuboCop
|
|
21
21
|
RESTRICT_ON_SEND = %i[add_column add_reference].freeze
|
22
22
|
|
23
23
|
def_node_matcher :add_not_null_column?, <<~PATTERN
|
24
|
-
(send nil? :add_column _ _ _ (hash $...))
|
24
|
+
(send nil? :add_column _ _ $_ (hash $...))
|
25
25
|
PATTERN
|
26
26
|
|
27
27
|
def_node_matcher :add_not_null_reference?, <<~PATTERN
|
@@ -44,17 +44,20 @@ module RuboCop
|
|
44
44
|
private
|
45
45
|
|
46
46
|
def check_add_column(node)
|
47
|
-
|
48
|
-
|
47
|
+
add_not_null_column?(node) do |type, pairs|
|
48
|
+
return if type.respond_to?(:value) && (type.value == :virtual || type.value == 'virtual')
|
49
|
+
|
50
|
+
check_pairs(pairs)
|
51
|
+
end
|
49
52
|
end
|
50
53
|
|
51
54
|
def check_add_reference(node)
|
52
|
-
|
53
|
-
|
55
|
+
add_not_null_reference?(node) do |pairs|
|
56
|
+
check_pairs(pairs)
|
57
|
+
end
|
54
58
|
end
|
55
59
|
|
56
60
|
def check_pairs(pairs)
|
57
|
-
return unless pairs
|
58
61
|
return if pairs.any? { |pair| default_option?(pair) }
|
59
62
|
|
60
63
|
null_false = pairs.find { |pair| null_false?(pair) }
|
@@ -21,11 +21,8 @@ module RuboCop
|
|
21
21
|
include RangeHelp
|
22
22
|
extend AutoCorrector
|
23
23
|
|
24
|
-
MSG =
|
25
|
-
|
26
|
-
RESTRICT_ON_SEND = %i[
|
27
|
-
ap p pp pretty_print print puts binwrite syswrite write write_nonblock
|
28
|
-
].freeze
|
24
|
+
MSG = "Do not write to stdout. Use Rails's logger if you want to log."
|
25
|
+
RESTRICT_ON_SEND = %i[ap p pp pretty_print print puts binwrite syswrite write write_nonblock].freeze
|
29
26
|
|
30
27
|
def_node_matcher :output?, <<~PATTERN
|
31
28
|
(send nil? {:ap :p :pp :pretty_print :print :puts} ...)
|
@@ -35,14 +32,15 @@ module RuboCop
|
|
35
32
|
(send
|
36
33
|
{
|
37
34
|
(gvar #match_gvar?)
|
38
|
-
|
35
|
+
(const {nil? cbase} {:STDOUT :STDERR})
|
39
36
|
}
|
40
37
|
{:binwrite :syswrite :write :write_nonblock}
|
41
38
|
...)
|
42
39
|
PATTERN
|
43
40
|
|
44
41
|
def on_send(node)
|
45
|
-
return
|
42
|
+
return if node.parent&.call_type?
|
43
|
+
return unless output?(node) || io_output?(node)
|
46
44
|
|
47
45
|
range = offense_range(node)
|
48
46
|
|
@@ -59,7 +57,7 @@ module RuboCop
|
|
59
57
|
|
60
58
|
def offense_range(node)
|
61
59
|
if node.receiver
|
62
|
-
range_between(node.
|
60
|
+
range_between(node.source_range.begin_pos, node.loc.selector.end_pos)
|
63
61
|
else
|
64
62
|
node.loc.selector
|
65
63
|
end
|
@@ -66,8 +66,12 @@ module RuboCop
|
|
66
66
|
MSG = 'Tagging a string as html safe may be a security risk.'
|
67
67
|
RESTRICT_ON_SEND = %i[html_safe raw safe_concat].freeze
|
68
68
|
|
69
|
+
def_node_search :i18n_method?, <<~PATTERN
|
70
|
+
(send {nil? (const {nil? cbase} :I18n)} {:t :translate :l :localize} ...)
|
71
|
+
PATTERN
|
72
|
+
|
69
73
|
def on_send(node)
|
70
|
-
return if non_interpolated_string?(node)
|
74
|
+
return if non_interpolated_string?(node) || i18n_method?(node)
|
71
75
|
|
72
76
|
return unless looks_like_rails_html_safe?(node) ||
|
73
77
|
looks_like_rails_raw?(node) ||
|
@@ -9,6 +9,18 @@ module RuboCop
|
|
9
9
|
# element in an enumerable. When called on an Active Record relation, it
|
10
10
|
# results in a more efficient query that only selects the necessary key.
|
11
11
|
#
|
12
|
+
# @safety
|
13
|
+
# This cop is unsafe because model can use column aliases.
|
14
|
+
#
|
15
|
+
# [source,ruby]
|
16
|
+
# ----
|
17
|
+
# # Original code
|
18
|
+
# User.select('name AS nickname').map { |user| user[:nickname] } # => array of nicknames
|
19
|
+
#
|
20
|
+
# # After autocorrection
|
21
|
+
# User.select('name AS nickname').pluck(:nickname) # => raises ActiveRecord::StatementInvalid
|
22
|
+
# ----
|
23
|
+
#
|
12
24
|
# @example
|
13
25
|
# # bad
|
14
26
|
# Post.published.map { |post| post[:title] }
|
@@ -21,42 +33,62 @@ module RuboCop
|
|
21
33
|
extend AutoCorrector
|
22
34
|
extend TargetRailsVersion
|
23
35
|
|
24
|
-
MSG = 'Prefer
|
36
|
+
MSG = 'Prefer `%<replacement>s` over `%<current>s`.'
|
25
37
|
|
26
38
|
minimum_target_rails_version 5.0
|
27
39
|
|
28
40
|
def_node_matcher :pluck_candidate?, <<~PATTERN
|
29
|
-
({block numblock} (send _ {:map :collect}) $_argument (send
|
41
|
+
({block numblock} (send _ {:map :collect}) $_argument (send lvar :[] $_key))
|
30
42
|
PATTERN
|
31
43
|
|
32
44
|
def on_block(node)
|
33
|
-
pluck_candidate?(node) do |argument,
|
45
|
+
pluck_candidate?(node) do |argument, key|
|
46
|
+
next if key.regexp_type? || !use_one_block_argument?(argument)
|
47
|
+
|
34
48
|
match = if node.block_type?
|
35
|
-
argument.children.first.source
|
49
|
+
block_argument = argument.children.first.source
|
50
|
+
use_block_argument_in_key?(block_argument, key)
|
36
51
|
else # numblock
|
37
|
-
argument == 1 &&
|
52
|
+
argument == 1 && use_block_argument_in_key?('_1', key)
|
38
53
|
end
|
39
54
|
next unless match
|
40
55
|
|
41
|
-
|
42
|
-
|
43
|
-
add_offense(offense_range(node), message: message) do |corrector|
|
44
|
-
corrector.replace(offense_range(node), "pluck(:#{value})")
|
45
|
-
end
|
56
|
+
register_offense(node, key)
|
46
57
|
end
|
47
58
|
end
|
48
59
|
alias on_numblock on_block
|
49
60
|
|
50
61
|
private
|
51
62
|
|
63
|
+
def use_one_block_argument?(argument)
|
64
|
+
return true if argument == 1 # Checks for numbered argument `_1`.
|
65
|
+
|
66
|
+
argument.respond_to?(:one?) && argument.one?
|
67
|
+
end
|
68
|
+
|
69
|
+
def use_block_argument_in_key?(block_argument, key)
|
70
|
+
return false if block_argument == key.source
|
71
|
+
|
72
|
+
key.each_descendant(:lvar).none? { |lvar| block_argument == lvar.source }
|
73
|
+
end
|
74
|
+
|
52
75
|
def offense_range(node)
|
53
76
|
node.send_node.loc.selector.join(node.loc.end)
|
54
77
|
end
|
55
78
|
|
56
|
-
def
|
79
|
+
def register_offense(node, key)
|
80
|
+
replacement = "pluck(#{key.source})"
|
81
|
+
message = message(replacement, node)
|
82
|
+
|
83
|
+
add_offense(offense_range(node), message: message) do |corrector|
|
84
|
+
corrector.replace(offense_range(node), replacement)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def message(replacement, node)
|
57
89
|
current = offense_range(node).source
|
58
90
|
|
59
|
-
format(MSG,
|
91
|
+
format(MSG, replacement: replacement, current: current)
|
60
92
|
end
|
61
93
|
end
|
62
94
|
end
|
@@ -94,8 +94,7 @@ module RuboCop
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def duration_method?(method_name)
|
97
|
-
SINGULAR_DURATION_METHODS.key?(method_name) ||
|
98
|
-
PLURAL_DURATION_METHODS.key?(method_name)
|
97
|
+
SINGULAR_DURATION_METHODS.key?(method_name) || PLURAL_DURATION_METHODS.key?(method_name)
|
99
98
|
end
|
100
99
|
end
|
101
100
|
end
|
@@ -93,7 +93,7 @@ module RuboCop
|
|
93
93
|
|
94
94
|
def register_offense(node, receiver, other)
|
95
95
|
add_offense(node, message: message(node, receiver, other)) do |corrector|
|
96
|
-
corrector.replace(node
|
96
|
+
corrector.replace(node, replacement(receiver, other, node.left_sibling))
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -106,12 +106,20 @@ module RuboCop
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def message(node, receiver, other)
|
109
|
-
|
110
|
-
|
111
|
-
|
109
|
+
prefer = replacement(receiver, other, node.left_sibling).gsub(/^\s*|\n/, '')
|
110
|
+
current = current(node).gsub(/^\s*|\n/, '')
|
111
|
+
format(MSG, prefer: prefer, current: current)
|
112
112
|
end
|
113
113
|
|
114
|
-
def
|
114
|
+
def current(node)
|
115
|
+
if !node.ternary? && node.source.include?("\n")
|
116
|
+
"#{node.loc.keyword.with(end_pos: node.condition.loc.selector.end_pos).source} ... end"
|
117
|
+
else
|
118
|
+
node.source.gsub(/\n\s*/, ' ')
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def replacement(receiver, other, left_sibling)
|
115
123
|
or_source = if other&.send_type?
|
116
124
|
build_source_for_or_method(other)
|
117
125
|
elsif other.nil? || other.nil_type?
|
@@ -120,23 +128,24 @@ module RuboCop
|
|
120
128
|
" || #{other.source}"
|
121
129
|
end
|
122
130
|
|
123
|
-
"#{receiver.source}.presence"
|
131
|
+
replaced = "#{receiver.source}.presence#{or_source}"
|
132
|
+
left_sibling ? "(#{replaced})" : replaced
|
124
133
|
end
|
125
134
|
|
126
135
|
def build_source_for_or_method(other)
|
127
|
-
if other.parenthesized? || other.method?('[]') || !other.arguments?
|
136
|
+
if other.parenthesized? || other.method?('[]') || other.arithmetic_operation? || !other.arguments?
|
128
137
|
" || #{other.source}"
|
129
138
|
else
|
130
|
-
method =
|
131
|
-
other.source_range.begin_pos,
|
132
|
-
other.first_argument.source_range.begin_pos - 1
|
133
|
-
).source
|
134
|
-
|
139
|
+
method = method_range(other).source
|
135
140
|
arguments = other.arguments.map(&:source).join(', ')
|
136
141
|
|
137
142
|
" || #{method}(#{arguments})"
|
138
143
|
end
|
139
144
|
end
|
145
|
+
|
146
|
+
def method_range(node)
|
147
|
+
range_between(node.source_range.begin_pos, node.first_argument.source_range.begin_pos - 1)
|
148
|
+
end
|
140
149
|
end
|
141
150
|
end
|
142
151
|
end
|
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
#
|
9
9
|
# Interaction with `Style/UnlessElse`:
|
10
10
|
# The configuration of `NotBlank` will not produce an offense in the
|
11
|
-
# context of `unless else` if `Style/UnlessElse` is
|
11
|
+
# context of `unless else` if `Style/UnlessElse` is enabled. This is
|
12
12
|
# to prevent interference between the autocorrection of the two cops.
|
13
13
|
#
|
14
14
|
# @example NotNilAndNotEmpty: true (default)
|
@@ -47,10 +47,8 @@ module RuboCop
|
|
47
47
|
extend AutoCorrector
|
48
48
|
|
49
49
|
MSG_NOT_BLANK = 'Use `%<prefer>s` instead of `%<current>s`.'
|
50
|
-
MSG_EXISTS_AND_NOT_EMPTY = 'Use `%<prefer>s` instead of '
|
51
|
-
|
52
|
-
MSG_UNLESS_BLANK = 'Use `if %<prefer>s` instead of ' \
|
53
|
-
'`%<current>s`.'
|
50
|
+
MSG_EXISTS_AND_NOT_EMPTY = 'Use `%<prefer>s` instead of `%<current>s`.'
|
51
|
+
MSG_UNLESS_BLANK = 'Use `if %<prefer>s` instead of `%<current>s`.'
|
54
52
|
RESTRICT_ON_SEND = %i[!].freeze
|
55
53
|
|
56
54
|
def_node_matcher :exists_and_not_empty?, <<~PATTERN
|
@@ -118,8 +116,7 @@ module RuboCop
|
|
118
116
|
|
119
117
|
unless_blank?(node) do |method_call, receiver|
|
120
118
|
range = unless_condition(node, method_call)
|
121
|
-
msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver),
|
122
|
-
current: range.source)
|
119
|
+
msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver), current: range.source)
|
123
120
|
add_offense(range, message: msg) do |corrector|
|
124
121
|
autocorrect(corrector, node)
|
125
122
|
end
|
@@ -131,10 +128,10 @@ module RuboCop
|
|
131
128
|
|
132
129
|
if method_call
|
133
130
|
corrector.replace(node.loc.keyword, 'if')
|
134
|
-
range = method_call.
|
131
|
+
range = method_call.source_range
|
135
132
|
else
|
136
133
|
variable1, _variable2 = exists_and_not_empty?(node) || not_blank?(node)
|
137
|
-
range = node.
|
134
|
+
range = node.source_range
|
138
135
|
end
|
139
136
|
|
140
137
|
corrector.replace(range, replacement(variable1))
|
@@ -144,9 +141,9 @@ module RuboCop
|
|
144
141
|
|
145
142
|
def unless_condition(node, method_call)
|
146
143
|
if node.modifier_form?
|
147
|
-
node.loc.keyword.join(node.
|
144
|
+
node.loc.keyword.join(node.source_range.end)
|
148
145
|
else
|
149
|
-
node.
|
146
|
+
node.source_range.begin.join(method_call.source_range)
|
150
147
|
end
|
151
148
|
end
|
152
149
|
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
39
39
|
(block $(send nil? :task ...) ...)
|
40
40
|
PATTERN
|
41
41
|
|
42
|
-
def on_block(node)
|
42
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
43
43
|
task_definition?(node) do |task_method|
|
44
44
|
return if task_name(task_method) == :default
|
45
45
|
return if with_dependencies?(task_method)
|
@@ -48,7 +48,7 @@ module RuboCop
|
|
48
48
|
task_name = task_method.arguments[0]
|
49
49
|
task_dependency = correct_task_dependency(task_name)
|
50
50
|
|
51
|
-
corrector.replace(task_name
|
51
|
+
corrector.replace(task_name, task_dependency)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -30,11 +30,9 @@ module RuboCop
|
|
30
30
|
include RangeHelp
|
31
31
|
extend AutoCorrector
|
32
32
|
|
33
|
-
MSG_SAME =
|
34
|
-
'`allow_nil` is redundant when `allow_blank` has the same value.'
|
33
|
+
MSG_SAME = '`allow_nil` is redundant when `allow_blank` has the same value.'
|
35
34
|
|
36
|
-
MSG_ALLOW_NIL_FALSE =
|
37
|
-
'`allow_nil: false` is redundant when `allow_blank` is true.'
|
35
|
+
MSG_ALLOW_NIL_FALSE = '`allow_nil: false` is redundant when `allow_blank` is true.'
|
38
36
|
|
39
37
|
RESTRICT_ON_SEND = %i[validates].freeze
|
40
38
|
|
@@ -64,7 +62,7 @@ module RuboCop
|
|
64
62
|
elsif prv_sib
|
65
63
|
corrector.remove(range_between(node_end(prv_sib), node_end(allow_nil)))
|
66
64
|
else
|
67
|
-
corrector.remove(allow_nil
|
65
|
+
corrector.remove(allow_nil)
|
68
66
|
end
|
69
67
|
end
|
70
68
|
end
|
@@ -89,11 +87,11 @@ module RuboCop
|
|
89
87
|
end
|
90
88
|
|
91
89
|
def node_beg(node)
|
92
|
-
node.
|
90
|
+
node.source_range.begin_pos
|
93
91
|
end
|
94
92
|
|
95
93
|
def node_end(node)
|
96
|
-
node.
|
94
|
+
node.source_range.end_pos
|
97
95
|
end
|
98
96
|
end
|
99
97
|
end
|
@@ -40,8 +40,8 @@ module RuboCop
|
|
40
40
|
def on_send(node)
|
41
41
|
association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key|
|
42
42
|
if redundant?(node, type, name, options, foreign_key)
|
43
|
-
add_offense(foreign_key_pair.
|
44
|
-
range = range_with_surrounding_space(
|
43
|
+
add_offense(foreign_key_pair.source_range) do |corrector|
|
44
|
+
range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left)
|
45
45
|
range = range_with_surrounding_comma(range, :left)
|
46
46
|
|
47
47
|
corrector.remove(range)
|