rbhint 0.85.1.rc2 → 0.87.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -1
  3. data/bin/rubocop-profile +16 -0
  4. data/config/default.yml +118 -10
  5. data/lib/rbhint/version.rb +1 -1
  6. data/lib/rubocop.rb +15 -1
  7. data/lib/rubocop/cli.rb +0 -2
  8. data/lib/rubocop/cli/command/auto_genenerate_config.rb +40 -5
  9. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  10. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  11. data/lib/rubocop/config_loader.rb +24 -66
  12. data/lib/rubocop/config_obsoletion.rb +0 -1
  13. data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
  14. data/lib/rubocop/cop/badge.rb +1 -1
  15. data/lib/rubocop/cop/base.rb +407 -0
  16. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  17. data/lib/rubocop/cop/commissioner.rb +48 -50
  18. data/lib/rubocop/cop/cop.rb +85 -236
  19. data/lib/rubocop/cop/corrector.rb +38 -115
  20. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
  21. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +6 -1
  22. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  23. data/lib/rubocop/cop/generator.rb +1 -1
  24. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  25. data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
  26. data/lib/rubocop/cop/layout/class_structure.rb +2 -37
  27. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  28. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -8
  29. data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
  30. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -0
  31. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -2
  32. data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
  33. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
  34. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
  35. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +3 -2
  36. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  37. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  38. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
  39. data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
  40. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  41. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
  42. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  43. data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
  44. data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
  45. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  46. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
  47. data/lib/rubocop/cop/lint/raise_exception.rb +8 -0
  48. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  49. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +27 -23
  50. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  51. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  52. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +8 -0
  53. data/lib/rubocop/cop/lint/syntax.rb +11 -26
  54. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  55. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
  56. data/lib/rubocop/cop/metrics/block_length.rb +24 -2
  57. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  58. data/lib/rubocop/cop/metrics/class_length.rb +26 -3
  59. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  60. data/lib/rubocop/cop/metrics/method_length.rb +24 -1
  61. data/lib/rubocop/cop/metrics/module_length.rb +26 -3
  62. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
  63. data/lib/rubocop/cop/metrics/perceived_complexity.rb +3 -3
  64. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  65. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  66. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  67. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  68. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  69. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  70. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  71. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
  72. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  73. data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -2
  74. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  75. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -2
  76. data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
  77. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  78. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +2 -2
  79. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  80. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  81. data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
  82. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  83. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  84. data/lib/rubocop/cop/offense.rb +16 -2
  85. data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
  86. data/lib/rubocop/cop/style/array_coercion.rb +63 -0
  87. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
  88. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
  89. data/lib/rubocop/cop/style/case_like_if.rb +217 -0
  90. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  91. data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
  92. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  93. data/lib/rubocop/cop/style/date_time.rb +1 -1
  94. data/lib/rubocop/cop/style/dir.rb +2 -2
  95. data/lib/rubocop/cop/style/empty_literal.rb +5 -5
  96. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  97. data/lib/rubocop/cop/style/exponential_notation.rb +6 -8
  98. data/lib/rubocop/cop/style/float_division.rb +7 -10
  99. data/lib/rubocop/cop/style/format_string_token.rb +5 -5
  100. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
  101. data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
  102. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -11
  103. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  104. data/lib/rubocop/cop/style/missing_else.rb +1 -11
  105. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  106. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  107. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  108. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -4
  109. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
  110. data/lib/rubocop/cop/style/proc.rb +1 -1
  111. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  112. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  113. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  114. data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
  115. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
  116. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  117. data/lib/rubocop/cop/style/redundant_parentheses.rb +7 -1
  118. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -1
  119. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -2
  120. data/lib/rubocop/cop/style/redundant_sort.rb +3 -2
  121. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  122. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  123. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
  124. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  125. data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
  126. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  127. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
  128. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  129. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  130. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  131. data/lib/rubocop/cop/team.rb +97 -81
  132. data/lib/rubocop/cop/utils/format_string.rb +1 -2
  133. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  134. data/lib/rubocop/file_finder.rb +4 -4
  135. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  136. data/lib/rubocop/name_similarity.rb +1 -3
  137. data/lib/rubocop/options.rb +15 -8
  138. data/lib/rubocop/path_util.rb +2 -17
  139. data/lib/rubocop/rake_task.rb +6 -9
  140. data/lib/rubocop/result_cache.rb +9 -5
  141. data/lib/rubocop/rspec/cop_helper.rb +4 -4
  142. data/lib/rubocop/rspec/expect_offense.rb +52 -22
  143. data/lib/rubocop/rspec/shared_contexts.rb +8 -8
  144. data/lib/rubocop/runner.rb +33 -32
  145. data/lib/rubocop/target_ruby.rb +1 -1
  146. data/lib/rubocop/version.rb +1 -1
  147. metadata +25 -8
  148. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -8,117 +8,25 @@ module RuboCop
8
8
  # Important!
9
9
  # The nodes modified by the corrections should be part of the
10
10
  # AST of the source_buffer.
11
- class Corrector
11
+ class Corrector < ::Parser::Source::TreeRewriter
12
+ # @param source [Parser::Source::Buffer, or anything
13
+ # leading to one via `(processed_source.)buffer`]
12
14
  #
13
- # @param source_buffer [Parser::Source::Buffer]
14
- # @param corrections [Array(#call)]
15
- # Array of Objects that respond to #call. They will receive the
16
- # corrector itself and should use its method to modify the source.
17
- #
18
- # @example
19
- #
20
- # class AndOrCorrector
21
- # def initialize(node)
22
- # @node = node
23
- # end
24
- #
25
- # def call(corrector)
26
- # replacement = (@node.type == :and ? '&&' : '||')
27
- # corrector.replace(@node.loc.operator, replacement)
28
- # end
29
- # end
30
- #
31
- # corrections = [AndOrCorrector.new(node)]
32
- # corrector = Corrector.new(source_buffer, corrections)
33
- def initialize(source_buffer, corrections = [])
34
- @source_buffer = source_buffer
35
- raise 'source_buffer should be a Parser::Source::Buffer' unless \
36
- source_buffer.is_a? Parser::Source::Buffer
37
-
38
- @corrections = corrections
39
- @source_rewriter = Parser::Source::TreeRewriter.new(
40
- source_buffer,
15
+ # corrector = Corrector.new(cop)
16
+ def initialize(source)
17
+ source = self.class.source_buffer(source)
18
+ super(
19
+ source,
41
20
  different_replacements: :raise,
42
21
  swallowed_insertions: :raise,
43
22
  crossing_deletions: :accept
44
23
  )
45
24
 
46
- @diagnostics = []
47
25
  # Don't print warnings to stderr if corrections conflict with each other
48
- @source_rewriter.diagnostics.consumer = lambda do |diagnostic|
49
- @diagnostics << diagnostic
50
- end
51
- end
52
-
53
- attr_reader :corrections, :diagnostics
54
-
55
- # Does the actual rewrite and returns string corresponding to
56
- # the rewritten source.
57
- #
58
- # @return [String]
59
- def rewrite
60
- @corrections.each do |correction|
61
- begin
62
- @source_rewriter.transaction do
63
- correction.call(self)
64
- end
65
- rescue ErrorWithAnalyzedFileLocation => e
66
- raise e unless e.cause.is_a?(::Parser::ClobberingError)
67
- end
68
- end
69
-
70
- @source_rewriter.process
71
- end
72
-
73
- # Removes the source range.
74
- #
75
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
76
- def remove(node_or_range)
77
- range = to_range(node_or_range)
78
- @source_rewriter.remove(range)
79
- end
80
-
81
- # Inserts new code before the given source range.
82
- #
83
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
84
- # @param [String] content
85
- def insert_before(node_or_range, content)
86
- range = to_range(node_or_range)
87
- # TODO: Fix Cops using bad ranges instead
88
- if range.end_pos > @source_buffer.source.size
89
- range = range.with(end_pos: @source_buffer.source.size)
90
- end
91
-
92
- @source_rewriter.insert_before(range, content)
93
- end
94
-
95
- # Inserts new code after the given source range.
96
- #
97
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
98
- # @param [String] content
99
- def insert_after(node_or_range, content)
100
- range = to_range(node_or_range)
101
- @source_rewriter.insert_after(range, content)
26
+ diagnostics.consumer = ->(diagnostic) {}
102
27
  end
103
28
 
104
- # Wraps the given source range with the given before and after texts
105
- #
106
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
107
- # @param [String] before
108
- # @param [String] after
109
- def wrap(node_or_range, before, after)
110
- range = to_range(node_or_range)
111
- @source_rewriter.wrap(range, before, after)
112
- end
113
-
114
- # Replaces the code of the source range `range` with `content`.
115
- #
116
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
117
- # @param [String] content
118
- def replace(node_or_range, content)
119
- range = to_range(node_or_range)
120
- @source_rewriter.replace(range, content)
121
- end
29
+ alias rewrite process # Legacy
122
30
 
123
31
  # Removes `size` characters prior to the source range.
124
32
  #
@@ -126,10 +34,11 @@ module RuboCop
126
34
  # @param [Integer] size
127
35
  def remove_preceding(node_or_range, size)
128
36
  range = to_range(node_or_range)
129
- to_remove = Parser::Source::Range.new(range.source_buffer,
130
- range.begin_pos - size,
131
- range.begin_pos)
132
- @source_rewriter.remove(to_remove)
37
+ to_remove = range.with(
38
+ begin_pos: range.begin_pos - size,
39
+ end_pos: range.begin_pos
40
+ )
41
+ remove(to_remove)
133
42
  end
134
43
 
135
44
  # Removes `size` characters from the beginning of the given range.
@@ -140,10 +49,8 @@ module RuboCop
140
49
  # @param [Integer] size
141
50
  def remove_leading(node_or_range, size)
142
51
  range = to_range(node_or_range)
143
- to_remove = Parser::Source::Range.new(range.source_buffer,
144
- range.begin_pos,
145
- range.begin_pos + size)
146
- @source_rewriter.remove(to_remove)
52
+ to_remove = range.with(end_pos: range.begin_pos + size)
53
+ remove(to_remove)
147
54
  end
148
55
 
149
56
  # Removes `size` characters from the end of the given range.
@@ -154,10 +61,22 @@ module RuboCop
154
61
  # @param [Integer] size
155
62
  def remove_trailing(node_or_range, size)
156
63
  range = to_range(node_or_range)
157
- to_remove = Parser::Source::Range.new(range.source_buffer,
158
- range.end_pos - size,
159
- range.end_pos)
160
- @source_rewriter.remove(to_remove)
64
+ to_remove = range.with(begin_pos: range.end_pos - size)
65
+ remove(to_remove)
66
+ end
67
+
68
+ # Duck typing for get to a ::Parser::Source::Buffer
69
+ def self.source_buffer(source)
70
+ source = source.processed_source if source.respond_to?(:processed_source)
71
+ source = source.buffer if source.respond_to?(:buffer)
72
+ source = source.source_buffer if source.respond_to?(:source_buffer)
73
+
74
+ unless source.is_a? ::Parser::Source::Buffer
75
+ raise TypeError, 'Expected argument to lead to a Parser::Source::Buffer ' \
76
+ "but got #{source.inspect}"
77
+ end
78
+
79
+ source
161
80
  end
162
81
 
163
82
  private
@@ -178,8 +97,12 @@ module RuboCop
178
97
  range
179
98
  end
180
99
 
100
+ def check_range_validity(node_or_range)
101
+ super(to_range(node_or_range))
102
+ end
103
+
181
104
  def validate_buffer(buffer)
182
- return if buffer == @source_buffer
105
+ return if buffer == source_buffer
183
106
 
184
107
  unless buffer.is_a?(::Parser::Source::Buffer)
185
108
  # actually this should be enforced by parser gem
@@ -42,8 +42,34 @@ module RuboCop
42
42
 
43
43
  corrector.insert_before(
44
44
  last_element_range_with_trailing_comma(node).end,
45
+ content_if_comment_present(corrector, node)
46
+ )
47
+ end
48
+
49
+ def content_if_comment_present(corrector, node)
50
+ range = range_with_surrounding_space(
51
+ range: children(node).last.source_range,
52
+ side: :right
53
+ ).end.resize(1)
54
+ if range.source == '#'
55
+ select_content_to_be_inserted_after_last_element(corrector, node)
56
+ else
45
57
  node.loc.end.source
58
+ end
59
+ end
60
+
61
+ def select_content_to_be_inserted_after_last_element(corrector, node)
62
+ range = range_between(
63
+ node.loc.end.begin_pos,
64
+ range_by_whole_lines(node.loc.expression).end.end_pos
46
65
  )
66
+
67
+ remove_trailing_content_of_comment(corrector, range)
68
+ range.source
69
+ end
70
+
71
+ def remove_trailing_content_of_comment(corrector, range)
72
+ corrector.remove(range)
47
73
  end
48
74
 
49
75
  def last_element_range_with_trailing_comma(node)
@@ -35,6 +35,11 @@ module RuboCop
35
35
  # Gem::Specification.new do |spec|
36
36
  # spec.required_ruby_version = ['>= 2.5.0', '< 2.7.0']
37
37
  # end
38
+ #
39
+ # # good
40
+ # Gem::Specification.new do |spec|
41
+ # spec.required_ruby_version = '~> 2.5'
42
+ # end
38
43
  class RequiredRubyVersion < Cop
39
44
  MSG = '`required_ruby_version` (%<required_ruby_version>s, ' \
40
45
  'declared in %<gemspec_filename>s) and `TargetRubyVersion` ' \
@@ -68,7 +73,7 @@ module RuboCop
68
73
  end
69
74
  end
70
75
 
71
- required_ruby_version.str_content.match(/(\d\.\d)/)[1]
76
+ required_ruby_version.str_content.scan(/\d/).first(2).join('.')
72
77
  end
73
78
 
74
79
  def message(required_ruby_version, target_ruby_version)
@@ -28,7 +28,7 @@ module RuboCop
28
28
  class RubyVersionGlobalsUsage < Cop
29
29
  MSG = 'Do not use `RUBY_VERSION` in gemspec file.'
30
30
 
31
- def_node_matcher :ruby_version?, '(const nil? :RUBY_VERSION)'
31
+ def_node_matcher :ruby_version?, '(const {cbase nil?} :RUBY_VERSION)'
32
32
 
33
33
  def_node_search :gem_specification?, <<~PATTERN
34
34
  (block
@@ -54,7 +54,7 @@ module RuboCop
54
54
  # # good
55
55
  # good_foo_method(args)
56
56
  #
57
- class %<cop_name>s < Cop
57
+ class %<cop_name>s < Base
58
58
  # TODO: Implement the cop in here.
59
59
  #
60
60
  # In many cases, you can use a node matcher for matching node pattern.
@@ -13,7 +13,9 @@ module RuboCop
13
13
  # # good
14
14
  # node.send_type?
15
15
  #
16
- class NodeTypePredicate < Cop
16
+ class NodeTypePredicate < Base
17
+ extend AutoCorrector
18
+
17
19
  MSG = 'Use `#%<type>s_type?` to check node type.'
18
20
 
19
21
  def_node_matcher :node_type_check, <<~PATTERN
@@ -21,21 +23,16 @@ module RuboCop
21
23
  PATTERN
22
24
 
23
25
  def on_send(node)
24
- node_type_check(node) do |_receiver, node_type|
26
+ node_type_check(node) do |receiver, node_type|
25
27
  return unless Parser::Meta::NODE_TYPES.include?(node_type)
26
28
 
27
- add_offense(node, message: format(MSG, type: node_type))
28
- end
29
- end
30
-
31
- def autocorrect(node)
32
- receiver, node_type = node_type_check(node)
33
- range = Parser::Source::Range.new(node.source_range.source_buffer,
34
- receiver.loc.expression.end_pos + 1,
35
- node.loc.expression.end_pos)
36
-
37
- lambda do |corrector|
38
- corrector.replace(range, "#{node_type}_type?")
29
+ message = format(MSG, type: node_type)
30
+ add_offense(node, message: message) do |corrector|
31
+ range = node.loc.expression.with(
32
+ begin_pos: receiver.loc.expression.end_pos + 1
33
+ )
34
+ corrector.replace(range, "#{node_type}_type?")
35
+ end
39
36
  end
40
37
  end
41
38
  end
@@ -67,10 +67,11 @@ module RuboCop
67
67
  # else
68
68
  # y / 3
69
69
  # end
70
- class CaseIndentation < Cop
70
+ class CaseIndentation < Base
71
71
  include Alignment
72
72
  include ConfigurableEnforcedStyle
73
73
  include RangeHelp
74
+ extend AutoCorrector
74
75
 
75
76
  MSG = 'Indent `when` %<depth>s `%<base>s`.'
76
77
 
@@ -82,16 +83,6 @@ module RuboCop
82
83
  end
83
84
  end
84
85
 
85
- def autocorrect(node)
86
- whitespace = whitespace_range(node)
87
-
88
- return false unless whitespace.source.strip.empty?
89
-
90
- lambda do |corrector|
91
- corrector.replace(whitespace, replacement(node))
92
- end
93
- end
94
-
95
86
  private
96
87
 
97
88
  def check_when(when_node)
@@ -114,22 +105,30 @@ module RuboCop
114
105
  end
115
106
 
116
107
  def incorrect_style(when_node)
108
+ add_offense(when_node.loc.keyword) do |corrector|
109
+ detect_incorrect_style(when_node)
110
+
111
+ whitespace = whitespace_range(when_node)
112
+
113
+ corrector.replace(whitespace, replacement(when_node)) if whitespace.source.strip.empty?
114
+ end
115
+ end
116
+
117
+ def detect_incorrect_style(when_node)
117
118
  when_column = when_node.loc.keyword.column
118
119
  base_column = base_column(when_node.parent, alternative_style)
119
120
 
120
- add_offense(when_node, location: :keyword, message: message(style)) do
121
- if when_column == base_column
122
- opposite_style_detected
123
- else
124
- unrecognized_style_detected
125
- end
121
+ if when_column == base_column
122
+ opposite_style_detected
123
+ else
124
+ unrecognized_style_detected
126
125
  end
127
126
  end
128
127
 
129
- def message(base)
128
+ def find_message(*)
130
129
  depth = indent_one_step? ? 'one step more than' : 'as deep as'
131
130
 
132
- format(MSG, depth: depth, base: base)
131
+ format(MSG, depth: depth, base: style)
133
132
  end
134
133
 
135
134
  def base_column(case_node, base)
@@ -134,6 +134,8 @@ module RuboCop
134
134
  #
135
135
  # @see https://rubystyle.guide#consistent-classes
136
136
  class ClassStructure < Cop
137
+ include VisibilityHelp
138
+
137
139
  HUMANIZED_NODE_TYPE = {
138
140
  casgn: :constants,
139
141
  defs: :class_methods,
@@ -141,14 +143,9 @@ module RuboCop
141
143
  sclass: :class_singleton
142
144
  }.freeze
143
145
 
144
- VISIBILITY_SCOPES = %i[private protected public].freeze
145
146
  MSG = '`%<category>s` is supposed to appear before ' \
146
147
  '`%<previous>s`.'
147
148
 
148
- def_node_matcher :visibility_block?, <<~PATTERN
149
- (send nil? { :private :protected :public })
150
- PATTERN
151
-
152
149
  # Validates code style on class declaration.
153
150
  # Add offense when find a node out of expected order.
154
151
  def on_class(class_node)
@@ -243,38 +240,6 @@ module RuboCop
243
240
  expected_order.index(classification).nil?
244
241
  end
245
242
 
246
- def node_visibility(node)
247
- scope = find_visibility_start(node)
248
- scope&.method_name || :public
249
- end
250
-
251
- def find_visibility_start(node)
252
- left_siblings_of(node)
253
- .reverse
254
- .find(&method(:visibility_block?))
255
- end
256
-
257
- # Navigate to find the last protected method
258
- def find_visibility_end(node)
259
- possible_visibilities = VISIBILITY_SCOPES - [node_visibility(node)]
260
- right = right_siblings_of(node)
261
- right.find do |child_node|
262
- possible_visibilities.include?(node_visibility(child_node))
263
- end || right.last
264
- end
265
-
266
- def siblings_of(node)
267
- node.parent.children
268
- end
269
-
270
- def right_siblings_of(node)
271
- siblings_of(node)[node.sibling_index..-1]
272
- end
273
-
274
- def left_siblings_of(node)
275
- siblings_of(node)[0, node.sibling_index]
276
- end
277
-
278
243
  def humanize_node(node)
279
244
  if node.def_type?
280
245
  return :initializer if node.method?(:initialize)
@@ -109,6 +109,7 @@ module RuboCop
109
109
 
110
110
  def allowed_only_before_style?(node)
111
111
  if node.special_modifier?
112
+ return true if processed_source[node.last_line] == 'end'
112
113
  return false if next_line_empty?(node.last_line)
113
114
  end
114
115