rubocop 0.89.0 → 0.89.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  4. data/lib/rubocop/comment_config.rb +3 -5
  5. data/lib/rubocop/config.rb +1 -1
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_validator.rb +1 -1
  8. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  10. data/lib/rubocop/cop/layout/class_structure.rb +3 -7
  11. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  12. data/lib/rubocop/cop/layout/extra_spacing.rb +14 -21
  13. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  14. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -1
  15. data/lib/rubocop/cop/lint/missing_super.rb +9 -9
  16. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +33 -4
  17. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +14 -17
  18. data/lib/rubocop/cop/lint/uri_regexp.rb +9 -25
  19. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  20. data/lib/rubocop/cop/mixin/alignment.rb +2 -1
  21. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  22. data/lib/rubocop/cop/mixin/enforce_superclass.rb +0 -2
  23. data/lib/rubocop/cop/mixin/hash_transform_method.rb +5 -11
  24. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -3
  25. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -2
  26. data/lib/rubocop/cop/mixin/percent_array.rb +2 -6
  27. data/lib/rubocop/cop/mixin/range_help.rb +18 -4
  28. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  29. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  30. data/lib/rubocop/cop/style/access_modifier_declarations.rb +5 -8
  31. data/lib/rubocop/cop/style/accessor_grouping.rb +20 -20
  32. data/lib/rubocop/cop/style/alias.rb +34 -33
  33. data/lib/rubocop/cop/style/and_or.rb +9 -11
  34. data/lib/rubocop/cop/style/array_join.rb +6 -8
  35. data/lib/rubocop/cop/style/ascii_comments.rb +4 -4
  36. data/lib/rubocop/cop/style/attr.rb +11 -9
  37. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -5
  38. data/lib/rubocop/cop/style/bare_percent_literals.rb +10 -12
  39. data/lib/rubocop/cop/style/begin_block.rb +2 -2
  40. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +9 -12
  41. data/lib/rubocop/cop/style/block_comments.rb +14 -18
  42. data/lib/rubocop/cop/style/block_delimiters.rb +23 -23
  43. data/lib/rubocop/cop/style/case_like_if.rb +17 -14
  44. data/lib/rubocop/cop/style/class_and_module_children.rb +14 -11
  45. data/lib/rubocop/cop/style/class_check.rb +7 -9
  46. data/lib/rubocop/cop/style/class_methods.rb +7 -11
  47. data/lib/rubocop/cop/style/class_vars.rb +6 -10
  48. data/lib/rubocop/cop/style/collection_methods.rb +11 -17
  49. data/lib/rubocop/cop/style/colon_method_call.rb +6 -7
  50. data/lib/rubocop/cop/style/colon_method_definition.rb +6 -6
  51. data/lib/rubocop/cop/style/command_literal.rb +23 -24
  52. data/lib/rubocop/cop/style/comment_annotation.rb +15 -15
  53. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  54. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  55. data/lib/rubocop/cop/style/constant_visibility.rb +3 -2
  56. data/lib/rubocop/cop/style/copyright.rb +12 -12
  57. data/lib/rubocop/cop/style/date_time.rb +1 -1
  58. data/lib/rubocop/cop/style/def_with_parentheses.rb +8 -10
  59. data/lib/rubocop/cop/style/dir.rb +7 -10
  60. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +7 -9
  61. data/lib/rubocop/cop/style/documentation.rb +2 -4
  62. data/lib/rubocop/cop/style/documentation_method.rb +1 -1
  63. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +12 -15
  64. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  65. data/lib/rubocop/cop/style/each_for_simple_loop.rb +5 -8
  66. data/lib/rubocop/cop/style/each_with_object.rb +16 -17
  67. data/lib/rubocop/cop/style/empty_case_condition.rb +19 -20
  68. data/lib/rubocop/cop/style/empty_else.rb +17 -19
  69. data/lib/rubocop/cop/style/empty_literal.rb +15 -16
  70. data/lib/rubocop/cop/style/empty_method.rb +5 -8
  71. data/lib/rubocop/cop/style/encoding.rb +5 -9
  72. data/lib/rubocop/cop/style/end_block.rb +4 -6
  73. data/lib/rubocop/cop/style/eval_with_location.rb +5 -7
  74. data/lib/rubocop/cop/style/even_odd.rb +7 -11
  75. data/lib/rubocop/cop/style/expand_path_arguments.rb +21 -24
  76. data/lib/rubocop/cop/style/exponential_notation.rb +1 -1
  77. data/lib/rubocop/cop/style/float_division.rb +1 -1
  78. data/lib/rubocop/cop/style/for.rb +11 -15
  79. data/lib/rubocop/cop/style/format_string.rb +17 -19
  80. data/lib/rubocop/cop/style/format_string_token.rb +4 -7
  81. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +19 -41
  82. data/lib/rubocop/cop/style/global_vars.rb +2 -2
  83. data/lib/rubocop/cop/style/guard_clause.rb +3 -4
  84. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  85. data/lib/rubocop/cop/style/hash_each_methods.rb +5 -8
  86. data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
  87. data/lib/rubocop/cop/style/hash_transform_keys.rb +3 -2
  88. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -1
  89. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  90. data/lib/rubocop/cop/style/if_inside_else.rb +2 -2
  91. data/lib/rubocop/cop/style/if_unless_modifier.rb +10 -12
  92. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -7
  93. data/lib/rubocop/cop/style/if_with_semicolon.rb +3 -6
  94. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -1
  95. data/lib/rubocop/cop/style/infinite_loop.rb +23 -23
  96. data/lib/rubocop/cop/style/inline_comment.rb +3 -3
  97. data/lib/rubocop/cop/style/inverse_methods.rb +20 -29
  98. data/lib/rubocop/cop/style/lambda.rb +7 -12
  99. data/lib/rubocop/cop/style/lambda_call.rb +14 -13
  100. data/lib/rubocop/cop/style/line_end_concatenation.rb +19 -16
  101. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -1
  102. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -11
  103. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +4 -8
  104. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +7 -6
  105. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -2
  106. data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -16
  107. data/lib/rubocop/cop/style/min_max.rb +8 -12
  108. data/lib/rubocop/cop/style/missing_else.rb +13 -13
  109. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +1 -1
  110. data/lib/rubocop/cop/style/mixin_grouping.rb +24 -27
  111. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  112. data/lib/rubocop/cop/style/module_function.rb +10 -13
  113. data/lib/rubocop/cop/style/multiline_if_modifier.rb +3 -10
  114. data/lib/rubocop/cop/style/multiline_if_then.rb +4 -10
  115. data/lib/rubocop/cop/style/multiline_memoization.rb +12 -10
  116. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  117. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +4 -6
  118. data/lib/rubocop/cop/style/multiline_when_then.rb +7 -9
  119. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +23 -20
  121. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +3 -1
  122. data/lib/rubocop/cop/style/redundant_sort.rb +0 -2
  123. data/lib/rubocop/cop/style/safe_navigation.rb +4 -4
  124. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  125. data/lib/rubocop/cop/util.rb +2 -0
  126. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  127. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  128. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  129. data/lib/rubocop/options.rb +3 -3
  130. data/lib/rubocop/rspec/shared_contexts.rb +1 -5
  131. data/lib/rubocop/runner.rb +1 -1
  132. data/lib/rubocop/version.rb +1 -1
  133. metadata +4 -4
@@ -17,20 +17,18 @@ module RuboCop
17
17
  # # good
18
18
  # %w(foo bar baz).join(",")
19
19
  #
20
- class ArrayJoin < Cop
20
+ class ArrayJoin < Base
21
+ extend AutoCorrector
22
+
21
23
  MSG = 'Favor `Array#join` over `Array#*`.'
22
24
 
23
25
  def_node_matcher :join_candidate?, '(send $array :* $str)'
24
26
 
25
27
  def on_send(node)
26
- join_candidate?(node) { add_offense(node, location: :selector) }
27
- end
28
-
29
- def autocorrect(node)
30
- array, join_arg = join_candidate?(node).map(&:source)
28
+ return unless (array, join_arg = join_candidate?(node))
31
29
 
32
- lambda do |corrector|
33
- corrector.replace(node, "#{array}.join(#{join_arg})")
30
+ add_offense(node.loc.selector) do |corrector|
31
+ corrector.replace(node, "#{array.source}.join(#{join_arg.source})")
34
32
  end
35
33
  end
36
34
  end
@@ -15,17 +15,17 @@ module RuboCop
15
15
  #
16
16
  # # good
17
17
  # # Translates from English to Japanese
18
- class AsciiComments < Cop
18
+ class AsciiComments < Base
19
19
  include RangeHelp
20
20
 
21
21
  MSG = 'Use only ascii symbols in comments.'
22
22
 
23
- def investigate(processed_source)
24
- processed_source.each_comment do |comment|
23
+ def on_new_investigation
24
+ processed_source.comments.each do |comment|
25
25
  next if comment.text.ascii_only?
26
26
  next if only_allowed_non_ascii_chars?(comment.text)
27
27
 
28
- add_offense(comment, location: first_offense_range(comment))
28
+ add_offense(first_offense_range(comment))
29
29
  end
30
30
  end
31
31
 
@@ -14,8 +14,9 @@ module RuboCop
14
14
  # attr_accessor :something
15
15
  # attr_reader :one, :two, :three
16
16
  #
17
- class Attr < Cop
17
+ class Attr < Base
18
18
  include RangeHelp
19
+ extend AutoCorrector
19
20
 
20
21
  MSG = 'Do not use `attr`. Use `%<replacement>s` instead.'
21
22
 
@@ -26,10 +27,15 @@ module RuboCop
26
27
  !node.parent.class_type? &&
27
28
  !class_eval?(node.parent)
28
29
 
29
- add_offense(node, location: :selector)
30
+ message = message(node)
31
+ add_offense(node.loc.selector, message: message) do |corrector|
32
+ autocorrect(corrector, node)
33
+ end
30
34
  end
31
35
 
32
- def autocorrect(node)
36
+ private
37
+
38
+ def autocorrect(corrector, node)
33
39
  attr_name, setter = *node.arguments
34
40
 
35
41
  node_expr = node.source_range
@@ -37,14 +43,10 @@ module RuboCop
37
43
 
38
44
  remove = range_between(attr_expr.end_pos, node_expr.end_pos) if setter&.boolean_type?
39
45
 
40
- lambda do |corrector|
41
- corrector.replace(node.loc.selector, replacement_method(node))
42
- corrector.remove(remove) if remove
43
- end
46
+ corrector.replace(node.loc.selector, replacement_method(node))
47
+ corrector.remove(remove) if remove
44
48
  end
45
49
 
46
- private
47
-
48
50
  def message(node)
49
51
  format(MSG, replacement: replacement_method(node))
50
52
  end
@@ -16,7 +16,7 @@ module RuboCop
16
16
  # File.open('file') do |f|
17
17
  # # ...
18
18
  # end
19
- class AutoResourceCleanup < Cop
19
+ class AutoResourceCleanup < Base
20
20
  MSG = 'Use the block version of `%<class>s.%<method>s`.'
21
21
 
22
22
  TARGET_METHODS = {
@@ -32,10 +32,7 @@ module RuboCop
32
32
 
33
33
  next if cleanup?(node)
34
34
 
35
- add_offense(node,
36
- message: format(MSG,
37
- class: target_class,
38
- method: target_method))
35
+ add_offense(node, message: format(MSG, class: target_class, method: target_method))
39
36
  end
40
37
  end
41
38
 
@@ -23,8 +23,9 @@ module RuboCop
23
23
  # %Q|He said: "#{greeting}"|
24
24
  # %q/She said: 'Hi'/
25
25
  #
26
- class BarePercentLiterals < Cop
26
+ class BarePercentLiterals < Base
27
27
  include ConfigurableEnforcedStyle
28
+ extend AutoCorrector
28
29
 
29
30
  MSG = 'Use `%%%<good>s` instead of `%%%<bad>s`.'
30
31
 
@@ -36,14 +37,6 @@ module RuboCop
36
37
  check(node)
37
38
  end
38
39
 
39
- def autocorrect(node)
40
- src = node.loc.begin.source
41
- replacement = src.start_with?('%Q') ? '%' : '%Q'
42
- lambda do |corrector|
43
- corrector.replace(node.loc.begin, src.sub(/%Q?/, replacement))
44
- end
45
- end
46
-
47
40
  private
48
41
 
49
42
  def check(node)
@@ -68,9 +61,14 @@ module RuboCop
68
61
  end
69
62
 
70
63
  def add_offense_for_wrong_style(node, good, bad)
71
- add_offense(node, location: :begin, message: format(MSG,
72
- good: good,
73
- bad: bad))
64
+ location = node.loc.begin
65
+
66
+ add_offense(location, message: format(MSG, good: good, bad: bad)) do |corrector|
67
+ source = location.source
68
+ replacement = source.start_with?('%Q') ? '%' : '%Q'
69
+
70
+ corrector.replace(location, source.sub(/%Q?/, replacement))
71
+ end
74
72
  end
75
73
  end
76
74
  end
@@ -10,11 +10,11 @@ module RuboCop
10
10
  # # bad
11
11
  # BEGIN { test }
12
12
  #
13
- class BeginBlock < Cop
13
+ class BeginBlock < Base
14
14
  MSG = 'Avoid the use of `BEGIN` blocks.'
15
15
 
16
16
  def on_preexe(node)
17
- add_offense(node, location: :keyword)
17
+ add_offense(node.loc.keyword)
18
18
  end
19
19
  end
20
20
  end
@@ -18,8 +18,9 @@ module RuboCop
18
18
  # attr_accessor :bar
19
19
  # end
20
20
  #
21
- class BisectedAttrAccessor < Cop
21
+ class BisectedAttrAccessor < Base
22
22
  include VisibilityHelp
23
+ extend AutoCorrector
23
24
 
24
25
  MSG = 'Combine both accessors into `attr_accessor %<name>s`.'
25
26
 
@@ -36,14 +37,6 @@ module RuboCop
36
37
  alias on_sclass on_class
37
38
  alias on_module on_class
38
39
 
39
- def autocorrect(node)
40
- macro = node.parent
41
-
42
- lambda do |corrector|
43
- corrector.replace(macro, replacement(macro, node))
44
- end
45
- end
46
-
47
40
  private
48
41
 
49
42
  def accessor_names(class_node, visibility)
@@ -97,9 +90,13 @@ module RuboCop
97
90
  macro.arguments.each do |arg_node|
98
91
  name = arg_node.source
99
92
 
100
- if (attr_reader?(macro) && writer_names.include?(name)) ||
101
- (attr_writer?(macro) && reader_names.include?(name))
102
- add_offense(arg_node, message: format(MSG, name: name))
93
+ next unless (attr_reader?(macro) && writer_names.include?(name)) ||
94
+ (attr_writer?(macro) && reader_names.include?(name))
95
+
96
+ add_offense(arg_node, message: format(MSG, name: name)) do |corrector|
97
+ macro = arg_node.parent
98
+
99
+ corrector.replace(macro, replacement(macro, arg_node))
103
100
  end
104
101
  end
105
102
  end
@@ -16,34 +16,30 @@ module RuboCop
16
16
  # # Multiple lines
17
17
  # # of comments...
18
18
  #
19
- class BlockComments < Cop
19
+ class BlockComments < Base
20
20
  include RangeHelp
21
+ extend AutoCorrector
21
22
 
22
23
  MSG = 'Do not use block comments.'
23
24
  BEGIN_LENGTH = "=begin\n".length
24
25
  END_LENGTH = "\n=end".length
25
26
 
26
- def investigate(processed_source)
27
- processed_source.each_comment do |comment|
27
+ def on_new_investigation
28
+ processed_source.comments.each do |comment|
28
29
  next unless comment.document?
29
30
 
30
- add_offense(comment)
31
- end
32
- end
33
-
34
- def autocorrect(comment)
35
- eq_begin, eq_end, contents = parts(comment)
31
+ add_offense(comment) do |corrector|
32
+ eq_begin, eq_end, contents = parts(comment)
36
33
 
37
- lambda do |corrector|
38
- corrector.remove(eq_begin)
39
- unless contents.length.zero?
40
- corrector.replace(contents,
41
- contents.source
42
- .gsub(/\A/, '# ')
43
- .gsub(/\n\n/, "\n#\n")
44
- .gsub(/\n(?=[^#])/, "\n# "))
34
+ corrector.remove(eq_begin)
35
+ unless contents.length.zero?
36
+ corrector.replace(
37
+ contents,
38
+ contents.source.gsub(/\A/, '# ').gsub(/\n\n/, "\n#\n").gsub(/\n(?=[^#])/, "\n# ")
39
+ )
40
+ end
41
+ corrector.remove(eq_end)
45
42
  end
46
- corrector.remove(eq_end)
47
43
  end
48
44
  end
49
45
 
@@ -132,14 +132,14 @@ module RuboCop
132
132
  # puts foo
133
133
  # end
134
134
  #
135
- class BlockDelimiters < Cop
135
+ class BlockDelimiters < Base
136
136
  include ConfigurableEnforcedStyle
137
137
  include IgnoredMethods
138
+ extend AutoCorrector
138
139
 
139
140
  ALWAYS_BRACES_MESSAGE = 'Prefer `{...}` over `do...end` for blocks.'
140
141
 
141
- BRACES_REQUIRED_MESSAGE = 'Brace delimiters `{...}` required for ' \
142
- "'%<method_name>s' method."
142
+ BRACES_REQUIRED_MESSAGE = "Brace delimiters `{...}` required for '%<method_name>s' method."
143
143
 
144
144
  def on_send(node)
145
145
  return unless node.arguments?
@@ -158,22 +158,26 @@ module RuboCop
158
158
 
159
159
  def on_block(node)
160
160
  return if ignored_node?(node)
161
+ return if proper_block_style?(node)
161
162
 
162
- add_offense(node, location: :begin) unless proper_block_style?(node)
163
+ message = message(node)
164
+ add_offense(node.loc.begin, message: message) do |corrector|
165
+ autocorrect(corrector, node)
166
+ end
163
167
  end
164
168
 
165
- def autocorrect(node)
169
+ private
170
+
171
+ def autocorrect(corrector, node)
166
172
  return if correction_would_break_code?(node)
167
173
 
168
174
  if node.braces?
169
- replace_braces_with_do_end(node.loc)
175
+ replace_braces_with_do_end(corrector, node.loc)
170
176
  else
171
- replace_do_end_with_braces(node.loc)
177
+ replace_do_end_with_braces(corrector, node.loc)
172
178
  end
173
179
  end
174
180
 
175
- private
176
-
177
181
  def line_count_based_message(node)
178
182
  if node.multiline?
179
183
  'Avoid using `{...}` for multi-line blocks.'
@@ -219,29 +223,25 @@ module RuboCop
219
223
  end
220
224
  end
221
225
 
222
- def replace_braces_with_do_end(loc)
226
+ def replace_braces_with_do_end(corrector, loc)
223
227
  b = loc.begin
224
228
  e = loc.end
225
229
 
226
- lambda do |corrector|
227
- corrector.insert_before(b, ' ') unless whitespace_before?(b)
228
- corrector.insert_before(e, ' ') unless whitespace_before?(e)
229
- corrector.insert_after(b, ' ') unless whitespace_after?(b)
230
- corrector.replace(b, 'do')
231
- corrector.replace(e, 'end')
232
- end
230
+ corrector.insert_before(b, ' ') unless whitespace_before?(b)
231
+ corrector.insert_before(e, ' ') unless whitespace_before?(e)
232
+ corrector.insert_after(b, ' ') unless whitespace_after?(b)
233
+ corrector.replace(b, 'do')
234
+ corrector.replace(e, 'end')
233
235
  end
234
236
 
235
- def replace_do_end_with_braces(loc)
237
+ def replace_do_end_with_braces(corrector, loc)
236
238
  b = loc.begin
237
239
  e = loc.end
238
240
 
239
- lambda do |corrector|
240
- corrector.insert_after(b, ' ') unless whitespace_after?(b, 2)
241
+ corrector.insert_after(b, ' ') unless whitespace_after?(b, 2)
241
242
 
242
- corrector.replace(b, '{')
243
- corrector.replace(e, '}')
244
- end
243
+ corrector.replace(b, '{')
244
+ corrector.replace(e, '}')
245
245
  end
246
246
 
247
247
  def whitespace_before?(range)
@@ -26,8 +26,9 @@ module RuboCop
26
26
  # final_action
27
27
  # end
28
28
  #
29
- class CaseLikeIf < Cop
29
+ class CaseLikeIf < Base
30
30
  include RangeHelp
31
+ extend AutoCorrector
31
32
 
32
33
  MSG = 'Convert `if-elsif` to `case-when`.'
33
34
 
@@ -46,28 +47,30 @@ module RuboCop
46
47
  break unless convertible
47
48
  end
48
49
 
49
- add_offense(node) if convertible
50
+ return unless convertible
51
+
52
+ add_offense(node) do |corrector|
53
+ autocorrect(corrector, node)
54
+ end
50
55
  end
51
56
 
52
- def autocorrect(node)
57
+ private
58
+
59
+ def autocorrect(corrector, node)
53
60
  target = find_target(node.condition)
54
61
 
55
- lambda do |corrector|
56
- corrector.insert_before(node, "case #{target.source}\n#{indent(node)}")
62
+ corrector.insert_before(node, "case #{target.source}\n#{indent(node)}")
57
63
 
58
- branch_conditions(node).each do |branch_condition|
59
- conditions = []
60
- collect_conditions(branch_condition, target, conditions)
64
+ branch_conditions(node).each do |branch_condition|
65
+ conditions = []
66
+ collect_conditions(branch_condition, target, conditions)
61
67
 
62
- range = correction_range(branch_condition)
63
- branch_replacement = "when #{conditions.map(&:source).join(', ')}"
64
- corrector.replace(range, branch_replacement)
65
- end
68
+ range = correction_range(branch_condition)
69
+ branch_replacement = "when #{conditions.map(&:source).join(', ')}"
70
+ corrector.replace(range, branch_replacement)
66
71
  end
67
72
  end
68
73
 
69
- private
70
-
71
74
  def should_check?(node)
72
75
  !node.unless? && !node.elsif? && !node.modifier_form? && !node.ternary? &&
73
76
  node.elsif_conditional?
@@ -21,9 +21,10 @@ module RuboCop
21
21
  # end
22
22
  #
23
23
  # The compact style is only forced for classes/modules with one child.
24
- class ClassAndModuleChildren < Cop
24
+ class ClassAndModuleChildren < Base
25
25
  include ConfigurableEnforcedStyle
26
26
  include RangeHelp
27
+ extend AutoCorrector
27
28
 
28
29
  NESTED_MSG = 'Use nested module/class definitions instead of ' \
29
30
  'compact style.'
@@ -40,14 +41,6 @@ module RuboCop
40
41
  check_style(node, node.body)
41
42
  end
42
43
 
43
- def autocorrect(node)
44
- lambda do |corrector|
45
- return if node.class_type? && node.parent_class && style != :nested
46
-
47
- nest_or_compact(corrector, node)
48
- end
49
- end
50
-
51
44
  private
52
45
 
53
46
  def nest_or_compact(corrector, node)
@@ -129,13 +122,23 @@ module RuboCop
129
122
  def check_nested_style(node)
130
123
  return unless compact_node_name?(node)
131
124
 
132
- add_offense(node, location: :name, message: NESTED_MSG)
125
+ add_offense(node.loc.name, message: NESTED_MSG) do |corrector|
126
+ autocorrect(corrector, node)
127
+ end
133
128
  end
134
129
 
135
130
  def check_compact_style(node, body)
136
131
  return unless one_child?(body) && !compact_node_name?(node)
137
132
 
138
- add_offense(node, location: :name, message: COMPACT_MSG)
133
+ add_offense(node.loc.name, message: COMPACT_MSG) do |corrector|
134
+ autocorrect(corrector, node)
135
+ end
136
+ end
137
+
138
+ def autocorrect(corrector, node)
139
+ return if node.class_type? && node.parent_class && style != :nested
140
+
141
+ nest_or_compact(corrector, node)
139
142
  end
140
143
 
141
144
  def one_child?(body)