rubocop 1.92.2 → 1.92.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/#directive_comment.rb# +224 -0
  3. data/lib/rubocop/cache_config.rb +3 -0
  4. data/lib/rubocop/cli.rb +5 -4
  5. data/lib/rubocop/config.rb +16 -0
  6. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  7. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  8. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  9. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  10. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  11. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  12. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  13. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  14. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  15. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +14 -6
  16. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  17. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  18. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  19. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  20. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  21. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  22. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  23. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  24. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  25. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  26. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  27. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  28. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  29. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  30. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  31. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  32. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  33. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  34. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  35. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  36. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  37. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  38. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  39. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  40. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  41. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  42. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  43. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  44. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  45. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  46. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  47. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  48. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  49. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  50. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  51. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  52. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  53. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  54. data/lib/rubocop/cop/lint/void.rb +20 -9
  55. data/lib/rubocop/cop/message_annotator.rb +6 -8
  56. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  57. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  58. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  59. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  60. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  61. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  62. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  63. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  64. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  65. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  66. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  67. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  68. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  69. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  70. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  71. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  72. data/lib/rubocop/cop/style/and_or.rb +8 -1
  73. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  74. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  75. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  76. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  77. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  78. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  79. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  80. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  81. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  82. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  83. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  84. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  85. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  86. data/lib/rubocop/cop/style/for.rb +59 -4
  87. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  88. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  89. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  90. data/lib/rubocop/cop/style/hash_lookup_method.rb +11 -2
  91. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  92. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  93. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  94. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  95. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  96. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  97. data/lib/rubocop/cop/style/lambda.rb +13 -5
  98. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  99. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  100. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  101. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  102. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  103. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  104. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  105. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  106. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  107. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  108. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  109. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  110. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  111. data/lib/rubocop/cop/style/redundant_begin.rb +96 -16
  112. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  113. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  114. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  115. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  116. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  117. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  118. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  119. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  120. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  121. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  122. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  123. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  124. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  125. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  126. data/lib/rubocop/cop/style/word_array.rb +1 -3
  127. data/lib/rubocop/cop/util.rb +2 -0
  128. data/lib/rubocop/cop/variable_force.rb +14 -1
  129. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  130. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  131. data/lib/rubocop/result_cache.rb +9 -3
  132. data/lib/rubocop/runner.rb +5 -0
  133. data/lib/rubocop/server/cache.rb +24 -5
  134. data/lib/rubocop/todo_audit.rb +61 -15
  135. data/lib/rubocop/version.rb +1 -1
  136. metadata +4 -3
@@ -199,7 +199,7 @@ module RuboCop
199
199
  return unless base_receiver.hash_type?
200
200
 
201
201
  first_call = first_call_has_a_dot(node)
202
- first_call.loc.dot.join(first_call.loc.selector)
202
+ dot_with_method_name(first_call)
203
203
  end
204
204
 
205
205
  def first_dot_alignment_base(node, rhs)
@@ -215,7 +215,7 @@ module RuboCop
215
215
 
216
216
  return unless same_line?(dot, first_call.receiver.source_range)
217
217
 
218
- dot.join(first_call.loc.selector)
218
+ dot_with_method_name(first_call)
219
219
  end
220
220
 
221
221
  def after_multiline_block_base(first_call, node)
@@ -224,7 +224,7 @@ module RuboCop
224
224
  after_block = first_call.block_node.parent
225
225
  return unless after_block&.call_type? && after_block.loc?(:dot) && after_block != node
226
226
 
227
- after_block.loc.dot.join(after_block.loc.selector)
227
+ dot_with_method_name(after_block)
228
228
  end
229
229
 
230
230
  def inside_multiline_chain_arg?(node)
@@ -381,13 +381,17 @@ module RuboCop
381
381
  while receiver_chain&.call_type?
382
382
  base_receiver = unwrap_block_node(receiver_chain.receiver)
383
383
  if alignment_base_for_chained_receiver?(receiver_chain, base_receiver)
384
- return receiver_chain.loc.dot.join(receiver_chain.loc.selector)
384
+ return dot_with_method_name(receiver_chain)
385
385
  end
386
386
 
387
387
  receiver_chain = base_receiver
388
388
  end
389
389
  end
390
390
 
391
+ def dot_with_method_name(node)
392
+ node.loc.dot.join(node.loc.selector || node.loc.begin)
393
+ end
394
+
391
395
  def alignment_base_for_chained_receiver?(receiver_chain, base_receiver)
392
396
  base_receiver&.hash_type? ||
393
397
  method_on_receiver_last_line?(receiver_chain, base_receiver, :begin)
@@ -119,7 +119,7 @@ module RuboCop
119
119
  'on the same line as the last parameter.'
120
120
 
121
121
  def self.autocorrect_incompatible_with
122
- [Style::MultilineMethodSignature]
122
+ [Style::MethodDefParentheses, Style::MultilineMethodSignature]
123
123
  end
124
124
 
125
125
  def on_def(node)
@@ -28,7 +28,17 @@ module RuboCop
28
28
  # These cops rewrite a method chained to the block in the same pass,
29
29
  # which must not start a line of its own.
30
30
  def self.autocorrect_incompatible_with
31
- [Style::FloatDivision, Style::MapToHash, Style::MapToSet, Style::NilComparison, Style::RedundantSort]
31
+ [
32
+ Style::CollectionQuerying,
33
+ Style::FloatDivision,
34
+ Style::HashTransformKeys,
35
+ Style::HashTransformValues,
36
+ Style::MapToHash,
37
+ Style::MapToSet,
38
+ Style::NilComparison,
39
+ Style::RedundantFilterChain,
40
+ Style::RedundantSort
41
+ ]
32
42
  end
33
43
 
34
44
  def on_send(node)
@@ -36,6 +36,8 @@ module RuboCop
36
36
  # # The `compact` style normally requires a space inside
37
37
  # # array brackets, with the exception that successive left
38
38
  # # or right brackets are collapsed together in nested arrays.
39
+ # # A right bracket on its own line is left there when
40
+ # # `Layout/MultilineArrayBraceLayout` enforces the `new_line` style.
39
41
  #
40
42
  # # bad
41
43
  # array = [a, b, c, d]
@@ -216,11 +218,20 @@ module RuboCop
216
218
 
217
219
  # A closing bracket on its own line is only collapsed when the opening
218
220
  # bracket ends up on the first element's line, so that the brackets
219
- # stay symmetrical (see `Layout/MultilineArrayBraceLayout`).
221
+ # stay symmetrical (see `Layout/MultilineArrayBraceLayout`). It is never
222
+ # collapsed when that cop requires it to stay on its own line.
220
223
  def closing_bracket_kept_apart?(node, tokens, left, right)
221
- end_has_own_line?(right) &&
222
- !multi_dimensional_array?(tokens, left, side: :left) &&
223
- !same_line?(left, node.children.first)
224
+ return false unless end_has_own_line?(right)
225
+ return true if node.array_type? && closing_bracket_on_new_line_enforced?
226
+ return false if multi_dimensional_array?(tokens, left, side: :left)
227
+
228
+ !same_line?(left, node.children.first)
229
+ end
230
+
231
+ def closing_bracket_on_new_line_enforced?
232
+ return false unless config.cop_enabled?('Layout/MultilineArrayBraceLayout')
233
+
234
+ config.for_cop('Layout/MultilineArrayBraceLayout')['EnforcedStyle'] == 'new_line'
224
235
  end
225
236
 
226
237
  def compact_corrections(corrector, node, tokens, left, right)
@@ -89,6 +89,10 @@ module RuboCop
89
89
  group_by filter_map
90
90
  ].to_set.freeze
91
91
 
92
+ def self.autocorrect_incompatible_with
93
+ [Style::AndOr]
94
+ end
95
+
92
96
  def on_send(node)
93
97
  return unless node.arguments?
94
98
 
@@ -44,6 +44,10 @@ module RuboCop
44
44
  RESTRICT_ON_SEND = PRECEDENCE.flatten.freeze
45
45
  MSG = 'Wrap expressions with varying precedence with parentheses to avoid ambiguity.'
46
46
 
47
+ def self.autocorrect_incompatible_with
48
+ [Style::LineEndConcatenation]
49
+ end
50
+
47
51
  def on_and(node)
48
52
  return unless (parent = node.parent)
49
53
 
@@ -54,7 +54,7 @@ module RuboCop
54
54
  def find_offense_node(node, regexp_receiver)
55
55
  return node if first_argument_is_regexp?(node) || !node.parent
56
56
 
57
- if (node.parent.send_type? && node.receiver) ||
57
+ if (node.parent.call_type? && node.receiver) ||
58
58
  method_chain_to_regexp_receiver?(node, regexp_receiver) ||
59
59
  part_of_hash_argument?(node)
60
60
  node = find_offense_node(node.parent, regexp_receiver)
@@ -64,7 +64,7 @@ module RuboCop
64
64
  end
65
65
 
66
66
  def first_argument_is_regexp?(node)
67
- node.send_type? && node.first_argument&.regexp_type?
67
+ node.call_type? && node.first_argument&.regexp_type?
68
68
  end
69
69
 
70
70
  def method_chain_to_regexp_receiver?(node, regexp_receiver)
@@ -72,6 +72,8 @@ module RuboCop
72
72
 
73
73
  if node.method?(:freeze)
74
74
  corrector.replace(node, 'ENV')
75
+ elsif node.method?(:attr)
76
+ correct_attr(corrector, node)
75
77
  else
76
78
  corrector.replace(offense_range, prefer)
77
79
  end
@@ -92,10 +94,7 @@ module RuboCop
92
94
 
93
95
  def preferred_method(node)
94
96
  if node.method?(:attr)
95
- boolean_argument = node.arguments[1].source
96
- preferred_attr_method = boolean_argument == 'true' ? 'attr_accessor' : 'attr_reader'
97
-
98
- "#{preferred_attr_method} #{node.first_argument.source}"
97
+ "#{preferred_attr_method(node)} #{node.first_argument.source}"
99
98
  elsif dir_env_file_const?(node.receiver)
100
99
  prefer = PREFERRED_METHODS[node.method_name]
101
100
 
@@ -105,6 +104,17 @@ module RuboCop
105
104
  end
106
105
  end
107
106
 
107
+ def preferred_attr_method(node)
108
+ node.arguments[1].true_type? ? 'attr_accessor' : 'attr_reader'
109
+ end
110
+
111
+ def correct_attr(corrector, node)
112
+ corrector.replace(node.loc.selector, preferred_attr_method(node))
113
+ first_argument, boolean_argument = node.arguments
114
+
115
+ corrector.remove(first_argument.source_range.end.join(boolean_argument.source_range.end))
116
+ end
117
+
108
118
  def socket_const?(node)
109
119
  node&.short_name == :Socket
110
120
  end
@@ -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
@@ -65,7 +65,9 @@ module RuboCop
65
65
 
66
66
  def valid_syntax?(node)
67
67
  processed_source = parse(interpolated_source(node))
68
- processed_source.valid_syntax? && processed_source.ast.dstr_type?
68
+ return false unless processed_source.valid_syntax? && processed_source.ast.dstr_type?
69
+
70
+ !yield_outside_method?(processed_source.ast, node)
69
71
  end
70
72
 
71
73
  def interpolated_source(node)
@@ -80,6 +82,10 @@ module RuboCop
80
82
  def continued_string_part?(node)
81
83
  node.parent&.dstr_type? && node.left_sibling
82
84
  end
85
+
86
+ def yield_outside_method?(interpolated_node, node)
87
+ interpolated_node.each_node(:yield).any? && node.each_ancestor(:any_def).none?
88
+ end
83
89
  end
84
90
  end
85
91
  end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  # while true
36
36
  # break if condition
37
37
  # end
38
- class LiteralAsCondition < Base
38
+ class LiteralAsCondition < Base # rubocop:disable Metrics/ClassLength
39
39
  include RangeHelp
40
40
  extend AutoCorrector
41
41
 
@@ -50,7 +50,7 @@ module RuboCop
50
50
  # the leftmost node can lead to a void value expression syntax error.
51
51
  next if void_value_expression?(node.rhs)
52
52
 
53
- corrector.replace(node, node.rhs.source)
53
+ replace_node(corrector, node, node.rhs.source)
54
54
  end
55
55
  end
56
56
 
@@ -62,7 +62,7 @@ module RuboCop
62
62
  # the leftmost node can lead to a void value expression syntax error.
63
63
  next if void_value_expression?(node.rhs)
64
64
 
65
- corrector.replace(node, node.rhs.source)
65
+ replace_node(corrector, node, node.rhs.source)
66
66
  end
67
67
  end
68
68
 
@@ -79,11 +79,11 @@ module RuboCop
79
79
 
80
80
  if node.condition.truthy_literal?
81
81
  add_offense(node.condition) do |corrector|
82
- corrector.replace(node.condition, 'true')
82
+ replace_node(corrector, node.condition, 'true')
83
83
  end
84
84
  elsif node.condition.falsey_literal?
85
85
  add_offense(node.condition) do |corrector|
86
- corrector.remove(node)
86
+ replace_node(corrector, node, removable?(node) ? '' : 'nil')
87
87
  end
88
88
  end
89
89
  end
@@ -94,13 +94,13 @@ module RuboCop
94
94
 
95
95
  if node.condition.truthy_literal?
96
96
  add_offense(node.condition) do |corrector|
97
- corrector.replace(node, node.source.sub(node.condition.source, 'true'))
97
+ replace_node(corrector, node, node.source.sub(node.condition.source, 'true'))
98
98
  end
99
99
  elsif node.condition.falsey_literal?
100
100
  add_offense(node.condition) do |corrector|
101
101
  next if orphans_loop_control_keyword?(node)
102
102
 
103
- corrector.replace(node, node.body.child_nodes.map(&:source).join("\n"))
103
+ replace_node(corrector, node, node.body.child_nodes.map(&:source).join("\n"))
104
104
  end
105
105
  end
106
106
  end
@@ -110,11 +110,11 @@ module RuboCop
110
110
 
111
111
  if node.condition.falsey_literal?
112
112
  add_offense(node.condition) do |corrector|
113
- corrector.replace(node.condition, 'false')
113
+ replace_node(corrector, node.condition, 'false')
114
114
  end
115
115
  elsif node.condition.truthy_literal?
116
116
  add_offense(node.condition) do |corrector|
117
- corrector.remove(node)
117
+ replace_node(corrector, node, removable?(node) ? '' : 'nil')
118
118
  end
119
119
  end
120
120
  end
@@ -125,13 +125,13 @@ module RuboCop
125
125
 
126
126
  if node.condition.falsey_literal?
127
127
  add_offense(node.condition) do |corrector|
128
- corrector.replace(node, node.source.sub(node.condition.source, 'false'))
128
+ replace_node(corrector, node, node.source.sub(node.condition.source, 'false'))
129
129
  end
130
130
  elsif node.condition.truthy_literal?
131
131
  add_offense(node.condition) do |corrector|
132
132
  next if orphans_loop_control_keyword?(node)
133
133
 
134
- corrector.replace(node, node.body.child_nodes.map(&:source).join("\n"))
134
+ replace_node(corrector, node, node.body.child_nodes.map(&:source).join("\n"))
135
135
  end
136
136
  end
137
137
  end
@@ -286,19 +286,30 @@ module RuboCop
286
286
  "#{node.else_branch.source.sub('elsif', 'if')}\nend"
287
287
  elsif node.else? || node.ternary?
288
288
  node.else_branch.source
289
- elsif node.value_used?
290
- 'nil'
291
- else
289
+ elsif removable?(node)
292
290
  '' # Equivalent to removing the node
291
+ else
292
+ 'nil'
293
293
  end
294
294
 
295
295
  add_offense(cond) do |corrector|
296
- next if part_of_ignored_node?(node)
296
+ replace_node(corrector, node, new_node)
297
+ end
298
+ end
297
299
 
298
- corrector.replace(node, new_node)
300
+ def removable?(node)
301
+ return false if node.value_used?
302
+ return true unless (parent = node.parent)
303
+ return true if parent.type?(:begin, :kwbegin, :when)
299
304
 
300
- ignore_node(node)
301
- end
305
+ parent.type?(:if, :while, :until) && parent.loc?(:end)
306
+ end
307
+
308
+ def replace_node(corrector, node, replacement)
309
+ return if node.each_ancestor.any? { |ancestor| ignored_node?(ancestor) }
310
+
311
+ corrector.replace(node, replacement)
312
+ ignore_node(node)
302
313
  end
303
314
  end
304
315
  end
@@ -24,6 +24,10 @@ module RuboCop
24
24
  MSG = 'Literal interpolation detected.'
25
25
  COMPOSITE = %i[array hash pair irange erange].freeze
26
26
 
27
+ def self.autocorrect_incompatible_with
28
+ [Style::PercentLiteralDelimiters, Style::RegexpLiteral]
29
+ end
30
+
27
31
  def on_interpolation(begin_node)
28
32
  final_node = begin_node.children.last
29
33
  return unless offending?(final_node)
@@ -71,6 +75,7 @@ module RuboCop
71
75
  # interpolation should not be removed if the expanded value
72
76
  # contains a space character.
73
77
  return if in_array_percent_literal?(begin_node) && /\s|\A\z/.match?(value)
78
+ return if breaks_delimiters?(begin_node, value)
74
79
 
75
80
  value
76
81
  end
@@ -151,9 +156,9 @@ module RuboCop
151
156
  end
152
157
 
153
158
  def autocorrected_value_for_array(node)
154
- return node.source.gsub('"', '\"') unless node.percent_literal?
159
+ return escape_string_content(node.source) unless node.percent_literal?
155
160
 
156
- contents_range(node).source.split.to_s.gsub('"', '\"')
161
+ escape_string_content(node.values.map { |value| value.value.to_s }.to_s)
157
162
  end
158
163
 
159
164
  def autocorrected_value_for_hash(node)
@@ -188,8 +193,15 @@ module RuboCop
188
193
 
189
194
  # Does node print its own source when converted to a string?
190
195
  def prints_as_self?(node)
191
- node.basic_literal? ||
192
- (COMPOSITE.include?(node.type) && node.children.all? { |child| prints_as_self?(child) })
196
+ return true if node.basic_literal?
197
+ return false unless COMPOSITE.include?(node.type)
198
+ return numeric_open_range?(node) if node.children.any?(&:nil?)
199
+
200
+ node.children.all? { |child| prints_as_self?(child) }
201
+ end
202
+
203
+ def numeric_open_range?(node)
204
+ node.children.compact.all? { |child| child.type?(:int, :float) }
193
205
  end
194
206
 
195
207
  def space_literal?(node)
@@ -215,6 +227,40 @@ module RuboCop
215
227
  grandparent&.array_type? && grandparent.percent_literal?
216
228
  end
217
229
 
230
+ def breaks_delimiters?(begin_node, value)
231
+ return false unless (literal = delimited_literal(begin_node))
232
+
233
+ opening = literal.loc.begin.source[-1]
234
+ closing = literal.loc.end.source[0]
235
+ return !balanced?(value, opening, closing) if opening != closing
236
+
237
+ !escaped_delimiter?(literal, closing) && value.include?(closing)
238
+ end
239
+
240
+ def delimited_literal(begin_node)
241
+ parent = begin_node.parent
242
+ literal = in_array_percent_literal?(begin_node) ? parent.parent : parent
243
+
244
+ literal if literal.loc?(:begin) && literal.loc?(:end)
245
+ end
246
+
247
+ def balanced?(value, opening, closing)
248
+ depth = 0
249
+ value.each_char do |char|
250
+ depth += 1 if char == opening
251
+ depth -= 1 if char == closing
252
+ return false if depth.negative?
253
+ end
254
+
255
+ depth.zero?
256
+ end
257
+
258
+ def escaped_delimiter?(literal, delimiter)
259
+ return true if delimiter == '"'
260
+
261
+ delimiter == '/' && literal.regexp_type? && literal.slash_literal?
262
+ end
263
+
218
264
  def escape_trailing_hash_sign(begin_node, value)
219
265
  following = processed_source.buffer.source[begin_node.source_range.end_pos]
220
266
  return value unless %w[{ @ $].include?(following) && unescaped_hash_sign?(value)
@@ -42,6 +42,7 @@ module RuboCop
42
42
  # FileUtils.rm_f(path)
43
43
  #
44
44
  class NonAtomicFileOperation < Base
45
+ include RangeHelp
45
46
  extend AutoCorrector
46
47
 
47
48
  MSG_REMOVE_FILE_EXIST_CHECK = 'Remove unnecessary existence check ' \
@@ -77,6 +78,10 @@ module RuboCop
77
78
  (pair (sym :force) (:false))
78
79
  PATTERN
79
80
 
81
+ def self.autocorrect_incompatible_with
82
+ [Style::MethodCallWithArgsParentheses]
83
+ end
84
+
80
85
  def on_send(node)
81
86
  return unless node.receiver&.const_type?
82
87
  return unless if_node_child?(node)
@@ -106,7 +111,7 @@ module RuboCop
106
111
  range = parent.loc.keyword.begin.join(parent.condition.source_range.end)
107
112
 
108
113
  add_offense(range, message: message_remove_file_exist_check(exist_node)) do |corrector|
109
- autocorrect(corrector, node, range) unless parent.elsif?
114
+ autocorrect(corrector, node, range) if autocorrectable?(node)
110
115
  end
111
116
  end
112
117
 
@@ -120,6 +125,34 @@ module RuboCop
120
125
  format(MSG_REMOVE_FILE_EXIST_CHECK, receiver: receiver, method_name: method_name)
121
126
  end
122
127
 
128
+ def autocorrectable?(node)
129
+ parent = node.parent
130
+ return false if parent.elsif?
131
+ return true if parent.modifier_form?
132
+ return true unless code_after_end?(parent)
133
+ return false if !node.parenthesized? || last_heredoc_end_pos(node)
134
+
135
+ !processed_source.contains_comment?(body_to_end_range(node))
136
+ end
137
+
138
+ def code_after_end?(if_node)
139
+ end_keyword = if_node.loc.end
140
+
141
+ !end_keyword.source_line[end_keyword.last_column..].match?(/\A\s*(?:#|\z)/)
142
+ end
143
+
144
+ def body_to_end_range(node)
145
+ begin_pos = [node.source_range.end_pos, last_heredoc_end_pos(node)].compact.max
146
+
147
+ range_between(begin_pos, node.parent.loc.end.end_pos)
148
+ end
149
+
150
+ def last_heredoc_end_pos(node)
151
+ node.each_descendant(:any_str).select(&:heredoc?).map do |heredoc|
152
+ heredoc.loc.heredoc_end.end_pos
153
+ end.max
154
+ end
155
+
123
156
  def autocorrect(corrector, node, range)
124
157
  corrector.remove(range)
125
158
  autocorrect_replace_method(corrector, node)
@@ -127,10 +160,17 @@ module RuboCop
127
160
  if node.parent.modifier_form?
128
161
  corrector.remove(node.source_range.end.join(node.parent.loc.keyword.begin))
129
162
  else
130
- corrector.remove(node.parent.loc.end)
163
+ corrector.remove(end_keyword_range(node))
131
164
  end
132
165
  end
133
166
 
167
+ def end_keyword_range(node)
168
+ range = body_to_end_range(node)
169
+ return node.parent.loc.end if processed_source.contains_comment?(range)
170
+
171
+ range
172
+ end
173
+
134
174
  def autocorrect_replace_method(corrector, node)
135
175
  return if force_method?(node)
136
176
 
@@ -88,6 +88,10 @@ module RuboCop
88
88
  CONVERSION_METHODS = %i[Integer Float Complex Rational to_i to_f to_c to_r].freeze
89
89
  METHODS = CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')
90
90
 
91
+ def self.autocorrect_incompatible_with
92
+ [Style::MethodCallWithArgsParentheses, Style::NestedParenthesizedCalls]
93
+ end
94
+
91
95
  # @!method to_method(node)
92
96
  def_node_matcher :to_method, <<~PATTERN
93
97
  (call $_ ${#{METHODS}})