rubocop 1.9.0 → 1.12.1

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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -14
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +70 -17
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +5 -0
  7. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  8. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
  9. data/lib/rubocop/comment_config.rb +43 -94
  10. data/lib/rubocop/config.rb +4 -1
  11. data/lib/rubocop/cop/base.rb +1 -0
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  14. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  15. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  16. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  17. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  18. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  19. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  20. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  21. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  22. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  23. data/lib/rubocop/cop/generator.rb +2 -2
  24. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  25. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
  26. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  27. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  29. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  30. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  31. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  36. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  37. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
  38. data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
  39. data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
  40. data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
  41. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  42. data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
  43. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
  46. data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
  47. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +20 -3
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  50. data/lib/rubocop/cop/layout/first_argument_indentation.rb +27 -7
  51. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
  52. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
  54. data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
  55. data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
  56. data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
  57. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  59. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -5
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +10 -5
  61. data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
  62. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  63. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  64. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  65. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  66. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  67. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  68. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  69. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  70. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  71. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  72. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  73. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  74. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  75. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  76. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  77. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  78. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  79. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  80. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  81. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  82. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  83. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  84. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  85. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  86. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  87. data/lib/rubocop/cop/lint/number_conversion.rb +11 -2
  88. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  89. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  90. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
  91. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  92. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  93. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  94. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  95. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  96. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  97. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  98. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  99. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  100. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  101. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  102. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  103. data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
  104. data/lib/rubocop/cop/lint/symbol_conversion.rb +91 -3
  105. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  106. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  107. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  108. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  109. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  110. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  111. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  112. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  113. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  114. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  115. data/lib/rubocop/cop/message_annotator.rb +4 -1
  116. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  117. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  118. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  119. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  120. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  121. data/lib/rubocop/cop/mixin/alignment.rb +10 -3
  122. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  123. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  124. data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
  125. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  126. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  127. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  128. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  129. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  130. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  131. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  132. data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
  133. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  134. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
  135. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -23
  136. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  137. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  138. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  139. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  140. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
  141. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  142. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  143. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  144. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
  145. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  146. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  147. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
  148. data/lib/rubocop/cop/registry.rb +10 -1
  149. data/lib/rubocop/cop/security/eval.rb +1 -0
  150. data/lib/rubocop/cop/security/json_load.rb +1 -0
  151. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  152. data/lib/rubocop/cop/security/open.rb +1 -0
  153. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  154. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -2
  155. data/lib/rubocop/cop/style/alias.rb +1 -0
  156. data/lib/rubocop/cop/style/and_or.rb +3 -1
  157. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  158. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  159. data/lib/rubocop/cop/style/array_join.rb +1 -0
  160. data/lib/rubocop/cop/style/attr.rb +1 -0
  161. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
  162. data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
  163. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  164. data/lib/rubocop/cop/style/case_like_if.rb +15 -4
  165. data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
  166. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  167. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  168. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  169. data/lib/rubocop/cop/style/commented_keyword.rb +10 -10
  170. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  171. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  172. data/lib/rubocop/cop/style/date_time.rb +3 -0
  173. data/lib/rubocop/cop/style/dir.rb +1 -0
  174. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  175. data/lib/rubocop/cop/style/documentation.rb +30 -3
  176. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  177. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  178. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  179. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  180. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  181. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  182. data/lib/rubocop/cop/style/eval_with_location.rb +90 -28
  183. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  184. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  185. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
  186. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  187. data/lib/rubocop/cop/style/float_division.rb +4 -0
  188. data/lib/rubocop/cop/style/format_string.rb +2 -0
  189. data/lib/rubocop/cop/style/format_string_token.rb +1 -0
  190. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +0 -3
  191. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  192. data/lib/rubocop/cop/style/hash_conversion.rb +108 -0
  193. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  194. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  195. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  196. data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
  197. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  198. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  199. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +37 -11
  200. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  201. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  202. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +46 -2
  203. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -3
  204. data/lib/rubocop/cop/style/min_max.rb +1 -0
  205. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  206. data/lib/rubocop/cop/style/module_function.rb +5 -0
  207. data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -3
  208. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  209. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  210. data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
  211. data/lib/rubocop/cop/style/nil_comparison.rb +6 -1
  212. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  213. data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
  214. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  215. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  216. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  217. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  218. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  219. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  220. data/lib/rubocop/cop/style/proc.rb +1 -0
  221. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  222. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  223. data/lib/rubocop/cop/style/redundant_begin.rb +44 -4
  224. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  225. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  226. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  227. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  228. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  229. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  230. data/lib/rubocop/cop/style/redundant_return.rb +4 -0
  231. data/lib/rubocop/cop/style/redundant_self.rb +7 -3
  232. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  233. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  234. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  235. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  236. data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
  237. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  238. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  239. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  240. data/lib/rubocop/cop/style/sample.rb +1 -0
  241. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  242. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  243. data/lib/rubocop/cop/style/single_line_methods.rb +4 -1
  244. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  245. data/lib/rubocop/cop/style/sole_nested_conditional.rb +20 -4
  246. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  247. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  248. data/lib/rubocop/cop/style/string_chars.rb +38 -0
  249. data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
  250. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  251. data/lib/rubocop/cop/style/strip.rb +1 -0
  252. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  253. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  254. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
  255. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
  256. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  257. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  258. data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
  259. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  260. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  261. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  262. data/lib/rubocop/cop/util.rb +4 -1
  263. data/lib/rubocop/directive_comment.rb +69 -9
  264. data/lib/rubocop/ext/regexp_parser.rb +3 -6
  265. data/lib/rubocop/formatter/clang_style_formatter.rb +4 -2
  266. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  267. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  268. data/lib/rubocop/formatter/tap_formatter.rb +4 -2
  269. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  270. data/lib/rubocop/magic_comment.rb +1 -1
  271. data/lib/rubocop/name_similarity.rb +1 -1
  272. data/lib/rubocop/target_finder.rb +1 -0
  273. data/lib/rubocop/target_ruby.rb +21 -13
  274. data/lib/rubocop/version.rb +1 -1
  275. metadata +14 -7
@@ -34,10 +34,12 @@ module RuboCop
34
34
 
35
35
  private
36
36
 
37
+ # @!method node_type_check(node)
37
38
  def_node_matcher :node_type_check, <<~PATTERN
38
39
  (send nil? :add_offense $_node $hash)
39
40
  PATTERN
40
41
 
42
+ # @!method offending_location_argument(node)
41
43
  def_node_matcher :offending_location_argument, <<~PATTERN
42
44
  (pair (sym :location) $(send (send $_node :loc) $_keyword))
43
45
  PATTERN
@@ -21,6 +21,7 @@ module RuboCop
21
21
 
22
22
  MSG = 'Remove the redundant `subject`%<additional_message>s.'
23
23
 
24
+ # @!method described_class_subject?(node)
24
25
  def_node_matcher :described_class_subject?, <<~PATTERN
25
26
  (block
26
27
  (send nil? :subject
@@ -25,6 +25,7 @@ module RuboCop
25
25
 
26
26
  MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'
27
27
 
28
+ # @!method let_rubocop_config_new?(node)
28
29
  def_node_matcher :let_rubocop_config_new?, <<~PATTERN
29
30
  (block
30
31
  (send nil? :let
@@ -23,6 +23,7 @@ module RuboCop
23
23
  MSG = 'Redundant location argument to `#add_offense`.'
24
24
  RESTRICT_ON_SEND = %i[add_offense].freeze
25
25
 
26
+ # @!method redundant_location_argument(node)
26
27
  def_node_matcher :redundant_location_argument, <<~PATTERN
27
28
  (send nil? :add_offense _
28
29
  (hash <$(pair (sym :location) (sym :expression)) ...>)
@@ -26,16 +26,19 @@ module RuboCop
26
26
  MSG = 'Redundant message argument to `#add_offense`.'
27
27
  RESTRICT_ON_SEND = %i[add_offense].freeze
28
28
 
29
+ # @!method node_type_check(node)
29
30
  def_node_matcher :node_type_check, <<~PATTERN
30
31
  (send nil? :add_offense $_node $hash)
31
32
  PATTERN
32
33
 
34
+ # @!method redundant_message_argument(node)
33
35
  def_node_matcher :redundant_message_argument, <<~PATTERN
34
36
  (pair
35
37
  (sym :message)
36
38
  ${(const nil? :MSG) (send nil? :message) (send nil? :message _)})
37
39
  PATTERN
38
40
 
41
+ # @!method message_method_call(node)
39
42
  def_node_matcher :message_method_call, '(send nil? :message $_node)'
40
43
 
41
44
  def on_send(node)
@@ -67,18 +67,22 @@ module RuboCop
67
67
  no_acceptable_style! style_detected
68
68
  ].freeze
69
69
 
70
+ # @!method correct_style_detected_check(node)
70
71
  def_node_matcher :correct_style_detected_check, <<~PATTERN
71
72
  (send nil? :correct_style_detected)
72
73
  PATTERN
73
74
 
75
+ # @!method negative_style_detected_method_check(node)
74
76
  def_node_matcher :negative_style_detected_method_check, <<~PATTERN
75
77
  (send nil? /(?:opposite|unexpected|ambiguous|unrecognized)_style_detected|conflicting_styles_detected/ ...)
76
78
  PATTERN
77
79
 
80
+ # @!method no_acceptable_style_check(node)
78
81
  def_node_matcher :no_acceptable_style_check, <<~PATTERN
79
82
  (send nil? :no_acceptable_style!)
80
83
  PATTERN
81
84
 
85
+ # @!method style_detected_check(node)
82
86
  def_node_matcher :style_detected_check, <<~PATTERN
83
87
  (send nil? :style_detected ...)
84
88
  PATTERN
@@ -16,10 +16,12 @@ module RuboCop
16
16
  class UselessMessageAssertion < Base
17
17
  MSG = 'Do not specify cop behavior using `described_class::MSG`.'
18
18
 
19
+ # @!method described_class_msg(node)
19
20
  def_node_search :described_class_msg, <<~PATTERN
20
21
  (const (send nil? :described_class) :MSG)
21
22
  PATTERN
22
23
 
24
+ # @!method rspec_expectation_on_msg?(node)
23
25
  def_node_matcher :rspec_expectation_on_msg?, <<~PATTERN
24
26
  (send (send nil? :expect #contains_described_class_msg?) :to ...)
25
27
  PATTERN
@@ -32,10 +32,11 @@ module RuboCop
32
32
  # private
33
33
  # def smooth; end
34
34
  # end
35
- class AccessModifierIndentation < Cop
35
+ class AccessModifierIndentation < Base
36
36
  include Alignment
37
37
  include ConfigurableEnforcedStyle
38
38
  include RangeHelp
39
+ extend AutoCorrector
39
40
 
40
41
  MSG = '%<style>s access modifiers like `%<node>s`.'
41
42
 
@@ -48,12 +49,12 @@ module RuboCop
48
49
  alias on_module on_class
49
50
  alias on_block on_class
50
51
 
51
- def autocorrect(node)
52
- AlignmentCorrector.correct(processed_source, node, @column_delta)
53
- end
54
-
55
52
  private
56
53
 
54
+ def autocorrect(corrector, node)
55
+ AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
56
+ end
57
+
57
58
  def check_body(body, node)
58
59
  modifiers = body.each_child_node(:send)
59
60
  .select(&:bare_access_modifier?)
@@ -69,18 +70,20 @@ module RuboCop
69
70
  if @column_delta.zero?
70
71
  correct_style_detected
71
72
  else
72
- add_offense(send_node) do
73
+ add_offense(send_node) do |corrector|
73
74
  if offset == unexpected_indent_offset
74
75
  opposite_style_detected
75
76
  else
76
77
  unrecognized_style_detected
77
78
  end
79
+
80
+ autocorrect(corrector, send_node)
78
81
  end
79
82
  end
80
83
  end
81
84
 
82
- def message(node)
83
- format(MSG, style: style.capitalize, node: node.loc.selector.source)
85
+ def message(range)
86
+ format(MSG, style: style.capitalize, node: range.source)
84
87
  end
85
88
 
86
89
  def expected_indent_offset
@@ -37,8 +37,9 @@ module RuboCop
37
37
  #
38
38
  # foo :bar,
39
39
  # :baz
40
- class ArgumentAlignment < Cop
40
+ class ArgumentAlignment < Base
41
41
  include Alignment
42
+ extend AutoCorrector
42
43
 
43
44
  ALIGN_PARAMS_MSG = 'Align the arguments of a method call if ' \
44
45
  'they span more than one line.'
@@ -54,12 +55,12 @@ module RuboCop
54
55
  end
55
56
  alias on_csend on_send
56
57
 
57
- def autocorrect(node)
58
- AlignmentCorrector.correct(processed_source, node, column_delta)
59
- end
60
-
61
58
  private
62
59
 
60
+ def autocorrect(corrector, node)
61
+ AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
62
+ end
63
+
63
64
  def message(_node)
64
65
  fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG
65
66
  end
@@ -33,8 +33,9 @@ module RuboCop
33
33
  #
34
34
  # array = [1, 2, 3,
35
35
  # 4, 5, 6]
36
- class ArrayAlignment < Cop
36
+ class ArrayAlignment < Base
37
37
  include Alignment
38
+ extend AutoCorrector
38
39
 
39
40
  ALIGN_ELEMENTS_MSG = 'Align the elements of an array literal ' \
40
41
  'if they span more than one line.'
@@ -49,13 +50,13 @@ module RuboCop
49
50
  check_alignment(node.children, base_column(node, node.children))
50
51
  end
51
52
 
52
- def autocorrect(node)
53
- AlignmentCorrector.correct(processed_source, node, column_delta)
54
- end
55
-
56
53
  private
57
54
 
58
- def message(_node)
55
+ def autocorrect(corrector, node)
56
+ AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
57
+ end
58
+
59
+ def message(_range)
59
60
  fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_ELEMENTS_MSG
60
61
  end
61
62
 
@@ -21,13 +21,16 @@ module RuboCop
21
21
  #
22
22
  # The indentation of the remaining lines can be corrected with
23
23
  # other cops such as `IndentationConsistency` and `EndAlignment`.
24
- class AssignmentIndentation < Cop
24
+ class AssignmentIndentation < Base
25
25
  include CheckAssignment
26
26
  include Alignment
27
+ extend AutoCorrector
27
28
 
28
29
  MSG = 'Indent the first line of the right-hand-side of a ' \
29
30
  'multi-line assignment.'
30
31
 
32
+ private
33
+
31
34
  def check_assignment(node, rhs)
32
35
  return unless rhs
33
36
  return unless node.loc.operator
@@ -37,8 +40,8 @@ module RuboCop
37
40
  check_alignment([rhs], base + configured_indentation_width)
38
41
  end
39
42
 
40
- def autocorrect(node)
41
- AlignmentCorrector.correct(processed_source, node, column_delta)
43
+ def autocorrect(corrector, node)
44
+ AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
42
45
  end
43
46
 
44
47
  def leftmost_multiple_assignment(node)
@@ -68,6 +68,7 @@ module RuboCop
68
68
 
69
69
  MSG = '%<current>s is not aligned with %<prefer>s%<alt_prefer>s.'
70
70
 
71
+ # @!method block_end_align_target?(node, child)
71
72
  def_node_matcher :block_end_align_target?, <<~PATTERN
72
73
  {assignment?
73
74
  splat
@@ -24,8 +24,9 @@ module RuboCop
24
24
  # blah { |i|
25
25
  # foo(i)
26
26
  # }
27
- class BlockEndNewline < Cop
27
+ class BlockEndNewline < Base
28
28
  include Alignment
29
+ extend AutoCorrector
29
30
 
30
31
  MSG = 'Expression at %<line>d, %<column>d should be on its own line.'
31
32
 
@@ -35,13 +36,8 @@ module RuboCop
35
36
  # If the end is on its own line, there is no offense
36
37
  return if begins_its_line?(node.loc.end)
37
38
 
38
- add_offense(node, location: :end)
39
- end
40
-
41
- def autocorrect(node)
42
- lambda do |corrector|
43
- corrector.replace(delimiter_range(node),
44
- "\n#{node.loc.end.source}#{offset(node)}")
39
+ add_offense(node.loc.end, message: message(node)) do |corrector|
40
+ corrector.replace(delimiter_range(node), "\n#{node.loc.end.source}#{offset(node)}")
45
41
  end
46
42
  end
47
43
 
@@ -147,6 +147,7 @@ module RuboCop
147
147
  MSG = '`%<category>s` is supposed to appear before ' \
148
148
  '`%<previous>s`.'
149
149
 
150
+ # @!method dynamic_constant?(node)
150
151
  def_node_matcher :dynamic_constant?, <<~PATTERN
151
152
  (casgn nil? _ (send ...))
152
153
  PATTERN
@@ -68,8 +68,9 @@ module RuboCop
68
68
  # )
69
69
  #
70
70
  #
71
- class ClosingParenthesisIndentation < Cop
71
+ class ClosingParenthesisIndentation < Base
72
72
  include Alignment
73
+ extend AutoCorrector
73
74
 
74
75
  MSG_INDENT = 'Indent `)` to column %<expected>d (not %<actual>d)'
75
76
 
@@ -89,12 +90,12 @@ module RuboCop
89
90
  end
90
91
  alias on_defs on_def
91
92
 
92
- def autocorrect(node)
93
- AlignmentCorrector.correct(processed_source, node, @column_delta)
94
- end
95
-
96
93
  private
97
94
 
95
+ def autocorrect(corrector, node)
96
+ AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
97
+ end
98
+
98
99
  def check(node, elements)
99
100
  if elements.empty?
100
101
  check_for_no_elements(node)
@@ -115,11 +116,10 @@ module RuboCop
115
116
 
116
117
  return if @column_delta.zero?
117
118
 
118
- add_offense(right_paren,
119
- location: right_paren,
120
- message: message(correct_column,
121
- left_paren,
122
- right_paren))
119
+ message = message(correct_column, left_paren, right_paren)
120
+ add_offense(right_paren, message: message) do |corrector|
121
+ autocorrect(corrector, right_paren)
122
+ end
123
123
  end
124
124
 
125
125
  def check_for_no_elements(node)
@@ -135,11 +135,10 @@ module RuboCop
135
135
  # select the first one of candidates to determine a specification.
136
136
  correct_column = candidates.first
137
137
  @column_delta = correct_column - right_paren.column
138
- add_offense(right_paren,
139
- location: right_paren,
140
- message: message(correct_column,
141
- left_paren,
142
- right_paren))
138
+ message = message(correct_column, left_paren, right_paren)
139
+ add_offense(right_paren, message: message) do |corrector|
140
+ autocorrect(corrector, right_paren)
141
+ end
143
142
  end
144
143
 
145
144
  def expected_column(left_paren, elements)
@@ -32,29 +32,30 @@ module RuboCop
32
32
  # true
33
33
  # end
34
34
  #
35
- class CommentIndentation < Cop
35
+ class CommentIndentation < Base
36
36
  include Alignment
37
+ extend AutoCorrector
37
38
 
38
39
  MSG = 'Incorrect indentation detected (column %<column>d ' \
39
40
  'instead of %<correct_comment_indentation>d).'
40
41
 
41
- def investigate(processed_source)
42
+ def on_new_investigation
42
43
  processed_source.comments.each { |comment| check(comment) }
43
44
  end
44
45
 
45
- def autocorrect(comment)
46
- corrections = autocorrect_preceding_comments(comment) <<
47
- autocorrect_one(comment)
48
- ->(corrector) { corrections.each { |corr| corr.call(corrector) } }
49
- end
50
-
51
46
  private
52
47
 
48
+ def autocorrect(corrector, comment)
49
+ autocorrect_preceding_comments(corrector, comment)
50
+
51
+ autocorrect_one(corrector, comment)
52
+ end
53
+
53
54
  # Corrects all comment lines that occur immediately before the given
54
55
  # comment and have the same indentation. This is to avoid a long chain
55
56
  # of correcting, saving the file, parsing and inspecting again, and
56
57
  # then correcting one more line, and so on.
57
- def autocorrect_preceding_comments(comment)
58
+ def autocorrect_preceding_comments(corrector, comment)
58
59
  comments = processed_source.comments
59
60
  index = comments.index(comment)
60
61
 
@@ -62,7 +63,7 @@ module RuboCop
62
63
  .reverse_each
63
64
  .each_cons(2)
64
65
  .take_while { |below, above| should_correct?(above, below) }
65
- .map { |_, above| autocorrect_one(above) }
66
+ .map { |_, above| autocorrect_one(corrector, above) }
66
67
  end
67
68
 
68
69
  def should_correct?(preceding_comment, reference_comment)
@@ -71,8 +72,8 @@ module RuboCop
71
72
  loc.line == ref_loc.line - 1 && loc.column == ref_loc.column
72
73
  end
73
74
 
74
- def autocorrect_one(comment)
75
- AlignmentCorrector.correct(processed_source, comment, @column_delta)
75
+ def autocorrect_one(corrector, comment)
76
+ AlignmentCorrector.correct(corrector, processed_source, comment, @column_delta)
76
77
  end
77
78
 
78
79
  def check(comment)
@@ -93,10 +94,9 @@ module RuboCop
93
94
  return if column == correct_comment_indentation
94
95
  end
95
96
 
96
- add_offense(
97
- comment,
98
- message: message(column, correct_comment_indentation)
99
- )
97
+ add_offense(comment, message: message(column, correct_comment_indentation)) do |corrector|
98
+ autocorrect(corrector, comment)
99
+ end
100
100
  end
101
101
 
102
102
  def message(column, correct_comment_indentation)
@@ -29,10 +29,11 @@ module RuboCop
29
29
  # else
30
30
  # code
31
31
  # end
32
- class ElseAlignment < Cop
32
+ class ElseAlignment < Base
33
33
  include EndKeywordAlignment
34
34
  include Alignment
35
35
  include CheckAssignment
36
+ extend AutoCorrector
36
37
 
37
38
  MSG = 'Align `%<else_range>s` with `%<base_range>s`.'
38
39
 
@@ -67,12 +68,12 @@ module RuboCop
67
68
  )
68
69
  end
69
70
 
70
- def autocorrect(node)
71
- AlignmentCorrector.correct(processed_source, node, column_delta)
72
- end
73
-
74
71
  private
75
72
 
73
+ def autocorrect(corrector, node)
74
+ AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
75
+ end
76
+
76
77
  def check_nested(node, base)
77
78
  on_if(node, base)
78
79
  ignore_node(node)
@@ -140,7 +141,9 @@ module RuboCop
140
141
  else_range: else_range.source,
141
142
  base_range: base_range.source[/^\S*/]
142
143
  )
143
- add_offense(else_range, location: else_range, message: message)
144
+ add_offense(else_range, message: message) do |corrector|
145
+ autocorrect(corrector, else_range)
146
+ end
144
147
  end
145
148
 
146
149
  def assignment_node(node)