rubocop 1.66.1 → 1.69.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +96 -12
  4. data/config/internal_affairs.yml +11 -0
  5. data/lib/rubocop/cached_data.rb +12 -4
  6. data/lib/rubocop/cli/command/auto_generate_config.rb +6 -7
  7. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  8. data/lib/rubocop/cli/command/lsp.rb +2 -2
  9. data/lib/rubocop/cli/command/version.rb +2 -2
  10. data/lib/rubocop/config_loader_resolver.rb +3 -3
  11. data/lib/rubocop/config_validator.rb +2 -1
  12. data/lib/rubocop/cop/autocorrect_logic.rb +22 -2
  13. data/lib/rubocop/cop/base.rb +7 -3
  14. data/lib/rubocop/cop/bundler/gem_filename.rb +0 -1
  15. data/lib/rubocop/cop/bundler/gem_version.rb +1 -0
  16. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +0 -1
  17. data/lib/rubocop/cop/cop.rb +8 -0
  18. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -12
  19. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +1 -1
  20. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  21. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +10 -0
  22. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -2
  23. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +0 -2
  24. data/lib/rubocop/cop/generator.rb +6 -0
  25. data/lib/rubocop/cop/internal_affairs/cop_description.rb +0 -4
  26. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -4
  27. data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +1 -1
  28. data/lib/rubocop/cop/internal_affairs/operator_keyword.rb +46 -0
  29. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +6 -21
  30. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +8 -1
  31. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +0 -2
  32. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +0 -5
  33. data/lib/rubocop/cop/internal_affairs.rb +17 -0
  34. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +5 -1
  35. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -2
  36. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  37. data/lib/rubocop/cop/layout/begin_end_alignment.rb +0 -1
  38. data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
  39. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  40. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
  41. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -3
  42. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -4
  43. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -1
  44. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +8 -0
  45. data/lib/rubocop/cop/layout/indentation_width.rb +11 -12
  46. data/lib/rubocop/cop/layout/leading_comment_space.rb +71 -1
  47. data/lib/rubocop/cop/layout/line_length.rb +118 -4
  48. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  49. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  50. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -3
  51. data/lib/rubocop/cop/layout/parameter_alignment.rb +3 -4
  52. data/lib/rubocop/cop/layout/redundant_line_break.rb +3 -35
  53. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -2
  54. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  55. data/lib/rubocop/cop/layout/space_around_operators.rb +16 -17
  56. data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -5
  57. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +6 -0
  58. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +4 -0
  59. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +4 -0
  60. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +0 -1
  61. data/lib/rubocop/cop/lint/ambiguous_range.rb +4 -1
  62. data/lib/rubocop/cop/lint/big_decimal_new.rb +4 -7
  63. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +10 -12
  64. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  65. data/lib/rubocop/cop/lint/circular_argument_reference.rb +6 -0
  66. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +2 -1
  67. data/lib/rubocop/cop/lint/duplicate_branch.rb +39 -4
  68. data/lib/rubocop/cop/lint/duplicate_set_element.rb +74 -0
  69. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  70. data/lib/rubocop/cop/lint/empty_file.rb +0 -2
  71. data/lib/rubocop/cop/lint/ensure_return.rb +1 -4
  72. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  73. data/lib/rubocop/cop/lint/float_comparison.rb +15 -7
  74. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -3
  75. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +55 -0
  76. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +10 -4
  77. data/lib/rubocop/cop/lint/interpolation_check.rb +9 -0
  78. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +8 -14
  79. data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -0
  80. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +1 -1
  81. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +25 -2
  82. data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -5
  83. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  84. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +2 -2
  85. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +8 -1
  86. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -3
  87. data/lib/rubocop/cop/lint/number_conversion.rb +0 -1
  88. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -2
  89. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +106 -0
  90. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -2
  91. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
  92. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +5 -6
  93. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  94. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +13 -8
  95. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +8 -7
  96. data/lib/rubocop/cop/lint/refinement_import_methods.rb +1 -1
  97. data/lib/rubocop/cop/lint/regexp_as_condition.rb +0 -1
  98. data/lib/rubocop/cop/lint/rescue_type.rb +3 -7
  99. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +9 -0
  100. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +109 -41
  101. data/lib/rubocop/cop/lint/self_assignment.rb +8 -10
  102. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  103. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  104. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +91 -0
  105. data/lib/rubocop/cop/lint/unreachable_code.rb +51 -2
  106. data/lib/rubocop/cop/lint/unused_method_argument.rb +18 -2
  107. data/lib/rubocop/cop/lint/uri_regexp.rb +25 -7
  108. data/lib/rubocop/cop/lint/useless_defined.rb +55 -0
  109. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +4 -0
  110. data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
  111. data/lib/rubocop/cop/lint/useless_setter_call.rb +14 -25
  112. data/lib/rubocop/cop/lint/void.rb +3 -2
  113. data/lib/rubocop/cop/metrics/class_length.rb +7 -7
  114. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +4 -1
  115. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  116. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -3
  117. data/lib/rubocop/cop/mixin/check_assignment.rb +4 -12
  118. data/lib/rubocop/cop/mixin/check_line_breakable.rb +10 -0
  119. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +49 -0
  120. data/lib/rubocop/cop/mixin/dig_help.rb +27 -0
  121. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +24 -0
  122. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +3 -1
  123. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +5 -9
  124. data/lib/rubocop/cop/mixin/percent_array.rb +1 -1
  125. data/lib/rubocop/cop/mixin/range_help.rb +0 -1
  126. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -2
  127. data/lib/rubocop/cop/mixin/target_ruby_version.rb +17 -1
  128. data/lib/rubocop/cop/naming/accessor_method_name.rb +6 -6
  129. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  130. data/lib/rubocop/cop/naming/constant_name.rb +6 -7
  131. data/lib/rubocop/cop/naming/file_name.rb +0 -2
  132. data/lib/rubocop/cop/naming/inclusive_language.rb +12 -3
  133. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +11 -12
  134. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  135. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -11
  136. data/lib/rubocop/cop/naming/variable_name.rb +3 -4
  137. data/lib/rubocop/cop/naming/variable_number.rb +2 -3
  138. data/lib/rubocop/cop/offense.rb +4 -5
  139. data/lib/rubocop/cop/security/compound_hash.rb +1 -0
  140. data/lib/rubocop/cop/security/yaml_load.rb +3 -2
  141. data/lib/rubocop/cop/style/access_modifier_declarations.rb +64 -25
  142. data/lib/rubocop/cop/style/accessor_grouping.rb +10 -2
  143. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +79 -0
  144. data/lib/rubocop/cop/style/arguments_forwarding.rb +46 -6
  145. data/lib/rubocop/cop/style/array_intersect.rb +5 -4
  146. data/lib/rubocop/cop/style/bitwise_predicate.rb +100 -0
  147. data/lib/rubocop/cop/style/block_delimiters.rb +41 -5
  148. data/lib/rubocop/cop/style/case_like_if.rb +8 -11
  149. data/lib/rubocop/cop/style/collection_compact.rb +10 -10
  150. data/lib/rubocop/cop/style/combinable_defined.rb +115 -0
  151. data/lib/rubocop/cop/style/combinable_loops.rb +7 -0
  152. data/lib/rubocop/cop/style/commented_keyword.rb +17 -1
  153. data/lib/rubocop/cop/style/conditional_assignment.rb +20 -22
  154. data/lib/rubocop/cop/style/constant_visibility.rb +3 -12
  155. data/lib/rubocop/cop/style/data_inheritance.rb +1 -1
  156. data/lib/rubocop/cop/style/dig_chain.rb +89 -0
  157. data/lib/rubocop/cop/style/endless_method.rb +1 -14
  158. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  159. data/lib/rubocop/cop/style/fetch_env_var.rb +1 -0
  160. data/lib/rubocop/cop/style/file_null.rb +73 -0
  161. data/lib/rubocop/cop/style/file_touch.rb +75 -0
  162. data/lib/rubocop/cop/style/for.rb +0 -1
  163. data/lib/rubocop/cop/style/global_vars.rb +1 -3
  164. data/lib/rubocop/cop/style/guard_clause.rb +16 -3
  165. data/lib/rubocop/cop/style/hash_conversion.rb +1 -2
  166. data/lib/rubocop/cop/style/hash_each_methods.rb +6 -0
  167. data/lib/rubocop/cop/style/hash_except.rb +19 -7
  168. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  169. data/lib/rubocop/cop/style/if_inside_else.rb +1 -2
  170. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -2
  171. data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -7
  172. data/lib/rubocop/cop/style/inverse_methods.rb +0 -1
  173. data/lib/rubocop/cop/style/keyword_arguments_merging.rb +67 -0
  174. data/lib/rubocop/cop/style/lambda.rb +1 -1
  175. data/lib/rubocop/cop/style/lambda_call.rb +3 -2
  176. data/lib/rubocop/cop/style/map_into_array.rb +59 -8
  177. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +13 -8
  178. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +7 -11
  179. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +33 -3
  180. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  181. data/lib/rubocop/cop/style/multiple_comparison.rb +28 -39
  182. data/lib/rubocop/cop/style/mutable_constant.rb +4 -5
  183. data/lib/rubocop/cop/style/negated_if_else_condition.rb +6 -4
  184. data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
  185. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  186. data/lib/rubocop/cop/style/nested_ternary_operator.rb +5 -4
  187. data/lib/rubocop/cop/style/not.rb +1 -1
  188. data/lib/rubocop/cop/style/object_then.rb +1 -0
  189. data/lib/rubocop/cop/style/one_line_conditional.rb +29 -4
  190. data/lib/rubocop/cop/style/operator_method_call.rb +25 -7
  191. data/lib/rubocop/cop/style/or_assignment.rb +3 -6
  192. data/lib/rubocop/cop/style/parallel_assignment.rb +8 -13
  193. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  194. data/lib/rubocop/cop/style/redundant_argument.rb +3 -1
  195. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -1
  196. data/lib/rubocop/cop/style/redundant_begin.rb +4 -0
  197. data/lib/rubocop/cop/style/redundant_condition.rb +37 -22
  198. data/lib/rubocop/cop/style/redundant_line_continuation.rb +24 -5
  199. data/lib/rubocop/cop/style/redundant_parentheses.rb +11 -13
  200. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +1 -0
  201. data/lib/rubocop/cop/style/redundant_return.rb +2 -2
  202. data/lib/rubocop/cop/style/redundant_self.rb +8 -15
  203. data/lib/rubocop/cop/style/redundant_self_assignment.rb +7 -5
  204. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +4 -4
  205. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  206. data/lib/rubocop/cop/style/require_order.rb +1 -1
  207. data/lib/rubocop/cop/style/rescue_modifier.rb +15 -4
  208. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +54 -12
  209. data/lib/rubocop/cop/style/safe_navigation.rb +104 -50
  210. data/lib/rubocop/cop/style/safe_navigation_chain_length.rb +52 -0
  211. data/lib/rubocop/cop/style/select_by_regexp.rb +10 -7
  212. data/lib/rubocop/cop/style/self_assignment.rb +11 -17
  213. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  214. data/lib/rubocop/cop/style/signal_exception.rb +2 -3
  215. data/lib/rubocop/cop/style/single_argument_dig.rb +9 -5
  216. data/lib/rubocop/cop/style/single_line_do_end_block.rb +13 -3
  217. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -3
  218. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  219. data/lib/rubocop/cop/style/string_concatenation.rb +13 -12
  220. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  221. data/lib/rubocop/cop/style/swap_values.rb +4 -15
  222. data/lib/rubocop/cop/style/ternary_parentheses.rb +26 -5
  223. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -4
  224. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  225. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -2
  226. data/lib/rubocop/cop/team.rb +8 -1
  227. data/lib/rubocop/cop/util.rb +1 -1
  228. data/lib/rubocop/cop/variable_force/assignment.rb +18 -3
  229. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  230. data/lib/rubocop/cop/variable_force/variable.rb +5 -1
  231. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  232. data/lib/rubocop/cop/variable_force.rb +4 -10
  233. data/lib/rubocop/cops_documentation_generator.rb +90 -41
  234. data/lib/rubocop/file_finder.rb +9 -4
  235. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  236. data/lib/rubocop/lsp/runtime.rb +2 -0
  237. data/lib/rubocop/lsp/server.rb +0 -1
  238. data/lib/rubocop/rspec/expect_offense.rb +1 -0
  239. data/lib/rubocop/runner.rb +17 -6
  240. data/lib/rubocop/server/cache.rb +6 -1
  241. data/lib/rubocop/server/core.rb +1 -0
  242. data/lib/rubocop/target_ruby.rb +13 -13
  243. data/lib/rubocop/version.rb +28 -7
  244. data/lib/rubocop/yaml_duplication_checker.rb +20 -27
  245. data/lib/rubocop.rb +18 -0
  246. metadata +32 -14
@@ -71,11 +71,17 @@ module RuboCop
71
71
  (send nil? :bad_method ...)
72
72
  PATTERN
73
73
 
74
+ # Called on every `send` node (method call) while walking the AST.
75
+ # TODO: remove this method if inspecting `send` nodes is unneeded for your cop.
76
+ # By default, this is aliased to `on_csend` as well to handle method calls
77
+ # with safe navigation, remove the alias if this is unnecessary.
78
+ # If kept, ensure your tests cover safe navigation as well!
74
79
  def on_send(node)
75
80
  return unless bad_method?(node)
76
81
 
77
82
  add_offense(node)
78
83
  end
84
+ alias on_csend on_send
79
85
  end
80
86
  end
81
87
  end
@@ -112,10 +112,6 @@ module RuboCop
112
112
  body = comment_body(comment_line)
113
113
  node.source.index(body)
114
114
  end
115
-
116
- def relevant_file?(file)
117
- file.match?(%r{/cop/.*\.rb\z})
118
- end
119
115
  end
120
116
  end
121
117
  end
@@ -19,6 +19,7 @@ module RuboCop
19
19
  extend AutoCorrector
20
20
 
21
21
  MSG = 'Use `%<preferred>s`.'
22
+ RESTRICT_ON_SEND = [:==].freeze
22
23
 
23
24
  # @!method line_send(node)
24
25
  def_node_matcher :line_send, <<~PATTERN
@@ -36,10 +37,8 @@ module RuboCop
36
37
  def on_send(node)
37
38
  return unless location_line_equality_comparison?(node)
38
39
 
39
- lhs, _op, rhs = *node
40
-
41
- lhs_receiver = extract_receiver(lhs)
42
- rhs_receiver = extract_receiver(rhs)
40
+ lhs_receiver = extract_receiver(node.receiver)
41
+ rhs_receiver = extract_receiver(node.first_argument)
43
42
  preferred = "same_line?(#{lhs_receiver}, #{rhs_receiver})"
44
43
 
45
44
  add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module InternalAffairs
6
- # Checks for missing `numblock handlers. The blocks with numbered
6
+ # Checks for missing `numblock` handlers. The blocks with numbered
7
7
  # arguments introduced in Ruby 2.7 are parsed with a node type of
8
8
  # `numblock` instead of block. Cops that define `block` handlers
9
9
  # need to define `numblock` handlers or disable this cope for them.
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Enforces the use of `node.operator_keyword?` instead of `node.and_type? || node.or_type?`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # node.and_type? || node.or_type?
11
+ # node.or_type? || node.and_type?
12
+ #
13
+ # # good
14
+ # node.operator_keyword?
15
+ #
16
+ class OperatorKeyword < Base
17
+ extend AutoCorrector
18
+
19
+ MSG = 'Use `%<prefer>s`.'
20
+
21
+ # @!method and_or_type(node)
22
+ def_node_matcher :and_or_type, <<~PATTERN
23
+ {
24
+ (or $(send _node :and_type?) $(send _node :or_type?))
25
+ (or $(send _node :or_type?) $(send _node :and_type?))
26
+ (or
27
+ (or _ $(send _node :and_type?)) $(send _node :or_type?))
28
+ (or
29
+ (or _ $(send _node :or_type?)) $(send _node :and_type?))
30
+ }
31
+ PATTERN
32
+
33
+ def on_or(node)
34
+ return unless (lhs, rhs = and_or_type(node))
35
+
36
+ offense = lhs.receiver.source_range.join(rhs.source_range.end)
37
+ prefer = "#{lhs.receiver.source}.operator_keyword?"
38
+
39
+ add_offense(offense, message: format(MSG, prefer: prefer)) do |corrector|
40
+ corrector.replace(offense, prefer)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -11,13 +11,9 @@ module RuboCop
11
11
  #
12
12
  # # bad
13
13
  # add_offense(node, message: MSG)
14
- # add_offense(node, message: message)
15
- # add_offense(node, message: message(node))
16
14
  #
17
15
  # # good
18
16
  # add_offense(node)
19
- # add_offense(node, message: CUSTOM_MSG)
20
- # add_offense(node, message: message(other_node))
21
17
  #
22
18
  class RedundantMessageArgument < Base
23
19
  include RangeHelp
@@ -28,22 +24,21 @@ module RuboCop
28
24
 
29
25
  # @!method node_type_check(node)
30
26
  def_node_matcher :node_type_check, <<~PATTERN
31
- (send nil? :add_offense $_node $hash)
27
+ (send nil? :add_offense _node $hash)
32
28
  PATTERN
33
29
 
34
30
  # @!method redundant_message_argument(node)
35
31
  def_node_matcher :redundant_message_argument, <<~PATTERN
36
32
  (pair
37
33
  (sym :message)
38
- ${(const nil? :MSG) (send nil? :message) (send nil? :message _)})
34
+ $(const nil? :MSG))
39
35
  PATTERN
40
36
 
41
- # @!method message_method_call(node)
42
- def_node_matcher :message_method_call, '(send nil? :message $_node)'
43
-
44
37
  def on_send(node)
45
- node_type_check(node) do |node_arg, kwargs|
46
- find_offending_argument(node_arg, kwargs) do |pair|
38
+ return unless (kwargs = node_type_check(node))
39
+
40
+ kwargs.pairs.each do |pair|
41
+ redundant_message_argument(pair) do
47
42
  add_offense(pair) do |corrector|
48
43
  range = offending_range(pair)
49
44
 
@@ -60,16 +55,6 @@ module RuboCop
60
55
 
61
56
  range_with_surrounding_comma(with_space, :left)
62
57
  end
63
-
64
- def find_offending_argument(searched_node, kwargs)
65
- kwargs.pairs.each do |pair|
66
- redundant_message_argument(pair) do |message_argument|
67
- node = message_method_call(message_argument)
68
-
69
- yield pair if !node || node == searched_node
70
- end
71
- end
72
- end
73
58
  end
74
59
  end
75
60
  end
@@ -14,6 +14,12 @@ module RuboCop
14
14
  # node.source
15
15
  #
16
16
  # # bad
17
+ # add_offense(node.source_range)
18
+ #
19
+ # # good
20
+ # add_offense(node)
21
+ #
22
+ # # bad
17
23
  # add_offense(node) { |corrector| corrector.replace(node.source_range, prefer) }
18
24
  # add_offense(node) { |corrector| corrector.insert_before(node.source_range, prefer) }
19
25
  # add_offense(node) { |corrector| corrector.insert_before_multi(node.source_range, prefer) }
@@ -34,7 +40,7 @@ module RuboCop
34
40
 
35
41
  MSG = 'Remove the redundant `source_range`.'
36
42
  RESTRICT_ON_SEND = %i[
37
- source
43
+ source add_offense
38
44
  replace remove insert_before insert_before_multi insert_after insert_after_multi swap
39
45
  ].freeze
40
46
 
@@ -42,6 +48,7 @@ module RuboCop
42
48
  def_node_matcher :redundant_source_range, <<~PATTERN
43
49
  {
44
50
  (send $(send _ :source_range) :source)
51
+ (send nil? :add_offense $(send _ :source_range) ...)
45
52
  (send _ {
46
53
  :replace :insert_before :insert_before_multi :insert_after :insert_after_multi
47
54
  } $(send _ :source_range) _)
@@ -50,8 +50,6 @@ module RuboCop
50
50
  # end
51
51
  #
52
52
  class StyleDetectedApiUse < Base
53
- include RangeHelp
54
-
55
53
  MSG_FOR_POSITIVE_WITHOUT_NEGATIVE =
56
54
  '`correct_style_detected` method called without ' \
57
55
  'calling a negative `*_style_detected` method.'
@@ -43,11 +43,6 @@ module RuboCop
43
43
  node.ancestors.any? { |ancestor| rspec_expectation_on_msg?(ancestor) }
44
44
  end
45
45
  end
46
-
47
- # Only process spec files
48
- def relevant_file?(file)
49
- file.end_with?('_spec.rb')
50
- end
51
46
  end
52
47
  end
53
48
  end
@@ -17,6 +17,7 @@ require_relative 'internal_affairs/node_matcher_directive'
17
17
  require_relative 'internal_affairs/node_type_predicate'
18
18
  require_relative 'internal_affairs/numblock_handler'
19
19
  require_relative 'internal_affairs/offense_location_keyword'
20
+ require_relative 'internal_affairs/operator_keyword'
20
21
  require_relative 'internal_affairs/processed_source_buffer_name'
21
22
  require_relative 'internal_affairs/redundant_context_config_parameter'
22
23
  require_relative 'internal_affairs/redundant_described_class_as_subject'
@@ -31,3 +32,19 @@ require_relative 'internal_affairs/style_detected_api_use'
31
32
  require_relative 'internal_affairs/undefined_config'
32
33
  require_relative 'internal_affairs/useless_message_assertion'
33
34
  require_relative 'internal_affairs/useless_restrict_on_send'
35
+
36
+ module RuboCop
37
+ # Patch in the InternalAffairs specific config values
38
+ module InternalAffairs
39
+ def self.inject!
40
+ path = File.join(ConfigLoader::RUBOCOP_HOME, 'config', 'internal_affairs.yml')
41
+ hash = ConfigLoader.load_yaml_configuration(path)
42
+ config = Config.new(hash, path)
43
+ puts "configuration from #{path}" if ConfigLoader.debug?
44
+ config = ConfigLoader.merge_with_default(config, path)
45
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
46
+ end
47
+ end
48
+ end
49
+
50
+ RuboCop::InternalAffairs.inject!
@@ -59,7 +59,11 @@ module RuboCop
59
59
  modifiers = body.each_child_node(:send).select(&:bare_access_modifier?)
60
60
  end_range = node.loc.end
61
61
 
62
- modifiers.each { |modifier| check_modifier(modifier, end_range) }
62
+ modifiers.each do |modifier|
63
+ next if same_line?(node, modifier)
64
+
65
+ check_modifier(modifier, end_range)
66
+ end
63
67
  end
64
68
 
65
69
  def check_modifier(send_node, end_range)
@@ -3,8 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # Here we check if the arguments on a multi-line method
7
- # definition are aligned.
6
+ # Check that the arguments on a multi-line method definition are aligned.
8
7
  #
9
8
  # @example EnforcedStyle: with_first_argument (default)
10
9
  # # good
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # Here we check if the elements of a multi-line array literal are
6
+ # Check that the elements of a multi-line array literal are
7
7
  # aligned.
8
8
  #
9
9
  # @example EnforcedStyle: with_first_element (default)
@@ -40,7 +40,6 @@ module RuboCop
40
40
  #
41
41
  class BeginEndAlignment < Base
42
42
  include EndKeywordAlignment
43
- include RangeHelp
44
43
  extend AutoCorrector
45
44
 
46
45
  MSG = '`end` at %d, %d is not aligned with `%s` at %d, %d.'
@@ -127,7 +127,6 @@ module RuboCop
127
127
  start_loc,
128
128
  end_loc,
129
129
  do_source_line_column)
130
-
131
130
  error_source_line_column = if style == :start_of_block
132
131
  do_source_line_column
133
132
  else
@@ -189,7 +188,7 @@ module RuboCop
189
188
  # In offense message, we want to show the assignment LHS rather than
190
189
  # the entire assignment.
191
190
  def find_lhs_node(node)
192
- node, = *node while node.op_asgn_type? || node.masgn_type?
191
+ node = node.lhs while node.op_asgn_type? || node.masgn_type?
193
192
  node
194
193
  end
195
194
 
@@ -61,7 +61,7 @@ module RuboCop
61
61
  private
62
62
 
63
63
  def autocorrect(corrector, node)
64
- if style == :start_of_line && node.parent && node.parent.send_type?
64
+ if style == :start_of_line && node.parent&.send_type?
65
65
  AlignmentCorrector.align_end(corrector, processed_source, node, node.parent)
66
66
  else
67
67
  AlignmentCorrector.align_end(corrector, processed_source, node, node)
@@ -135,7 +135,7 @@ module RuboCop
135
135
 
136
136
  parent = next_sibling.parent
137
137
 
138
- parent&.if_type? && parent&.else?
138
+ parent&.if_type? && parent.else?
139
139
  end
140
140
 
141
141
  def next_sibling_empty_or_guard_clause?(node)
@@ -200,7 +200,7 @@ module RuboCop
200
200
  parent = node.parent
201
201
  return false unless parent
202
202
 
203
- parent.begin_type? && parent.single_line?
203
+ parent.begin_type? && same_line?(node, node.right_sibling)
204
204
  end
205
205
 
206
206
  # SimpleCov excludes code from the coverage report by wrapping it in `# :nocov:`:
@@ -84,9 +84,8 @@ module RuboCop
84
84
 
85
85
  alias on_numblock on_block
86
86
 
87
- def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
88
- return unless node.bare_access_modifier? &&
89
- !(node.parent&.block_type? || node.parent&.numblock_type?)
87
+ def on_send(node)
88
+ return unless node.bare_access_modifier? && !node.block_literal?
90
89
  return if expected_empty_lines?(node)
91
90
 
92
91
  message = message(node)
@@ -72,8 +72,7 @@ module RuboCop
72
72
  alias on_numblock on_def
73
73
 
74
74
  def on_kwbegin(node)
75
- body, = *node
76
- check_body(body, node.loc.line)
75
+ check_body(node.children.first, node.loc.line)
77
76
  end
78
77
 
79
78
  private
@@ -128,10 +127,10 @@ module RuboCop
128
127
  end
129
128
 
130
129
  def keyword_locations_in_ensure(node)
131
- ensure_body, = *node
130
+ rescue_body_without_ensure = node.children.first
132
131
  [
133
132
  node.loc.keyword,
134
- *keyword_locations(ensure_body)
133
+ *keyword_locations(rescue_body_without_ensure)
135
134
  ]
136
135
  end
137
136
  end
@@ -27,7 +27,9 @@ module RuboCop
27
27
  KIND = 'method'
28
28
 
29
29
  def on_def(node)
30
- check(node, node.body)
30
+ first_line = node.arguments.source_range&.last_line
31
+
32
+ check(node, node.body, adjusted_first_line: first_line)
31
33
  end
32
34
  alias on_defs on_def
33
35
 
@@ -63,13 +63,21 @@ module RuboCop
63
63
  # }
64
64
  # )
65
65
  #
66
+ # @example AllowedMethods: ['some_method']
67
+ #
68
+ # # good
69
+ # some_method(foo, bar,
70
+ # baz)
66
71
  class FirstMethodArgumentLineBreak < Base
67
72
  include FirstElementLineBreak
73
+ include AllowedMethods
68
74
  extend AutoCorrector
69
75
 
70
76
  MSG = 'Add a line break before the first argument of a multi-line method argument list.'
71
77
 
72
78
  def on_send(node)
79
+ return if allowed_method?(node.method_name)
80
+
73
81
  args = node.arguments.dup
74
82
 
75
83
  # If there is a trailing hash arg without explicit braces, like this:
@@ -3,13 +3,12 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # Checks for indentation that doesn't use the specified number
7
- # of spaces.
6
+ # Checks for indentation that doesn't use the specified number of spaces.
7
+ # The indentation width can be configured using the `Width` setting. The default width is 2.
8
8
  #
9
- # See also the IndentationConsistency cop which is the companion to this
10
- # one.
9
+ # See also the `Layout/IndentationConsistency` cop which is the companion to this one.
11
10
  #
12
- # @example
11
+ # @example Width: 2 (default)
13
12
  # # bad
14
13
  # class A
15
14
  # def test
@@ -59,16 +58,17 @@ module RuboCop
59
58
  PATTERN
60
59
 
61
60
  def on_rescue(node)
62
- _begin_node, *_rescue_nodes, else_node = *node
63
- check_indentation(node.loc.else, else_node)
61
+ check_indentation(node.loc.else, node.else_branch)
64
62
  end
65
63
 
66
- def on_ensure(node)
64
+ def on_resbody(node)
67
65
  check_indentation(node.loc.keyword, node.body)
68
66
  end
67
+ alias on_for on_resbody
69
68
 
70
- alias on_resbody on_ensure
71
- alias on_for on_ensure
69
+ def on_ensure(node)
70
+ check_indentation(node.loc.keyword, node.branch)
71
+ end
72
72
 
73
73
  def on_kwbegin(node)
74
74
  # Check indentation against end keyword but only if it's first on its
@@ -326,8 +326,7 @@ module RuboCop
326
326
  if body_node.rescue_type?
327
327
  check_rescue?(body_node)
328
328
  elsif body_node.ensure_type?
329
- block_body, = *body_node
330
-
329
+ block_body, = *body_node # rubocop:disable InternalAffairs/NodeDestructuring
331
330
  if block_body&.rescue_type?
332
331
  check_rescue?(block_body)
333
332
  else
@@ -49,18 +49,58 @@ module RuboCop
49
49
  # #ruby=2.7.0
50
50
  # #ruby-gemset=myproject
51
51
  #
52
+ # @example AllowRBSInlineAnnotation: false (default)
53
+ #
54
+ # # bad
55
+ #
56
+ # include Enumerable #[Integer]
57
+ #
58
+ # attr_reader :name #: String
59
+ # attr_reader :age #: Integer?
60
+ #
61
+ # @example AllowRBSInlineAnnotation: true
62
+ #
63
+ # # good
64
+ #
65
+ # include Enumerable #[Integer]
66
+ #
67
+ # attr_reader :name #: String
68
+ # attr_reader :age #: Integer?
69
+ #
70
+ # @example AllowSteepAnnotation: false (default)
71
+ #
72
+ # # bad
73
+ # [1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
74
+ # list << n
75
+ # end
76
+ #
77
+ # name = 'John' #: String
78
+ #
79
+ # @example AllowSteepAnnotation: true
80
+ #
81
+ # # good
82
+ #
83
+ # [1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
84
+ # list << n
85
+ # end
86
+ #
87
+ # name = 'John' #: String
88
+ #
52
89
  class LeadingCommentSpace < Base
53
90
  include RangeHelp
54
91
  extend AutoCorrector
55
92
 
56
93
  MSG = 'Missing space after `#`.'
57
94
 
58
- def on_new_investigation
95
+ def on_new_investigation # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
59
96
  processed_source.comments.each do |comment|
60
97
  next unless /\A(?!#\+\+|#--)(#+[^#\s=])/.match?(comment.text)
61
98
  next if comment.loc.line == 1 && allowed_on_first_line?(comment)
99
+ next if shebang_continuation?(comment)
62
100
  next if doxygen_comment_style?(comment)
63
101
  next if gemfile_ruby_comment?(comment)
102
+ next if rbs_inline_annotation?(comment)
103
+ next if steep_annotation?(comment)
64
104
 
65
105
  add_offense(comment) do |corrector|
66
106
  expr = comment.source_range
@@ -84,6 +124,20 @@ module RuboCop
84
124
  comment.text.start_with?('#!')
85
125
  end
86
126
 
127
+ def shebang_continuation?(comment)
128
+ return false unless shebang?(comment)
129
+ return true if comment.loc.line == 1
130
+
131
+ previous_line_comment = processed_source.comment_at_line(comment.loc.line - 1)
132
+ return false unless previous_line_comment
133
+
134
+ # If the comment is a shebang but not on the first line, check if the previous
135
+ # line has a shebang comment that wasn't marked as an offense; if so, this comment
136
+ # continues the shebang and is acceptable.
137
+ shebang?(previous_line_comment) &&
138
+ !current_offense_locations.include?(previous_line_comment.source_range)
139
+ end
140
+
87
141
  def rackup_options?(comment)
88
142
  comment.text.start_with?('#\\')
89
143
  end
@@ -115,6 +169,22 @@ module RuboCop
115
169
  def gemfile_ruby_comment?(comment)
116
170
  allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment)
117
171
  end
172
+
173
+ def allow_rbs_inline_annotation?
174
+ cop_config['AllowRBSInlineAnnotation']
175
+ end
176
+
177
+ def rbs_inline_annotation?(comment)
178
+ allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]/)
179
+ end
180
+
181
+ def allow_steep_annotation?
182
+ cop_config['AllowSteepAnnotation']
183
+ end
184
+
185
+ def steep_annotation?(comment)
186
+ allow_steep_annotation? && comment.text.start_with?(/#[$:]/)
187
+ end
118
188
  end
119
189
  end
120
190
  end