rubocop 1.70.0 → 1.71.2

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 (195) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +17 -0
  4. data/lib/rubocop/cli/command/show_cops.rb +24 -2
  5. data/lib/rubocop/comment_config.rb +1 -1
  6. data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
  7. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  8. data/lib/rubocop/cop/internal_affairs/location_expression.rb +2 -1
  9. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +3 -2
  10. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  11. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +63 -0
  12. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +131 -0
  13. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +229 -0
  14. data/lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb +126 -0
  15. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +90 -0
  16. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +3 -1
  17. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
  18. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  19. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  21. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  22. data/lib/rubocop/cop/layout/class_structure.rb +9 -9
  23. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  24. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  25. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +7 -11
  26. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
  27. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  28. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  29. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  30. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  31. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  32. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +1 -1
  33. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  34. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -0
  35. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
  36. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  37. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -5
  38. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  39. data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
  40. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  41. data/lib/rubocop/cop/layout/space_after_comma.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_after_semicolon.rb +1 -1
  43. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -0
  44. data/lib/rubocop/cop/layout/space_before_comma.rb +1 -1
  45. data/lib/rubocop/cop/layout/space_before_semicolon.rb +1 -1
  46. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  47. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +119 -0
  48. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -3
  49. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
  50. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +3 -3
  51. data/lib/rubocop/cop/lint/constant_reassignment.rb +2 -6
  52. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  53. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +1 -1
  54. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
  55. data/lib/rubocop/cop/lint/duplicate_set_element.rb +1 -1
  56. data/lib/rubocop/cop/lint/empty_expression.rb +0 -2
  57. data/lib/rubocop/cop/lint/float_comparison.rb +5 -2
  58. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  59. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  60. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  61. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +13 -3
  62. data/lib/rubocop/cop/lint/missing_super.rb +2 -2
  63. data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
  64. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
  65. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -3
  66. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  67. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
  68. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +13 -18
  69. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
  70. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -5
  71. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
  72. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +2 -2
  73. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  74. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -1
  75. data/lib/rubocop/cop/lint/shared_mutable_default.rb +3 -3
  76. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  77. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  78. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +1 -1
  79. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -1
  80. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  81. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -1
  82. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -4
  83. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  84. data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
  85. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +2 -1
  86. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +2 -2
  87. data/lib/rubocop/cop/lint/void.rb +2 -7
  88. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  89. data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
  90. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  91. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  92. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  93. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -5
  94. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  95. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  96. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +4 -4
  97. data/lib/rubocop/cop/mixin/hash_subset.rb +188 -0
  98. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
  99. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +22 -8
  100. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -2
  101. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  102. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -3
  103. data/lib/rubocop/cop/naming/block_forwarding.rb +18 -14
  104. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -3
  105. data/lib/rubocop/cop/security/compound_hash.rb +1 -0
  106. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -4
  107. data/lib/rubocop/cop/style/arguments_forwarding.rb +38 -19
  108. data/lib/rubocop/cop/style/array_first_last.rb +18 -2
  109. data/lib/rubocop/cop/style/block_delimiters.rb +7 -20
  110. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  111. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  112. data/lib/rubocop/cop/style/combinable_defined.rb +1 -1
  113. data/lib/rubocop/cop/style/combinable_loops.rb +2 -2
  114. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  115. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -4
  116. data/lib/rubocop/cop/style/documentation.rb +1 -1
  117. data/lib/rubocop/cop/style/double_negation.rb +3 -3
  118. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  119. data/lib/rubocop/cop/style/each_with_object.rb +2 -3
  120. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  121. data/lib/rubocop/cop/style/exact_regexp_match.rb +1 -1
  122. data/lib/rubocop/cop/style/explicit_block_argument.rb +15 -2
  123. data/lib/rubocop/cop/style/fetch_env_var.rb +1 -1
  124. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  125. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -6
  126. data/lib/rubocop/cop/style/hash_except.rb +20 -131
  127. data/lib/rubocop/cop/style/hash_slice.rb +80 -0
  128. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  129. data/lib/rubocop/cop/style/identical_conditional_branches.rb +22 -3
  130. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -3
  131. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  132. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  133. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  134. data/lib/rubocop/cop/style/inverse_methods.rb +6 -6
  135. data/lib/rubocop/cop/style/it_assignment.rb +1 -1
  136. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  137. data/lib/rubocop/cop/style/map_into_array.rb +1 -1
  138. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  139. data/lib/rubocop/cop/style/map_to_set.rb +3 -2
  140. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -13
  141. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -1
  142. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -4
  143. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  145. data/lib/rubocop/cop/style/mutable_constant.rb +2 -2
  146. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
  147. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  148. data/lib/rubocop/cop/style/open_struct_use.rb +5 -5
  149. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -5
  150. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  151. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  152. data/lib/rubocop/cop/style/proc.rb +1 -2
  153. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  154. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  155. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  156. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +6 -10
  157. data/lib/rubocop/cop/style/redundant_each.rb +1 -1
  158. data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
  159. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
  160. data/lib/rubocop/cop/style/redundant_line_continuation.rb +27 -10
  161. data/lib/rubocop/cop/style/redundant_parentheses.rb +9 -6
  162. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  163. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  164. data/lib/rubocop/cop/style/redundant_self_assignment.rb +12 -27
  165. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  166. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  167. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  168. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  169. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  170. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  171. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  172. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  173. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  174. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  175. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  176. data/lib/rubocop/cop/style/super_arguments.rb +4 -4
  177. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  178. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
  179. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -1
  180. data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
  181. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  182. data/lib/rubocop/cop/util.rb +2 -2
  183. data/lib/rubocop/cop/variable_force/variable.rb +14 -2
  184. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  185. data/lib/rubocop/cops_documentation_generator.rb +13 -13
  186. data/lib/rubocop/directive_comment.rb +9 -8
  187. data/lib/rubocop/options.rb +2 -1
  188. data/lib/rubocop/result_cache.rb +13 -13
  189. data/lib/rubocop/rspec/expect_offense.rb +6 -2
  190. data/lib/rubocop/rspec/support.rb +1 -2
  191. data/lib/rubocop/target_finder.rb +1 -0
  192. data/lib/rubocop/version.rb +1 -1
  193. data/lib/rubocop.rb +3 -0
  194. metadata +15 -11
  195. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -28
@@ -69,11 +69,7 @@ module RuboCop
69
69
  elements = Array(node).compact
70
70
 
71
71
  # Account for edge case of `Constant::CONSTANT`
72
- !node.array_type? || return_of_method_call?(node) || elements.any?(&:splat_type?)
73
- end
74
-
75
- def return_of_method_call?(node)
76
- node.block_type? || node.send_type?
72
+ !node.array_type? || elements.any?(&:splat_type?)
77
73
  end
78
74
 
79
75
  def assignment_corrector(node, rhs, order)
@@ -94,8 +94,8 @@ module RuboCop
94
94
  end
95
95
 
96
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?
97
+ return false unless node.type?(:while, :until)
98
+ return false unless condition_body.any_block_type?
99
99
 
100
100
  condition_body.send_node.block_literal? && condition_body.keywords?
101
101
  end
@@ -99,7 +99,7 @@ module RuboCop
99
99
  def string_source(node)
100
100
  if node.is_a?(String)
101
101
  node.scrub
102
- elsif node.respond_to?(:type) && (node.str_type? || node.sym_type?)
102
+ elsif node.respond_to?(:type) && node.type?(:str, :sym)
103
103
  node.source
104
104
  end
105
105
  end
@@ -19,8 +19,7 @@ module RuboCop
19
19
  MSG = 'Use `proc` instead of `Proc.new`.'
20
20
 
21
21
  # @!method proc_new?(node)
22
- def_node_matcher :proc_new?,
23
- '({block numblock} $(send (const {nil? cbase} :Proc) :new) ...)'
22
+ def_node_matcher :proc_new?, '(any_block $(send (const {nil? cbase} :Proc) :new) ...)'
24
23
 
25
24
  def on_block(node)
26
25
  proc_new?(node) do |block_method|
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # passed multiple arguments.
15
15
  #
16
16
  # The exploded style has an `AllowedCompactTypes` configuration
17
- # option that takes an Array of exception name Strings.
17
+ # option that takes an `Array` of exception name Strings.
18
18
  #
19
19
  # @safety
20
20
  # This cop is unsafe because `raise Foo` calls `Foo.exception`, not `Foo.new`.
@@ -185,7 +185,7 @@ module RuboCop
185
185
  def contain_rescue_or_ensure?(node)
186
186
  first_child = node.children.first
187
187
 
188
- first_child.rescue_type? || first_child.ensure_type?
188
+ first_child.type?(:rescue, :ensure)
189
189
  end
190
190
 
191
191
  def valid_context_using_only_begin?(node)
@@ -157,7 +157,7 @@ module RuboCop
157
157
  end
158
158
 
159
159
  def asgn_type?(node)
160
- node.lvasgn_type? || node.ivasgn_type? || node.cvasgn_type? || node.gvasgn_type?
160
+ node.type?(:lvasgn, :ivasgn, :cvasgn, :gvasgn)
161
161
  end
162
162
 
163
163
  def branches_have_method?(node)
@@ -184,7 +184,7 @@ module RuboCop
184
184
  return false unless argument.hash_type?
185
185
  return false unless (node = argument.children.first)
186
186
 
187
- node.kwsplat_type? || node.forwarded_kwrestarg_type?
187
+ node.type?(:kwsplat, :forwarded_kwrestarg)
188
188
  end
189
189
 
190
190
  def if_source(if_branch, arithmetic_operation)
@@ -25,11 +25,11 @@ module RuboCop
25
25
  MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
26
26
  MERGE_METHODS = %i[merge merge!].freeze
27
27
 
28
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
28
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
29
29
  def on_hash(node)
30
30
  return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
31
31
  return unless (parent = node.parent)
32
- return unless parent.call_type? || parent.kwsplat_type?
32
+ return unless parent.type?(:call, :kwsplat)
33
33
  return unless mergeable?(parent)
34
34
  return unless (kwsplat = node.each_ancestor(:kwsplat).first)
35
35
  return if !node.braces? || allowed_double_splat_receiver?(kwsplat)
@@ -38,13 +38,13 @@ module RuboCop
38
38
  autocorrect(corrector, node, kwsplat)
39
39
  end
40
40
  end
41
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
41
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
42
42
 
43
43
  private
44
44
 
45
45
  def allowed_double_splat_receiver?(kwsplat)
46
46
  first_child = kwsplat.children.first
47
- return true if first_child.block_type? || first_child.numblock_type?
47
+ return true if first_child.any_block_type?
48
48
  return false unless first_child.call_type?
49
49
 
50
50
  root_receiver = root_receiver(first_child)
@@ -73,7 +73,7 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def select_merge_method_nodes(kwsplat)
76
- extract_send_methods(kwsplat).select do |node|
76
+ kwsplat.each_descendant(:call).select do |node|
77
77
  mergeable?(node)
78
78
  end
79
79
  end
@@ -89,7 +89,7 @@ module RuboCop
89
89
  def autocorrect_merge_methods(corrector, merge_methods, kwsplat)
90
90
  range = range_of_merge_methods(merge_methods)
91
91
 
92
- new_kwsplat_arguments = extract_send_methods(kwsplat).map do |descendant|
92
+ new_kwsplat_arguments = kwsplat.each_descendant(:call).map do |descendant|
93
93
  convert_to_new_arguments(descendant)
94
94
  end
95
95
  new_source = new_kwsplat_arguments.compact.reverse.unshift('').join(', ')
@@ -104,10 +104,6 @@ module RuboCop
104
104
  begin_merge_method.loc.dot.begin.join(end_merge_method.source_range.end)
105
105
  end
106
106
 
107
- def extract_send_methods(kwsplat)
108
- kwsplat.each_descendant(:send, :csend)
109
- end
110
-
111
107
  def convert_to_new_arguments(node)
112
108
  return unless mergeable?(node)
113
109
 
@@ -65,7 +65,7 @@ module RuboCop
65
65
  return if node.last_argument&.block_pass_type?
66
66
 
67
67
  if node.method?(:each) && !node.parent&.block_type?
68
- ancestor_node = node.each_ancestor(:send, :csend).detect do |ancestor|
68
+ ancestor_node = node.each_ancestor(:call).detect do |ancestor|
69
69
  ancestor.receiver == node &&
70
70
  (RESTRICT_ON_SEND.include?(ancestor.method_name) || ancestor.method?(:reverse_each))
71
71
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for RuntimeError as the argument of raise/fail.
6
+ # Checks for `RuntimeError` as the argument of `raise`/`fail`.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -51,7 +51,7 @@ module RuboCop
51
51
  end
52
52
 
53
53
  def string_message?(message)
54
- message.str_type? || message.dstr_type? || message.xstr_type?
54
+ message.type?(:str, :dstr, :xstr)
55
55
  end
56
56
 
57
57
  def fix_compact(node)
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Style
6
6
  # Check for uses of `Object#freeze` on immutable objects.
7
7
  #
8
- # NOTE: Regexp and Range literals are frozen objects since Ruby 3.0.
8
+ # NOTE: `Regexp` and `Range` literals are frozen objects since Ruby 3.0.
9
9
  #
10
10
  # NOTE: From Ruby 3.0, this cop allows explicit freezing of interpolated
11
11
  # string literals when `# frozen-string-literal: true` is used.
@@ -42,7 +42,7 @@ module RuboCop
42
42
  return true if node.immutable_literal?
43
43
  return true if frozen_string_literal?(node)
44
44
 
45
- target_ruby_version >= 3.0 && (node.regexp_type? || node.range_type?)
45
+ target_ruby_version >= 3.0 && node.type?(:regexp, :range)
46
46
  end
47
47
 
48
48
  def strip_parenthesis(node)
@@ -73,10 +73,15 @@ module RuboCop
73
73
  LINE_CONTINUATION_PATTERN = /(\\\n)/.freeze
74
74
  ALLOWED_STRING_TOKENS = %i[tSTRING tSTRING_CONTENT].freeze
75
75
  ARGUMENT_TYPES = %i[
76
- kDEF kFALSE kNIL kSELF kTRUE tCONSTANT tCVAR tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR
77
- tLBRACK tLCURLY tLPAREN_ARG tSTRING tSTRING_BEG tSYMBOL tXSTRING_BEG
76
+ kDEF kDEFINED kFALSE kNIL kSELF kTRUE tAMPER tBANG tCARET tCHARACTER tCOLON3 tCONSTANT
77
+ tCVAR tDOT2 tDOT3 tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR tLAMBDA tLBRACK tLCURLY
78
+ tLPAREN_ARG tPIPE tQSYMBOLS_BEG tQWORDS_BEG tREGEXP_BEG tSTAR tSTRING tSTRING_BEG tSYMBEG
79
+ tSYMBOL tSYMBOLS_BEG tTILDE tUMINUS tUNARY_NUM tUPLUS tWORDS_BEG tXSTRING_BEG
78
80
  ].freeze
79
- ARGUMENT_TAKING_FLOW_TOKEN_TYPES = %i[tIDENTIFIER kRETURN kBREAK kNEXT kYIELD].freeze
81
+ ARGUMENT_TAKING_FLOW_TOKEN_TYPES = %i[
82
+ tIDENTIFIER kBREAK kNEXT kRETURN kSUPER kYIELD
83
+ ].freeze
84
+ ARITHMETIC_OPERATOR_TOKENS = %i[tDIVIDE tDSTAR tMINUS tPERCENT tPLUS tSTAR2].freeze
80
85
 
81
86
  def on_new_investigation
82
87
  return unless processed_source.ast
@@ -96,15 +101,20 @@ module RuboCop
96
101
  private
97
102
 
98
103
  def require_line_continuation?(range)
99
- !ends_with_backslash_without_comment?(range.source_line) ||
104
+ !ends_with_uncommented_backslash?(range) ||
100
105
  string_concatenation?(range.source_line) ||
101
- start_with_arithmetic_operator?(processed_source[range.line]) ||
106
+ start_with_arithmetic_operator?(range) ||
102
107
  inside_string_literal_or_method_with_argument?(range) ||
103
108
  leading_dot_method_chain_with_blank_line?(range)
104
109
  end
105
110
 
106
- def ends_with_backslash_without_comment?(source_line)
107
- source_line.gsub(/#.+/, '').end_with?('\\')
111
+ def ends_with_uncommented_backslash?(range)
112
+ # A line continuation always needs to be the last character on the line, which
113
+ # means that it is impossible to have a comment following a continuation.
114
+ # Therefore, if the line contains a comment, it cannot end with a continuation.
115
+ return false if processed_source.line_with_comment?(range.line)
116
+
117
+ range.source_line.end_with?(LINE_CONTINUATION)
108
118
  end
109
119
 
110
120
  def string_concatenation?(source_line)
@@ -140,7 +150,7 @@ module RuboCop
140
150
 
141
151
  def inspect_end_of_ruby_code_line_continuation
142
152
  last_line = processed_source.lines[processed_source.ast.last_line - 1]
143
- return unless last_line.end_with?(LINE_CONTINUATION)
153
+ return unless code_ends_with_continuation?(last_line)
144
154
 
145
155
  last_column = last_line.length
146
156
  line_continuation_range = range_between(last_column - 1, last_column)
@@ -150,6 +160,12 @@ module RuboCop
150
160
  end
151
161
  end
152
162
 
163
+ def code_ends_with_continuation?(last_line)
164
+ return false if processed_source.line_with_comment?(processed_source.ast.last_line)
165
+
166
+ last_line.end_with?(LINE_CONTINUATION)
167
+ end
168
+
153
169
  def inside_string_literal?(range, token)
154
170
  ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
155
171
  end
@@ -213,8 +229,9 @@ module RuboCop
213
229
  node.call_type? && !node.arguments.empty?
214
230
  end
215
231
 
216
- def start_with_arithmetic_operator?(source_line)
217
- %r{\A\s*[+\-*/%]}.match?(source_line)
232
+ def start_with_arithmetic_operator?(range)
233
+ line_range = processed_source.buffer.line_range(range.line + 1)
234
+ ARITHMETIC_OPERATOR_TOKENS.include?(processed_source.first_token_of(line_range).type)
218
235
  end
219
236
  end
220
237
  end
@@ -50,7 +50,7 @@ module RuboCop
50
50
  def ignore_syntax?(node)
51
51
  return false unless (parent = node.parent)
52
52
 
53
- parent.while_post_type? || parent.until_post_type? || parent.match_with_lvasgn_type? ||
53
+ parent.type?(:while_post, :until_post, :match_with_lvasgn) ||
54
54
  like_method_argument_parentheses?(parent) || multiline_control_flow_statements?(node)
55
55
  end
56
56
 
@@ -72,7 +72,7 @@ module RuboCop
72
72
  ancestor = node.ancestors.first
73
73
  return false unless ancestor
74
74
 
75
- !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
75
+ !ancestor.type?(:begin, :def, :any_block)
76
76
  end
77
77
 
78
78
  def allowed_ternary?(node)
@@ -89,7 +89,7 @@ module RuboCop
89
89
  end
90
90
 
91
91
  def like_method_argument_parentheses?(node)
92
- return false if !node.send_type? && !node.super_type? && !node.yield_type?
92
+ return false unless node.type?(:send, :super, :yield)
93
93
 
94
94
  node.arguments.one? && !node.parenthesized? &&
95
95
  !node.arithmetic_operation? && node.first_argument.begin_type?
@@ -99,7 +99,7 @@ module RuboCop
99
99
  return false unless (parent = node.parent)
100
100
  return false if parent.single_line?
101
101
 
102
- parent.return_type? || parent.next_type? || parent.break_type?
102
+ parent.type?(:return, :next, :break)
103
103
  end
104
104
 
105
105
  def empty_parentheses?(node)
@@ -140,6 +140,9 @@ module RuboCop
140
140
  return 'a literal' if disallowed_literal?(begin_node, node)
141
141
  return 'a variable' if node.variable?
142
142
  return 'a constant' if node.const_type?
143
+ if node.assignment? && (begin_node.parent.nil? || begin_node.parent.begin_type?)
144
+ return 'an assignment'
145
+ end
143
146
  if node.lambda_or_proc? && (node.braces? || node.send_node.lambda_literal?)
144
147
  return 'an expression'
145
148
  end
@@ -276,9 +279,9 @@ module RuboCop
276
279
  end
277
280
 
278
281
  def do_end_block_in_method_chain?(begin_node, node)
279
- return false unless (block = node.each_descendant(:block, :numblock).first)
282
+ return false unless (block = node.each_descendant(:any_block).first)
280
283
 
281
- block.keywords? && begin_node.each_ancestor(:send, :csend).any?
284
+ block.keywords? && begin_node.each_ancestor(:call).any?
282
285
  end
283
286
  end
284
287
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for unnecessary single-element Regexp character classes.
6
+ # Checks for unnecessary single-element `Regexp` character classes.
7
7
  #
8
8
  # @example
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for redundant escapes inside Regexp literals.
6
+ # Checks for redundant escapes inside `Regexp` literals.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -21,12 +21,8 @@ module RuboCop
21
21
  # args += foo
22
22
  # hash.merge!(other)
23
23
  #
24
- # # bad
25
- # self.foo = foo.concat(ary)
26
- #
27
24
  # # good
28
25
  # foo.concat(ary)
29
- # self.foo += ary
30
26
  #
31
27
  class RedundantSelfAssignment < Base
32
28
  include RangeHelp
@@ -49,6 +45,16 @@ module RuboCop
49
45
  gvasgn: :gvar
50
46
  }.freeze
51
47
 
48
+ # @!method redundant_self_assignment?
49
+ def_node_matcher :redundant_self_assignment?, <<~PATTERN
50
+ (call
51
+ %1 _
52
+ (call
53
+ (call
54
+ %1 %2) #method_returning_self?
55
+ ...))
56
+ PATTERN
57
+
52
58
  # rubocop:disable Metrics/AbcSize
53
59
  def on_lvasgn(node)
54
60
  return unless (rhs = node.rhs)
@@ -85,31 +91,10 @@ module RuboCop
85
91
  METHODS_RETURNING_SELF.include?(method_name)
86
92
  end
87
93
 
88
- # @!method redundant_self_assignment?(node, method_name)
89
- def_node_matcher :redundant_self_assignment?, <<~PATTERN
90
- (send
91
- (self) _
92
- (call
93
- (send
94
- {(self) nil?} %1) #method_returning_self?
95
- ...))
96
- PATTERN
97
-
98
- # @!method redundant_nonself_assignment?(node, receiver, method_name)
99
- def_node_matcher :redundant_nonself_assignment?, <<~PATTERN
100
- (call
101
- %1 _
102
- (call
103
- (call
104
- %1 %2) #method_returning_self?
105
- ...))
106
- PATTERN
107
-
108
94
  def redundant_assignment?(node)
109
- receiver_name = node.method_name.to_s[0...-1].to_sym
95
+ receiver_name = node.method_name.to_s.delete_suffix('=').to_sym
110
96
 
111
- redundant_self_assignment?(node, receiver_name) ||
112
- redundant_nonself_assignment?(node, node.receiver, receiver_name)
97
+ redundant_self_assignment?(node, node.receiver, receiver_name)
113
98
  end
114
99
 
115
100
  def correction_range(node)
@@ -93,9 +93,9 @@ module RuboCop
93
93
  (call $(call _ $:sort_by _) ${:last :first})
94
94
  (send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
95
95
 
96
- (call ({block numblock} $(call _ ${:sort_by :sort}) ...) ${:last :first})
96
+ (call (any_block $(call _ ${:sort_by :sort}) ...) ${:last :first})
97
97
  (call
98
- ({block numblock} $(call _ ${:sort_by :sort}) ...)
98
+ (any_block $(call _ ${:sort_by :sort}) ...)
99
99
  ${:[] :at :slice} {(int 0) (int -1)}
100
100
  )
101
101
  }
@@ -41,7 +41,7 @@ module RuboCop
41
41
  MSG = 'Redundant escape of %<char>s inside string literal.'
42
42
 
43
43
  def on_str(node)
44
- return if node.parent&.regexp_type? || node.parent&.xstr_type? || node.character_literal?
44
+ return if node.parent&.type?(:regexp, :xstr) || node.character_literal?
45
45
 
46
46
  str_contents_range = str_contents_range(node)
47
47
 
@@ -147,7 +147,7 @@ module RuboCop
147
147
  end
148
148
 
149
149
  def heredoc?(node)
150
- (node.str_type? || node.dstr_type?) && node.heredoc?
150
+ node.type?(:str, :dstr) && node.heredoc?
151
151
  end
152
152
 
153
153
  def delimiter?(node, char)
@@ -83,7 +83,7 @@ module RuboCop
83
83
  end
84
84
 
85
85
  def scoped_node?(node)
86
- node.def_type? || node.defs_type? || node.lambda?
86
+ node.type?(:def, :defs) || node.lambda?
87
87
  end
88
88
 
89
89
  # @!method chained_send?(node)
@@ -312,7 +312,7 @@ module RuboCop
312
312
  end
313
313
 
314
314
  def chain_length(method_chain, method)
315
- method.each_ancestor(:send, :csend).inject(0) do |total, ancestor|
315
+ method.each_ancestor(:call).inject(0) do |total, ancestor|
316
316
  break total + 1 if ancestor == method_chain
317
317
 
318
318
  total + 1
@@ -164,7 +164,7 @@ module RuboCop
164
164
 
165
165
  ast = processed_source.ast
166
166
  @range_nodes = ast.range_type? ? [ast] : []
167
- @range_nodes.concat(ast.each_descendant(:irange, :erange).to_a)
167
+ @range_nodes.concat(ast.each_descendant(:range).to_a)
168
168
  end
169
169
  end
170
170
  end
@@ -10,7 +10,7 @@ module RuboCop
10
10
  # parameters.
11
11
  #
12
12
  # Configuration option: Methods
13
- # Should be set to use this cop. Array of hashes, where each key is the
13
+ # Should be set to use this cop. `Array` of hashes, where each key is the
14
14
  # method name and value - array of argument names.
15
15
  #
16
16
  # @example Methods: [{reduce: %w[a b]}]
@@ -68,7 +68,7 @@ module RuboCop
68
68
  return false if body_node.parent.assignment_method? ||
69
69
  NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type)
70
70
 
71
- !(body_node.begin_type? || body_node.kwbegin_type?)
71
+ !body_node.type?(:begin, :kwbegin)
72
72
  end
73
73
 
74
74
  def correct_to_multiline(corrector, node)
@@ -156,7 +156,7 @@ module RuboCop
156
156
  # Handle `send` and `block` nodes that need to be wrapped in parens
157
157
  # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
158
158
  # but wrapping the argument list would be a more ergonomic correction.
159
- node_to_check = condition&.block_type? ? condition.send_node : condition
159
+ node_to_check = condition&.any_block_type? ? condition.send_node : condition
160
160
  return unless wrap_condition?(node_to_check)
161
161
 
162
162
  if condition.call_type?
@@ -243,7 +243,7 @@ module RuboCop
243
243
  def replace_condition(condition)
244
244
  return condition.source unless wrap_condition?(condition)
245
245
 
246
- if condition.call_type?
246
+ if condition.call_type? && !condition.comparison_method?
247
247
  parenthesized_method_arguments(condition)
248
248
  else
249
249
  "(#{condition.source})"
@@ -132,7 +132,7 @@ module RuboCop
132
132
  end
133
133
 
134
134
  def heredoc?(node)
135
- return false unless node.str_type? || node.dstr_type?
135
+ return false unless node.type?(:str, :dstr)
136
136
 
137
137
  node.heredoc?
138
138
  end
@@ -69,7 +69,7 @@ module RuboCop
69
69
  end
70
70
 
71
71
  def all_string_literals?(nodes)
72
- nodes.all? { |n| n.str_type? || n.dstr_type? }
72
+ nodes.all? { |n| n.type?(:str, :dstr) }
73
73
  end
74
74
 
75
75
  def detect_quote_styles(node)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Style
6
6
  # Enforces the use of consistent method names
7
- # from the String class.
7
+ # from the `String` class.
8
8
  #
9
9
  # @example
10
10
  # # bad
@@ -98,7 +98,7 @@ module RuboCop
98
98
  # When defining dynamic methods, implicitly calling `super` is not possible.
99
99
  # Since there is a possibility of delegation to `define_method`,
100
100
  # `super` used within the block is always allowed.
101
- break if node.block_type? && !block_sends_to_super?(super_node, node)
101
+ break if node.any_block_type? && !block_sends_to_super?(super_node, node)
102
102
 
103
103
  break node if DEF_TYPES.include?(node.type)
104
104
  end
@@ -136,13 +136,13 @@ module RuboCop
136
136
  # Checks if the send node of a block is the given super node,
137
137
  # or a method chain containing it.
138
138
  return false unless parent_node
139
- return false unless parent_node.type?(:block, :numblock)
139
+ return false unless parent_node.any_block_type?
140
140
 
141
141
  parent_node.send_node.each_node(:super).any?(super_node)
142
142
  end
143
143
 
144
144
  def positional_arg_same?(def_arg, super_arg)
145
- return false unless def_arg.arg_type? || def_arg.optarg_type?
145
+ return false unless def_arg.type?(:arg, :optarg)
146
146
  return false unless super_arg.lvar_type?
147
147
 
148
148
  def_arg.name == super_arg.children.first
@@ -159,7 +159,7 @@ module RuboCop
159
159
  end
160
160
 
161
161
  def keyword_arg_same?(def_arg, super_arg)
162
- return false unless def_arg.kwarg_type? || def_arg.kwoptarg_type?
162
+ return false unless def_arg.type?(:kwarg, :kwoptarg)
163
163
  return false unless (pair_node = super_arg).pair_type?
164
164
  return false unless (sym_node = pair_node.key).sym_type?
165
165
  return false unless (lvar_node = pair_node.value).lvar_type?
@@ -232,7 +232,7 @@ module RuboCop
232
232
  end
233
233
 
234
234
  def node_with_args?(node)
235
- node.call_type? || node.defined_type?
235
+ node.type?(:call, :defined?)
236
236
  end
237
237
  end
238
238
  end
@@ -77,7 +77,7 @@ module RuboCop
77
77
 
78
78
  # @!method define_method_block?(node)
79
79
  def_node_matcher :define_method_block?, <<~PATTERN
80
- ({block numblock} (send _ :define_method _) ...)
80
+ (any_block (send _ :define_method _) ...)
81
81
  PATTERN
82
82
  end
83
83
  end
@@ -7,12 +7,15 @@ module RuboCop
7
7
  # The supported styles are:
8
8
  #
9
9
  # * `consistent_comma`: Requires a comma after the last argument,
10
- # for all parenthesized method calls with arguments.
10
+ # for all parenthesized multi-line method calls with arguments.
11
11
  # * `comma`: Requires a comma after the last argument, but only for
12
12
  # parenthesized method calls where each argument is on its own line.
13
13
  # * `no_comma`: Requires that there is no comma after the last
14
14
  # argument.
15
15
  #
16
+ # Regardless of style, trailing commas are not allowed in
17
+ # single-line method calls.
18
+ #
16
19
  # @example EnforcedStyleForMultiline: consistent_comma
17
20
  # # bad
18
21
  # method(1, 2,)
@@ -24,7 +24,6 @@ module RuboCop
24
24
  # # good
25
25
  # x += 1 until x > 10
26
26
  #
27
- # @example
28
27
  # # bad
29
28
  # x += 100 while x < 500 # a long comment that makes code too long if it were a single line
30
29
  #
@@ -72,7 +72,7 @@ module RuboCop
72
72
  end
73
73
 
74
74
  def constant_portion?(node)
75
- node.numeric_type? || node.const_type?
75
+ node.type?(:numeric, :const)
76
76
  end
77
77
 
78
78
  def supported_operators
@@ -74,9 +74,9 @@ module RuboCop
74
74
 
75
75
  def args_begin(node)
76
76
  loc = node.loc
77
- selector = if node.super_type? || node.yield_type?
77
+ selector = if node.type?(:super, :yield)
78
78
  loc.keyword
79
- elsif node.def_type? || node.defs_type?
79
+ elsif node.type?(:def, :defs)
80
80
  loc.name
81
81
  else
82
82
  loc.selector