rubocop-rails 2.8.1 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +18 -2
  4. data/config/default.yml +144 -6
  5. data/config/obsoletion.yml +7 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +15 -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/add_column_index.rb +64 -0
  15. data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
  16. data/lib/rubocop/cop/rails/application_controller.rb +3 -7
  17. data/lib/rubocop/cop/rails/application_job.rb +2 -1
  18. data/lib/rubocop/cop/rails/application_mailer.rb +2 -7
  19. data/lib/rubocop/cop/rails/application_record.rb +2 -7
  20. data/lib/rubocop/cop/rails/arel_star.rb +41 -0
  21. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  22. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  23. data/lib/rubocop/cop/rails/belongs_to.rb +10 -19
  24. data/lib/rubocop/cop/rails/blank.rb +31 -27
  25. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  26. data/lib/rubocop/cop/rails/content_tag.rb +33 -18
  27. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +2 -1
  28. data/lib/rubocop/cop/rails/date.rb +27 -17
  29. data/lib/rubocop/cop/rails/default_scope.rb +11 -4
  30. data/lib/rubocop/cop/rails/delegate.rb +9 -9
  31. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +7 -8
  32. data/lib/rubocop/cop/rails/dynamic_find_by.rb +16 -13
  33. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +78 -0
  34. data/lib/rubocop/cop/rails/enum_hash.rb +11 -10
  35. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -1
  36. data/lib/rubocop/cop/rails/environment_comparison.rb +18 -14
  37. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  38. data/lib/rubocop/cop/rails/exit.rb +4 -10
  39. data/lib/rubocop/cop/rails/expanded_date_range.rb +86 -0
  40. data/lib/rubocop/cop/rails/file_path.rb +6 -7
  41. data/lib/rubocop/cop/rails/find_by.rb +32 -24
  42. data/lib/rubocop/cop/rails/find_by_id.rb +12 -21
  43. data/lib/rubocop/cop/rails/find_each.rb +19 -18
  44. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
  45. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +37 -6
  46. data/lib/rubocop/cop/rails/helper_instance_variable.rb +29 -3
  47. data/lib/rubocop/cop/rails/http_positional_arguments.rb +26 -21
  48. data/lib/rubocop/cop/rails/http_status.rb +18 -11
  49. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +37 -0
  50. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +8 -6
  51. data/lib/rubocop/cop/rails/index_by.rb +2 -1
  52. data/lib/rubocop/cop/rails/index_with.rb +2 -1
  53. data/lib/rubocop/cop/rails/inquiry.rb +4 -3
  54. data/lib/rubocop/cop/rails/inverse_of.rb +3 -2
  55. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -15
  56. data/lib/rubocop/cop/rails/link_to_blank.rb +25 -23
  57. data/lib/rubocop/cop/rails/mailer_name.rb +19 -13
  58. data/lib/rubocop/cop/rails/match_route.rb +14 -13
  59. data/lib/rubocop/cop/rails/negate_include.rb +10 -8
  60. data/lib/rubocop/cop/rails/not_null_column.rb +2 -1
  61. data/lib/rubocop/cop/rails/order_by_id.rb +1 -2
  62. data/lib/rubocop/cop/rails/output.rb +5 -2
  63. data/lib/rubocop/cop/rails/output_safety.rb +3 -2
  64. data/lib/rubocop/cop/rails/pick.rb +14 -12
  65. data/lib/rubocop/cop/rails/pluck.rb +6 -9
  66. data/lib/rubocop/cop/rails/pluck_id.rb +4 -6
  67. data/lib/rubocop/cop/rails/pluck_in_where.rb +7 -7
  68. data/lib/rubocop/cop/rails/pluralization_grammar.rb +10 -14
  69. data/lib/rubocop/cop/rails/presence.rb +12 -13
  70. data/lib/rubocop/cop/rails/present.rb +30 -24
  71. data/lib/rubocop/cop/rails/rake_environment.rb +8 -10
  72. data/lib/rubocop/cop/rails/read_write_attribute.rb +12 -11
  73. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +29 -31
  74. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +9 -12
  75. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +11 -10
  76. data/lib/rubocop/cop/rails/reflection_class_name.rb +17 -3
  77. data/lib/rubocop/cop/rails/refute_methods.rb +9 -10
  78. data/lib/rubocop/cop/rails/relative_date_constant.rb +34 -27
  79. data/lib/rubocop/cop/rails/render_inline.rb +2 -1
  80. data/lib/rubocop/cop/rails/render_plain_text.rb +9 -14
  81. data/lib/rubocop/cop/rails/request_referer.rb +7 -7
  82. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  83. data/lib/rubocop/cop/rails/reversible_migration.rb +3 -7
  84. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +75 -0
  85. data/lib/rubocop/cop/rails/safe_navigation.rb +30 -11
  86. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +5 -10
  87. data/lib/rubocop/cop/rails/save_bang.rb +17 -20
  88. data/lib/rubocop/cop/rails/scope_args.rb +2 -1
  89. data/lib/rubocop/cop/rails/short_i18n.rb +7 -9
  90. data/lib/rubocop/cop/rails/skips_model_validations.rb +4 -4
  91. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +5 -6
  92. data/lib/rubocop/cop/rails/time_zone.rb +44 -42
  93. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  94. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +4 -6
  95. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +2 -2
  96. data/lib/rubocop/cop/rails/unknown_env.rb +3 -3
  97. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +69 -0
  98. data/lib/rubocop/cop/rails/validation.rb +15 -14
  99. data/lib/rubocop/cop/rails/where_equals.rb +98 -0
  100. data/lib/rubocop/cop/rails/where_exists.rb +19 -13
  101. data/lib/rubocop/cop/rails/where_not.rb +10 -17
  102. data/lib/rubocop/cop/rails_cops.rb +13 -0
  103. data/lib/rubocop/rails.rb +2 -0
  104. data/lib/rubocop/rails/schema_loader.rb +4 -4
  105. data/lib/rubocop/rails/schema_loader/schema.rb +3 -5
  106. data/lib/rubocop/rails/version.rb +5 -1
  107. metadata +34 -14
@@ -17,7 +17,9 @@ module RuboCop
17
17
  # # good
18
18
  # get :new, params: { user_id: 1 }
19
19
  # get :new, **options
20
- class HttpPositionalArguments < Cop
20
+ class HttpPositionalArguments < Base
21
+ include RangeHelp
22
+ extend AutoCorrector
21
23
  extend TargetRailsVersion
22
24
 
23
25
  MSG = 'Use keyword arguments instead of ' \
@@ -25,12 +27,12 @@ module RuboCop
25
27
  KEYWORD_ARGS = %i[
26
28
  method params session body flash xhr as headers env to
27
29
  ].freeze
28
- HTTP_METHODS = %i[get post put patch delete head].freeze
30
+ RESTRICT_ON_SEND = %i[get post put patch delete head].freeze
29
31
 
30
32
  minimum_target_rails_version 5.0
31
33
 
32
34
  def_node_matcher :http_request?, <<~PATTERN
33
- (send nil? {#{HTTP_METHODS.map(&:inspect).join(' ')}} !nil? $_ ...)
35
+ (send nil? {#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}} !nil? $_ ...)
34
36
  PATTERN
35
37
 
36
38
  def_node_matcher :kwsplat_hash?, <<~PATTERN
@@ -41,24 +43,21 @@ module RuboCop
41
43
  http_request?(node) do |data|
42
44
  return unless needs_conversion?(data)
43
45
 
44
- add_offense(node, location: :selector,
45
- message: format(MSG, verb: node.method_name))
46
- end
47
- end
48
-
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
- def autocorrect(node)
60
- lambda do |corrector|
61
- 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
62
61
  end
63
62
  end
64
63
 
@@ -82,6 +81,12 @@ module RuboCop
82
81
  node.sym_type? && node.value == :format
83
82
  end
84
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
+
85
90
  def convert_hash_data(data, type)
86
91
  return '' if data.hash_type? && data.empty?
87
92
 
@@ -11,12 +11,14 @@ module RuboCop
11
11
  # render json: { foo: 'bar' }, status: 200
12
12
  # render plain: 'foo/bar', status: 304
13
13
  # redirect_to root_url, status: 301
14
+ # head 200
14
15
  #
15
16
  # # good
16
17
  # render :foo, status: :ok
17
18
  # render json: { foo: 'bar' }, status: :ok
18
19
  # render plain: 'foo/bar', status: :not_modified
19
20
  # redirect_to root_url, status: :moved_permanently
21
+ # head :ok
20
22
  #
21
23
  # @example EnforcedStyle: numeric
22
24
  # # bad
@@ -24,20 +26,26 @@ module RuboCop
24
26
  # render json: { foo: 'bar' }, status: :not_found
25
27
  # render plain: 'foo/bar', status: :not_modified
26
28
  # redirect_to root_url, status: :moved_permanently
29
+ # head :ok
27
30
  #
28
31
  # # good
29
32
  # render :foo, status: 200
30
33
  # render json: { foo: 'bar' }, status: 404
31
34
  # render plain: 'foo/bar', status: 304
32
35
  # redirect_to root_url, status: 301
36
+ # head 200
33
37
  #
34
- class HttpStatus < Cop
38
+ class HttpStatus < Base
35
39
  include ConfigurableEnforcedStyle
40
+ extend AutoCorrector
41
+
42
+ RESTRICT_ON_SEND = %i[render redirect_to head].freeze
36
43
 
37
44
  def_node_matcher :http_status, <<~PATTERN
38
45
  {
39
46
  (send nil? {:render :redirect_to} _ $hash)
40
47
  (send nil? {:render :redirect_to} $hash)
48
+ (send nil? :head ${int sym} ...)
41
49
  }
42
50
  PATTERN
43
51
 
@@ -46,21 +54,20 @@ module RuboCop
46
54
  PATTERN
47
55
 
48
56
  def on_send(node)
49
- http_status(node) do |hash_node|
50
- status = status_code(hash_node)
57
+ http_status(node) do |hash_node_or_status_code|
58
+ status = if hash_node_or_status_code.hash_type?
59
+ status_code(hash_node_or_status_code)
60
+ else
61
+ hash_node_or_status_code
62
+ end
51
63
  return unless status
52
64
 
53
65
  checker = checker_class.new(status)
54
66
  return unless checker.offensive?
55
67
 
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)
68
+ add_offense(checker.node, message: checker.message) do |corrector|
69
+ corrector.replace(checker.node.loc.expression, checker.preferred_style)
70
+ end
64
71
  end
65
72
  end
66
73
 
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks for the use of `I18n.locale=` method.
7
+ #
8
+ # The `locale` attribute persists for the rest of the Ruby runtime, potentially causing
9
+ # unexpected behavior at a later time.
10
+ # Using `I18n.with_locale` ensures the code passed in the block is the only place `I18n.locale` is affected.
11
+ # It eliminates the possibility of a `locale` sticking around longer than intended.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # I18n.locale = :fr
16
+ #
17
+ # # good
18
+ # I18n.with_locale(:fr) do
19
+ # end
20
+ #
21
+ class I18nLocaleAssignment < Base
22
+ MSG = 'Use `I18n.with_locale` with block instead of `I18n.locale=`.'
23
+ RESTRICT_ON_SEND = %i[locale=].freeze
24
+
25
+ def_node_matcher :i18n_locale_assignment?, <<~PATTERN
26
+ (send (const {nil? cbase} :I18n) :locale= ...)
27
+ PATTERN
28
+
29
+ def on_send(node)
30
+ return unless i18n_locale_assignment?(node)
31
+
32
+ add_offense(node)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -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,8 +17,9 @@ 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
@@ -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
 
@@ -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