rubocop 1.8.0 → 1.11.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 (246) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -13
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +89 -22
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +9 -0
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
  8. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  9. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  10. data/lib/rubocop/config.rb +5 -2
  11. data/lib/rubocop/config_loader.rb +7 -14
  12. data/lib/rubocop/config_store.rb +12 -1
  13. data/lib/rubocop/cop/base.rb +2 -1
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  15. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  16. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  17. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  18. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  19. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  20. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  21. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  22. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  23. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  24. data/lib/rubocop/cop/generator.rb +3 -5
  25. data/lib/rubocop/cop/internal_affairs.rb +6 -1
  26. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
  27. data/lib/rubocop/cop/internal_affairs/example_description.rb +90 -0
  28. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  30. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  31. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +62 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -0
  35. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  36. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  37. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  38. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  39. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  40. data/lib/rubocop/cop/layout/class_structure.rb +8 -2
  41. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  42. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  43. data/lib/rubocop/cop/layout/first_argument_indentation.rb +22 -3
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  45. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  46. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  47. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  48. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  49. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  50. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  51. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  54. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  55. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  56. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  57. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  58. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  59. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  60. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  61. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  62. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  63. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  64. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  65. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  66. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  67. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  68. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  69. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  70. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  71. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  72. data/lib/rubocop/cop/lint/number_conversion.rb +43 -6
  73. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
  74. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
  75. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  76. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  77. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -3
  78. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  79. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  80. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  82. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  83. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  84. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  85. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  86. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  87. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  88. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  89. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  90. data/lib/rubocop/cop/lint/symbol_conversion.rb +103 -0
  91. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  92. data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
  93. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  94. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  95. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  96. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  97. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  98. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  99. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  100. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  101. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  102. data/lib/rubocop/cop/message_annotator.rb +4 -1
  103. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  104. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  105. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  106. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  107. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  108. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
  109. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  110. data/lib/rubocop/cop/mixin/comments_help.rb +0 -1
  111. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  112. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  113. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  114. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  115. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  116. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  117. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  118. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  119. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  120. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  121. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  122. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
  123. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  124. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  125. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  126. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -0
  127. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  128. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  129. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
  130. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  131. data/lib/rubocop/cop/registry.rb +1 -1
  132. data/lib/rubocop/cop/security/eval.rb +1 -0
  133. data/lib/rubocop/cop/security/json_load.rb +1 -0
  134. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  135. data/lib/rubocop/cop/security/open.rb +1 -0
  136. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  137. data/lib/rubocop/cop/severity.rb +3 -3
  138. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  139. data/lib/rubocop/cop/style/alias.rb +1 -0
  140. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  141. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  142. data/lib/rubocop/cop/style/array_join.rb +1 -0
  143. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  144. data/lib/rubocop/cop/style/attr.rb +1 -0
  145. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  146. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -0
  147. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  148. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  149. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  150. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  151. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  152. data/lib/rubocop/cop/style/date_time.rb +3 -0
  153. data/lib/rubocop/cop/style/dir.rb +1 -0
  154. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
  155. data/lib/rubocop/cop/style/documentation.rb +5 -0
  156. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  157. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  158. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  159. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  160. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  161. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  162. data/lib/rubocop/cop/style/eval_with_location.rb +140 -49
  163. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  164. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  165. data/lib/rubocop/cop/style/explicit_block_argument.rb +12 -1
  166. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  167. data/lib/rubocop/cop/style/float_division.rb +7 -0
  168. data/lib/rubocop/cop/style/format_string.rb +2 -0
  169. data/lib/rubocop/cop/style/format_string_token.rb +19 -2
  170. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  171. data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
  172. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  173. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  174. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  175. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  176. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  177. data/lib/rubocop/cop/style/if_inside_else.rb +14 -7
  178. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -0
  179. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  180. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  181. data/lib/rubocop/cop/style/min_max.rb +1 -0
  182. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  183. data/lib/rubocop/cop/style/module_function.rb +5 -0
  184. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  185. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  186. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -0
  187. data/lib/rubocop/cop/style/nil_comparison.rb +6 -0
  188. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  189. data/lib/rubocop/cop/style/non_nil_check.rb +30 -13
  190. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  191. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  192. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  193. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  194. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  195. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  196. data/lib/rubocop/cop/style/proc.rb +1 -0
  197. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  198. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  199. data/lib/rubocop/cop/style/redundant_begin.rb +7 -1
  200. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  201. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  202. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  203. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  204. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  205. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  206. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  207. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  208. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  209. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  210. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  211. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  212. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  213. data/lib/rubocop/cop/style/sample.rb +1 -0
  214. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  215. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  216. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  217. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  218. data/lib/rubocop/cop/style/sole_nested_conditional.rb +28 -4
  219. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  220. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  221. data/lib/rubocop/cop/style/string_concatenation.rb +2 -1
  222. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  223. data/lib/rubocop/cop/style/strip.rb +1 -0
  224. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -0
  225. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  226. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -1
  227. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -0
  228. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  229. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  230. data/lib/rubocop/cop/style/unless_logical_operators.rb +99 -0
  231. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  232. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
  233. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  234. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  235. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
  236. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  237. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  238. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  239. data/lib/rubocop/magic_comment.rb +30 -1
  240. data/lib/rubocop/name_similarity.rb +1 -1
  241. data/lib/rubocop/options.rb +1 -1
  242. data/lib/rubocop/rspec/expect_offense.rb +5 -2
  243. data/lib/rubocop/runner.rb +1 -0
  244. data/lib/rubocop/target_ruby.rb +21 -13
  245. data/lib/rubocop/version.rb +2 -2
  246. metadata +21 -7
@@ -26,10 +26,12 @@ module RuboCop
26
26
  return length if @foldable_types.empty?
27
27
 
28
28
  each_top_level_descendant(@node, @foldable_types) do |descendant|
29
- if foldable_node?(descendant)
30
- descendant_length = code_length(descendant)
31
- length = length - descendant_length + 1
32
- end
29
+ next unless foldable_node?(descendant)
30
+
31
+ descendant_length = code_length(descendant)
32
+ length = length - descendant_length + 1
33
+ # Subtract 2 length of opening and closing brace if method argument omits hash braces.
34
+ length -= 2 if descendant.hash_type? && !descendant.braces?
33
35
  end
34
36
 
35
37
  length
@@ -60,6 +60,7 @@ module RuboCop
60
60
 
61
61
  private
62
62
 
63
+ # @!method attribute_call?(node)
63
64
  def_node_matcher :attribute_call?, <<~PATTERN
64
65
  ( {csend send} _receiver _method # and no parameters
65
66
  )
@@ -90,6 +91,7 @@ module RuboCop
90
91
  end
91
92
  end
92
93
 
94
+ # @!method root_node?(node)
93
95
  def_node_matcher :root_node?, <<~PATTERN
94
96
  { nil? | self # e.g. receiver of `my_method` or `self.my_attr`
95
97
  | lvar | ivar | cvar | gvar # e.g. receiver of `var.my_method`
@@ -69,6 +69,11 @@ module RuboCop
69
69
  # @api private
70
70
  def extract_first_element_over_column_limit(node, elements, max)
71
71
  line = node.first_line
72
+
73
+ # If the first argument is a hash pair but the method is not parenthesized,
74
+ # the argument cannot be moved to another line because it cause a syntax error.
75
+ elements.shift if node.send_type? && !node.parenthesized? && elements.first.pair_type?
76
+
72
77
  i = 0
73
78
  i += 1 while within_column_limit?(elements[i], max, line)
74
79
  return elements.first if i.zero?
@@ -4,10 +4,12 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for checking length of code segments.
6
6
  module CodeLength
7
- include ConfigurableMax
7
+ extend ExcludeLimit
8
8
 
9
9
  MSG = '%<label>s has too many lines. [%<length>d/%<max>d]'
10
10
 
11
+ exclude_limit 'Max'
12
+
11
13
  private
12
14
 
13
15
  def message(length, max_length)
@@ -9,7 +9,6 @@ module RuboCop
9
9
  def source_range_with_comment(node)
10
10
  begin_pos = begin_pos_with_comment(node)
11
11
  end_pos = end_position_for(node)
12
- end_pos += 1 if node.def_type?
13
12
 
14
13
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
15
14
  end
@@ -4,6 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  # Handles `Max` configuration parameters, especially setting them to an
6
6
  # appropriate value with --auto-gen-config.
7
+ # @deprecated Use `exclude_limit ParameterName` instead.
7
8
  module ConfigurableMax
8
9
  private
9
10
 
@@ -25,6 +25,7 @@ module RuboCop
25
25
  processed_source[0..index].map(&:strip)
26
26
  end
27
27
 
28
+ # @!method non_public_modifier?(node)
28
29
  def_node_matcher :non_public_modifier?, <<~PATTERN
29
30
  (send nil? {:private :protected} ({def defs} ...))
30
31
  PATTERN
@@ -18,7 +18,10 @@ module RuboCop
18
18
 
19
19
  private
20
20
 
21
+ # @!method constant_definition?(node)
21
22
  def_node_matcher :constant_definition?, '{class module}'
23
+
24
+ # @!method empty_line_required?(node)
22
25
  def_node_matcher :empty_line_required?,
23
26
  '{def defs class module (send nil? {:private :protected :public})}'
24
27
 
@@ -8,6 +8,7 @@ module RuboCop
8
8
 
9
9
  private
10
10
 
11
+ # @!method empty_arguments?(node)
11
12
  def_node_matcher :empty_arguments?, <<~PATTERN
12
13
  (block _ $(args) _)
13
14
  PATTERN
@@ -13,10 +13,12 @@ module RuboCop
13
13
  # @api private
14
14
  module EnforceSuperclass
15
15
  def self.included(base)
16
+ # @!method class_definition(node)
16
17
  base.def_node_matcher :class_definition, <<~PATTERN
17
18
  (class (const _ !:#{base::SUPERCLASS}) #{base::BASE_PATTERN} ...)
18
19
  PATTERN
19
20
 
21
+ # @!method class_new_definition(node)
20
22
  base.def_node_matcher :class_new_definition, <<~PATTERN
21
23
  [!^(casgn {nil? cbase} :#{base::SUPERCLASS} ...)
22
24
  !^^(casgn {nil? cbase} :#{base::SUPERCLASS} (block ...))
@@ -9,6 +9,7 @@ module RuboCop
9
9
 
10
10
  RESTRICT_ON_SEND = %i[[] to_h].freeze
11
11
 
12
+ # @!method array_receiver?(node)
12
13
  def_node_matcher :array_receiver?, <<~PATTERN
13
14
  {(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
14
15
  PATTERN
@@ -6,10 +6,12 @@ module RuboCop
6
6
  #
7
7
  # This module handles measurement and reporting of complexity in methods.
8
8
  module MethodComplexity
9
- include ConfigurableMax
10
9
  include IgnoredMethods
11
10
  include Metrics::Utils::RepeatedCsendDiscount
12
11
  extend NodePattern::Macros
12
+ extend ExcludeLimit
13
+
14
+ exclude_limit 'Max'
13
15
 
14
16
  # Ensure cops that include `MethodComplexity` have the config
15
17
  # `attr_accessor`s that `ignored_method?` needs.
@@ -34,6 +36,7 @@ module RuboCop
34
36
 
35
37
  private
36
38
 
39
+ # @!method define_method?(node)
37
40
  def_node_matcher :define_method?, <<~PATTERN
38
41
  (block
39
42
  (send nil? :define_method ({sym str} $_))
@@ -12,7 +12,10 @@ module RuboCop
12
12
 
13
13
  private
14
14
 
15
+ # @!method single_negative?(node)
15
16
  def_node_matcher :single_negative?, '(send !(send _ :!) :!)'
17
+
18
+ # @!method empty_condition?(node)
16
19
  def_node_matcher :empty_condition?, '(begin)'
17
20
 
18
21
  def check_negative_conditional(node, message:, &block)
@@ -15,7 +15,7 @@ module RuboCop
15
15
  end
16
16
 
17
17
  def delimiters
18
- preferred_delimiters[type].split(//)
18
+ preferred_delimiters[type].split('')
19
19
  end
20
20
 
21
21
  private
@@ -35,10 +35,10 @@ module RuboCop
35
35
  ensure_valid_preferred_delimiters
36
36
 
37
37
  if preferred_delimiters_config.key?('default')
38
- Hash[PERCENT_LITERAL_TYPES.map do |type|
38
+ PERCENT_LITERAL_TYPES.map do |type|
39
39
  [type, preferred_delimiters_config[type] ||
40
40
  preferred_delimiters_config['default']]
41
- end]
41
+ end.to_h
42
42
  else
43
43
  preferred_delimiters_config
44
44
  end
@@ -8,6 +8,7 @@ module RuboCop
8
8
 
9
9
  private
10
10
 
11
+ # @!method rational_literal?(node)
11
12
  def_node_matcher :rational_literal?, <<~PATTERN
12
13
  (send
13
14
  (int _) :/
@@ -10,8 +10,13 @@ module RuboCop
10
10
 
11
11
  private
12
12
 
13
+ # @!method empty_condition?(node)
13
14
  def_node_matcher :empty_condition?, '(begin)'
15
+
16
+ # @!method setter_method?(node)
14
17
  def_node_matcher :setter_method?, '[(send ...) setter_method?]'
18
+
19
+ # @!method safe_assignment?(node)
15
20
  def_node_matcher :safe_assignment?,
16
21
  '(begin {equals_asgn? #setter_method?})'
17
22
 
@@ -24,7 +24,11 @@ module RuboCop
24
24
  name = full_name.gsub(/\A(_+)/, '')
25
25
  next if allowed_names.include?(name)
26
26
 
27
- range = arg_range(arg, name.size)
27
+ length = full_name.size
28
+ length += 1 if arg.restarg_type?
29
+ length += 2 if arg.kwrestarg_type?
30
+
31
+ range = arg_range(arg, length)
28
32
  issue_offenses(node, range, name)
29
33
  end
30
34
  end
@@ -28,6 +28,7 @@ module RuboCop
28
28
  end || right.last
29
29
  end
30
30
 
31
+ # @!method visibility_block?(node)
31
32
  def_node_matcher :visibility_block?, <<~PATTERN
32
33
  (send nil? { :private :protected :public })
33
34
  PATTERN
@@ -22,6 +22,7 @@ module RuboCop
22
22
  OP_LIKE_METHODS = %i[eql? equal?].freeze
23
23
  EXCLUDED = %i[+@ -@ [] []= << === ` =~].freeze
24
24
 
25
+ # @!method op_method_candidate?(node)
25
26
  def_node_matcher :op_method_candidate?, <<~PATTERN
26
27
  (def [#op_method? $_] (args $(arg [!:other !:_other])) _)
27
28
  PATTERN
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # than just standard ASCII characters
24
24
  SNAKE_CASE = /^[[:digit:][:upper:]_]+$/.freeze
25
25
 
26
+ # @!method class_or_struct_return_method?(node)
26
27
  def_node_matcher :class_or_struct_return_method?, <<~PATTERN
27
28
  (send
28
29
  (const _ {:Class :Struct}) :new
@@ -64,6 +65,7 @@ module RuboCop
64
65
  (node.receiver.nil? || !literal_receiver?(node))
65
66
  end
66
67
 
68
+ # @!method literal_receiver?(node)
67
69
  def_node_matcher :literal_receiver?, <<~PATTERN
68
70
  {(send literal? ...)
69
71
  (send (begin literal?) ...)}
@@ -148,6 +148,7 @@ module RuboCop
148
148
  'with `_`. Use `@%<suggested_var>s` instead.'
149
149
  DYNAMIC_DEFINE_METHODS = %i[define_method define_singleton_method].to_set.freeze
150
150
 
151
+ # @!method method_definition?(node)
151
152
  def_node_matcher :method_definition?, <<~PATTERN
152
153
  ${
153
154
  (block (send _ %DYNAMIC_DEFINE_METHODS ({sym str} $_)) ...)
@@ -179,6 +180,7 @@ module RuboCop
179
180
  end
180
181
  # rubocop:enable Metrics/AbcSize
181
182
 
183
+ # @!method defined_memoized?(node, ivar)
182
184
  def_node_matcher :defined_memoized?, <<~PATTERN
183
185
  (begin
184
186
  (if (defined $(ivar %1)) (return $(ivar %1)) nil?)
@@ -35,7 +35,10 @@ module RuboCop
35
35
 
36
36
  MSG = 'Use %<style>s for method names.'
37
37
 
38
+ # @!method sym_name(node)
38
39
  def_node_matcher :sym_name, '(sym $_name)'
40
+
41
+ # @!method str_name(node)
39
42
  def_node_matcher :str_name, '(str $_name)'
40
43
 
41
44
  def on_send(node)
@@ -30,6 +30,7 @@ module RuboCop
30
30
  class PredicateName < Base
31
31
  include AllowedMethods
32
32
 
33
+ # @!method dynamic_method_define(node)
33
34
  def_node_matcher :dynamic_method_define, <<~PATTERN
34
35
  (send nil? #method_definition_macros
35
36
  (sym $_)
@@ -71,11 +71,7 @@ module RuboCop
71
71
  add_offense(range, message: message) do |corrector|
72
72
  corrector.replace(range, preferred_name)
73
73
 
74
- node.body&.each_descendant(:lvar) do |var|
75
- next unless var.children.first == offending_name
76
-
77
- corrector.replace(var, preferred_name)
78
- end
74
+ correct_node(corrector, node.body, offending_name, preferred_name)
79
75
  end
80
76
  end
81
77
 
@@ -86,6 +82,43 @@ module RuboCop
86
82
  variable.loc.expression
87
83
  end
88
84
 
85
+ def variable_name_matches?(node, name)
86
+ if node.masgn_type?
87
+ node.each_descendant(:lvasgn).any? do |lvasgn_node|
88
+ variable_name_matches?(lvasgn_node, name)
89
+ end
90
+ else
91
+ node.children.first == name
92
+ end
93
+ end
94
+
95
+ def correct_node(corrector, node, offending_name, preferred_name)
96
+ return unless node
97
+
98
+ node.each_node(:lvar, :lvasgn, :masgn) do |child_node|
99
+ next unless variable_name_matches?(child_node, offending_name)
100
+
101
+ corrector.replace(child_node, preferred_name) if child_node.lvar_type?
102
+
103
+ if child_node.masgn_type? || child_node.lvasgn_type?
104
+ correct_reassignment(corrector, child_node, offending_name, preferred_name)
105
+ break
106
+ end
107
+ end
108
+ end
109
+
110
+ # If the exception variable is reassigned, that assignment needs to be corrected.
111
+ # Further `lvar` nodes will not be corrected though since they now refer to a
112
+ # different variable.
113
+ def correct_reassignment(corrector, node, offending_name, preferred_name)
114
+ if node.lvasgn_type?
115
+ correct_node(corrector, node.child_nodes.first, offending_name, preferred_name)
116
+ elsif node.masgn_type?
117
+ # With multiple assign, the assignments are in an array as the last child
118
+ correct_node(corrector, node.children.last, offending_name, preferred_name)
119
+ end
120
+ end
121
+
89
122
  def preferred_name(variable_name)
90
123
  preferred_name = cop_config.fetch('PreferredName', 'e')
91
124
  if variable_name.to_s.start_with?('_')
@@ -92,7 +92,7 @@ module RuboCop
92
92
  # # good
93
93
  # :some_sym_1
94
94
  #
95
- # @example AllowedIdentifier: [capture3]
95
+ # @example AllowedIdentifiers: [capture3]
96
96
  # # good
97
97
  # expect(Open3).to receive(:capture3)
98
98
  #
@@ -185,7 +185,7 @@ module RuboCop
185
185
 
186
186
  def sort!
187
187
  clear_enrollment_queue
188
- @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }]
188
+ @registry = @registry.sort_by { |badge, _| badge.cop_name }.to_h
189
189
 
190
190
  self
191
191
  end
@@ -15,6 +15,7 @@ module RuboCop
15
15
  MSG = 'The use of `eval` is a serious security risk.'
16
16
  RESTRICT_ON_SEND = %i[eval].freeze
17
17
 
18
+ # @!method eval?(node)
18
19
  def_node_matcher :eval?, <<~PATTERN
19
20
  (send {nil? (send nil? :binding)} :eval $!str ...)
20
21
  PATTERN
@@ -28,6 +28,7 @@ module RuboCop
28
28
  MSG = 'Prefer `JSON.parse` over `JSON.%<method>s`.'
29
29
  RESTRICT_ON_SEND = %i[load restore].freeze
30
30
 
31
+ # @!method json_load(node)
31
32
  def_node_matcher :json_load, <<~PATTERN
32
33
  (send (const {nil? cbase} :JSON) ${:load :restore} ...)
33
34
  PATTERN
@@ -22,6 +22,7 @@ module RuboCop
22
22
  MSG = 'Avoid using `Marshal.%<method>s`.'
23
23
  RESTRICT_ON_SEND = %i[load restore].freeze
24
24
 
25
+ # @!method marshal_load(node)
25
26
  def_node_matcher :marshal_load, <<~PATTERN
26
27
  (send (const {nil? cbase} :Marshal) ${:load :restore}
27
28
  !(send (const {nil? cbase} :Marshal) :dump ...))
@@ -24,6 +24,7 @@ module RuboCop
24
24
  MSG = 'The use of `%<receiver>sopen` is a serious security risk.'
25
25
  RESTRICT_ON_SEND = %i[open].freeze
26
26
 
27
+ # @!method open?(node)
27
28
  def_node_matcher :open?, <<~PATTERN
28
29
  (send ${nil? (const {nil? cbase} :URI)} :open $!str ...)
29
30
  PATTERN
@@ -21,6 +21,7 @@ module RuboCop
21
21
  MSG = 'Prefer using `YAML.safe_load` over `YAML.load`.'
22
22
  RESTRICT_ON_SEND = %i[load].freeze
23
23
 
24
+ # @!method yaml_load(node)
24
25
  def_node_matcher :yaml_load, <<~PATTERN
25
26
  (send (const {nil? cbase} :YAML) :load ...)
26
27
  PATTERN
@@ -6,10 +6,10 @@ module RuboCop
6
6
  class Severity
7
7
  include Comparable
8
8
 
9
- NAMES = %i[refactor convention warning error fatal].freeze
9
+ NAMES = %i[info refactor convention warning error fatal].freeze
10
10
 
11
11
  # @api private
12
- CODE_TABLE = { R: :refactor, C: :convention,
12
+ CODE_TABLE = { I: :info, R: :refactor, C: :convention,
13
13
  W: :warning, E: :error, F: :fatal }.freeze
14
14
 
15
15
  # @api public
@@ -18,7 +18,7 @@ module RuboCop
18
18
  #
19
19
  # @return [Symbol]
20
20
  # severity.
21
- # any of `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.
21
+ # any of `:info`, `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.
22
22
  attr_reader :name
23
23
 
24
24
  def self.name_from_code(code)