rubocop 1.56.4 → 1.60.1

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 (163) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +57 -3
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
  6. data/lib/rubocop/cli.rb +1 -1
  7. data/lib/rubocop/config.rb +0 -2
  8. data/lib/rubocop/config_loader.rb +0 -1
  9. data/lib/rubocop/config_obsoletion.rb +11 -8
  10. data/lib/rubocop/config_validator.rb +0 -2
  11. data/lib/rubocop/cop/base.rb +6 -0
  12. data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
  13. data/lib/rubocop/cop/exclude_limit.rb +1 -1
  14. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
  15. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
  16. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
  17. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
  18. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  19. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  20. data/lib/rubocop/cop/layout/end_alignment.rb +12 -2
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
  22. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +22 -7
  23. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  25. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  26. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  27. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
  28. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +16 -1
  29. data/lib/rubocop/cop/layout/redundant_line_break.rb +2 -1
  30. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
  31. data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
  32. data/lib/rubocop/cop/layout/space_around_operators.rb +50 -20
  33. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  34. data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -4
  35. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
  36. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  37. data/lib/rubocop/cop/lint/debugger.rb +11 -1
  38. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
  39. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  40. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -3
  41. data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
  42. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
  43. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
  44. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
  45. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  46. data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
  47. data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
  48. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +0 -1
  49. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
  50. data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
  51. data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -0
  52. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +63 -4
  53. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  54. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  55. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -4
  56. data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
  57. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  58. data/lib/rubocop/cop/lint/symbol_conversion.rb +7 -2
  59. data/lib/rubocop/cop/lint/syntax.rb +6 -3
  60. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  61. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
  62. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  63. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  64. data/lib/rubocop/cop/lint/void.rb +43 -12
  65. data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
  66. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  67. data/lib/rubocop/cop/metrics/class_length.rb +8 -3
  68. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  69. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  70. data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
  71. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  72. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  73. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  74. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  75. data/lib/rubocop/cop/naming/block_forwarding.rb +12 -4
  76. data/lib/rubocop/cop/naming/constant_name.rb +1 -2
  77. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  78. data/lib/rubocop/cop/security/open.rb +2 -2
  79. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -2
  80. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  81. data/lib/rubocop/cop/style/arguments_forwarding.rb +120 -17
  82. data/lib/rubocop/cop/style/array_first_last.rb +64 -0
  83. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
  84. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
  85. data/lib/rubocop/cop/style/case_like_if.rb +4 -4
  86. data/lib/rubocop/cop/style/class_check.rb +1 -0
  87. data/lib/rubocop/cop/style/class_equality_comparison.rb +5 -0
  88. data/lib/rubocop/cop/style/collection_compact.rb +18 -8
  89. data/lib/rubocop/cop/style/combinable_loops.rb +13 -7
  90. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -0
  91. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  92. data/lib/rubocop/cop/style/date_time.rb +5 -4
  93. data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
  94. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  95. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  96. data/lib/rubocop/cop/style/eval_with_location.rb +3 -14
  97. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
  98. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  99. data/lib/rubocop/cop/style/format_string.rb +24 -3
  100. data/lib/rubocop/cop/style/guard_clause.rb +26 -0
  101. data/lib/rubocop/cop/style/hash_each_methods.rb +83 -10
  102. data/lib/rubocop/cop/style/hash_except.rb +2 -1
  103. data/lib/rubocop/cop/style/identical_conditional_branches.rb +28 -3
  104. data/lib/rubocop/cop/style/inverse_methods.rb +6 -5
  105. data/lib/rubocop/cop/style/invertible_unless_condition.rb +39 -2
  106. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +3 -2
  107. data/lib/rubocop/cop/style/map_to_hash.rb +17 -7
  108. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +14 -5
  109. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
  110. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
  111. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  112. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
  113. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  114. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -3
  115. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  116. data/lib/rubocop/cop/style/next.rb +1 -1
  117. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  118. data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
  119. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  120. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
  121. data/lib/rubocop/cop/style/redundant_argument.rb +3 -2
  122. data/lib/rubocop/cop/style/redundant_begin.rb +9 -1
  123. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
  124. data/lib/rubocop/cop/style/redundant_each.rb +7 -4
  125. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  126. data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
  127. data/lib/rubocop/cop/style/redundant_filter_chain.rb +22 -5
  128. data/lib/rubocop/cop/style/redundant_line_continuation.rb +10 -1
  129. data/lib/rubocop/cop/style/redundant_parentheses.rb +68 -21
  130. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  131. data/lib/rubocop/cop/style/redundant_self.rb +17 -2
  132. data/lib/rubocop/cop/style/redundant_sort.rb +9 -8
  133. data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
  134. data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
  135. data/lib/rubocop/cop/style/sample.rb +2 -1
  136. data/lib/rubocop/cop/style/select_by_regexp.rb +7 -6
  137. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  138. data/lib/rubocop/cop/style/semicolon.rb +8 -0
  139. data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
  140. data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
  141. data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
  142. data/lib/rubocop/cop/style/string_chars.rb +1 -0
  143. data/lib/rubocop/cop/style/strip.rb +7 -4
  144. data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
  145. data/lib/rubocop/cop/style/symbol_proc.rb +36 -0
  146. data/lib/rubocop/cop/style/unpack_first.rb +11 -14
  147. data/lib/rubocop/cops_documentation_generator.rb +11 -1
  148. data/lib/rubocop/ext/regexp_node.rb +9 -4
  149. data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
  150. data/lib/rubocop/formatter/html_formatter.rb +5 -4
  151. data/lib/rubocop/formatter/json_formatter.rb +0 -1
  152. data/lib/rubocop/formatter.rb +1 -1
  153. data/lib/rubocop/lsp/routes.rb +1 -1
  154. data/lib/rubocop/options.rb +0 -8
  155. data/lib/rubocop/result_cache.rb +0 -1
  156. data/lib/rubocop/rspec/shared_contexts.rb +6 -0
  157. data/lib/rubocop/rspec/support.rb +1 -0
  158. data/lib/rubocop/runner.rb +1 -1
  159. data/lib/rubocop/server/cache.rb +1 -1
  160. data/lib/rubocop/version.rb +1 -1
  161. data/lib/rubocop.rb +5 -0
  162. metadata +16 -24
  163. /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
@@ -60,17 +60,17 @@ module RuboCop
60
60
  # @!method inverse_candidate?(node)
61
61
  def_node_matcher :inverse_candidate?, <<~PATTERN
62
62
  {
63
- (send $(send $(...) $_ $...) :!)
64
- (send ({block numblock} $(send $(...) $_) $...) :!)
65
- (send (begin $(send $(...) $_ $...)) :!)
63
+ (send $(call $(...) $_ $...) :!)
64
+ (send ({block numblock} $(call $(...) $_) $...) :!)
65
+ (send (begin $(call $(...) $_ $...)) :!)
66
66
  }
67
67
  PATTERN
68
68
 
69
69
  # @!method inverse_block?(node)
70
70
  def_node_matcher :inverse_block?, <<~PATTERN
71
- ({block numblock} $(send (...) $_) ... { $(send ... :!)
71
+ ({block numblock} $(call (...) $_) ... { $(call ... :!)
72
72
  $(send (...) {:!= :!~} ...)
73
- (begin ... $(send ... :!))
73
+ (begin ... $(call ... :!))
74
74
  (begin ... $(send (...) {:!= :!~} ...))
75
75
  })
76
76
  PATTERN
@@ -87,6 +87,7 @@ module RuboCop
87
87
  end
88
88
  end
89
89
  end
90
+ alias on_csend on_send
90
91
 
91
92
  def on_block(node)
92
93
  inverse_block?(node) do |_method_call, method, block|
@@ -51,7 +51,7 @@ module RuboCop
51
51
  class InvertibleUnlessCondition < Base
52
52
  extend AutoCorrector
53
53
 
54
- MSG = 'Favor `if` with inverted condition over `unless`.'
54
+ MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
55
55
 
56
56
  def on_if(node)
57
57
  return unless node.unless?
@@ -59,7 +59,10 @@ module RuboCop
59
59
  condition = node.condition
60
60
  return unless invertible?(condition)
61
61
 
62
- add_offense(node) do |corrector|
62
+ message = format(MSG, prefer: "#{node.inverse_keyword} #{preferred_condition(condition)}",
63
+ current: "#{node.keyword} #{condition.source}")
64
+
65
+ add_offense(node, message: message) do |corrector|
63
66
  corrector.replace(node.loc.keyword, node.inverse_keyword)
64
67
  autocorrect(corrector, condition)
65
68
  end
@@ -88,6 +91,40 @@ module RuboCop
88
91
  (argument.const_type? && argument.short_name.to_s.upcase != argument.short_name.to_s)
89
92
  end
90
93
 
94
+ def preferred_condition(node)
95
+ case node.type
96
+ when :begin then "(#{preferred_condition(node.children.first)})"
97
+ when :send then preferred_send_condition(node)
98
+ when :or, :and then preferred_logical_condition(node)
99
+ end
100
+ end
101
+
102
+ def preferred_send_condition(node)
103
+ receiver_source = node.receiver.source
104
+ return receiver_source if node.method?(:!)
105
+
106
+ inverse_method_name = inverse_methods[node.method_name]
107
+ return "#{receiver_source}.#{inverse_method_name}" unless node.arguments?
108
+
109
+ argument_list = node.arguments.map(&:source).join(', ')
110
+ if node.operator_method?
111
+ return "#{receiver_source} #{inverse_method_name} #{argument_list}"
112
+ end
113
+
114
+ if node.parenthesized?
115
+ return "#{receiver_source}.#{inverse_method_name}(#{argument_list})"
116
+ end
117
+
118
+ "#{receiver_source}.#{inverse_method_name} #{argument_list}"
119
+ end
120
+
121
+ def preferred_logical_condition(node)
122
+ preferred_lhs = preferred_condition(node.lhs)
123
+ preferred_rhs = preferred_condition(node.rhs)
124
+
125
+ "#{preferred_lhs} #{node.inverse_operator} #{preferred_rhs}"
126
+ end
127
+
91
128
  def autocorrect(corrector, node)
92
129
  case node.type
93
130
  when :begin
@@ -44,9 +44,9 @@ module RuboCop
44
44
 
45
45
  # @!method map_and_compact?(node)
46
46
  def_node_matcher :map_and_compact?, <<~RUBY
47
- (send
47
+ (call
48
48
  (block
49
- (send _ :map)
49
+ (call _ :map)
50
50
  (args
51
51
  $(arg _))
52
52
  {
@@ -85,6 +85,7 @@ module RuboCop
85
85
  end
86
86
  end
87
87
  end
88
+ alias on_csend on_send
88
89
 
89
90
  private
90
91
 
@@ -34,21 +34,25 @@ module RuboCop
34
34
 
35
35
  minimum_target_ruby_version 2.6
36
36
 
37
- MSG = 'Pass a block to `to_h` instead of calling `%<method>s.to_h`.'
37
+ MSG = 'Pass a block to `to_h` instead of calling `%<method>s%<dot>sto_h`.'
38
38
  RESTRICT_ON_SEND = %i[to_h].freeze
39
39
 
40
- # @!method map_to_h?(node)
41
- def_node_matcher :map_to_h?, <<~PATTERN
40
+ # @!method map_to_h(node)
41
+ def_node_matcher :map_to_h, <<~PATTERN
42
42
  {
43
- $(send ({block numblock} $(send _ {:map :collect}) ...) :to_h)
44
- $(send $(send _ {:map :collect} (block_pass sym)) :to_h)
43
+ $(call ({block numblock} $(call _ {:map :collect}) ...) :to_h)
44
+ $(call $(call _ {:map :collect} (block_pass sym)) :to_h)
45
45
  }
46
46
  PATTERN
47
47
 
48
+ def self.autocorrect_incompatible_with
49
+ [Layout::SingleLineBlockChain]
50
+ end
51
+
48
52
  def on_send(node)
49
- return unless (to_h_node, map_node = map_to_h?(node))
53
+ return unless (to_h_node, map_node = map_to_h(node))
50
54
 
51
- message = format(MSG, method: map_node.loc.selector.source)
55
+ message = format(MSG, method: map_node.loc.selector.source, dot: to_h_node.loc.dot.source)
52
56
  add_offense(map_node.loc.selector, message: message) do |corrector|
53
57
  # If the `to_h` call already has a block, do not autocorrect.
54
58
  next if to_h_node.block_node
@@ -56,15 +60,21 @@ module RuboCop
56
60
  autocorrect(corrector, to_h_node, map_node)
57
61
  end
58
62
  end
63
+ alias on_csend on_send
59
64
 
60
65
  private
61
66
 
67
+ # rubocop:disable Metrics/AbcSize
62
68
  def autocorrect(corrector, to_h, map)
63
69
  removal_range = range_between(to_h.loc.dot.begin_pos, to_h.loc.selector.end_pos)
64
70
 
65
71
  corrector.remove(range_with_surrounding_space(removal_range, side: :left))
72
+ if (map_dot = map.loc.dot)
73
+ corrector.replace(map_dot, to_h.loc.dot.source)
74
+ end
66
75
  corrector.replace(map.loc.selector, 'to_h')
67
76
  end
77
+ # rubocop:enable Metrics/AbcSize
68
78
  end
69
79
  end
70
80
  end
@@ -86,6 +86,7 @@ module RuboCop
86
86
 
87
87
  def legitimate_call_with_parentheses?(node) # rubocop:disable Metrics/PerceivedComplexity
88
88
  call_in_literals?(node) ||
89
+ node.parent&.when_type? ||
89
90
  call_with_ambiguous_arguments?(node) ||
90
91
  call_in_logical_operators?(node) ||
91
92
  call_in_optional_arguments?(node) ||
@@ -126,23 +127,31 @@ module RuboCop
126
127
 
127
128
  def call_with_ambiguous_arguments?(node) # rubocop:disable Metrics/PerceivedComplexity
128
129
  call_with_braced_block?(node) ||
130
+ call_in_argument_with_block?(node) ||
129
131
  call_as_argument_or_chain?(node) ||
130
132
  call_in_match_pattern?(node) ||
131
133
  hash_literal_in_arguments?(node) ||
132
134
  node.descendants.any? do |n|
133
- n.forwarded_args_type? || ambiguous_literal?(n) || logical_operator?(n) ||
134
- call_with_braced_block?(n)
135
+ n.forwarded_args_type? || n.block_type? ||
136
+ ambiguous_literal?(n) || logical_operator?(n)
135
137
  end
136
138
  end
137
139
 
138
140
  def call_with_braced_block?(node)
139
- (node.send_type? || node.super_type?) && node.block_node&.braces?
141
+ (node.call_type? || node.super_type?) && node.block_node&.braces?
142
+ end
143
+
144
+ def call_in_argument_with_block?(node)
145
+ parent = node.parent&.block_type? && node.parent&.parent
146
+ return false unless parent
147
+
148
+ parent.call_type? || parent.super_type? || parent.yield_type?
140
149
  end
141
150
 
142
151
  def call_as_argument_or_chain?(node)
143
152
  node.parent &&
144
- ((node.parent.send_type? && !assigned_before?(node.parent, node)) ||
145
- node.parent.csend_type? || node.parent.super_type? || node.parent.yield_type?)
153
+ (node.parent.call_type? || node.parent.super_type? || node.parent.yield_type?) &&
154
+ !assigned_before?(node.parent, node)
146
155
  end
147
156
 
148
157
  def call_in_match_pattern?(node)
@@ -218,15 +218,13 @@ module RuboCop
218
218
  send(style, node) # call require_parentheses or omit_parentheses
219
219
  end
220
220
  alias on_csend on_send
221
- alias on_super on_send
222
221
  alias on_yield on_send
223
222
 
224
223
  private
225
224
 
226
225
  def args_begin(node)
227
226
  loc = node.loc
228
- selector =
229
- node.super_type? || node.yield_type? ? loc.keyword : loc.selector
227
+ selector = node.yield_type? ? loc.keyword : loc.selector
230
228
 
231
229
  resize_by = args_parenthesized?(node) ? 2 : 1
232
230
  selector.end.resize(resize_by)
@@ -239,7 +237,7 @@ module RuboCop
239
237
  def args_parenthesized?(node)
240
238
  return false unless node.arguments.one?
241
239
 
242
- first_node = node.arguments.first
240
+ first_node = node.first_argument
243
241
  first_node.begin_type? && first_node.parenthesized_call?
244
242
  end
245
243
  end
@@ -8,6 +8,9 @@ module RuboCop
8
8
  # This cop can be customized allowed methods with `AllowedMethods`.
9
9
  # By default, there are no methods to allowed.
10
10
  #
11
+ # NOTE: This cop allows the use of `it()` without arguments in blocks,
12
+ # as in `0.times { it() }`, following `Lint/ItWithoutArgumentsInBlock` cop.
13
+ #
11
14
  # @example
12
15
  # # bad
13
16
  # object.some_method()
@@ -30,15 +33,18 @@ module RuboCop
30
33
 
31
34
  MSG = 'Do not use parentheses for method calls with no arguments.'
32
35
 
36
+ # rubocop:disable Metrics/CyclomaticComplexity
33
37
  def on_send(node)
34
38
  return unless !node.arguments? && node.parenthesized?
35
39
  return if ineligible_node?(node)
36
40
  return if default_argument?(node)
37
41
  return if allowed_method_name?(node.method_name)
38
42
  return if same_name_assignment?(node)
43
+ return if parenthesized_it_method_in_block?(node)
39
44
 
40
45
  register_offense(node)
41
46
  end
47
+ # rubocop:enable Metrics/CyclomaticComplexity
42
48
 
43
49
  private
44
50
 
@@ -71,6 +77,20 @@ module RuboCop
71
77
  end
72
78
  end
73
79
 
80
+ # Respects `Lint/ItWithoutArgumentsInBlock` cop and the following Ruby 3.3's warning:
81
+ #
82
+ # $ ruby -e '0.times { begin; it; end }'
83
+ # -e:1: warning: `it` calls without arguments will refer to the first block param in
84
+ # Ruby 3.4; use it() or self.it
85
+ #
86
+ def parenthesized_it_method_in_block?(node)
87
+ return false unless node.method?(:it)
88
+ return false unless (block_node = node.each_ancestor(:block).first)
89
+ return false unless block_node.arguments.empty_and_without_delimiters?
90
+
91
+ !node.receiver && node.arguments.empty? && !node.block_literal?
92
+ end
93
+
74
94
  def any_assignment?(node)
75
95
  node.each_ancestor(*AST::Node::ASSIGNMENTS).any? do |asgn_node|
76
96
  # `obj.method = value` parses as (send ... :method= ...), and will
@@ -170,7 +170,7 @@ module RuboCop
170
170
  return true if node.arguments.any? do |arg|
171
171
  arg.forward_arg_type? || arg.restarg_type? || arg.kwrestarg_type?
172
172
  end
173
- return false unless (last_argument = node.arguments.last)
173
+ return false unless (last_argument = node.last_argument)
174
174
 
175
175
  last_argument.blockarg_type? && last_argument.name.nil?
176
176
  end
@@ -7,12 +7,12 @@ module RuboCop
7
7
  # defining `respond_to_missing?`.
8
8
  #
9
9
  # @example
10
- # #bad
10
+ # # bad
11
11
  # def method_missing(name, *args)
12
12
  # # ...
13
13
  # end
14
14
  #
15
- # #good
15
+ # # good
16
16
  # def respond_to_missing?(name, include_private)
17
17
  # # ...
18
18
  # end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  MSG = 'Avoid multi-line chains of blocks.'
29
29
 
30
30
  def on_block(node)
31
- node.send_node.each_node(:send) do |send_node|
31
+ node.send_node.each_node(:send, :csend) do |send_node|
32
32
  receiver = send_node.receiver
33
33
 
34
34
  next unless (receiver&.block_type? || receiver&.numblock_type?) && receiver&.multiline?
@@ -54,12 +54,10 @@ module RuboCop
54
54
  private
55
55
 
56
56
  def offense?(node)
57
- node.ternary? && node.multiline?
57
+ node.ternary? && node.multiline? && node.source != replacement(node)
58
58
  end
59
59
 
60
60
  def autocorrect(corrector, node)
61
- return unless offense?(node)
62
-
63
61
  corrector.replace(node, replacement(node))
64
62
  return unless (parent = node.parent)
65
63
  return unless (comments_in_condition = comments_in_condition(node))
@@ -27,24 +27,16 @@ module RuboCop
27
27
 
28
28
  node.each_descendant(:if).select(&:ternary?).each do |nested_ternary|
29
29
  add_offense(nested_ternary) do |corrector|
30
- if_node = if_node(nested_ternary)
31
- next if part_of_ignored_node?(if_node)
30
+ next if part_of_ignored_node?(node)
32
31
 
33
- autocorrect(corrector, if_node)
34
- ignore_node(if_node)
32
+ autocorrect(corrector, node)
33
+ ignore_node(node)
35
34
  end
36
35
  end
37
36
  end
38
37
 
39
38
  private
40
39
 
41
- def if_node(node)
42
- node = node.parent
43
- return node if node.if_type?
44
-
45
- if_node(node)
46
- end
47
-
48
40
  def autocorrect(corrector, if_node)
49
41
  replace_loc_and_whitespace(corrector, if_node.loc.question, "\n")
50
42
  replace_loc_and_whitespace(corrector, if_node.loc.colon, "\nelse\n")
@@ -66,7 +66,7 @@ module RuboCop
66
66
  end
67
67
 
68
68
  def on_block(node)
69
- return unless node.send_node.send_type? && node.send_node.enumerator_method?
69
+ return unless node.send_node.call_type? && node.send_node.enumerator_method?
70
70
 
71
71
  check(node)
72
72
  end
@@ -62,7 +62,7 @@ module RuboCop
62
62
  private
63
63
 
64
64
  def message(node)
65
- self.class.const_get("#{literal_type(node).upcase}_MSG")
65
+ self.class.const_get(:"#{literal_type(node).upcase}_MSG")
66
66
  end
67
67
 
68
68
  def literal_type(node)
@@ -4,8 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Checks for redundant dot before operator method call.
7
- # The target operator methods are `|`, `^`, `&`, `<=>`, `==`, `===`, `=~`, `>`, `>=`, `<`,
8
- # `<=`, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `~`, `!`, `!=`, and `!~`.
7
+ # The target operator methods are `|`, `^`, `&`, ``<=>``, `==`, `===`, `=~`, `>`, `>=`, `<`,
8
+ # ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `~`, `!`, `!=`, and `!~`.
9
9
  #
10
10
  # @example
11
11
  #
@@ -142,8 +142,8 @@ module RuboCop
142
142
  @assignments = assignments
143
143
  end
144
144
 
145
- def tsort_each_node(&block)
146
- @assignments.each(&block)
145
+ def tsort_each_node(...)
146
+ @assignments.each(...)
147
147
  end
148
148
 
149
149
  def tsort_each_child(assignment)
@@ -81,6 +81,7 @@ module RuboCop
81
81
  cond = node.condition
82
82
 
83
83
  control_op_condition(cond) do |first_child, rest_children|
84
+ return if require_parentheses?(node, first_child)
84
85
  return if semicolon_separated_expressions?(first_child, rest_children)
85
86
  return if modifier_op?(first_child)
86
87
  return if parens_allowed?(cond)
@@ -92,6 +93,13 @@ module RuboCop
92
93
  end
93
94
  end
94
95
 
96
+ def require_parentheses?(node, condition_body)
97
+ return false if !node.while_type? && !node.until_type?
98
+ return false if !condition_body.block_type? && !condition_body.numblock_type?
99
+
100
+ condition_body.send_node.block_literal? && condition_body.keywords?
101
+ end
102
+
95
103
  def semicolon_separated_expressions?(first_exp, rest_exps)
96
104
  return false unless (second_exp = rest_exps.first)
97
105
 
@@ -67,12 +67,13 @@ module RuboCop
67
67
  return unless redundant_argument?(node)
68
68
 
69
69
  offense_range = argument_range(node)
70
- message = format(MSG, arg: node.arguments.first.source)
70
+ message = format(MSG, arg: node.first_argument.source)
71
71
 
72
72
  add_offense(offense_range, message: message) do |corrector|
73
73
  corrector.remove(offense_range)
74
74
  end
75
75
  end
76
+ alias on_csend on_send
76
77
 
77
78
  private
78
79
 
@@ -80,7 +81,7 @@ module RuboCop
80
81
  redundant_argument = redundant_arg_for_method(node.method_name.to_s)
81
82
  return false if redundant_argument.nil?
82
83
 
83
- node.arguments.first == redundant_argument
84
+ node.first_argument == redundant_argument
84
85
  end
85
86
 
86
87
  def redundant_arg_for_method(method_name)
@@ -114,7 +114,7 @@ module RuboCop
114
114
  if node.parent&.assignment?
115
115
  replace_begin_with_statement(corrector, offense_range, node)
116
116
  else
117
- corrector.remove(offense_range)
117
+ remove_begin(corrector, offense_range, node)
118
118
  end
119
119
 
120
120
  if use_modifier_form_after_multiline_begin_block?(node)
@@ -136,6 +136,14 @@ module RuboCop
136
136
  restore_removed_comments(corrector, offense_range, node, first_child)
137
137
  end
138
138
 
139
+ def remove_begin(corrector, offense_range, node)
140
+ if node.parent.respond_to?(:endless?) && node.parent.endless?
141
+ offense_range = range_with_surrounding_space(offense_range, newlines: true)
142
+ end
143
+
144
+ corrector.remove(offense_range)
145
+ end
146
+
139
147
  # Restore comments that occur between "begin" and "first_child".
140
148
  # These comments will be moved to above the assignment line.
141
149
  def restore_removed_comments(corrector, offense_range, node, first_child)
@@ -13,25 +13,71 @@ module RuboCop
13
13
  # # good
14
14
  # do_something(foo: bar, baz: qux)
15
15
  #
16
+ # # bad
17
+ # do_something(**{foo: bar, baz: qux}.merge(options))
18
+ #
19
+ # # good
20
+ # do_something(foo: bar, baz: qux, **options)
21
+ #
16
22
  class RedundantDoubleSplatHashBraces < Base
17
23
  extend AutoCorrector
18
24
 
19
25
  MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
26
+ MERGE_METHODS = %i[merge merge!].freeze
20
27
 
28
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
21
29
  def on_hash(node)
22
30
  return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
23
31
  return unless (parent = node.parent)
24
- return unless parent.kwsplat_type?
32
+ return unless parent.call_type? || parent.kwsplat_type?
33
+ return unless mergeable?(parent)
34
+ return unless (kwsplat = node.each_ancestor(:kwsplat).first)
35
+ return if !node.braces? || allowed_double_splat_receiver?(kwsplat)
25
36
 
26
- add_offense(parent) do |corrector|
27
- corrector.remove(parent.loc.operator)
28
- corrector.remove(opening_brace(node))
29
- corrector.remove(closing_brace(node))
37
+ add_offense(kwsplat) do |corrector|
38
+ autocorrect(corrector, node, kwsplat)
30
39
  end
31
40
  end
41
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
32
42
 
33
43
  private
34
44
 
45
+ def allowed_double_splat_receiver?(kwsplat)
46
+ first_child = kwsplat.children.first
47
+ return true if first_child.block_type? || first_child.numblock_type?
48
+ return false unless first_child.call_type?
49
+
50
+ root_receiver = root_receiver(first_child)
51
+
52
+ !root_receiver&.hash_type?
53
+ end
54
+
55
+ def autocorrect(corrector, node, kwsplat)
56
+ corrector.remove(kwsplat.loc.operator)
57
+ corrector.remove(opening_brace(node))
58
+ corrector.remove(closing_brace(node))
59
+
60
+ merge_methods = select_merge_method_nodes(kwsplat)
61
+ return if merge_methods.empty?
62
+
63
+ autocorrect_merge_methods(corrector, merge_methods, kwsplat)
64
+ end
65
+
66
+ def root_receiver(node)
67
+ receiver = node.receiver
68
+ if receiver&.receiver
69
+ root_receiver(receiver)
70
+ else
71
+ receiver
72
+ end
73
+ end
74
+
75
+ def select_merge_method_nodes(kwsplat)
76
+ extract_send_methods(kwsplat).select do |node|
77
+ mergeable?(node)
78
+ end
79
+ end
80
+
35
81
  def opening_brace(node)
36
82
  node.loc.begin.join(node.children.first.source_range.begin)
37
83
  end
@@ -39,6 +85,48 @@ module RuboCop
39
85
  def closing_brace(node)
40
86
  node.children.last.source_range.end.join(node.loc.end)
41
87
  end
88
+
89
+ def autocorrect_merge_methods(corrector, merge_methods, kwsplat)
90
+ range = range_of_merge_methods(merge_methods)
91
+
92
+ new_kwsplat_arguments = extract_send_methods(kwsplat).map do |descendant|
93
+ convert_to_new_arguments(descendant)
94
+ end
95
+ new_source = new_kwsplat_arguments.compact.reverse.unshift('').join(', ')
96
+
97
+ corrector.replace(range, new_source)
98
+ end
99
+
100
+ def range_of_merge_methods(merge_methods)
101
+ begin_merge_method = merge_methods.last
102
+ end_merge_method = merge_methods.first
103
+
104
+ begin_merge_method.loc.dot.begin.join(end_merge_method.source_range.end)
105
+ end
106
+
107
+ def extract_send_methods(kwsplat)
108
+ kwsplat.each_descendant(:send, :csend)
109
+ end
110
+
111
+ def convert_to_new_arguments(node)
112
+ return unless mergeable?(node)
113
+
114
+ node.arguments.map do |arg|
115
+ if arg.hash_type?
116
+ arg.source
117
+ else
118
+ "**#{arg.source}"
119
+ end
120
+ end
121
+ end
122
+
123
+ def mergeable?(node)
124
+ return true unless node.call_type?
125
+ return false unless MERGE_METHODS.include?(node.method_name)
126
+ return true unless (parent = node.parent)
127
+
128
+ mergeable?(parent)
129
+ end
42
130
  end
43
131
  end
44
132
  end
@@ -56,6 +56,7 @@ module RuboCop
56
56
  end
57
57
  end
58
58
  end
59
+ alias on_csend on_send
59
60
 
60
61
  private
61
62
 
@@ -64,7 +65,7 @@ module RuboCop
64
65
  return if node.last_argument&.block_pass_type?
65
66
 
66
67
  if node.method?(:each) && !node.parent&.block_type?
67
- ancestor_node = node.each_ancestor(:send).detect do |ancestor|
68
+ ancestor_node = node.each_ancestor(:send, :csend).detect do |ancestor|
68
69
  ancestor.receiver == node &&
69
70
  (RESTRICT_ON_SEND.include?(ancestor.method_name) || ancestor.method?(:reverse_each))
70
71
  end
@@ -83,10 +84,12 @@ module RuboCop
83
84
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
84
85
 
85
86
  def range(node)
86
- if node.method?(:each)
87
- node.loc.dot.join(node.loc.selector)
87
+ return node.selector unless node.method?(:each)
88
+
89
+ if node.parent.call_type?
90
+ node.selector.join(node.parent.loc.dot)
88
91
  else
89
- node.loc.selector
92
+ node.loc.dot.join(node.selector)
90
93
  end
91
94
  end
92
95