rubocop 1.92.2 → 1.92.3

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 (136) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/#directive_comment.rb# +224 -0
  3. data/lib/rubocop/cache_config.rb +3 -0
  4. data/lib/rubocop/cli.rb +5 -4
  5. data/lib/rubocop/config.rb +16 -0
  6. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  7. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  8. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  9. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  10. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  11. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  12. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  13. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  14. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  15. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +14 -6
  16. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  17. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  18. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  19. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  20. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  21. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  22. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  23. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  24. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  25. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  26. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  27. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  28. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  29. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  30. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  31. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  32. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  33. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  34. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  35. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  36. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  37. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  38. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  39. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  40. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  41. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  42. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  43. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  44. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  45. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  46. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  47. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  48. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  49. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  50. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  51. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  52. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  53. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  54. data/lib/rubocop/cop/lint/void.rb +20 -9
  55. data/lib/rubocop/cop/message_annotator.rb +6 -8
  56. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  57. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  58. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  59. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  60. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  61. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  62. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  63. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  64. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  65. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  66. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  67. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  68. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  69. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  70. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  71. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  72. data/lib/rubocop/cop/style/and_or.rb +8 -1
  73. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  74. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  75. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  76. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  77. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  78. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  79. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  80. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  81. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  82. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  83. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  84. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  85. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  86. data/lib/rubocop/cop/style/for.rb +59 -4
  87. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  88. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  89. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  90. data/lib/rubocop/cop/style/hash_lookup_method.rb +11 -2
  91. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  92. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  93. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  94. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  95. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  96. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  97. data/lib/rubocop/cop/style/lambda.rb +13 -5
  98. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  99. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  100. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  101. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  102. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  103. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  104. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  105. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  106. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  107. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  108. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  109. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  110. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  111. data/lib/rubocop/cop/style/redundant_begin.rb +96 -16
  112. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  113. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  114. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  115. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  116. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  117. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  118. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  119. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  120. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  121. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  122. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  123. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  124. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  125. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  126. data/lib/rubocop/cop/style/word_array.rb +1 -3
  127. data/lib/rubocop/cop/util.rb +2 -0
  128. data/lib/rubocop/cop/variable_force.rb +14 -1
  129. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  130. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  131. data/lib/rubocop/result_cache.rb +9 -3
  132. data/lib/rubocop/runner.rb +5 -0
  133. data/lib/rubocop/server/cache.rb +24 -5
  134. data/lib/rubocop/todo_audit.rb +61 -15
  135. data/lib/rubocop/version.rb +1 -1
  136. metadata +4 -3
@@ -174,7 +174,7 @@ module RuboCop
174
174
  # end
175
175
  # end
176
176
  #
177
- class ClassStructure < Base
177
+ class ClassStructure < Base # rubocop:disable Metrics/ClassLength
178
178
  include VisibilityHelp
179
179
  include CommentsHelp
180
180
  extend AutoCorrector
@@ -191,9 +191,19 @@ module RuboCop
191
191
  # A moved element is inserted before the line break that precedes its anchor.
192
192
  # When the anchor follows a blank line, `Layout/EmptyLines`,
193
193
  # `Layout/EmptyLinesAroundAccessModifier` or `Layout/EmptyLinesAroundClassBody`
194
- # may remove that line break in the same pass.
194
+ # may remove that line break in the same pass. A moved element is removed with the
195
+ # line break before it, and `Style/AccessModifierDeclarations`,
196
+ # `Style/BisectedAttrAccessor` or `Style/RedundantInitialize` may remove the same
197
+ # element with the line break after it, joining the lines around it.
195
198
  def self.autocorrect_incompatible_with
196
- [Layout::EmptyLines, Layout::EmptyLinesAroundAccessModifier, Layout::EmptyLinesAroundClassBody]
199
+ [
200
+ Layout::EmptyLines,
201
+ Layout::EmptyLinesAroundAccessModifier,
202
+ Layout::EmptyLinesAroundClassBody,
203
+ Style::AccessModifierDeclarations,
204
+ Style::BisectedAttrAccessor,
205
+ Style::RedundantInitialize
206
+ ]
197
207
  end
198
208
 
199
209
  # Validates code style on class declaration.
@@ -204,14 +214,17 @@ module RuboCop
204
214
  # Consecutive elements of the same category are reported only once,
205
215
  # on the first element of the group.
206
216
  def on_class(class_node)
217
+ out_of_order = out_of_order_elements(class_node)
218
+ collapsing_nodes = collapsing_nodes(out_of_order.map(&:first))
219
+
207
220
  # Corrections are registered in reverse source order because an insertion at
208
221
  # a given position lands before any insertion already made there;
209
222
  # this keeps the source order of nodes moved before the same anchor.
210
- out_of_order_elements(class_node).reverse_each do |node, category, previous|
223
+ out_of_order.reverse_each do |node, category, previous|
211
224
  message = format(MSG, category: category, previous: previous)
212
225
 
213
226
  add_offense(node, message: message) do |corrector|
214
- autocorrect(corrector, node)
227
+ autocorrect(corrector, node, collapsing_nodes)
215
228
  end
216
229
  end
217
230
  end
@@ -234,24 +247,89 @@ module RuboCop
234
247
  out_of_order
235
248
  end
236
249
 
250
+ # The moved elements whose removal takes the blank lines above them. An offense disabled
251
+ # by a directive moves nothing. An element that is another's insertion anchor keeps its
252
+ # blanks, as the insertion would otherwise land inside the removal.
253
+ def collapsing_nodes(nodes)
254
+ moving = nodes.select do |node|
255
+ enabled_lines?(node.source_range) && moved_group(node).any?
256
+ end
257
+ moved = moving.flat_map { |node| moved_group(node) }.to_set
258
+ anchors = moving.map { |node| insertion_anchor(node) }
259
+
260
+ moved - anchors - separators(moved)
261
+ end
262
+
237
263
  # Autocorrect by moving the node, together with the contiguous group of
238
264
  # same-category elements that follows it, to its expected position.
239
- def autocorrect(corrector, node)
240
- return if dynamic_constant?(node)
241
-
242
- anchor = insertion_anchor(node)
243
- return unless anchor
265
+ def autocorrect(corrector, node, collapsing_nodes)
266
+ group = moved_group(node)
267
+ return if group.empty?
244
268
 
245
- anchor_range = source_range_with_comment(anchor)
269
+ anchor_range = source_range_with_comment(insertion_anchor(node))
246
270
  # Reversed for the same reason offenses are registered in reverse source order:
247
271
  # the last insertion at a position comes first.
248
- movable_group(node).reverse_each do |group_node|
272
+ group.reverse_each do |group_node|
249
273
  current_range = source_range_with_comment(group_node)
250
274
  corrector.insert_before(anchor_range, current_range.source)
251
- corrector.remove(current_range)
275
+ corrector.remove(vacated_range(group_node, current_range, collapsing_nodes))
252
276
  end
253
277
  end
254
278
 
279
+ # The node's movable group, or none when the node itself cannot be moved.
280
+ def moved_group(node)
281
+ return [] if dynamic_constant?(node) || !insertion_anchor(node)
282
+
283
+ movable_group(node)
284
+ end
285
+
286
+ # The node's own lines together with the blank lines above it, which would otherwise
287
+ # join whatever follows.
288
+ def vacated_range(node, range, collapsing_nodes)
289
+ return range unless collapsing_nodes.include?(node)
290
+
291
+ # The range starts on the line break that ends the line above the node.
292
+ line = range.first_line
293
+ line -= 1 while buffer.source_line(line).blank?
294
+ range.with(begin_pos: buffer.line_range(line).end_pos)
295
+ end
296
+
297
+ # The moved elements whose blank lines above them are all that would separate the code
298
+ # around their run: in each run that a remaining element or a comment directly follows,
299
+ # the first one with blank lines above it.
300
+ def separators(moved_nodes)
301
+ moved_runs(moved_nodes).filter_map do |run|
302
+ run.find { |node| blank_line_above?(node) } if directly_followed?(run.last)
303
+ end
304
+ end
305
+
306
+ # The moved elements in runs of siblings with only blank lines between them, since a
307
+ # comment between two of them stays behind.
308
+ def moved_runs(moved_nodes)
309
+ moved_nodes.sort_by { |node| node.source_range.begin_pos }.chunk_while do |first, second|
310
+ first.right_sibling.equal?(second) && blank_between?(first, second)
311
+ end
312
+ end
313
+
314
+ def blank_between?(first, second)
315
+ from = buffer.line_for_position(end_position_for(first)) + 1
316
+ to = buffer.line_for_position(begin_pos_with_comment(second) + 1) - 1
317
+ (from..to).all? { |line| buffer.source_line(line).blank? }
318
+ end
319
+
320
+ # Whether the line after the node holds a remaining element or a comment.
321
+ def directly_followed?(node)
322
+ line = buffer.line_for_position(end_position_for(node)) + 1
323
+ return false if buffer.source_line(line).blank?
324
+
325
+ !node.right_sibling.nil? || whole_line_comment_at_line?(line)
326
+ end
327
+
328
+ # Whether the line above the node, or above the comment attached to it, is blank.
329
+ def blank_line_above?(node)
330
+ buffer.source_line(source_range_with_comment(node).first_line).blank?
331
+ end
332
+
255
333
  # Classifies a node to match with something in the {expected_order}
256
334
  # @param node to be analysed
257
335
  # @return String when the node type is a `:block` then
@@ -65,11 +65,15 @@ module RuboCop
65
65
  private
66
66
 
67
67
  def autocorrect(corrector, node)
68
- if style == :start_of_line && node.parent&.send_type?
69
- AlignmentCorrector.align_end(corrector, processed_source, node, node.parent)
70
- else
71
- AlignmentCorrector.align_end(corrector, processed_source, node, node)
72
- end
68
+ align_to = style == :start_of_line ? outermost_def_modifier(node) : node
69
+
70
+ AlignmentCorrector.align_end(corrector, processed_source, node, align_to)
71
+ end
72
+
73
+ def outermost_def_modifier(node)
74
+ modifiers = node.each_ancestor(:send).take_while { |send| send.def_modifier.equal?(node) }
75
+
76
+ modifiers.last || node
73
77
  end
74
78
  end
75
79
  end
@@ -37,6 +37,8 @@ module RuboCop
37
37
 
38
38
  MSG = 'Align `%<else_range>s` with `%<base_range>s`.'
39
39
 
40
+ alias on_csend on_send
41
+
40
42
  def on_if(node, base = nil)
41
43
  return if ignored_node?(node)
42
44
  return unless node.else? && begins_its_line?(node.loc.else)
@@ -111,10 +111,10 @@ module RuboCop
111
111
  end
112
112
 
113
113
  def correct_style?(node)
114
- !node.if_branch&.guard_clause? ||
115
- next_line_rescue_or_ensure?(node) ||
116
- next_sibling_parent_empty_or_else?(node) ||
117
- next_sibling_empty_or_guard_clause?(node)
114
+ return true unless node.if_branch&.guard_clause?
115
+ return true unless node.parent&.type?(:begin, :kwbegin)
116
+
117
+ next_sibling_empty_or_guard_clause?(node)
118
118
  end
119
119
 
120
120
  def contains_guard_clause?(node)
@@ -140,20 +140,6 @@ module RuboCop
140
140
  DirectiveComment.new(comment).enabled? || simplecov_directive_comment?(comment)
141
141
  end
142
142
 
143
- def next_line_rescue_or_ensure?(node)
144
- parent = node.parent
145
- parent.nil? || parent.rescue_type? || parent.ensure_type?
146
- end
147
-
148
- def next_sibling_parent_empty_or_else?(node)
149
- next_sibling = node.right_sibling
150
- return true unless next_sibling.is_a?(AST::Node)
151
-
152
- parent = next_sibling.parent
153
-
154
- parent&.if_type? && parent.else?
155
- end
156
-
157
143
  def next_sibling_empty_or_guard_clause?(node)
158
144
  next_sibling = node.right_sibling
159
145
  return true if next_sibling.nil?
@@ -218,7 +204,7 @@ module RuboCop
218
204
  parent = node.parent
219
205
  return false unless parent
220
206
 
221
- parent.begin_type? && same_line?(node, node.right_sibling)
207
+ parent.type?(:begin, :kwbegin) && node.last_line == node.right_sibling.first_line
222
208
  end
223
209
 
224
210
  # SimpleCov excludes code from the coverage report by wrapping it in directive comments:
@@ -28,7 +28,6 @@ module RuboCop
28
28
  # end
29
29
  #
30
30
  class EmptyLinesAfterModuleInclusion < Base
31
- include RangeHelp
32
31
  extend AutoCorrector
33
32
 
34
33
  MSG = 'Add an empty line after module inclusion.'
@@ -41,20 +40,29 @@ module RuboCop
41
40
  return if node.receiver || node.arguments.empty?
42
41
  return if node.parent&.type?(:send, :any_block, :array)
43
42
 
44
- return if next_line_empty_or_enable_directive_comment?(node.last_line)
43
+ last_line = last_line_with_heredocs(node)
44
+ return if next_line_empty_or_enable_directive_comment?(last_line)
45
45
 
46
46
  next_line_node = next_line_node(node)
47
47
  return unless require_empty_line?(next_line_node)
48
48
 
49
- add_offense(node) { |corrector| autocorrect(corrector, node) }
49
+ add_offense(node) { |corrector| autocorrect(corrector, last_line) }
50
50
  end
51
51
 
52
52
  private
53
53
 
54
- def autocorrect(corrector, node)
55
- node_range = range_by_whole_lines(node.source_range)
54
+ def last_line_with_heredocs(node)
55
+ heredoc_end_lines = processed_source.ast.each_node(:any_str).filter_map do |str|
56
+ str.loc.heredoc_end.line if str.heredoc? && str.first_line == node.last_line
57
+ end
58
+
59
+ [node.last_line, *heredoc_end_lines].max
60
+ end
61
+
62
+ def autocorrect(corrector, last_line)
63
+ node_range = processed_source.buffer.line_range(last_line)
56
64
 
57
- next_line = node_range.last_line + 1
65
+ next_line = last_line + 1
58
66
  if enable_directive_comment?(next_line)
59
67
  node_range = processed_source.comment_at_line(next_line)
60
68
  end
@@ -114,10 +114,7 @@ module RuboCop
114
114
  end
115
115
 
116
116
  def allowed_only_before_style?(node)
117
- if node.special_modifier?
118
- return true if processed_source[node.last_line] == 'end'
119
- return false if next_line_empty_and_exists?(node.last_line)
120
- end
117
+ return false if node.special_modifier? && removable_blank_line_after?(node)
121
118
 
122
119
  previous_line_empty?(node.first_line)
123
120
  end
@@ -129,7 +126,7 @@ module RuboCop
129
126
  when :around
130
127
  corrector.insert_after(line, "\n") unless next_line_empty?(node.last_line)
131
128
  when :only_before
132
- if next_line_empty_and_exists?(node.last_line)
129
+ if removable_blank_line_after?(node)
133
130
  range = next_empty_line_range(node)
134
131
 
135
132
  corrector.remove(range)
@@ -154,10 +151,6 @@ module RuboCop
154
151
  body_end?(last_send_line) || next_line.blank?
155
152
  end
156
153
 
157
- def next_line_empty_and_exists?(last_send_line)
158
- next_line_empty?(last_send_line) && last_send_line.next != processed_source.lines.size
159
- end
160
-
161
154
  def empty_lines_around?(node)
162
155
  previous_line_empty?(node.first_line) && next_line_empty?(node.last_line)
163
156
  end
@@ -206,13 +199,39 @@ module RuboCop
206
199
  def message_for_only_before_style(node)
207
200
  modifier = node.loc.selector.source
208
201
 
209
- if next_line_empty?(node.last_line)
202
+ if removable_blank_line_after?(node)
210
203
  format(MSG_AFTER_FOR_ONLY_BEFORE, modifier: modifier)
211
204
  else
212
205
  format(MSG_BEFORE_FOR_ONLY_BEFORE, modifier: modifier)
213
206
  end
214
207
  end
215
208
 
209
+ def removable_blank_line_after?(node)
210
+ return false if followed_by_access_modifier?(node)
211
+ return false if trailing_in_block_with_empty_lines?(node)
212
+
213
+ next_line_blank_and_exists?(node.last_line)
214
+ end
215
+
216
+ def trailing_in_block_with_empty_lines?(node)
217
+ return false unless node.parent && inside_block?(node) && !node.right_sibling
218
+
219
+ config.for_enabled_cop('Layout/EmptyLinesAroundBlockBody')['EnforcedStyle'] ==
220
+ 'empty_lines'
221
+ end
222
+
223
+ def followed_by_access_modifier?(node)
224
+ sibling = node.right_sibling
225
+
226
+ sibling&.send_type? && sibling.bare_access_modifier?
227
+ end
228
+
229
+ def next_line_blank_and_exists?(last_send_line)
230
+ return false unless processed_source[last_send_line].blank?
231
+
232
+ processed_source.lines.drop(last_send_line).any? { |source_line| !source_line.blank? }
233
+ end
234
+
216
235
  def should_insert_line_before?(node)
217
236
  return false if previous_line_empty?(node.first_line)
218
237
  return true unless inside_block?(node) && no_empty_lines_around_block_body?
@@ -228,7 +247,9 @@ module RuboCop
228
247
  end
229
248
 
230
249
  def inside_block?(node)
231
- node.parent.block_type? || (node.parent.begin_type? && node.parent.parent&.block_type?)
250
+ return true if node.parent.any_block_type?
251
+
252
+ node.parent.begin_type? && node.parent.parent&.any_block_type?
232
253
  end
233
254
 
234
255
  def no_empty_lines_around_block_body?
@@ -65,11 +65,14 @@ module RuboCop
65
65
  MSG = 'Extra empty line detected %<location>s the `%<keyword>s`.'
66
66
 
67
67
  def on_def(node)
68
+ return if node.any_def_type? && node.endless?
69
+
68
70
  check_body(node.body, node.loc.line)
69
71
  end
70
72
  alias on_defs on_def
71
73
  alias on_block on_def
72
74
  alias on_numblock on_def
75
+ alias on_itblock on_def
73
76
 
74
77
  def on_kwbegin(node)
75
78
  check_body(node.children.first, node.loc.line)
@@ -87,12 +90,14 @@ module RuboCop
87
90
  keyword = loc.source
88
91
 
89
92
  # below the keyword
90
- unless left_to_block_body_cop?(body, line)
93
+ unless ending_left_to_block_body_cop?(body, line)
91
94
  check_line(style, line, message('after', keyword), &:empty?)
92
95
  end
93
96
 
94
97
  # above the keyword
95
- check_line(style, line - 2, message('before', keyword), &:empty?)
98
+ unless beginning_left_to_block_body_cop?(body, line)
99
+ check_line(style, line - 2, message('before', keyword), &:empty?)
100
+ end
96
101
  end
97
102
  end
98
103
 
@@ -126,13 +131,23 @@ module RuboCop
126
131
  end
127
132
  end
128
133
 
129
- def left_to_block_body_cop?(body, keyword_line)
134
+ def ending_left_to_block_body_cop?(body, keyword_line)
135
+ return false unless block_body_with_empty_lines_style?(body)
136
+
137
+ only_empty_lines_until_end?(body, keyword_line)
138
+ end
139
+
140
+ def block_body_with_empty_lines_style?(body)
130
141
  return false unless body.parent.any_block_type?
131
142
 
132
143
  cop_config = config.for_enabled_cop('Layout/EmptyLinesAroundBlockBody')
133
- return false if cop_config['EnforcedStyle'] != 'empty_lines'
144
+ cop_config['EnforcedStyle'] == 'empty_lines'
145
+ end
134
146
 
135
- only_empty_lines_until_end?(body, keyword_line)
147
+ def beginning_left_to_block_body_cop?(body, keyword_line)
148
+ return false unless block_body_with_empty_lines_style?(body)
149
+
150
+ only_empty_lines_since_beginning?(body, keyword_line)
136
151
  end
137
152
 
138
153
  def keyword_locations_in_rescue(node)
@@ -155,6 +170,21 @@ module RuboCop
155
170
 
156
171
  next_line == end_line
157
172
  end
173
+
174
+ def only_empty_lines_since_beginning?(body, keyword_line)
175
+ start_line = block_body_start_line(body.parent)
176
+ previous_line = (keyword_line - 1).downto(start_line).find do |line_number|
177
+ !processed_source.lines[line_number - 1].strip.empty?
178
+ end
179
+
180
+ previous_line == start_line
181
+ end
182
+
183
+ def block_body_start_line(block_node)
184
+ return block_node.loc.begin.line unless block_node.arguments?
185
+
186
+ [block_node.loc.begin.line, block_node.arguments.last_line].max
187
+ end
158
188
  end
159
189
  end
160
190
  end
@@ -80,6 +80,8 @@ module RuboCop
80
80
  include RangeHelp
81
81
  extend AutoCorrector
82
82
 
83
+ alias on_csend on_send
84
+
83
85
  def on_sclass(node)
84
86
  if node.parent&.assignment?
85
87
  check_asgn_alignment(node.parent, node)
@@ -183,7 +185,7 @@ module RuboCop
183
185
  return node.parent
184
186
  end
185
187
 
186
- assignment = assignment_or_operator_method(node)
188
+ assignment = assignment_or_method_call(node)
187
189
 
188
190
  if assignment && !line_break_before_keyword?(assignment.source_range, node)
189
191
  assignment
@@ -195,13 +197,11 @@ module RuboCop
195
197
  end
196
198
  end
197
199
 
198
- def assignment_or_operator_method(node)
200
+ def assignment_or_method_call(node)
199
201
  node.ancestors.find do |ancestor|
200
202
  if ancestor.assignment_or_similar?
201
203
  leading_node(extract_rhs(ancestor)).equal?(node)
202
- elsif ancestor.send_type? && ancestor.operator_method?
203
- !leading_node(ancestor).equal?(node)
204
- elsif ancestor.send_type?
204
+ elsif ancestor.call_type?
205
205
  leading_node(ancestor.last_argument).equal?(node)
206
206
  end
207
207
  end
@@ -128,7 +128,7 @@ module RuboCop
128
128
  entries = by_column[column]
129
129
 
130
130
  if lines.size > 1
131
- uniform_alignment_allowed?(token, lines, spacings)
131
+ uniform_alignment_allowed?(token, lines, spacings, entries)
132
132
  elsif entries.size > 1
133
133
  distinct_spacings(entries).size > 1
134
134
  else
@@ -136,11 +136,12 @@ module RuboCop
136
136
  end
137
137
  end
138
138
 
139
- def uniform_alignment_allowed?(token, lines, spacings)
139
+ def uniform_alignment_allowed?(token, lines, spacings, entries)
140
140
  return @alignment_verdicts[lines] if @alignment_verdicts.key?(lines)
141
141
 
142
142
  @alignment_verdicts[lines] =
143
- spacings.uniq.size > 1 || aligned_on_other_column?(token, lines)
143
+ spacings.uniq.size > 1 || distinct_spacings(entries).count(&:positive?) > 1 ||
144
+ aligned_on_other_column?(token, lines)
144
145
  end
145
146
 
146
147
  def aligned_on_other_column?(token, lines)
@@ -189,9 +190,7 @@ module RuboCop
189
190
  line_ranges = [(token.line - 1).downto(1),
190
191
  (token.line + 1).upto(processed_source.lines.length)]
191
192
 
192
- line_ranges.any? do |line_numbers|
193
- nearest_spacing_differs?(line_numbers, token, spacing)
194
- end
193
+ line_ranges.any? { |line_numbers| nearest_spacing_differs?(line_numbers, token, spacing) }
195
194
  end
196
195
 
197
196
  def nearest_spacing_differs?(line_numbers, token, spacing)
@@ -201,7 +201,10 @@ module RuboCop
201
201
  return false unless display_column(send_node.source_range) < column_delta.abs
202
202
  return false if column_delta.positive?
203
203
 
204
- top_level_send != send_node || begins_its_line?(top_level_send.loc.end)
204
+ return true if top_level_send != send_node
205
+ return false unless (closing_parenthesis = top_level_send.loc.end)
206
+
207
+ begins_its_line?(closing_parenthesis)
205
208
  end
206
209
 
207
210
  def inner_call?(top_level_send)
@@ -165,7 +165,7 @@ module RuboCop
165
165
  def check_first_pair(hash_node, first_pair, left_brace, left_parenthesis)
166
166
  return unless begins_its_line?(first_pair.source_range)
167
167
 
168
- if separator_style?(first_pair)
168
+ if separator_aligned?(hash_node, first_pair)
169
169
  check_based_on_longest_key(hash_node, left_brace, left_parenthesis)
170
170
  else
171
171
  check_first(first_pair, left_brace, left_parenthesis, 0)
@@ -186,6 +186,12 @@ module RuboCop
186
186
  end
187
187
  end
188
188
 
189
+ def separator_aligned?(hash_node, first_pair)
190
+ return false unless separator_style?(first_pair)
191
+
192
+ HashAlignmentStyles::SeparatorAlignment.new.checkable_layout?(hash_node)
193
+ end
194
+
189
195
  def separator_style?(first_pair)
190
196
  separator = first_pair.loc.operator
191
197
  key = "Enforced#{separator.is?(':') ? 'Colon' : 'HashRocket'}Style"
@@ -56,13 +56,16 @@ module RuboCop
56
56
 
57
57
  MSG = 'Put the closing parenthesis for a method call with a ' \
58
58
  'HEREDOC parameter on the same line as the HEREDOC opening.'
59
+ CODE_THAT_CAN_START_A_LINE = /\A\s*(?:\z|[,;)\]}#]|&?\.(?!\.))/.freeze
59
60
 
60
61
  # `Layout/MultilineMethodCallBraceLayout` can move the same closing parenthesis after
61
- # the last argument in the same pass, and both insertions are kept.
62
+ # the last argument in the same pass, and both insertions are kept. `Style/RaiseArgs`
63
+ # removes the parentheses of `raise Error.new(<<~MSG ...)` around the moved one.
62
64
  def self.autocorrect_incompatible_with
63
- [Layout::MultilineMethodCallBraceLayout, Style::TrailingCommaInArguments]
65
+ [Layout::MultilineMethodCallBraceLayout, Style::RaiseArgs, Style::TrailingCommaInArguments]
64
66
  end
65
67
 
68
+ # rubocop:disable-next Metrics/CyclomaticComplexity
66
69
  def on_send(node)
67
70
  heredoc_arg = extract_heredoc_argument(node)
68
71
  return unless heredoc_arg
@@ -70,8 +73,10 @@ module RuboCop
70
73
  outermost_send = outermost_send_on_same_line(heredoc_arg)
71
74
  return unless outermost_send
72
75
  return if end_keyword_before_closing_parenthesis?(node)
76
+ return if code_after_closing_parenthesis_cannot_start_a_line?(outermost_send)
73
77
  return if subsequent_closing_parentheses_in_same_line?(outermost_send)
74
78
  return if exist_argument_between_heredoc_end_and_closing_parentheses?(node)
79
+ return if exist_argument_between_heredoc_end_and_closing_parentheses?(outermost_send)
75
80
 
76
81
  add_offense(outermost_send.loc.end) do |corrector|
77
82
  autocorrect(corrector, outermost_send)
@@ -119,7 +124,7 @@ module RuboCop
119
124
  previous = heredoc
120
125
  current = previous.parent
121
126
  until send_missing_closing_parens?(current, previous, heredoc)
122
- return if current&.any_block_type? && !current.send_node.equal?(previous)
127
+ return if body_boundary?(current, previous)
123
128
 
124
129
  previous = current
125
130
  current = current.parent
@@ -135,6 +140,12 @@ module RuboCop
135
140
  parent.loc.end.line != heredoc.last_line
136
141
  end
137
142
 
143
+ def body_boundary?(node, child)
144
+ return true if heredoc_node?(node)
145
+
146
+ node&.any_block_type? && !node.send_node.equal?(child)
147
+ end
148
+
138
149
  def extract_heredoc_argument(node)
139
150
  node.arguments.find { |arg_node| extract_heredoc(arg_node) }
140
151
  end
@@ -170,6 +181,13 @@ module RuboCop
170
181
  end
171
182
  end
172
183
 
184
+ def code_after_closing_parenthesis_cannot_start_a_line?(node)
185
+ paren = node.loc.end
186
+ code_after = processed_source.lines[paren.line - 1][paren.last_column..]
187
+
188
+ !CODE_THAT_CAN_START_A_LINE.match?(code_after)
189
+ end
190
+
173
191
  def subsequent_closing_parentheses_in_same_line?(outermost_send)
174
192
  last_arg_of_outer_send = outermost_send.last_argument
175
193
  return false unless last_arg_of_outer_send&.loc?(:end) &&
@@ -97,11 +97,11 @@ module RuboCop
97
97
  return [] if ast.nil?
98
98
 
99
99
  ranges = Set.new
100
- ast.each_node(:str, :dstr) do |str|
100
+ ast.each_node(:any_str) do |str|
101
101
  loc = str.location
102
102
 
103
103
  if str.heredoc?
104
- ranges << loc.heredoc_body
104
+ ranges << loc.heredoc_body.join(loc.heredoc_end.begin)
105
105
  elsif str.loc?(:begin)
106
106
  ranges << loc.expression
107
107
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  private
35
35
 
36
36
  def first_token
37
- processed_source.tokens.find { |t| !t.text.start_with?('#') }
37
+ processed_source.tokens.find { |t| !t.comment? }
38
38
  end
39
39
 
40
40
  def space_before(token)
@@ -111,10 +111,15 @@ module RuboCop
111
111
  # that element's closing brace in the same pass places the bracket after whatever
112
112
  # follows the moved brace, such as a comment.
113
113
  # Moving it while `Style/ConcatArrayLiterals` removes the brackets in the same pass
114
- # leaves the bracket behind.
114
+ # leaves the bracket behind. Moving it with a comment before it while
115
+ # `Style/MethodCallWithArgsParentheses` adds the closing parenthesis after
116
+ # the bracket in the same pass places the parenthesis after the comment.
115
117
  def self.autocorrect_incompatible_with
116
118
  [
117
- Layout::MultilineHashBraceLayout, Layout::MultilineMethodCallBraceLayout, Style::ConcatArrayLiterals
119
+ Layout::MultilineHashBraceLayout,
120
+ Layout::MultilineMethodCallBraceLayout,
121
+ Style::ConcatArrayLiterals,
122
+ Style::MethodCallWithArgsParentheses
118
123
  ]
119
124
  end
120
125
 
@@ -110,9 +110,16 @@ module RuboCop
110
110
  # `Layout/MultilineMethodCallBraceLayout` moves that call's closing parenthesis
111
111
  # before a comment in the same pass places the brace after the comment.
112
112
  # Moving it while `Style/HashAsLastArrayItem` or `Style/RedundantDoubleSplatHashBraces`
113
- # removes the braces in the same pass leaves the brace behind.
113
+ # removes the braces in the same pass leaves the brace behind. Moving it with a comment
114
+ # before it while `Style/MethodCallWithArgsParentheses` adds the closing parenthesis
115
+ # after the brace in the same pass places the parenthesis after the comment.
114
116
  def self.autocorrect_incompatible_with
115
- [Layout::MultilineMethodCallBraceLayout, Style::HashAsLastArrayItem, Style::RedundantDoubleSplatHashBraces]
117
+ [
118
+ Layout::MultilineMethodCallBraceLayout,
119
+ Style::HashAsLastArrayItem,
120
+ Style::RedundantDoubleSplatHashBraces,
121
+ Style::MethodCallWithArgsParentheses
122
+ ]
116
123
  end
117
124
 
118
125
  def on_hash(node)