rubocop-rails 2.22.2 → 2.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -9
  3. data/config/default.yml +13 -4
  4. data/lib/rubocop/cop/mixin/active_record_helper.rb +15 -3
  5. data/lib/rubocop/cop/mixin/database_type_resolvable.rb +1 -1
  6. data/lib/rubocop/cop/mixin/target_rails_version.rb +29 -2
  7. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +2 -0
  8. data/lib/rubocop/cop/rails/active_support_aliases.rb +6 -5
  9. data/lib/rubocop/cop/rails/active_support_on_load.rb +21 -1
  10. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  11. data/lib/rubocop/cop/rails/content_tag.rb +1 -1
  12. data/lib/rubocop/cop/rails/dangerous_column_names.rb +1 -2
  13. data/lib/rubocop/cop/rails/expanded_date_range.rb +1 -1
  14. data/lib/rubocop/cop/rails/find_by.rb +3 -3
  15. data/lib/rubocop/cop/rails/find_by_id.rb +9 -23
  16. data/lib/rubocop/cop/rails/http_status.rb +12 -2
  17. data/lib/rubocop/cop/rails/inquiry.rb +1 -0
  18. data/lib/rubocop/cop/rails/not_null_column.rb +91 -13
  19. data/lib/rubocop/cop/rails/pick.rb +10 -5
  20. data/lib/rubocop/cop/rails/pluck.rb +1 -1
  21. data/lib/rubocop/cop/rails/pluck_id.rb +2 -1
  22. data/lib/rubocop/cop/rails/pluck_in_where.rb +18 -5
  23. data/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +1 -2
  24. data/lib/rubocop/cop/rails/response_parsed_body.rb +52 -10
  25. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -1
  26. data/lib/rubocop/cop/rails/save_bang.rb +2 -0
  27. data/lib/rubocop/cop/rails/skips_model_validations.rb +1 -1
  28. data/lib/rubocop/cop/rails/time_zone.rb +2 -1
  29. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +12 -4
  30. data/lib/rubocop/cop/rails/unknown_env.rb +1 -1
  31. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +6 -0
  32. data/lib/rubocop/cop/rails/validation.rb +5 -3
  33. data/lib/rubocop/cop/rails/where_equals.rb +3 -2
  34. data/lib/rubocop/cop/rails/where_exists.rb +9 -8
  35. data/lib/rubocop/cop/rails/where_missing.rb +6 -2
  36. data/lib/rubocop/cop/rails/where_not.rb +8 -6
  37. data/lib/rubocop/cop/rails/where_range.rb +157 -0
  38. data/lib/rubocop/cop/rails_cops.rb +1 -0
  39. data/lib/rubocop/rails/schema_loader/schema.rb +1 -0
  40. data/lib/rubocop/rails/schema_loader.rb +5 -15
  41. data/lib/rubocop/rails/version.rb +1 -1
  42. metadata +7 -6
@@ -9,6 +9,10 @@ module RuboCop
9
9
  # `pick` avoids. When called on an Active Record relation, `pick` adds a
10
10
  # limit to the query so that only one value is fetched from the database.
11
11
  #
12
+ # Note that when `pick` is added to a relation with an existing limit, it
13
+ # causes a subquery to be added. In most cases this is undesirable, and
14
+ # care should be taken while resolving this violation.
15
+ #
12
16
  # @safety
13
17
  # This cop is unsafe because `pluck` is defined on both `ActiveRecord::Relation` and `Enumerable`,
14
18
  # whereas `pick` is only defined on `ActiveRecord::Relation` in Rails 6.0. This was addressed
@@ -28,13 +32,13 @@ module RuboCop
28
32
  extend AutoCorrector
29
33
  extend TargetRailsVersion
30
34
 
31
- MSG = 'Prefer `pick(%<args>s)` over `pluck(%<args>s).first`.'
35
+ MSG = 'Prefer `pick(%<args>s)` over `%<current>s`.'
32
36
  RESTRICT_ON_SEND = %i[first].freeze
33
37
 
34
38
  minimum_target_rails_version 6.0
35
39
 
36
40
  def_node_matcher :pick_candidate?, <<~PATTERN
37
- (send (send _ :pluck ...) :first)
41
+ (call (call _ :pluck ...) :first)
38
42
  PATTERN
39
43
 
40
44
  def on_send(node)
@@ -44,7 +48,7 @@ module RuboCop
44
48
  node_selector = node.loc.selector
45
49
  range = receiver_selector.join(node_selector)
46
50
 
47
- add_offense(range, message: message(receiver)) do |corrector|
51
+ add_offense(range, message: message(receiver, range)) do |corrector|
48
52
  first_range = receiver.source_range.end.join(node_selector)
49
53
 
50
54
  corrector.remove(first_range)
@@ -52,11 +56,12 @@ module RuboCop
52
56
  end
53
57
  end
54
58
  end
59
+ alias on_csend on_send
55
60
 
56
61
  private
57
62
 
58
- def message(receiver)
59
- format(MSG, args: receiver.arguments.map(&:source).join(', '))
63
+ def message(receiver, current)
64
+ format(MSG, args: receiver.arguments.map(&:source).join(', '), current: current.source)
60
65
  end
61
66
  end
62
67
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  minimum_target_rails_version 5.0
39
39
 
40
40
  def_node_matcher :pluck_candidate?, <<~PATTERN
41
- ({block numblock} (send _ {:map :collect}) $_argument (send lvar :[] $_key))
41
+ ({block numblock} (call _ {:map :collect}) $_argument (send lvar :[] $_key))
42
42
  PATTERN
43
43
 
44
44
  def on_block(node)
@@ -34,7 +34,7 @@ module RuboCop
34
34
  RESTRICT_ON_SEND = %i[pluck].freeze
35
35
 
36
36
  def_node_matcher :pluck_id_call?, <<~PATTERN
37
- (send _ :pluck {(sym :id) (send nil? :primary_key)})
37
+ (call _ :pluck {(sym :id) (send nil? :primary_key)})
38
38
  PATTERN
39
39
 
40
40
  def on_send(node)
@@ -47,6 +47,7 @@ module RuboCop
47
47
  corrector.replace(offense_range(node), 'ids')
48
48
  end
49
49
  end
50
+ alias on_csend on_send
50
51
 
51
52
  private
52
53
 
@@ -22,10 +22,13 @@ module RuboCop
22
22
  # @example
23
23
  # # bad
24
24
  # Post.where(user_id: User.active.pluck(:id))
25
+ # Post.where(user_id: User.active.ids)
26
+ # Post.where.not(user_id: User.active.pluck(:id))
25
27
  #
26
28
  # # good
27
29
  # Post.where(user_id: User.active.select(:id))
28
30
  # Post.where(user_id: active_users.select(:id))
31
+ # Post.where.not(user_id: active_users.select(:id))
29
32
  #
30
33
  # @example EnforcedStyle: conservative (default)
31
34
  # # good
@@ -40,8 +43,9 @@ module RuboCop
40
43
  include ConfigurableEnforcedStyle
41
44
  extend AutoCorrector
42
45
 
43
- MSG = 'Use `select` instead of `pluck` within `where` query method.'
44
- RESTRICT_ON_SEND = %i[pluck].freeze
46
+ MSG_SELECT = 'Use `select` instead of `pluck` within `where` query method.'
47
+ MSG_IDS = 'Use `select(:id)` instead of `ids` within `where` query method.'
48
+ RESTRICT_ON_SEND = %i[pluck ids].freeze
45
49
 
46
50
  def on_send(node)
47
51
  return unless in_where?(node)
@@ -49,17 +53,26 @@ module RuboCop
49
53
 
50
54
  range = node.loc.selector
51
55
 
52
- add_offense(range) do |corrector|
53
- corrector.replace(range, 'select')
56
+ if node.method?(:ids)
57
+ replacement = 'select(:id)'
58
+ message = MSG_IDS
59
+ else
60
+ replacement = 'select'
61
+ message = MSG_SELECT
62
+ end
63
+
64
+ add_offense(range, message: message) do |corrector|
65
+ corrector.replace(range, replacement)
54
66
  end
55
67
  end
68
+ alias on_csend on_send
56
69
 
57
70
  private
58
71
 
59
72
  def root_receiver(node)
60
73
  receiver = node.receiver
61
74
 
62
- if receiver&.send_type?
75
+ if receiver&.call_type?
63
76
  root_receiver(receiver)
64
77
  else
65
78
  receiver
@@ -34,8 +34,7 @@ module RuboCop
34
34
 
35
35
  # Detect redundant `all` used as a receiver for Active Record query methods.
36
36
  #
37
- # NOTE: For the methods `delete_all` and `destroy_all`,
38
- # this cop will only check cases where the receiver is a model.
37
+ # For the methods `delete_all` and `destroy_all`, this cop will only check cases where the receiver is a model.
39
38
  # It will ignore cases where the receiver is an association (e.g., `user.articles.all.delete_all`).
40
39
  # This is because omitting `all` from an association changes the methods
41
40
  # from `ActiveRecord::Relation` to `ActiveRecord::Associations::CollectionProxy`,
@@ -3,25 +3,30 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # Prefer `response.parsed_body` to `JSON.parse(response.body)`.
6
+ # Prefer `response.parsed_body` to custom parsing logic for `response.body`.
7
7
  #
8
8
  # @safety
9
- # This cop is unsafe because Content-Type may not be `application/json`. For example, the proprietary
10
- # Content-Type provided by corporate entities such as `application/vnd.github+json` is not supported at
11
- # `response.parsed_body` by default, so you still have to use `JSON.parse(response.body)` there.
9
+ # This cop is unsafe because Content-Type may not be `application/json` or `text/html`.
10
+ # For example, the proprietary Content-Type provided by corporate entities such as
11
+ # `application/vnd.github+json` is not supported at `response.parsed_body` by default,
12
+ # so you still have to use `JSON.parse(response.body)` there.
12
13
  #
13
14
  # @example
14
15
  # # bad
15
16
  # JSON.parse(response.body)
16
17
  #
18
+ # # bad
19
+ # Nokogiri::HTML.parse(response.body)
20
+ #
21
+ # # bad
22
+ # Nokogiri::HTML5.parse(response.body)
23
+ #
17
24
  # # good
18
25
  # response.parsed_body
19
26
  class ResponseParsedBody < Base
20
27
  extend AutoCorrector
21
28
  extend TargetRailsVersion
22
29
 
23
- MSG = 'Prefer `response.parsed_body` to `JSON.parse(response.body)`.'
24
-
25
30
  RESTRICT_ON_SEND = %i[parse].freeze
26
31
 
27
32
  minimum_target_rails_version 5.0
@@ -38,12 +43,27 @@ module RuboCop
38
43
  )
39
44
  PATTERN
40
45
 
46
+ # @!method nokogiri_html_parse_response_body(node)
47
+ def_node_matcher :nokogiri_html_parse_response_body, <<~PATTERN
48
+ (send
49
+ (const
50
+ (const {nil? cbase} :Nokogiri)
51
+ ${:HTML :HTML5}
52
+ )
53
+ :parse
54
+ (send
55
+ (send nil? :response)
56
+ :body
57
+ )
58
+ )
59
+ PATTERN
60
+
41
61
  def on_send(node)
42
- return unless json_parse_response_body?(node)
62
+ check_json_parse_response_body(node)
43
63
 
44
- add_offense(node) do |corrector|
45
- autocorrect(corrector, node)
46
- end
64
+ return unless target_rails_version >= 7.1
65
+
66
+ check_nokogiri_html_parse_response_body(node)
47
67
  end
48
68
 
49
69
  private
@@ -51,6 +71,28 @@ module RuboCop
51
71
  def autocorrect(corrector, node)
52
72
  corrector.replace(node, 'response.parsed_body')
53
73
  end
74
+
75
+ def check_json_parse_response_body(node)
76
+ return unless json_parse_response_body?(node)
77
+
78
+ add_offense(
79
+ node,
80
+ message: 'Prefer `response.parsed_body` to `JSON.parse(response.body)`.'
81
+ ) do |corrector|
82
+ autocorrect(corrector, node)
83
+ end
84
+ end
85
+
86
+ def check_nokogiri_html_parse_response_body(node)
87
+ return unless (const = nokogiri_html_parse_response_body(node))
88
+
89
+ add_offense(
90
+ node,
91
+ message: "Prefer `response.parsed_body` to `Nokogiri::#{const}.parse(response.body)`."
92
+ ) do |corrector|
93
+ autocorrect(corrector, node)
94
+ end
95
+ end
54
96
  end
55
97
  end
56
98
  end
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # # good
18
18
  # def change
19
19
  # change_table :users do |t|
20
- # t.remove :name, :string
20
+ # t.remove :name, type: :string
21
21
  # end
22
22
  # end
23
23
  #
@@ -196,6 +196,8 @@ module RuboCop
196
196
  end
197
197
 
198
198
  def call_to_persisted?(node)
199
+ node = node.parent.condition if node.parenthesized_call? && node.parent.if_type?
200
+
199
201
  node.send_type? && node.method?(:persisted?)
200
202
  end
201
203
 
@@ -63,7 +63,7 @@ module RuboCop
63
63
  PATTERN
64
64
 
65
65
  def_node_matcher :good_insert?, <<~PATTERN
66
- (send _ {:insert :insert!} _ {
66
+ (call _ {:insert :insert!} _ {
67
67
  !(hash ...)
68
68
  (hash <(pair (sym !{:returning :unique_by}) _) ...>)
69
69
  } ...)
@@ -69,9 +69,10 @@ module RuboCop
69
69
  return if !node.receiver&.str_type? || !node.method?(:to_time)
70
70
 
71
71
  add_offense(node.loc.selector, message: MSG_STRING_TO_TIME) do |corrector|
72
- corrector.replace(node, "Time.zone.parse(#{node.receiver.source})")
72
+ corrector.replace(node, "Time.zone.parse(#{node.receiver.source})") unless node.csend_type?
73
73
  end
74
74
  end
75
+ alias on_csend on_send
75
76
 
76
77
  private
77
78
 
@@ -68,15 +68,23 @@ module RuboCop
68
68
  return unless uniq
69
69
 
70
70
  add_offense(node.loc.selector) do |corrector|
71
- method = node.method_name
72
-
73
- corrector.remove(dot_method_with_whitespace(method, node))
74
- corrector.insert_before(node.receiver.loc.dot.begin, '.distinct')
71
+ autocorrect(corrector, node)
75
72
  end
76
73
  end
77
74
 
78
75
  private
79
76
 
77
+ def autocorrect(corrector, node)
78
+ method = node.method_name
79
+
80
+ corrector.remove(dot_method_with_whitespace(method, node))
81
+ if (dot = node.receiver.loc.dot)
82
+ corrector.insert_before(dot.begin, '.distinct')
83
+ else
84
+ corrector.insert_before(node.receiver, 'distinct.')
85
+ end
86
+ end
87
+
80
88
  def dot_method_with_whitespace(method, node)
81
89
  range_between(dot_method_begin_pos(method, node), node.loc.selector.end_pos)
82
90
  end
@@ -87,7 +87,7 @@ module RuboCop
87
87
 
88
88
  def environments
89
89
  @environments ||= begin
90
- environments = cop_config['Environments'] || []
90
+ environments = cop_config['Environments'].dup || []
91
91
  environments << 'local' if target_rails_version >= 7.1
92
92
  environments
93
93
  end
@@ -7,6 +7,12 @@ module RuboCop
7
7
  # `ignored_columns` is necessary to drop a column from RDBMS, but you don't need it after the migration
8
8
  # to drop the column. You avoid forgetting to remove `ignored_columns` by this cop.
9
9
  #
10
+ # IMPORTANT: This cop can't be used to effectively check for unused columns because the development
11
+ # and production schema can be out of sync until the migration has been run on production. As such,
12
+ # this cop can cause `ignored_columns` to be removed even though the production schema still contains
13
+ # the column, which can lead to downtime when the migration is actually executed. Only enable this cop
14
+ # if you know your migrations will be run before any of your Rails applications boot with the modified code.
15
+ #
10
16
  # @example
11
17
  # # bad
12
18
  # class User < ApplicationRecord
@@ -29,7 +29,7 @@ module RuboCop
29
29
  # validates :foo, numericality: true
30
30
  # validates :foo, presence: true
31
31
  # validates :foo, absence: true
32
- # validates :foo, size: true
32
+ # validates :foo, length: true
33
33
  # validates :foo, uniqueness: true
34
34
  #
35
35
  class Validation < Base
@@ -51,7 +51,7 @@ module RuboCop
51
51
  uniqueness
52
52
  ].freeze
53
53
 
54
- RESTRICT_ON_SEND = TYPES.map { |p| "validates_#{p}_of".to_sym }.freeze
54
+ RESTRICT_ON_SEND = TYPES.map { |p| :"validates_#{p}_of" }.freeze
55
55
  ALLOWLIST = TYPES.map { |p| "validates :column, #{p}: value" }.freeze
56
56
 
57
57
  def on_send(node)
@@ -120,7 +120,9 @@ module RuboCop
120
120
  end
121
121
 
122
122
  def validate_type(node)
123
- node.method_name.to_s.split('_')[1]
123
+ type = node.method_name.to_s.split('_')[1]
124
+
125
+ type == 'size' ? 'length' : type
124
126
  end
125
127
 
126
128
  def frozen_array_argument?(argument)
@@ -33,8 +33,8 @@ module RuboCop
33
33
 
34
34
  def_node_matcher :where_method_call?, <<~PATTERN
35
35
  {
36
- (send _ :where (array $str_type? $_ ?))
37
- (send _ :where $str_type? $_ ?)
36
+ (call _ :where (array $str_type? $_ ?))
37
+ (call _ :where $str_type? $_ ?)
38
38
  }
39
39
  PATTERN
40
40
 
@@ -55,6 +55,7 @@ module RuboCop
55
55
  end
56
56
  end
57
57
  end
58
+ alias on_csend on_send
58
59
 
59
60
  EQ_ANONYMOUS_RE = /\A([\w.]+)\s+=\s+\?\z/.freeze # column = ?
60
61
  IN_ANONYMOUS_RE = /\A([\w.]+)\s+IN\s+\(\?\)\z/i.freeze # column IN (?)
@@ -55,11 +55,11 @@ module RuboCop
55
55
  RESTRICT_ON_SEND = %i[exists?].freeze
56
56
 
57
57
  def_node_matcher :where_exists_call?, <<~PATTERN
58
- (send (send _ :where $...) :exists?)
58
+ (call (call _ :where $...) :exists?)
59
59
  PATTERN
60
60
 
61
61
  def_node_matcher :exists_with_args?, <<~PATTERN
62
- (send _ :exists? $...)
62
+ (call _ :exists? $...)
63
63
  PATTERN
64
64
 
65
65
  def on_send(node)
@@ -67,7 +67,7 @@ module RuboCop
67
67
  return unless convertable_args?(args)
68
68
 
69
69
  range = correction_range(node)
70
- good_method = build_good_method(args)
70
+ good_method = build_good_method(args, dot: node.loc.dot)
71
71
  message = format(MSG, good_method: good_method, bad_method: range.source)
72
72
 
73
73
  add_offense(range, message: message) do |corrector|
@@ -75,6 +75,7 @@ module RuboCop
75
75
  end
76
76
  end
77
77
  end
78
+ alias on_csend on_send
78
79
 
79
80
  private
80
81
 
@@ -108,11 +109,11 @@ module RuboCop
108
109
  end
109
110
  end
110
111
 
111
- def build_good_method(args)
112
+ def build_good_method(args, dot:)
112
113
  if exists_style?
113
114
  build_good_method_exists(args)
114
115
  elsif where_style?
115
- build_good_method_where(args)
116
+ build_good_method_where(args, dot&.source || '.')
116
117
  end
117
118
  end
118
119
 
@@ -124,11 +125,11 @@ module RuboCop
124
125
  end
125
126
  end
126
127
 
127
- def build_good_method_where(args)
128
+ def build_good_method_where(args, dot_source)
128
129
  if args.size > 1
129
- "where(#{args.map(&:source).join(', ')}).exists?"
130
+ "where(#{args.map(&:source).join(', ')})#{dot_source}exists?"
130
131
  else
131
- "where(#{args[0].source}).exists?"
132
+ "where(#{args[0].source})#{dot_source}exists?"
132
133
  end
133
134
  end
134
135
  end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  PATTERN
37
37
 
38
38
  def on_send(node)
39
- return unless node.first_argument.sym_type?
39
+ return unless node.first_argument&.sym_type?
40
40
 
41
41
  root_receiver = root_receiver(node)
42
42
  where_node_and_argument(root_receiver) do |where_node, where_argument|
@@ -89,16 +89,20 @@ module RuboCop
89
89
  end
90
90
  end
91
91
 
92
+ # rubocop:disable Metrics/AbcSize
92
93
  def remove_where_method(corrector, node, where_node)
93
94
  range = range_between(where_node.loc.selector.begin_pos, where_node.loc.end.end_pos)
94
95
  if node.multiline? && !same_line?(node, where_node)
95
96
  range = range_by_whole_lines(range, include_final_newline: true)
96
- else
97
+ elsif where_node.receiver
97
98
  corrector.remove(where_node.loc.dot)
99
+ else
100
+ corrector.remove(node.loc.dot)
98
101
  end
99
102
 
100
103
  corrector.remove(range)
101
104
  end
105
+ # rubocop:enable Metrics/AbcSize
102
106
 
103
107
  def same_line?(left_joins_node, where_node)
104
108
  left_joins_node.loc.selector.line == where_node.loc.selector.line
@@ -32,8 +32,8 @@ module RuboCop
32
32
 
33
33
  def_node_matcher :where_method_call?, <<~PATTERN
34
34
  {
35
- (send _ :where (array $str_type? $_ ?))
36
- (send _ :where $str_type? $_ ?)
35
+ (call _ :where (array $str_type? $_ ?))
36
+ (call _ :where $str_type? $_ ?)
37
37
  }
38
38
  PATTERN
39
39
 
@@ -46,7 +46,7 @@ module RuboCop
46
46
  column_and_value = extract_column_and_value(template_node, value_node)
47
47
  return unless column_and_value
48
48
 
49
- good_method = build_good_method(*column_and_value)
49
+ good_method = build_good_method(node.loc.dot&.source, *column_and_value)
50
50
  message = format(MSG, good_method: good_method)
51
51
 
52
52
  add_offense(range, message: message) do |corrector|
@@ -54,6 +54,7 @@ module RuboCop
54
54
  end
55
55
  end
56
56
  end
57
+ alias on_csend on_send
57
58
 
58
59
  NOT_EQ_ANONYMOUS_RE = /\A([\w.]+)\s+(?:!=|<>)\s+\?\z/.freeze # column != ?, column <> ?
59
60
  NOT_IN_ANONYMOUS_RE = /\A([\w.]+)\s+NOT\s+IN\s+\(\?\)\z/i.freeze # column NOT IN (?)
@@ -86,13 +87,14 @@ module RuboCop
86
87
  [Regexp.last_match(1), value]
87
88
  end
88
89
 
89
- def build_good_method(column, value)
90
+ def build_good_method(dot, column, value)
91
+ dot ||= '.'
90
92
  if column.include?('.')
91
93
  table, column = column.split('.')
92
94
 
93
- "where.not(#{table}: { #{column}: #{value} })"
95
+ "where#{dot}not(#{table}: { #{column}: #{value} })"
94
96
  else
95
- "where.not(#{column}: #{value})"
97
+ "where#{dot}not(#{column}: #{value})"
96
98
  end
97
99
  end
98
100
  end