rubocop 1.53.0 → 1.60.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +5 -3
  4. data/config/default.yml +80 -11
  5. data/config/obsoletion.yml +5 -0
  6. data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
  7. data/lib/rubocop/cli.rb +1 -1
  8. data/lib/rubocop/config.rb +0 -2
  9. data/lib/rubocop/config_finder.rb +2 -2
  10. data/lib/rubocop/config_loader.rb +0 -1
  11. data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
  12. data/lib/rubocop/config_obsoletion.rb +11 -8
  13. data/lib/rubocop/config_validator.rb +0 -2
  14. data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
  15. data/lib/rubocop/cop/base.rb +7 -1
  16. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
  17. data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
  18. data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
  19. data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
  20. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
  21. data/lib/rubocop/cop/exclude_limit.rb +1 -1
  22. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
  23. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
  24. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
  26. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
  27. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
  28. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
  29. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
  30. data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
  31. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  32. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  33. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  34. data/lib/rubocop/cop/layout/dot_position.rb +1 -5
  35. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
  36. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +26 -3
  37. data/lib/rubocop/cop/layout/end_alignment.rb +12 -2
  38. data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
  39. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +22 -7
  40. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  41. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  42. data/lib/rubocop/cop/layout/heredoc_indentation.rb +4 -1
  43. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  44. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  45. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
  46. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
  47. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
  48. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  49. data/lib/rubocop/cop/layout/redundant_line_break.rb +15 -4
  50. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
  51. data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
  52. data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
  53. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  54. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
  55. data/lib/rubocop/cop/layout/space_around_operators.rb +53 -21
  56. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  57. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
  58. data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -4
  59. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
  60. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  61. data/lib/rubocop/cop/lint/debugger.rb +18 -4
  62. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
  63. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  64. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -3
  65. data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
  66. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
  67. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
  68. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
  69. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  70. data/lib/rubocop/cop/lint/mixed_case_range.rb +3 -1
  71. data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
  72. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
  73. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
  74. data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
  75. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +10 -0
  76. data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -0
  77. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +63 -4
  78. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  79. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  80. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +14 -8
  81. data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
  82. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  83. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
  84. data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
  85. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  86. data/lib/rubocop/cop/lint/symbol_conversion.rb +8 -3
  87. data/lib/rubocop/cop/lint/syntax.rb +6 -3
  88. data/lib/rubocop/cop/lint/to_enum_arguments.rb +5 -3
  89. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  90. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
  91. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  92. data/lib/rubocop/cop/lint/useless_assignment.rb +38 -12
  93. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  94. data/lib/rubocop/cop/lint/void.rb +46 -21
  95. data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
  96. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  97. data/lib/rubocop/cop/metrics/class_length.rb +8 -3
  98. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  99. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +32 -4
  100. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  101. data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
  102. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  103. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  104. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  105. data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
  106. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
  107. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -8
  108. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  109. data/lib/rubocop/cop/mixin/string_help.rb +4 -2
  110. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  111. data/lib/rubocop/cop/naming/block_forwarding.rb +12 -4
  112. data/lib/rubocop/cop/naming/constant_name.rb +1 -2
  113. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
  115. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  116. data/lib/rubocop/cop/registry.rb +4 -4
  117. data/lib/rubocop/cop/security/open.rb +2 -2
  118. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -2
  119. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  120. data/lib/rubocop/cop/style/alias.rb +9 -8
  121. data/lib/rubocop/cop/style/arguments_forwarding.rb +355 -63
  122. data/lib/rubocop/cop/style/array_first_last.rb +64 -0
  123. data/lib/rubocop/cop/style/array_intersect.rb +13 -5
  124. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
  125. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
  126. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  127. data/lib/rubocop/cop/style/case_like_if.rb +4 -4
  128. data/lib/rubocop/cop/style/class_check.rb +1 -0
  129. data/lib/rubocop/cop/style/class_equality_comparison.rb +7 -0
  130. data/lib/rubocop/cop/style/collection_compact.rb +7 -6
  131. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  132. data/lib/rubocop/cop/style/combinable_loops.rb +17 -9
  133. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -1
  134. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  135. data/lib/rubocop/cop/style/date_time.rb +5 -4
  136. data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
  137. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  138. data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
  139. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  140. data/lib/rubocop/cop/style/eval_with_location.rb +3 -14
  141. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
  142. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  143. data/lib/rubocop/cop/style/for.rb +1 -1
  144. data/lib/rubocop/cop/style/format_string.rb +24 -3
  145. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -1
  146. data/lib/rubocop/cop/style/guard_clause.rb +26 -0
  147. data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
  148. data/lib/rubocop/cop/style/hash_each_methods.rb +83 -10
  149. data/lib/rubocop/cop/style/hash_except.rb +2 -1
  150. data/lib/rubocop/cop/style/identical_conditional_branches.rb +28 -3
  151. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  152. data/lib/rubocop/cop/style/inverse_methods.rb +6 -5
  153. data/lib/rubocop/cop/style/invertible_unless_condition.rb +39 -2
  154. data/lib/rubocop/cop/style/lambda.rb +3 -3
  155. data/lib/rubocop/cop/style/lambda_call.rb +5 -0
  156. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +3 -2
  157. data/lib/rubocop/cop/style/map_to_hash.rb +17 -7
  158. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +22 -6
  159. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
  160. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
  161. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  162. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
  163. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  164. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  165. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -4
  166. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  167. data/lib/rubocop/cop/style/next.rb +1 -1
  168. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  169. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  170. data/lib/rubocop/cop/style/operator_method_call.rb +8 -2
  171. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  172. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
  173. data/lib/rubocop/cop/style/redundant_argument.rb +9 -3
  174. data/lib/rubocop/cop/style/redundant_begin.rb +9 -1
  175. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -9
  176. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +1 -1
  177. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
  178. data/lib/rubocop/cop/style/redundant_each.rb +7 -4
  179. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  180. data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
  181. data/lib/rubocop/cop/style/redundant_filter_chain.rb +22 -5
  182. data/lib/rubocop/cop/style/redundant_line_continuation.rb +10 -1
  183. data/lib/rubocop/cop/style/redundant_parentheses.rb +63 -21
  184. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +18 -4
  185. data/lib/rubocop/cop/style/redundant_return.rb +8 -3
  186. data/lib/rubocop/cop/style/redundant_self.rb +17 -2
  187. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +5 -0
  188. data/lib/rubocop/cop/style/redundant_sort.rb +9 -8
  189. data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
  190. data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
  191. data/lib/rubocop/cop/style/return_nil.rb +6 -2
  192. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +23 -9
  193. data/lib/rubocop/cop/style/sample.rb +2 -1
  194. data/lib/rubocop/cop/style/select_by_regexp.rb +7 -6
  195. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  196. data/lib/rubocop/cop/style/semicolon.rb +8 -3
  197. data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
  198. data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
  199. data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
  200. data/lib/rubocop/cop/style/sole_nested_conditional.rb +3 -1
  201. data/lib/rubocop/cop/style/string_chars.rb +1 -0
  202. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
  203. data/lib/rubocop/cop/style/strip.rb +7 -4
  204. data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
  205. data/lib/rubocop/cop/style/symbol_array.rb +35 -15
  206. data/lib/rubocop/cop/style/symbol_proc.rb +36 -0
  207. data/lib/rubocop/cop/style/unpack_first.rb +11 -14
  208. data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
  209. data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
  210. data/lib/rubocop/cop/utils/regexp_ranges.rb +26 -13
  211. data/lib/rubocop/cop/variable_force/assignment.rb +14 -5
  212. data/lib/rubocop/cops_documentation_generator.rb +11 -1
  213. data/lib/rubocop/ext/regexp_node.rb +9 -4
  214. data/lib/rubocop/file_finder.rb +4 -7
  215. data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
  216. data/lib/rubocop/formatter/html_formatter.rb +5 -4
  217. data/lib/rubocop/formatter/json_formatter.rb +0 -1
  218. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  219. data/lib/rubocop/formatter.rb +1 -1
  220. data/lib/rubocop/lsp/routes.rb +42 -19
  221. data/lib/rubocop/lsp/runtime.rb +22 -2
  222. data/lib/rubocop/lsp/server.rb +10 -4
  223. data/lib/rubocop/magic_comment.rb +12 -10
  224. data/lib/rubocop/options.rb +0 -8
  225. data/lib/rubocop/result_cache.rb +4 -1
  226. data/lib/rubocop/rspec/shared_contexts.rb +8 -3
  227. data/lib/rubocop/rspec/support.rb +1 -0
  228. data/lib/rubocop/runner.rb +6 -4
  229. data/lib/rubocop/server/client_command/exec.rb +1 -1
  230. data/lib/rubocop/string_interpreter.rb +3 -3
  231. data/lib/rubocop/target_finder.rb +7 -3
  232. data/lib/rubocop/target_ruby.rb +9 -5
  233. data/lib/rubocop/version.rb +1 -1
  234. data/lib/rubocop.rb +6 -0
  235. metadata +20 -13
  236. /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
@@ -10,7 +10,7 @@ module RuboCop
10
10
  include Util
11
11
 
12
12
  FOLDABLE_TYPES = %i[array hash heredoc send csend].freeze
13
- CLASSLIKE_TYPES = %i[class module sclass].freeze
13
+ CLASSLIKE_TYPES = %i[class module].freeze
14
14
  private_constant :FOLDABLE_TYPES, :CLASSLIKE_TYPES
15
15
 
16
16
  def initialize(node, processed_source, count_comments: false, foldable_types: [])
@@ -63,7 +63,7 @@ module RuboCop
63
63
  types
64
64
  end
65
65
 
66
- def code_length(node)
66
+ def code_length(node) # rubocop:disable Metrics/MethodLength
67
67
  if classlike_node?(node)
68
68
  classlike_code_length(node)
69
69
  elsif heredoc_node?(node)
@@ -72,7 +72,14 @@ module RuboCop
72
72
  body = extract_body(node)
73
73
  return 0 unless body
74
74
 
75
- body.source.each_line.count { |line| !irrelevant_line?(line) }
75
+ source =
76
+ if node_with_heredoc?(body)
77
+ source_from_node_with_heredoc(body)
78
+ else
79
+ body.source.lines
80
+ end
81
+
82
+ source.count { |line| !irrelevant_line?(line) }
76
83
  end
77
84
  end
78
85
 
@@ -138,7 +145,7 @@ module RuboCop
138
145
 
139
146
  def extract_body(node)
140
147
  case node.type
141
- when :class, :module, :block, :numblock, :def, :defs
148
+ when :class, :module, :sclass, :block, :numblock, :def, :defs
142
149
  node.body
143
150
  when :casgn
144
151
  _scope, _name, value = *node
@@ -175,6 +182,27 @@ module RuboCop
175
182
  def another_args?(node)
176
183
  node.call_type? && node.arguments.count > 1
177
184
  end
185
+
186
+ def node_with_heredoc?(node)
187
+ node.each_descendant(:str, :dstr).any? { |descendant| heredoc_node?(descendant) }
188
+ end
189
+
190
+ def source_from_node_with_heredoc(node)
191
+ last_line = -1
192
+ node.each_descendant do |descendant|
193
+ next unless descendant.source
194
+
195
+ descendant_last_line =
196
+ if heredoc_node?(descendant)
197
+ descendant.loc.heredoc_end.line
198
+ else
199
+ descendant.last_line
200
+ end
201
+
202
+ last_line = [last_line, descendant_last_line].max
203
+ end
204
+ @processed_source[(node.first_line - 1)..(last_line - 1)]
205
+ end
178
206
  end
179
207
  end
180
208
  end
@@ -218,7 +218,7 @@ module RuboCop
218
218
 
219
219
  # @api private
220
220
  def already_on_multiple_lines?(node)
221
- return node.first_line != node.arguments.last.last_line if node.def_type?
221
+ return node.first_line != node.last_argument.last_line if node.def_type?
222
222
 
223
223
  !node.single_line?
224
224
  end
@@ -62,25 +62,29 @@ module RuboCop
62
62
  # Returns the end line of a node, which might be a comment and not part of the AST
63
63
  # End line is considered either the line at which another node starts, or
64
64
  # the line at which the parent node ends.
65
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Lint/DuplicateBranch
65
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
66
66
  def find_end_line(node)
67
- if node.if_type? && node.else?
68
- node.loc.else.line
69
- elsif node.if_type? && node.ternary?
70
- node.else_branch.loc.line
71
- elsif node.if_type? && node.elsif?
72
- node.each_ancestor(:if).find(&:if?).loc.end.line
67
+ if node.if_type?
68
+ if node.else?
69
+ node.loc.else.line
70
+ elsif node.ternary?
71
+ node.else_branch.loc.line
72
+ elsif node.elsif?
73
+ node.each_ancestor(:if).find(&:if?).loc.end.line
74
+ end
73
75
  elsif node.block_type? || node.numblock_type?
74
76
  node.loc.end.line
75
77
  elsif (next_sibling = node.right_sibling) && next_sibling.is_a?(AST::Node)
76
78
  next_sibling.loc.line
77
79
  elsif (parent = node.parent)
78
- parent.loc.respond_to?(:end) && parent.loc.end ? parent.loc.end.line : parent.loc.line
79
- else
80
- node.loc.end.line
81
- end
80
+ if parent.loc.respond_to?(:end) && parent.loc.end
81
+ parent.loc.end.line
82
+ else
83
+ parent.loc.line
84
+ end
85
+ end || node.loc.end.line
82
86
  end
83
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Lint/DuplicateBranch
87
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
84
88
  end
85
89
  end
86
90
  end
@@ -18,6 +18,7 @@ module RuboCop
18
18
  alternative_styles.each do |alternative|
19
19
  return unexpected_style_detected(alternative) if valid_name?(node, name, alternative)
20
20
  end
21
+ unrecognized_style_detected
21
22
  end
22
23
 
23
24
  def valid_name?(node, name, given_style = style)
@@ -19,7 +19,7 @@ module RuboCop
19
19
 
20
20
  # @!method non_public_modifier?(node)
21
21
  def_node_matcher :non_public_modifier?, <<~PATTERN
22
- (send nil? {:private :protected} ({def defs} ...))
22
+ (send nil? {:private :protected :private_class_method} ({def defs} ...))
23
23
  PATTERN
24
24
  end
25
25
  end
@@ -48,18 +48,21 @@ module RuboCop
48
48
 
49
49
  def register_offense(node, message, replacement) # rubocop:disable Metrics/AbcSize
50
50
  add_offense(node.value, message: message) do |corrector|
51
- if (def_node = def_node_that_require_parentheses(node))
52
- last_argument = def_node.last_argument
53
- if last_argument.nil? || !last_argument.hash_type?
54
- next corrector.replace(node, replacement)
55
- end
56
-
57
- white_spaces = range_between(def_node.selector.end_pos,
58
- def_node.first_argument.source_range.begin_pos)
59
- corrector.replace(white_spaces, '(')
60
- corrector.insert_after(last_argument, ')') if node == last_argument.pairs.last
61
- end
62
51
  corrector.replace(node, replacement)
52
+
53
+ next unless (def_node = def_node_that_require_parentheses(node))
54
+
55
+ last_argument = def_node.last_argument
56
+ if last_argument.nil? || !last_argument.hash_type?
57
+ next corrector.replace(node, replacement)
58
+ end
59
+
60
+ white_spaces = range_between(def_node.selector.end_pos,
61
+ def_node.first_argument.source_range.begin_pos)
62
+ next if node.parent.braces?
63
+
64
+ corrector.replace(white_spaces, '(')
65
+ corrector.insert_after(last_argument, ')') if node == last_argument.pairs.last
63
66
  end
64
67
  end
65
68
 
@@ -26,11 +26,15 @@ module RuboCop
26
26
  end
27
27
 
28
28
  def delimiter_string(node)
29
- node.source.match(OPENING_DELIMITER).captures[1]
29
+ return '' unless (match = node.source.match(OPENING_DELIMITER))
30
+
31
+ match.captures[1]
30
32
  end
31
33
 
32
34
  def heredoc_type(node)
33
- node.source.match(OPENING_DELIMITER).captures[0]
35
+ return '' unless (match = node.source.match(OPENING_DELIMITER))
36
+
37
+ match.captures[0]
34
38
  end
35
39
  end
36
40
  end
@@ -20,16 +20,17 @@ module RuboCop
20
20
  range = offending_range(node, lhs, rhs, style)
21
21
  check(range, node, lhs, rhs)
22
22
  end
23
+ alias on_csend on_send
23
24
 
24
25
  private
25
26
 
26
- # In a chain of method calls, we regard the top send node as the base
27
+ # In a chain of method calls, we regard the top call node as the base
27
28
  # for indentation of all lines following the first. For example:
28
29
  # a.
29
30
  # b c { block }. <-- b is indented relative to a
30
31
  # d <-- d is indented relative to a
31
32
  def left_hand_side(lhs)
32
- while lhs.parent&.send_type? && lhs.parent.loc.dot && !lhs.parent.assignment_method?
33
+ while lhs.parent&.call_type? && lhs.parent.loc.dot && !lhs.parent.assignment_method?
33
34
  lhs = lhs.parent
34
35
  end
35
36
  lhs
@@ -75,21 +75,19 @@ module RuboCop
75
75
  end
76
76
 
77
77
  def aligned_token?(range, line)
78
- aligned_words?(range, line) ||
79
- aligned_char?(range, line) ||
80
- aligned_assignment?(range, line)
78
+ aligned_words?(range, line) || aligned_assignment?(range, line)
81
79
  end
82
80
 
83
81
  def aligned_operator?(range, line)
84
- (aligned_identical?(range, line) || aligned_assignment?(range, line))
82
+ aligned_identical?(range, line) || aligned_assignment?(range, line)
85
83
  end
86
84
 
87
85
  def aligned_words?(range, line)
88
- /\s\S/.match?(line[range.column - 1, 2])
89
- end
86
+ left_edge = range.column
87
+ return true if /\s\S/.match?(line[left_edge - 1, 2])
90
88
 
91
- def aligned_char?(range, line)
92
- line[range.column] == range.source[0]
89
+ token = range.source
90
+ token == line[left_edge, token.length]
93
91
  end
94
92
 
95
93
  def aligned_assignment?(range, line)
@@ -36,7 +36,7 @@ module RuboCop
36
36
  end
37
37
 
38
38
  def space_required_after?(token)
39
- token.left_curly_brace? && space_required_after_lcurly?
39
+ (token.left_curly_brace? || token.type == :tLAMBEG) && space_required_after_lcurly?
40
40
  end
41
41
 
42
42
  def space_required_after_lcurly?
@@ -30,8 +30,10 @@ module RuboCop
30
30
  private
31
31
 
32
32
  def inside_interpolation?(node)
33
- # A :begin node inside a :dstr node is an interpolation.
34
- node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
33
+ # A :begin node inside a :dstr, :dsym, or :regexp node is an interpolation.
34
+ node.ancestors
35
+ .drop_while { |a| !a.begin_type? }
36
+ .any? { |a| a.dstr_type? || a.dsym_type? || a.regexp_type? }
35
37
  end
36
38
  end
37
39
  end
@@ -106,7 +106,7 @@ module RuboCop
106
106
  end
107
107
 
108
108
  def elements(node)
109
- return node.children unless %i[csend send].include?(node.type)
109
+ return node.children unless node.call_type?
110
110
 
111
111
  node.arguments.flat_map do |argument|
112
112
  # For each argument, if it is a multi-line hash without braces,
@@ -48,24 +48,32 @@ module RuboCop
48
48
  MSG = 'Use %<style>s block forwarding.'
49
49
 
50
50
  def self.autocorrect_incompatible_with
51
- [Lint::AmbiguousOperator]
51
+ [Lint::AmbiguousOperator, Style::ArgumentsForwarding]
52
52
  end
53
53
 
54
+ # rubocop:disable Metrics/CyclomaticComplexity
54
55
  def on_def(node)
55
56
  return if node.arguments.empty?
56
57
 
57
- last_argument = node.arguments.last
58
+ last_argument = node.last_argument
58
59
  return if expected_block_forwarding_style?(node, last_argument)
59
60
 
60
- register_offense(last_argument, node)
61
-
61
+ invalid_syntax = false
62
62
  node.each_descendant(:block_pass) do |block_pass_node|
63
63
  next if block_pass_node.children.first&.sym_type? ||
64
64
  last_argument.source != block_pass_node.source
65
65
 
66
+ if block_pass_node.each_ancestor(:block, :numblock).any?
67
+ invalid_syntax = true
68
+ next
69
+ end
70
+
66
71
  register_offense(block_pass_node, node)
67
72
  end
73
+
74
+ register_offense(last_argument, node) unless invalid_syntax
68
75
  end
76
+ # rubocop:enable Metrics/CyclomaticComplexity
69
77
  alias on_defs on_def
70
78
 
71
79
  private
@@ -61,8 +61,7 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def allowed_method_call_on_rhs?(node)
64
- node&.send_type? &&
65
- (node.receiver.nil? || !literal_receiver?(node))
64
+ node&.send_type? && (node.receiver.nil? || !literal_receiver?(node))
66
65
  end
67
66
 
68
67
  # @!method literal_receiver?(node)
@@ -136,7 +136,7 @@ module RuboCop
136
136
  end
137
137
 
138
138
  def filename_good?(basename)
139
- basename = basename.sub(/^\./, '')
139
+ basename = basename.delete_prefix('.')
140
140
  basename = basename.sub(/\.[^.]+$/, '')
141
141
  # special handling for Action Pack Variants file names like
142
142
  # some_file.xlsx+mobile.axlsx
@@ -31,7 +31,9 @@ module RuboCop
31
31
  def on_heredoc(node)
32
32
  return if meaningful_delimiters?(node)
33
33
 
34
- add_offense(node.loc.heredoc_end)
34
+ range = node.children.empty? ? node : node.loc.heredoc_end
35
+
36
+ add_offense(range)
35
37
  end
36
38
 
37
39
  private
@@ -203,7 +203,7 @@ module RuboCop
203
203
 
204
204
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
205
205
  def on_defined?(node)
206
- arg = node.arguments.first
206
+ arg = node.first_argument
207
207
  return false unless arg.ivar_type?
208
208
 
209
209
  method_node, method_name = find_definition(node)
@@ -240,12 +240,12 @@ module RuboCop
240
240
  self
241
241
  end
242
242
 
243
- def select(&block)
244
- cops.select(&block)
243
+ def select(...)
244
+ cops.select(...)
245
245
  end
246
246
 
247
- def each(&block)
248
- cops.each(&block)
247
+ def each(...)
248
+ cops.each(...)
249
249
  end
250
250
 
251
251
  # @param [String] cop_name
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # # bad
24
24
  # open(something)
25
25
  # open("| #{something}")
26
+ # open("| foo")
26
27
  # URI.open(something)
27
28
  #
28
29
  # # good
@@ -32,7 +33,6 @@ module RuboCop
32
33
  #
33
34
  # # good (literal strings)
34
35
  # open("foo.text")
35
- # open("| foo")
36
36
  # URI.open("http://example.com")
37
37
  class Open < Base
38
38
  MSG = 'The use of `%<receiver>sopen` is a serious security risk.'
@@ -40,7 +40,7 @@ module RuboCop
40
40
 
41
41
  # @!method open?(node)
42
42
  def_node_matcher :open?, <<~PATTERN
43
- (send ${nil? (const {nil? cbase} :URI)} :open $!str ...)
43
+ (send ${nil? (const {nil? cbase} :URI)} :open $_ ...)
44
44
  PATTERN
45
45
 
46
46
  def on_send(node)
@@ -168,12 +168,12 @@ module RuboCop
168
168
 
169
169
  def find_corresponding_def_node(node)
170
170
  if access_modifier_with_symbol?(node)
171
- method_name = node.arguments.first.value
171
+ method_name = node.first_argument.value
172
172
  node.parent.each_child_node(:def).find do |child|
173
173
  child.method?(method_name)
174
174
  end
175
175
  else
176
- node.arguments.first
176
+ node.first_argument
177
177
  end
178
178
  end
179
179
 
@@ -166,7 +166,7 @@ module RuboCop
166
166
  *processed_source.ast_with_comments[arg].map(&:text),
167
167
  "#{node.method_name} #{arg.source}"
168
168
  ]
169
- if arg == node.arguments.first
169
+ if arg == node.first_argument
170
170
  lines
171
171
  else
172
172
  indent = ' ' * node.loc.column
@@ -122,7 +122,7 @@ module RuboCop
122
122
  end
123
123
 
124
124
  def bareword?(sym_node)
125
- !sym_node.source.start_with?(':')
125
+ !sym_node.source.start_with?(':') || sym_node.dsym_type?
126
126
  end
127
127
 
128
128
  def correct_alias_method_to_alias(corrector, send_node)
@@ -134,9 +134,7 @@ module RuboCop
134
134
 
135
135
  def correct_alias_to_alias_method(corrector, node)
136
136
  replacement =
137
- 'alias_method ' \
138
- ":#{identifier(node.new_identifier)}, " \
139
- ":#{identifier(node.old_identifier)}"
137
+ "alias_method #{identifier(node.new_identifier)}, #{identifier(node.old_identifier)}"
140
138
 
141
139
  corrector.replace(node, replacement)
142
140
  end
@@ -146,10 +144,13 @@ module RuboCop
146
144
  corrector.replace(node.old_identifier, node.old_identifier.source[1..])
147
145
  end
148
146
 
149
- # @!method identifier(node)
150
- def_node_matcher :identifier, <<~PATTERN
151
- (sym $_)
152
- PATTERN
147
+ def identifier(node)
148
+ if node.sym_type?
149
+ ":#{node.children.first}"
150
+ else
151
+ node.source
152
+ end
153
+ end
153
154
  end
154
155
  end
155
156
  end