rubocop-rails 2.11.2 → 2.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +23 -2
  4. data/config/default.yml +316 -20
  5. data/config/obsoletion.yml +10 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +17 -8
  7. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +32 -0
  8. data/lib/rubocop/cop/mixin/class_send_node_helper.rb +20 -0
  9. data/lib/rubocop/cop/mixin/enforce_superclass.rb +1 -1
  10. data/lib/rubocop/cop/mixin/index_method.rb +7 -17
  11. data/lib/rubocop/cop/mixin/migrations_helper.rb +26 -0
  12. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +112 -0
  13. data/lib/rubocop/cop/rails/action_controller_test_case.rb +47 -0
  14. data/lib/rubocop/cop/rails/action_filter.rb +2 -2
  15. data/lib/rubocop/cop/rails/action_order.rb +116 -0
  16. data/lib/rubocop/cop/rails/active_record_aliases.rb +9 -6
  17. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +8 -13
  18. data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
  19. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  20. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  21. data/lib/rubocop/cop/rails/add_column_index.rb +3 -6
  22. data/lib/rubocop/cop/rails/after_commit_override.rb +3 -13
  23. data/lib/rubocop/cop/rails/application_controller.rb +6 -2
  24. data/lib/rubocop/cop/rails/application_job.rb +7 -3
  25. data/lib/rubocop/cop/rails/application_mailer.rb +6 -2
  26. data/lib/rubocop/cop/rails/application_record.rb +7 -2
  27. data/lib/rubocop/cop/rails/arel_star.rb +8 -2
  28. data/lib/rubocop/cop/rails/assert_not.rb +1 -1
  29. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +1 -1
  30. data/lib/rubocop/cop/rails/belongs_to.rb +2 -5
  31. data/lib/rubocop/cop/rails/blank.rb +13 -13
  32. data/lib/rubocop/cop/rails/bulk_change_table.rb +28 -31
  33. data/lib/rubocop/cop/rails/compact_blank.rb +111 -0
  34. data/lib/rubocop/cop/rails/content_tag.rb +42 -33
  35. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -8
  36. data/lib/rubocop/cop/rails/date.rb +12 -17
  37. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  38. data/lib/rubocop/cop/rails/delegate.rb +24 -18
  39. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +2 -2
  40. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +168 -0
  41. data/lib/rubocop/cop/rails/dot_separated_keys.rb +71 -0
  42. data/lib/rubocop/cop/rails/duplicate_association.rb +56 -0
  43. data/lib/rubocop/cop/rails/duplicate_scope.rb +46 -0
  44. data/lib/rubocop/cop/rails/duration_arithmetic.rb +98 -0
  45. data/lib/rubocop/cop/rails/dynamic_find_by.rb +31 -15
  46. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +6 -2
  47. data/lib/rubocop/cop/rails/enum_hash.rb +3 -4
  48. data/lib/rubocop/cop/rails/enum_uniqueness.rb +3 -6
  49. data/lib/rubocop/cop/rails/environment_comparison.rb +3 -4
  50. data/lib/rubocop/cop/rails/environment_variable_access.rb +1 -1
  51. data/lib/rubocop/cop/rails/exit.rb +1 -1
  52. data/lib/rubocop/cop/rails/expanded_date_range.rb +39 -23
  53. data/lib/rubocop/cop/rails/file_path.rb +41 -24
  54. data/lib/rubocop/cop/rails/find_by.rb +1 -1
  55. data/lib/rubocop/cop/rails/find_by_id.rb +3 -3
  56. data/lib/rubocop/cop/rails/find_each.rb +27 -4
  57. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  58. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  59. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +19 -9
  60. data/lib/rubocop/cop/rails/helper_instance_variable.rb +3 -3
  61. data/lib/rubocop/cop/rails/http_positional_arguments.rb +29 -11
  62. data/lib/rubocop/cop/rails/http_status.rb +6 -11
  63. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +96 -0
  64. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +1 -1
  65. data/lib/rubocop/cop/rails/i18n_locale_texts.rb +110 -0
  66. data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
  67. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +5 -14
  68. data/lib/rubocop/cop/rails/index_by.rb +8 -8
  69. data/lib/rubocop/cop/rails/index_with.rb +8 -8
  70. data/lib/rubocop/cop/rails/inquiry.rb +1 -1
  71. data/lib/rubocop/cop/rails/inverse_of.rb +20 -10
  72. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +44 -16
  73. data/lib/rubocop/cop/rails/link_to_blank.rb +4 -7
  74. data/lib/rubocop/cop/rails/mailer_name.rb +9 -5
  75. data/lib/rubocop/cop/rails/match_route.rb +1 -1
  76. data/lib/rubocop/cop/rails/migration_class_name.rb +63 -0
  77. data/lib/rubocop/cop/rails/negate_include.rb +5 -4
  78. data/lib/rubocop/cop/rails/not_null_column.rb +10 -7
  79. data/lib/rubocop/cop/rails/order_by_id.rb +2 -3
  80. data/lib/rubocop/cop/rails/output.rb +26 -10
  81. data/lib/rubocop/cop/rails/output_safety.rb +6 -2
  82. data/lib/rubocop/cop/rails/pick.rb +8 -1
  83. data/lib/rubocop/cop/rails/pluck.rb +51 -11
  84. data/lib/rubocop/cop/rails/pluck_id.rb +5 -2
  85. data/lib/rubocop/cop/rails/pluck_in_where.rb +8 -7
  86. data/lib/rubocop/cop/rails/pluralization_grammar.rb +4 -5
  87. data/lib/rubocop/cop/rails/presence.rb +22 -13
  88. data/lib/rubocop/cop/rails/present.rb +10 -13
  89. data/lib/rubocop/cop/rails/rake_environment.rb +8 -3
  90. data/lib/rubocop/cop/rails/read_write_attribute.rb +52 -15
  91. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +7 -17
  92. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +3 -3
  93. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +257 -0
  94. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +31 -27
  95. data/lib/rubocop/cop/rails/redundant_travel_back.rb +57 -0
  96. data/lib/rubocop/cop/rails/reflection_class_name.rb +39 -4
  97. data/lib/rubocop/cop/rails/refute_methods.rb +1 -5
  98. data/lib/rubocop/cop/rails/relative_date_constant.rb +9 -9
  99. data/lib/rubocop/cop/rails/render_inline.rb +1 -1
  100. data/lib/rubocop/cop/rails/render_plain_text.rb +1 -1
  101. data/lib/rubocop/cop/rails/request_referer.rb +2 -3
  102. data/lib/rubocop/cop/rails/require_dependency.rb +2 -2
  103. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  104. data/lib/rubocop/cop/rails/reversible_migration.rb +29 -67
  105. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +6 -15
  106. data/lib/rubocop/cop/rails/root_join_chain.rb +72 -0
  107. data/lib/rubocop/cop/rails/root_pathname_methods.rb +238 -0
  108. data/lib/rubocop/cop/rails/root_public_path.rb +59 -0
  109. data/lib/rubocop/cop/rails/safe_navigation.rb +8 -13
  110. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +14 -7
  111. data/lib/rubocop/cop/rails/save_bang.rb +30 -23
  112. data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
  113. data/lib/rubocop/cop/rails/scope_args.rb +6 -2
  114. data/lib/rubocop/cop/rails/short_i18n.rb +3 -6
  115. data/lib/rubocop/cop/rails/skips_model_validations.rb +3 -4
  116. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +13 -8
  117. data/lib/rubocop/cop/rails/strip_heredoc.rb +56 -0
  118. data/lib/rubocop/cop/rails/table_name_assignment.rb +44 -0
  119. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +73 -0
  120. data/lib/rubocop/cop/rails/time_zone.rb +37 -32
  121. data/lib/rubocop/cop/rails/time_zone_assignment.rb +4 -4
  122. data/lib/rubocop/cop/rails/to_formatted_s.rb +46 -0
  123. data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
  124. data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
  125. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +99 -0
  126. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +32 -41
  127. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +16 -9
  128. data/lib/rubocop/cop/rails/unknown_env.rb +3 -5
  129. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +9 -2
  130. data/lib/rubocop/cop/rails/validation.rb +5 -13
  131. data/lib/rubocop/cop/rails/where_equals.rb +6 -2
  132. data/lib/rubocop/cop/rails/where_exists.rb +11 -10
  133. data/lib/rubocop/cop/rails/where_missing.rb +118 -0
  134. data/lib/rubocop/cop/rails/where_not.rb +2 -2
  135. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  136. data/lib/rubocop/cop/rails_cops.rb +34 -0
  137. data/lib/rubocop/rails/schema_loader/schema.rb +8 -5
  138. data/lib/rubocop/rails/version.rb +1 -1
  139. data/lib/rubocop/rails.rb +1 -1
  140. data/lib/rubocop-rails.rb +19 -0
  141. metadata +42 -8
  142. data/bin/setup +0 -7
@@ -5,3 +5,13 @@
5
5
  #
6
6
  extracted:
7
7
  Rails/*: ~
8
+
9
+ # Cop parameters that have been changed
10
+ # Can be treated as a warning instead of a failure with `severity: warning`
11
+ changed_parameters:
12
+ - cops: Rails/FindEach
13
+ parameters: IgnoredMethods
14
+ alternatives:
15
+ - AllowedMethods
16
+ - AllowedPatterns
17
+ severity: warning
@@ -10,8 +10,8 @@ module RuboCop
10
10
 
11
11
  def_node_matcher :active_record?, <<~PATTERN
12
12
  {
13
- (const nil? :ApplicationRecord)
14
- (const (const nil? :ActiveRecord) :Base)
13
+ (const {nil? cbase} :ApplicationRecord)
14
+ (const (const {nil? cbase} :ActiveRecord) :Base)
15
15
  }
16
16
  PATTERN
17
17
 
@@ -48,21 +48,19 @@ module RuboCop
48
48
 
49
49
  class_nodes = class_node.defined_module.each_node
50
50
  namespaces = class_node.each_ancestor(:class, :module).map(&:identifier)
51
- [*class_nodes, *namespaces]
52
- .reverse
53
- .map { |node| node.children[1] }.join('_')
54
- .tableize
51
+ [*class_nodes, *namespaces].reverse.map { |node| node.children[1] }.join('_').tableize
55
52
  end
56
53
 
57
54
  # Resolve relation into column name.
58
55
  # It just returns column_name if the column exists.
59
56
  # Or it tries to resolve column_name as a relation.
57
+ # Returns an array of column names if the relation is polymorphic.
60
58
  # It returns `nil` if it can't resolve.
61
59
  #
62
60
  # @param name [String]
63
61
  # @param class_node [RuboCop::AST::Node]
64
62
  # @param table [RuboCop::Rails::SchemaLoader::Table]
65
- # @return [String, nil]
63
+ # @return [Array, String, nil]
66
64
  def resolve_relation_into_column(name:, class_node:, table:)
67
65
  return unless table
68
66
  return name if table.with_column?(name: name)
@@ -71,7 +69,9 @@ module RuboCop
71
69
  next unless belongs_to.first_argument.value.to_s == name
72
70
 
73
71
  fk = foreign_key_of(belongs_to) || "#{name}_id"
74
- return fk if table.with_column?(name: fk)
72
+ next unless table.with_column?(name: fk)
73
+
74
+ return polymorphic?(belongs_to) ? [fk, "#{name}_type"] : fk
75
75
  end
76
76
  nil
77
77
  end
@@ -88,6 +88,15 @@ module RuboCop
88
88
  end
89
89
  end
90
90
 
91
+ def polymorphic?(belongs_to)
92
+ options = belongs_to.last_argument
93
+ return false unless options.hash_type?
94
+
95
+ options.each_pair.any? do |pair|
96
+ pair.key.sym_type? && pair.key.value == :polymorphic && pair.value.true_type?
97
+ end
98
+ end
99
+
91
100
  def in_where?(node)
92
101
  send_node = node.each_ancestor(:send).first
93
102
  send_node && WHERE_METHODS.include?(send_node.method_name)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # A mixin to extend cops for Active Record features
6
+ module ActiveRecordMigrationsHelper
7
+ extend NodePattern::Macros
8
+
9
+ RAILS_ABSTRACT_SCHEMA_DEFINITIONS = %i[
10
+ bigint binary boolean date datetime decimal float integer json string
11
+ text time timestamp virtual
12
+ ].freeze
13
+ RAILS_ABSTRACT_SCHEMA_DEFINITIONS_HELPERS = %i[column references belongs_to primary_key numeric].freeze
14
+ POSTGRES_SCHEMA_DEFINITIONS = %i[
15
+ bigserial bit bit_varying cidr citext daterange hstore inet interval
16
+ int4range int8range jsonb ltree macaddr money numrange oid point line
17
+ lseg box path polygon circle serial tsrange tstzrange tsvector uuid xml
18
+ ].freeze
19
+ MYSQL_SCHEMA_DEFINITIONS = %i[
20
+ blob tinyblob mediumblob longblob tinytext mediumtext longtext
21
+ unsigned_integer unsigned_bigint unsigned_float unsigned_decimal
22
+ ].freeze
23
+
24
+ def_node_matcher :create_table_with_block?, <<~PATTERN
25
+ (block
26
+ (send nil? :create_table ...)
27
+ (args (arg _var))
28
+ _)
29
+ PATTERN
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # A mixin to return all of the class send nodes.
6
+ module ClassSendNodeHelper
7
+ def class_send_nodes(class_node)
8
+ class_def = class_node.body
9
+
10
+ return [] unless class_def
11
+
12
+ if class_def.send_type?
13
+ [class_def]
14
+ else
15
+ class_def.each_child_node(:send)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -32,7 +32,7 @@ module RuboCop
32
32
 
33
33
  def register_offense(offense_node)
34
34
  add_offense(offense_node) do |corrector|
35
- corrector.replace(offense_node.source_range, self.class::SUPERCLASS)
35
+ corrector.replace(offense_node, self.class::SUPERCLASS)
36
36
  end
37
37
  end
38
38
  end
@@ -6,7 +6,7 @@ module RuboCop
6
6
  module IndexMethod # rubocop:disable Metrics/ModuleLength
7
7
  RESTRICT_ON_SEND = %i[each_with_object to_h map collect []].freeze
8
8
 
9
- def on_block(node)
9
+ def on_block(node) # rubocop:todo InternalAffairs/NumblockHandler
10
10
  on_bad_each_with_object(node) do |*match|
11
11
  handle_possible_offense(node, match, 'each_with_object')
12
12
  end
@@ -98,10 +98,7 @@ module RuboCop
98
98
 
99
99
  captures = extract_captures(correction.match)
100
100
  correction.set_new_arg_name(captures.transformed_argname, corrector)
101
- correction.set_new_body_expression(
102
- captures.transforming_body_expr,
103
- corrector
104
- )
101
+ correction.set_new_body_expression(captures.transforming_body_expr, corrector)
105
102
  end
106
103
 
107
104
  # Internal helper class to hold match data
@@ -110,8 +107,7 @@ module RuboCop
110
107
  :transforming_body_expr
111
108
  ) do
112
109
  def noop_transformation?
113
- transforming_body_expr.lvar_type? &&
114
- transforming_body_expr.children == [transformed_argname]
110
+ transforming_body_expr.lvar_type? && transforming_body_expr.children == [transformed_argname]
115
111
  end
116
112
  end
117
113
 
@@ -138,11 +134,11 @@ module RuboCop
138
134
  end
139
135
 
140
136
  def self.from_hash_brackets_map(node, match)
141
- new(match, node.children.last, 'Hash['.length, ']'.length)
137
+ new(match, node.children.last, "#{node.receiver.source}[".length, ']'.length)
142
138
  end
143
139
 
144
140
  def strip_prefix_and_suffix(node, corrector)
145
- expression = node.loc.expression
141
+ expression = node.source_range
146
142
  corrector.remove_leading(expression, leading)
147
143
  corrector.remove_trailing(expression, trailing)
148
144
  end
@@ -157,17 +153,11 @@ module RuboCop
157
153
  end
158
154
 
159
155
  def set_new_arg_name(transformed_argname, corrector)
160
- corrector.replace(
161
- block_node.arguments.loc.expression,
162
- "|#{transformed_argname}|"
163
- )
156
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
164
157
  end
165
158
 
166
159
  def set_new_body_expression(transforming_body_expr, corrector)
167
- corrector.replace(
168
- block_node.body.loc.expression,
169
- transforming_body_expr.loc.expression.source
170
- )
160
+ corrector.replace(block_node.body, transforming_body_expr.source)
171
161
  end
172
162
  end
173
163
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for cops working with migrations
6
+ module MigrationsHelper
7
+ extend NodePattern::Macros
8
+
9
+ def_node_matcher :migration_class?, <<~PATTERN
10
+ (class
11
+ (const {nil? cbase} _)
12
+ (send
13
+ (const (const {nil? cbase} :ActiveRecord) :Migration)
14
+ :[]
15
+ (float _))
16
+ _)
17
+ PATTERN
18
+
19
+ def in_migration?(node)
20
+ node.each_ancestor(:class).any? do |class_node|
21
+ migration_class?(class_node)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Using `flash` assignment before `render` in Rails controllers will persist the message for too long.
7
+ # Check https://guides.rubyonrails.org/action_controller_overview.html#flash-now
8
+ #
9
+ # @safety
10
+ # This cop's autocorrection is unsafe because it replaces `flash` by `flash.now`.
11
+ # Even though it is usually a mistake, it might be used intentionally.
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # class HomeController < ApplicationController
17
+ # def create
18
+ # flash[:alert] = "msg"
19
+ # render :index
20
+ # end
21
+ # end
22
+ #
23
+ # # good
24
+ # class HomeController < ApplicationController
25
+ # def create
26
+ # flash.now[:alert] = "msg"
27
+ # render :index
28
+ # end
29
+ # end
30
+ #
31
+ class ActionControllerFlashBeforeRender < Base
32
+ extend AutoCorrector
33
+
34
+ MSG = 'Use `flash.now` before `render`.'
35
+
36
+ def_node_search :flash_assignment?, <<~PATTERN
37
+ ^(send (send nil? :flash) :[]= ...)
38
+ PATTERN
39
+
40
+ def_node_search :render?, <<~PATTERN
41
+ (send nil? :render ...)
42
+ PATTERN
43
+
44
+ def_node_search :action_controller?, <<~PATTERN
45
+ {
46
+ (const {nil? cbase} :ApplicationController)
47
+ (const (const {nil? cbase} :ActionController) :Base)
48
+ }
49
+ PATTERN
50
+
51
+ RESTRICT_ON_SEND = [:flash].freeze
52
+
53
+ def on_send(flash_node)
54
+ return unless flash_assignment?(flash_node)
55
+
56
+ return unless followed_by_render?(flash_node)
57
+
58
+ return unless instance_method_or_block?(flash_node)
59
+
60
+ return unless inherit_action_controller_base?(flash_node)
61
+
62
+ add_offense(flash_node) do |corrector|
63
+ corrector.replace(flash_node, 'flash.now')
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def followed_by_render?(flash_node)
70
+ flash_assignment_node = find_ancestor(flash_node, type: :send)
71
+ context = flash_assignment_node
72
+ if (node = context.each_ancestor(:if, :rescue).first)
73
+ return false if use_redirect_to?(context)
74
+
75
+ context = node
76
+ elsif context.right_siblings.empty?
77
+ return true
78
+ end
79
+ context = context.right_siblings
80
+
81
+ context.compact.any? do |render_candidate|
82
+ render?(render_candidate)
83
+ end
84
+ end
85
+
86
+ def inherit_action_controller_base?(node)
87
+ class_node = find_ancestor(node, type: :class)
88
+ return unless class_node
89
+
90
+ action_controller?(class_node)
91
+ end
92
+
93
+ def instance_method_or_block?(node)
94
+ def_node = find_ancestor(node, type: :def)
95
+ block_node = find_ancestor(node, type: :block)
96
+
97
+ def_node || block_node
98
+ end
99
+
100
+ def use_redirect_to?(context)
101
+ context.right_siblings.compact.any? do |sibling|
102
+ sibling.send_type? && sibling.method?(:redirect_to)
103
+ end
104
+ end
105
+
106
+ def find_ancestor(node, type:)
107
+ node.each_ancestor(type).first
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Using `ActionController::TestCase`` is discouraged and should be replaced by
7
+ # `ActionDispatch::IntegrationTest``. Controller tests are too close to the
8
+ # internals of a controller whereas integration tests mimic the browser/user.
9
+ #
10
+ # @safety
11
+ # This cop's autocorrection is unsafe because the API of each test case class is different.
12
+ # Make sure to update each test of your controller test cases after changing the superclass.
13
+ #
14
+ # @example
15
+ # # bad
16
+ # class MyControllerTest < ActionController::TestCase
17
+ # end
18
+ #
19
+ # # good
20
+ # class MyControllerTest < ActionDispatch::IntegrationTest
21
+ # end
22
+ #
23
+ class ActionControllerTestCase < Base
24
+ extend AutoCorrector
25
+ extend TargetRailsVersion
26
+
27
+ MSG = 'Use `ActionDispatch::IntegrationTest` instead.'
28
+
29
+ minimum_target_rails_version 5.0
30
+
31
+ def_node_matcher :action_controller_test_case?, <<~PATTERN
32
+ (class
33
+ (const _ _)
34
+ (const (const {nil? cbase} :ActionController) :TestCase) _)
35
+ PATTERN
36
+
37
+ def on_class(node)
38
+ return unless action_controller_test_case?(node)
39
+
40
+ add_offense(node.parent_class) do |corrector|
41
+ corrector.replace(node.parent_class, 'ActionDispatch::IntegrationTest')
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces the consistent use of action filter methods.
6
+ # Enforces the consistent use of action filter methods.
7
7
  #
8
8
  # The cop is configurable and can enforce the use of the older
9
9
  # something_filter methods or the newer something_action methods.
@@ -69,7 +69,7 @@ module RuboCop
69
69
 
70
70
  RESTRICT_ON_SEND = FILTER_METHODS + ACTION_METHODS
71
71
 
72
- def on_block(node)
72
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
73
73
  check_method_node(node.send_node)
74
74
  end
75
75
 
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces consistent ordering of the standard Rails RESTful controller actions.
7
+ #
8
+ # The cop is configurable and can enforce any ordering of the standard actions.
9
+ # All other methods are ignored. So, the actions specified in `ExpectedOrder` should be
10
+ # defined before actions not specified.
11
+ #
12
+ # [source,yaml]
13
+ # ----
14
+ # Rails/ActionOrder:
15
+ # ExpectedOrder:
16
+ # - index
17
+ # - show
18
+ # - new
19
+ # - edit
20
+ # - create
21
+ # - update
22
+ # - destroy
23
+ # ----
24
+ #
25
+ # @example
26
+ # # bad
27
+ # def index; end
28
+ # def destroy; end
29
+ # def show; end
30
+ #
31
+ # # good
32
+ # def index; end
33
+ # def show; end
34
+ # def destroy; end
35
+ class ActionOrder < Base
36
+ extend AutoCorrector
37
+ include VisibilityHelp
38
+ include DefNode
39
+ include RangeHelp
40
+
41
+ MSG = 'Action `%<current>s` should appear before `%<previous>s`.'
42
+
43
+ def_node_search :action_declarations, '(def {%1} ...)'
44
+
45
+ def on_class(node)
46
+ action_declarations(node, actions).each_cons(2) do |previous, current|
47
+ next if node_visibility(current) != :public || non_public?(current)
48
+ next if find_index(current) >= find_index(previous)
49
+
50
+ register_offense(previous, current)
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def expected_order
57
+ cop_config['ExpectedOrder'].map(&:to_sym)
58
+ end
59
+
60
+ def actions
61
+ @actions ||= Set.new(expected_order)
62
+ end
63
+
64
+ def find_index(node)
65
+ expected_order.find_index(node.method_name)
66
+ end
67
+
68
+ def register_offense(previous, current)
69
+ message = format(
70
+ MSG,
71
+ expected_order: expected_order.join(', '),
72
+ previous: previous.method_name,
73
+ current: current.method_name
74
+ )
75
+ add_offense(current, message: message) do |corrector|
76
+ current = correction_target(current)
77
+ previous = correction_target(previous)
78
+
79
+ swap_range(corrector, current, previous)
80
+ end
81
+ end
82
+
83
+ def correction_target(def_node)
84
+ range_with_comments_and_lines(def_node.each_ancestor(:if).first || def_node)
85
+ end
86
+
87
+ def add_range(range1, range2)
88
+ range1.with(
89
+ begin_pos: [range1.begin_pos, range2.begin_pos].min,
90
+ end_pos: [range1.end_pos, range2.end_pos].max
91
+ )
92
+ end
93
+
94
+ def range_with_comments(node)
95
+ # rubocop:todo InternalAffairs/LocationExpression
96
+ # Using `RuboCop::Ext::Comment#source_range` requires RuboCop > 1.46,
97
+ # which introduces https://github.com/rubocop/rubocop/pull/11630.
98
+ ranges = [node, *processed_source.ast_with_comments[node]].map { |comment| comment.loc.expression }
99
+ # rubocop:enable InternalAffairs/LocationExpression
100
+ ranges.reduce do |result, range|
101
+ add_range(result, range)
102
+ end
103
+ end
104
+
105
+ def range_with_comments_and_lines(node)
106
+ range_by_whole_lines(range_with_comments(node), include_final_newline: true)
107
+ end
108
+
109
+ def swap_range(corrector, range1, range2)
110
+ corrector.insert_before(range2, range1.source)
111
+ corrector.remove(range1)
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -6,25 +6,28 @@ module RuboCop
6
6
  # Checks that ActiveRecord aliases are not used. The direct method names
7
7
  # are more clear and easier to read.
8
8
  #
9
+ # @safety
10
+ # This cop is unsafe because custom `update_attributes` method call was changed to
11
+ # `update` but the method name remained same in the method definition.
12
+ #
9
13
  # @example
10
14
  # #bad
11
- # Book.update_attributes!(author: 'Alice')
15
+ # book.update_attributes!(author: 'Alice')
12
16
  #
13
17
  # #good
14
- # Book.update!(author: 'Alice')
18
+ # book.update!(author: 'Alice')
15
19
  class ActiveRecordAliases < Base
16
20
  extend AutoCorrector
17
21
 
18
22
  MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
19
23
 
20
- ALIASES = {
21
- update_attributes: :update,
22
- update_attributes!: :update!
23
- }.freeze
24
+ ALIASES = { update_attributes: :update, update_attributes!: :update! }.freeze
24
25
 
25
26
  RESTRICT_ON_SEND = ALIASES.keys.freeze
26
27
 
27
28
  def on_send(node)
29
+ return if node.arguments.empty?
30
+
28
31
  method_name = node.method_name
29
32
  alias_method = ALIASES[method_name]
30
33
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that Active Record callbacks are declared
6
+ # Checks that Active Record callbacks are declared
7
7
  # in the order in which they will be executed.
8
8
  #
9
9
  # @example
@@ -71,7 +71,7 @@ module RuboCop
71
71
 
72
72
  # Autocorrect by swapping between two nodes autocorrecting them
73
73
  def autocorrect(corrector, node)
74
- previous = left_siblings_of(node).reverse_each.find do |sibling|
74
+ previous = node.left_siblings.reverse_each.find do |sibling|
75
75
  callback?(sibling)
76
76
  end
77
77
 
@@ -96,14 +96,6 @@ module RuboCop
96
96
  node.send_type? && CALLBACKS_ORDER_MAP.key?(node.method_name)
97
97
  end
98
98
 
99
- def left_siblings_of(node)
100
- siblings_of(node)[0, node.sibling_index]
101
- end
102
-
103
- def siblings_of(node)
104
- node.parent.children
105
- end
106
-
107
99
  def source_range_with_comment(node)
108
100
  begin_pos = begin_pos_with_comment(node)
109
101
  end_pos = end_position_for(node)
@@ -112,7 +104,7 @@ module RuboCop
112
104
  end
113
105
 
114
106
  def end_position_for(node)
115
- end_line = buffer.line_for_position(node.loc.expression.end_pos)
107
+ end_line = buffer.line_for_position(node.source_range.end_pos)
116
108
  buffer.line_range(end_line).end_pos
117
109
  end
118
110
 
@@ -120,8 +112,7 @@ module RuboCop
120
112
  annotation_line = node.first_line - 1
121
113
  first_comment = nil
122
114
 
123
- processed_source.comments_before_line(annotation_line)
124
- .reverse_each do |comment|
115
+ processed_source.each_comment_in_lines(0..annotation_line).reverse_each do |comment|
125
116
  if comment.location.line == annotation_line && !inline_comment?(comment)
126
117
  first_comment = comment
127
118
  annotation_line -= 1
@@ -132,7 +123,11 @@ module RuboCop
132
123
  end
133
124
 
134
125
  def inline_comment?(comment)
126
+ # rubocop:todo InternalAffairs/LocationExpression
127
+ # Using `RuboCop::Ext::Comment#source_range` requires RuboCop > 1.46,
128
+ # which introduces https://github.com/rubocop/rubocop/pull/11630.
135
129
  !comment_line?(comment.loc.expression.source_line)
130
+ # rubocop:enable InternalAffairs/LocationExpression
136
131
  end
137
132
 
138
133
  def start_line_position(node)
@@ -25,12 +25,9 @@ module RuboCop
25
25
  # end
26
26
  #
27
27
  class ActiveRecordOverride < Base
28
- MSG =
29
- 'Use %<prefer>s callbacks instead of overriding the Active Record ' \
30
- 'method `%<bad>s`.'
28
+ MSG = 'Use %<prefer>s callbacks instead of overriding the Active Record method `%<bad>s`.'
31
29
  BAD_METHODS = %i[create destroy save update].freeze
32
- ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base
33
- ActiveRecord::Base].freeze
30
+ ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base ActiveRecord::Base].freeze
34
31
 
35
32
  def on_def(node)
36
33
  return unless BAD_METHODS.include?(node.method_name)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that ActiveSupport aliases to core ruby methods
6
+ # Checks that ActiveSupport aliases to core ruby methods
7
7
  # are not used.
8
8
  #
9
9
  # @example