rubocop 1.18.1 → 1.22.3

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 (249) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +116 -23
  4. data/lib/rubocop/cli.rb +18 -0
  5. data/lib/rubocop/config.rb +5 -0
  6. data/lib/rubocop/config_loader.rb +5 -3
  7. data/lib/rubocop/config_loader_resolver.rb +22 -7
  8. data/lib/rubocop/config_validator.rb +27 -6
  9. data/lib/rubocop/cop/base.rb +3 -3
  10. data/lib/rubocop/cop/bundler/gem_comment.rb +3 -3
  11. data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
  12. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +45 -21
  13. data/lib/rubocop/cop/bundler/ordered_gems.rb +3 -12
  14. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +2 -2
  15. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  16. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +11 -10
  17. data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
  18. data/lib/rubocop/cop/documentation.rb +1 -1
  19. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +3 -12
  20. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +31 -24
  21. data/lib/rubocop/cop/generator.rb +14 -8
  22. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
  23. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +60 -0
  24. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
  26. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  27. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  28. data/lib/rubocop/cop/layout/assignment_indentation.rb +1 -1
  29. data/lib/rubocop/cop/layout/block_alignment.rb +3 -3
  30. data/lib/rubocop/cop/layout/class_structure.rb +7 -2
  31. data/lib/rubocop/cop/layout/dot_position.rb +34 -5
  32. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
  34. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +7 -4
  35. data/lib/rubocop/cop/layout/end_alignment.rb +9 -2
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  37. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -1
  38. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  39. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  40. data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
  41. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  42. data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
  43. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  45. data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
  46. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +36 -17
  47. data/lib/rubocop/cop/layout/line_length.rb +9 -7
  48. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  49. data/lib/rubocop/cop/layout/multiline_block_layout.rb +3 -3
  50. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
  51. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  52. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -0
  53. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +23 -10
  54. data/lib/rubocop/cop/layout/single_line_block_chain.rb +15 -4
  55. data/lib/rubocop/cop/layout/space_after_not.rb +1 -0
  56. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -1
  57. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  58. data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
  59. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -0
  60. data/lib/rubocop/cop/layout/space_before_comment.rb +2 -2
  61. data/lib/rubocop/cop/layout/space_inside_parens.rb +78 -32
  62. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -1
  63. data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
  64. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +111 -0
  65. data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
  66. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
  67. data/lib/rubocop/cop/lint/assignment_in_condition.rb +7 -5
  68. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +18 -5
  69. data/lib/rubocop/cop/lint/boolean_symbol.rb +5 -0
  70. data/lib/rubocop/cop/lint/debugger.rb +2 -4
  71. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
  72. data/lib/rubocop/cop/lint/deprecated_constants.rb +3 -2
  73. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +24 -1
  74. data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
  75. data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
  76. data/lib/rubocop/cop/lint/else_layout.rb +10 -6
  77. data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
  78. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  79. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  80. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +12 -3
  81. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +67 -0
  82. data/lib/rubocop/cop/lint/interpolation_check.rb +5 -0
  83. data/lib/rubocop/cop/lint/loop.rb +4 -3
  84. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +5 -1
  85. data/lib/rubocop/cop/lint/number_conversion.rb +12 -1
  86. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
  87. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +4 -2
  88. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +17 -0
  89. data/lib/rubocop/cop/lint/percent_string_array.rb +10 -0
  90. data/lib/rubocop/cop/lint/raise_exception.rb +4 -0
  91. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +5 -4
  92. data/lib/rubocop/cop/lint/require_relative_self_path.rb +50 -0
  93. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  94. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  95. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  96. data/lib/rubocop/cop/lint/triple_quotes.rb +1 -1
  97. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +8 -3
  98. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -3
  99. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -2
  100. data/lib/rubocop/cop/lint/useless_setter_call.rb +7 -4
  101. data/lib/rubocop/cop/lint/useless_times.rb +5 -4
  102. data/lib/rubocop/cop/metrics/abc_size.rb +6 -0
  103. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  104. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  105. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  106. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  107. data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
  108. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  109. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  110. data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
  111. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -2
  112. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +23 -1
  113. data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -4
  114. data/lib/rubocop/cop/mixin/heredoc.rb +5 -0
  115. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
  116. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
  117. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -1
  118. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +9 -1
  119. data/lib/rubocop/cop/mixin/percent_array.rb +18 -7
  120. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +9 -1
  121. data/lib/rubocop/cop/mixin/require_library.rb +59 -0
  122. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  123. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -2
  124. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  125. data/lib/rubocop/cop/mixin/string_literals_help.rb +5 -1
  126. data/lib/rubocop/cop/mixin/trailing_body.rb +1 -1
  127. data/lib/rubocop/cop/naming/ascii_identifiers.rb +0 -3
  128. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  129. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  130. data/lib/rubocop/cop/naming/inclusive_language.rb +27 -10
  131. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +5 -4
  132. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +7 -0
  133. data/lib/rubocop/cop/security/io_methods.rb +49 -0
  134. data/lib/rubocop/cop/security/json_load.rb +8 -7
  135. data/lib/rubocop/cop/security/open.rb +4 -0
  136. data/lib/rubocop/cop/security/yaml_load.rb +4 -0
  137. data/lib/rubocop/cop/style/accessor_grouping.rb +2 -2
  138. data/lib/rubocop/cop/style/and_or.rb +5 -0
  139. data/lib/rubocop/cop/style/arguments_forwarding.rb +13 -2
  140. data/lib/rubocop/cop/style/array_coercion.rb +21 -3
  141. data/lib/rubocop/cop/style/ascii_comments.rb +0 -3
  142. data/lib/rubocop/cop/style/block_delimiters.rb +50 -2
  143. data/lib/rubocop/cop/style/case_equality.rb +6 -9
  144. data/lib/rubocop/cop/style/case_like_if.rb +5 -0
  145. data/lib/rubocop/cop/style/class_and_module_children.rb +9 -0
  146. data/lib/rubocop/cop/style/collection_compact.rb +7 -5
  147. data/lib/rubocop/cop/style/collection_methods.rb +8 -6
  148. data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
  149. data/lib/rubocop/cop/style/comment_annotation.rb +55 -25
  150. data/lib/rubocop/cop/style/commented_keyword.rb +9 -3
  151. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
  152. data/lib/rubocop/cop/style/date_time.rb +5 -0
  153. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  154. data/lib/rubocop/cop/style/documentation.rb +23 -8
  155. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
  156. data/lib/rubocop/cop/style/double_negation.rb +27 -6
  157. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  158. data/lib/rubocop/cop/style/encoding.rb +26 -15
  159. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  160. data/lib/rubocop/cop/style/explicit_block_argument.rb +46 -11
  161. data/lib/rubocop/cop/style/float_division.rb +10 -2
  162. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +14 -3
  163. data/lib/rubocop/cop/style/global_std_stream.rb +4 -0
  164. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
  165. data/lib/rubocop/cop/style/hash_each_methods.rb +5 -0
  166. data/lib/rubocop/cop/style/hash_except.rb +4 -3
  167. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  168. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -9
  169. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -6
  170. data/lib/rubocop/cop/style/identical_conditional_branches.rb +32 -5
  171. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +18 -4
  172. data/lib/rubocop/cop/style/infinite_loop.rb +4 -3
  173. data/lib/rubocop/cop/style/inverse_methods.rb +9 -2
  174. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  175. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -1
  176. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -6
  177. data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
  178. data/lib/rubocop/cop/style/missing_else.rb +7 -0
  179. data/lib/rubocop/cop/style/module_function.rb +8 -9
  180. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
  181. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
  182. data/lib/rubocop/cop/style/mutable_constant.rb +79 -14
  183. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  184. data/lib/rubocop/cop/style/negated_unless.rb +1 -1
  185. data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
  186. data/lib/rubocop/cop/style/not.rb +2 -2
  187. data/lib/rubocop/cop/style/numbered_parameters.rb +46 -0
  188. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +50 -0
  189. data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
  190. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -0
  191. data/lib/rubocop/cop/style/optional_arguments.rb +4 -0
  192. data/lib/rubocop/cop/style/optional_boolean_parameter.rb +14 -4
  193. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
  194. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  195. data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -4
  196. data/lib/rubocop/cop/style/quoted_symbols.rb +10 -6
  197. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  198. data/lib/rubocop/cop/style/redundant_argument.rb +19 -9
  199. data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
  200. data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
  201. data/lib/rubocop/cop/style/redundant_fetch_block.rb +4 -0
  202. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +12 -3
  203. data/lib/rubocop/cop/style/redundant_freeze.rb +4 -4
  204. data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
  205. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -3
  206. data/lib/rubocop/cop/style/redundant_self.rb +10 -0
  207. data/lib/rubocop/cop/style/redundant_self_assignment.rb +4 -3
  208. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
  209. data/lib/rubocop/cop/style/redundant_sort.rb +53 -20
  210. data/lib/rubocop/cop/style/regexp_literal.rb +3 -3
  211. data/lib/rubocop/cop/style/return_nil.rb +2 -1
  212. data/lib/rubocop/cop/style/safe_navigation.rb +13 -2
  213. data/lib/rubocop/cop/style/select_by_regexp.rb +139 -0
  214. data/lib/rubocop/cop/style/semicolon.rb +32 -24
  215. data/lib/rubocop/cop/style/single_argument_dig.rb +5 -0
  216. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  217. data/lib/rubocop/cop/style/single_line_methods.rb +25 -15
  218. data/lib/rubocop/cop/style/slicing_with_range.rb +13 -0
  219. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
  220. data/lib/rubocop/cop/style/special_global_vars.rb +25 -0
  221. data/lib/rubocop/cop/style/static_class.rb +5 -5
  222. data/lib/rubocop/cop/style/string_chars.rb +4 -2
  223. data/lib/rubocop/cop/style/string_concatenation.rb +5 -1
  224. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -0
  225. data/lib/rubocop/cop/style/struct_inheritance.rb +4 -0
  226. data/lib/rubocop/cop/style/swap_values.rb +4 -2
  227. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  228. data/lib/rubocop/cop/style/symbol_proc.rb +26 -0
  229. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +19 -0
  230. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  231. data/lib/rubocop/cop/style/word_array.rb +23 -5
  232. data/lib/rubocop/cop/style/yoda_condition.rb +24 -7
  233. data/lib/rubocop/cop/style/zero_length_predicate.rb +6 -0
  234. data/lib/rubocop/cop/util.rb +22 -6
  235. data/lib/rubocop/cops_documentation_generator.rb +17 -5
  236. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
  237. data/lib/rubocop/magic_comment.rb +44 -15
  238. data/lib/rubocop/options.rb +127 -113
  239. data/lib/rubocop/rake_task.rb +1 -1
  240. data/lib/rubocop/result_cache.rb +3 -3
  241. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  242. data/lib/rubocop/rspec/expect_offense.rb +6 -2
  243. data/lib/rubocop/rspec/parallel_formatter.rb +90 -0
  244. data/lib/rubocop/rspec/support.rb +1 -0
  245. data/lib/rubocop/runner.rb +2 -3
  246. data/lib/rubocop/target_finder.rb +1 -1
  247. data/lib/rubocop/version.rb +1 -1
  248. data/lib/rubocop.rb +14 -2
  249. metadata +21 -5
@@ -9,6 +9,16 @@ module RuboCop
9
9
  # example, mistranslating an array of literals to percent string notation)
10
10
  # rather than meant to be part of the resulting strings.
11
11
  #
12
+ # @safety
13
+ # The cop is unsafe because the correction changes the values in the array
14
+ # and that might have been done purposely.
15
+ #
16
+ # [source,ruby]
17
+ # ----
18
+ # %w('foo', "bar") #=> ["'foo',", '"bar"']
19
+ # %w(foo bar) #=> ['foo', 'bar']
20
+ # ----
21
+ #
12
22
  # @example
13
23
  #
14
24
  # # bad
@@ -13,6 +13,10 @@ module RuboCop
13
13
  # `Exception`. Alternatively, make `Exception` a fully qualified class
14
14
  # name with an explicit namespace.
15
15
  #
16
+ # @safety
17
+ # This cop is unsafe because it will change the exception class being
18
+ # raised, which is a change in behaviour.
19
+ #
16
20
  # @example
17
21
  # # bad
18
22
  # raise Exception, 'Error message here'
@@ -7,13 +7,14 @@ module RuboCop
7
7
  # `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
8
8
  # are checked by default. These are customizable with `AllowedMethods` option.
9
9
  #
10
- # This cop is marked as unsafe, because auto-correction can change the
11
- # return type of the expression. An offending expression that previously
12
- # could return `nil` will be auto-corrected to never return `nil`.
13
- #
14
10
  # In the example below, the safe navigation operator (`&.`) is unnecessary
15
11
  # because `NilClass` has methods like `respond_to?` and `is_a?`.
16
12
  #
13
+ # @safety
14
+ # This cop is unsafe, because auto-correction can change the return type of
15
+ # the expression. An offending expression that previously could return `nil`
16
+ # will be auto-corrected to never return `nil`.
17
+ #
17
18
  # @example
18
19
  # # bad
19
20
  # do_something if attrs&.respond_to?(:[])
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for uses a file requiring itself with `require_relative`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ #
12
+ # # foo.rb
13
+ # require_relative 'foo'
14
+ # require_relative 'bar'
15
+ #
16
+ # # good
17
+ #
18
+ # # foo.rb
19
+ # require_relative 'bar'
20
+ #
21
+ class RequireRelativeSelfPath < Base
22
+ include RangeHelp
23
+ extend AutoCorrector
24
+
25
+ MSG = 'Remove the `require_relative` that requires itself.'
26
+ RESTRICT_ON_SEND = %i[require_relative].freeze
27
+
28
+ def on_send(node)
29
+ return unless (required_feature = node.first_argument)
30
+ return unless required_feature.respond_to?(:value)
31
+ return unless same_file?(processed_source.file_path, required_feature.value)
32
+
33
+ add_offense(node) do |corrector|
34
+ corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def same_file?(file_path, required_feature)
41
+ file_path == required_feature || remove_ext(file_path) == required_feature
42
+ end
43
+
44
+ def remove_ext(file_path)
45
+ File.basename(file_path, File.extname(file_path))
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -141,7 +141,7 @@ module RuboCop
141
141
  end
142
142
 
143
143
  def reference_pos(node)
144
- node = node.parent.masgn_type? ? node.parent : node
144
+ node = node.parent if node.parent.masgn_type?
145
145
 
146
146
  node.source_range.begin_pos
147
147
  end
@@ -10,7 +10,7 @@ module RuboCop
10
10
  #
11
11
  # NOTE: Shadowing of variables in block passed to `Ractor.new` is allowed
12
12
  # because `Ractor` should not access outer variables.
13
- # eg. following syle is encouraged:
13
+ # eg. following style is encouraged:
14
14
  #
15
15
  # worker_id, pipe = env
16
16
  # Ractor.new(worker_id, pipe) do |worker_id, pipe|
@@ -143,7 +143,7 @@ module RuboCop
143
143
  # Although some operators can be converted to symbols normally
144
144
  # (ie. `:==`), these are not accepted as hash keys and will
145
145
  # raise a syntax error (eg. `{ ==: ... }`). Therefore, if the
146
- # symbol does not start with an alpha-numeric or underscore, it
146
+ # symbol does not start with an alphanumeric or underscore, it
147
147
  # will be ignored.
148
148
  return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
149
149
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # This cop checks for "triple quotes" (strings delimted by any odd number
6
+ # This cop checks for "triple quotes" (strings delimited by any odd number
7
7
  # of quotes greater than 1).
8
8
  #
9
9
  # Ruby allows multiple strings to be implicitly concatenated by just
@@ -13,14 +13,19 @@ module RuboCop
13
13
  # Keyword arguments (including `**kwargs`) do not get counted towards
14
14
  # this, as they are not used by the methods in question.
15
15
  #
16
- # NOTE: This cop matches for method names only and hence cannot tell apart
17
- # methods with same name in different classes.
18
- #
19
16
  # Method names and their expected arity can be configured like this:
20
17
  #
18
+ # [source,yaml]
19
+ # ----
21
20
  # Methods:
22
21
  # inject: 2
23
22
  # reduce: 2
23
+ # ----
24
+ #
25
+ # @safety
26
+ # This cop matches for method names only and hence cannot tell apart
27
+ # methods with same name in different classes, which may lead to a
28
+ # false positive.
24
29
  #
25
30
  # @example
26
31
  # # bad
@@ -87,9 +87,8 @@ module RuboCop
87
87
  end
88
88
 
89
89
  def ignored_method?(body)
90
- cop_config['IgnoreEmptyMethods'] && body.nil? ||
91
- cop_config['IgnoreNotImplementedMethods'] &&
92
- not_implemented?(body)
90
+ (cop_config['IgnoreEmptyMethods'] && body.nil?) ||
91
+ (cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body))
93
92
  end
94
93
 
95
94
  def message(variable)
@@ -6,8 +6,9 @@ module RuboCop
6
6
  # This cop checks for useless method definitions, specifically: empty constructors
7
7
  # and methods just delegating to `super`.
8
8
  #
9
- # This cop is marked as unsafe as it can trigger false positives for cases when
10
- # an empty constructor just overrides the parent constructor, which is bad anyway.
9
+ # @safety
10
+ # This cop is unsafe as it can register false positives for cases when an empty
11
+ # constructor just overrides the parent constructor, which is bad anyway.
11
12
  #
12
13
  # @example
13
14
  # # bad
@@ -5,11 +5,14 @@ module RuboCop
5
5
  module Lint
6
6
  # This cop checks for setter call to local variable as the final
7
7
  # expression of a function definition.
8
- # Its auto-correction is marked as unsafe because return value will be changed.
9
8
  #
10
- # NOTE: There are edge cases in which the local variable references a
11
- # value that is also accessible outside the local scope. This is not
12
- # detected by the cop, and it can yield a false positive.
9
+ # @safety
10
+ # There are edge cases in which the local variable references a
11
+ # value that is also accessible outside the local scope. This is not
12
+ # detected by the cop, and it can yield a false positive.
13
+ #
14
+ # As well, auto-correction is unsafe because the method's
15
+ # return value will be changed.
13
16
  #
14
17
  # @example
15
18
  #
@@ -7,8 +7,9 @@ module RuboCop
7
7
  # (when the integer <= 0) or that will only ever yield once
8
8
  # (`1.times`).
9
9
  #
10
- # This cop is marked as unsafe as `times` returns its receiver, which
11
- # is *usually* OK, but might change behavior.
10
+ # @safety
11
+ # This cop is unsafe as `times` returns its receiver, which is
12
+ # *usually* OK, but might change behavior.
12
13
  #
13
14
  # @example
14
15
  # # bad
@@ -65,7 +66,7 @@ module RuboCop
65
66
  private
66
67
 
67
68
  def never_process?(count, node)
68
- count < 1 || node.block_type? && node.body.nil?
69
+ count < 1 || (node.block_type? && node.body.nil?)
69
70
  end
70
71
 
71
72
  def remove_node(corrector, node)
@@ -81,7 +82,7 @@ module RuboCop
81
82
  return if block_reassigns_arg?(node, block_arg)
82
83
 
83
84
  source = node.body.source
84
- source.gsub!(/\b#{block_arg}\b/, '1') if block_arg
85
+ source.gsub!(/\b#{block_arg}\b/, '0') if block_arg
85
86
 
86
87
  corrector.replace(node, fix_indentation(source, node.loc.column...node.body.loc.column))
87
88
  end
@@ -8,6 +8,12 @@ module RuboCop
8
8
  # (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
9
9
  # and https://en.wikipedia.org/wiki/ABC_Software_Metric.
10
10
  #
11
+ # Interpreting ABC size:
12
+ #
13
+ # * <= 17 satisfactory
14
+ # * 18..30 unsatisfactory
15
+ # * > 30 dangerous
16
+ #
11
17
  # You can have repeated "attributes" calls count as a single "branch".
12
18
  # For this purpose, attributes are any method with no argument; no attempt
13
19
  # is meant to distinguish actual `attr_reader` from other methods.
@@ -9,6 +9,9 @@ module RuboCop
9
9
  # Keyword arguments can optionally be excluded from the total count,
10
10
  # as they add less complexity than positional or optional parameters.
11
11
  #
12
+ # NOTE: Explicit block argument `&block` is not counted to prevent
13
+ # erroneous change that is avoided by making block argument implicit.
14
+ #
12
15
  # @example Max: 3
13
16
  # # good
14
17
  # def foo(a, b, c = 1)
@@ -94,9 +97,9 @@ module RuboCop
94
97
 
95
98
  def args_count(node)
96
99
  if count_keyword_args?
97
- node.children.size
100
+ node.children.count { |a| !a.blockarg_type? }
98
101
  else
99
- node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
102
+ node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) && !a.blockarg_type? }
100
103
  end
101
104
  end
102
105
 
@@ -45,7 +45,7 @@ module RuboCop
45
45
  else
46
46
  # Otherwise, the case node gets 0.8 complexity points and each
47
47
  # when gets 0.2.
48
- (0.8 + 0.2 * nb_branches).round
48
+ (0.8 + (0.2 * nb_branches)).round
49
49
  end
50
50
  when :if
51
51
  node.else? && !node.elsif? ? 2 : 1
@@ -46,7 +46,7 @@ module RuboCop
46
46
  visit_depth_last(@node) { |child| calculate_node(child) }
47
47
 
48
48
  [
49
- Math.sqrt(@assignment**2 + @branch**2 + @condition**2).round(2),
49
+ Math.sqrt((@assignment**2) + (@branch**2) + (@condition**2)).round(2),
50
50
  "<#{@assignment}, #{@branch}, #{@condition}>"
51
51
  ]
52
52
  end
@@ -147,7 +147,7 @@ module RuboCop
147
147
 
148
148
  # Returns true for lines that shall not be included in the count.
149
149
  def irrelevant_line?(source_line)
150
- source_line.blank? || !count_comments? && comment_line?(source_line)
150
+ source_line.blank? || (!count_comments? && comment_line?(source_line))
151
151
  end
152
152
 
153
153
  def count_comments?
@@ -2,40 +2,63 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- module Style
6
- # Common functionality related to annotation comments.
7
- module AnnotationComment
8
- private
9
-
10
- # @api public
11
- def annotation?(comment)
12
- _margin, first_word, colon, space, note = split_comment(comment)
13
- keyword_appearance?(first_word, colon, space) &&
14
- !just_first_word_of_sentence?(first_word, colon, space, note)
15
- end
16
-
17
- # @api public
18
- def split_comment(comment)
19
- match = comment.text.match(/^(# ?)([A-Za-z]+)(\s*:)?(\s+)?(\S+)?/)
20
- return false unless match
21
-
22
- match.captures
23
- end
24
-
25
- # @api public
26
- def keyword_appearance?(first_word, colon, space)
27
- first_word && keyword?(first_word.upcase) && (colon || space)
28
- end
29
-
30
- # @api private
31
- def just_first_word_of_sentence?(first_word, colon, space, note)
32
- first_word == first_word.capitalize && !colon && space && note
33
- end
34
-
35
- # @api public
36
- def keyword?(word)
37
- config.for_cop('Style/CommentAnnotation')['Keywords'].include?(word)
38
- end
5
+ # Representation of an annotation comment in source code (eg. `# TODO: blah blah blah`).
6
+ class AnnotationComment
7
+ extend Forwardable
8
+
9
+ attr_reader :comment, :margin, :keyword, :colon, :space, :note
10
+
11
+ # @param [Parser::Source::Comment] comment
12
+ # @param [Array<String>] keywords
13
+ def initialize(comment, keywords)
14
+ @comment = comment
15
+ @keywords = keywords
16
+ @margin, @keyword, @colon, @space, @note = split_comment(comment)
17
+ end
18
+
19
+ def annotation?
20
+ keyword_appearance? && !just_keyword_of_sentence?
21
+ end
22
+
23
+ def correct?(colon:)
24
+ return false unless keyword && space && note
25
+ return false unless keyword == keyword.upcase
26
+
27
+ self.colon.nil? == !colon
28
+ end
29
+
30
+ # Returns the range bounds for just the annotation
31
+ def bounds
32
+ start = comment.loc.expression.begin_pos + margin.length
33
+ length = [keyword, colon, space].reduce(0) { |len, elem| len + elem.to_s.length }
34
+ [start, start + length]
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :keywords
40
+
41
+ def split_comment(comment)
42
+ # Sort keywords by reverse length so that if a keyword is in a phrase
43
+ # but also on its own, both will match properly.
44
+ keywords_regex = Regexp.new(
45
+ Regexp.union(keywords.sort_by { |w| -w.length }).source,
46
+ Regexp::IGNORECASE
47
+ )
48
+ regex = /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
49
+
50
+ match = comment.text.match(regex)
51
+ return false unless match
52
+
53
+ match.captures
54
+ end
55
+
56
+ def keyword_appearance?
57
+ keyword && (colon || space)
58
+ end
59
+
60
+ def just_keyword_of_sentence?
61
+ keyword == keyword.capitalize && !colon && space && note
39
62
  end
40
63
  end
41
64
  end
@@ -97,9 +97,9 @@ module RuboCop
97
97
  # If a send node contains a heredoc argument, splitting cannot happen
98
98
  # after the heredoc or else it will cause a syntax error.
99
99
  def shift_elements_for_heredoc_arg(node, elements, index)
100
- return index unless node.send_type?
100
+ return index unless node.send_type? || node.array_type?
101
101
 
102
- heredoc_index = elements.index { |arg| (arg.str_type? || arg.dstr_type?) && arg.heredoc? }
102
+ heredoc_index = elements.index { |arg| arg.respond_to?(:heredoc?) && arg.heredoc? }
103
103
  return index unless heredoc_index
104
104
  return nil if heredoc_index.zero?
105
105
 
@@ -43,7 +43,7 @@ module RuboCop
43
43
 
44
44
  # Returns true for lines that shall not be included in the count.
45
45
  def irrelevant_line(source_line)
46
- source_line.blank? || !count_comments? && comment_line?(source_line)
46
+ source_line.blank? || (!count_comments? && comment_line?(source_line))
47
47
  end
48
48
 
49
49
  def build_code_length_calculator(node)
@@ -5,7 +5,6 @@ module RuboCop
5
5
  # Common functionality for checking documentation.
6
6
  module DocumentationComment
7
7
  extend NodePattern::Macros
8
- include Style::AnnotationComment
9
8
 
10
9
  private
11
10
 
@@ -15,7 +14,7 @@ module RuboCop
15
14
  return false unless preceding_comment?(node, preceding_lines.last)
16
15
 
17
16
  preceding_lines.any? do |comment|
18
- !annotation?(comment) &&
17
+ !AnnotationComment.new(comment, annotation_keywords).annotation? &&
19
18
  !interpreter_directive_comment?(comment) &&
20
19
  !rubocop_directive_comment?(comment)
21
20
  end
@@ -44,6 +43,10 @@ module RuboCop
44
43
  def rubocop_directive_comment?(comment)
45
44
  !!DirectiveComment.new(comment).match_captures
46
45
  end
46
+
47
+ def annotation_keywords
48
+ config.for_cop('Style/CommentAnnotation')['Keywords']
49
+ end
47
50
  end
48
51
  end
49
52
  end
@@ -34,8 +34,7 @@ module RuboCop
34
34
 
35
35
  def matching_ranges(end_loc, align_ranges)
36
36
  align_ranges.select do |_, range|
37
- range.line == end_loc.line ||
38
- column_offset_between(range, end_loc).zero?
37
+ same_line?(range, end_loc) || column_offset_between(range, end_loc).zero?
39
38
  end
40
39
  end
41
40
 
@@ -8,7 +8,9 @@ module RuboCop
8
8
 
9
9
  FROZEN_STRING_LITERAL = '# frozen_string_literal:'
10
10
  FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
11
- FROZEN_STRING_LITERAL_TYPES = %i[str dstr].freeze
11
+ FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze
12
+
13
+ private_constant :FROZEN_STRING_LITERAL_TYPES_RUBY27
12
14
 
13
15
  def frozen_string_literal_comment_exists?
14
16
  leading_comment_lines.any? { |line| MagicComment.parse(line).valid_literal_value? }
@@ -16,6 +18,26 @@ module RuboCop
16
18
 
17
19
  private
18
20
 
21
+ def frozen_string_literal?(node)
22
+ frozen_string = if target_ruby_version >= 3.0
23
+ uninterpolated_string?(node) || frozen_heredoc?(node)
24
+ else
25
+ FROZEN_STRING_LITERAL_TYPES_RUBY27.include?(node.type)
26
+ end
27
+
28
+ frozen_string && frozen_string_literals_enabled?
29
+ end
30
+
31
+ def uninterpolated_string?(node)
32
+ node.str_type? || (node.dstr_type? && node.each_descendant(:begin).none?)
33
+ end
34
+
35
+ def frozen_heredoc?(node)
36
+ return false unless node.dstr_type? && node.heredoc?
37
+
38
+ node.children.all?(&:str_type?)
39
+ end
40
+
19
41
  def frozen_string_literals_enabled?
20
42
  ruby_version = processed_source.ruby_version
21
43
  return false unless ruby_version
@@ -139,9 +139,9 @@ module RuboCop
139
139
  end
140
140
 
141
141
  def self.from_map_to_h(node, match)
142
- strip_trailing_chars = 0
143
-
144
- unless node.parent&.block_type?
142
+ if node.parent&.block_type? && node.parent.send_node == node
143
+ strip_trailing_chars = 0
144
+ else
145
145
  map_range = node.children.first.source_range
146
146
  node_range = node.source_range
147
147
  strip_trailing_chars = node_range.end_pos - map_range.end_pos
@@ -175,7 +175,12 @@ module RuboCop
175
175
  end
176
176
 
177
177
  def set_new_body_expression(transforming_body_expr, corrector)
178
- corrector.replace(block_node.body, transforming_body_expr.loc.expression.source)
178
+ body_source = transforming_body_expr.loc.expression.source
179
+ if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
180
+ body_source = "{ #{body_source} }"
181
+ end
182
+
183
+ corrector.replace(block_node.body, body_source)
179
184
  end
180
185
  end
181
186
  end
@@ -20,6 +20,11 @@ module RuboCop
20
20
 
21
21
  private
22
22
 
23
+ def indent_level(str)
24
+ indentations = str.lines.map { |line| line[/^\s*/] }.reject { |line| line.end_with?("\n") }
25
+ indentations.empty? ? 0 : indentations.min_by(&:size).size
26
+ end
27
+
23
28
  def delimiter_string(node)
24
29
  node.source.match(OPENING_DELIMITER).captures[1]
25
30
  end
@@ -15,7 +15,7 @@ module RuboCop
15
15
  node.arguments.each do |arg|
16
16
  on_node(type, arg, :send) do |type_node|
17
17
  left_brace = type_node.loc.begin
18
- if left_brace && left_brace.line == left_parenthesis.line
18
+ if left_brace && same_line?(left_brace, left_parenthesis)
19
19
  yield type_node, left_parenthesis
20
20
  ignore_node(type_node)
21
21
  end
@@ -134,7 +134,7 @@ module RuboCop
134
134
 
135
135
  next if a.setter_method?
136
136
  next unless kind == :with_or_without_parentheses ||
137
- kind == :with_parentheses && parentheses?(a)
137
+ (kind == :with_parentheses && parentheses?(a))
138
138
 
139
139
  a.arguments.any? { |arg| within_node?(node, arg) }
140
140
  end
@@ -156,7 +156,7 @@ module RuboCop
156
156
 
157
157
  def disqualified_rhs?(candidate, ancestor)
158
158
  UNALIGNED_RHS_TYPES.include?(ancestor.type) ||
159
- ancestor.block_type? && part_of_block_body?(candidate, ancestor)
159
+ (ancestor.block_type? && part_of_block_body?(candidate, ancestor))
160
160
  end
161
161
 
162
162
  def valid_rhs?(candidate, ancestor)
@@ -90,7 +90,7 @@ module RuboCop
90
90
  # This method depends on the fact that we have guarded
91
91
  # against implicit and empty literals.
92
92
  def opening_brace_on_same_line?(node)
93
- node.loc.begin.line == children(node).first.first_line
93
+ same_line?(node.loc.begin, children(node).first)
94
94
  end
95
95
 
96
96
  # This method depends on the fact that we have guarded
@@ -35,7 +35,15 @@ module RuboCop
35
35
  previous: gem_name(current),
36
36
  current: gem_name(previous)
37
37
  )
38
- add_offense(current, message: message)
38
+
39
+ add_offense(current, message: message) do |corrector|
40
+ OrderedGemCorrector.correct(
41
+ processed_source,
42
+ current,
43
+ previous_declaration(current),
44
+ treat_comments_as_separators
45
+ ).call(corrector)
46
+ end
39
47
  end
40
48
 
41
49
  def gem_name(declaration_node)
@@ -18,15 +18,16 @@ module RuboCop
18
18
  !parent.parenthesized? && parent&.block_literal?
19
19
  end
20
20
 
21
+ # Override to determine values that are invalid in a percent array
22
+ def invalid_percent_array_contents?(_node)
23
+ false
24
+ end
25
+
21
26
  def allowed_bracket_array?(node)
22
27
  comments_in_array?(node) || below_array_length?(node) ||
23
28
  invalid_percent_array_context?(node)
24
29
  end
25
30
 
26
- def message(_node)
27
- style == :percent ? self.class::PERCENT_MSG : self.class::ARRAY_MSG
28
- end
29
-
30
31
  def comments_in_array?(node)
31
32
  line_span = node.source_range.first_line...node.source_range.last_line
32
33
  processed_source.each_comment_in_lines(line_span).any?
@@ -35,9 +36,19 @@ module RuboCop
35
36
  def check_percent_array(node)
36
37
  array_style_detected(:percent, node.values.size)
37
38
 
38
- return unless style == :brackets
39
+ brackets_required = invalid_percent_array_contents?(node)
40
+ return unless style == :brackets || brackets_required
39
41
 
40
- add_offense(node) { |corrector| correct_bracketed(corrector, node) }
42
+ # If in percent style but brackets are required due to
43
+ # string content, the file should be excluded in auto-gen-config
44
+ no_acceptable_style! if brackets_required
45
+
46
+ bracketed_array = build_bracketed_array(node)
47
+ message = format(self.class::ARRAY_MSG, prefer: bracketed_array)
48
+
49
+ add_offense(node, message: message) do |corrector|
50
+ corrector.replace(node, bracketed_array)
51
+ end
41
52
  end
42
53
 
43
54
  def check_bracketed_array(node, literal_prefix)
@@ -47,7 +58,7 @@ module RuboCop
47
58
 
48
59
  return unless style == :percent
49
60
 
50
- add_offense(node) do |corrector|
61
+ add_offense(node, message: self.class::PERCENT_MSG) do |corrector|
51
62
  percent_literal_corrector = PercentLiteralCorrector.new(@config, @preferred_delimiters)
52
63
  percent_literal_corrector.correct(corrector, node, literal_prefix)
53
64
  end