rubocop-rails 2.8.0 → 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +18 -2
  4. data/config/default.yml +101 -5
  5. data/config/obsoletion.yml +7 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +16 -3
  7. data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
  8. data/lib/rubocop/cop/mixin/index_method.rb +8 -11
  9. data/lib/rubocop/cop/rails/action_filter.rb +10 -14
  10. data/lib/rubocop/cop/rails/active_record_aliases.rb +13 -17
  11. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +17 -12
  12. data/lib/rubocop/cop/rails/active_record_override.rb +1 -1
  13. data/lib/rubocop/cop/rails/active_support_aliases.rb +12 -21
  14. data/lib/rubocop/cop/rails/after_commit_override.rb +9 -2
  15. data/lib/rubocop/cop/rails/application_controller.rb +3 -7
  16. data/lib/rubocop/cop/rails/application_job.rb +2 -1
  17. data/lib/rubocop/cop/rails/application_mailer.rb +2 -7
  18. data/lib/rubocop/cop/rails/application_record.rb +2 -7
  19. data/lib/rubocop/cop/rails/arel_star.rb +41 -0
  20. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  21. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  22. data/lib/rubocop/cop/rails/belongs_to.rb +10 -19
  23. data/lib/rubocop/cop/rails/blank.rb +31 -27
  24. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  25. data/lib/rubocop/cop/rails/content_tag.rb +33 -18
  26. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -1
  27. data/lib/rubocop/cop/rails/date.rb +10 -11
  28. data/lib/rubocop/cop/rails/default_scope.rb +11 -4
  29. data/lib/rubocop/cop/rails/delegate.rb +9 -9
  30. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +7 -8
  31. data/lib/rubocop/cop/rails/dynamic_find_by.rb +15 -12
  32. data/lib/rubocop/cop/rails/enum_hash.rb +11 -10
  33. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -1
  34. data/lib/rubocop/cop/rails/environment_comparison.rb +18 -14
  35. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  36. data/lib/rubocop/cop/rails/exit.rb +4 -10
  37. data/lib/rubocop/cop/rails/file_path.rb +6 -7
  38. data/lib/rubocop/cop/rails/find_by.rb +13 -13
  39. data/lib/rubocop/cop/rails/find_by_id.rb +12 -21
  40. data/lib/rubocop/cop/rails/find_each.rb +19 -18
  41. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
  42. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +37 -6
  43. data/lib/rubocop/cop/rails/helper_instance_variable.rb +29 -3
  44. data/lib/rubocop/cop/rails/http_positional_arguments.rb +32 -21
  45. data/lib/rubocop/cop/rails/http_status.rb +7 -9
  46. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +8 -6
  47. data/lib/rubocop/cop/rails/index_by.rb +3 -2
  48. data/lib/rubocop/cop/rails/index_with.rb +3 -2
  49. data/lib/rubocop/cop/rails/inquiry.rb +4 -3
  50. data/lib/rubocop/cop/rails/inverse_of.rb +3 -2
  51. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -15
  52. data/lib/rubocop/cop/rails/link_to_blank.rb +25 -23
  53. data/lib/rubocop/cop/rails/mailer_name.rb +19 -13
  54. data/lib/rubocop/cop/rails/match_route.rb +14 -13
  55. data/lib/rubocop/cop/rails/negate_include.rb +10 -8
  56. data/lib/rubocop/cop/rails/not_null_column.rb +2 -1
  57. data/lib/rubocop/cop/rails/order_by_id.rb +1 -2
  58. data/lib/rubocop/cop/rails/output.rb +5 -2
  59. data/lib/rubocop/cop/rails/output_safety.rb +3 -2
  60. data/lib/rubocop/cop/rails/pick.rb +14 -12
  61. data/lib/rubocop/cop/rails/pluck.rb +6 -9
  62. data/lib/rubocop/cop/rails/pluck_id.rb +4 -6
  63. data/lib/rubocop/cop/rails/pluck_in_where.rb +7 -7
  64. data/lib/rubocop/cop/rails/pluralization_grammar.rb +10 -14
  65. data/lib/rubocop/cop/rails/presence.rb +12 -13
  66. data/lib/rubocop/cop/rails/present.rb +30 -24
  67. data/lib/rubocop/cop/rails/rake_environment.rb +8 -10
  68. data/lib/rubocop/cop/rails/read_write_attribute.rb +12 -11
  69. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +29 -31
  70. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +9 -12
  71. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +11 -10
  72. data/lib/rubocop/cop/rails/reflection_class_name.rb +17 -3
  73. data/lib/rubocop/cop/rails/refute_methods.rb +9 -10
  74. data/lib/rubocop/cop/rails/relative_date_constant.rb +30 -21
  75. data/lib/rubocop/cop/rails/render_inline.rb +2 -1
  76. data/lib/rubocop/cop/rails/render_plain_text.rb +9 -14
  77. data/lib/rubocop/cop/rails/request_referer.rb +7 -7
  78. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  79. data/lib/rubocop/cop/rails/reversible_migration.rb +4 -8
  80. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +75 -0
  81. data/lib/rubocop/cop/rails/safe_navigation.rb +30 -11
  82. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +5 -10
  83. data/lib/rubocop/cop/rails/save_bang.rb +17 -20
  84. data/lib/rubocop/cop/rails/scope_args.rb +2 -1
  85. data/lib/rubocop/cop/rails/short_i18n.rb +7 -9
  86. data/lib/rubocop/cop/rails/skips_model_validations.rb +4 -4
  87. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +5 -6
  88. data/lib/rubocop/cop/rails/time_zone.rb +35 -25
  89. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  90. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +4 -6
  91. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +4 -2
  92. data/lib/rubocop/cop/rails/unknown_env.rb +3 -3
  93. data/lib/rubocop/cop/rails/validation.rb +15 -14
  94. data/lib/rubocop/cop/rails/where_equals.rb +98 -0
  95. data/lib/rubocop/cop/rails/where_exists.rb +19 -13
  96. data/lib/rubocop/cop/rails/where_not.rb +14 -19
  97. data/lib/rubocop/cop/rails_cops.rb +8 -0
  98. data/lib/rubocop/rails.rb +2 -0
  99. data/lib/rubocop/rails/schema_loader.rb +4 -4
  100. data/lib/rubocop/rails/schema_loader/schema.rb +2 -4
  101. data/lib/rubocop/rails/version.rb +5 -1
  102. metadata +29 -14
@@ -13,6 +13,9 @@ module RuboCop
13
13
  # variable, consider moving the behaviour elsewhere, for
14
14
  # example to a model, decorator or presenter.
15
15
  #
16
+ # Provided that a class inherits `ActionView::Helpers::FormBuilder`,
17
+ # an offense will not be registered.
18
+ #
16
19
  # @example
17
20
  # # bad
18
21
  # def welcome_message
@@ -23,17 +26,40 @@ module RuboCop
23
26
  # def welcome_message(user)
24
27
  # "Hello #{user.name}"
25
28
  # end
26
- class HelperInstanceVariable < Cop
29
+ #
30
+ # # good
31
+ # class MyFormBuilder < ActionView::Helpers::FormBuilder
32
+ # @template.do_something
33
+ # end
34
+ class HelperInstanceVariable < Base
27
35
  MSG = 'Do not use instance variables in helpers.'
28
36
 
37
+ def_node_matcher :form_builder_class?, <<~PATTERN
38
+ (const
39
+ (const
40
+ (const nil? :ActionView) :Helpers) :FormBuilder)
41
+ PATTERN
42
+
29
43
  def on_ivar(node)
44
+ return if inherit_form_builder?(node)
45
+
30
46
  add_offense(node)
31
47
  end
32
48
 
33
49
  def on_ivasgn(node)
34
- return if node.parent.or_asgn_type?
50
+ return if node.parent.or_asgn_type? || inherit_form_builder?(node)
51
+
52
+ add_offense(node.loc.name)
53
+ end
54
+
55
+ private
56
+
57
+ def inherit_form_builder?(node)
58
+ node.each_ancestor(:class) do |class_node|
59
+ return true if form_builder_class?(class_node.parent_class)
60
+ end
35
61
 
36
- add_offense(node, location: :name)
62
+ false
37
63
  end
38
64
  end
39
65
  end
@@ -16,7 +16,10 @@ module RuboCop
16
16
  #
17
17
  # # good
18
18
  # get :new, params: { user_id: 1 }
19
- class HttpPositionalArguments < Cop
19
+ # get :new, **options
20
+ class HttpPositionalArguments < Base
21
+ include RangeHelp
22
+ extend AutoCorrector
20
23
  extend TargetRailsVersion
21
24
 
22
25
  MSG = 'Use keyword arguments instead of ' \
@@ -24,36 +27,37 @@ module RuboCop
24
27
  KEYWORD_ARGS = %i[
25
28
  method params session body flash xhr as headers env to
26
29
  ].freeze
27
- HTTP_METHODS = %i[get post put patch delete head].freeze
30
+ RESTRICT_ON_SEND = %i[get post put patch delete head].freeze
28
31
 
29
32
  minimum_target_rails_version 5.0
30
33
 
31
34
  def_node_matcher :http_request?, <<~PATTERN
32
- (send nil? {#{HTTP_METHODS.map(&:inspect).join(' ')}} !nil? $_ ...)
35
+ (send nil? {#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}} !nil? $_ ...)
36
+ PATTERN
37
+
38
+ def_node_matcher :kwsplat_hash?, <<~PATTERN
39
+ (hash (kwsplat _))
33
40
  PATTERN
34
41
 
35
42
  def on_send(node)
36
43
  http_request?(node) do |data|
37
44
  return unless needs_conversion?(data)
38
45
 
39
- add_offense(node, location: :selector,
40
- message: format(MSG, verb: node.method_name))
41
- end
42
- end
43
-
44
- # given a pre Rails 5 method: get :new, {user_id: @user.id}, {}
45
- #
46
- # @return lambda of auto correct procedure
47
- # the result should look like:
48
- # get :new, params: { user_id: @user.id }, session: {}
49
- # the http_method is the method used to call the controller
50
- # the controller node can be a symbol, method, object or string
51
- # that represents the path/action on the Rails controller
52
- # the data is the http parameters and environment sent in
53
- # the Rails 5 http call
54
- def autocorrect(node)
55
- lambda do |corrector|
56
- corrector.replace(node.loc.expression, correction(node))
46
+ message = format(MSG, verb: node.method_name)
47
+
48
+ add_offense(highlight_range(node), message: message) do |corrector|
49
+ # given a pre Rails 5 method: get :new, {user_id: @user.id}, {}
50
+ #
51
+ # @return lambda of auto correct procedure
52
+ # the result should look like:
53
+ # get :new, params: { user_id: @user.id }, session: {}
54
+ # the http_method is the method used to call the controller
55
+ # the controller node can be a symbol, method, object or string
56
+ # that represents the path/action on the Rails controller
57
+ # the data is the http parameters and environment sent in
58
+ # the Rails 5 http call
59
+ corrector.replace(node.loc.expression, correction(node))
60
+ end
57
61
  end
58
62
  end
59
63
 
@@ -61,6 +65,7 @@ module RuboCop
61
65
 
62
66
  def needs_conversion?(data)
63
67
  return true unless data.hash_type?
68
+ return false if kwsplat_hash?(data)
64
69
 
65
70
  data.each_pair.none? do |pair|
66
71
  special_keyword_arg?(pair.key) ||
@@ -76,6 +81,12 @@ module RuboCop
76
81
  node.sym_type? && node.value == :format
77
82
  end
78
83
 
84
+ def highlight_range(node)
85
+ _http_path, *data = *node.arguments
86
+
87
+ range_between(data.first.source_range.begin_pos, data.last.source_range.end_pos)
88
+ end
89
+
79
90
  def convert_hash_data(data, type)
80
91
  return '' if data.hash_type? && data.empty?
81
92
 
@@ -31,8 +31,11 @@ module RuboCop
31
31
  # render plain: 'foo/bar', status: 304
32
32
  # redirect_to root_url, status: 301
33
33
  #
34
- class HttpStatus < Cop
34
+ class HttpStatus < Base
35
35
  include ConfigurableEnforcedStyle
36
+ extend AutoCorrector
37
+
38
+ RESTRICT_ON_SEND = %i[render redirect_to].freeze
36
39
 
37
40
  def_node_matcher :http_status, <<~PATTERN
38
41
  {
@@ -53,14 +56,9 @@ module RuboCop
53
56
  checker = checker_class.new(status)
54
57
  return unless checker.offensive?
55
58
 
56
- add_offense(checker.node, message: checker.message)
57
- end
58
- end
59
-
60
- def autocorrect(node)
61
- lambda do |corrector|
62
- checker = checker_class.new(node)
63
- corrector.replace(node.loc.expression, checker.preferred_style)
59
+ add_offense(checker.node, message: checker.message) do |corrector|
60
+ corrector.replace(checker.node.loc.expression, checker.preferred_style)
61
+ end
64
62
  end
65
63
  end
66
64
 
@@ -37,18 +37,20 @@ module RuboCop
37
37
  # end
38
38
  #
39
39
  # @see https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options
40
- class IgnoredSkipActionFilterOption < Cop
40
+ class IgnoredSkipActionFilterOption < Base
41
41
  MSG = <<~MSG.chomp.freeze
42
42
  `%<ignore>s` option will be ignored when `%<prefer>s` and `%<ignore>s` are used together.
43
43
  MSG
44
44
 
45
- FILTERS = %w[
46
- :skip_after_action
47
- :skip_around_action
48
- :skip_before_action
49
- :skip_action_callback
45
+ RESTRICT_ON_SEND = %i[
46
+ skip_after_action
47
+ skip_around_action
48
+ skip_before_action
49
+ skip_action_callback
50
50
  ].freeze
51
51
 
52
+ FILTERS = RESTRICT_ON_SEND.map { |method_name| ":#{method_name}" }
53
+
52
54
  def_node_matcher :filter_options, <<~PATTERN
53
55
  (send
54
56
  nil?
@@ -17,14 +17,15 @@ module RuboCop
17
17
  #
18
18
  # # good
19
19
  # [1, 2, 3].index_by { |el| foo(el) }
20
- class IndexBy < Cop
20
+ class IndexBy < Base
21
21
  include IndexMethod
22
+ extend AutoCorrector
22
23
 
23
24
  def_node_matcher :on_bad_each_with_object, <<~PATTERN
24
25
  (block
25
26
  ({send csend} _ :each_with_object (hash))
26
27
  (args (arg $_el) (arg _memo))
27
- ({send csend} (lvar _memo) :[]= $_ (lvar _el)))
28
+ ({send csend} (lvar _memo) :[]= $!`_memo (lvar _el)))
28
29
  PATTERN
29
30
 
30
31
  def_node_matcher :on_bad_to_h, <<~PATTERN
@@ -17,7 +17,8 @@ module RuboCop
17
17
  #
18
18
  # # good
19
19
  # [1, 2, 3].index_with { |el| foo(el) }
20
- class IndexWith < Cop
20
+ class IndexWith < Base
21
+ extend AutoCorrector
21
22
  extend TargetRailsVersion
22
23
  include IndexMethod
23
24
 
@@ -27,7 +28,7 @@ module RuboCop
27
28
  (block
28
29
  ({send csend} _ :each_with_object (hash))
29
30
  (args (arg $_el) (arg _memo))
30
- ({send csend} (lvar _memo) :[]= (lvar _el) $_))
31
+ ({send csend} (lvar _memo) :[]= (lvar _el) $!`_memo))
31
32
  PATTERN
32
33
 
33
34
  def_node_matcher :on_bad_to_h, <<~PATTERN
@@ -22,15 +22,16 @@ module RuboCop
22
22
  # pets = %w(cat dog)
23
23
  # pets.include? 'cat'
24
24
  #
25
- class Inquiry < Cop
25
+ class Inquiry < Base
26
26
  MSG = "Prefer Ruby's comparison operators over Active Support's `inquiry`."
27
+ RESTRICT_ON_SEND = %i[inquiry].freeze
27
28
 
28
29
  def on_send(node)
29
- return unless node.method?(:inquiry) && node.arguments.empty?
30
+ return unless node.arguments.empty?
30
31
  return unless (receiver = node.receiver)
31
32
  return if !receiver.str_type? && !receiver.array_type?
32
33
 
33
- add_offense(node, location: :selector)
34
+ add_offense(node.loc.selector)
34
35
  end
35
36
  end
36
37
  end
@@ -128,10 +128,11 @@ module RuboCop
128
128
  #
129
129
  # @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations
130
130
  # @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
131
- class InverseOf < Cop
131
+ class InverseOf < Base
132
132
  SPECIFY_MSG = 'Specify an `:inverse_of` option.'
133
133
  NIL_MSG = 'You specified `inverse_of: nil`, you probably meant to ' \
134
134
  'use `inverse_of: false`.'
135
+ RESTRICT_ON_SEND = %i[has_many has_one belongs_to].freeze
135
136
 
136
137
  def_node_matcher :association_recv_arguments, <<~PATTERN
137
138
  (send $_ {:has_many :has_one :belongs_to} _ $...)
@@ -185,7 +186,7 @@ module RuboCop
185
186
 
186
187
  return if options_contain_inverse_of?(options)
187
188
 
188
- add_offense(node, message: message(options), location: :selector)
189
+ add_offense(node.loc.selector, message: message(options))
189
190
  end
190
191
 
191
192
  def scope?(arguments)
@@ -82,25 +82,27 @@ module RuboCop
82
82
  # @content = Article.find(params[:article_id])
83
83
  # end
84
84
  # end
85
- class LexicallyScopedActionFilter < Cop
85
+ class LexicallyScopedActionFilter < Base
86
86
  MSG = '%<action>s not explicitly defined on the %<type>s.'
87
87
 
88
- FILTERS = %w[
89
- :after_action
90
- :append_after_action
91
- :append_around_action
92
- :append_before_action
93
- :around_action
94
- :before_action
95
- :prepend_after_action
96
- :prepend_around_action
97
- :prepend_before_action
98
- :skip_after_action
99
- :skip_around_action
100
- :skip_before_action
101
- :skip_action_callback
88
+ RESTRICT_ON_SEND = %i[
89
+ after_action
90
+ append_after_action
91
+ append_around_action
92
+ append_before_action
93
+ around_action
94
+ before_action
95
+ prepend_after_action
96
+ prepend_around_action
97
+ prepend_before_action
98
+ skip_after_action
99
+ skip_around_action
100
+ skip_before_action
101
+ skip_action_callback
102
102
  ].freeze
103
103
 
104
+ FILTERS = RESTRICT_ON_SEND.map { |method_name| ":#{method_name}" }
105
+
104
106
  def_node_matcher :only_or_except_filter_methods, <<~PATTERN
105
107
  (send
106
108
  nil?
@@ -20,8 +20,11 @@ module RuboCop
20
20
  #
21
21
  # # good
22
22
  # link_to 'Click here', url, target: '_blank', rel: 'noreferrer'
23
- class LinkToBlank < Cop
23
+ class LinkToBlank < Base
24
+ extend AutoCorrector
25
+
24
26
  MSG = 'Specify a `:rel` option containing noopener.'
27
+ RESTRICT_ON_SEND = %i[link_to].freeze
25
28
 
26
29
  def_node_matcher :blank_target?, <<~PATTERN
27
30
  (pair {(sym :target) (str "target")} {(str "_blank") (sym :_blank)})
@@ -35,39 +38,34 @@ module RuboCop
35
38
  (pair {(sym :rel) (str "rel")} (str _))
36
39
  PATTERN
37
40
 
38
- # rubocop:disable Metrics/CyclomaticComplexity
39
41
  def on_send(node)
40
- return unless node.method?(:link_to)
41
-
42
42
  option_nodes = node.each_child_node(:hash)
43
43
 
44
44
  option_nodes.map(&:children).each do |options|
45
45
  blank = options.find { |o| blank_target?(o) }
46
- add_offense(blank) if blank && options.none? { |o| includes_noopener?(o) }
46
+ next unless blank && options.none? { |o| includes_noopener?(o) }
47
+
48
+ add_offense(blank) do |corrector|
49
+ autocorrect(corrector, node, blank, option_nodes)
50
+ end
47
51
  end
48
52
  end
49
- # rubocop:enable Metrics/CyclomaticComplexity
50
53
 
51
- def autocorrect(node)
52
- lambda do |corrector|
53
- send_node = node.parent.parent
54
+ private
54
55
 
55
- option_nodes = send_node.each_child_node(:hash)
56
- rel_node = nil
57
- option_nodes.map(&:children).each do |options|
58
- rel_node ||= options.find { |o| rel_node?(o) }
59
- end
56
+ def autocorrect(corrector, send_node, node, option_nodes)
57
+ rel_node = nil
58
+ option_nodes.map(&:children).each do |options|
59
+ rel_node ||= options.find { |o| rel_node?(o) }
60
+ end
60
61
 
61
- if rel_node
62
- append_to_rel(rel_node, corrector)
63
- else
64
- add_rel(send_node, node, corrector)
65
- end
62
+ if rel_node
63
+ append_to_rel(rel_node, corrector)
64
+ else
65
+ add_rel(send_node, node, corrector)
66
66
  end
67
67
  end
68
68
 
69
- private
70
-
71
69
  def append_to_rel(rel_node, corrector)
72
70
  existing_rel = rel_node.children.last.value
73
71
  str_range = rel_node.children.last.loc.expression.adjust(
@@ -81,7 +79,11 @@ module RuboCop
81
79
  opening_quote = offence_node.children.last.source[0]
82
80
  closing_quote = opening_quote == ':' ? '' : opening_quote
83
81
  new_rel_exp = ", rel: #{opening_quote}noopener#{closing_quote}"
84
- range = send_node.arguments.last.source_range
82
+ range = if (last_argument = send_node.last_argument).hash_type?
83
+ last_argument.pairs.last.source_range
84
+ else
85
+ last_argument.source_range
86
+ end
85
87
 
86
88
  corrector.insert_after(range, new_rel_exp)
87
89
  end
@@ -89,7 +91,7 @@ module RuboCop
89
91
  def contains_noopener?(value)
90
92
  return false unless value
91
93
 
92
- rel_array = value.to_s.split(' ')
94
+ rel_array = value.to_s.split
93
95
  rel_array.include?('noopener') || rel_array.include?('noreferrer')
94
96
  end
95
97
  end
@@ -23,7 +23,9 @@ module RuboCop
23
23
  # class UserMailer < ApplicationMailer
24
24
  # end
25
25
  #
26
- class MailerName < Cop
26
+ class MailerName < Base
27
+ extend AutoCorrector
28
+
27
29
  MSG = 'Mailer should end with `Mailer` suffix.'
28
30
 
29
31
  def_node_matcher :mailer_base_class?, <<~PATTERN
@@ -43,7 +45,9 @@ module RuboCop
43
45
 
44
46
  def on_class(node)
45
47
  class_definition?(node) do |name_node|
46
- add_offense(name_node)
48
+ add_offense(name_node) do |corrector|
49
+ autocorrect(corrector, name_node)
50
+ end
47
51
  end
48
52
  end
49
53
 
@@ -54,23 +58,25 @@ module RuboCop
54
58
  return unless casgn_parent
55
59
 
56
60
  name = casgn_parent.children[1]
57
- add_offense(casgn_parent, location: :name) unless mailer_suffix?(name)
58
- end
61
+ return if mailer_suffix?(name)
59
62
 
60
- def autocorrect(node)
61
- lambda do |corrector|
62
- if node.casgn_type?
63
- name = node.children[1]
64
- corrector.replace(node.loc.name, "#{name}Mailer")
65
- else
66
- name = node.children.last
67
- corrector.replace(node.source_range, "#{name}Mailer")
68
- end
63
+ add_offense(casgn_parent.loc.name) do |corrector|
64
+ autocorrect(corrector, casgn_parent)
69
65
  end
70
66
  end
71
67
 
72
68
  private
73
69
 
70
+ def autocorrect(corrector, node)
71
+ if node.casgn_type?
72
+ name = node.children[1]
73
+ corrector.replace(node.loc.name, "#{name}Mailer")
74
+ else
75
+ name = node.children.last
76
+ corrector.replace(node.source_range, "#{name}Mailer")
77
+ end
78
+ end
79
+
74
80
  def mailer_suffix?(mailer_name)
75
81
  mailer_name.to_s.end_with?('Mailer')
76
82
  end