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
@@ -3,10 +3,12 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
# Checks the migration for which timestamps are not included
|
7
|
-
# when creating a new table.
|
6
|
+
# Checks the migration for which timestamps are not included when creating a new table.
|
8
7
|
# In many cases, timestamps are useful information and should be added.
|
9
8
|
#
|
9
|
+
# NOTE: Allow `timestamps` not written when `id: false` because this emphasizes respecting
|
10
|
+
# user's editing intentions.
|
11
|
+
#
|
10
12
|
# @example
|
11
13
|
# # bad
|
12
14
|
# create_table :users
|
@@ -40,12 +42,23 @@ module RuboCop
|
|
40
42
|
#
|
41
43
|
# t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' }
|
42
44
|
# end
|
45
|
+
#
|
46
|
+
# # good
|
47
|
+
# create_table :users, articles, id: false do |t|
|
48
|
+
# t.integer :user_id
|
49
|
+
# t.integer :article_id
|
50
|
+
# end
|
51
|
+
#
|
43
52
|
class CreateTableWithTimestamps < Base
|
44
53
|
include ActiveRecordMigrationsHelper
|
45
54
|
|
46
55
|
MSG = 'Add timestamps when creating a new table.'
|
47
56
|
RESTRICT_ON_SEND = %i[create_table].freeze
|
48
57
|
|
58
|
+
def_node_search :use_id_false_option?, <<~PATTERN
|
59
|
+
(pair (sym :id) (false))
|
60
|
+
PATTERN
|
61
|
+
|
49
62
|
def_node_matcher :create_table_with_timestamps_proc?, <<~PATTERN
|
50
63
|
(send nil? :create_table (sym _) ... (block-pass (sym :timestamps)))
|
51
64
|
PATTERN
|
@@ -61,7 +74,7 @@ module RuboCop
|
|
61
74
|
PATTERN
|
62
75
|
|
63
76
|
def on_send(node)
|
64
|
-
return
|
77
|
+
return if !node.command?(:create_table) || use_id_false_option?(node)
|
65
78
|
|
66
79
|
parent = node.parent
|
67
80
|
|
@@ -22,6 +22,9 @@ module RuboCop
|
|
22
22
|
# And you can set a warning for `to_time` with `AllowToTime: false`.
|
23
23
|
# `AllowToTime` is `true` by default to prevent false positive on `DateTime` object.
|
24
24
|
#
|
25
|
+
# @safety
|
26
|
+
# This cop's autocorrection is unsafe because it may change handling time.
|
27
|
+
#
|
25
28
|
# @example EnforcedStyle: flexible (default)
|
26
29
|
# # bad
|
27
30
|
# Date.today
|
@@ -51,24 +54,21 @@ module RuboCop
|
|
51
54
|
# # bad
|
52
55
|
# date.to_time
|
53
56
|
class Date < Base
|
57
|
+
extend AutoCorrector
|
58
|
+
|
54
59
|
include ConfigurableEnforcedStyle
|
55
60
|
|
56
|
-
MSG = 'Do not use `Date.%<method_called>s` without zone. Use '
|
57
|
-
'`Time.zone.%<day>s` instead.'
|
61
|
+
MSG = 'Do not use `Date.%<method_called>s` without zone. Use `Time.zone.%<day>s` instead.'
|
58
62
|
|
59
|
-
MSG_SEND = 'Do not use `%<method>s` on Date objects, because they '
|
60
|
-
'know nothing about the time zone in use.'
|
63
|
+
MSG_SEND = 'Do not use `%<method>s` on Date objects, because they know nothing about the time zone in use.'
|
61
64
|
|
62
65
|
RESTRICT_ON_SEND = %i[to_time to_time_in_current_zone].freeze
|
63
66
|
|
64
67
|
BAD_DAYS = %i[today current yesterday tomorrow].freeze
|
65
68
|
|
66
|
-
DEPRECATED_METHODS = [
|
67
|
-
{ deprecated: 'to_time_in_current_zone', relevant: 'in_time_zone' }
|
68
|
-
].freeze
|
69
|
+
DEPRECATED_METHODS = [{ deprecated: 'to_time_in_current_zone', relevant: 'in_time_zone' }].freeze
|
69
70
|
|
70
|
-
DEPRECATED_MSG = '`%<deprecated>s` is deprecated. '
|
71
|
-
'Use `%<relevant>s` instead.'
|
71
|
+
DEPRECATED_MSG = '`%<deprecated>s` is deprecated. Use `%<relevant>s` instead.'
|
72
72
|
|
73
73
|
def on_const(node)
|
74
74
|
mod, klass = *node.children
|
@@ -97,7 +97,9 @@ module RuboCop
|
|
97
97
|
|
98
98
|
message = format(DEPRECATED_MSG, deprecated: method[:deprecated], relevant: method[:relevant])
|
99
99
|
|
100
|
-
add_offense(node.loc.selector, message: message)
|
100
|
+
add_offense(node.loc.selector, message: message) do |corrector|
|
101
|
+
corrector.replace(node.loc.selector, method[:relevant].to_s)
|
102
|
+
end
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
@@ -113,7 +115,9 @@ module RuboCop
|
|
113
115
|
|
114
116
|
message = format(MSG, method_called: method_name, day: day)
|
115
117
|
|
116
|
-
add_offense(node.loc.selector, message: message)
|
118
|
+
add_offense(node.loc.selector, message: message) do |corrector|
|
119
|
+
corrector.replace(node.receiver.loc.name, 'Time.zone')
|
120
|
+
end
|
117
121
|
end
|
118
122
|
|
119
123
|
def extract_method_chain(node)
|
@@ -24,6 +24,14 @@ module RuboCop
|
|
24
24
|
# # good
|
25
25
|
# delegate :bar, to: :foo
|
26
26
|
#
|
27
|
+
# # bad
|
28
|
+
# def bar
|
29
|
+
# self.bar
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # good
|
33
|
+
# delegate :bar, to: :self
|
34
|
+
#
|
27
35
|
# # good
|
28
36
|
# def bar
|
29
37
|
# foo&.bar
|
@@ -60,7 +68,7 @@ module RuboCop
|
|
60
68
|
|
61
69
|
def_node_matcher :delegate?, <<~PATTERN
|
62
70
|
(def _method_name _args
|
63
|
-
(send (send nil? _) _ ...))
|
71
|
+
(send {(send nil? _) (self)} _ ...))
|
64
72
|
PATTERN
|
65
73
|
|
66
74
|
def on_def(node)
|
@@ -74,10 +82,14 @@ module RuboCop
|
|
74
82
|
|
75
83
|
def register_offense(node)
|
76
84
|
add_offense(node.loc.keyword) do |corrector|
|
77
|
-
|
85
|
+
body = node.body
|
86
|
+
|
87
|
+
receiver = body.receiver.self_type? ? 'self' : ":#{body.receiver.method_name}"
|
88
|
+
|
89
|
+
delegation = ["delegate :#{body.method_name}", "to: #{receiver}"]
|
78
90
|
delegation << ['prefix: true'] if node.method?(prefixed_method_name(node.body))
|
79
91
|
|
80
|
-
corrector.replace(node
|
92
|
+
corrector.replace(node, delegation.join(', '))
|
81
93
|
end
|
82
94
|
end
|
83
95
|
|
@@ -93,15 +105,12 @@ module RuboCop
|
|
93
105
|
return false if arg_array.size != argument_array.size
|
94
106
|
|
95
107
|
arg_array.zip(argument_array).all? do |arg, argument|
|
96
|
-
arg.arg_type? &&
|
97
|
-
argument.lvar_type? &&
|
98
|
-
arg.children == argument.children
|
108
|
+
arg.arg_type? && argument.lvar_type? && arg.children == argument.children
|
99
109
|
end
|
100
110
|
end
|
101
111
|
|
102
112
|
def method_name_matches?(method_name, body)
|
103
|
-
method_name == body.method_name ||
|
104
|
-
(include_prefix_case? && method_name == prefixed_method_name(body))
|
113
|
+
method_name == body.method_name || (include_prefix_case? && method_name == prefixed_method_name(body))
|
105
114
|
end
|
106
115
|
|
107
116
|
def include_prefix_case?
|
@@ -109,6 +118,8 @@ module RuboCop
|
|
109
118
|
end
|
110
119
|
|
111
120
|
def prefixed_method_name(body)
|
121
|
+
return '' if body.receiver.self_type?
|
122
|
+
|
112
123
|
[body.receiver.method_name, body.method_name].join('_').to_sym
|
113
124
|
end
|
114
125
|
|
@@ -27,7 +27,7 @@ module RuboCop
|
|
27
27
|
return unless (offending_node = allow_blank_option(node))
|
28
28
|
|
29
29
|
add_offense(offending_node) do |corrector|
|
30
|
-
corrector.replace(offending_node.key
|
30
|
+
corrector.replace(offending_node.key, 'allow_nil')
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
@@ -37,7 +37,8 @@ module RuboCop
|
|
37
37
|
extend AutoCorrector
|
38
38
|
|
39
39
|
MSG = 'Avoid manipulating ActiveModel errors as hash directly.'
|
40
|
-
|
40
|
+
AUTOCORRECTABLE_METHODS = %i[<< clear keys].freeze
|
41
|
+
INCOMPATIBLE_METHODS = %i[keys values to_h to_xml].freeze
|
41
42
|
|
42
43
|
MANIPULATIVE_METHODS = Set[
|
43
44
|
*%i[
|
@@ -55,7 +56,7 @@ module RuboCop
|
|
55
56
|
{
|
56
57
|
#root_manipulation?
|
57
58
|
#root_assignment?
|
58
|
-
#
|
59
|
+
#errors_deprecated?
|
59
60
|
#messages_details_manipulation?
|
60
61
|
#messages_details_assignment?
|
61
62
|
}
|
@@ -77,10 +78,10 @@ module RuboCop
|
|
77
78
|
...)
|
78
79
|
PATTERN
|
79
80
|
|
80
|
-
def_node_matcher :
|
81
|
+
def_node_matcher :errors_deprecated?, <<~PATTERN
|
81
82
|
(send
|
82
83
|
(send #receiver_matcher :errors)
|
83
|
-
:keys)
|
84
|
+
{:keys :values :to_h :to_xml})
|
84
85
|
PATTERN
|
85
86
|
|
86
87
|
def_node_matcher :messages_details_manipulation?, <<~PATTERN
|
@@ -106,10 +107,10 @@ module RuboCop
|
|
106
107
|
|
107
108
|
def on_send(node)
|
108
109
|
any_manipulation?(node) do
|
109
|
-
next if
|
110
|
+
next if target_rails_version <= 6.0 && INCOMPATIBLE_METHODS.include?(node.method_name)
|
110
111
|
|
111
112
|
add_offense(node) do |corrector|
|
112
|
-
next
|
113
|
+
next if skip_autocorrect?(node)
|
113
114
|
|
114
115
|
autocorrect(corrector, node)
|
115
116
|
end
|
@@ -118,14 +119,16 @@ module RuboCop
|
|
118
119
|
|
119
120
|
private
|
120
121
|
|
122
|
+
def skip_autocorrect?(node)
|
123
|
+
return true unless AUTOCORRECTABLE_METHODS.include?(node.method_name)
|
124
|
+
return false unless (receiver = node.receiver.receiver)
|
125
|
+
|
126
|
+
receiver.send_type? && receiver.method?(:details) && node.method?(:<<)
|
127
|
+
end
|
128
|
+
|
121
129
|
def autocorrect(corrector, node)
|
122
130
|
receiver = node.receiver
|
123
131
|
|
124
|
-
if receiver.receiver.send_type? && receiver.receiver.method?(:messages)
|
125
|
-
corrector.remove(receiver.receiver.loc.dot)
|
126
|
-
corrector.remove(receiver.receiver.loc.selector)
|
127
|
-
end
|
128
|
-
|
129
132
|
range = offense_range(node, receiver)
|
130
133
|
replacement = replacement(node, receiver)
|
131
134
|
|
@@ -133,11 +136,12 @@ module RuboCop
|
|
133
136
|
end
|
134
137
|
|
135
138
|
def offense_range(node, receiver)
|
136
|
-
|
139
|
+
receiver = receiver.receiver while receiver.send_type? && !receiver.method?(:errors) && receiver.receiver
|
140
|
+
range_between(receiver.source_range.end_pos, node.source_range.end_pos)
|
137
141
|
end
|
138
142
|
|
139
143
|
def replacement(node, receiver)
|
140
|
-
return '.
|
144
|
+
return '.attribute_names' if node.method?(:keys)
|
141
145
|
|
142
146
|
key = receiver.first_argument.source
|
143
147
|
|
@@ -156,7 +160,7 @@ module RuboCop
|
|
156
160
|
end
|
157
161
|
|
158
162
|
def model_file?
|
159
|
-
processed_source.
|
163
|
+
processed_source.file_path.include?('/models/')
|
160
164
|
end
|
161
165
|
end
|
162
166
|
end
|
@@ -24,7 +24,7 @@ module RuboCop
|
|
24
24
|
TRANSLATE_METHODS = %i[translate t].freeze
|
25
25
|
|
26
26
|
def_node_matcher :translate_with_scope?, <<~PATTERN
|
27
|
-
(send {nil? (const nil? :I18n)} {:translate :t} ${sym_type? str_type?}
|
27
|
+
(send {nil? (const {nil? cbase} :I18n)} {:translate :t} ${sym_type? str_type?}
|
28
28
|
(hash <$(pair (sym :scope) ${array_type? sym_type?}) ...>)
|
29
29
|
)
|
30
30
|
PATTERN
|
@@ -37,7 +37,7 @@ module RuboCop
|
|
37
37
|
|
38
38
|
add_offense(scope_node) do |corrector|
|
39
39
|
# Eat the comma on the left.
|
40
|
-
range = range_with_surrounding_space(
|
40
|
+
range = range_with_surrounding_space(scope_node.source_range, side: :left)
|
41
41
|
range = range_with_surrounding_comma(range, :left)
|
42
42
|
corrector.remove(range)
|
43
43
|
|
@@ -70,15 +70,15 @@ module RuboCop
|
|
70
70
|
# @return [Boolean] true if matches
|
71
71
|
def_node_matcher :time_current?, <<~PATTERN
|
72
72
|
{
|
73
|
-
(send (const
|
74
|
-
(send (send (const
|
73
|
+
(send (const {nil? cbase} :Time) :current)
|
74
|
+
(send (send (const {nil? cbase} :Time) :zone) :now)
|
75
75
|
}
|
76
76
|
PATTERN
|
77
77
|
|
78
78
|
def on_send(node)
|
79
79
|
duration_arithmetic_argument?(node) do |*operation|
|
80
80
|
add_offense(node) do |corrector|
|
81
|
-
corrector.replace(node
|
81
|
+
corrector.replace(node, corrected_source(*operation))
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
@@ -22,19 +22,23 @@ module RuboCop
|
|
22
22
|
# User.find_by(name: name, email: email)
|
23
23
|
# User.find_by!(email: email)
|
24
24
|
#
|
25
|
-
# @example AllowedMethods: find_by_sql
|
25
|
+
# @example AllowedMethods: ['find_by_sql', 'find_by_token_for'] (default)
|
26
26
|
# # bad
|
27
27
|
# User.find_by_query(users_query)
|
28
|
+
# User.find_by_token_for(:password_reset, token)
|
28
29
|
#
|
29
30
|
# # good
|
30
31
|
# User.find_by_sql(users_sql)
|
32
|
+
# User.find_by_token_for(:password_reset, token)
|
31
33
|
#
|
32
|
-
# @example AllowedReceivers: Gem::Specification
|
34
|
+
# @example AllowedReceivers: ['Gem::Specification', 'page'] (default)
|
33
35
|
# # bad
|
34
36
|
# Specification.find_by_name('backend').gem_dir
|
37
|
+
# page.find_by_id('a_dom_id').click
|
35
38
|
#
|
36
39
|
# # good
|
37
40
|
# Gem::Specification.find_by_name('backend').gem_dir
|
41
|
+
# page.find_by_id('a_dom_id').click
|
38
42
|
class DynamicFindBy < Base
|
39
43
|
include ActiveRecordHelper
|
40
44
|
extend AutoCorrector
|
@@ -49,7 +53,7 @@ module RuboCop
|
|
49
53
|
method_name = node.method_name
|
50
54
|
static_name = static_method_name(method_name)
|
51
55
|
return unless static_name
|
52
|
-
return
|
56
|
+
return unless dynamic_find_by_arguments?(node)
|
53
57
|
|
54
58
|
message = format(MSG, static_name: static_name, method: method_name)
|
55
59
|
add_offense(node, message: message) do |corrector|
|
@@ -61,17 +65,12 @@ module RuboCop
|
|
61
65
|
private
|
62
66
|
|
63
67
|
def autocorrect(corrector, node)
|
64
|
-
keywords = column_keywords(node.method_name)
|
65
|
-
|
66
|
-
return if keywords.size != node.arguments.size
|
67
|
-
|
68
68
|
autocorrect_method_name(corrector, node)
|
69
|
-
autocorrect_argument_keywords(corrector, node,
|
69
|
+
autocorrect_argument_keywords(corrector, node, column_keywords(node.method_name))
|
70
70
|
end
|
71
71
|
|
72
72
|
def allowed_invocation?(node)
|
73
|
-
allowed_method?(node) || allowed_receiver?(node) ||
|
74
|
-
whitelisted?(node)
|
73
|
+
allowed_method?(node) || allowed_receiver?(node) || whitelisted?(node)
|
75
74
|
end
|
76
75
|
|
77
76
|
def allowed_method?(node)
|
@@ -95,13 +94,12 @@ module RuboCop
|
|
95
94
|
end
|
96
95
|
|
97
96
|
def autocorrect_method_name(corrector, node)
|
98
|
-
corrector.replace(node.loc.selector,
|
99
|
-
static_method_name(node.method_name.to_s))
|
97
|
+
corrector.replace(node.loc.selector, static_method_name(node.method_name.to_s))
|
100
98
|
end
|
101
99
|
|
102
100
|
def autocorrect_argument_keywords(corrector, node, keywords)
|
103
101
|
keywords.each.with_index do |keyword, idx|
|
104
|
-
corrector.insert_before(node.arguments[idx]
|
102
|
+
corrector.insert_before(node.arguments[idx], keyword)
|
105
103
|
end
|
106
104
|
end
|
107
105
|
|
@@ -118,6 +116,20 @@ module RuboCop
|
|
118
116
|
|
119
117
|
match[2] ? 'find_by!' : 'find_by'
|
120
118
|
end
|
119
|
+
|
120
|
+
def dynamic_find_by_arguments?(node)
|
121
|
+
dynamic_find_by_arguments_count?(node) && dynamic_find_by_arguments_type?(node)
|
122
|
+
end
|
123
|
+
|
124
|
+
def dynamic_find_by_arguments_count?(node)
|
125
|
+
column_keywords(node.method_name).size == node.arguments.size
|
126
|
+
end
|
127
|
+
|
128
|
+
def dynamic_find_by_arguments_type?(node)
|
129
|
+
node.arguments.none? do |argument|
|
130
|
+
IGNORED_ARGUMENT_TYPES.include?(argument.type)
|
131
|
+
end
|
132
|
+
end
|
121
133
|
end
|
122
134
|
end
|
123
135
|
end
|
@@ -37,6 +37,10 @@ module RuboCop
|
|
37
37
|
)
|
38
38
|
PATTERN
|
39
39
|
|
40
|
+
def self.autocorrect_incompatible_with
|
41
|
+
[Style::MethodCallWithArgsParentheses]
|
42
|
+
end
|
43
|
+
|
40
44
|
def on_send(node)
|
41
45
|
return if node.parent&.block_type?
|
42
46
|
|
@@ -55,7 +59,7 @@ module RuboCop
|
|
55
59
|
private
|
56
60
|
|
57
61
|
def replacement_range(node)
|
58
|
-
stop = node.
|
62
|
+
stop = node.source_range.end
|
59
63
|
start = node.loc.selector.end
|
60
64
|
|
61
65
|
if node.parenthesized_call?
|
@@ -20,8 +20,7 @@ module RuboCop
|
|
20
20
|
class EnumUniqueness < Base
|
21
21
|
include Duplication
|
22
22
|
|
23
|
-
MSG = 'Duplicate value `%<value>s` found in `%<enum>s` '
|
24
|
-
'enum declaration.'
|
23
|
+
MSG = 'Duplicate value `%<value>s` found in `%<enum>s` enum declaration.'
|
25
24
|
RESTRICT_ON_SEND = %i[enum].freeze
|
26
25
|
|
27
26
|
def_node_matcher :enum?, <<~PATTERN
|
@@ -41,9 +40,7 @@ module RuboCop
|
|
41
40
|
next unless duplicates?(items)
|
42
41
|
|
43
42
|
consecutive_duplicates(items).each do |item|
|
44
|
-
add_offense(item, message: format(
|
45
|
-
MSG, value: item.source, enum: enum_name(key)
|
46
|
-
))
|
43
|
+
add_offense(item, message: format(MSG, value: item.source, enum: enum_name(key)))
|
47
44
|
end
|
48
45
|
end
|
49
46
|
end
|
@@ -84,7 +84,7 @@ module RuboCop
|
|
84
84
|
def autocorrect(corrector, node)
|
85
85
|
replacement = build_predicate_method(node)
|
86
86
|
|
87
|
-
corrector.replace(node
|
87
|
+
corrector.replace(node, replacement)
|
88
88
|
end
|
89
89
|
|
90
90
|
def build_predicate_method(node)
|
@@ -96,8 +96,7 @@ module RuboCop
|
|
96
96
|
end
|
97
97
|
|
98
98
|
def rails_env_on_lhs?(node)
|
99
|
-
comparing_str_env_with_rails_env_on_lhs?(node) ||
|
100
|
-
comparing_sym_env_with_rails_env_on_lhs?(node)
|
99
|
+
comparing_str_env_with_rails_env_on_lhs?(node) || comparing_sym_env_with_rails_env_on_lhs?(node)
|
101
100
|
end
|
102
101
|
|
103
102
|
def build_predicate_method_for_rails_env_on_lhs(node)
|