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.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +23 -2
  4. data/config/default.yml +181 -13
  5. data/config/obsoletion.yml +10 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +3 -6
  7. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +1 -3
  8. data/lib/rubocop/cop/mixin/enforce_superclass.rb +1 -1
  9. data/lib/rubocop/cop/mixin/index_method.rb +7 -17
  10. data/lib/rubocop/cop/mixin/migrations_helper.rb +1 -1
  11. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +112 -0
  12. data/lib/rubocop/cop/rails/action_controller_test_case.rb +2 -2
  13. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  14. data/lib/rubocop/cop/rails/action_order.rb +116 -0
  15. data/lib/rubocop/cop/rails/active_record_aliases.rb +3 -4
  16. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +6 -3
  17. data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
  18. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  19. data/lib/rubocop/cop/rails/add_column_index.rb +2 -5
  20. data/lib/rubocop/cop/rails/application_controller.rb +1 -1
  21. data/lib/rubocop/cop/rails/application_job.rb +2 -2
  22. data/lib/rubocop/cop/rails/application_mailer.rb +1 -1
  23. data/lib/rubocop/cop/rails/application_record.rb +1 -1
  24. data/lib/rubocop/cop/rails/arel_star.rb +1 -1
  25. data/lib/rubocop/cop/rails/assert_not.rb +1 -2
  26. data/lib/rubocop/cop/rails/belongs_to.rb +1 -4
  27. data/lib/rubocop/cop/rails/blank.rb +6 -7
  28. data/lib/rubocop/cop/rails/bulk_change_table.rb +7 -24
  29. data/lib/rubocop/cop/rails/compact_blank.rb +5 -1
  30. data/lib/rubocop/cop/rails/content_tag.rb +5 -6
  31. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -3
  32. data/lib/rubocop/cop/rails/date.rb +15 -11
  33. data/lib/rubocop/cop/rails/delegate.rb +19 -8
  34. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  35. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +18 -14
  36. data/lib/rubocop/cop/rails/dot_separated_keys.rb +2 -2
  37. data/lib/rubocop/cop/rails/duration_arithmetic.rb +3 -3
  38. data/lib/rubocop/cop/rails/dynamic_find_by.rb +25 -13
  39. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +5 -1
  40. data/lib/rubocop/cop/rails/enum_hash.rb +1 -1
  41. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -5
  42. data/lib/rubocop/cop/rails/environment_comparison.rb +2 -3
  43. data/lib/rubocop/cop/rails/file_path.rb +154 -27
  44. data/lib/rubocop/cop/rails/find_by_id.rb +2 -2
  45. data/lib/rubocop/cop/rails/find_each.rb +15 -5
  46. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  47. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +4 -6
  48. data/lib/rubocop/cop/rails/helper_instance_variable.rb +1 -1
  49. data/lib/rubocop/cop/rails/http_positional_arguments.rb +22 -11
  50. data/lib/rubocop/cop/rails/http_status.rb +6 -11
  51. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +2 -0
  52. data/lib/rubocop/cop/rails/i18n_locale_texts.rb +7 -3
  53. data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
  54. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +23 -12
  55. data/lib/rubocop/cop/rails/index_by.rb +1 -1
  56. data/lib/rubocop/cop/rails/index_with.rb +1 -1
  57. data/lib/rubocop/cop/rails/inverse_of.rb +3 -9
  58. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +21 -15
  59. data/lib/rubocop/cop/rails/link_to_blank.rb +1 -4
  60. data/lib/rubocop/cop/rails/mailer_name.rb +4 -4
  61. data/lib/rubocop/cop/rails/migration_class_name.rb +1 -1
  62. data/lib/rubocop/cop/rails/negate_include.rb +1 -1
  63. data/lib/rubocop/cop/rails/not_null_column.rb +9 -6
  64. data/lib/rubocop/cop/rails/output.rb +6 -8
  65. data/lib/rubocop/cop/rails/output_safety.rb +5 -1
  66. data/lib/rubocop/cop/rails/pluck.rb +44 -12
  67. data/lib/rubocop/cop/rails/pluck_id.rb +1 -1
  68. data/lib/rubocop/cop/rails/pluralization_grammar.rb +1 -2
  69. data/lib/rubocop/cop/rails/presence.rb +21 -12
  70. data/lib/rubocop/cop/rails/present.rb +8 -11
  71. data/lib/rubocop/cop/rails/rake_environment.rb +2 -2
  72. data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -1
  73. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +5 -7
  74. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +2 -2
  75. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +3 -3
  76. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +30 -26
  77. data/lib/rubocop/cop/rails/reflection_class_name.rb +34 -1
  78. data/lib/rubocop/cop/rails/refute_methods.rb +1 -6
  79. data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -7
  80. data/lib/rubocop/cop/rails/request_referer.rb +1 -2
  81. data/lib/rubocop/cop/rails/require_dependency.rb +1 -1
  82. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  83. data/lib/rubocop/cop/rails/reversible_migration.rb +14 -62
  84. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +1 -2
  85. data/lib/rubocop/cop/rails/root_join_chain.rb +1 -1
  86. data/lib/rubocop/cop/rails/root_pathname_methods.rb +238 -0
  87. data/lib/rubocop/cop/rails/safe_navigation.rb +1 -1
  88. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +1 -3
  89. data/lib/rubocop/cop/rails/save_bang.rb +10 -22
  90. data/lib/rubocop/cop/rails/short_i18n.rb +2 -5
  91. data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -3
  92. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +9 -7
  93. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +71 -0
  94. data/lib/rubocop/cop/rails/time_zone.rb +27 -25
  95. data/lib/rubocop/cop/rails/time_zone_assignment.rb +1 -1
  96. data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
  97. data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
  98. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +8 -3
  99. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -6
  100. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +22 -19
  101. data/lib/rubocop/cop/rails/unknown_env.rb +2 -4
  102. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +6 -1
  103. data/lib/rubocop/cop/rails/validation.rb +4 -12
  104. data/lib/rubocop/cop/rails/where_equals.rb +1 -1
  105. data/lib/rubocop/cop/rails/where_exists.rb +1 -1
  106. data/lib/rubocop/cop/rails/where_missing.rb +118 -0
  107. data/lib/rubocop/cop/rails/where_not.rb +1 -1
  108. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  109. data/lib/rubocop/cop/rails_cops.rb +12 -0
  110. data/lib/rubocop/rails/schema_loader/schema.rb +4 -4
  111. data/lib/rubocop/rails/version.rb +1 -1
  112. data/lib/rubocop/rails.rb +1 -1
  113. data/lib/rubocop-rails.rb +11 -0
  114. metadata +19 -9
  115. data/bin/console +0 -11
  116. data/bin/setup +0 -7
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Identifies top-level `HashWithIndifferentAccess`.
7
+ # This has been soft-deprecated since Rails 5.1.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # HashWithIndifferentAccess.new(foo: 'bar')
12
+ #
13
+ # # good
14
+ # ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
15
+ #
16
+ class TopLevelHashWithIndifferentAccess < Base
17
+ extend AutoCorrector
18
+ extend TargetRailsVersion
19
+
20
+ minimum_target_rails_version 5.1
21
+
22
+ MSG = 'Avoid top-level `HashWithIndifferentAccess`.'
23
+
24
+ # @!method top_level_hash_with_indifferent_access?(node)
25
+ # @param [RuboCop::AST::ConstNode] node
26
+ # @return [Boolean]
27
+ def_node_matcher :top_level_hash_with_indifferent_access?, <<~PATTERN
28
+ (const {nil? cbase} :HashWithIndifferentAccess)
29
+ PATTERN
30
+
31
+ # @param [RuboCop::AST::ConstNode] node
32
+ def on_const(node)
33
+ return unless top_level_hash_with_indifferent_access?(node)
34
+ return if node.parent&.class_type? && node.parent.ancestors.any?(&:module_type?)
35
+
36
+ add_offense(node) do |corrector|
37
+ autocorrect(corrector, node)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def autocorrect(corrector, node)
44
+ corrector.insert_before(node.location.name, 'ActiveSupport::')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -34,6 +34,11 @@ module RuboCop
34
34
  # throw if user.active?
35
35
  # end
36
36
  #
37
+ # # bad, as `with_lock` implicitly opens a transaction too
38
+ # ApplicationRecord.with_lock do
39
+ # break if user.active?
40
+ # end
41
+ #
37
42
  # # good
38
43
  # ApplicationRecord.transaction do
39
44
  # # Rollback
@@ -45,8 +50,6 @@ module RuboCop
45
50
  # # Commit
46
51
  # next if user.active?
47
52
  # end
48
- #
49
- # @see https://github.com/rails/rails/commit/15aa4200e083
50
53
  class TransactionExitStatement < Base
51
54
  MSG = <<~MSG.chomp
52
55
  Exit statement `%<statement>s` is not allowed. Use `raise` (rollback) or `next` (commit).
@@ -93,7 +96,9 @@ module RuboCop
93
96
  end
94
97
 
95
98
  def nested_block?(statement_node)
96
- !statement_node.ancestors.find(&:block_type?).method?(:transaction)
99
+ block_node = statement_node.ancestors.find(&:block_type?)
100
+
101
+ RESTRICT_ON_SEND.none? { |name| block_node.method?(name) }
97
102
  end
98
103
  end
99
104
  end
@@ -54,11 +54,9 @@ module RuboCop
54
54
  NEWLINE = "\n"
55
55
  PATTERN = '[!^block (send (send %<type>s :pluck ...) :uniq ...)]'
56
56
 
57
- def_node_matcher :conservative_node_match,
58
- format(PATTERN, type: 'const')
57
+ def_node_matcher :conservative_node_match, format(PATTERN, type: 'const')
59
58
 
60
- def_node_matcher :aggressive_node_match,
61
- format(PATTERN, type: '_')
59
+ def_node_matcher :aggressive_node_match, format(PATTERN, type: '_')
62
60
 
63
61
  def on_send(node)
64
62
  uniq = if style == :conservative
@@ -80,8 +78,7 @@ module RuboCop
80
78
  private
81
79
 
82
80
  def dot_method_with_whitespace(method, node)
83
- range_between(dot_method_begin_pos(method, node),
84
- node.loc.selector.end_pos)
81
+ range_between(dot_method_begin_pos(method, node), node.loc.selector.end_pos)
85
82
  end
86
83
 
87
84
  def dot_method_begin_pos(method, node)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Rails
6
6
  # When you define a uniqueness validation in Active Record model,
7
- # you also should add a unique index for the column. There are two reasons
7
+ # you also should add a unique index for the column. There are two reasons.
8
8
  # First, duplicated records may occur even if Active Record's validation
9
9
  # is defined.
10
10
  # Second, it will cause slow queries. The validation executes a `SELECT`
@@ -31,11 +31,11 @@ module RuboCop
31
31
  RESTRICT_ON_SEND = %i[validates].freeze
32
32
 
33
33
  def on_send(node)
34
- return unless uniqueness_part(node)
35
- return if condition_part?(node)
36
34
  return unless schema
35
+ return unless (uniqueness_part = uniqueness_part(node))
36
+ return if uniqueness_part.falsey_literal? || condition_part?(node, uniqueness_part)
37
37
 
38
- klass, table, names = find_schema_information(node)
38
+ klass, table, names = find_schema_information(node, uniqueness_part)
39
39
  return unless names
40
40
  return if with_index?(klass, table, names)
41
41
 
@@ -44,12 +44,12 @@ module RuboCop
44
44
 
45
45
  private
46
46
 
47
- def find_schema_information(node)
47
+ def find_schema_information(node, uniqueness_part)
48
48
  klass = class_node(node)
49
49
  return unless klass
50
50
 
51
51
  table = schema.table_by(name: table_name(klass))
52
- names = column_names(node)
52
+ names = column_names(node, uniqueness_part)
53
53
 
54
54
  [klass, table, names]
55
55
  end
@@ -59,9 +59,7 @@ module RuboCop
59
59
  add_indices = schema.add_indices_by(table_name: table_name(klass))
60
60
 
61
61
  (table.indices + add_indices).any? do |index|
62
- index.unique &&
63
- (index.columns.to_set == names ||
64
- include_column_names_in_expression_index?(index, names))
62
+ index.unique && (index.columns.to_set == names || include_column_names_in_expression_index?(index, names))
65
63
  end
66
64
  end
67
65
 
@@ -73,12 +71,12 @@ module RuboCop
73
71
  end
74
72
  end
75
73
 
76
- def column_names(node)
74
+ def column_names(node, uniqueness_part)
77
75
  arg = node.first_argument
78
76
  return unless arg.str_type? || arg.sym_type?
79
77
 
80
78
  ret = [arg.value]
81
- names_from_scope = column_names_from_scope(node)
79
+ names_from_scope = column_names_from_scope(uniqueness_part)
82
80
  ret.concat(names_from_scope) if names_from_scope
83
81
 
84
82
  ret = ret.flat_map do |name|
@@ -92,11 +90,10 @@ module RuboCop
92
90
  ret.include?(nil) ? nil : ret.to_set
93
91
  end
94
92
 
95
- def column_names_from_scope(node)
96
- uniq = uniqueness_part(node)
97
- return unless uniq.hash_type?
93
+ def column_names_from_scope(uniqueness_part)
94
+ return unless uniqueness_part.hash_type?
98
95
 
99
- scope = find_scope(uniq)
96
+ scope = find_scope(uniqueness_part)
100
97
  return unless scope
101
98
 
102
99
  scope = unfreeze_scope(scope)
@@ -137,15 +134,21 @@ module RuboCop
137
134
  end
138
135
  end
139
136
 
140
- def condition_part?(node)
141
- pairs = node.arguments.last
142
- return unless pairs.hash_type?
137
+ def condition_part?(node, uniqueness_node)
138
+ pairs = node.last_argument
139
+ return false unless pairs.hash_type?
140
+ return true if condition_hash_part?(pairs, keys: %i[if unless])
141
+ return false unless uniqueness_node.hash_type?
142
+
143
+ condition_hash_part?(uniqueness_node, keys: %i[if unless conditions])
144
+ end
143
145
 
146
+ def condition_hash_part?(pairs, keys:)
144
147
  pairs.each_pair.any? do |pair|
145
148
  key = pair.key
146
149
  next unless key.sym_type?
147
150
 
148
- key.value == :if || key.value == :unless
151
+ keys.include?(key.value)
149
152
  end
150
153
  end
151
154
 
@@ -19,8 +19,7 @@ module RuboCop
19
19
  # Rails.env == 'production'
20
20
  class UnknownEnv < Base
21
21
  MSG = 'Unknown environment `%<name>s`.'
22
- MSG_SIMILAR = 'Unknown environment `%<name>s`. ' \
23
- 'Did you mean `%<similar>s`?'
22
+ MSG_SIMILAR = 'Unknown environment `%<name>s`. Did you mean `%<similar>s`?'
24
23
 
25
24
  def_node_matcher :rails_env?, <<~PATTERN
26
25
  (send
@@ -79,8 +78,7 @@ module RuboCop
79
78
 
80
79
  def unknown_env_predicate?(name)
81
80
  name = name.to_s
82
- name.end_with?('?') &&
83
- !environments.include?(name[0..-2])
81
+ name.end_with?('?') && !environments.include?(name[0..-2])
84
82
  end
85
83
 
86
84
  def unknown_env_name?(name)
@@ -28,12 +28,16 @@ module RuboCop
28
28
  (send self :ignored_columns= $array)
29
29
  PATTERN
30
30
 
31
+ def_node_matcher :appended_ignored_columns, <<~PATTERN
32
+ (op-asgn (send self :ignored_columns) :+ $array)
33
+ PATTERN
34
+
31
35
  def_node_matcher :column_name, <<~PATTERN
32
36
  ({str sym} $_)
33
37
  PATTERN
34
38
 
35
39
  def on_send(node)
36
- return unless (columns = ignored_columns(node))
40
+ return unless (columns = ignored_columns(node) || appended_ignored_columns(node))
37
41
  return unless schema
38
42
 
39
43
  table = table(node)
@@ -43,6 +47,7 @@ module RuboCop
43
47
  check_column_existence(column_node, table)
44
48
  end
45
49
  end
50
+ alias on_op_asgn on_send
46
51
 
47
52
  private
48
53
 
@@ -35,8 +35,7 @@ module RuboCop
35
35
  class Validation < Base
36
36
  extend AutoCorrector
37
37
 
38
- MSG = 'Prefer the new style validations `%<prefer>s` over ' \
39
- '`%<current>s`.'
38
+ MSG = 'Prefer the new style validations `%<prefer>s` over `%<current>s`.'
40
39
 
41
40
  TYPES = %w[
42
41
  acceptance
@@ -62,8 +61,7 @@ module RuboCop
62
61
 
63
62
  add_offense(range, message: message(node)) do |corrector|
64
63
  last_argument = node.arguments.last
65
- return if !last_argument.literal? && !last_argument.splat_type? &&
66
- !frozen_array_argument?(last_argument)
64
+ return if !last_argument.literal? && !last_argument.splat_type? && !frozen_array_argument?(last_argument)
67
65
 
68
66
  corrector.replace(range, 'validates')
69
67
  correct_validate_type(corrector, node)
@@ -104,10 +102,7 @@ module RuboCop
104
102
  end
105
103
 
106
104
  def correct_validate_type_for_hash(corrector, node, arguments)
107
- corrector.replace(
108
- arguments.loc.expression,
109
- "#{validate_type(node)}: #{braced_options(arguments)}"
110
- )
105
+ corrector.replace(arguments, "#{validate_type(node)}: #{braced_options(arguments)}")
111
106
  end
112
107
 
113
108
  def correct_validate_type_for_array(corrector, node, arguments, loc)
@@ -121,10 +116,7 @@ module RuboCop
121
116
  end
122
117
  end
123
118
 
124
- corrector.replace(
125
- loc.expression,
126
- "#{attributes.join(', ')}, #{validate_type(node)}: true"
127
- )
119
+ corrector.replace(loc.expression, "#{attributes.join(', ')}, #{validate_type(node)}: true")
128
120
  end
129
121
 
130
122
  def validate_type(node)
@@ -65,7 +65,7 @@ module RuboCop
65
65
  private
66
66
 
67
67
  def offense_range(node)
68
- range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
68
+ range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
69
69
  end
70
70
 
71
71
  def extract_column_and_value(template_node, value_node)
@@ -105,7 +105,7 @@ module RuboCop
105
105
  if exists_style?
106
106
  node.receiver.loc.selector.join(node.loc.selector)
107
107
  elsif where_style?
108
- node.loc.selector.with(end_pos: node.loc.expression.end_pos)
108
+ node.loc.selector.with(end_pos: node.source_range.end_pos)
109
109
  end
110
110
  end
111
111
 
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Use `where.missing(...)` to find missing relationship records.
7
+ #
8
+ # This cop is enabled in Rails 6.1 or higher.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # Post.left_joins(:author).where(authors: { id: nil })
13
+ #
14
+ # # good
15
+ # Post.where.missing(:author)
16
+ #
17
+ class WhereMissing < Base
18
+ include RangeHelp
19
+ extend AutoCorrector
20
+ extend TargetRailsVersion
21
+
22
+ MSG = 'Use `where.missing(:%<left_joins_association>s)` instead of ' \
23
+ '`%<left_joins_method>s(:%<left_joins_association>s).where(%<where_association>s: { id: nil })`.'
24
+ RESTRICT_ON_SEND = %i[left_joins left_outer_joins].freeze
25
+
26
+ minimum_target_rails_version 6.1
27
+
28
+ # @!method where_node_and_argument(node)
29
+ def_node_search :where_node_and_argument, <<~PATTERN
30
+ $(send ... :where (hash <(pair $(sym _) (hash (pair (sym :id) (nil))))...> ))
31
+ PATTERN
32
+
33
+ # @!method missing_relationship(node)
34
+ def_node_search :missing_relationship, <<~PATTERN
35
+ (pair (sym _) (hash (pair (sym :id) (nil))))
36
+ PATTERN
37
+
38
+ def on_send(node)
39
+ return unless node.first_argument.sym_type?
40
+
41
+ root_receiver = root_receiver(node)
42
+ where_node_and_argument(root_receiver) do |where_node, where_argument|
43
+ next unless root_receiver == root_receiver(where_node)
44
+ next unless same_relationship?(where_argument, node.first_argument)
45
+
46
+ range = range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
47
+ register_offense(node, where_node, where_argument, range)
48
+ break
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def root_receiver(node)
55
+ parent = node.parent
56
+ if !parent&.send_type? || parent.method?(:or) || parent.method?(:and)
57
+ node
58
+ else
59
+ root_receiver(parent)
60
+ end
61
+ end
62
+
63
+ def same_relationship?(where, left_joins)
64
+ where.value.to_s.match?(/^#{left_joins.value}s?$/)
65
+ end
66
+
67
+ def register_offense(node, where_node, where_argument, range)
68
+ add_offense(range, message: message(node, where_argument)) do |corrector|
69
+ corrector.replace(node.loc.selector, 'where.missing')
70
+ if multi_condition?(where_node.first_argument)
71
+ replace_where_method(corrector, where_node)
72
+ else
73
+ remove_where_method(corrector, node, where_node)
74
+ end
75
+ end
76
+ end
77
+
78
+ def replace_where_method(corrector, where_node)
79
+ missing_relationship(where_node) do |where_clause|
80
+ corrector.remove(replace_range(where_clause))
81
+ end
82
+ end
83
+
84
+ def replace_range(child)
85
+ if (right_sibling = child.right_sibling)
86
+ range_between(child.source_range.begin_pos, right_sibling.source_range.begin_pos)
87
+ else
88
+ range_between(child.left_sibling.source_range.end_pos, child.source_range.end_pos)
89
+ end
90
+ end
91
+
92
+ def remove_where_method(corrector, node, where_node)
93
+ range = range_between(where_node.loc.selector.begin_pos, where_node.loc.end.end_pos)
94
+ if node.multiline? && !same_line?(node, where_node)
95
+ range = range_by_whole_lines(range, include_final_newline: true)
96
+ else
97
+ corrector.remove(where_node.loc.dot)
98
+ end
99
+
100
+ corrector.remove(range)
101
+ end
102
+
103
+ def same_line?(left_joins_node, where_node)
104
+ left_joins_node.loc.selector.line == where_node.loc.selector.line
105
+ end
106
+
107
+ def multi_condition?(where_arg)
108
+ where_arg.children.count > 1
109
+ end
110
+
111
+ def message(node, where_argument)
112
+ format(MSG, left_joins_association: node.first_argument.value, left_joins_method: node.method_name,
113
+ where_association: where_argument.value)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -64,7 +64,7 @@ module RuboCop
64
64
  private
65
65
 
66
66
  def offense_range(node)
67
- range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
67
+ range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
68
68
  end
69
69
 
70
70
  def extract_column_and_value(template_node, value_node)
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Identifies calls to `where.not` with multiple hash arguments.
7
+ #
8
+ # The behavior of `where.not` changed in Rails 6.1. Prior to the change,
9
+ # `.where.not(trashed: true, role: 'admin')` evaluated to
10
+ # `WHERE trashed != TRUE AND role != 'admin'`.
11
+ # From Rails 6.1 onwards, this executes the query
12
+ # `WHERE NOT (trashed == TRUE AND roles == 'admin')`.
13
+ #
14
+ # @example
15
+ # # bad
16
+ # User.where.not(trashed: true, role: 'admin')
17
+ # User.where.not(trashed: true, role: ['moderator', 'admin'])
18
+ # User.joins(:posts).where.not(posts: { trashed: true, title: 'Rails' })
19
+ #
20
+ # # good
21
+ # User.where.not(trashed: true)
22
+ # User.where.not(role: ['moderator', 'admin'])
23
+ # User.where.not(trashed: true).where.not(role: ['moderator', 'admin'])
24
+ # User.where.not('trashed = ? OR role = ?', true, 'admin')
25
+ class WhereNotWithMultipleConditions < Base
26
+ MSG = 'Use a SQL statement instead of `where.not` with multiple conditions.'
27
+ RESTRICT_ON_SEND = %i[not].freeze
28
+
29
+ def_node_matcher :where_not_call?, <<~PATTERN
30
+ (send (send _ :where) :not $...)
31
+ PATTERN
32
+
33
+ def on_send(node)
34
+ where_not_call?(node) do |args|
35
+ next unless args[0]&.hash_type?
36
+ next unless multiple_arguments_hash? args[0]
37
+
38
+ range = node.receiver.loc.selector.with(end_pos: node.source_range.end_pos)
39
+
40
+ add_offense(range)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def multiple_arguments_hash?(hash)
47
+ return true if hash.pairs.size >= 2
48
+ return false unless hash.values[0]&.hash_type?
49
+
50
+ multiple_arguments_hash?(hash.values[0])
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -8,12 +8,15 @@ require_relative 'mixin/index_method'
8
8
  require_relative 'mixin/migrations_helper'
9
9
  require_relative 'mixin/target_rails_version'
10
10
 
11
+ require_relative 'rails/action_controller_flash_before_render'
11
12
  require_relative 'rails/action_controller_test_case'
12
13
  require_relative 'rails/action_filter'
14
+ require_relative 'rails/action_order'
13
15
  require_relative 'rails/active_record_aliases'
14
16
  require_relative 'rails/active_record_callbacks_order'
15
17
  require_relative 'rails/active_record_override'
16
18
  require_relative 'rails/active_support_aliases'
19
+ require_relative 'rails/active_support_on_load'
17
20
  require_relative 'rails/add_column_index'
18
21
  require_relative 'rails/after_commit_override'
19
22
  require_relative 'rails/application_controller'
@@ -50,6 +53,7 @@ require_relative 'rails/file_path'
50
53
  require_relative 'rails/find_by'
51
54
  require_relative 'rails/find_by_id'
52
55
  require_relative 'rails/find_each'
56
+ require_relative 'rails/freeze_time'
53
57
  require_relative 'rails/has_and_belongs_to_many'
54
58
  require_relative 'rails/has_many_or_has_one_dependent'
55
59
  require_relative 'rails/helper_instance_variable'
@@ -58,6 +62,7 @@ require_relative 'rails/http_status'
58
62
  require_relative 'rails/i18n_lazy_lookup'
59
63
  require_relative 'rails/i18n_locale_assignment'
60
64
  require_relative 'rails/i18n_locale_texts'
65
+ require_relative 'rails/ignored_columns_assignment'
61
66
  require_relative 'rails/ignored_skip_action_filter_option'
62
67
  require_relative 'rails/index_by'
63
68
  require_relative 'rails/index_with'
@@ -94,9 +99,11 @@ require_relative 'rails/render_inline'
94
99
  require_relative 'rails/render_plain_text'
95
100
  require_relative 'rails/request_referer'
96
101
  require_relative 'rails/require_dependency'
102
+ require_relative 'rails/response_parsed_body'
97
103
  require_relative 'rails/reversible_migration'
98
104
  require_relative 'rails/reversible_migration_method_definition'
99
105
  require_relative 'rails/root_join_chain'
106
+ require_relative 'rails/root_pathname_methods'
100
107
  require_relative 'rails/root_public_path'
101
108
  require_relative 'rails/safe_navigation'
102
109
  require_relative 'rails/safe_navigation_with_blank'
@@ -108,9 +115,12 @@ require_relative 'rails/skips_model_validations'
108
115
  require_relative 'rails/squished_sql_heredocs'
109
116
  require_relative 'rails/strip_heredoc'
110
117
  require_relative 'rails/table_name_assignment'
118
+ require_relative 'rails/three_state_boolean_column'
111
119
  require_relative 'rails/time_zone'
112
120
  require_relative 'rails/time_zone_assignment'
113
121
  require_relative 'rails/to_formatted_s'
122
+ require_relative 'rails/to_s_with_argument'
123
+ require_relative 'rails/top_level_hash_with_indifferent_access'
114
124
  require_relative 'rails/transaction_exit_statement'
115
125
  require_relative 'rails/uniq_before_pluck'
116
126
  require_relative 'rails/unique_validation_without_index'
@@ -119,4 +129,6 @@ require_relative 'rails/unused_ignored_columns'
119
129
  require_relative 'rails/validation'
120
130
  require_relative 'rails/where_equals'
121
131
  require_relative 'rails/where_exists'
132
+ require_relative 'rails/where_missing'
122
133
  require_relative 'rails/where_not'
134
+ require_relative 'rails/where_not_with_multiple_conditions'
@@ -83,21 +83,21 @@ module RuboCop
83
83
  private
84
84
 
85
85
  def build_columns(node)
86
- each_content(node).map do |child|
86
+ each_content(node).filter_map do |child|
87
87
  next unless child&.send_type?
88
88
  next if child.method?(:index)
89
89
 
90
90
  Column.new(child)
91
- end.compact
91
+ end
92
92
  end
93
93
 
94
94
  def build_indices(node)
95
- each_content(node).map do |child|
95
+ each_content(node).filter_map do |child|
96
96
  next unless child&.send_type?
97
97
  next unless child.method?(:index)
98
98
 
99
99
  Index.new(child)
100
- end.compact
100
+ end
101
101
  end
102
102
 
103
103
  def each_content(node, &block)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.15.2'
7
+ STRING = '2.20.2'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
data/lib/rubocop/rails.rb CHANGED
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Rails
6
6
  PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7
7
  CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
8
- CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
8
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read, permitted_classes: [Regexp, Symbol]).freeze
9
9
 
10
10
  private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11
11
 
data/lib/rubocop-rails.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'rubocop'
4
4
  require 'rack/utils'
5
5
  require 'active_support/inflector'
6
+ require 'active_support/core_ext/object/blank'
6
7
 
7
8
  require_relative 'rubocop/rails'
8
9
  require_relative 'rubocop/rails/version'
@@ -14,6 +15,16 @@ RuboCop::Rails::Inject.defaults!
14
15
 
15
16
  require_relative 'rubocop/cop/rails_cops'
16
17
 
18
+ RuboCop::Cop::Style::HashExcept.minimum_target_ruby_version(2.0)
19
+
20
+ RuboCop::Cop::Style::MethodCallWithArgsParentheses.singleton_class.prepend(
21
+ Module.new do
22
+ def autocorrect_incompatible_with
23
+ super.push(RuboCop::Cop::Rails::EagerEvaluationLogMessage)
24
+ end
25
+ end
26
+ )
27
+
17
28
  RuboCop::Cop::Style::RedundantSelf.singleton_class.prepend(
18
29
  Module.new do
19
30
  def autocorrect_incompatible_with