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
@@ -56,7 +56,6 @@ module RuboCop
56
56
  class ParenthesesAroundCondition < Base
57
57
  include SafeAssignment
58
58
  include Parentheses
59
- include RangeHelp
60
59
  extend AutoCorrector
61
60
 
62
61
  def on_if(node)
@@ -82,8 +81,10 @@ module RuboCop
82
81
 
83
82
  control_op_condition(cond) do |first_child, rest_children|
84
83
  return if require_parentheses?(node, first_child)
85
- return if semicolon_separated_expressions?(first_child, rest_children)
84
+ # `(foo; bar)` and `(foo\n bar)` would leave only `foo` as the condition.
85
+ return if rest_children.any?
86
86
  return if modifier_op?(first_child) || first_child.masgn_type?
87
+ return if command_call_assignment?(first_child)
87
88
  return if parens_allowed?(cond)
88
89
 
89
90
  message = message(cond)
@@ -100,14 +101,6 @@ module RuboCop
100
101
  condition_body.send_node.block_literal? && condition_body.keywords?
101
102
  end
102
103
 
103
- def semicolon_separated_expressions?(first_exp, rest_exps)
104
- return false unless (second_exp = rest_exps.first)
105
-
106
- range = range_between(first_exp.source_range.end_pos, second_exp.source_range.begin_pos)
107
-
108
- range.source.include?(';')
109
- end
110
-
111
104
  def modifier_op?(node)
112
105
  return false if node.if_type? && node.ternary?
113
106
  return true if node.rescue_type?
@@ -115,6 +108,23 @@ module RuboCop
115
108
  node.basic_conditional? && node.modifier_form?
116
109
  end
117
110
 
111
+ def command_call_assignment?(node)
112
+ return false if !node.assignment? && !setter_method?(node)
113
+
114
+ value = node.children.last
115
+ value = value.body if value.rescue_type?
116
+
117
+ command_call_assignment?(value) || command_call?(value)
118
+ end
119
+
120
+ def command_call?(node)
121
+ node = node.send_node if node.any_block_type?
122
+ return false if !node.type?(:call, :super, :yield) || !node.arguments?
123
+ return false if node.parenthesized? || node.operator_method? || node.setter_method?
124
+
125
+ !node.method?(:[])
126
+ end
127
+
118
128
  def message(node)
119
129
  kw = node.parent.keyword
120
130
  article = kw == 'while' ? 'a' : 'an'
@@ -220,8 +220,7 @@ module RuboCop
220
220
  replacement = "#{select_var}, #{reject_var} = #{partition_call}"
221
221
 
222
222
  corrector.replace(sibling_container, replacement)
223
- range = range_by_whole_lines(container.source_range, include_final_newline: true)
224
- corrector.remove(range)
223
+ corrector.remove(range_with_surrounding_space(container.source_range, side: :left))
225
224
  end
226
225
 
227
226
  def complementary_variable_order(sibling, container, sibling_container)
@@ -109,6 +109,9 @@ module RuboCop
109
109
  # @private
110
110
  # @param [RuboCop::AST::Node] node
111
111
  def on_back_ref_or_gvar_or_nth_ref(node)
112
+ # `alias $MATCH $&` names the variables, it does not read them.
113
+ return if node.parent&.alias_type?
114
+
112
115
  preferred_expression = preferred_expression_to_node_with_constant_prefix(node)
113
116
  return unless preferred_expression
114
117
 
@@ -86,14 +86,12 @@ module RuboCop
86
86
  end
87
87
 
88
88
  def correct_quotes(str)
89
- correction = if style == :single_quotes
90
- to_string_literal(str)
91
- else
92
- str.gsub("\\'", "'").inspect
93
- end
94
-
95
89
  # The conversion process doubles escaped slashes, so they have to be reverted
96
- correction.gsub('\\\\', '\\').gsub('\"', '"')
90
+ if style == :single_quotes
91
+ to_string_literal(str).gsub('\\\\', '\\').gsub('\"', '"')
92
+ else
93
+ str.gsub("\\'", "'").inspect.gsub('\\\\', '\\')
94
+ end
97
95
  end
98
96
 
99
97
  def style
@@ -74,7 +74,7 @@ module RuboCop
74
74
 
75
75
  arguments = [exception_node, message_node].compact.map(&:source).join(', ')
76
76
 
77
- if node.parent && requires_parens?(node.parent)
77
+ if parenthesize?(node)
78
78
  "#{node.method_name}(#{arguments})"
79
79
  else
80
80
  "#{node.method_name} #{arguments}"
@@ -86,28 +86,60 @@ module RuboCop
86
86
  return if message_nodes.size > 1
87
87
 
88
88
  argument = message_nodes.first.source
89
- exception_class = exception_node.receiver&.source || exception_node.source
89
+ exception_class = exception_class_node(exception_node).source
90
90
 
91
- if node.parent && requires_parens?(node.parent)
91
+ if parenthesize?(node)
92
92
  "#{node.method_name}(#{exception_class}.new(#{argument}))"
93
93
  else
94
94
  "#{node.method_name} #{exception_class}.new(#{argument})"
95
95
  end
96
96
  end
97
97
 
98
+ def exception_class_node(exception_node)
99
+ use_new_method?(exception_node) ? exception_node.receiver : exception_node
100
+ end
101
+
98
102
  def check_compact(node)
99
103
  if node.arguments.size > 1
100
104
  exception = node.first_argument
101
105
  return if exception.send_type? && exception.first_argument&.hash_type?
106
+ return if use_new_method?(exception) && exception.arguments?
102
107
 
103
108
  add_offense(node, message: format(COMPACT_MSG, method: node.method_name)) do |corrector|
104
- replace_raise(corrector, node, correction_exploded_to_compact(node))
109
+ correct_raise(node) { correct_to_compact(corrector, node) }
105
110
  end
106
111
  else
107
112
  correct_style_detected
108
113
  end
109
114
  end
110
115
 
116
+ def correct_to_compact(corrector, node)
117
+ if heredoc_message?(node)
118
+ correct_heredoc_exploded_to_compact(corrector, node)
119
+ else
120
+ corrector.replace(node, correction_exploded_to_compact(node))
121
+ end
122
+ end
123
+
124
+ def heredoc_message?(node)
125
+ node.parenthesized? && node.arguments.size == 2 && heredoc?(node.last_argument)
126
+ end
127
+
128
+ def heredoc?(node)
129
+ node.each_node(:any_str).any?(&:heredoc?)
130
+ end
131
+
132
+ def correct_heredoc_exploded_to_compact(corrector, node)
133
+ exception_node, message_node = *node.arguments
134
+ exception_class = exception_class_node(exception_node)
135
+ corrector.replace(range_between(exception_class, message_node), '.new(')
136
+ corrector.insert_after(message_node, ')')
137
+ end
138
+
139
+ def range_between(left_node, right_node)
140
+ left_node.source_range.end.join(right_node.source_range.begin)
141
+ end
142
+
111
143
  def check_exploded(node)
112
144
  return correct_style_detected unless node.arguments.one?
113
145
 
@@ -118,15 +150,36 @@ module RuboCop
118
150
  return if allowed_non_exploded_type?(first_arg)
119
151
 
120
152
  add_offense(node, message: format(EXPLODED_MSG, method: node.method_name)) do |corrector|
121
- replace_raise(corrector, node, correction_compact_to_exploded(node))
153
+ correct_raise(node) { correct_to_exploded(corrector, node) }
122
154
  end
123
155
  end
124
156
 
125
- def replace_raise(corrector, node, replacement)
157
+ def correct_to_exploded(corrector, node)
158
+ if heredoc_new_argument?(node)
159
+ correct_heredoc_compact_to_exploded(corrector, node)
160
+ else
161
+ corrector.replace(node, correction_compact_to_exploded(node))
162
+ end
163
+ end
164
+
165
+ def heredoc_new_argument?(node)
166
+ new_call = node.first_argument
167
+ return false unless new_call.arguments.one? && heredoc?(new_call.first_argument)
168
+
169
+ node.parenthesized? || !parenthesize?(node)
170
+ end
171
+
172
+ def correct_heredoc_compact_to_exploded(corrector, node)
173
+ new_call = node.first_argument
174
+ corrector.replace(range_between(new_call.receiver, new_call.first_argument), ', ')
175
+ corrector.remove(new_call.loc.end) if new_call.loc.end
176
+ end
177
+
178
+ def correct_raise(node)
126
179
  opposite_style_detected
127
180
  return if inside_corrected_raise?(node)
128
181
 
129
- corrector.replace(node, replacement)
182
+ yield
130
183
  ignore_node(node)
131
184
  end
132
185
 
@@ -157,6 +210,12 @@ module RuboCop
157
210
  Array(cop_config['AllowedCompactTypes']).include?(type)
158
211
  end
159
212
 
213
+ def parenthesize?(node)
214
+ return true if node.parenthesized?
215
+
216
+ node.parent && requires_parens?(node.parent)
217
+ end
218
+
160
219
  def requires_parens?(parent)
161
220
  return true if parent.operator_keyword?
162
221
  return true if parent.type?(:call, :super, :yield, :array, :pair)
@@ -100,6 +100,7 @@ module RuboCop
100
100
  return unless key
101
101
  return if accumulator_used_in_expressions?(block_node, key, value)
102
102
  return if nested_match?(key) || nested_match?(value)
103
+ return if heredoc_in_inject_numblock?(block_node, key, value)
103
104
 
104
105
  register_offense(node, block_node, key, value)
105
106
  end
@@ -124,6 +125,12 @@ module RuboCop
124
125
  end
125
126
  end
126
127
 
128
+ def heredoc_in_inject_numblock?(block_node, key, value)
129
+ return false if !block_node.numblock_type? || block_node.method?(:each_with_object)
130
+
131
+ [key, value].any? { |node| node.each_node(:any_str).any?(&:heredoc?) }
132
+ end
133
+
127
134
  def accumulator_name(block_node)
128
135
  index = block_node.method?(:each_with_object) ? 1 : 0
129
136
  block_node.argument_list[index].name
@@ -136,42 +143,20 @@ module RuboCop
136
143
  def register_offense(send_node, block_node, key_expr, value_expr)
137
144
  message = format(MSG, method: send_node.method_name)
138
145
 
146
+ # Each part is replaced separately so that heredoc bodies between them are kept.
139
147
  add_offense(send_node.loc.selector, message: message) do |corrector|
140
- corrector.replace(
141
- replacement_range(send_node, block_node),
142
- replacement(block_node, key_expr, value_expr)
143
- )
144
- end
145
- end
148
+ corrector.replace(method_call_range(send_node), 'to_h')
146
149
 
147
- def replacement(block_node, key_expr, value_expr)
148
- key_source = adjusted_source(key_expr, block_node)
149
- value_source = adjusted_source(value_expr, block_node)
150
- body = "[#{key_source}, #{value_source}]"
151
-
152
- if block_node.numblock_type?
153
- block_node.braces? ? "to_h { #{body} }" : do_end_replacement(block_node, body)
154
- else
155
- named_block_replacement(block_node, body)
156
- end
157
- end
150
+ unless block_node.numblock_type?
151
+ corrector.replace(block_node.arguments, "|#{element_arg_source(block_node)}|")
152
+ end
158
153
 
159
- def named_block_replacement(block_node, body)
160
- arg = element_arg_source(block_node)
161
- if block_node.braces?
162
- "to_h { |#{arg}| #{body} }"
163
- else
164
- do_end_replacement(block_node, body, arg)
154
+ correct_body(corrector, block_node, key_expr, value_expr)
165
155
  end
166
156
  end
167
157
 
168
- def do_end_replacement(block_node, body, arg = nil)
169
- args = arg ? " |#{arg}|" : ''
170
- "to_h do#{args}\n#{indent(block_node)} #{body}\n#{indent(block_node)}end"
171
- end
172
-
173
- def replacement_range(send_node, block_node)
174
- range_between(send_node.loc.selector.begin_pos, block_node.source_range.end_pos)
158
+ def method_call_range(send_node)
159
+ range_between(send_node.loc.selector.begin_pos, send_node.source_range.end_pos)
175
160
  end
176
161
 
177
162
  def element_arg_source(block_node)
@@ -182,6 +167,16 @@ module RuboCop
182
167
  end
183
168
  end
184
169
 
170
+ def correct_body(corrector, block_node, key_expr, value_expr)
171
+ key_source = adjusted_source(key_expr, block_node)
172
+ value_source = adjusted_source(value_expr, block_node)
173
+ body = block_node.body
174
+ assignment = body.begin_type? ? body.children.first : body
175
+
176
+ corrector.replace(assignment, "[#{key_source}, #{value_source}]")
177
+ corrector.remove(accumulator_range(body.children.last)) if body.begin_type?
178
+ end
179
+
185
180
  def adjusted_source(expr_node, block_node)
186
181
  source = element_source(expr_node)
187
182
  return source unless block_node.numblock_type?
@@ -191,10 +186,6 @@ module RuboCop
191
186
  source.gsub('_2', '_1')
192
187
  end
193
188
 
194
- def indent(node)
195
- ' ' * node.source_range.column
196
- end
197
-
198
189
  def element_source(expr_node)
199
190
  if command_call?(expr_node) || expr_node.assignment? || expr_node.rescue_type?
200
191
  return "(#{expr_node.source})"
@@ -210,6 +201,13 @@ module RuboCop
210
201
 
211
202
  node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
212
203
  end
204
+
205
+ def accumulator_range(accumulator)
206
+ range = range_with_surrounding_space(accumulator.source_range, side: :left)
207
+ return range unless range.source_buffer.source[range.begin_pos - 1] == ';'
208
+
209
+ range_with_surrounding_space(range.adjust(begin_pos: -1), side: :left, newlines: false)
210
+ end
213
211
  end
214
212
  end
215
213
  end
@@ -67,8 +67,11 @@ module RuboCop
67
67
  private
68
68
 
69
69
  def register_offense(range, node, replacement)
70
+ replacement = replacement.source_range if replacement.is_a?(AST::Node)
71
+
70
72
  add_offense(range) do |corrector|
71
- corrector.replace(node, replacement.source)
73
+ corrector.remove(node.source_range.begin.join(replacement.begin))
74
+ corrector.remove(replacement.end.join(node.source_range.end))
72
75
  end
73
76
  end
74
77
  end
@@ -62,14 +62,24 @@ module RuboCop
62
62
  # baz
63
63
  # end
64
64
  # end
65
- class RedundantBegin < Base
65
+ class RedundantBegin < Base # rubocop:disable Metrics/ClassLength
66
66
  include RangeHelp
67
67
  extend AutoCorrector
68
68
 
69
69
  MSG = 'Redundant `begin` block detected.'
70
+ SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
71
+ VALUE_PARENT_TYPES = %i[return next break optarg kwoptarg].freeze
72
+ STATEMENT_CONTAINER_TYPES = %i[
73
+ begin kwbegin any_block any_def class module sclass rescue resbody ensure
74
+ ].freeze
70
75
 
71
76
  def self.autocorrect_incompatible_with
72
- [Style::BlockDelimiters]
77
+ [
78
+ Style::BlockDelimiters,
79
+ Style::TrailingCommaInArguments,
80
+ Style::TrailingCommaInArrayLiteral,
81
+ Style::TrailingCommaInHashLiteral
82
+ ]
73
83
  end
74
84
 
75
85
  # @!method offensive_kwbegins(node)
@@ -102,7 +112,7 @@ module RuboCop
102
112
  body = node.body
103
113
 
104
114
  return unless body&.kwbegin_type?
105
- return if body.rescue_node || body.ensure_node
115
+ return if body.ensure_node || body.rescue_node
106
116
 
107
117
  register_offense(body)
108
118
  end
@@ -132,31 +142,64 @@ module RuboCop
132
142
  empty_begin?(node) ||
133
143
  begin_block_has_multiline_statements?(node) ||
134
144
  contain_rescue_or_ensure?(node) ||
135
- valid_context_using_only_begin?(node)
145
+ valid_context_using_only_begin?(node) ||
146
+ ternary_operand_requiring_begin?(node)
136
147
  end
137
148
 
138
149
  def register_offense(node)
150
+ return if end_cannot_join_statement_line?(node)
151
+
139
152
  offense_range = node.loc.begin
140
153
 
141
154
  add_offense(offense_range) do |corrector|
142
- if node.parent&.assignment? || node.parent&.type?(:return, :next, :break)
155
+ if node.parent&.assignment? || node.parent&.type?(*VALUE_PARENT_TYPES)
143
156
  replace_begin_with_statement(corrector, offense_range, node)
144
157
  else
145
158
  remove_begin(corrector, offense_range, node)
146
159
  end
147
160
 
148
- if use_modifier_form_after_multiline_begin_block?(node)
149
- correct_modifier_form_after_multiline_begin_block(corrector, node)
150
- end
151
- corrector.remove(node.loc.end)
161
+ correct_end(corrector, node)
152
162
  end
153
163
  end
154
164
 
165
+ def end_cannot_join_statement_line?(node)
166
+ return false if use_modifier_form_after_multiline_begin_block?(node)
167
+ return false unless code_after_end_on_its_own_line?(node)
168
+
169
+ range_with_end_line_break(node).begin_pos != statement_end_pos(node)
170
+ end
171
+
172
+ def use_modifier_form_after_multiline_begin_block?(node)
173
+ return false unless (parent = node.parent)
174
+ return false unless node.multiline? && parent.if_type? && parent.modifier_form?
175
+
176
+ !parent.condition.equal?(node)
177
+ end
178
+
179
+ def code_after_end_on_its_own_line?(node)
180
+ end_range = node.loc.end
181
+ return false unless begins_its_line?(end_range)
182
+
183
+ rest_of_line = end_range.source_line[end_range.last_column..].lstrip
184
+
185
+ !rest_of_line.empty? && !rest_of_line.start_with?('#')
186
+ end
187
+
188
+ def range_with_end_line_break(node)
189
+ range_with_surrounding_space(node.loc.end, side: :left, newlines: true)
190
+ end
191
+
192
+ def statement_end_pos(node)
193
+ return node.loc.begin.end_pos if node.children.empty?
194
+
195
+ node.children.last.source_range.end_pos
196
+ end
197
+
155
198
  def replace_begin_with_statement(corrector, offense_range, node)
156
199
  first_child = node.children.first
157
200
 
158
201
  source = first_child.source
159
- source = "(#{source})" if low_precedence_statement?(first_child)
202
+ source = "(#{source})" if parentheses_required?(first_child, node.parent)
160
203
 
161
204
  corrector.replace(offense_range, source)
162
205
  corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
@@ -164,6 +207,15 @@ module RuboCop
164
207
  restore_removed_comments(corrector, offense_range, node, first_child)
165
208
  end
166
209
 
210
+ def parentheses_required?(statement, parent)
211
+ return true if low_precedence_statement?(statement)
212
+ return false unless command_call?(statement)
213
+ return parent.type?(:optarg, :kwoptarg) unless parent.assignment?
214
+
215
+ parent = parent.parent while parent.parent&.assignment?
216
+ !statement_position?(parent)
217
+ end
218
+
167
219
  def low_precedence_statement?(node)
168
220
  return node.modifier_form? if node.basic_conditional?
169
221
  return node.prefix_not? if node.send_type?
@@ -171,6 +223,22 @@ module RuboCop
171
223
  node.operator_keyword? && node.semantic_operator?
172
224
  end
173
225
 
226
+ def command_call?(node)
227
+ node = node.send_node if node.any_block_type?
228
+ return false unless node.type?(:call, :super, :yield)
229
+
230
+ node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
231
+ end
232
+
233
+ def statement_position?(node)
234
+ parent = node.parent
235
+ return true if parent.nil? || parent.type?(*STATEMENT_CONTAINER_TYPES)
236
+ return parent.body.equal?(node) if parent.type?(:when, :in_pattern, :for)
237
+ return false if parent.if_type? && parent.ternary?
238
+
239
+ parent.type?(:if, :while, :until, :case, :case_match) && !parent.condition.equal?(node)
240
+ end
241
+
174
242
  def remove_begin(corrector, offense_range, node)
175
243
  if node.parent.respond_to?(:endless?) && node.parent.endless?
176
244
  offense_range = range_with_surrounding_space(offense_range, newlines: true)
@@ -188,11 +256,14 @@ module RuboCop
188
256
  corrector.insert_before(node.parent, comments) unless comments.blank?
189
257
  end
190
258
 
191
- def use_modifier_form_after_multiline_begin_block?(node)
192
- return false unless (parent = node.parent)
193
-
194
- node.multiline? && parent.if_type? && parent.modifier_form? &&
195
- !parent.condition.equal?(node)
259
+ def correct_end(corrector, node)
260
+ if use_modifier_form_after_multiline_begin_block?(node)
261
+ correct_modifier_form_after_multiline_begin_block(corrector, node)
262
+ elsif code_after_end_on_its_own_line?(node)
263
+ corrector.remove(range_with_end_line_break(node))
264
+ else
265
+ corrector.remove(node.loc.end)
266
+ end
196
267
  end
197
268
 
198
269
  def correct_modifier_form_after_multiline_begin_block(corrector, node)
@@ -229,8 +300,16 @@ module RuboCop
229
300
  parent&.send_type? || parent&.operator_keyword?
230
301
  end
231
302
 
303
+ def ternary_operand_requiring_begin?(node)
304
+ return false unless node.parent&.if_type? && node.parent.ternary?
305
+
306
+ !node.children.one? || low_precedence_statement?(node.children.first)
307
+ end
308
+
232
309
  def valid_begin_assignment?(node)
233
- node.parent&.assignment? && (!node.children.one? || too_long_when_joined?(node))
310
+ return false unless node.parent&.assignment?
311
+
312
+ !node.children.one? || too_long_when_joined?(node) || multiline_memoization?(node)
234
313
  end
235
314
 
236
315
  def too_long_when_joined?(node)
@@ -239,14 +318,37 @@ module RuboCop
239
318
  return false unless max && statement.single_line?
240
319
 
241
320
  length = statement.source.length
242
- length += 2 if low_precedence_statement?(statement)
321
+ length += parentheses_width if low_precedence_statement?(statement)
322
+
243
323
  node.loc.column + length > max
244
324
  end
245
325
 
326
+ def parentheses_width
327
+ space_inside_parens? ? 4 : 2
328
+ end
329
+
330
+ def space_inside_parens?
331
+ return false unless config.cop_enabled?('Layout/SpaceInsideParens')
332
+
333
+ style = config.for_cop('Layout/SpaceInsideParens')['EnforcedStyle']
334
+
335
+ SPACE_INSIDE_PARENS_STYLES.include?(style)
336
+ end
337
+
338
+ def multiline_memoization?(node)
339
+ if !node.parent.or_asgn_type? || !config.cop_enabled?('Style/MultilineMemoization')
340
+ return false
341
+ end
342
+
343
+ statement = node.children.first
344
+ statement.multiline? && low_precedence_statement?(statement)
345
+ end
346
+
246
347
  def inspect_branches(node)
247
348
  node.branches.each do |branch|
248
349
  next unless branch&.kwbegin_type?
249
- next if branch.rescue_node || branch.ensure_node
350
+ next if branch.ensure_node || branch.rescue_node
351
+ next if ternary_operand_requiring_begin?(branch)
250
352
 
251
353
  register_offense(branch)
252
354
  end
@@ -85,7 +85,7 @@ module RuboCop
85
85
  def range(node)
86
86
  return node.selector unless node.method?(:each)
87
87
 
88
- if node.parent&.call_type?
88
+ if receiver_of_dot_call?(node)
89
89
  node.selector.join(node.parent.loc.dot)
90
90
  else
91
91
  node.loc.dot.join(node.selector)
@@ -112,6 +112,13 @@ module RuboCop
112
112
  corrector.replace(redundant_node.loc.selector, 'each.with_object')
113
113
  end
114
114
  end
115
+
116
+ def receiver_of_dot_call?(node)
117
+ parent = node.parent
118
+ return false unless parent&.call_type?
119
+
120
+ parent.receiver.equal?(node) && parent.loc?(:dot)
121
+ end
115
122
  end
116
123
  end
117
124
  end
@@ -37,23 +37,18 @@ module RuboCop
37
37
  private
38
38
 
39
39
  def fix_exploded(node)
40
- exploded?(node) do |command, message|
40
+ exploded?(node) do |exception, message|
41
41
  # `raise RuntimeError, nil` uses the class name as the message, so
42
42
  # rewriting it to `raise nil.to_s` (an empty message) would change it.
43
43
  next if message.nil_type?
44
44
 
45
45
  add_offense(node, message: MSG_1) do |corrector|
46
- corrector.replace(node, replaced_exploded(node, command, message))
46
+ corrector.remove(exception.source_range.join(message.source_range.begin))
47
+ corrector.replace(message, message_source(message))
47
48
  end
48
49
  end
49
50
  end
50
51
 
51
- def replaced_exploded(node, command, message)
52
- arg = message_source(message)
53
- arg = node.parenthesized? ? "(#{arg})" : " #{arg}"
54
- "#{command}#{arg}"
55
- end
56
-
57
52
  def string_message?(message)
58
53
  message.any_str_type?
59
54
  end
@@ -63,11 +58,20 @@ module RuboCop
63
58
  next if message.nil_type?
64
59
 
65
60
  add_offense(node, message: MSG_2) do |corrector|
61
+ # Replacing the call would drop a heredoc body that sits inside its parentheses.
62
+ next if heredoc_body_inside?(new_call, message)
63
+
66
64
  corrector.replace(new_call, message_source(message))
67
65
  end
68
66
  end
69
67
  end
70
68
 
69
+ def heredoc_body_inside?(new_call, message)
70
+ message.each_node(:any_str).any? do |str|
71
+ str.heredoc? && str.loc.heredoc_end.end_pos < new_call.source_range.end_pos
72
+ end
73
+ end
74
+
71
75
  def message_source(message)
72
76
  if string_message?(message)
73
77
  message.source
@@ -90,7 +94,7 @@ module RuboCop
90
94
 
91
95
  # @!method exploded?(node)
92
96
  def_node_matcher :exploded?, <<~PATTERN
93
- (send nil? ${:raise :fail} (const {nil? cbase} :RuntimeError) $_)
97
+ (send nil? {:raise :fail} $(const {nil? cbase} :RuntimeError) $_)
94
98
  PATTERN
95
99
 
96
100
  # @!method compact?(node)