rubocop 1.29.1 → 1.44.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (628) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +6 -4
  4. data/config/default.yml +341 -37
  5. data/config/obsoletion.yml +25 -1
  6. data/exe/rubocop +15 -7
  7. data/lib/rubocop/arguments_env.rb +17 -0
  8. data/lib/rubocop/arguments_file.rb +17 -0
  9. data/lib/rubocop/cache_config.rb +29 -0
  10. data/lib/rubocop/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +20 -5
  11. data/lib/rubocop/cli/command/execute_runner.rb +8 -8
  12. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  13. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  14. data/lib/rubocop/cli/command/suggest_extensions.rb +61 -16
  15. data/lib/rubocop/cli.rb +3 -1
  16. data/lib/rubocop/comment_config.rb +41 -1
  17. data/lib/rubocop/config.rb +45 -17
  18. data/lib/rubocop/config_finder.rb +68 -0
  19. data/lib/rubocop/config_loader.rb +41 -61
  20. data/lib/rubocop/config_loader_resolver.rb +8 -8
  21. data/lib/rubocop/config_obsoletion/changed_parameter.rb +5 -0
  22. data/lib/rubocop/config_obsoletion/parameter_rule.rb +4 -0
  23. data/lib/rubocop/config_obsoletion.rb +7 -2
  24. data/lib/rubocop/config_validator.rb +22 -5
  25. data/lib/rubocop/cop/autocorrect_logic.rb +4 -2
  26. data/lib/rubocop/cop/badge.rb +9 -4
  27. data/lib/rubocop/cop/base.rb +89 -75
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  29. data/lib/rubocop/cop/bundler/gem_filename.rb +5 -5
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -2
  31. data/lib/rubocop/cop/commissioner.rb +11 -4
  32. data/lib/rubocop/cop/cop.rb +30 -30
  33. data/lib/rubocop/cop/corrector.rb +32 -12
  34. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
  35. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  36. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
  37. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  38. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +4 -2
  39. data/lib/rubocop/cop/correctors/if_then_corrector.rb +1 -1
  40. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
  41. data/lib/rubocop/cop/correctors/line_break_corrector.rb +7 -1
  42. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +25 -9
  43. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +2 -7
  44. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +59 -1
  45. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  46. data/lib/rubocop/cop/correctors/punctuation_corrector.rb +1 -1
  47. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -1
  48. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +1 -1
  49. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +2 -2
  50. data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
  51. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +92 -0
  52. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  53. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -1
  54. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  55. data/lib/rubocop/cop/gemspec/require_mfa.rb +21 -21
  56. data/lib/rubocop/cop/generator/require_file_injector.rb +2 -2
  57. data/lib/rubocop/cop/generator.rb +6 -3
  58. data/lib/rubocop/cop/internal_affairs/cop_description.rb +98 -0
  59. data/lib/rubocop/cop/internal_affairs/create_empty_file.rb +37 -0
  60. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +1 -1
  61. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +111 -0
  62. data/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb +46 -0
  63. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -1
  64. data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +1 -1
  65. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -5
  66. data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
  67. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -1
  68. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +12 -4
  69. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  70. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  71. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +62 -0
  72. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +60 -0
  73. data/lib/rubocop/cop/internal_affairs.rb +7 -0
  74. data/lib/rubocop/cop/layout/argument_alignment.rb +22 -1
  75. data/lib/rubocop/cop/layout/assignment_indentation.rb +1 -1
  76. data/lib/rubocop/cop/layout/begin_end_alignment.rb +1 -1
  77. data/lib/rubocop/cop/layout/block_alignment.rb +17 -13
  78. data/lib/rubocop/cop/layout/block_end_newline.rb +42 -6
  79. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  80. data/lib/rubocop/cop/layout/class_structure.rb +32 -11
  81. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +3 -7
  82. data/lib/rubocop/cop/layout/comment_indentation.rb +4 -2
  83. data/lib/rubocop/cop/layout/condition_position.rb +1 -1
  84. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  85. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  86. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  87. data/lib/rubocop/cop/layout/empty_comment.rb +2 -2
  88. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  89. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
  90. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  91. data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
  92. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
  93. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -1
  94. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +25 -4
  95. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +1 -1
  96. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +7 -5
  97. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +10 -10
  98. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
  99. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +1 -1
  100. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +10 -10
  101. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  102. data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
  103. data/lib/rubocop/cop/layout/extra_spacing.rb +10 -6
  104. data/lib/rubocop/cop/layout/first_argument_indentation.rb +35 -29
  105. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +21 -14
  106. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +39 -3
  107. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +52 -13
  108. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +50 -3
  109. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +62 -3
  110. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +53 -3
  111. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  112. data/lib/rubocop/cop/layout/hash_alignment.rb +2 -0
  113. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  114. data/lib/rubocop/cop/layout/heredoc_indentation.rb +7 -10
  115. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -1
  116. data/lib/rubocop/cop/layout/indentation_style.rb +8 -3
  117. data/lib/rubocop/cop/layout/indentation_width.rb +14 -7
  118. data/lib/rubocop/cop/layout/initial_indentation.rb +2 -2
  119. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  120. data/lib/rubocop/cop/layout/leading_empty_lines.rb +1 -1
  121. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +138 -0
  122. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +136 -0
  123. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +1 -1
  124. data/lib/rubocop/cop/layout/line_length.rb +9 -2
  125. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +1 -1
  126. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +52 -3
  127. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
  128. data/lib/rubocop/cop/layout/multiline_block_layout.rb +5 -3
  129. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +1 -1
  130. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +50 -3
  131. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +58 -4
  132. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  133. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +3 -3
  134. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  135. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +101 -0
  136. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
  137. data/lib/rubocop/cop/layout/redundant_line_break.rb +4 -4
  138. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  139. data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
  140. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  141. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +2 -2
  142. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  143. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  144. data/lib/rubocop/cop/layout/space_before_block_braces.rb +5 -3
  145. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  146. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  147. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  148. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +21 -19
  149. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +3 -0
  150. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +33 -15
  151. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +30 -3
  152. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +34 -0
  153. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +6 -2
  154. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -1
  155. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +9 -9
  156. data/lib/rubocop/cop/layout/trailing_whitespace.rb +13 -6
  157. data/lib/rubocop/cop/legacy/corrections_proxy.rb +1 -1
  158. data/lib/rubocop/cop/legacy/corrector.rb +1 -1
  159. data/lib/rubocop/cop/lint/ambiguous_assignment.rb +1 -1
  160. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +28 -8
  161. data/lib/rubocop/cop/lint/ambiguous_operator.rb +5 -1
  162. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +1 -1
  163. data/lib/rubocop/cop/lint/ambiguous_range.rb +3 -3
  164. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +1 -1
  165. data/lib/rubocop/cop/lint/assignment_in_condition.rb +12 -2
  166. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
  167. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  168. data/lib/rubocop/cop/lint/circular_argument_reference.rb +1 -1
  169. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +51 -0
  170. data/lib/rubocop/cop/lint/constant_resolution.rb +4 -0
  171. data/lib/rubocop/cop/lint/debugger.rb +30 -18
  172. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +67 -111
  173. data/lib/rubocop/cop/lint/deprecated_constants.rb +9 -2
  174. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  175. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -3
  176. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
  177. data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +1 -1
  178. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +1 -1
  179. data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
  180. data/lib/rubocop/cop/lint/duplicate_methods.rb +48 -18
  181. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +26 -7
  182. data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
  183. data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +1 -1
  184. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  185. data/lib/rubocop/cop/lint/else_layout.rb +5 -9
  186. data/lib/rubocop/cop/lint/empty_block.rb +3 -7
  187. data/lib/rubocop/cop/lint/empty_class.rb +4 -2
  188. data/lib/rubocop/cop/lint/empty_conditional_body.rb +109 -3
  189. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  190. data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
  191. data/lib/rubocop/cop/lint/empty_file.rb +1 -1
  192. data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
  193. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  194. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  195. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  196. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
  197. data/lib/rubocop/cop/lint/flip_flop.rb +1 -1
  198. data/lib/rubocop/cop/lint/float_comparison.rb +1 -1
  199. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  200. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +11 -7
  201. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +16 -18
  202. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  203. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +1 -1
  204. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  205. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  206. data/lib/rubocop/cop/lint/interpolation_check.rb +6 -5
  207. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +2 -2
  208. data/lib/rubocop/cop/lint/literal_as_condition.rb +6 -1
  209. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +5 -1
  210. data/lib/rubocop/cop/lint/loop.rb +1 -1
  211. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +19 -4
  212. data/lib/rubocop/cop/lint/missing_super.rb +1 -1
  213. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  214. data/lib/rubocop/cop/lint/nested_method_definition.rb +51 -2
  215. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  216. data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
  217. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +162 -0
  218. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
  219. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
  220. data/lib/rubocop/cop/lint/number_conversion.rb +32 -10
  221. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
  222. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -3
  223. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +4 -5
  224. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +20 -1
  225. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +6 -1
  226. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  227. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  228. data/lib/rubocop/cop/lint/raise_exception.rb +1 -1
  229. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  230. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +44 -10
  231. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +3 -3
  232. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
  233. data/lib/rubocop/cop/lint/redundant_require_statement.rb +48 -7
  234. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +16 -3
  235. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  236. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  237. data/lib/rubocop/cop/lint/redundant_with_index.rb +14 -11
  238. data/lib/rubocop/cop/lint/redundant_with_object.rb +13 -12
  239. data/lib/rubocop/cop/lint/refinement_import_methods.rb +1 -1
  240. data/lib/rubocop/cop/lint/regexp_as_condition.rb +9 -3
  241. data/lib/rubocop/cop/lint/require_parentheses.rb +4 -2
  242. data/lib/rubocop/cop/lint/require_range_parentheses.rb +57 -0
  243. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  244. data/lib/rubocop/cop/lint/return_in_void_context.rb +1 -1
  245. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +44 -8
  246. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  247. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  248. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  249. data/lib/rubocop/cop/lint/self_assignment.rb +1 -1
  250. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +5 -4
  251. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  252. data/lib/rubocop/cop/lint/shadowed_exception.rb +16 -11
  253. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +29 -4
  254. data/lib/rubocop/cop/lint/struct_new_override.rb +3 -3
  255. data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
  256. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  257. data/lib/rubocop/cop/lint/syntax.rb +1 -1
  258. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  259. data/lib/rubocop/cop/lint/to_json.rb +1 -1
  260. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  261. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +2 -2
  262. data/lib/rubocop/cop/lint/triple_quotes.rb +1 -1
  263. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
  264. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +1 -1
  265. data/lib/rubocop/cop/lint/unified_integer.rb +3 -1
  266. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  267. data/lib/rubocop/cop/lint/unreachable_loop.rb +10 -4
  268. data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
  269. data/lib/rubocop/cop/lint/unused_method_argument.rb +7 -2
  270. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -1
  271. data/lib/rubocop/cop/lint/uri_regexp.rb +1 -1
  272. data/lib/rubocop/cop/lint/useless_access_modifier.rb +9 -7
  273. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  274. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +44 -0
  275. data/lib/rubocop/cop/lint/useless_method_definition.rb +4 -4
  276. data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
  277. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +16 -6
  278. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  279. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  280. data/lib/rubocop/cop/lint/void.rb +27 -16
  281. data/lib/rubocop/cop/metrics/abc_size.rb +5 -3
  282. data/lib/rubocop/cop/metrics/block_length.rb +16 -11
  283. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  284. data/lib/rubocop/cop/metrics/class_length.rb +10 -5
  285. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
  286. data/lib/rubocop/cop/metrics/method_length.rb +18 -12
  287. data/lib/rubocop/cop/metrics/module_length.rb +10 -5
  288. data/lib/rubocop/cop/metrics/parameter_lists.rb +28 -1
  289. data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
  290. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
  291. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +7 -4
  292. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  293. data/lib/rubocop/cop/mixin/allowed_identifiers.rb +2 -2
  294. data/lib/rubocop/cop/mixin/allowed_methods.rb +20 -1
  295. data/lib/rubocop/cop/mixin/allowed_pattern.rb +17 -1
  296. data/lib/rubocop/cop/mixin/annotation_comment.rb +13 -6
  297. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -1
  298. data/lib/rubocop/cop/mixin/comments_help.rb +17 -1
  299. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +21 -9
  300. data/lib/rubocop/cop/mixin/def_node.rb +2 -7
  301. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -1
  302. data/lib/rubocop/cop/mixin/first_element_line_break.rb +11 -7
  303. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
  304. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +146 -10
  305. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -6
  306. data/lib/rubocop/cop/mixin/line_length_help.rb +11 -2
  307. data/lib/rubocop/cop/mixin/method_complexity.rb +13 -16
  308. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +34 -12
  309. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +5 -3
  310. data/lib/rubocop/cop/mixin/percent_array.rb +58 -1
  311. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  312. data/lib/rubocop/cop/mixin/range_help.rb +33 -7
  313. data/lib/rubocop/cop/mixin/require_library.rb +2 -0
  314. data/lib/rubocop/cop/mixin/rescue_node.rb +5 -3
  315. data/lib/rubocop/cop/mixin/statement_modifier.rb +16 -1
  316. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  317. data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -8
  318. data/lib/rubocop/cop/mixin/visibility_help.rb +40 -5
  319. data/lib/rubocop/cop/naming/accessor_method_name.rb +4 -2
  320. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  321. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  322. data/lib/rubocop/cop/naming/block_forwarding.rb +7 -3
  323. data/lib/rubocop/cop/naming/block_parameter_name.rb +2 -2
  324. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +3 -1
  325. data/lib/rubocop/cop/naming/constant_name.rb +3 -3
  326. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  327. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  328. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  329. data/lib/rubocop/cop/naming/inclusive_language.rb +5 -2
  330. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  331. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  332. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  333. data/lib/rubocop/cop/naming/predicate_name.rb +30 -1
  334. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  335. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  336. data/lib/rubocop/cop/naming/variable_number.rb +18 -18
  337. data/lib/rubocop/cop/registry.rb +70 -44
  338. data/lib/rubocop/cop/security/compound_hash.rb +3 -2
  339. data/lib/rubocop/cop/security/eval.rb +1 -1
  340. data/lib/rubocop/cop/security/json_load.rb +1 -1
  341. data/lib/rubocop/cop/security/marshal_load.rb +1 -1
  342. data/lib/rubocop/cop/security/open.rb +1 -1
  343. data/lib/rubocop/cop/security/yaml_load.rb +1 -1
  344. data/lib/rubocop/cop/style/access_modifier_declarations.rb +84 -2
  345. data/lib/rubocop/cop/style/accessor_grouping.rb +11 -7
  346. data/lib/rubocop/cop/style/alias.rb +10 -2
  347. data/lib/rubocop/cop/style/and_or.rb +11 -11
  348. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  349. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  350. data/lib/rubocop/cop/style/array_intersect.rb +111 -0
  351. data/lib/rubocop/cop/style/array_join.rb +1 -1
  352. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  353. data/lib/rubocop/cop/style/attr.rb +1 -1
  354. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
  355. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  356. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  357. data/lib/rubocop/cop/style/block_comments.rb +2 -2
  358. data/lib/rubocop/cop/style/block_delimiters.rb +38 -11
  359. data/lib/rubocop/cop/style/case_equality.rb +41 -11
  360. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  361. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  362. data/lib/rubocop/cop/style/class_and_module_children.rb +7 -14
  363. data/lib/rubocop/cop/style/class_check.rb +1 -1
  364. data/lib/rubocop/cop/style/class_equality_comparison.rb +55 -6
  365. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  366. data/lib/rubocop/cop/style/class_methods_definitions.rb +3 -2
  367. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  368. data/lib/rubocop/cop/style/collection_compact.rb +13 -4
  369. data/lib/rubocop/cop/style/collection_methods.rb +3 -1
  370. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  371. data/lib/rubocop/cop/style/colon_method_definition.rb +1 -1
  372. data/lib/rubocop/cop/style/combinable_loops.rb +4 -2
  373. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  374. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  375. data/lib/rubocop/cop/style/commented_keyword.rb +4 -4
  376. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  377. data/lib/rubocop/cop/style/concat_array_literals.rb +86 -0
  378. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -7
  379. data/lib/rubocop/cop/style/constant_visibility.rb +1 -1
  380. data/lib/rubocop/cop/style/date_time.rb +1 -1
  381. data/lib/rubocop/cop/style/def_with_parentheses.rb +1 -1
  382. data/lib/rubocop/cop/style/dir.rb +4 -1
  383. data/lib/rubocop/cop/style/documentation.rb +12 -6
  384. data/lib/rubocop/cop/style/documentation_method.rb +1 -1
  385. data/lib/rubocop/cop/style/double_negation.rb +3 -1
  386. data/lib/rubocop/cop/style/each_for_simple_loop.rb +42 -7
  387. data/lib/rubocop/cop/style/each_with_object.rb +40 -9
  388. data/lib/rubocop/cop/style/empty_block_parameter.rb +2 -2
  389. data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
  390. data/lib/rubocop/cop/style/empty_else.rb +40 -3
  391. data/lib/rubocop/cop/style/empty_heredoc.rb +73 -0
  392. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +2 -2
  393. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  394. data/lib/rubocop/cop/style/empty_method.rb +18 -3
  395. data/lib/rubocop/cop/style/encoding.rb +2 -2
  396. data/lib/rubocop/cop/style/end_block.rb +1 -1
  397. data/lib/rubocop/cop/style/endless_method.rb +2 -2
  398. data/lib/rubocop/cop/style/env_home.rb +1 -1
  399. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  400. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  401. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  402. data/lib/rubocop/cop/style/explicit_block_argument.rb +6 -2
  403. data/lib/rubocop/cop/style/exponential_notation.rb +1 -1
  404. data/lib/rubocop/cop/style/fetch_env_var.rb +37 -224
  405. data/lib/rubocop/cop/style/float_division.rb +1 -1
  406. data/lib/rubocop/cop/style/for.rb +3 -1
  407. data/lib/rubocop/cop/style/format_string.rb +1 -1
  408. data/lib/rubocop/cop/style/format_string_token.rb +73 -23
  409. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +6 -3
  410. data/lib/rubocop/cop/style/global_std_stream.rb +1 -1
  411. data/lib/rubocop/cop/style/global_vars.rb +1 -1
  412. data/lib/rubocop/cop/style/guard_clause.rb +132 -43
  413. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +2 -1
  414. data/lib/rubocop/cop/style/hash_conversion.rb +1 -1
  415. data/lib/rubocop/cop/style/hash_each_methods.rb +49 -13
  416. data/lib/rubocop/cop/style/hash_except.rb +89 -9
  417. data/lib/rubocop/cop/style/hash_like_case.rb +1 -1
  418. data/lib/rubocop/cop/style/hash_syntax.rb +24 -3
  419. data/lib/rubocop/cop/style/hash_transform_keys.rb +6 -1
  420. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -1
  421. data/lib/rubocop/cop/style/identical_conditional_branches.rb +17 -2
  422. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  423. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +29 -4
  424. data/lib/rubocop/cop/style/if_with_semicolon.rb +4 -4
  425. data/lib/rubocop/cop/style/implicit_runtime_error.rb +3 -3
  426. data/lib/rubocop/cop/style/in_pattern_then.rb +1 -1
  427. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  428. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  429. data/lib/rubocop/cop/style/inverse_methods.rb +12 -8
  430. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  431. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  432. data/lib/rubocop/cop/style/keyword_parameters_order.rb +2 -2
  433. data/lib/rubocop/cop/style/lambda.rb +1 -1
  434. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  435. data/lib/rubocop/cop/style/line_end_concatenation.rb +6 -3
  436. data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
  437. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +136 -0
  438. data/lib/rubocop/cop/style/map_to_hash.rb +3 -3
  439. data/lib/rubocop/cop/style/map_to_set.rb +61 -0
  440. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +39 -19
  441. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -1
  442. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +8 -8
  443. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +22 -3
  444. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +5 -2
  445. data/lib/rubocop/cop/style/method_def_parentheses.rb +12 -5
  446. data/lib/rubocop/cop/style/min_max.rb +1 -1
  447. data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
  448. data/lib/rubocop/cop/style/missing_else.rb +37 -25
  449. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +1 -1
  450. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  451. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  452. data/lib/rubocop/cop/style/module_function.rb +30 -8
  453. data/lib/rubocop/cop/style/multiline_block_chain.rb +4 -2
  454. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -6
  455. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  456. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +3 -5
  457. data/lib/rubocop/cop/style/multiline_memoization.rb +3 -3
  458. data/lib/rubocop/cop/style/multiline_method_signature.rb +2 -2
  459. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -3
  460. data/lib/rubocop/cop/style/multiline_when_then.rb +2 -4
  461. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  462. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  463. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -7
  464. data/lib/rubocop/cop/style/nested_file_dirname.rb +1 -1
  465. data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
  466. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +11 -2
  467. data/lib/rubocop/cop/style/nested_ternary_operator.rb +20 -8
  468. data/lib/rubocop/cop/style/next.rb +3 -5
  469. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  470. data/lib/rubocop/cop/style/nil_lambda.rb +4 -4
  471. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  472. data/lib/rubocop/cop/style/not.rb +2 -2
  473. data/lib/rubocop/cop/style/numbered_parameters.rb +1 -1
  474. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +1 -1
  475. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  476. data/lib/rubocop/cop/style/numeric_literals.rb +16 -1
  477. data/lib/rubocop/cop/style/numeric_predicate.rb +53 -11
  478. data/lib/rubocop/cop/style/object_then.rb +6 -1
  479. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -7
  480. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  481. data/lib/rubocop/cop/style/operator_method_call.rb +67 -0
  482. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  483. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  484. data/lib/rubocop/cop/style/optional_boolean_parameter.rb +1 -1
  485. data/lib/rubocop/cop/style/or_assignment.rb +1 -1
  486. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
  487. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  488. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  489. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  490. data/lib/rubocop/cop/style/perl_backrefs.rb +23 -2
  491. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  492. data/lib/rubocop/cop/style/proc.rb +5 -2
  493. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
  494. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  495. data/lib/rubocop/cop/style/random_with_offset.rb +1 -1
  496. data/lib/rubocop/cop/style/redundant_argument.rb +5 -2
  497. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -1
  498. data/lib/rubocop/cop/style/redundant_begin.rb +20 -6
  499. data/lib/rubocop/cop/style/redundant_capital_w.rb +1 -1
  500. data/lib/rubocop/cop/style/redundant_condition.rb +33 -11
  501. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -5
  502. data/lib/rubocop/cop/style/redundant_constant_base.rb +85 -0
  503. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +45 -0
  504. data/lib/rubocop/cop/style/redundant_each.rb +116 -0
  505. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  506. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -2
  507. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
  508. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  509. data/lib/rubocop/cop/style/redundant_initialize.rb +3 -1
  510. data/lib/rubocop/cop/style/redundant_interpolation.rb +22 -1
  511. data/lib/rubocop/cop/style/redundant_parentheses.rb +22 -24
  512. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  513. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +10 -3
  514. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -4
  515. data/lib/rubocop/cop/style/redundant_return.rb +9 -2
  516. data/lib/rubocop/cop/style/redundant_self.rb +3 -1
  517. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
  518. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +1 -1
  519. data/lib/rubocop/cop/style/redundant_sort.rb +23 -8
  520. data/lib/rubocop/cop/style/redundant_sort_by.rb +25 -9
  521. data/lib/rubocop/cop/style/redundant_string_escape.rb +184 -0
  522. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  523. data/lib/rubocop/cop/style/require_order.rb +135 -0
  524. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
  525. data/lib/rubocop/cop/style/rescue_standard_error.rb +13 -13
  526. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  527. data/lib/rubocop/cop/style/safe_navigation.rb +43 -9
  528. data/lib/rubocop/cop/style/sample.rb +1 -1
  529. data/lib/rubocop/cop/style/select_by_regexp.rb +14 -6
  530. data/lib/rubocop/cop/style/self_assignment.rb +3 -3
  531. data/lib/rubocop/cop/style/semicolon.rb +53 -6
  532. data/lib/rubocop/cop/style/send.rb +1 -1
  533. data/lib/rubocop/cop/style/signal_exception.rb +9 -7
  534. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  535. data/lib/rubocop/cop/style/single_line_methods.rb +2 -2
  536. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
  537. data/lib/rubocop/cop/style/sole_nested_conditional.rb +42 -14
  538. data/lib/rubocop/cop/style/static_class.rb +33 -2
  539. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  540. data/lib/rubocop/cop/style/string_concatenation.rb +6 -7
  541. data/lib/rubocop/cop/style/string_hash_keys.rb +5 -2
  542. data/lib/rubocop/cop/style/string_literals.rb +1 -5
  543. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -1
  544. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  545. data/lib/rubocop/cop/style/strip.rb +1 -1
  546. data/lib/rubocop/cop/style/struct_inheritance.rb +4 -4
  547. data/lib/rubocop/cop/style/swap_values.rb +2 -2
  548. data/lib/rubocop/cop/style/symbol_array.rb +11 -6
  549. data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
  550. data/lib/rubocop/cop/style/symbol_proc.rb +46 -14
  551. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -14
  552. data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
  553. data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -1
  554. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -1
  555. data/lib/rubocop/cop/style/trailing_body_on_module.rb +1 -1
  556. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +5 -5
  557. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
  558. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +2 -2
  559. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
  560. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  561. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  562. data/lib/rubocop/cop/style/trivial_accessors.rb +4 -1
  563. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  564. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -1
  565. data/lib/rubocop/cop/style/unpack_first.rb +5 -2
  566. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  567. data/lib/rubocop/cop/style/when_then.rb +1 -1
  568. data/lib/rubocop/cop/style/word_array.rb +47 -5
  569. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  570. data/lib/rubocop/cop/style/yoda_expression.rb +81 -0
  571. data/lib/rubocop/cop/style/zero_length_predicate.rb +32 -15
  572. data/lib/rubocop/cop/team.rb +35 -36
  573. data/lib/rubocop/cop/util.rb +34 -7
  574. data/lib/rubocop/cop/variable_force/assignment.rb +1 -1
  575. data/lib/rubocop/cop/variable_force/variable_table.rb +4 -2
  576. data/lib/rubocop/cop/variable_force.rb +18 -30
  577. data/lib/rubocop/cops_documentation_generator.rb +53 -13
  578. data/lib/rubocop/directive_comment.rb +1 -1
  579. data/lib/rubocop/ext/processed_source.rb +2 -0
  580. data/lib/rubocop/ext/range.rb +15 -0
  581. data/lib/rubocop/feature_loader.rb +94 -0
  582. data/lib/rubocop/file_patterns.rb +43 -0
  583. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  584. data/lib/rubocop/formatter/disabled_config_formatter.rb +36 -16
  585. data/lib/rubocop/formatter/formatter_set.rb +20 -19
  586. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +15 -2
  587. data/lib/rubocop/formatter/html_formatter.rb +4 -5
  588. data/lib/rubocop/formatter/markdown_formatter.rb +3 -1
  589. data/lib/rubocop/formatter/offense_count_formatter.rb +10 -5
  590. data/lib/rubocop/formatter/simple_text_formatter.rb +9 -8
  591. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  592. data/lib/rubocop/formatter/worst_offenders_formatter.rb +6 -3
  593. data/lib/rubocop/formatter.rb +34 -0
  594. data/lib/rubocop/magic_comment.rb +27 -2
  595. data/lib/rubocop/options.rb +107 -53
  596. data/lib/rubocop/path_util.rb +50 -22
  597. data/lib/rubocop/rake_task.rb +34 -9
  598. data/lib/rubocop/result_cache.rb +26 -23
  599. data/lib/rubocop/rspec/cop_helper.rb +25 -2
  600. data/lib/rubocop/rspec/expect_offense.rb +9 -7
  601. data/lib/rubocop/rspec/shared_contexts.rb +42 -9
  602. data/lib/rubocop/rspec/support.rb +16 -2
  603. data/lib/rubocop/runner.rb +38 -19
  604. data/lib/rubocop/server/cache.rb +150 -0
  605. data/lib/rubocop/server/cli.rb +128 -0
  606. data/lib/rubocop/server/client_command/base.rb +44 -0
  607. data/lib/rubocop/server/client_command/exec.rb +64 -0
  608. data/lib/rubocop/server/client_command/restart.rb +25 -0
  609. data/lib/rubocop/server/client_command/start.rb +43 -0
  610. data/lib/rubocop/server/client_command/status.rb +28 -0
  611. data/lib/rubocop/server/client_command/stop.rb +31 -0
  612. data/lib/rubocop/server/client_command.rb +26 -0
  613. data/lib/rubocop/server/core.rb +96 -0
  614. data/lib/rubocop/server/errors.rb +23 -0
  615. data/lib/rubocop/server/helper.rb +34 -0
  616. data/lib/rubocop/server/server_command/base.rb +50 -0
  617. data/lib/rubocop/server/server_command/exec.rb +34 -0
  618. data/lib/rubocop/server/server_command/stop.rb +24 -0
  619. data/lib/rubocop/server/server_command.rb +21 -0
  620. data/lib/rubocop/server/socket_reader.rb +69 -0
  621. data/lib/rubocop/server.rb +53 -0
  622. data/lib/rubocop/target_finder.rb +1 -1
  623. data/lib/rubocop/target_ruby.rb +8 -7
  624. data/lib/rubocop/version.rb +22 -9
  625. data/lib/rubocop.rb +44 -35
  626. metadata +84 -12
  627. data/lib/rubocop/cop/gemspec/date_assignment.rb +0 -49
  628. data/lib/rubocop/cop/mixin/ignored_methods.rb +0 -52
@@ -6,7 +6,7 @@ module RuboCop
6
6
  #
7
7
  # This mixin makes it easier to specify strict offense expectations
8
8
  # in a declarative and visual fashion. Just type out the code that
9
- # should generate a offense, annotate code by writing '^'s
9
+ # should generate an offense, annotate code by writing '^'s
10
10
  # underneath each character that should be highlighted, and follow
11
11
  # the carets with a string (separated by a space) that is the
12
12
  # message of the offense. You can include multiple offenses in
@@ -38,7 +38,7 @@ module RuboCop
38
38
  # 'Avoid chaining a method call on a do...end block.'
39
39
  # )
40
40
  #
41
- # Auto-correction can be tested using `expect_correction` after
41
+ # Autocorrection can be tested using `expect_correction` after
42
42
  # `expect_offense`.
43
43
  #
44
44
  # @example `expect_offense` and `expect_correction`
@@ -58,7 +58,7 @@ module RuboCop
58
58
  # that there were no offenses. The `expect_offense` method has
59
59
  # to do more work by parsing out lines that contain carets.
60
60
  #
61
- # If the code produces an offense that could not be auto-corrected, you can
61
+ # If the code produces an offense that could not be autocorrected, you can
62
62
  # use `expect_no_corrections` after `expect_offense`.
63
63
  #
64
64
  # @example `expect_offense` and `expect_no_corrections`
@@ -126,7 +126,7 @@ module RuboCop
126
126
  @offenses
127
127
  end
128
128
 
129
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
129
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
130
130
  def expect_correction(correction, loop: true, source: nil)
131
131
  if source
132
132
  expected_annotations = parse_annotations(source, raise_error: false)
@@ -138,6 +138,8 @@ module RuboCop
138
138
 
139
139
  source = @processed_source.raw_source
140
140
 
141
+ raise 'Use `expect_no_corrections` if the code will not change' if correction == source
142
+
141
143
  iteration = 0
142
144
  new_source = loop do
143
145
  iteration += 1
@@ -157,11 +159,11 @@ module RuboCop
157
159
  _investigate(cop, @processed_source)
158
160
  end
159
161
 
160
- raise 'Use `expect_no_corrections` if the code will not change' if new_source == source
162
+ raise 'Expected correction but no corrections were made' if new_source == source
161
163
 
162
164
  expect(new_source).to eq(correction)
163
165
  end
164
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
166
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
165
167
 
166
168
  def expect_no_corrections
167
169
  raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source
@@ -205,7 +207,7 @@ module RuboCop
205
207
  def set_formatter_options
206
208
  RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
207
209
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
208
- cop.instance_variable_get(:@options)[:auto_correct] = true
210
+ cop.instance_variable_get(:@options)[:autocorrect] = true
209
211
  end
210
212
 
211
213
  # Parsed representation of code annotated with the `^^^ Message` style
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'tmpdir'
4
4
 
5
- RSpec.shared_context 'isolated environment', :isolated_environment do
5
+ RSpec.shared_context 'isolated environment' do # rubocop:disable Metrics/BlockLength
6
6
  around do |example|
7
7
  Dir.mktmpdir do |tmpdir|
8
8
  original_home = Dir.home
@@ -36,9 +36,22 @@ RSpec.shared_context 'isolated environment', :isolated_environment do
36
36
  end
37
37
  end
38
38
  end
39
+
40
+ if RuboCop.const_defined?(:Server)
41
+ around do |example|
42
+ RuboCop::Server::Cache.cache_root_path = nil
43
+ RuboCop::Server::Cache.instance_variable_set(:@project_dir_cache_key, nil)
44
+ begin
45
+ example.run
46
+ ensure
47
+ RuboCop::Server::Cache.cache_root_path = nil
48
+ RuboCop::Server::Cache.instance_variable_set(:@project_dir_cache_key, nil)
49
+ end
50
+ end
51
+ end
39
52
  end
40
53
 
41
- RSpec.shared_context 'maintain registry', :restore_registry do
54
+ RSpec.shared_context 'maintain registry' do
42
55
  around(:each) { |example| RuboCop::Cop::Registry.with_temporary_global { example.run } }
43
56
 
44
57
  def stub_cop_class(name, inherit: RuboCop::Cop::Base, &block)
@@ -49,7 +62,7 @@ RSpec.shared_context 'maintain registry', :restore_registry do
49
62
  end
50
63
 
51
64
  # This context assumes nothing and defines `cop`, among others.
52
- RSpec.shared_context 'config', :config do # rubocop:disable Metrics/BlockLength
65
+ RSpec.shared_context 'config' do # rubocop:disable Metrics/BlockLength
53
66
  ### Meant to be overridden at will
54
67
 
55
68
  let(:cop_class) do
@@ -116,26 +129,46 @@ RSpec.shared_context 'mock console output' do
116
129
  end
117
130
  end
118
131
 
119
- RSpec.shared_context 'ruby 2.5', :ruby25 do
132
+ RSpec.shared_context 'ruby 2.0' do
133
+ let(:ruby_version) { 2.0 }
134
+ end
135
+
136
+ RSpec.shared_context 'ruby 2.1' do
137
+ let(:ruby_version) { 2.1 }
138
+ end
139
+
140
+ RSpec.shared_context 'ruby 2.2' do
141
+ let(:ruby_version) { 2.2 }
142
+ end
143
+
144
+ RSpec.shared_context 'ruby 2.3' do
145
+ let(:ruby_version) { 2.3 }
146
+ end
147
+
148
+ RSpec.shared_context 'ruby 2.4' do
149
+ let(:ruby_version) { 2.4 }
150
+ end
151
+
152
+ RSpec.shared_context 'ruby 2.5' do
120
153
  let(:ruby_version) { 2.5 }
121
154
  end
122
155
 
123
- RSpec.shared_context 'ruby 2.6', :ruby26 do
156
+ RSpec.shared_context 'ruby 2.6' do
124
157
  let(:ruby_version) { 2.6 }
125
158
  end
126
159
 
127
- RSpec.shared_context 'ruby 2.7', :ruby27 do
160
+ RSpec.shared_context 'ruby 2.7' do
128
161
  let(:ruby_version) { 2.7 }
129
162
  end
130
163
 
131
- RSpec.shared_context 'ruby 3.0', :ruby30 do
164
+ RSpec.shared_context 'ruby 3.0' do
132
165
  let(:ruby_version) { 3.0 }
133
166
  end
134
167
 
135
- RSpec.shared_context 'ruby 3.1', :ruby31 do
168
+ RSpec.shared_context 'ruby 3.1' do
136
169
  let(:ruby_version) { 3.1 }
137
170
  end
138
171
 
139
- RSpec.shared_context 'ruby 3.2', :ruby32 do
172
+ RSpec.shared_context 'ruby 3.2' do
140
173
  let(:ruby_version) { 3.2 }
141
174
  end
@@ -3,12 +3,26 @@
3
3
  # Require this file to load code that supports testing using RSpec.
4
4
 
5
5
  require_relative 'cop_helper'
6
- require_relative 'host_environment_simulation_helper'
7
- require_relative 'shared_contexts'
8
6
  require_relative 'expect_offense'
7
+ require_relative 'host_environment_simulation_helper'
9
8
  require_relative 'parallel_formatter'
9
+ require_relative 'shared_contexts'
10
10
 
11
11
  RSpec.configure do |config|
12
12
  config.include CopHelper
13
13
  config.include HostEnvironmentSimulatorHelper
14
+ config.include_context 'config', :config
15
+ config.include_context 'isolated environment', :isolated_environment
16
+ config.include_context 'maintain registry', :restore_registry
17
+ config.include_context 'ruby 2.0', :ruby20
18
+ config.include_context 'ruby 2.1', :ruby21
19
+ config.include_context 'ruby 2.2', :ruby22
20
+ config.include_context 'ruby 2.3', :ruby23
21
+ config.include_context 'ruby 2.4', :ruby24
22
+ config.include_context 'ruby 2.5', :ruby25
23
+ config.include_context 'ruby 2.6', :ruby26
24
+ config.include_context 'ruby 2.7', :ruby27
25
+ config.include_context 'ruby 3.0', :ruby30
26
+ config.include_context 'ruby 3.1', :ruby31
27
+ config.include_context 'ruby 3.2', :ruby32
14
28
  end
@@ -27,6 +27,11 @@ module RuboCop
27
27
  # @api private
28
28
  MAX_ITERATIONS = 200
29
29
 
30
+ # @api private
31
+ REDUNDANT_COP_DISABLE_DIRECTIVE_RULES = %w[
32
+ Lint/RedundantCopDisableDirective RedundantCopDisableDirective Lint
33
+ ].freeze
34
+
30
35
  attr_reader :errors, :warnings
31
36
  attr_writer :aborting
32
37
 
@@ -63,8 +68,16 @@ module RuboCop
63
68
  # Warms up the RuboCop cache by forking a suitable number of RuboCop
64
69
  # instances that each inspects its allotted group of files.
65
70
  def warm_cache(target_files)
71
+ saved_options = @options.dup
72
+ if target_files.length <= 1
73
+ puts 'Skipping parallel inspection: only a single file needs inspection' if @options[:debug]
74
+ return
75
+ end
66
76
  puts 'Running parallel inspection' if @options[:debug]
77
+ %i[autocorrect safe_autocorrect].each { |opt| @options[opt] = false }
67
78
  Parallel.each(target_files) { |target_file| file_offenses(target_file) }
79
+ ensure
80
+ @options = saved_options
68
81
  end
69
82
 
70
83
  def find_target_files(paths)
@@ -143,10 +156,10 @@ module RuboCop
143
156
 
144
157
  if cache&.valid?
145
158
  offenses = cache.load
146
- # If we're running --auto-correct and the cache says there are
159
+ # If we're running --autocorrect and the cache says there are
147
160
  # offenses, we need to actually inspect the file. If the cache shows no
148
161
  # offenses, we're good.
149
- real_run_needed = @options[:auto_correct] && offenses.any?
162
+ real_run_needed = @options[:autocorrect] && offenses.any?
150
163
  else
151
164
  real_run_needed = true
152
165
  end
@@ -186,7 +199,9 @@ module RuboCop
186
199
  end
187
200
 
188
201
  def check_for_redundant_disables?(source)
189
- !source.disabled_line_ranges.empty? && !filtered_run?
202
+ return false if source.disabled_line_ranges.empty? || except_redundant_cop_disable_directive?
203
+
204
+ !@options[:only]
190
205
  end
191
206
 
192
207
  def redundant_cop_disable_directive(file)
@@ -197,8 +212,8 @@ module RuboCop
197
212
  yield cop if cop.relevant_file?(file)
198
213
  end
199
214
 
200
- def filtered_run?
201
- @options[:except] || @options[:only]
215
+ def except_redundant_cop_disable_directive?
216
+ @options[:except] && (@options[:except] & REDUNDANT_COP_DISABLE_DIRECTIVE_RULES).any?
202
217
  end
203
218
 
204
219
  def file_started(file)
@@ -240,7 +255,7 @@ module RuboCop
240
255
  # error message.
241
256
  offenses_by_iteration = []
242
257
 
243
- # When running with --auto-correct, we need to inspect the file (which
258
+ # When running with --autocorrect, we need to inspect the file (which
244
259
  # includes writing a corrected version of it) until no more corrections
245
260
  # are made. This is because automatic corrections can introduce new
246
261
  # offenses. In the normal case the loop is only executed once.
@@ -382,7 +397,7 @@ module RuboCop
382
397
  if @options[:display_only_fail_level_offenses]
383
398
  offenses.select { |o| considered_failure?(o) }
384
399
  elsif @options[:display_only_safe_correctable]
385
- offenses.select { |o| supports_safe_auto_correct?(o) }
400
+ offenses.select { |o| supports_safe_autocorrect?(o) }
386
401
  elsif @options[:display_only_correctable]
387
402
  offenses.select(&:correctable?)
388
403
  else
@@ -390,7 +405,7 @@ module RuboCop
390
405
  end
391
406
  end
392
407
 
393
- def supports_safe_auto_correct?(offense)
408
+ def supports_safe_autocorrect?(offense)
394
409
  cop_class = Cop::Registry.global.find_by_cop_name(offense.cop_name)
395
410
  default_cfg = default_config(offense.cop_name)
396
411
 
@@ -415,17 +430,21 @@ module RuboCop
415
430
  end
416
431
 
417
432
  def get_processed_source(file)
418
- ruby_version = @config_store.for_file(file).target_ruby_version
419
-
420
- if @options[:stdin]
421
- ProcessedSource.new(@options[:stdin], ruby_version, file)
422
- else
423
- begin
424
- ProcessedSource.from_file(file, ruby_version)
425
- rescue Errno::ENOENT
426
- raise RuboCop::Error, "No such file or directory: #{file}"
427
- end
428
- end
433
+ config = @config_store.for_file(file)
434
+ ruby_version = config.target_ruby_version
435
+
436
+ processed_source = if @options[:stdin]
437
+ ProcessedSource.new(@options[:stdin], ruby_version, file)
438
+ else
439
+ begin
440
+ ProcessedSource.from_file(file, ruby_version)
441
+ rescue Errno::ENOENT
442
+ raise RuboCop::Error, "No such file or directory: #{file}"
443
+ end
444
+ end
445
+ processed_source.config = config
446
+ processed_source.registry = mobilized_cop_classes(config)
447
+ processed_source
429
448
  end
430
449
 
431
450
  # A Cop::Team instance is stateful and may change when inspecting.
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require_relative '../cache_config'
5
+ require_relative '../config_finder'
6
+
7
+ #
8
+ # This code is based on https://github.com/fohte/rubocop-daemon.
9
+ #
10
+ # Copyright (c) 2018 Hayato Kawai
11
+ #
12
+ # The MIT License (MIT)
13
+ #
14
+ # https://github.com/fohte/rubocop-daemon/blob/master/LICENSE.txt
15
+ #
16
+ module RuboCop
17
+ module Server
18
+ # Caches the states of server process.
19
+ # @api private
20
+ class Cache
21
+ GEMFILE_NAMES = %w[Gemfile gems.rb].freeze
22
+
23
+ class << self
24
+ attr_accessor :cache_root_path
25
+
26
+ # Searches for Gemfile or gems.rb in the current dir or any parent dirs
27
+ def project_dir
28
+ current_dir = Dir.pwd
29
+ while current_dir != '/'
30
+ return current_dir if GEMFILE_NAMES.any? do |gemfile|
31
+ File.exist?(File.join(current_dir, gemfile))
32
+ end
33
+
34
+ current_dir = File.expand_path('..', current_dir)
35
+ end
36
+ # If we can't find a Gemfile, just use the current directory
37
+ Dir.pwd
38
+ end
39
+
40
+ def project_dir_cache_key
41
+ @project_dir_cache_key ||= project_dir[1..].tr('/', '+')
42
+ end
43
+
44
+ def dir
45
+ Pathname.new(File.join(cache_path, project_dir_cache_key)).tap do |d|
46
+ d.mkpath unless d.exist?
47
+ end
48
+ end
49
+
50
+ def cache_path
51
+ cache_root_dir = if cache_root_path
52
+ File.join(cache_root_path, 'rubocop_cache')
53
+ else
54
+ cache_root_dir_from_config
55
+ end
56
+
57
+ File.expand_path(File.join(cache_root_dir, 'server'))
58
+ end
59
+
60
+ def cache_root_dir_from_config
61
+ CacheConfig.root_dir do
62
+ # `RuboCop::ConfigStore` has heavy dependencies, this is a lightweight implementation
63
+ # so that only the necessary `CacheRootDirectory` can be obtained.
64
+ require 'yaml'
65
+ config_path = ConfigFinder.find_config_path(Dir.pwd)
66
+
67
+ require 'erb'
68
+ file_contents = File.read(config_path)
69
+ yaml_code = ERB.new(file_contents).result
70
+
71
+ config_yaml = YAML.safe_load(
72
+ yaml_code, permitted_classes: [Regexp, Symbol], aliases: true
73
+ )
74
+
75
+ # For compatibility with Ruby 3.0 or lower.
76
+ if Gem::Version.new(Psych::VERSION) < Gem::Version.new('4.0.0')
77
+ config_yaml == false ? nil : config_yaml
78
+ end
79
+
80
+ config_yaml&.dig('AllCops', 'CacheRootDirectory')
81
+ end
82
+ end
83
+
84
+ def port_path
85
+ dir.join('port')
86
+ end
87
+
88
+ def token_path
89
+ dir.join('token')
90
+ end
91
+
92
+ def pid_path
93
+ dir.join('pid')
94
+ end
95
+
96
+ def lock_path
97
+ dir.join('lock')
98
+ end
99
+
100
+ def status_path
101
+ dir.join('status')
102
+ end
103
+
104
+ def version_path
105
+ dir.join('version')
106
+ end
107
+
108
+ def stderr_path
109
+ dir.join('stderr')
110
+ end
111
+
112
+ def pid_running?
113
+ Process.kill(0, pid_path.read.to_i) == 1
114
+ rescue Errno::ESRCH, Errno::ENOENT
115
+ false
116
+ end
117
+
118
+ def acquire_lock
119
+ lock_file = File.open(lock_path, File::CREAT)
120
+ # flock returns 0 if successful, and false if not.
121
+ flock_result = lock_file.flock(File::LOCK_EX | File::LOCK_NB)
122
+ yield flock_result != false
123
+ ensure
124
+ lock_file.flock(File::LOCK_UN)
125
+ lock_file.close
126
+ end
127
+
128
+ def write_port_and_token_files(port:, token:)
129
+ port_path.write(port)
130
+ token_path.write(token)
131
+ end
132
+
133
+ def write_pid_file
134
+ pid_path.write(Process.pid)
135
+ yield
136
+ ensure
137
+ dir.rmtree
138
+ end
139
+
140
+ def write_status_file(status)
141
+ status_path.write(status)
142
+ end
143
+
144
+ def write_version_file(version)
145
+ version_path.write(version)
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rainbow'
4
+ require_relative '../arguments_env'
5
+ require_relative '../arguments_file'
6
+
7
+ #
8
+ # This code is based on https://github.com/fohte/rubocop-daemon.
9
+ #
10
+ # Copyright (c) 2018 Hayato Kawai
11
+ #
12
+ # The MIT License (MIT)
13
+ #
14
+ # https://github.com/fohte/rubocop-daemon/blob/master/LICENSE.txt
15
+ #
16
+ module RuboCop
17
+ module Server
18
+ # The CLI is a class responsible of handling server command line interface logic.
19
+ # @api private
20
+ class CLI
21
+ # Same exit status value as `RuboCop::CLI`.
22
+ STATUS_SUCCESS = 0
23
+ STATUS_ERROR = 2
24
+
25
+ SERVER_OPTIONS = %w[
26
+ --server --no-server --server-status --restart-server --start-server --stop-server
27
+ ].freeze
28
+ EXCLUSIVE_OPTIONS = (SERVER_OPTIONS - %w[--server --no-server]).freeze
29
+
30
+ def initialize
31
+ @exit = false
32
+ end
33
+
34
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
35
+ def run(argv = ARGV)
36
+ unless Server.support_server?
37
+ return error('RuboCop server is not supported by this Ruby.') if use_server_option?(argv)
38
+
39
+ return STATUS_SUCCESS
40
+ end
41
+
42
+ Cache.cache_root_path = fetch_cache_root_path_from(argv)
43
+ deleted_server_arguments = delete_server_argument_from(argv)
44
+
45
+ if deleted_server_arguments.size >= 2
46
+ return error("#{deleted_server_arguments.join(', ')} cannot be specified together.")
47
+ end
48
+
49
+ server_command = deleted_server_arguments.first
50
+
51
+ if EXCLUSIVE_OPTIONS.include?(server_command) && argv.count > allowed_option_count
52
+ return error("#{server_command} cannot be combined with other options.")
53
+ end
54
+
55
+ if server_command.nil?
56
+ server_command = ArgumentsEnv.read_as_arguments.delete('--server') ||
57
+ ArgumentsFile.read_as_arguments.delete('--server')
58
+ end
59
+
60
+ run_command(server_command)
61
+
62
+ STATUS_SUCCESS
63
+ end
64
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
65
+
66
+ def exit?
67
+ @exit
68
+ end
69
+
70
+ private
71
+
72
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength:
73
+ def run_command(server_command)
74
+ case server_command
75
+ when '--server'
76
+ Server::ClientCommand::Start.new.run unless Server.running?
77
+ when '--no-server'
78
+ Server::ClientCommand::Stop.new.run if Server.running?
79
+ when '--restart-server'
80
+ @exit = true
81
+ Server::ClientCommand::Restart.new.run
82
+ when '--start-server'
83
+ @exit = true
84
+ Server::ClientCommand::Start.new.run
85
+ when '--stop-server'
86
+ @exit = true
87
+ Server::ClientCommand::Stop.new.run
88
+ when '--server-status'
89
+ @exit = true
90
+ Server::ClientCommand::Status.new.run
91
+ end
92
+ end
93
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength:
94
+
95
+ def fetch_cache_root_path_from(arguments)
96
+ cache_root = arguments.detect { |argument| argument.start_with?('--cache-root') }
97
+ return unless cache_root
98
+
99
+ if cache_root.start_with?('--cache-root=')
100
+ cache_root.split('=')[1]
101
+ else
102
+ arguments[arguments.index(cache_root) + 1]
103
+ end
104
+ end
105
+
106
+ def delete_server_argument_from(all_arguments)
107
+ SERVER_OPTIONS.each_with_object([]) do |server_option, server_arguments|
108
+ server_arguments << all_arguments.delete(server_option)
109
+ end.compact
110
+ end
111
+
112
+ def use_server_option?(argv)
113
+ (argv & SERVER_OPTIONS).any?
114
+ end
115
+
116
+ def allowed_option_count
117
+ Cache.cache_root_path ? 2 : 1
118
+ end
119
+
120
+ def error(message)
121
+ @exit = true
122
+ warn Rainbow(message).red
123
+
124
+ STATUS_ERROR
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shellwords'
4
+ require 'socket'
5
+
6
+ #
7
+ # This code is based on https://github.com/fohte/rubocop-daemon.
8
+ #
9
+ # Copyright (c) 2018 Hayato Kawai
10
+ #
11
+ # The MIT License (MIT)
12
+ #
13
+ # https://github.com/fohte/rubocop-daemon/blob/master/LICENSE.txt
14
+ #
15
+ module RuboCop
16
+ module Server
17
+ module ClientCommand
18
+ # Abstract base class for server client command.
19
+ # @api private
20
+ class Base
21
+ def run
22
+ raise NotImplementedError
23
+ end
24
+
25
+ private
26
+
27
+ def send_request(command:, args: [], body: '')
28
+ TCPSocket.open('127.0.0.1', Cache.port_path.read) do |socket|
29
+ socket.puts [Cache.token_path.read, Dir.pwd, command, *args].shelljoin
30
+ socket.write body
31
+ socket.close_write
32
+ $stdout.write socket.readpartial(4096) until socket.eof?
33
+ end
34
+ end
35
+
36
+ def check_running_server
37
+ Server.running?.tap do |running|
38
+ warn 'RuboCop server is not running.' unless running
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # This code is based on https://github.com/fohte/rubocop-daemon.
5
+ #
6
+ # Copyright (c) 2018 Hayato Kawai
7
+ #
8
+ # The MIT License (MIT)
9
+ #
10
+ # https://github.com/fohte/rubocop-daemon/blob/master/LICENSE.txt
11
+ #
12
+ module RuboCop
13
+ module Server
14
+ module ClientCommand
15
+ # This class is a client command to execute server process.
16
+ # @api private
17
+ class Exec < Base
18
+ def run
19
+ ensure_server!
20
+ Cache.status_path.delete if Cache.status_path.file?
21
+ send_request(
22
+ command: 'exec',
23
+ args: ARGV.dup,
24
+ body: $stdin.tty? ? '' : $stdin.read
25
+ )
26
+ warn stderr unless stderr.empty?
27
+ status
28
+ end
29
+
30
+ private
31
+
32
+ def ensure_server!
33
+ if incompatible_version?
34
+ puts 'RuboCop version incompatibility found, RuboCop server restarting...'
35
+ ClientCommand::Stop.new.run
36
+ elsif check_running_server
37
+ return
38
+ end
39
+
40
+ ClientCommand::Start.new.run
41
+ end
42
+
43
+ def incompatible_version?
44
+ RuboCop::Version::STRING != Cache.version_path.read
45
+ end
46
+
47
+ def stderr
48
+ Cache.stderr_path.read
49
+ end
50
+
51
+ def status
52
+ unless Cache.status_path.file?
53
+ raise "RuboCop server: Could not find status file at: #{Cache.status_path}"
54
+ end
55
+
56
+ status = Cache.status_path.read
57
+ raise "RuboCop server: '#{status}' is not a valid status!" if (status =~ /^\d+$/).nil?
58
+
59
+ status.to_i
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end