rubocop-rails 2.0.1 → 2.19.1
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 +52 -5
- data/config/default.yml +726 -32
- data/config/obsoletion.yml +17 -0
- data/lib/rubocop/cop/mixin/active_record_helper.rb +106 -0
- data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +32 -0
- data/lib/rubocop/cop/mixin/class_send_node_helper.rb +20 -0
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
- data/lib/rubocop/cop/mixin/index_method.rb +165 -0
- data/lib/rubocop/cop/mixin/migrations_helper.rb +26 -0
- data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +112 -0
- data/lib/rubocop/cop/rails/action_controller_test_case.rb +47 -0
- data/lib/rubocop/cop/rails/action_filter.rb +11 -21
- data/lib/rubocop/cop/rails/action_order.rb +116 -0
- data/lib/rubocop/cop/rails/active_record_aliases.rb +23 -24
- data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +143 -0
- data/lib/rubocop/cop/rails/active_record_override.rb +3 -6
- data/lib/rubocop/cop/rails/active_support_aliases.rb +13 -22
- data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
- data/lib/rubocop/cop/rails/add_column_index.rb +61 -0
- data/lib/rubocop/cop/rails/after_commit_override.rb +81 -0
- data/lib/rubocop/cop/rails/application_controller.rb +36 -0
- data/lib/rubocop/cop/rails/application_job.rb +9 -4
- data/lib/rubocop/cop/rails/application_mailer.rb +39 -0
- data/lib/rubocop/cop/rails/application_record.rb +9 -9
- data/lib/rubocop/cop/rails/arel_star.rb +47 -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 +12 -24
- data/lib/rubocop/cop/rails/blank.rb +40 -36
- data/lib/rubocop/cop/rails/bulk_change_table.rb +40 -35
- data/lib/rubocop/cop/rails/compact_blank.rb +111 -0
- data/lib/rubocop/cop/rails/content_tag.rb +93 -0
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +22 -15
- data/lib/rubocop/cop/rails/date.rb +41 -36
- data/lib/rubocop/cop/rails/default_scope.rb +61 -0
- data/lib/rubocop/cop/rails/delegate.rb +33 -29
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +9 -10
- data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +168 -0
- data/lib/rubocop/cop/rails/dot_separated_keys.rb +71 -0
- data/lib/rubocop/cop/rails/duplicate_association.rb +56 -0
- data/lib/rubocop/cop/rails/duplicate_scope.rb +46 -0
- data/lib/rubocop/cop/rails/duration_arithmetic.rb +98 -0
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +76 -31
- data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +82 -0
- data/lib/rubocop/cop/rails/enum_hash.rb +75 -0
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +30 -12
- data/lib/rubocop/cop/rails/environment_comparison.rb +70 -22
- data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
- data/lib/rubocop/cop/rails/exit.rb +7 -13
- data/lib/rubocop/cop/rails/expanded_date_range.rb +102 -0
- data/lib/rubocop/cop/rails/file_path.rb +48 -31
- data/lib/rubocop/cop/rails/find_by.rb +43 -24
- data/lib/rubocop/cop/rails/find_by_id.rb +94 -0
- data/lib/rubocop/cop/rails/find_each.rb +42 -18
- data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +4 -3
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +62 -25
- data/lib/rubocop/cop/rails/helper_instance_variable.rb +32 -4
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +61 -32
- data/lib/rubocop/cop/rails/http_status.rb +27 -23
- data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +96 -0
- data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +37 -0
- data/lib/rubocop/cop/rails/i18n_locale_texts.rb +110 -0
- data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +9 -16
- data/lib/rubocop/cop/rails/index_by.rb +65 -0
- data/lib/rubocop/cop/rails/index_with.rb +68 -0
- data/lib/rubocop/cop/rails/inquiry.rb +39 -0
- data/lib/rubocop/cop/rails/inverse_of.rb +33 -27
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +62 -32
- data/lib/rubocop/cop/rails/link_to_blank.rb +31 -32
- data/lib/rubocop/cop/rails/mailer_name.rb +90 -0
- data/lib/rubocop/cop/rails/match_route.rb +120 -0
- data/lib/rubocop/cop/rails/migration_class_name.rb +63 -0
- data/lib/rubocop/cop/rails/negate_include.rb +42 -0
- data/lib/rubocop/cop/rails/not_null_column.rb +16 -12
- data/lib/rubocop/cop/rails/order_by_id.rb +51 -0
- data/lib/rubocop/cop/rails/output.rb +29 -10
- data/lib/rubocop/cop/rails/output_safety.rb +9 -4
- data/lib/rubocop/cop/rails/pick.rb +64 -0
- data/lib/rubocop/cop/rails/pluck.rb +96 -0
- data/lib/rubocop/cop/rails/pluck_id.rb +59 -0
- data/lib/rubocop/cop/rails/pluck_in_where.rb +71 -0
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +14 -19
- data/lib/rubocop/cop/rails/presence.rb +54 -26
- data/lib/rubocop/cop/rails/present.rb +40 -37
- data/lib/rubocop/cop/rails/rake_environment.rb +112 -0
- data/lib/rubocop/cop/rails/read_write_attribute.rb +56 -18
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +33 -45
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +77 -0
- data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +257 -0
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +34 -32
- data/lib/rubocop/cop/rails/redundant_travel_back.rb +57 -0
- data/lib/rubocop/cop/rails/reflection_class_name.rb +56 -7
- data/lib/rubocop/cop/rails/refute_methods.rb +56 -35
- data/lib/rubocop/cop/rails/relative_date_constant.rb +52 -33
- data/lib/rubocop/cop/rails/render_inline.rb +41 -0
- data/lib/rubocop/cop/rails/render_plain_text.rb +71 -0
- data/lib/rubocop/cop/rails/request_referer.rb +10 -11
- data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
- data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
- data/lib/rubocop/cop/rails/reversible_migration.rb +122 -82
- data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +66 -0
- data/lib/rubocop/cop/rails/root_join_chain.rb +72 -0
- data/lib/rubocop/cop/rails/root_pathname_methods.rb +238 -0
- data/lib/rubocop/cop/rails/root_public_path.rb +59 -0
- data/lib/rubocop/cop/rails/safe_navigation.rb +55 -43
- data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +50 -0
- data/lib/rubocop/cop/rails/save_bang.rb +89 -63
- data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
- data/lib/rubocop/cop/rails/scope_args.rb +8 -3
- data/lib/rubocop/cop/rails/short_i18n.rb +71 -0
- data/lib/rubocop/cop/rails/skips_model_validations.rb +53 -16
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +87 -0
- data/lib/rubocop/cop/rails/strip_heredoc.rb +56 -0
- data/lib/rubocop/cop/rails/table_name_assignment.rb +44 -0
- data/lib/rubocop/cop/rails/three_state_boolean_column.rb +73 -0
- data/lib/rubocop/cop/rails/time_zone.rb +83 -67
- data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
- data/lib/rubocop/cop/rails/to_formatted_s.rb +46 -0
- 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 +99 -0
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +40 -49
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +172 -0
- data/lib/rubocop/cop/rails/unknown_env.rb +52 -21
- data/lib/rubocop/cop/rails/unused_ignored_columns.rb +76 -0
- data/lib/rubocop/cop/rails/validation.rb +54 -23
- data/lib/rubocop/cop/rails/where_equals.rb +102 -0
- data/lib/rubocop/cop/rails/where_exists.rb +138 -0
- data/lib/rubocop/cop/rails/where_missing.rb +118 -0
- data/lib/rubocop/cop/rails/where_not.rb +101 -0
- data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
- data/lib/rubocop/cop/rails_cops.rb +78 -8
- data/lib/rubocop/rails/inject.rb +1 -1
- data/lib/rubocop/rails/schema_loader/schema.rb +191 -0
- data/lib/rubocop/rails/schema_loader.rb +61 -0
- data/lib/rubocop/rails/version.rb +5 -1
- data/lib/rubocop/rails.rb +3 -1
- data/lib/rubocop-rails.rb +22 -0
- metadata +120 -19
- data/bin/setup +0 -7
@@ -0,0 +1,120 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Identifies places where defining routes with `match`
|
7
|
+
# can be replaced with a specific HTTP method.
|
8
|
+
#
|
9
|
+
# Don't use `match` to define any routes unless there is a need to map multiple request types
|
10
|
+
# among [:get, :post, :patch, :put, :delete] to a single action using the `:via` option.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# # bad
|
14
|
+
# match ':controller/:action/:id'
|
15
|
+
# match 'photos/:id', to: 'photos#show', via: :get
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# get ':controller/:action/:id'
|
19
|
+
# get 'photos/:id', to: 'photos#show'
|
20
|
+
# match 'photos/:id', to: 'photos#show', via: [:get, :post]
|
21
|
+
# match 'photos/:id', to: 'photos#show', via: :all
|
22
|
+
#
|
23
|
+
class MatchRoute < Base
|
24
|
+
extend AutoCorrector
|
25
|
+
|
26
|
+
MSG = 'Use `%<http_method>s` instead of `match` to define a route.'
|
27
|
+
RESTRICT_ON_SEND = %i[match].freeze
|
28
|
+
HTTP_METHODS = %i[get post put patch delete].freeze
|
29
|
+
|
30
|
+
def_node_matcher :match_method_call?, <<~PATTERN
|
31
|
+
(send nil? :match $_ $(hash ...) ?)
|
32
|
+
PATTERN
|
33
|
+
|
34
|
+
def on_send(node)
|
35
|
+
match_method_call?(node) do |path_node, options_node|
|
36
|
+
return unless within_routes?(node)
|
37
|
+
|
38
|
+
options_node = path_node.hash_type? ? path_node : options_node.first
|
39
|
+
|
40
|
+
if options_node.nil?
|
41
|
+
register_offense(node, 'get')
|
42
|
+
else
|
43
|
+
via = extract_via(options_node)
|
44
|
+
return unless via.size == 1 && http_method?(via.first)
|
45
|
+
|
46
|
+
register_offense(node, via.first)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def register_offense(node, http_method)
|
54
|
+
add_offense(node, message: format(MSG, http_method: http_method)) do |corrector|
|
55
|
+
match_method_call?(node) do |path_node, options_node|
|
56
|
+
options_node = options_node.first
|
57
|
+
|
58
|
+
corrector.replace(node, replacement(path_node, options_node))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def_node_matcher :routes_draw?, <<~PATTERN
|
64
|
+
(send (send _ :routes) :draw)
|
65
|
+
PATTERN
|
66
|
+
|
67
|
+
def within_routes?(node)
|
68
|
+
node.each_ancestor(:block).any? { |a| routes_draw?(a.send_node) }
|
69
|
+
end
|
70
|
+
|
71
|
+
def extract_via(node)
|
72
|
+
via_pair = via_pair(node)
|
73
|
+
return %i[get] unless via_pair
|
74
|
+
|
75
|
+
_, via = *via_pair
|
76
|
+
|
77
|
+
if via.basic_literal?
|
78
|
+
[via.value]
|
79
|
+
elsif via.array_type?
|
80
|
+
via.values.map(&:value)
|
81
|
+
else
|
82
|
+
[]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def via_pair(node)
|
87
|
+
node.pairs.find { |p| p.key.value == :via }
|
88
|
+
end
|
89
|
+
|
90
|
+
def http_method?(method)
|
91
|
+
HTTP_METHODS.include?(method.to_sym)
|
92
|
+
end
|
93
|
+
|
94
|
+
def replacement(path_node, options_node)
|
95
|
+
if path_node.hash_type?
|
96
|
+
http_method, options = *http_method_and_options(path_node)
|
97
|
+
"#{http_method} #{options.map(&:source).join(', ')}"
|
98
|
+
elsif options_node.nil?
|
99
|
+
"get #{path_node.source}"
|
100
|
+
else
|
101
|
+
http_method, options = *http_method_and_options(options_node)
|
102
|
+
|
103
|
+
if options.any?
|
104
|
+
"#{http_method} #{path_node.source}, #{options.map(&:source).join(', ')}"
|
105
|
+
else
|
106
|
+
"#{http_method} #{path_node.source}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def http_method_and_options(node)
|
112
|
+
via_pair = via_pair(node)
|
113
|
+
http_method = extract_via(node).first
|
114
|
+
rest_pairs = node.pairs - [via_pair]
|
115
|
+
[http_method, rest_pairs]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Makes sure that each migration file defines a migration class
|
7
|
+
# whose name matches the file name.
|
8
|
+
# (e.g. `20220224111111_create_users.rb` should define `CreateUsers` class.)
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# # db/migrate/20220224111111_create_users.rb
|
12
|
+
#
|
13
|
+
# # bad
|
14
|
+
# class SellBooks < ActiveRecord::Migration[7.0]
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# class CreateUsers < ActiveRecord::Migration[7.0]
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
class MigrationClassName < Base
|
22
|
+
extend AutoCorrector
|
23
|
+
include MigrationsHelper
|
24
|
+
|
25
|
+
MSG = 'Replace with `%<camelized_basename>s` that matches the file name.'
|
26
|
+
|
27
|
+
def on_class(node)
|
28
|
+
return unless migration_class?(node)
|
29
|
+
|
30
|
+
basename = basename_without_timestamp_and_suffix(processed_source.file_path)
|
31
|
+
|
32
|
+
class_identifier = node.identifier.location.name
|
33
|
+
camelized_basename = camelize(basename)
|
34
|
+
return if class_identifier.source.casecmp(camelized_basename).zero?
|
35
|
+
|
36
|
+
message = format(MSG, camelized_basename: camelized_basename)
|
37
|
+
|
38
|
+
add_offense(class_identifier, message: message) do |corrector|
|
39
|
+
corrector.replace(class_identifier, camelized_basename)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def basename_without_timestamp_and_suffix(filepath)
|
46
|
+
basename = File.basename(filepath, '.rb')
|
47
|
+
basename = remove_gem_suffix(basename)
|
48
|
+
|
49
|
+
basename.sub(/\A\d+_/, '')
|
50
|
+
end
|
51
|
+
|
52
|
+
# e.g.: from `add_blobs.active_storage` to `add_blobs`.
|
53
|
+
def remove_gem_suffix(file_name)
|
54
|
+
file_name.sub(/\..+\z/, '')
|
55
|
+
end
|
56
|
+
|
57
|
+
def camelize(word)
|
58
|
+
word.split('_').map(&:capitalize).join
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Enforces the use of `collection.exclude?(obj)`
|
7
|
+
# over `!collection.include?(obj)`.
|
8
|
+
#
|
9
|
+
# @safety
|
10
|
+
# This cop is unsafe because false positive will occur for
|
11
|
+
# receiver objects that do not have an `exclude?` method. (e.g. `IPAddr`)
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# # bad
|
15
|
+
# !array.include?(2)
|
16
|
+
# !hash.include?(:key)
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
# array.exclude?(2)
|
20
|
+
# hash.exclude?(:key)
|
21
|
+
#
|
22
|
+
class NegateInclude < Base
|
23
|
+
extend AutoCorrector
|
24
|
+
|
25
|
+
MSG = 'Use `.exclude?` and remove the negation part.'
|
26
|
+
RESTRICT_ON_SEND = %i[!].freeze
|
27
|
+
|
28
|
+
def_node_matcher :negate_include_call?, <<~PATTERN
|
29
|
+
(send (send $!nil? :include? $_) :!)
|
30
|
+
PATTERN
|
31
|
+
|
32
|
+
def on_send(node)
|
33
|
+
return unless (receiver, obj = negate_include_call?(node))
|
34
|
+
|
35
|
+
add_offense(node) do |corrector|
|
36
|
+
corrector.replace(node, "#{receiver.source}.exclude?(#{obj.source})")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
#
|
6
|
+
# Checks for add_column call with NOT NULL constraint
|
7
7
|
# in migration file.
|
8
8
|
#
|
9
9
|
# @example
|
@@ -16,22 +16,23 @@ module RuboCop
|
|
16
16
|
# add_column :users, :name, :string, null: false, default: ''
|
17
17
|
# add_reference :products, :category
|
18
18
|
# add_reference :products, :category, null: false, default: 1
|
19
|
-
class NotNullColumn <
|
19
|
+
class NotNullColumn < Base
|
20
20
|
MSG = 'Do not add a NOT NULL column without a default value.'
|
21
|
+
RESTRICT_ON_SEND = %i[add_column add_reference].freeze
|
21
22
|
|
22
|
-
def_node_matcher :add_not_null_column?,
|
23
|
-
(send nil? :add_column _ _ _ (hash $...))
|
23
|
+
def_node_matcher :add_not_null_column?, <<~PATTERN
|
24
|
+
(send nil? :add_column _ _ $_ (hash $...))
|
24
25
|
PATTERN
|
25
26
|
|
26
|
-
def_node_matcher :add_not_null_reference?,
|
27
|
+
def_node_matcher :add_not_null_reference?, <<~PATTERN
|
27
28
|
(send nil? :add_reference _ _ (hash $...))
|
28
29
|
PATTERN
|
29
30
|
|
30
|
-
def_node_matcher :null_false?,
|
31
|
+
def_node_matcher :null_false?, <<~PATTERN
|
31
32
|
(pair (sym :null) (false))
|
32
33
|
PATTERN
|
33
34
|
|
34
|
-
def_node_matcher :default_option?,
|
35
|
+
def_node_matcher :default_option?, <<~PATTERN
|
35
36
|
(pair (sym :default) !nil)
|
36
37
|
PATTERN
|
37
38
|
|
@@ -43,17 +44,20 @@ module RuboCop
|
|
43
44
|
private
|
44
45
|
|
45
46
|
def check_add_column(node)
|
46
|
-
|
47
|
-
|
47
|
+
add_not_null_column?(node) do |type, pairs|
|
48
|
+
return if type.value == :virtual || type.value == 'virtual'
|
49
|
+
|
50
|
+
check_pairs(pairs)
|
51
|
+
end
|
48
52
|
end
|
49
53
|
|
50
54
|
def check_add_reference(node)
|
51
|
-
|
52
|
-
|
55
|
+
add_not_null_reference?(node) do |pairs|
|
56
|
+
check_pairs(pairs)
|
57
|
+
end
|
53
58
|
end
|
54
59
|
|
55
60
|
def check_pairs(pairs)
|
56
|
-
return unless pairs
|
57
61
|
return if pairs.any? { |pair| default_option?(pair) }
|
58
62
|
|
59
63
|
null_false = pairs.find { |pair| null_false?(pair) }
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Checks for places where ordering by `id` column is used.
|
7
|
+
#
|
8
|
+
# Don't use the `id` column for ordering. The sequence of ids is not guaranteed
|
9
|
+
# to be in any particular order, despite often (incidentally) being chronological.
|
10
|
+
# Use a timestamp column to order chronologically. As a bonus the intent is clearer.
|
11
|
+
#
|
12
|
+
# NOTE: Make sure the changed order column does not introduce performance
|
13
|
+
# bottlenecks and appropriate database indexes are added.
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
# # bad
|
17
|
+
# scope :chronological, -> { order(id: :asc) }
|
18
|
+
# scope :chronological, -> { order(primary_key => :asc) }
|
19
|
+
#
|
20
|
+
# # good
|
21
|
+
# scope :chronological, -> { order(created_at: :asc) }
|
22
|
+
#
|
23
|
+
class OrderById < Base
|
24
|
+
include RangeHelp
|
25
|
+
|
26
|
+
MSG = 'Do not use the `id` column for ordering. Use a timestamp column to order chronologically.'
|
27
|
+
RESTRICT_ON_SEND = %i[order].freeze
|
28
|
+
|
29
|
+
def_node_matcher :order_by_id?, <<~PATTERN
|
30
|
+
(send _ :order
|
31
|
+
{
|
32
|
+
(sym :id)
|
33
|
+
(hash (pair (sym :id) _))
|
34
|
+
(send _ :primary_key)
|
35
|
+
(hash (pair (send _ :primary_key) _))
|
36
|
+
})
|
37
|
+
PATTERN
|
38
|
+
|
39
|
+
def on_send(node)
|
40
|
+
add_offense(offense_range(node)) if order_by_id?(node)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def offense_range(node)
|
46
|
+
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -3,7 +3,11 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
#
|
6
|
+
# Checks for the use of output calls like puts and print
|
7
|
+
#
|
8
|
+
# @safety
|
9
|
+
# This cop's autocorrection is unsafe because depending on the Rails log level configuration,
|
10
|
+
# changing from `puts` to `Rails.logger.debug` could result in no output being shown.
|
7
11
|
#
|
8
12
|
# @example
|
9
13
|
# # bad
|
@@ -13,29 +17,36 @@ module RuboCop
|
|
13
17
|
#
|
14
18
|
# # good
|
15
19
|
# Rails.logger.debug 'A debug message'
|
16
|
-
class Output <
|
17
|
-
|
18
|
-
|
20
|
+
class Output < Base
|
21
|
+
include RangeHelp
|
22
|
+
extend AutoCorrector
|
23
|
+
|
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
|
19
26
|
|
20
|
-
def_node_matcher :output?,
|
27
|
+
def_node_matcher :output?, <<~PATTERN
|
21
28
|
(send nil? {:ap :p :pp :pretty_print :print :puts} ...)
|
22
29
|
PATTERN
|
23
30
|
|
24
|
-
def_node_matcher :io_output?,
|
31
|
+
def_node_matcher :io_output?, <<~PATTERN
|
25
32
|
(send
|
26
33
|
{
|
27
34
|
(gvar #match_gvar?)
|
28
|
-
|
35
|
+
(const {nil? cbase} {:STDOUT :STDERR})
|
29
36
|
}
|
30
37
|
{:binwrite :syswrite :write :write_nonblock}
|
31
38
|
...)
|
32
39
|
PATTERN
|
33
40
|
|
34
41
|
def on_send(node)
|
35
|
-
return
|
36
|
-
|
42
|
+
return if node.parent&.call_type?
|
43
|
+
return unless output?(node) || io_output?(node)
|
44
|
+
|
45
|
+
range = offense_range(node)
|
37
46
|
|
38
|
-
add_offense(
|
47
|
+
add_offense(range) do |corrector|
|
48
|
+
corrector.replace(range, 'Rails.logger.debug')
|
49
|
+
end
|
39
50
|
end
|
40
51
|
|
41
52
|
private
|
@@ -43,6 +54,14 @@ module RuboCop
|
|
43
54
|
def match_gvar?(sym)
|
44
55
|
%i[$stdout $stderr].include?(sym)
|
45
56
|
end
|
57
|
+
|
58
|
+
def offense_range(node)
|
59
|
+
if node.receiver
|
60
|
+
range_between(node.source_range.begin_pos, node.loc.selector.end_pos)
|
61
|
+
else
|
62
|
+
node.loc.selector
|
63
|
+
end
|
64
|
+
end
|
46
65
|
end
|
47
66
|
end
|
48
67
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
#
|
6
|
+
# Checks for the use of output safety calls like `html_safe`,
|
7
7
|
# `raw`, and `safe_concat`. These methods do not escape content. They
|
8
8
|
# simply return a SafeBuffer containing the content as is. Instead,
|
9
9
|
# use `safe_join` to join content and escape it and concat to
|
@@ -62,17 +62,22 @@ module RuboCop
|
|
62
62
|
# safe_join([user_content, " ", content_tag(:span, user_content)])
|
63
63
|
# # => ActiveSupport::SafeBuffer
|
64
64
|
# # "<b>hi</b> <span><b>hi</b></span>"
|
65
|
-
class OutputSafety <
|
65
|
+
class OutputSafety < Base
|
66
66
|
MSG = 'Tagging a string as html safe may be a security risk.'
|
67
|
+
RESTRICT_ON_SEND = %i[html_safe raw safe_concat].freeze
|
68
|
+
|
69
|
+
def_node_search :i18n_method?, <<~PATTERN
|
70
|
+
(send {nil? (const {nil? cbase} :I18n)} {:t :translate :l :localize} ...)
|
71
|
+
PATTERN
|
67
72
|
|
68
73
|
def on_send(node)
|
69
|
-
return if non_interpolated_string?(node)
|
74
|
+
return if non_interpolated_string?(node) || i18n_method?(node)
|
70
75
|
|
71
76
|
return unless looks_like_rails_html_safe?(node) ||
|
72
77
|
looks_like_rails_raw?(node) ||
|
73
78
|
looks_like_rails_safe_concat?(node)
|
74
79
|
|
75
|
-
add_offense(node
|
80
|
+
add_offense(node.loc.selector)
|
76
81
|
end
|
77
82
|
alias on_csend on_send
|
78
83
|
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Enforces the use of `pick` over `pluck(...).first`.
|
7
|
+
#
|
8
|
+
# Using `pluck` followed by `first` creates an intermediate array, which
|
9
|
+
# `pick` avoids. When called on an Active Record relation, `pick` adds a
|
10
|
+
# limit to the query so that only one value is fetched from the database.
|
11
|
+
#
|
12
|
+
# @safety
|
13
|
+
# This cop is unsafe because `pluck` is defined on both `ActiveRecord::Relation` and `Enumerable`,
|
14
|
+
# whereas `pick` is only defined on `ActiveRecord::Relation` in Rails 6.0. This was addressed
|
15
|
+
# in Rails 6.1 via rails/rails#38760, at which point the cop is safe.
|
16
|
+
#
|
17
|
+
# See: https://github.com/rubocop/rubocop-rails/pull/249
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# # bad
|
21
|
+
# Model.pluck(:a).first
|
22
|
+
# [{ a: :b, c: :d }].pluck(:a, :b).first
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# Model.pick(:a)
|
26
|
+
# [{ a: :b, c: :d }].pick(:a, :b)
|
27
|
+
class Pick < Base
|
28
|
+
extend AutoCorrector
|
29
|
+
extend TargetRailsVersion
|
30
|
+
|
31
|
+
MSG = 'Prefer `pick(%<args>s)` over `pluck(%<args>s).first`.'
|
32
|
+
RESTRICT_ON_SEND = %i[first].freeze
|
33
|
+
|
34
|
+
minimum_target_rails_version 6.0
|
35
|
+
|
36
|
+
def_node_matcher :pick_candidate?, <<~PATTERN
|
37
|
+
(send (send _ :pluck ...) :first)
|
38
|
+
PATTERN
|
39
|
+
|
40
|
+
def on_send(node)
|
41
|
+
pick_candidate?(node) do
|
42
|
+
receiver = node.receiver
|
43
|
+
receiver_selector = receiver.loc.selector
|
44
|
+
node_selector = node.loc.selector
|
45
|
+
range = receiver_selector.join(node_selector)
|
46
|
+
|
47
|
+
add_offense(range, message: message(receiver)) do |corrector|
|
48
|
+
first_range = receiver.source_range.end.join(node_selector)
|
49
|
+
|
50
|
+
corrector.remove(first_range)
|
51
|
+
corrector.replace(receiver_selector, 'pick')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def message(receiver)
|
59
|
+
format(MSG, args: receiver.arguments.map(&:source).join(', '))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Enforces the use of `pluck` over `map`.
|
7
|
+
#
|
8
|
+
# `pluck` can be used instead of `map` to extract a single key from each
|
9
|
+
# element in an enumerable. When called on an Active Record relation, it
|
10
|
+
# results in a more efficient query that only selects the necessary key.
|
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
|
+
#
|
24
|
+
# @example
|
25
|
+
# # bad
|
26
|
+
# Post.published.map { |post| post[:title] }
|
27
|
+
# [{ a: :b, c: :d }].collect { |el| el[:a] }
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# Post.published.pluck(:title)
|
31
|
+
# [{ a: :b, c: :d }].pluck(:a)
|
32
|
+
class Pluck < Base
|
33
|
+
extend AutoCorrector
|
34
|
+
extend TargetRailsVersion
|
35
|
+
|
36
|
+
MSG = 'Prefer `%<replacement>s` over `%<current>s`.'
|
37
|
+
|
38
|
+
minimum_target_rails_version 5.0
|
39
|
+
|
40
|
+
def_node_matcher :pluck_candidate?, <<~PATTERN
|
41
|
+
({block numblock} (send _ {:map :collect}) $_argument (send lvar :[] $_key))
|
42
|
+
PATTERN
|
43
|
+
|
44
|
+
def on_block(node)
|
45
|
+
pluck_candidate?(node) do |argument, key|
|
46
|
+
next if key.regexp_type? || !use_one_block_argument?(argument)
|
47
|
+
|
48
|
+
match = if node.block_type?
|
49
|
+
block_argument = argument.children.first.source
|
50
|
+
use_block_argument_in_key?(block_argument, key)
|
51
|
+
else # numblock
|
52
|
+
argument == 1 && use_block_argument_in_key?('_1', key)
|
53
|
+
end
|
54
|
+
next unless match
|
55
|
+
|
56
|
+
register_offense(node, key)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
alias on_numblock on_block
|
60
|
+
|
61
|
+
private
|
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
|
+
|
75
|
+
def offense_range(node)
|
76
|
+
node.send_node.loc.selector.join(node.loc.end)
|
77
|
+
end
|
78
|
+
|
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)
|
89
|
+
current = offense_range(node).source
|
90
|
+
|
91
|
+
format(MSG, replacement: replacement, current: current)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Enforces the use of `ids` over `pluck(:id)` and `pluck(primary_key)`.
|
7
|
+
#
|
8
|
+
# @safety
|
9
|
+
# This cop is unsafe if the receiver object is not an Active Record object.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# # bad
|
13
|
+
# User.pluck(:id)
|
14
|
+
# user.posts.pluck(:id)
|
15
|
+
#
|
16
|
+
# def self.user_ids
|
17
|
+
# pluck(primary_key)
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# # good
|
21
|
+
# User.ids
|
22
|
+
# user.posts.ids
|
23
|
+
#
|
24
|
+
# def self.user_ids
|
25
|
+
# ids
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
class PluckId < Base
|
29
|
+
include RangeHelp
|
30
|
+
include ActiveRecordHelper
|
31
|
+
extend AutoCorrector
|
32
|
+
|
33
|
+
MSG = 'Use `ids` instead of `%<bad_method>s`.'
|
34
|
+
RESTRICT_ON_SEND = %i[pluck].freeze
|
35
|
+
|
36
|
+
def_node_matcher :pluck_id_call?, <<~PATTERN
|
37
|
+
(send _ :pluck {(sym :id) (send nil? :primary_key)})
|
38
|
+
PATTERN
|
39
|
+
|
40
|
+
def on_send(node)
|
41
|
+
return if !pluck_id_call?(node) || in_where?(node)
|
42
|
+
|
43
|
+
range = offense_range(node)
|
44
|
+
message = format(MSG, bad_method: range.source)
|
45
|
+
|
46
|
+
add_offense(range, message: message) do |corrector|
|
47
|
+
corrector.replace(offense_range(node), 'ids')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def offense_range(node)
|
54
|
+
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|