rubocop 1.70.0 → 1.71.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) 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/empty_line_between_defs.rb +7 -5
  25. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
  26. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  27. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  28. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  29. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  30. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  31. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +1 -1
  32. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  33. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -0
  34. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
  35. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  36. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -5
  37. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  38. data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  40. data/lib/rubocop/cop/layout/space_after_comma.rb +1 -1
  41. data/lib/rubocop/cop/layout/space_after_semicolon.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -0
  43. data/lib/rubocop/cop/layout/space_before_comma.rb +1 -1
  44. data/lib/rubocop/cop/layout/space_before_semicolon.rb +1 -1
  45. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  46. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +119 -0
  47. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -3
  48. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
  49. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +3 -3
  50. data/lib/rubocop/cop/lint/constant_reassignment.rb +2 -6
  51. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  52. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +1 -1
  53. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
  54. data/lib/rubocop/cop/lint/duplicate_set_element.rb +1 -1
  55. data/lib/rubocop/cop/lint/float_comparison.rb +5 -2
  56. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  57. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  58. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  59. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +13 -3
  60. data/lib/rubocop/cop/lint/missing_super.rb +2 -2
  61. data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
  62. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
  63. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -3
  64. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  65. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
  66. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +13 -18
  67. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
  68. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -5
  69. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
  70. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +2 -2
  71. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  72. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -1
  73. data/lib/rubocop/cop/lint/shared_mutable_default.rb +3 -3
  74. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  75. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  76. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +1 -1
  77. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  78. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -1
  79. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -4
  80. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  81. data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
  82. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +2 -1
  83. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +2 -2
  84. data/lib/rubocop/cop/lint/void.rb +1 -1
  85. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  86. data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
  87. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  88. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  89. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  90. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -5
  91. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  92. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  93. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +4 -4
  94. data/lib/rubocop/cop/mixin/hash_subset.rb +188 -0
  95. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
  96. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +22 -8
  97. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -2
  98. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  99. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -3
  100. data/lib/rubocop/cop/naming/block_forwarding.rb +18 -14
  101. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -3
  102. data/lib/rubocop/cop/security/compound_hash.rb +1 -0
  103. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -4
  104. data/lib/rubocop/cop/style/arguments_forwarding.rb +38 -19
  105. data/lib/rubocop/cop/style/array_first_last.rb +18 -2
  106. data/lib/rubocop/cop/style/block_delimiters.rb +7 -20
  107. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  108. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  109. data/lib/rubocop/cop/style/combinable_defined.rb +1 -1
  110. data/lib/rubocop/cop/style/combinable_loops.rb +2 -2
  111. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  112. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -4
  113. data/lib/rubocop/cop/style/documentation.rb +1 -1
  114. data/lib/rubocop/cop/style/double_negation.rb +3 -3
  115. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  116. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  117. data/lib/rubocop/cop/style/exact_regexp_match.rb +1 -1
  118. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  119. data/lib/rubocop/cop/style/fetch_env_var.rb +1 -1
  120. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  121. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -6
  122. data/lib/rubocop/cop/style/hash_except.rb +20 -131
  123. data/lib/rubocop/cop/style/hash_slice.rb +80 -0
  124. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  125. data/lib/rubocop/cop/style/identical_conditional_branches.rb +22 -3
  126. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -3
  127. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  128. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  129. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  130. data/lib/rubocop/cop/style/inverse_methods.rb +6 -6
  131. data/lib/rubocop/cop/style/it_assignment.rb +1 -1
  132. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  133. data/lib/rubocop/cop/style/map_into_array.rb +1 -1
  134. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  135. data/lib/rubocop/cop/style/map_to_set.rb +3 -2
  136. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -13
  137. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -1
  138. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -4
  139. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  140. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  141. data/lib/rubocop/cop/style/mutable_constant.rb +2 -2
  142. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
  143. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  144. data/lib/rubocop/cop/style/open_struct_use.rb +5 -5
  145. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -5
  146. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  147. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  148. data/lib/rubocop/cop/style/proc.rb +1 -2
  149. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  150. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  151. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  152. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +6 -10
  153. data/lib/rubocop/cop/style/redundant_each.rb +1 -1
  154. data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
  155. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
  156. data/lib/rubocop/cop/style/redundant_line_continuation.rb +27 -10
  157. data/lib/rubocop/cop/style/redundant_parentheses.rb +9 -6
  158. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  159. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  160. data/lib/rubocop/cop/style/redundant_self_assignment.rb +12 -27
  161. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  162. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  163. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  164. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  165. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  166. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  167. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  168. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  169. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  170. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  171. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  172. data/lib/rubocop/cop/style/super_arguments.rb +4 -4
  173. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  174. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
  175. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -1
  176. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  177. data/lib/rubocop/cop/util.rb +2 -2
  178. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  179. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  180. data/lib/rubocop/cops_documentation_generator.rb +13 -13
  181. data/lib/rubocop/directive_comment.rb +9 -8
  182. data/lib/rubocop/options.rb +2 -1
  183. data/lib/rubocop/result_cache.rb +13 -13
  184. data/lib/rubocop/rspec/expect_offense.rb +6 -2
  185. data/lib/rubocop/rspec/support.rb +1 -2
  186. data/lib/rubocop/target_finder.rb +1 -0
  187. data/lib/rubocop/version.rb +1 -1
  188. data/lib/rubocop.rb +3 -0
  189. metadata +15 -11
  190. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -28
@@ -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,)
@@ -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
@@ -72,7 +72,7 @@ module RuboCop
72
72
  parent = parent.parent if parent&.begin_type?
73
73
  return false if parent.nil?
74
74
 
75
- (parent.if_type? || parent.while_type? || parent.until_type?) && parent.modifier_form?
75
+ parent.type?(:if, :while, :until) && parent.modifier_form?
76
76
  end
77
77
 
78
78
  def capture_with_block!
@@ -100,7 +100,7 @@ module RuboCop
100
100
 
101
101
  # Only block scope allows referencing outer scope variables.
102
102
  node = scope.node
103
- return nil unless node.block_type? || node.numblock_type?
103
+ return nil unless node.any_block_type?
104
104
  end
105
105
 
106
106
  nil
@@ -113,14 +113,14 @@ module RuboCop
113
113
  def accessible_variables
114
114
  scope_stack.reverse_each.with_object([]) do |scope, variables|
115
115
  variables.concat(scope.variables.values)
116
- break variables unless scope.node.block_type? || scope.node.numblock_type?
116
+ break variables unless scope.node.any_block_type?
117
117
  end
118
118
  end
119
119
 
120
120
  private
121
121
 
122
122
  def mark_variable_as_captured_by_block_if_so(variable)
123
- return unless current_scope.node.block_type? || current_scope.node.numblock_type?
123
+ return unless current_scope.node.any_block_type?
124
124
  return if variable.scope == current_scope
125
125
 
126
126
  variable.capture_with_block!
@@ -18,7 +18,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
18
18
  description: ->(data) { "#{data.description}\n" },
19
19
  safety: ->(data) { safety_object(data.safety_objects, data.cop) },
20
20
  examples: ->(data) { examples(data.example_objects, data.cop) },
21
- configuration: ->(data) { configurations(data.cop.department, data.config, data.cop) },
21
+ configuration: ->(data) { configurations(data.cop.department, data.cop, data.config) },
22
22
  references: ->(data) { references(data.cop, data.see_objects) }
23
23
  }.freeze
24
24
 
@@ -180,17 +180,17 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
180
180
  content
181
181
  end
182
182
 
183
- def configurations(department, pars, cop)
184
- return '' if pars.empty?
185
-
183
+ def configurations(department, cop, cop_config)
186
184
  header = ['Name', 'Default value', 'Configurable values']
187
- configs = pars
185
+ configs = cop_config
188
186
  .each_key
189
187
  .reject { |key| key.start_with?('Supported') }
190
188
  .reject { |key| key.start_with?('AllowMultipleStyles') }
189
+ return '' if configs.empty?
190
+
191
191
  content = configs.map do |name|
192
- configurable = configurable_values(pars, name)
193
- default = format_table_value(pars[name])
192
+ configurable = configurable_values(cop_config, name)
193
+ default = format_table_value(cop_config[name])
194
194
 
195
195
  [configuration_name(department, name), default, configurable]
196
196
  end
@@ -206,17 +206,17 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
206
206
  end
207
207
 
208
208
  # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
209
- def configurable_values(pars, name)
209
+ def configurable_values(cop_config, name)
210
210
  case name
211
211
  when /^Enforced/
212
212
  supported_style_name = RuboCop::Cop::Util.to_supported_styles(name)
213
- format_table_value(pars[supported_style_name])
213
+ format_table_value(cop_config[supported_style_name])
214
214
  when 'IndentationWidth'
215
215
  'Integer'
216
216
  when 'Database'
217
- format_table_value(pars['SupportedDatabases'])
217
+ format_table_value(cop_config['SupportedDatabases'])
218
218
  else
219
- case pars[name]
219
+ case cop_config[name]
220
220
  when String
221
221
  'String'
222
222
  when Integer
@@ -319,7 +319,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
319
319
  AutoCorrect Description Enabled StyleGuide Reference Safe SafeAutoCorrect VersionAdded
320
320
  VersionChanged
321
321
  ]
322
- pars = cop_config.reject { |k| non_display_keys.include? k }
322
+ parameters = cop_config.reject { |k| non_display_keys.include? k }
323
323
  description = 'No documentation'
324
324
  example_objects = safety_objects = see_objects = []
325
325
  cop_code(cop) do |code_object|
@@ -329,7 +329,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
329
329
  see_objects = code_object.tags('see')
330
330
  end
331
331
  data = CopData.new(cop: cop, description: description, example_objects: example_objects,
332
- safety_objects: safety_objects, see_objects: see_objects, config: pars)
332
+ safety_objects: safety_objects, see_objects: see_objects, config: parameters)
333
333
  cops_body(data)
334
334
  end
335
335
 
@@ -88,10 +88,15 @@ module RuboCop
88
88
  @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
89
89
  end
90
90
 
91
+ # Returns an array of cops for this directive comment, without resolving departments
92
+ def raw_cop_names
93
+ @raw_cop_names ||= (cops || '').split(/,\s*/)
94
+ end
95
+
91
96
  # Returns array of specified in this directive department names
92
97
  # when all department disabled
93
98
  def department_names
94
- splitted_cops_string.select { |cop| department?(cop) }
99
+ raw_cop_names.select { |cop| department?(cop) }
95
100
  end
96
101
 
97
102
  # Checks if directive departments include cop
@@ -101,11 +106,11 @@ module RuboCop
101
106
 
102
107
  # Checks if cop department has already used in directive comment
103
108
  def overridden_by_department?(cop)
104
- in_directive_department?(cop) && splitted_cops_string.include?(cop)
109
+ in_directive_department?(cop) && raw_cop_names.include?(cop)
105
110
  end
106
111
 
107
112
  def directive_count
108
- splitted_cops_string.count
113
+ raw_cop_names.count
109
114
  end
110
115
 
111
116
  # Returns line number for directive
@@ -115,12 +120,8 @@ module RuboCop
115
120
 
116
121
  private
117
122
 
118
- def splitted_cops_string
119
- (cops || '').split(/,\s*/)
120
- end
121
-
122
123
  def parsed_cop_names
123
- cops = splitted_cops_string.map do |name|
124
+ cops = raw_cop_names.map do |name|
124
125
  department?(name) ? cop_names_for_department(name) : name
125
126
  end.flatten
126
127
  cops - [LINT_SYNTAX_COP]
@@ -579,7 +579,8 @@ module RuboCop
579
579
  'when combined with --display-only-correctable.'],
580
580
  show_cops: ['Shows the given cops, or all cops by',
581
581
  'default, and their configurations for the',
582
- 'current directory.'],
582
+ 'current directory.',
583
+ 'You can use `*` as a wildcard.'],
583
584
  show_docs_url: ['Display url to documentation for the given',
584
585
  'cops, or base url by default.'],
585
586
  fail_fast: ['Inspect files in order of modification',