rubocop 1.88.0 → 1.89.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 (197) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +72 -12
  3. data/lib/rubocop/comment_config.rb +35 -19
  4. data/lib/rubocop/config.rb +53 -0
  5. data/lib/rubocop/config_validator.rb +38 -1
  6. data/lib/rubocop/cop/badge.rb +8 -0
  7. data/lib/rubocop/cop/base.rb +19 -12
  8. data/lib/rubocop/cop/bundler/gem_comment.rb +3 -1
  9. data/lib/rubocop/cop/bundler.rb +19 -0
  10. data/lib/rubocop/cop/commissioner.rb +13 -11
  11. data/lib/rubocop/cop/corrector.rb +1 -1
  12. data/lib/rubocop/cop/correctors/alignment_corrector.rb +55 -5
  13. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
  14. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -1
  15. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +8 -1
  16. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  17. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +12 -2
  18. data/lib/rubocop/cop/gemspec/require_mfa.rb +4 -1
  19. data/lib/rubocop/cop/gemspec.rb +22 -0
  20. data/lib/rubocop/cop/generator/registration_injector.rb +188 -0
  21. data/lib/rubocop/cop/generator.rb +4 -0
  22. data/lib/rubocop/cop/layout/block_alignment.rb +17 -0
  23. data/lib/rubocop/cop/layout/class_structure.rb +14 -4
  24. data/lib/rubocop/cop/layout/comment_indentation.rb +9 -2
  25. data/lib/rubocop/cop/layout/condition_position.rb +13 -3
  26. data/lib/rubocop/cop/layout/else_alignment.rb +1 -14
  27. data/lib/rubocop/cop/layout/empty_comment.rb +8 -10
  28. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +14 -1
  29. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -1
  30. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +13 -14
  31. data/lib/rubocop/cop/layout/indentation_width.rb +31 -1
  32. data/lib/rubocop/cop/layout/leading_comment_space.rb +27 -0
  33. data/lib/rubocop/cop/layout/line_length.rb +95 -17
  34. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  35. data/lib/rubocop/cop/layout/redundant_line_break.rb +77 -0
  36. data/lib/rubocop/cop/layout/space_around_operators.rb +6 -2
  37. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +33 -34
  38. data/lib/rubocop/cop/layout.rb +114 -0
  39. data/lib/rubocop/cop/lazy_loader.rb +41 -0
  40. data/lib/rubocop/cop/lint/ambiguous_assignment.rb +9 -7
  41. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +71 -0
  42. data/lib/rubocop/cop/lint/assignment_in_condition.rb +13 -1
  43. data/lib/rubocop/cop/lint/constant_reassignment.rb +1 -10
  44. data/lib/rubocop/cop/lint/constant_resolution.rb +25 -0
  45. data/lib/rubocop/cop/lint/debugger.rb +13 -1
  46. data/lib/rubocop/cop/lint/deprecated_reference.rb +185 -0
  47. data/lib/rubocop/cop/lint/duplicate_methods.rb +202 -22
  48. data/lib/rubocop/cop/lint/inherit_exception.rb +53 -7
  49. data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -0
  50. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  51. data/lib/rubocop/cop/lint/missing_super.rb +31 -1
  52. data/lib/rubocop/cop/lint/name_typo.rb +223 -0
  53. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +7 -0
  54. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +0 -4
  55. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -0
  56. data/lib/rubocop/cop/lint/to_enum_arguments.rb +44 -2
  57. data/lib/rubocop/cop/lint/to_json.rb +8 -1
  58. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +32 -8
  59. data/lib/rubocop/cop/lint/unreachable_code.rb +9 -4
  60. data/lib/rubocop/cop/lint/unused_private_method.rb +157 -0
  61. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +10 -10
  62. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +2 -0
  63. data/lib/rubocop/cop/lint/void.rb +19 -8
  64. data/lib/rubocop/cop/lint.rb +176 -0
  65. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  66. data/lib/rubocop/cop/metrics/perceived_complexity.rb +38 -7
  67. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  68. data/lib/rubocop/cop/metrics.rb +31 -0
  69. data/lib/rubocop/cop/migration/department_name.rb +0 -4
  70. data/lib/rubocop/cop/migration.rb +13 -0
  71. data/lib/rubocop/cop/mixin/allowed_methods.rb +5 -5
  72. data/lib/rubocop/cop/mixin/allowed_pattern.rb +5 -1
  73. data/lib/rubocop/cop/mixin/check_line_breakable.rb +23 -5
  74. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +13 -5
  75. data/lib/rubocop/cop/mixin/forbidden_pattern.rb +5 -1
  76. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +23 -6
  77. data/lib/rubocop/cop/mixin/hash_subset.rb +8 -0
  78. data/lib/rubocop/cop/mixin/hash_transform_method.rb +4 -0
  79. data/lib/rubocop/cop/mixin/project_index_help.rb +140 -0
  80. data/lib/rubocop/cop/mixin/reparsed_equivalence.rb +171 -0
  81. data/lib/rubocop/cop/mixin/statement_modifier.rb +43 -7
  82. data/lib/rubocop/cop/mixin.rb +78 -77
  83. data/lib/rubocop/cop/naming/accessor_method_name.rb +24 -0
  84. data/lib/rubocop/cop/naming/file_name.rb +4 -3
  85. data/lib/rubocop/cop/naming/inclusive_language.rb +8 -2
  86. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +9 -0
  87. data/lib/rubocop/cop/naming/predicate_prefix.rb +33 -5
  88. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +9 -3
  89. data/lib/rubocop/cop/naming/variable_number.rb +3 -1
  90. data/lib/rubocop/cop/naming.rb +31 -0
  91. data/lib/rubocop/cop/offense.rb +10 -6
  92. data/lib/rubocop/cop/registry.rb +153 -43
  93. data/lib/rubocop/cop/security/io_methods.rb +1 -1
  94. data/lib/rubocop/cop/security/marshal_load.rb +1 -1
  95. data/lib/rubocop/cop/security.rb +19 -0
  96. data/lib/rubocop/cop/style/accessor_grouping.rb +11 -1
  97. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  98. data/lib/rubocop/cop/style/array_intersect.rb +24 -5
  99. data/lib/rubocop/cop/style/class_and_module_children.rb +78 -18
  100. data/lib/rubocop/cop/style/collection_compact.rb +1 -1
  101. data/lib/rubocop/cop/style/conditional_assignment.rb +22 -0
  102. data/lib/rubocop/cop/style/copyright.rb +6 -1
  103. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  104. data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -2
  105. data/lib/rubocop/cop/style/dir_empty.rb +4 -0
  106. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +41 -3
  107. data/lib/rubocop/cop/style/documentation.rb +47 -0
  108. data/lib/rubocop/cop/style/documentation_method.rb +5 -1
  109. data/lib/rubocop/cop/style/empty_case_condition.rb +12 -2
  110. data/lib/rubocop/cop/style/empty_class_definition.rb +8 -1
  111. data/lib/rubocop/cop/style/empty_heredoc.rb +4 -0
  112. data/lib/rubocop/cop/style/empty_literal.rb +7 -2
  113. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +30 -20
  114. data/lib/rubocop/cop/style/endless_method.rb +0 -10
  115. data/lib/rubocop/cop/style/env_home.rb +4 -0
  116. data/lib/rubocop/cop/style/even_odd.rb +11 -1
  117. data/lib/rubocop/cop/style/exact_regexp_match.rb +8 -1
  118. data/lib/rubocop/cop/style/file_null.rb +4 -2
  119. data/lib/rubocop/cop/style/format_string.rb +13 -1
  120. data/lib/rubocop/cop/style/hash_conversion.rb +14 -6
  121. data/lib/rubocop/cop/style/hash_lookup_method.rb +2 -6
  122. data/lib/rubocop/cop/style/hash_syntax.rb +2 -0
  123. data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -4
  124. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -52
  125. data/lib/rubocop/cop/style/if_with_semicolon.rb +9 -1
  126. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  127. data/lib/rubocop/cop/style/invertible_unless_condition.rb +25 -5
  128. data/lib/rubocop/cop/style/keyword_arguments_merging.rb +4 -0
  129. data/lib/rubocop/cop/style/keyword_parameters_order.rb +7 -3
  130. data/lib/rubocop/cop/style/lambda.rb +11 -1
  131. data/lib/rubocop/cop/style/lambda_call.rb +11 -0
  132. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +11 -0
  133. data/lib/rubocop/cop/style/map_into_array.rb +1 -1
  134. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -3
  135. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -5
  136. data/lib/rubocop/cop/style/method_def_parentheses.rb +5 -1
  137. data/lib/rubocop/cop/style/min_max_comparison.rb +3 -0
  138. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +40 -7
  139. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  140. data/lib/rubocop/cop/style/module_function.rb +4 -2
  141. data/lib/rubocop/cop/style/multiline_if_then.rb +7 -1
  142. data/lib/rubocop/cop/style/multiline_memoization.rb +7 -1
  143. data/lib/rubocop/cop/style/multiline_method_signature.rb +11 -4
  144. data/lib/rubocop/cop/style/multiple_comparison.rb +33 -22
  145. data/lib/rubocop/cop/style/mutable_constant.rb +5 -0
  146. data/lib/rubocop/cop/style/negative_array_index.rb +3 -1
  147. data/lib/rubocop/cop/style/nil_lambda.rb +8 -0
  148. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  149. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  150. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  151. data/lib/rubocop/cop/style/optional_arguments.rb +1 -0
  152. data/lib/rubocop/cop/style/parallel_assignment.rb +11 -2
  153. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  154. data/lib/rubocop/cop/style/perl_backrefs.rb +5 -3
  155. data/lib/rubocop/cop/style/redundant_assignment.rb +11 -0
  156. data/lib/rubocop/cop/style/redundant_constant_base.rb +37 -1
  157. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +13 -1
  158. data/lib/rubocop/cop/style/redundant_exception.rb +6 -0
  159. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +9 -0
  160. data/lib/rubocop/cop/style/redundant_filter_chain.rb +1 -1
  161. data/lib/rubocop/cop/style/redundant_format.rb +40 -3
  162. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +6 -1
  163. data/lib/rubocop/cop/style/redundant_line_continuation.rb +80 -129
  164. data/lib/rubocop/cop/style/redundant_parentheses.rb +63 -63
  165. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -4
  166. data/lib/rubocop/cop/style/redundant_self.rb +9 -0
  167. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +23 -4
  168. data/lib/rubocop/cop/style/safe_navigation.rb +10 -0
  169. data/lib/rubocop/cop/style/semicolon.rb +4 -4
  170. data/lib/rubocop/cop/style/single_line_do_end_block.rb +17 -4
  171. data/lib/rubocop/cop/style/sole_nested_conditional.rb +20 -3
  172. data/lib/rubocop/cop/style/static_class.rb +21 -0
  173. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -0
  174. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -1
  175. data/lib/rubocop/cop/style/ternary_parentheses.rb +11 -0
  176. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -8
  177. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  178. data/lib/rubocop/cop/style.rb +310 -0
  179. data/lib/rubocop/cop/team.rb +66 -7
  180. data/lib/rubocop/cop/variable_force/variable.rb +18 -6
  181. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  182. data/lib/rubocop/directive_comment.rb +2 -0
  183. data/lib/rubocop/feature_loader.rb +0 -6
  184. data/lib/rubocop/lsp/routes.rb +41 -3
  185. data/lib/rubocop/lsp/runtime.rb +4 -0
  186. data/lib/rubocop/lsp/server.rb +4 -0
  187. data/lib/rubocop/lsp/stdin_runner.rb +20 -0
  188. data/lib/rubocop/pending_cops_reporter.rb +1 -6
  189. data/lib/rubocop/project_index_loader.rb +16 -0
  190. data/lib/rubocop/result_cache.rb +6 -4
  191. data/lib/rubocop/rspec/shared_contexts.rb +14 -2
  192. data/lib/rubocop/runner.rb +100 -33
  193. data/lib/rubocop/server/cache.rb +10 -5
  194. data/lib/rubocop/target_ruby.rb +1 -1
  195. data/lib/rubocop/version.rb +1 -1
  196. data/lib/rubocop.rb +17 -626
  197. metadata +18 -3
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Cops for the `Layout` department. The department's cops are registered for lazy loading
6
+ # and their files are loaded on demand.
7
+ module Layout
8
+ extend LazyLoader
9
+
10
+ register_cop :AccessModifierIndentation, "#{__dir__}/layout/access_modifier_indentation"
11
+ register_cop :ArgumentAlignment, "#{__dir__}/layout/argument_alignment"
12
+ register_cop :ArrayAlignment, "#{__dir__}/layout/array_alignment"
13
+ register_cop :AssignmentIndentation, "#{__dir__}/layout/assignment_indentation"
14
+ register_cop :BeginEndAlignment, "#{__dir__}/layout/begin_end_alignment"
15
+ register_cop :BlockAlignment, "#{__dir__}/layout/block_alignment"
16
+ register_cop :BlockEndNewline, "#{__dir__}/layout/block_end_newline"
17
+ register_cop :CaseIndentation, "#{__dir__}/layout/case_indentation"
18
+ register_cop :ClassStructure, "#{__dir__}/layout/class_structure"
19
+ register_cop :ClosingHeredocIndentation, "#{__dir__}/layout/closing_heredoc_indentation"
20
+ register_cop :ClosingParenthesisIndentation, "#{__dir__}/layout/closing_parenthesis_indentation"
21
+ register_cop :CommentIndentation, "#{__dir__}/layout/comment_indentation"
22
+ register_cop :ConditionPosition, "#{__dir__}/layout/condition_position"
23
+ register_cop :DefEndAlignment, "#{__dir__}/layout/def_end_alignment"
24
+ register_cop :DotPosition, "#{__dir__}/layout/dot_position"
25
+ register_cop :ElseAlignment, "#{__dir__}/layout/else_alignment"
26
+ register_cop :EmptyComment, "#{__dir__}/layout/empty_comment"
27
+ register_cop :EmptyLineAfterGuardClause, "#{__dir__}/layout/empty_line_after_guard_clause"
28
+ register_cop :EmptyLineAfterMagicComment, "#{__dir__}/layout/empty_line_after_magic_comment"
29
+ register_cop :EmptyLineAfterMultilineCondition, "#{__dir__}/layout/empty_line_after_multiline_condition"
30
+ register_cop :EmptyLineBetweenDefs, "#{__dir__}/layout/empty_line_between_defs"
31
+ register_cop :EmptyLinesAfterModuleInclusion, "#{__dir__}/layout/empty_lines_after_module_inclusion"
32
+ register_cop :EmptyLinesAroundAccessModifier, "#{__dir__}/layout/empty_lines_around_access_modifier"
33
+ register_cop :EmptyLinesAroundArguments, "#{__dir__}/layout/empty_lines_around_arguments"
34
+ register_cop :EmptyLinesAroundAttributeAccessor, "#{__dir__}/layout/empty_lines_around_attribute_accessor"
35
+ register_cop :EmptyLinesAroundBeginBody, "#{__dir__}/layout/empty_lines_around_begin_body"
36
+ register_cop :EmptyLinesAroundBlockBody, "#{__dir__}/layout/empty_lines_around_block_body"
37
+ register_cop :EmptyLinesAroundClassBody, "#{__dir__}/layout/empty_lines_around_class_body"
38
+ register_cop :EmptyLinesAroundExceptionHandlingKeywords, "#{__dir__}/layout/empty_lines_around_exception_handling_keywords"
39
+ register_cop :EmptyLinesAroundMethodBody, "#{__dir__}/layout/empty_lines_around_method_body"
40
+ register_cop :EmptyLinesAroundModuleBody, "#{__dir__}/layout/empty_lines_around_module_body"
41
+ register_cop :EmptyLines, "#{__dir__}/layout/empty_lines"
42
+ register_cop :EndAlignment, "#{__dir__}/layout/end_alignment"
43
+ register_cop :EndOfLine, "#{__dir__}/layout/end_of_line"
44
+ register_cop :ExtraSpacing, "#{__dir__}/layout/extra_spacing"
45
+ register_cop :FirstArgumentIndentation, "#{__dir__}/layout/first_argument_indentation"
46
+ register_cop :FirstArrayElementIndentation, "#{__dir__}/layout/first_array_element_indentation"
47
+ register_cop :FirstArrayElementLineBreak, "#{__dir__}/layout/first_array_element_line_break"
48
+ register_cop :FirstHashElementIndentation, "#{__dir__}/layout/first_hash_element_indentation"
49
+ register_cop :FirstHashElementLineBreak, "#{__dir__}/layout/first_hash_element_line_break"
50
+ register_cop :FirstMethodArgumentLineBreak, "#{__dir__}/layout/first_method_argument_line_break"
51
+ register_cop :FirstMethodParameterLineBreak, "#{__dir__}/layout/first_method_parameter_line_break"
52
+ register_cop :FirstParameterIndentation, "#{__dir__}/layout/first_parameter_indentation"
53
+ register_cop :HashAlignment, "#{__dir__}/layout/hash_alignment"
54
+ register_cop :HeredocArgumentClosingParenthesis, "#{__dir__}/layout/heredoc_argument_closing_parenthesis"
55
+ register_cop :HeredocIndentation, "#{__dir__}/layout/heredoc_indentation"
56
+ register_cop :IndentationConsistency, "#{__dir__}/layout/indentation_consistency"
57
+ register_cop :IndentationStyle, "#{__dir__}/layout/indentation_style"
58
+ register_cop :IndentationWidth, "#{__dir__}/layout/indentation_width"
59
+ register_cop :InitialIndentation, "#{__dir__}/layout/initial_indentation"
60
+ register_cop :LeadingCommentSpace, "#{__dir__}/layout/leading_comment_space"
61
+ register_cop :LeadingEmptyLines, "#{__dir__}/layout/leading_empty_lines"
62
+ register_cop :LineContinuationLeadingSpace, "#{__dir__}/layout/line_continuation_leading_space"
63
+ register_cop :LineContinuationSpacing, "#{__dir__}/layout/line_continuation_spacing"
64
+ register_cop :LineEndStringConcatenationIndentation, "#{__dir__}/layout/line_end_string_concatenation_indentation"
65
+ register_cop :LineLength, "#{__dir__}/layout/line_length"
66
+ register_cop :MultilineArrayBraceLayout, "#{__dir__}/layout/multiline_array_brace_layout"
67
+ register_cop :MultilineArrayLineBreaks, "#{__dir__}/layout/multiline_array_line_breaks"
68
+ register_cop :MultilineAssignmentLayout, "#{__dir__}/layout/multiline_assignment_layout"
69
+ register_cop :MultilineBlockLayout, "#{__dir__}/layout/multiline_block_layout"
70
+ register_cop :MultilineHashBraceLayout, "#{__dir__}/layout/multiline_hash_brace_layout"
71
+ register_cop :MultilineHashKeyLineBreaks, "#{__dir__}/layout/multiline_hash_key_line_breaks"
72
+ register_cop :MultilineMethodArgumentLineBreaks, "#{__dir__}/layout/multiline_method_argument_line_breaks"
73
+ register_cop :MultilineMethodCallBraceLayout, "#{__dir__}/layout/multiline_method_call_brace_layout"
74
+ register_cop :MultilineMethodCallIndentation, "#{__dir__}/layout/multiline_method_call_indentation"
75
+ register_cop :MultilineMethodDefinitionBraceLayout, "#{__dir__}/layout/multiline_method_definition_brace_layout"
76
+ register_cop :MultilineMethodParameterLineBreaks, "#{__dir__}/layout/multiline_method_parameter_line_breaks"
77
+ register_cop :MultilineOperationIndentation, "#{__dir__}/layout/multiline_operation_indentation"
78
+ register_cop :ParameterAlignment, "#{__dir__}/layout/parameter_alignment"
79
+ register_cop :RedundantLineBreak, "#{__dir__}/layout/redundant_line_break"
80
+ register_cop :RescueEnsureAlignment, "#{__dir__}/layout/rescue_ensure_alignment"
81
+ register_cop :SingleLineBlockChain, "#{__dir__}/layout/single_line_block_chain"
82
+ register_cop :SpaceAfterColon, "#{__dir__}/layout/space_after_colon"
83
+ register_cop :SpaceAfterComma, "#{__dir__}/layout/space_after_comma"
84
+ register_cop :SpaceAfterMethodName, "#{__dir__}/layout/space_after_method_name"
85
+ register_cop :SpaceAfterNot, "#{__dir__}/layout/space_after_not"
86
+ register_cop :SpaceAfterSemicolon, "#{__dir__}/layout/space_after_semicolon"
87
+ register_cop :SpaceAroundBlockParameters, "#{__dir__}/layout/space_around_block_parameters"
88
+ register_cop :SpaceAroundEqualsInParameterDefault, "#{__dir__}/layout/space_around_equals_in_parameter_default"
89
+ register_cop :SpaceAroundKeyword, "#{__dir__}/layout/space_around_keyword"
90
+ register_cop :SpaceAroundMethodCallOperator, "#{__dir__}/layout/space_around_method_call_operator"
91
+ register_cop :SpaceAroundOperators, "#{__dir__}/layout/space_around_operators"
92
+ register_cop :SpaceBeforeBlockBraces, "#{__dir__}/layout/space_before_block_braces"
93
+ register_cop :SpaceBeforeBrackets, "#{__dir__}/layout/space_before_brackets"
94
+ register_cop :SpaceBeforeComma, "#{__dir__}/layout/space_before_comma"
95
+ register_cop :SpaceBeforeComment, "#{__dir__}/layout/space_before_comment"
96
+ register_cop :SpaceBeforeFirstArg, "#{__dir__}/layout/space_before_first_arg"
97
+ register_cop :SpaceBeforeSemicolon, "#{__dir__}/layout/space_before_semicolon"
98
+ register_cop :SpaceInLambdaLiteral, "#{__dir__}/layout/space_in_lambda_literal"
99
+ register_cop :SpaceInsideArrayPercentLiteral, "#{__dir__}/layout/space_inside_array_percent_literal"
100
+ register_cop :SpaceInsideArrayLiteralBrackets, "#{__dir__}/layout/space_inside_array_literal_brackets"
101
+ register_cop :SpaceInsideBlockBraces, "#{__dir__}/layout/space_inside_block_braces"
102
+ register_cop :SpaceInsideHashLiteralBraces, "#{__dir__}/layout/space_inside_hash_literal_braces"
103
+ register_cop :SpaceInsideParens, "#{__dir__}/layout/space_inside_parens"
104
+ register_cop :SpaceInsidePercentLiteralDelimiters, "#{__dir__}/layout/space_inside_percent_literal_delimiters"
105
+ register_cop :SpaceInsideRangeLiteral, "#{__dir__}/layout/space_inside_range_literal"
106
+ register_cop :SpaceInsideReferenceBrackets, "#{__dir__}/layout/space_inside_reference_brackets"
107
+ register_cop :SpaceInsideStringInterpolation, "#{__dir__}/layout/space_inside_string_interpolation"
108
+ register_cop :TrailingEmptyLines, "#{__dir__}/layout/trailing_empty_lines"
109
+ register_cop :TrailingWhitespace, "#{__dir__}/layout/trailing_whitespace"
110
+
111
+ autoload :EmptyLinesAroundBody, "#{__dir__}/mixin/empty_lines_around_body"
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Extend a department module with this module and call `register_cop` to register
6
+ # the department's cops with the global registry without loading their files until
7
+ # a cop class itself is needed:
8
+ #
9
+ # module RuboCop
10
+ # module Cop
11
+ # # The Foo department.
12
+ # module Foo
13
+ # extend LazyLoader
14
+ #
15
+ # register_cop :BarBaz, "#{__dir__}/foo/bar_baz"
16
+ # end
17
+ # end
18
+ # end
19
+ module LazyLoader
20
+ # Registers a cop for lazy loading. The cop appears in the registry by name right away,
21
+ # but its file is loaded only when the cop class is needed, e.g. when the cop is enabled
22
+ # for an inspection run.
23
+ #
24
+ # The path must be absolute so that loading does not depend on `$LOAD_PATH` and cannot resolve
25
+ # to another RuboCop installation.
26
+ #
27
+ # @param constant_name [Symbol] name of the cop class, e.g. `:BarBaz`
28
+ # @param path [String] absolute path to the file defining the cop class,
29
+ # with or without the `.rb` extension
30
+ def register_cop(constant_name, path)
31
+ raise ArgumentError, "cop path must be absolute: #{path}" unless File.absolute_path?(path)
32
+
33
+ autoload constant_name, path
34
+
35
+ qualified_name = "#{name}::#{constant_name}"
36
+
37
+ Registry.global.lazy_load(Badge.for(qualified_name), qualified_name)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -19,25 +19,27 @@ module RuboCop
19
19
  # x != y # or x = !y
20
20
  #
21
21
  class AmbiguousAssignment < Base
22
+ include CheckAssignment
22
23
  include RangeHelp
23
24
 
24
25
  MSG = 'Suspicious assignment detected. Did you mean `%<op>s`?'
25
26
 
26
- SIMPLE_ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn].freeze
27
-
28
27
  MISTAKES = { '=-' => '-=', '=+' => '+=', '=*' => '*=', '=!' => '!=' }.freeze
29
28
 
30
- def on_asgn(node)
31
- return unless (rhs = node.expression)
29
+ alias on_csend on_send
30
+
31
+ private
32
32
 
33
- range = range_between(node.loc.operator.end_pos - 1, rhs.source_range.begin_pos + 1)
33
+ def check_assignment(node, rhs)
34
+ return unless rhs
35
+ return unless (operator = node.loc.operator)
36
+
37
+ range = range_between(operator.end_pos - 1, rhs.source_range.begin_pos + 1)
34
38
  source = range.source
35
39
  return unless MISTAKES.key?(source)
36
40
 
37
41
  add_offense(range, message: format(MSG, op: MISTAKES[source]))
38
42
  end
39
-
40
- SIMPLE_ASSIGNMENT_TYPES.each { |asgn_type| alias_method :"on_#{asgn_type}", :on_asgn }
41
43
  end
42
44
  end
43
45
  end
@@ -6,6 +6,11 @@ module RuboCop
6
6
  # Checks for ambiguous block association with method
7
7
  # when param passed without parentheses.
8
8
  #
9
+ # This cop also detects `do...end` blocks that are likely intended for
10
+ # an enumerable method in the arguments but actually bind to the outer
11
+ # method call. For example, in `render json: data.map do |x| x end`,
12
+ # Ruby parses the `do...end` block as belonging to `render`, not `map`.
13
+ #
9
14
  # This cop can customize allowed methods with `AllowedMethods`.
10
15
  # By default, there are no allowed methods.
11
16
  #
@@ -20,6 +25,18 @@ module RuboCop
20
25
  # # or (different meaning)
21
26
  # some_method(a) { |val| puts val }
22
27
  #
28
+ # # bad
29
+ # render json: data.map do |item|
30
+ # item.to_h
31
+ # end
32
+ #
33
+ # # good
34
+ # render json: data.map { |item| item.to_h }
35
+ #
36
+ # # good
37
+ # mapped = data.map { |item| item.to_h }
38
+ # render json: mapped
39
+ #
23
40
  # # good
24
41
  # # Operator methods require no disambiguation
25
42
  # foo == bar { |b| b.baz }
@@ -58,6 +75,19 @@ module RuboCop
58
75
  MSG = 'Parenthesize the param `%<param>s` to make sure that the ' \
59
76
  'block will be associated with the `%<method>s` method ' \
60
77
  'call.'
78
+ MSG_DO_END_BLOCK = '`%<inner_method>s` is called without a block because the ' \
79
+ '`do` block binds to `%<outer_method>s`. ' \
80
+ 'Use braces or extract to a variable.'
81
+
82
+ BLOCK_METHODS = %i[
83
+ map collect flat_map collect_concat
84
+ select filter find_all reject
85
+ find detect
86
+ each each_with_object each_with_index
87
+ reduce inject
88
+ sort_by min_by max_by
89
+ group_by filter_map
90
+ ].to_set.freeze
61
91
 
62
92
  def on_send(node)
63
93
  return unless node.arguments?
@@ -74,8 +104,36 @@ module RuboCop
74
104
  end
75
105
  alias on_csend on_send
76
106
 
107
+ def on_block(node)
108
+ return if node.braces?
109
+
110
+ send_node = node.send_node
111
+ block_method_arg = find_ambiguous_block_method(node)
112
+ return unless block_method_arg
113
+
114
+ add_offense(block_method_arg,
115
+ message: format(MSG_DO_END_BLOCK,
116
+ inner_method: block_method_arg.method_name,
117
+ outer_method: send_node.method_name))
118
+ end
119
+ alias on_numblock on_block
120
+ alias on_itblock on_block
121
+
77
122
  private
78
123
 
124
+ def find_ambiguous_block_method(block_node)
125
+ send_node = block_node.send_node
126
+ return unless send_node.send_type? && send_node.arguments?
127
+ return if send_node.parenthesized?
128
+
129
+ block_method_arg = find_block_method_arg(send_node)
130
+ return unless block_method_arg
131
+ return if allowed_method?(block_method_arg.method_name) ||
132
+ matches_allowed_pattern?(block_method_arg.source)
133
+
134
+ block_method_arg
135
+ end
136
+
79
137
  def ambiguous_block_association?(send_node)
80
138
  send_node.last_argument.any_block_type? && !send_node.last_argument.send_node.arguments?
81
139
  end
@@ -92,6 +150,19 @@ module RuboCop
92
150
  format(MSG, param: block_param.source, method: block_param.send_node.source)
93
151
  end
94
152
 
153
+ # Only the call the `do` block could actually have attached to is a candidate:
154
+ # the one whose source ends where the arguments end. This skips calls chained
155
+ # into another call (`foo bar.map.to_a`) and calls buried inside a
156
+ # parenthesized subcall (`foo bar(baz.map)`), which were never block candidates.
157
+ def find_block_method_arg(send_node)
158
+ end_pos = send_node.source_range.end_pos
159
+
160
+ send_node.last_argument.each_node(:call).find do |node|
161
+ node.source_range.end_pos == end_pos &&
162
+ BLOCK_METHODS.include?(node.method_name) && !node.arguments?
163
+ end
164
+ end
165
+
95
166
  def wrap_in_parentheses(corrector, node)
96
167
  range = node.loc.selector.end.join(node.first_argument.source_range.begin)
97
168
 
@@ -78,13 +78,25 @@ module RuboCop
78
78
  end
79
79
 
80
80
  def allowed_construct?(asgn_node)
81
- asgn_node.begin_type? || conditional_assignment?(asgn_node)
81
+ return true if asgn_node.begin_type?
82
+
83
+ conditional_assignment?(asgn_node) || discarded_assignment?(asgn_node)
82
84
  end
83
85
 
84
86
  def conditional_assignment?(asgn_node)
85
87
  !asgn_node.loc.operator
86
88
  end
87
89
 
90
+ # An assignment that is a statement of a multi-statement `begin`
91
+ # (e.g. `(foo = bar; baz)`) has its value discarded, so it is not used
92
+ # as the condition. Wrapping it in parentheses would only conflict with
93
+ # `Style/RedundantParentheses`, so it is left alone.
94
+ def discarded_assignment?(asgn_node)
95
+ parent = asgn_node.parent
96
+
97
+ parent&.begin_type? && parent.children.size > 1
98
+ end
99
+
88
100
  def skip_children?(asgn_node)
89
101
  (asgn_node.call_type? && !asgn_node.assignment_method?) ||
90
102
  empty_condition?(asgn_node) ||
@@ -209,21 +209,12 @@ module RuboCop
209
209
  def report_cross_file_collision(node, fully_qualified_name, display_name)
210
210
  return unless project_index
211
211
  return unless (declaration = project_index[fully_qualified_name.delete_prefix('::')])
212
- return unless (prior = prior_definition_in_other_file(declaration))
212
+ return unless (prior = prior_definition_in_other_file(declaration.definitions))
213
213
 
214
214
  msg = format(CROSS_FILE_MSG, constant: display_name, path: prior.location.to_file_path)
215
215
 
216
216
  add_offense(node, message: msg)
217
217
  end
218
-
219
- def prior_definition_in_other_file(declaration)
220
- current = processed_source.file_path
221
-
222
- declaration.definitions.find do |definition|
223
- other = definition.location.to_file_path
224
- !File.identical?(other, current)
225
- end
226
- end
227
218
  end
228
219
  end
229
220
  end
@@ -20,6 +20,12 @@ module RuboCop
20
20
  # to prevent conflicting rules. This is because it respects user configurations
21
21
  # that want to enable this cop which is disabled by default.
22
22
  #
23
+ # When `AllCops/UseProjectIndex` is enabled and the `rubydex` gem is
24
+ # installed, only genuinely ambiguous constants are reported: those
25
+ # that resolve to a different declaration through the surrounding
26
+ # nesting than they would fully qualified. This makes the cop practical
27
+ # to enable without `Only`/`Ignore` lists.
28
+ #
23
29
  # @example
24
30
  # # By default checks every constant
25
31
  #
@@ -60,6 +66,8 @@ module RuboCop
60
66
  # Login
61
67
  #
62
68
  class ConstantResolution < Base
69
+ include ProjectIndexHelp
70
+
63
71
  MSG = 'Fully qualify this constant to avoid possibly ambiguous resolution.'
64
72
 
65
73
  # @!method unqualified_const?(node)
@@ -69,12 +77,29 @@ module RuboCop
69
77
 
70
78
  def on_const(node)
71
79
  return if !unqualified_const?(node) || node.parent&.defined_module || node.loc.nil?
80
+ return if project_index && !ambiguous_resolution?(node)
72
81
 
73
82
  add_offense(node)
74
83
  end
75
84
 
76
85
  private
77
86
 
87
+ # When `AllCops/UseProjectIndex` is enabled, only genuinely ambiguous
88
+ # references are reported: the constant resolves to a different
89
+ # declaration through the lexical nesting than it would fully
90
+ # qualified. Names that resolve identically either way, or that the
91
+ # index cannot resolve at all (e.g. constants from gems), are not
92
+ # reported.
93
+ def ambiguous_resolution?(node)
94
+ name = node.short_name.to_s
95
+ relative = project_index.resolve_constant(name, lexical_nesting_of(node))
96
+ absolute = project_index.resolve_constant(name, [])
97
+
98
+ !relative.nil? && !absolute.nil? && relative.name != absolute.name
99
+ rescue StandardError
100
+ false
101
+ end
102
+
78
103
  def const_name?(name)
79
104
  name = name.to_s
80
105
  (allowed_names.empty? || allowed_names.include?(name)) && !ignored_names.include?(name)
@@ -75,9 +75,10 @@ module RuboCop
75
75
  MSG = 'Remove debugger entry point `%<source>s`.'
76
76
 
77
77
  def on_send(node)
78
+ return unless debugger_method?(node) || debugger_require?(node)
78
79
  return if assumed_usage_context?(node)
79
80
 
80
- add_offense(node) if debugger_method?(node) || debugger_require?(node)
81
+ add_offense(node)
81
82
  end
82
83
 
83
84
  private
@@ -101,9 +102,20 @@ module RuboCop
101
102
  end
102
103
 
103
104
  def debugger_method?(send_node)
105
+ return false unless debugger_method_names.include?(send_node.method_name)
106
+
104
107
  debugger_methods.include?(chained_method_name(send_node))
105
108
  end
106
109
 
110
+ # The last segment of each configured debugger method, used to cheaply
111
+ # rule out the vast majority of `send` nodes before building the
112
+ # chained method name.
113
+ def debugger_method_names
114
+ @debugger_method_names ||= debugger_methods.to_set do |method|
115
+ method.to_s.split('.').last&.to_sym
116
+ end
117
+ end
118
+
107
119
  def debugger_require?(send_node)
108
120
  return false unless send_node.method?(:require) && send_node.arguments.one?
109
121
  return false unless (argument = send_node.first_argument).str_type?
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for calls to methods and references to constants that are documented
7
+ # as deprecated with a YARD `@deprecated` tag.
8
+ #
9
+ # The check is powered by the project-wide index, so it only runs when
10
+ # `AllCops/UseProjectIndex` is enabled and the `rubydex` gem is installed.
11
+ # Without the index the cop does nothing.
12
+ #
13
+ # Only references that can be resolved without type inference are checked:
14
+ # constants, method calls without an explicit receiver (or with `self`),
15
+ # which are looked up in the enclosing class or module and its ancestry,
16
+ # and calls whose receiver is a constant, which are looked up in that
17
+ # namespace's singleton class. Calls on arbitrary objects are not checked.
18
+ #
19
+ # References made from a definition that is itself deprecated are allowed,
20
+ # so deprecated implementations can keep calling each other.
21
+ #
22
+ # @example
23
+ # # Given a deprecated method and constant:
24
+ # #
25
+ # # class Api
26
+ # # # @deprecated Use `#new_method` instead.
27
+ # # def old_method
28
+ # # end
29
+ # #
30
+ # # # @deprecated
31
+ # # OLD_TIMEOUT = 10
32
+ # # end
33
+ #
34
+ # # bad
35
+ # class Client < Api
36
+ # def call
37
+ # old_method
38
+ # end
39
+ #
40
+ # def timeout
41
+ # OLD_TIMEOUT
42
+ # end
43
+ # end
44
+ #
45
+ # # good
46
+ # class Client < Api
47
+ # def call
48
+ # new_method
49
+ # end
50
+ #
51
+ # def timeout
52
+ # NEW_TIMEOUT
53
+ # end
54
+ # end
55
+ #
56
+ class DeprecatedReference < Base
57
+ include ProjectIndexHelp
58
+
59
+ METHOD_MSG = 'Method `%<name>s` is deprecated%<detail>s'
60
+ CONSTANT_MSG = 'Constant `%<name>s` is deprecated%<detail>s'
61
+
62
+ DEPRECATED_TAG = '@deprecated'
63
+ DEPRECATION_DETAIL = /#{DEPRECATED_TAG}\s+(.+)/.freeze
64
+
65
+ def on_new_investigation
66
+ @namespace_cache = {}
67
+ super
68
+ end
69
+
70
+ def on_send(node)
71
+ return unless project_index
72
+
73
+ declaration = method_declaration_for(node)
74
+ return unless declaration && deprecated?(declaration)
75
+ return if within_deprecated_definition?(node)
76
+
77
+ message = format(METHOD_MSG, name: node.method_name, detail: detail_for(declaration))
78
+ add_offense(node.loc.selector, message: message)
79
+ end
80
+ alias on_csend on_send
81
+
82
+ def on_const(node)
83
+ return unless project_index
84
+ return if node.parent&.defined_module
85
+
86
+ declaration = resolve_constant_in_index(node)
87
+ return unless declaration && deprecated?(declaration)
88
+ return if within_deprecated_definition?(node)
89
+
90
+ message = format(CONSTANT_MSG, name: node.const_name, detail: detail_for(declaration))
91
+ add_offense(node, message: message)
92
+ end
93
+
94
+ private
95
+
96
+ def method_declaration_for(node)
97
+ receiver = node.receiver
98
+
99
+ if receiver.nil? || receiver.self_type?
100
+ implicit_receiver_declaration(node)
101
+ elsif receiver.const_type?
102
+ const_receiver_declaration(node, receiver)
103
+ end
104
+ end
105
+
106
+ def implicit_receiver_declaration(node)
107
+ namespace = enclosing_namespace(node)
108
+ return nil unless namespace.is_a?(Rubydex::Namespace)
109
+
110
+ if singleton_context?(node)
111
+ indexed_singleton_member(namespace, "#{node.method_name}()")
112
+ else
113
+ namespace.find_member("#{node.method_name}()")
114
+ end
115
+ end
116
+
117
+ def const_receiver_declaration(node, receiver)
118
+ namespace = resolve_constant_in_index(receiver)
119
+ return nil unless namespace.is_a?(Rubydex::Namespace)
120
+
121
+ indexed_singleton_member(namespace, "#{node.method_name}()")
122
+ end
123
+
124
+ def enclosing_namespace(node)
125
+ namespace_node = node.each_ancestor(:class, :module).first
126
+ return project_index['Object'] unless namespace_node
127
+
128
+ @namespace_cache.fetch(namespace_node) do
129
+ @namespace_cache[namespace_node] = resolve_constant_in_index(namespace_node.identifier)
130
+ end
131
+ end
132
+
133
+ # Whether an implicit-receiver call runs with the class or module itself
134
+ # as `self` (directly in a namespace body, in a singleton method, or
135
+ # inside `class << self`) rather than inside an instance method.
136
+ def singleton_context?(node)
137
+ instance_def_seen = false
138
+
139
+ node.each_ancestor do |ancestor|
140
+ case ancestor.type
141
+ when :defs, :sclass
142
+ return true
143
+ when :def
144
+ instance_def_seen = true
145
+ when :class, :module
146
+ return !instance_def_seen
147
+ end
148
+ end
149
+
150
+ false
151
+ end
152
+
153
+ def deprecated?(declaration)
154
+ definitions = declaration.definitions.to_a
155
+
156
+ definitions.any? && definitions.all?(&:deprecated?)
157
+ end
158
+
159
+ # References from a definition that is itself documented as deprecated
160
+ # are allowed.
161
+ def within_deprecated_definition?(node)
162
+ node.each_ancestor(:any_def, :class, :module).any? do |ancestor|
163
+ comments = processed_source.ast_with_comments[ancestor]
164
+ comments.any? { |comment| comment.text.include?(DEPRECATED_TAG) }
165
+ end
166
+ end
167
+
168
+ def detail_for(declaration)
169
+ text = deprecation_text(declaration)
170
+ return '.' if text.nil? || text.empty?
171
+
172
+ text.end_with?('.') ? ": #{text}" : ": #{text}."
173
+ end
174
+
175
+ def deprecation_text(declaration)
176
+ tag_comment = declaration.definitions.to_a.filter_map do |definition|
177
+ definition.comments.to_a.map(&:string).find { |string| string.include?(DEPRECATED_TAG) }
178
+ end.first
179
+
180
+ tag_comment && tag_comment[DEPRECATION_DETAIL, 1]&.strip
181
+ end
182
+ end
183
+ end
184
+ end
185
+ end