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
@@ -61,10 +61,13 @@ module RuboCop
61
61
  MSG_CLASS_KEYWORD =
62
62
  'Use the `class` keyword instead of `Class.new` to define an empty class.'
63
63
  MSG_CLASS_NEW = 'Use `Class.new` instead of the `class` keyword to define an empty class.'
64
+ STATEMENTS_TYPES = %i[
65
+ begin kwbegin if case case_match while until for any_def class module sclass
66
+ ].freeze
64
67
 
65
68
  # @!method class_new_assignment(node)
66
69
  def_node_matcher :class_new_assignment, <<~PATTERN
67
- (casgn _ _ $(send (const _ :Class) :new _))
70
+ (casgn _ _ $(send (const {nil? cbase} :Class) :new _))
68
71
  PATTERN
69
72
 
70
73
  def on_casgn(node)
@@ -83,6 +86,7 @@ module RuboCop
83
86
  return unless node.parent_class
84
87
  return if (body = node.body) && !body.children.empty?
85
88
  return if allowed_parent_class?(node.parent_class.source)
89
+ return if do_end_block_with_command_call?(node.parent_class)
86
90
 
87
91
  add_offense(node, message: MSG_CLASS_NEW) do |corrector|
88
92
  autocorrect_class_definition(corrector, node)
@@ -99,6 +103,20 @@ module RuboCop
99
103
  cop_config.fetch('AllowedParentClasses', [])
100
104
  end
101
105
 
106
+ def do_end_block_with_command_call?(node)
107
+ return false if node.type?(*STATEMENTS_TYPES)
108
+ return do_end_block_on_command_call?(node) if node.any_block_type?
109
+
110
+ node.each_child_node.any? { |child| do_end_block_with_command_call?(child) }
111
+ end
112
+
113
+ def do_end_block_on_command_call?(block)
114
+ send_node = block.send_node
115
+ return true if block.keywords? && send_node.arguments? && !send_node.parenthesized?
116
+
117
+ do_end_block_with_command_call?(send_node)
118
+ end
119
+
102
120
  def autocorrect_class_new(corrector, node, class_new_node)
103
121
  indent = ' ' * node.loc.column
104
122
  class_name = constant_name(node)
@@ -107,11 +125,10 @@ module RuboCop
107
125
  corrector.replace(node, "class #{class_name} < #{parent_class_name}\n#{indent}end")
108
126
  end
109
127
 
110
- # Preserve any namespace on the assigned constant (e.g. `Foo::Bar`),
128
+ # Preserve any namespace on the assigned constant (e.g. `Foo::Bar` or `::Bar`),
111
129
  # which `node.name` drops.
112
130
  def constant_name(node)
113
- namespace = node.namespace
114
- namespace ? "#{namespace.source}::#{node.name}" : node.name
131
+ node.loc.name.with(begin_pos: node.source_range.begin_pos).source
115
132
  end
116
133
 
117
134
  def autocorrect_class_definition(corrector, node)
@@ -34,6 +34,12 @@ module RuboCop
34
34
 
35
35
  RESTRICT_ON_SEND = %i[new [] Array Hash].freeze
36
36
 
37
+ # A hash literal as the first argument needs the parentheses of the call,
38
+ # which `Style/MethodCallWithArgsParentheses` removes in the same pass.
39
+ def self.autocorrect_incompatible_with
40
+ [Style::MethodCallWithArgsParentheses]
41
+ end
42
+
37
43
  # @!method array_node(node)
38
44
  def_node_matcher :array_node, '(send (const {nil? cbase} :Array) :new (array)?)'
39
45
 
@@ -98,6 +104,7 @@ module RuboCop
98
104
  def first_argument_unparenthesized?(node)
99
105
  parent = node.parent
100
106
  return false unless parent && %i[send super zsuper].include?(parent.type)
107
+ return false if parent.operator_method? && !parent.loc.dot
101
108
 
102
109
  node.equal?(parent.first_argument) && !parentheses?(node.parent)
103
110
  end
@@ -48,6 +48,8 @@ module RuboCop
48
48
  include ConfigurableEnforcedStyle
49
49
  extend AutoCorrector
50
50
 
51
+ STRING_BEGIN_TYPES = %i[tSTRING_BEG tXSTRING_BEG].freeze
52
+
51
53
  MSG_COMPACT = 'Put empty method definitions on a single line.'
52
54
  MSG_EXPANDED = 'Put the `end` of empty method definitions on the next line.'
53
55
 
@@ -56,6 +58,8 @@ module RuboCop
56
58
  return if correct_style?(node) || compact_style_disallowed?
57
59
 
58
60
  add_offense(node) do |corrector|
61
+ next if heredoc_before_end?(node)
62
+
59
63
  correction = corrected(node)
60
64
  next if correction_exceeds_line_length?(correction)
61
65
 
@@ -80,18 +84,34 @@ module RuboCop
80
84
  config.for_enabled_cop('Style/SingleLineMethods')['AllowIfMethodIsEmpty'] == false
81
85
  end
82
86
 
87
+ def heredoc_before_end?(node)
88
+ range = node.source_range.adjust(begin_pos: -node.source_range.column)
89
+ processed_source.tokens_within(range).any? do |token|
90
+ STRING_BEGIN_TYPES.include?(token.type) && token.text.start_with?('<<')
91
+ end
92
+ end
93
+
83
94
  def corrected(node)
84
- scope = node.receiver ? "#{node.receiver.source}." : ''
85
95
  arguments = if node.arguments?
86
96
  args = node.arguments.map(&:source).join(', ')
87
97
 
88
98
  parentheses?(node.arguments) ? "(#{args})" : " #{args}"
89
99
  end
90
- signature = [scope, node.method_name, arguments].join
100
+ signature = [scope(node), node.method_name, arguments].join
91
101
 
92
102
  ["def #{signature}", 'end'].join(joint(node))
93
103
  end
94
104
 
105
+ def scope(node)
106
+ return '' unless (receiver = node.receiver)
107
+
108
+ parenthesized_receiver?(node) ? "(#{receiver.source})." : "#{receiver.source}."
109
+ end
110
+
111
+ def parenthesized_receiver?(node)
112
+ node.loc.keyword.end.join(node.receiver.source_range.begin).source.include?('(')
113
+ end
114
+
95
115
  def correction_exceeds_line_length?(correction)
96
116
  compact_style? && max_line_length && correction.size > max_line_length
97
117
  end
@@ -102,10 +122,13 @@ module RuboCop
102
122
  compact_style? ? '; ' : "\n#{indent}"
103
123
  end
104
124
 
105
- # A method with an argument spanning multiple lines (e.g. a hash default
125
+ # A method with a receiver or an argument spanning multiple lines (e.g. a hash default
106
126
  # value) can't be put on a single line, so it's as compact as it gets.
107
127
  def compact?(node)
108
- node.single_line? || node.arguments.any?(&:multiline?)
128
+ return true if node.single_line?
129
+ return true if node.receiver&.multiline?
130
+
131
+ node.arguments.any?(&:multiline?)
109
132
  end
110
133
 
111
134
  def expanded?(node)
@@ -128,14 +128,12 @@ module RuboCop
128
128
  end
129
129
 
130
130
  def ternary_style_autocorrect(node, outcome, keyword)
131
+ return if outcome.nil? || outcome.multiline?
132
+
131
133
  add_offense(node, message: MSG_TERNARY) do |corrector|
132
134
  next if empty_string_conditional_in_condition?(node)
133
135
 
134
- head = node.source_range.begin.join(outcome.source_range.begin)
135
- tail = outcome.source_range.end.join(node.source_range.end)
136
-
137
- corrector.remove(head)
138
- corrector.replace(tail, " #{keyword} #{node.condition.source}")
136
+ autocorrect_to_trailing_conditional(corrector, node, outcome, keyword)
139
137
  end
140
138
  end
141
139
 
@@ -146,6 +144,14 @@ module RuboCop
146
144
  empty_if_outcome?(node) || empty_else_outcome?(node)
147
145
  end
148
146
  end
147
+
148
+ def autocorrect_to_trailing_conditional(corrector, node, outcome, keyword)
149
+ head = node.source_range.begin.join(outcome.source_range.begin)
150
+ tail = outcome.source_range.end.join(node.source_range.end)
151
+
152
+ corrector.remove(head)
153
+ corrector.replace(tail, " #{keyword} #{node.condition.source}")
154
+ end
149
155
  end
150
156
  end
151
157
  end
@@ -147,15 +147,20 @@ module RuboCop
147
147
 
148
148
  # `Style/MethodDefParentheses` adds the same parentheses a parameter list needs in
149
149
  # an endless definition, so letting both correct in the same pass would double them.
150
- # `Style/GuardClause` turns a single `if` body into several statements,
151
- # which an endless definition cannot hold.
150
+ # `Style/GuardClause` and `Style/IdenticalConditionalBranches` turn a body that is
151
+ # a single conditional into several statements, which an endless definition cannot hold.
152
152
  # `Style/RaiseArgs` would drop the parentheses of a `raise` that becomes
153
153
  # the body in the same pass, which a method passed as an argument needs.
154
154
  # `Style/IfUnlessModifier` and `Style/WhileUntilModifier` would turn the body into
155
155
  # a modifier form, which then applies to the whole endless definition.
156
+ # `Lint/LiteralAsCondition` and `Lint/UselessTimes` replace the body with
157
+ # the statements inside it, which an endless definition may not hold.
156
158
  def self.autocorrect_incompatible_with
157
159
  [
160
+ Lint::LiteralAsCondition,
161
+ Lint::UselessTimes,
158
162
  Style::GuardClause,
163
+ Style::IdenticalConditionalBranches,
159
164
  Style::IfUnlessModifier,
160
165
  Style::MethodCallWithArgsParentheses,
161
166
  Style::MethodDefParentheses,
@@ -187,7 +192,7 @@ module RuboCop
187
192
  return if node.single_line? || style == :allow_always
188
193
 
189
194
  add_offense(node, message: MSG_MULTI_LINE) do |corrector|
190
- next if endless_parent?(node)
195
+ next if nested_in_multiline_correction?(node)
191
196
 
192
197
  correct_to_multiline(corrector, node)
193
198
  end
@@ -196,10 +201,14 @@ module RuboCop
196
201
  def handle_require_single_line_style(node)
197
202
  if node.endless? && node.multiline?
198
203
  add_offense(node, message: MSG_MULTI_LINE) do |corrector|
204
+ next if nested_in_multiline_correction?(node)
205
+
199
206
  correct_to_multiline(corrector, node)
200
207
  end
201
208
  elsif convertible_to_endless?(node) && single_line_when_made_endless?(node)
202
209
  add_offense(node, message: MSG_REQUIRE_SINGLE) do |corrector|
210
+ next if nested_in_multiline_correction?(node)
211
+
203
212
  correct_to_endless(corrector, node)
204
213
  end
205
214
  end
@@ -222,14 +231,29 @@ module RuboCop
222
231
  return unless node.endless?
223
232
 
224
233
  add_offense(node) do |corrector|
225
- next if endless_parent?(node)
234
+ next if nested_in_multiline_correction?(node)
226
235
 
227
236
  correct_to_multiline(corrector, node)
228
237
  end
229
238
  end
230
239
 
231
- def endless_parent?(node)
232
- node.parent&.any_def_type? && node.parent.endless?
240
+ def nested_in_multiline_correction?(node)
241
+ node.each_ancestor(:any_def).any? do |ancestor|
242
+ corrected_to_multiline?(ancestor)
243
+ end
244
+ end
245
+
246
+ def corrected_to_multiline?(node)
247
+ return false if !node.endless? || use_heredoc?(node)
248
+
249
+ case style
250
+ when :disallow
251
+ true
252
+ when :allow_single_line, :require_single_line
253
+ node.multiline?
254
+ else
255
+ false
256
+ end
233
257
  end
234
258
 
235
259
  def use_heredoc?(node)
@@ -274,10 +298,6 @@ module RuboCop
274
298
  RUBY
275
299
  end
276
300
 
277
- def receiver(node)
278
- node.receiver ? "#{node.receiver.source}#{node.loc.operator.source}" : ''
279
- end
280
-
281
301
  def arguments(node, missing = '')
282
302
  return missing unless node.arguments.any?
283
303
  return parenthesized_arguments(node) if unparenthesized_arguments?(node)
@@ -50,11 +50,15 @@ module RuboCop
50
50
  (block $_ (args $...) (yield $...))
51
51
  PATTERN
52
52
 
53
- # `Style/LambdaCall` rewrites the `call` that the block argument is appended to, and
54
- # `Style/TrailingCommaInArguments` changes the trailing comma where it is appended,
55
- # in the same pass.
53
+ # `Style/LambdaCall` rewrites the `call` that the block argument is appended to,
54
+ # `Style/MethodCallWithArgsParentheses` and `Style/NestedParenthesizedCalls` add the same
55
+ # parentheses around a last argument without them, and `Style/TrailingCommaInArguments`
56
+ # changes the trailing comma where it is appended, in the same pass.
56
57
  def self.autocorrect_incompatible_with
57
- [Lint::UnusedMethodArgument, Style::LambdaCall, Style::TrailingCommaInArguments]
58
+ [
59
+ Style::LambdaCall, Style::MethodCallWithArgsParentheses,
60
+ Style::NestedParenthesizedCalls, Style::TrailingCommaInArguments
61
+ ]
58
62
  end
59
63
 
60
64
  def initialize(config = nil, options = nil)
@@ -166,12 +170,23 @@ module RuboCop
166
170
 
167
171
  def insert_argument(node, corrector, block_name)
168
172
  last_arg = node.last_argument
173
+ parenthesize_arguments(last_arg, corrector) if command_call?(last_arg)
169
174
  arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
170
175
  replacement = " &#{block_name}"
171
176
  replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
172
177
  corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
173
178
  end
174
179
 
180
+ def command_call?(node)
181
+ node.call_type? && node.arguments? && !node.parenthesized? &&
182
+ !node.operator_method? && !node.setter_method?
183
+ end
184
+
185
+ def parenthesize_arguments(node, corrector)
186
+ corrector.replace(node.loc.selector.end.join(node.first_argument.source_range.begin), '(')
187
+ corrector.insert_after(node.last_argument, ')')
188
+ end
189
+
175
190
  def correct_call_node(node, corrector, block_name)
176
191
  new_arguments = if node.zsuper_type?
177
192
  args = build_new_arguments_for_zsuper(node) << "&#{block_name}"
@@ -44,7 +44,7 @@ module RuboCop
44
44
 
45
45
  # @!method env_with_bracket?(node)
46
46
  def_node_matcher :env_with_bracket?, <<~PATTERN
47
- (send (const nil? :ENV) :[] $_)
47
+ (send (const {nil? cbase} :ENV) :[] $_)
48
48
  PATTERN
49
49
 
50
50
  def on_send(node)
@@ -53,7 +53,7 @@ module RuboCop
53
53
 
54
54
  message = format(offense_message, key: name_node.source)
55
55
  add_offense(node, message: message) do |corrector|
56
- corrector.replace(node, new_code(name_node))
56
+ corrector.replace(node, new_code(node.receiver, name_node))
57
57
  end
58
58
  end
59
59
  end
@@ -146,11 +146,11 @@ module RuboCop
146
146
  parent.lhs == node || parent.parent&.or_type?
147
147
  end
148
148
 
149
- def new_code(name_node)
149
+ def new_code(env_node, name_node)
150
150
  if default_to_nil?
151
- "ENV.fetch(#{name_node.source}, nil)"
151
+ "#{env_node.source}.fetch(#{name_node.source}, nil)"
152
152
  else
153
- "ENV.fetch(#{name_node.source})"
153
+ "#{env_node.source}.fetch(#{name_node.source})"
154
154
  end
155
155
  end
156
156
  end
@@ -60,7 +60,11 @@ module RuboCop
60
60
 
61
61
  # @!method block_read?(node)
62
62
  def_node_matcher :block_read?, <<~PATTERN
63
- (block _ (args (arg _name)) (send (lvar _name) :read))
63
+ {
64
+ (block _ (args (arg _name)) (send (lvar _name) :read))
65
+ (numblock _ 1 (send (lvar :_1) :read))
66
+ (itblock _ :it (send (lvar :it) :read))
67
+ }
64
68
  PATTERN
65
69
 
66
70
  def on_send(node)
@@ -63,7 +63,11 @@ module RuboCop
63
63
 
64
64
  # @!method block_write?(node)
65
65
  def_node_matcher :block_write?, <<~PATTERN
66
- (block _ (args (arg $_)) (send (lvar $_) :write $_))
66
+ {
67
+ (block _ (args (arg _name)) (send (lvar _name) :write $_))
68
+ (numblock _ 1 (send (lvar :_1) :write $_))
69
+ (itblock _ :it (send (lvar :it) :write $_))
70
+ }
67
71
  PATTERN
68
72
 
69
73
  def on_send(node)
@@ -71,10 +75,16 @@ module RuboCop
71
75
  message = format(MSG, write_method: write_method(mode))
72
76
 
73
77
  add_offense(write_node, message: message) do |corrector|
74
- range = range_between(node.loc.selector.begin_pos, write_node.source_range.end_pos)
75
- replacement = replacement(mode, filename, content, write_node)
78
+ range = replaced_range(node, write_node)
79
+ heredocs = removed_heredocs(filename, content, write_node)
80
+ next if foreign_heredoc_body?(range, heredocs)
76
81
 
77
- corrector.replace(range, replacement)
82
+ heredocs = restored_heredocs(heredocs, [filename, content])
83
+ next unless restorable?(range, heredocs, write_node)
84
+
85
+ replacement = "#{write_method(mode)}(#{filename.source}, #{content.source})"
86
+
87
+ replace_and_restore_heredocs(corrector, range, replacement, heredocs)
78
88
  end
79
89
  end
80
90
  end
@@ -90,9 +100,7 @@ module RuboCop
90
100
  private
91
101
 
92
102
  def file_open_write?(node)
93
- content = send_write?(node) || block_write?(node) do |block_arg, lvar, write_arg|
94
- write_arg if block_arg == lvar
95
- end
103
+ content = send_write?(node) || block_write?(node)
96
104
  return false if content&.splat_type?
97
105
 
98
106
  yield(content) if content
@@ -102,27 +110,16 @@ module RuboCop
102
110
  mode.end_with?('b') ? :binwrite : :write
103
111
  end
104
112
 
105
- def replacement(mode, filename, content, write_node)
106
- replacement = "#{write_method(mode)}(#{filename.source}, #{content.source})"
107
- heredocs = removed_heredocs(filename, content, write_node)
108
- return replacement if heredocs.empty?
109
-
110
- [replacement, *heredocs.map { |heredoc| heredoc_range(heredoc).source }].join("\n")
113
+ def replaced_range(node, write_node)
114
+ range_between(node.loc.selector.begin_pos, write_node.source_range.end_pos)
111
115
  end
112
116
 
113
- # Heredocs opened in the arguments keep working in the replacement, but their
114
- # bodies are lost when they lie within the replaced range, so they need to be
115
- # restored after the replacement.
116
117
  def removed_heredocs(filename, content, write_node)
117
118
  [filename, content].flat_map { |argument| find_heredocs(argument) }
118
119
  .select { |heredoc| removed?(heredoc, write_node) }
119
120
  .sort_by { |heredoc| heredoc.loc.heredoc_body.begin_pos }
120
121
  end
121
122
 
122
- def heredoc_range(heredoc)
123
- range_between(heredoc.loc.heredoc_body.begin_pos, heredoc.loc.heredoc_end.end_pos)
124
- end
125
-
126
123
  def find_heredocs(node)
127
124
  [node, *node.each_descendant(:any_str)].select do |child|
128
125
  child.respond_to?(:heredoc?) && child.heredoc?
@@ -132,6 +129,78 @@ module RuboCop
132
129
  def removed?(heredoc, write_node)
133
130
  heredoc.loc.heredoc_end.end_pos <= write_node.source_range.end_pos
134
131
  end
132
+
133
+ def foreign_heredoc_body?(range, heredocs)
134
+ find_heredocs(processed_source.ast).any? do |heredoc|
135
+ next false if heredocs.any? { |removed| removed.equal?(heredoc) }
136
+
137
+ range.contains?(heredoc_range(heredoc))
138
+ end
139
+ end
140
+
141
+ def heredoc_range(heredoc)
142
+ range_between(heredoc.loc.heredoc_body.begin_pos, heredoc.loc.heredoc_end.end_pos)
143
+ end
144
+
145
+ def restored_heredocs(heredocs, arguments)
146
+ containers = arguments.map(&:source_range) + heredocs.map { |h| heredoc_range(h) }
147
+
148
+ heredocs.reject do |heredoc|
149
+ body = heredoc_range(heredoc)
150
+
151
+ containers.any? { |container| container != body && container.contains?(body) }
152
+ end
153
+ end
154
+
155
+ def restorable?(range, heredocs, write_node)
156
+ return !overlaps_later_replacement?(range, write_node) if heredocs.empty?
157
+
158
+ rest = rest_of_line(range)
159
+ return false if line_ends_inside_literal?(rest)
160
+
161
+ !overlaps_later_replacement?(range.join(rest), write_node)
162
+ end
163
+
164
+ def rest_of_line(range)
165
+ range.end.join(processed_source.buffer.line_range(range.last_line).end)
166
+ end
167
+
168
+ def line_ends_inside_literal?(rest)
169
+ line_end = rest.end_pos
170
+ innermost = nil
171
+ processed_source.ast.each_node do |node|
172
+ range = node.source_range
173
+ innermost = node if range && range.begin_pos < line_end && line_end < range.end_pos
174
+ end
175
+
176
+ !innermost.nil? && literal?(innermost)
177
+ end
178
+
179
+ def literal?(node)
180
+ return node.percent_literal? if node.array_type?
181
+
182
+ node.type?(:any_str, :regexp, :any_sym)
183
+ end
184
+
185
+ def overlaps_later_replacement?(corrected, write_node)
186
+ processed_source.ast.each_node(:send).any? do |node|
187
+ other_write_node = evidence(node) { |*, other| other }
188
+ next false unless other_write_node
189
+ next false if other_write_node.equal?(write_node)
190
+
191
+ range = replaced_range(node, other_write_node)
192
+ corrected.begin_pos < range.begin_pos && range.begin_pos < corrected.end_pos
193
+ end
194
+ end
195
+
196
+ def replace_and_restore_heredocs(corrector, range, replacement, heredocs)
197
+ return corrector.replace(range, replacement) if heredocs.empty?
198
+
199
+ rest = rest_of_line(range)
200
+ bodies = heredocs.map { |heredoc| "\n#{heredoc_range(heredoc).source}" }
201
+
202
+ corrector.replace(range.join(rest), "#{replacement}#{rest.source}#{bodies.join}")
203
+ end
135
204
  end
136
205
  end
137
206
  end
@@ -53,13 +53,16 @@ module RuboCop
53
53
  PREFER_EACH = 'Prefer `each` over `for`.'
54
54
  PREFER_FOR = 'Prefer `for` over `each`.'
55
55
  IMPLICIT_PARAMETERS = %i[_1 _2 _3 _4 _5 _6 _7 _8 _9 it].to_set.freeze
56
+ SCOPE_TYPES = %i[any_def class module sclass].freeze
56
57
 
58
+ # `Style/BlockDelimiters` can turn a brace block in the receiver into `do...end`
59
+ # in the same pass, which is a syntax error in the `for` collection.
57
60
  # `Style/RedundantBegin` removes the `begin` of a block body in the same pass,
58
61
  # leaving a `rescue` or `ensure` clause directly in the `for` body.
59
62
  # `Style/ItBlockParameter` replaces block parameter references in the body
60
63
  # with `it` in the same pass, while they become `for` loop variables.
61
64
  def self.autocorrect_incompatible_with
62
- [Style::RedundantBegin, Style::ItBlockParameter]
65
+ [Style::BlockDelimiters, Style::RedundantBegin, Style::ItBlockParameter]
63
66
  end
64
67
 
65
68
  def on_for(node)
@@ -80,10 +83,9 @@ module RuboCop
80
83
 
81
84
  if style == :for
82
85
  return unless node.receiver
83
- return unless convertible_to_for?(node)
84
- return if rescue_or_ensure_body?(node) || do_end_block_in_collection?(node)
86
+ return unless correctable_to_for?(node)
85
87
 
86
- add_offense(node, message: PREFER_FOR) do |corrector|
88
+ add_offense(offense_range(node), message: PREFER_FOR) do |corrector|
87
89
  EachToForCorrector.new(node).call(corrector)
88
90
  opposite_style_detected
89
91
  end
@@ -126,6 +128,13 @@ module RuboCop
126
128
  node.multiline? && node.method?(:each) && !node.send_node.arguments?
127
129
  end
128
130
 
131
+ def correctable_to_for?(node)
132
+ return false unless convertible_to_for?(node)
133
+ return false if rescue_or_ensure_body?(node) || do_end_block_in_collection?(node)
134
+
135
+ !leaked_variable_called_later?(node)
136
+ end
137
+
129
138
  def convertible_to_for?(node)
130
139
  return false if node.type?(:numblock, :itblock)
131
140
  return true unless node.arguments?
@@ -143,6 +152,52 @@ module RuboCop
143
152
  def do_end_block_in_collection?(node)
144
153
  node.receiver.each_node(:any_block).any?(&:keywords?)
145
154
  end
155
+
156
+ def leaked_variable_called_later?(node)
157
+ names = leaked_variable_names(node)
158
+ return false if names.empty?
159
+
160
+ calls = innermost_scope(node, :any_block, *SCOPE_TYPES).each_descendant(:send)
161
+ scope = innermost_scope(node, *SCOPE_TYPES)
162
+
163
+ calls.any? { |call| leaked_name_called?(call, node, names, scope) }
164
+ end
165
+
166
+ def leaked_name_called?(call, node, names, scope)
167
+ return false unless names.include?(call.method_name) && variable_like_call?(call)
168
+ return false if call.source_range.begin_pos < node.source_range.end_pos
169
+
170
+ innermost_scope(call, *SCOPE_TYPES).equal?(scope)
171
+ end
172
+
173
+ def leaked_variable_names(node)
174
+ names = node.arguments.each_descendant(:arg, :restarg).filter_map(&:name)
175
+ return names unless node.body
176
+
177
+ assigned_names = node.body.each_node(:lvasgn).filter_map do |lvasgn|
178
+ lvasgn.name if innermost_scope(lvasgn, :any_block, *SCOPE_TYPES).equal?(node)
179
+ end
180
+
181
+ names + assigned_names
182
+ end
183
+
184
+ def innermost_scope(node, *types)
185
+ node.each_ancestor(*types).first || processed_source.ast
186
+ end
187
+
188
+ def variable_like_call?(send_node)
189
+ !send_node.receiver && !send_node.parenthesized?
190
+ end
191
+
192
+ def offense_range(node)
193
+ range = node.source_range
194
+ selector = node.send_node.loc.selector
195
+ return range if same_line?(range, selector)
196
+
197
+ line = selector.source_buffer.line_range(selector.line)
198
+
199
+ range.with(begin_pos: line.begin_pos + line.source.index(/\S/))
200
+ end
146
201
  end
147
202
  end
148
203
  end