rubocop 1.92.2 → 1.93.0

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 (191) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -1
  4. data/lib/rubocop/#directive_comment.rb# +224 -0
  5. data/lib/rubocop/cache_config.rb +3 -0
  6. data/lib/rubocop/cli.rb +5 -4
  7. data/lib/rubocop/config.rb +22 -0
  8. data/lib/rubocop/config_store.rb +1 -0
  9. data/lib/rubocop/config_validator.rb +45 -1
  10. data/lib/rubocop/cop/base.rb +5 -1
  11. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  12. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
  13. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  14. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
  16. data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
  17. data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  19. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  20. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  21. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  22. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  23. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  24. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  25. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
  26. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
  27. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  28. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  29. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  30. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
  31. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  32. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  33. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  34. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +14 -1
  36. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  39. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  40. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  41. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  42. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  43. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  44. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  45. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  46. data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
  47. data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
  48. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  49. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  50. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
  51. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  52. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  53. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
  54. data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
  55. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  56. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  57. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  58. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  59. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  60. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
  61. data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
  62. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  63. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  64. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  65. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  66. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  67. data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
  68. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
  69. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
  70. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
  71. data/lib/rubocop/cop/lint/useless_times.rb +52 -12
  72. data/lib/rubocop/cop/lint/void.rb +20 -9
  73. data/lib/rubocop/cop/message_annotator.rb +6 -8
  74. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  75. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
  76. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  77. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  78. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  79. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  80. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  81. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  82. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  83. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  84. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  85. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  86. data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
  87. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  88. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  89. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  90. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  91. data/lib/rubocop/cop/style/and_or.rb +8 -1
  92. data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
  93. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  94. data/lib/rubocop/cop/style/array_intersect.rb +15 -7
  95. data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
  96. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  97. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  98. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  99. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  100. data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
  101. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  102. data/lib/rubocop/cop/style/dig_chain.rb +13 -2
  103. data/lib/rubocop/cop/style/each_with_object.rb +21 -5
  104. data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
  105. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  106. data/lib/rubocop/cop/style/empty_method.rb +27 -4
  107. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  108. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  109. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  110. data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
  111. data/lib/rubocop/cop/style/file_read.rb +5 -1
  112. data/lib/rubocop/cop/style/file_write.rb +89 -20
  113. data/lib/rubocop/cop/style/for.rb +59 -4
  114. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  115. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  116. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  117. data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
  118. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  119. data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
  121. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  122. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  123. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  124. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  125. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  126. data/lib/rubocop/cop/style/lambda.rb +13 -5
  127. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  128. data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
  129. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  130. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  131. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  132. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  133. data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
  134. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
  135. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  136. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
  137. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
  138. data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
  139. data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
  140. data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
  141. data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
  142. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  143. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  144. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  145. data/lib/rubocop/cop/style/raise_args.rb +15 -0
  146. data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
  147. data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
  148. data/lib/rubocop/cop/style/redundant_each.rb +26 -1
  149. data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
  150. data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
  151. data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
  152. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
  153. data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
  154. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
  155. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  156. data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
  157. data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
  158. data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
  159. data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
  160. data/lib/rubocop/cop/style/select_by_range.rb +1 -6
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
  162. data/lib/rubocop/cop/style/self_assignment.rb +0 -4
  163. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  164. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  165. data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
  166. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  167. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  168. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  169. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  170. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  171. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  172. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  173. data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
  174. data/lib/rubocop/cop/style/swap_values.rb +10 -13
  175. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  176. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  177. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
  178. data/lib/rubocop/cop/style/word_array.rb +1 -3
  179. data/lib/rubocop/cop/util.rb +2 -0
  180. data/lib/rubocop/cop/variable_force.rb +14 -1
  181. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  182. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  183. data/lib/rubocop/result_cache.rb +9 -3
  184. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  185. data/lib/rubocop/rspec/shared_contexts.rb +11 -11
  186. data/lib/rubocop/runner.rb +21 -7
  187. data/lib/rubocop/server/cache.rb +24 -5
  188. data/lib/rubocop/target_ruby.rb +6 -8
  189. data/lib/rubocop/todo_audit.rb +61 -15
  190. data/lib/rubocop/version.rb +18 -8
  191. metadata +9 -5
@@ -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,29 +199,75 @@ 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
- return true unless inside_block?(node) && no_empty_lines_around_block_body?
219
- return true unless node.parent.begin_type?
220
237
 
221
- node.parent.children.first != node
238
+ statement = statement_of(node)
239
+ return true unless inside_block?(statement) && no_empty_lines_around_block_body?
240
+ return true unless statement.parent.begin_type?
241
+
242
+ statement.parent.children.first != statement
222
243
  end
223
244
 
224
245
  def should_insert_line_after?(node)
225
- return true unless inside_block?(node) && no_empty_lines_around_block_body?
246
+ statement = statement_of(node)
247
+ return true unless inside_block?(statement) && no_empty_lines_around_block_body?
248
+
249
+ statement.parent.children.last != statement
250
+ end
251
+
252
+ def statement_of(node)
253
+ node = node.parent while wraps_on_the_same_line?(node.parent)
254
+
255
+ node
256
+ end
257
+
258
+ def wraps_on_the_same_line?(parent)
259
+ return false if parent.nil?
260
+ return parent.modifier_form? || parent.ternary? if parent.if_type?
226
261
 
227
- node.parent.children.last != node
262
+ parent.type?(:begin, :kwbegin) && parent.loc?(:begin) && parent.single_line?
228
263
  end
229
264
 
230
265
  def inside_block?(node)
231
- node.parent.block_type? || (node.parent.begin_type? && node.parent.parent&.block_type?)
266
+ parent = node.parent
267
+ return false if parent.nil?
268
+ return true if parent.any_block_type?
269
+
270
+ parent.begin_type? && parent.parent&.any_block_type?
232
271
  end
233
272
 
234
273
  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)
@@ -280,7 +283,7 @@ module RuboCop
280
283
 
281
284
  # @!method eligible_method_call?(node)
282
285
  def_node_matcher :eligible_method_call?, <<~PATTERN
283
- (send _ !:[]= ...)
286
+ (call _ !:[]= ...)
284
287
  PATTERN
285
288
 
286
289
  def base_range(send_node, arg_node)
@@ -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)
@@ -163,11 +163,24 @@ module RuboCop
163
163
  def register_endless_method_offense(line, line_index)
164
164
  message = format(MSG, length: line_length(line), max: max)
165
165
  loc = excess_range(nil, line, line_index)
166
+ node = endless_methods_by_line[line_index]
166
167
 
167
168
  add_offense(loc, message: message) do |corrector|
168
169
  self.max = line_length(line)
169
170
 
170
- correct_to_multiline(corrector, endless_methods_by_line[line_index])
171
+ correct_to_multiline(corrector, node) unless heredoc_body_after_method?(node)
172
+ end
173
+ end
174
+
175
+ # The correction puts the body and `end` on lines of their own, which
176
+ # separates an opener before the body, or on the last line, from its
177
+ # heredoc body.
178
+ def heredoc_body_after_method?(node)
179
+ body_begin = node.body.source_range.begin_pos
180
+
181
+ heredoc_openers.any? do |opener|
182
+ (opener.line == node.first_line && opener.begin_pos < body_begin) ||
183
+ (opener.line == node.last_line && opener.begin_pos < node.source_range.end_pos)
171
184
  end
172
185
  end
173
186
 
@@ -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)
@@ -199,7 +199,7 @@ module RuboCop
199
199
  return unless base_receiver.hash_type?
200
200
 
201
201
  first_call = first_call_has_a_dot(node)
202
- first_call.loc.dot.join(first_call.loc.selector)
202
+ dot_with_method_name(first_call)
203
203
  end
204
204
 
205
205
  def first_dot_alignment_base(node, rhs)
@@ -215,7 +215,7 @@ module RuboCop
215
215
 
216
216
  return unless same_line?(dot, first_call.receiver.source_range)
217
217
 
218
- dot.join(first_call.loc.selector)
218
+ dot_with_method_name(first_call)
219
219
  end
220
220
 
221
221
  def after_multiline_block_base(first_call, node)
@@ -224,7 +224,7 @@ module RuboCop
224
224
  after_block = first_call.block_node.parent
225
225
  return unless after_block&.call_type? && after_block.loc?(:dot) && after_block != node
226
226
 
227
- after_block.loc.dot.join(after_block.loc.selector)
227
+ dot_with_method_name(after_block)
228
228
  end
229
229
 
230
230
  def inside_multiline_chain_arg?(node)
@@ -381,13 +381,17 @@ module RuboCop
381
381
  while receiver_chain&.call_type?
382
382
  base_receiver = unwrap_block_node(receiver_chain.receiver)
383
383
  if alignment_base_for_chained_receiver?(receiver_chain, base_receiver)
384
- return receiver_chain.loc.dot.join(receiver_chain.loc.selector)
384
+ return dot_with_method_name(receiver_chain)
385
385
  end
386
386
 
387
387
  receiver_chain = base_receiver
388
388
  end
389
389
  end
390
390
 
391
+ def dot_with_method_name(node)
392
+ node.loc.dot.join(node.loc.selector || node.loc.begin)
393
+ end
394
+
391
395
  def alignment_base_for_chained_receiver?(receiver_chain, base_receiver)
392
396
  base_receiver&.hash_type? ||
393
397
  method_on_receiver_last_line?(receiver_chain, base_receiver, :begin)
@@ -119,7 +119,7 @@ module RuboCop
119
119
  'on the same line as the last parameter.'
120
120
 
121
121
  def self.autocorrect_incompatible_with
122
- [Style::MultilineMethodSignature]
122
+ [Style::MethodDefParentheses, Style::MultilineMethodSignature]
123
123
  end
124
124
 
125
125
  def on_def(node)
@@ -28,7 +28,17 @@ module RuboCop
28
28
  # These cops rewrite a method chained to the block in the same pass,
29
29
  # which must not start a line of its own.
30
30
  def self.autocorrect_incompatible_with
31
- [Style::FloatDivision, Style::MapToHash, Style::MapToSet, Style::NilComparison, Style::RedundantSort]
31
+ [
32
+ Style::CollectionQuerying,
33
+ Style::FloatDivision,
34
+ Style::HashTransformKeys,
35
+ Style::HashTransformValues,
36
+ Style::MapToHash,
37
+ Style::MapToSet,
38
+ Style::NilComparison,
39
+ Style::RedundantFilterChain,
40
+ Style::RedundantSort
41
+ ]
32
42
  end
33
43
 
34
44
  def on_send(node)
@@ -36,6 +36,8 @@ module RuboCop
36
36
  # # The `compact` style normally requires a space inside
37
37
  # # array brackets, with the exception that successive left
38
38
  # # or right brackets are collapsed together in nested arrays.
39
+ # # A right bracket on its own line is left there when
40
+ # # `Layout/MultilineArrayBraceLayout` enforces the `new_line` style.
39
41
  #
40
42
  # # bad
41
43
  # array = [a, b, c, d]
@@ -216,11 +218,20 @@ module RuboCop
216
218
 
217
219
  # A closing bracket on its own line is only collapsed when the opening
218
220
  # bracket ends up on the first element's line, so that the brackets
219
- # stay symmetrical (see `Layout/MultilineArrayBraceLayout`).
221
+ # stay symmetrical (see `Layout/MultilineArrayBraceLayout`). It is never
222
+ # collapsed when that cop requires it to stay on its own line.
220
223
  def closing_bracket_kept_apart?(node, tokens, left, right)
221
- end_has_own_line?(right) &&
222
- !multi_dimensional_array?(tokens, left, side: :left) &&
223
- !same_line?(left, node.children.first)
224
+ return false unless end_has_own_line?(right)
225
+ return true if node.array_type? && closing_bracket_on_new_line_enforced?
226
+ return false if multi_dimensional_array?(tokens, left, side: :left)
227
+
228
+ !same_line?(left, node.children.first)
229
+ end
230
+
231
+ def closing_bracket_on_new_line_enforced?
232
+ return false unless config.cop_enabled?('Layout/MultilineArrayBraceLayout')
233
+
234
+ config.for_cop('Layout/MultilineArrayBraceLayout')['EnforcedStyle'] == 'new_line'
224
235
  end
225
236
 
226
237
  def compact_corrections(corrector, node, tokens, left, right)
@@ -89,6 +89,10 @@ module RuboCop
89
89
  group_by filter_map
90
90
  ].to_set.freeze
91
91
 
92
+ def self.autocorrect_incompatible_with
93
+ [Style::AndOr]
94
+ end
95
+
92
96
  def on_send(node)
93
97
  return unless node.arguments?
94
98
 
@@ -44,6 +44,10 @@ module RuboCop
44
44
  RESTRICT_ON_SEND = PRECEDENCE.flatten.freeze
45
45
  MSG = 'Wrap expressions with varying precedence with parentheses to avoid ambiguity.'
46
46
 
47
+ def self.autocorrect_incompatible_with
48
+ [Style::LineEndConcatenation]
49
+ end
50
+
47
51
  def on_and(node)
48
52
  return unless (parent = node.parent)
49
53
 
@@ -54,7 +54,7 @@ module RuboCop
54
54
  def find_offense_node(node, regexp_receiver)
55
55
  return node if first_argument_is_regexp?(node) || !node.parent
56
56
 
57
- if (node.parent.send_type? && node.receiver) ||
57
+ if (node.parent.call_type? && node.receiver) ||
58
58
  method_chain_to_regexp_receiver?(node, regexp_receiver) ||
59
59
  part_of_hash_argument?(node)
60
60
  node = find_offense_node(node.parent, regexp_receiver)
@@ -64,7 +64,7 @@ module RuboCop
64
64
  end
65
65
 
66
66
  def first_argument_is_regexp?(node)
67
- node.send_type? && node.first_argument&.regexp_type?
67
+ node.call_type? && node.first_argument&.regexp_type?
68
68
  end
69
69
 
70
70
  def method_chain_to_regexp_receiver?(node, regexp_receiver)
@@ -72,6 +72,8 @@ module RuboCop
72
72
 
73
73
  if node.method?(:freeze)
74
74
  corrector.replace(node, 'ENV')
75
+ elsif node.method?(:attr)
76
+ correct_attr(corrector, node)
75
77
  else
76
78
  corrector.replace(offense_range, prefer)
77
79
  end
@@ -92,10 +94,7 @@ module RuboCop
92
94
 
93
95
  def preferred_method(node)
94
96
  if node.method?(:attr)
95
- boolean_argument = node.arguments[1].source
96
- preferred_attr_method = boolean_argument == 'true' ? 'attr_accessor' : 'attr_reader'
97
-
98
- "#{preferred_attr_method} #{node.first_argument.source}"
97
+ "#{preferred_attr_method(node)} #{node.first_argument.source}"
99
98
  elsif dir_env_file_const?(node.receiver)
100
99
  prefer = PREFERRED_METHODS[node.method_name]
101
100
 
@@ -105,6 +104,17 @@ module RuboCop
105
104
  end
106
105
  end
107
106
 
107
+ def preferred_attr_method(node)
108
+ node.arguments[1].true_type? ? 'attr_accessor' : 'attr_reader'
109
+ end
110
+
111
+ def correct_attr(corrector, node)
112
+ corrector.replace(node.loc.selector, preferred_attr_method(node))
113
+ first_argument, boolean_argument = node.arguments
114
+
115
+ corrector.remove(first_argument.source_range.end.join(boolean_argument.source_range.end))
116
+ end
117
+
108
118
  def socket_const?(node)
109
119
  node&.short_name == :Socket
110
120
  end