rubocop 1.92.1 → 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 (160) 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 -4
  6. data/lib/rubocop/config_obsoletion.rb +9 -0
  7. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  8. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  10. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  11. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  12. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  13. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  14. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  15. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  16. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  17. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +15 -7
  18. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  19. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +6 -12
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  23. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  25. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  26. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  27. data/lib/rubocop/cop/layout/line_length.rb +23 -5
  28. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  29. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  30. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  31. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  32. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  33. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  34. data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
  35. data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
  36. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  37. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
  39. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  40. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  41. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  42. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  43. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  44. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  45. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  46. data/lib/rubocop/cop/lint/interpolation_check.rb +27 -18
  47. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  48. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  49. data/lib/rubocop/cop/lint/nested_percent_literal.rb +3 -0
  50. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  51. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  52. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +66 -3
  53. data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
  54. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
  55. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  56. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  57. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  58. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +11 -4
  59. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  60. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  61. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  62. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  63. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  64. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  65. data/lib/rubocop/cop/lint/void.rb +20 -9
  66. data/lib/rubocop/cop/message_annotator.rb +6 -8
  67. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  68. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  69. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  70. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  71. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +17 -1
  72. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  73. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  74. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  75. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  76. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  77. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  78. data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -1
  79. data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
  80. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  81. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  82. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  83. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  84. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  85. data/lib/rubocop/cop/style/and_or.rb +41 -11
  86. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  87. data/lib/rubocop/cop/style/case_equality.rb +14 -3
  88. data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
  89. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  90. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  91. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  92. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  93. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  94. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  95. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  96. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  97. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  98. data/lib/rubocop/cop/style/endless_method.rb +64 -16
  99. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  100. data/lib/rubocop/cop/style/float_division.rb +16 -1
  101. data/lib/rubocop/cop/style/for.rb +59 -4
  102. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  103. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  104. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  105. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  106. data/lib/rubocop/cop/style/hash_lookup_method.rb +21 -2
  107. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  108. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  109. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  110. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  111. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  112. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  113. data/lib/rubocop/cop/style/lambda.rb +13 -5
  114. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  115. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  116. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  117. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  118. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +12 -1
  119. data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  121. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  122. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  123. data/lib/rubocop/cop/style/numeric_predicate.rb +7 -0
  124. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  125. data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -10
  126. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  127. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  128. data/lib/rubocop/cop/style/quoted_symbols.rb +5 -7
  129. data/lib/rubocop/cop/style/raise_args.rb +66 -7
  130. data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
  131. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  132. data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
  133. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  134. data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
  135. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  136. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  137. data/lib/rubocop/cop/style/redundant_self.rb +9 -1
  138. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  139. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  140. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  141. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  142. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  143. data/lib/rubocop/cop/style/single_line_methods.rb +26 -2
  144. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  145. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  146. data/lib/rubocop/cop/style/string_concatenation.rb +28 -8
  147. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  148. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  149. data/lib/rubocop/cop/style/word_array.rb +1 -3
  150. data/lib/rubocop/cop/util.rb +2 -0
  151. data/lib/rubocop/cop/variable_force.rb +14 -1
  152. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  153. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  154. data/lib/rubocop/plugin/configuration_integrator.rb +24 -3
  155. data/lib/rubocop/result_cache.rb +9 -3
  156. data/lib/rubocop/runner.rb +6 -1
  157. data/lib/rubocop/server/cache.rb +24 -5
  158. data/lib/rubocop/todo_audit.rb +61 -15
  159. data/lib/rubocop/version.rb +1 -1
  160. metadata +4 -3
@@ -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)
@@ -125,6 +125,7 @@ module RuboCop
125
125
  @breakable_strings = {}
126
126
  @endless_methods_by_line = {}
127
127
  @heredocs = nil
128
+ @heredoc_openers = nil
128
129
 
129
130
  return unless processed_source.raw_source.include?(';')
130
131
 
@@ -214,11 +215,24 @@ module RuboCop
214
215
  range = breakable_node.source_range
215
216
 
216
217
  existing = breakable_range_by_line_index[line_index]
217
- return if existing
218
+ return if existing || after_heredoc_opener?(range)
218
219
 
219
220
  breakable_range_by_line_index[line_index] = range
220
221
  end
221
222
 
223
+ def after_heredoc_opener?(range)
224
+ heredoc_openers.any? do |opener|
225
+ opener.line == range.line && opener.begin_pos < range.begin_pos
226
+ end
227
+ end
228
+
229
+ def heredoc_openers
230
+ ast = processed_source.ast
231
+ return [] if ast.nil?
232
+
233
+ @heredoc_openers ||= ast.each_node(:any_str).select(&:heredoc?).map(&:source_range)
234
+ end
235
+
222
236
  def compact_nested_array_break?(node, breakable_node)
223
237
  return false if !node.array_type? || !node.square_brackets?
224
238
  return false unless breakable_node.equal?(node.children.first)
@@ -237,21 +251,24 @@ module RuboCop
237
251
  tokens = processed_source.tokens.select { |t| t.type == :tSEMI }
238
252
  tokens.reverse_each do |token|
239
253
  range = breakable_range_after_semicolon(token)
240
- breakable_range_by_line_index[range.line - 1] = range if range
254
+ next if range.nil? || after_heredoc_opener?(range)
255
+
256
+ breakable_range_by_line_index[range.line - 1] = range
241
257
  end
242
258
  end
243
259
 
244
260
  def check_for_breakable_block(block_node)
245
261
  return unless block_node.single_line?
246
- return if receiver_contains_heredoc?(block_node)
247
262
 
248
263
  line_index = block_node.loc.line - 1
249
264
  return if breakable_string_delimiters[line_index]
250
265
 
251
266
  range = breakable_block_range(block_node)
252
267
  pos = range.begin_pos + 1
268
+ range = range_between(pos, pos + 1)
269
+ return if after_heredoc_opener?(range)
253
270
 
254
- breakable_range_by_line_index[line_index] = range_between(pos, pos + 1)
271
+ breakable_range_by_line_index[line_index] = range
255
272
  end
256
273
 
257
274
  # rubocop:disable-next Metrics/AbcSize
@@ -289,7 +306,8 @@ module RuboCop
289
306
  !node.heredoc? &&
290
307
  # TODO: strings inside hashes, kwargs and arrays are currently ignored,
291
308
  # but could be considered in the future
292
- !node.parent&.type?(:pair, :kwoptarg, :array)
309
+ !node.parent&.type?(:pair, :kwoptarg, :array) &&
310
+ !after_heredoc_opener?(node.source_range)
293
311
  end
294
312
 
295
313
  def breakable_block_range(block_node)
@@ -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)
@@ -115,7 +115,7 @@ module RuboCop
115
115
  node.loc.name
116
116
  when :sclass
117
117
  node.identifier.source_range
118
- when :masgn
118
+ when :masgn, :op_asgn, :or_asgn, :and_asgn
119
119
  node.lhs.source_range
120
120
  else
121
121
  # It is a wrapper with receiver of object attribute or access modifier.
@@ -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::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)
@@ -245,20 +245,30 @@ module RuboCop
245
245
  return if with_space.is?(operator.source)
246
246
 
247
247
  "Space around operator `#{operator.source}` detected."
248
- elsif !/^\s.*\s$/.match?(with_space.source)
248
+ elsif missing_surrounding_space?(with_space)
249
249
  "Surrounding space missing for operator `#{operator.source}`."
250
- elsif excess_leading_space?(type, operator, with_space) ||
250
+ elsif excess_leading_space?(type, operator, with_space, right_operand) ||
251
251
  excess_trailing_space?(right_operand.source_range, with_space)
252
252
  "Operator `#{operator.source}` should be surrounded " \
253
253
  'by a single space.'
254
254
  end
255
255
  end
256
256
 
257
- def excess_leading_space?(type, operator, with_space)
257
+ def missing_surrounding_space?(with_space)
258
+ return false if /\A\s.*\s\z/m.match?(with_space.source)
259
+
260
+ !with_space.source.start_with?(/\s/) || !line_continuation_after?(with_space)
261
+ end
262
+
263
+ def line_continuation_after?(range)
264
+ processed_source.buffer.source[range.end_pos, 2] == "\\\n"
265
+ end
266
+
267
+ def excess_leading_space?(type, operator, with_space, right_operand)
258
268
  return false unless allow_for_alignment?
259
269
  return false unless with_space.source.start_with?(EXCESSIVE_SPACE)
260
270
 
261
- return !aligned_with_operator?(operator) unless grouped_alignment_type?(type)
271
+ return !aligned?(type, operator, right_operand) unless grouped_alignment_type?(type)
262
272
 
263
273
  token = Token.new(operator, nil, operator.source)
264
274
  align_preceding = aligned_with_preceding_equals_operator(token)
@@ -269,6 +279,23 @@ module RuboCop
269
279
  aligned_with_subsequent_equals_operator(token) != :yes
270
280
  end
271
281
 
282
+ def aligned?(type, operator, right_operand)
283
+ return true if aligned_with_operator?(operator)
284
+
285
+ type == :pair && aligned_with_another_hash_rocket?(right_operand)
286
+ end
287
+
288
+ def aligned_with_another_hash_rocket?(pair)
289
+ hash_rocket_columns(pair.parent)[pair.loc.operator.column] > 1
290
+ end
291
+
292
+ def hash_rocket_columns(hash_node)
293
+ @hash_rocket_columns ||= {}.compare_by_identity
294
+ @hash_rocket_columns[hash_node] ||= hash_node.pairs.select(&:hash_rocket?).map do |pair|
295
+ pair.loc.operator.column
296
+ end.tally
297
+ end
298
+
272
299
  def grouped_alignment_type?(type)
273
300
  type == :assignment || (type == :special_asgn && force_equal_sign_alignment?)
274
301
  end
@@ -136,11 +136,23 @@ module RuboCop
136
136
 
137
137
  def autocorrect(corrector, range)
138
138
  case range.source
139
+ when /\n/ then remove_line_continuation(corrector, range)
139
140
  when /\s/ then corrector.remove(range)
140
141
  else corrector.insert_before(range, ' ')
141
142
  end
142
143
  end
143
144
 
145
+ # When a heredoc body sits between the `\` and the brace, the newline before
146
+ # the brace ends the heredoc, and removing it would break the terminator.
147
+ def remove_line_continuation(corrector, range)
148
+ source = processed_source.buffer.source
149
+ return unless source[range.begin_pos - 1] == '\\'
150
+
151
+ begin_pos = range.begin_pos
152
+ begin_pos -= 1 while source[begin_pos - 1].match?(/[ \t\\]/)
153
+ corrector.remove(range_between(begin_pos, range.end_pos))
154
+ end
155
+
144
156
  def style_for_empty_braces
145
157
  case cop_config['EnforcedStyleForEmptyBraces']
146
158
  when 'space' then :space
@@ -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)
@@ -83,7 +83,7 @@ module RuboCop
83
83
  extend AutoCorrector
84
84
 
85
85
  def self.autocorrect_incompatible_with
86
- [Style::BlockDelimiters]
86
+ [Layout::BlockAlignment, Style::BlockDelimiters]
87
87
  end
88
88
 
89
89
  def on_block(node)
@@ -18,6 +18,7 @@ module RuboCop
18
18
  # # good
19
19
  # 'a'..'z'
20
20
  class SpaceInsideRangeLiteral < Base
21
+ include RangeHelp
21
22
  extend AutoCorrector
22
23
 
23
24
  MSG = 'Space inside range literal.'
@@ -33,21 +34,38 @@ module RuboCop
33
34
  private
34
35
 
35
36
  def check(node)
36
- expression = node.source
37
- op = node.loc.operator.source
38
- escaped_op = op.gsub('.', '\.')
39
-
40
- # account for multiline range literals
41
- expression.sub!(/#{escaped_op}\n\s*/, op)
42
-
43
- return unless /(\s#{escaped_op})|(#{escaped_op}\s)/.match?(expression)
37
+ operator = node.loc.operator
38
+ spaces = [space_before(node, operator), space_after(node, operator)].compact
39
+ return if spaces.all? { |space| line_break_after_operator?(space, operator) }
44
40
 
45
41
  add_offense(node) do |corrector|
46
- corrector.replace(
47
- node, expression.sub(/\s+#{escaped_op}/, op).sub(/#{escaped_op}\s+/, op)
48
- )
42
+ spaces.each do |space|
43
+ corrector.remove(space)
44
+ end
49
45
  end
50
46
  end
47
+
48
+ def space_before(node, operator)
49
+ return unless node.begin
50
+
51
+ whitespace_range(node.begin.source_range.end_pos, operator.begin_pos)
52
+ end
53
+
54
+ def space_after(node, operator)
55
+ return unless node.end
56
+
57
+ whitespace_range(operator.end_pos, node.end.source_range.begin_pos)
58
+ end
59
+
60
+ def line_break_after_operator?(space, operator)
61
+ space.begin_pos == operator.end_pos && space.source.start_with?("\n")
62
+ end
63
+
64
+ def whitespace_range(begin_pos, end_pos)
65
+ range = range_between(begin_pos, end_pos)
66
+
67
+ range if range.source.match?(/\A\s+\z/)
68
+ end
51
69
  end
52
70
  end
53
71
  end
@@ -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
@@ -205,15 +205,15 @@ module RuboCop
205
205
  end
206
206
 
207
207
  def on_def(node)
208
- # if a method definition is inside an if, it is very likely
208
+ # if a method definition is inside a conditional, it is very likely
209
209
  # that a different definition is used depending on platform, etc.
210
- return if node.each_ancestor.any?(&:if_type?)
210
+ return if inside_condition?(node)
211
211
 
212
212
  found_instance_method(node, node.method_name)
213
213
  end
214
214
 
215
215
  def on_defs(node)
216
- return if node.each_ancestor.any?(&:if_type?)
216
+ return if inside_condition?(node)
217
217
 
218
218
  if node.receiver.const_type?
219
219
  _, const_name = *node.receiver
@@ -236,7 +236,7 @@ module RuboCop
236
236
  track_intentional_redefinition(node, name)
237
237
  return
238
238
  end
239
- return if node.ancestors.any?(&:if_type?)
239
+ return if inside_condition?(node)
240
240
 
241
241
  found_instance_method(node, name)
242
242
  end
@@ -355,7 +355,7 @@ module RuboCop
355
355
  end
356
356
 
357
357
  def inside_condition?(node)
358
- node.ancestors.any?(&:if_type?)
358
+ node.each_ancestor(:if, :case, :case_match).any?
359
359
  end
360
360
 
361
361
  def message_for_dup(node, method_name, key)
@@ -399,7 +399,7 @@ module RuboCop
399
399
  end
400
400
 
401
401
  def hash_value(node, key)
402
- node.pairs.find { |pair| pair.key.value == key }&.value
402
+ node.pairs.find { |pair| pair.key.sym_type? && pair.key.value == key }&.value
403
403
  end
404
404
 
405
405
  def found_instance_method(node, name)
@@ -44,12 +44,19 @@ module RuboCop
44
44
 
45
45
  def on_send(node)
46
46
  return unless require_call?(node)
47
+ return unless statement_sequence?(node.parent)
47
48
  return if @required[node.parent].add?("#{node.method_name}#{node.first_argument}")
48
49
 
49
50
  add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
50
- corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
51
+ corrector.remove(range_for_statement_removal(node.source_range))
51
52
  end
52
53
  end
54
+
55
+ private
56
+
57
+ def statement_sequence?(node)
58
+ node&.type?(:begin, :kwbegin)
59
+ end
53
60
  end
54
61
  end
55
62
  end
@@ -32,6 +32,12 @@ module RuboCop
32
32
  MSG = 'Use a hash literal instead of keyword arguments.'
33
33
  RESTRICT_ON_SEND = %i[new].freeze
34
34
 
35
+ # The braced hash literal needs the parentheses of the call,
36
+ # which `Style/MethodCallWithArgsParentheses` removes in the same pass.
37
+ def self.autocorrect_incompatible_with
38
+ [Style::MethodCallWithArgsParentheses]
39
+ end
40
+
35
41
  # @!method hash_new(node)
36
42
  def_node_matcher :hash_new, <<~PATTERN
37
43
  (send (const {nil? (cbase)} :Hash) :new $[hash !braces?])
@@ -46,7 +52,20 @@ module RuboCop
46
52
  end
47
53
 
48
54
  add_offense(first_argument) do |corrector|
55
+ autocorrect(corrector, node, first_argument)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def autocorrect(corrector, node, first_argument)
62
+ if node.parenthesized?
49
63
  corrector.wrap(first_argument, '{', '}')
64
+ else
65
+ gap = node.loc.selector.end.join(first_argument.source_range.begin)
66
+
67
+ corrector.replace(gap, '({')
68
+ corrector.insert_after(first_argument, '})')
50
69
  end
51
70
  end
52
71
  end
@@ -41,26 +41,22 @@ module RuboCop
41
41
 
42
42
  private
43
43
 
44
- # rubocop:disable-next Metrics/CyclomaticComplexity
45
44
  def check(node)
46
45
  return if node.parent&.regexp_type?
47
46
  return unless /(?<!\\)#\{.*\}/.match?(node.source)
48
47
  return if heredoc?(node)
49
- return unless node.loc.begin && node.loc.end
48
+ return unless single_quoted?(node)
50
49
  return unless valid_syntax?(node)
51
50
 
52
51
  add_offense(node) { |corrector| autocorrect(corrector, node) }
53
52
  end
54
53
 
54
+ def single_quoted?(node)
55
+ node.loc.begin&.is?("'") && node.loc.end
56
+ end
57
+
55
58
  def autocorrect(corrector, node)
56
- starting_token, ending_token = if node.source.include?('"')
57
- ['%{', '}']
58
- else
59
- ['"', '"']
60
- end
61
-
62
- corrector.replace(node.loc.begin, starting_token)
63
- corrector.replace(node.loc.end, ending_token)
59
+ corrector.replace(node, interpolated_source(node))
64
60
  end
65
61
 
66
62
  def heredoc?(node)
@@ -68,14 +64,27 @@ module RuboCop
68
64
  end
69
65
 
70
66
  def valid_syntax?(node)
71
- double_quoted_string = if node.source.include?('"')
72
- node.source.sub(/\A'/, '%{').sub(/'\z/, '}')
73
- else
74
- node.source.gsub(/\A'|'\z/, '"')
75
- end
76
-
77
- processed_source = parse(double_quoted_string)
78
- processed_source.valid_syntax? && processed_source.ast.dstr_type?
67
+ processed_source = parse(interpolated_source(node))
68
+ return false unless processed_source.valid_syntax? && processed_source.ast.dstr_type?
69
+
70
+ !yield_outside_method?(processed_source.ast, node)
71
+ end
72
+
73
+ def interpolated_source(node)
74
+ content = node.source[1...-1]
75
+ return %("#{content}") unless content.include?('"')
76
+ return "%{#{content}}" unless continued_string_part?(node)
77
+
78
+ %("#{content.gsub(/\\.|"/) { |m| m == '"' ? '\"' : m }}")
79
+ end
80
+
81
+ # A percent literal right after another string part is parsed as the `%` operator.
82
+ def continued_string_part?(node)
83
+ node.parent&.dstr_type? && node.left_sibling
84
+ end
85
+
86
+ def yield_outside_method?(interpolated_node, node)
87
+ interpolated_node.each_node(:yield).any? && node.each_ancestor(:any_def).none?
79
88
  end
80
89
  end
81
90
  end