rubocop 1.84.2 → 1.86.2

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 (210) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +99 -16
  3. data/config/obsoletion.yml +5 -0
  4. data/lib/rubocop/cache_config.rb +1 -1
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +28 -2
  6. data/lib/rubocop/cli/command/list_enabled_cops_for.rb +40 -0
  7. data/lib/rubocop/cli/command/mcp.rb +19 -0
  8. data/lib/rubocop/cli/command/show_cops.rb +2 -2
  9. data/lib/rubocop/cli/command/show_docs_url.rb +4 -8
  10. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  11. data/lib/rubocop/cli.rb +7 -7
  12. data/lib/rubocop/comment_config.rb +12 -15
  13. data/lib/rubocop/config.rb +14 -10
  14. data/lib/rubocop/config_finder.rb +1 -1
  15. data/lib/rubocop/config_loader_resolver.rb +2 -1
  16. data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
  17. data/lib/rubocop/config_store.rb +1 -1
  18. data/lib/rubocop/config_validator.rb +1 -1
  19. data/lib/rubocop/cop/autocorrect_logic.rb +2 -1
  20. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
  21. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -5
  22. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  23. data/lib/rubocop/cop/correctors.rb +28 -0
  24. data/lib/rubocop/cop/documentation.rb +2 -3
  25. data/lib/rubocop/cop/exclude_limit.rb +31 -5
  26. data/lib/rubocop/cop/gemspec/require_mfa.rb +4 -4
  27. data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
  28. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  30. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
  31. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  32. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -2
  34. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  35. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
  36. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
  37. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
  38. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
  39. data/lib/rubocop/cop/layout/end_alignment.rb +6 -3
  40. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  41. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  42. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  43. data/lib/rubocop/cop/layout/line_length.rb +5 -3
  44. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
  45. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  46. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +53 -3
  47. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  48. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
  51. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_reassignment.rb +59 -9
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  54. data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
  55. data/lib/rubocop/cop/lint/duplicate_methods.rb +55 -8
  56. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  57. data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
  58. data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
  59. data/lib/rubocop/cop/lint/empty_when.rb +8 -1
  60. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  61. data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
  62. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -1
  63. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  64. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -13
  65. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +0 -9
  66. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
  67. data/lib/rubocop/cop/lint/require_relative_self_path.rb +2 -0
  68. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
  69. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
  70. data/lib/rubocop/cop/lint/syntax.rb +25 -1
  71. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
  72. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  73. data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
  74. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  75. data/lib/rubocop/cop/lint/useless_assignment.rb +4 -9
  76. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
  77. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
  78. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +35 -9
  79. data/lib/rubocop/cop/lint/void.rb +32 -12
  80. data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
  81. data/lib/rubocop/cop/migration/department_name.rb +12 -1
  82. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  83. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
  84. data/lib/rubocop/cop/mixin/configurable_max.rb +6 -5
  85. data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
  86. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
  87. data/lib/rubocop/cop/mixin.rb +85 -0
  88. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  89. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  90. data/lib/rubocop/cop/offense.rb +8 -0
  91. data/lib/rubocop/cop/registry.rb +39 -37
  92. data/lib/rubocop/cop/security/eval.rb +15 -2
  93. data/lib/rubocop/cop/style/access_modifier_declarations.rb +14 -2
  94. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
  95. data/lib/rubocop/cop/style/alias.rb +4 -1
  96. data/lib/rubocop/cop/style/and_or.rb +1 -0
  97. data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
  98. data/lib/rubocop/cop/style/array_join.rb +4 -2
  99. data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
  100. data/lib/rubocop/cop/style/attr.rb +5 -2
  101. data/lib/rubocop/cop/style/bare_percent_literals.rb +3 -1
  102. data/lib/rubocop/cop/style/begin_block.rb +3 -1
  103. data/lib/rubocop/cop/style/block_delimiters.rb +25 -33
  104. data/lib/rubocop/cop/style/case_equality.rb +4 -0
  105. data/lib/rubocop/cop/style/class_and_module_children.rb +10 -2
  106. data/lib/rubocop/cop/style/collection_compact.rb +36 -16
  107. data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
  108. data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
  109. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -4
  110. data/lib/rubocop/cop/style/copyright.rb +22 -11
  111. data/lib/rubocop/cop/style/date_time.rb +2 -2
  112. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +6 -1
  113. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  114. data/lib/rubocop/cop/style/each_with_object.rb +2 -0
  115. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  116. data/lib/rubocop/cop/style/empty_class_definition.rb +43 -20
  117. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  118. data/lib/rubocop/cop/style/encoding.rb +7 -1
  119. data/lib/rubocop/cop/style/end_block.rb +3 -1
  120. data/lib/rubocop/cop/style/endless_method.rb +8 -3
  121. data/lib/rubocop/cop/style/file_open.rb +84 -0
  122. data/lib/rubocop/cop/style/for.rb +3 -0
  123. data/lib/rubocop/cop/style/format_string_token.rb +29 -2
  124. data/lib/rubocop/cop/style/global_vars.rb +5 -2
  125. data/lib/rubocop/cop/style/guard_clause.rb +9 -6
  126. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +21 -5
  127. data/lib/rubocop/cop/style/hash_lookup_method.rb +19 -7
  128. data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
  129. data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
  130. data/lib/rubocop/cop/style/if_inside_else.rb +16 -7
  131. data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -3
  132. data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
  133. data/lib/rubocop/cop/style/inline_comment.rb +4 -1
  134. data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
  135. data/lib/rubocop/cop/style/magic_comment_format.rb +2 -2
  136. data/lib/rubocop/cop/style/map_join.rb +123 -0
  137. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -3
  138. data/lib/rubocop/cop/style/module_member_existence_check.rb +7 -14
  139. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -1
  140. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  141. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  142. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  143. data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
  144. data/lib/rubocop/cop/style/not.rb +2 -0
  145. data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
  146. data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
  147. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  148. data/lib/rubocop/cop/style/parallel_assignment.rb +4 -0
  149. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
  150. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
  151. data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
  152. data/lib/rubocop/cop/style/proc.rb +3 -2
  153. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  154. data/lib/rubocop/cop/style/reduce_to_hash.rb +200 -0
  155. data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
  156. data/lib/rubocop/cop/style/redundant_each.rb +3 -3
  157. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  158. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
  159. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
  160. data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
  161. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -22
  162. data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -1
  163. data/lib/rubocop/cop/style/redundant_return.rb +3 -1
  164. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  165. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
  166. data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
  167. data/lib/rubocop/cop/style/regexp_literal.rb +29 -0
  168. data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
  169. data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
  170. data/lib/rubocop/cop/style/select_by_range.rb +197 -0
  171. data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
  172. data/lib/rubocop/cop/style/semicolon.rb +2 -0
  173. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  174. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
  175. data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
  176. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -2
  177. data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
  178. data/lib/rubocop/cop/style/symbol_proc.rb +7 -6
  179. data/lib/rubocop/cop/style/tally_method.rb +181 -0
  180. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  181. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  182. data/lib/rubocop/cop/style/while_until_modifier.rb +16 -0
  183. data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
  184. data/lib/rubocop/cop/team.rb +86 -35
  185. data/lib/rubocop/cop/variable_force/branch.rb +2 -2
  186. data/lib/rubocop/directive_comment.rb +2 -1
  187. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -2
  188. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  189. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  190. data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
  191. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  192. data/lib/rubocop/formatter.rb +22 -21
  193. data/lib/rubocop/lsp/diagnostic.rb +1 -0
  194. data/lib/rubocop/lsp/routes.rb +10 -3
  195. data/lib/rubocop/lsp/runtime.rb +1 -2
  196. data/lib/rubocop/mcp/server.rb +200 -0
  197. data/lib/rubocop/options.rb +17 -4
  198. data/lib/rubocop/path_util.rb +14 -2
  199. data/lib/rubocop/plugin/loader.rb +1 -1
  200. data/lib/rubocop/result_cache.rb +22 -10
  201. data/lib/rubocop/rspec/cop_helper.rb +8 -0
  202. data/lib/rubocop/rspec/shared_contexts.rb +32 -2
  203. data/lib/rubocop/runner.rb +78 -51
  204. data/lib/rubocop/server/cache.rb +5 -7
  205. data/lib/rubocop/server/core.rb +2 -0
  206. data/lib/rubocop/target_finder.rb +14 -7
  207. data/lib/rubocop/target_ruby.rb +18 -12
  208. data/lib/rubocop/version.rb +2 -2
  209. data/lib/rubocop.rb +21 -96
  210. metadata +25 -5
@@ -16,7 +16,9 @@ module RuboCop
16
16
  'root of the project. RuboCop will use this path to determine which ' \
17
17
  'cops are enabled (via eg. Include/Exclude), and so that certain cops ' \
18
18
  'like Naming/FileName can be checked.'
19
- EXITING_OPTIONS = %i[version verbose_version show_cops show_docs_url lsp].freeze
19
+ EXITING_OPTIONS = %i[
20
+ version verbose_version show_cops list_enabled_cops_for show_docs_url lsp mcp
21
+ ].freeze
20
22
  DEFAULT_MAXIMUM_EXCLUSION_ITEMS = 15
21
23
 
22
24
  def initialize
@@ -57,6 +59,7 @@ module RuboCop
57
59
  add_check_options(opts)
58
60
  add_cache_options(opts)
59
61
  add_lsp_option(opts)
62
+ add_mcp_option(opts)
60
63
  add_server_options(opts)
61
64
  add_output_options(opts)
62
65
  add_autocorrection_options(opts)
@@ -215,6 +218,12 @@ module RuboCop
215
218
  end
216
219
  end
217
220
 
221
+ def add_mcp_option(opts)
222
+ section(opts, 'MCP Option') do
223
+ option(opts, '--mcp')
224
+ end
225
+ end
226
+
218
227
  def add_server_options(opts)
219
228
  section(opts, 'Server Options') do
220
229
  option(opts, '--[no-]server')
@@ -229,6 +238,7 @@ module RuboCop
229
238
  def add_additional_modes(opts)
230
239
  section(opts, 'Additional Modes') do
231
240
  option(opts, '-L', '--list-target-files')
241
+ option(opts, '--list-enabled-cops-for PATH')
232
242
  option(opts, '--show-cops [COP1,COP2,...]') do |list|
233
243
  @options[:show_cops] = list.nil? ? [] : list.split(',')
234
244
  end
@@ -467,8 +477,7 @@ module RuboCop
467
477
  end
468
478
 
469
479
  def invalid_arguments_for_parallel
470
- [('--auto-gen-config' if @options.key?(:auto_gen_config)),
471
- ('-F/--fail-fast' if @options.key?(:fail_fast)),
480
+ [('-F/--fail-fast' if @options.key?(:fail_fast)),
472
481
  ('--profile' if @options[:profile]),
473
482
  ('--memory' if @options[:memory]),
474
483
  ('--cache false' if @options > { cache: 'false' })].compact
@@ -592,7 +601,7 @@ module RuboCop
592
601
  display_only_correctable: ['Only output correctable offense messages.'],
593
602
  display_only_safe_correctable: ['Only output safe-correctable offense messages',
594
603
  'when combined with --display-only-correctable.'],
595
- show_cops: ['Shows the given cops, or all cops by',
604
+ show_cops: ['Show the given cops, or all cops by',
596
605
  'default, and their configurations for the',
597
606
  'current directory.',
598
607
  'You can use `*` as a wildcard.'],
@@ -621,6 +630,8 @@ module RuboCop
621
630
  'autocorrected source. This is especially useful',
622
631
  'when combined with --autocorrect and --stdin.'],
623
632
  list_target_files: 'List all files RuboCop will inspect.',
633
+ list_enabled_cops_for: ['List which cops will inspect a given file or',
634
+ 'directory.'],
624
635
  autocorrect: 'Autocorrect offenses (only when it\'s safe).',
625
636
  auto_correct: '(same, deprecated)',
626
637
  safe_auto_correct: '(same, deprecated)',
@@ -651,6 +662,8 @@ module RuboCop
651
662
  server_status: 'Show server status.',
652
663
  no_detach: 'Run the server process in the foreground.',
653
664
  lsp: 'Start a language server listening on STDIN.',
665
+ mcp: ['Start an MCP (Model Context Protocol) server that',
666
+ 'communicates over stdio.'],
654
667
  raise_cop_error: ['Raise cop-related errors with cause and location.',
655
668
  'This is used to prevent cops from failing silently.',
656
669
  'Default is false.'],
@@ -10,7 +10,19 @@ module RuboCop
10
10
 
11
11
  module_function
12
12
 
13
- def relative_path(path, base_dir = Dir.pwd)
13
+ # Returns the current working directory, cached for the duration of a run.
14
+ # Dir.pwd is a syscall; caching it avoids repeated overhead since RuboCop
15
+ # never changes the working directory during a run.
16
+ def pwd
17
+ @pwd ||= Dir.pwd
18
+ end
19
+
20
+ # Reset the cached pwd. Only needed in tests that use Dir.chdir.
21
+ def reset_pwd
22
+ @pwd = nil
23
+ end
24
+
25
+ def relative_path(path, base_dir = PathUtil.pwd)
14
26
  PathUtil.relative_paths_cache[base_dir][path] ||=
15
27
  # Optimization for the common case where path begins with the base
16
28
  # dir. Just cut off the first part.
@@ -41,7 +53,7 @@ module RuboCop
41
53
  path.uri.to_s
42
54
  else
43
55
  # Ideally, we calculate this relative to the project root.
44
- base_dir = Dir.pwd
56
+ base_dir = PathUtil.pwd
45
57
 
46
58
  if path.start_with? base_dir
47
59
  relative_path(path, base_dir)
@@ -84,7 +84,7 @@ module RuboCop
84
84
  end
85
85
 
86
86
  def require_plugin(require_path)
87
- FeatureLoader.load(config_directory_path: Dir.pwd, feature: require_path)
87
+ FeatureLoader.load(config_directory_path: PathUtil.pwd, feature: require_path)
88
88
  end
89
89
 
90
90
  def constantize_plugin_from_gemspec_metadata(plugin_name)
@@ -27,6 +27,7 @@ module RuboCop
27
27
  # there's parallel execution and the cache is shared.
28
28
  def self.cleanup(config_store, verbose, cache_root_override = nil)
29
29
  return if inhibit_cleanup # OPTIMIZE: For faster testing
30
+ return unless config_store.for_pwd.for_all_cops['MaxFilesInCache']
30
31
 
31
32
  rubocop_cache_dir = cache_root(config_store, cache_root_override)
32
33
  return unless File.exist?(rubocop_cache_dir)
@@ -85,15 +86,23 @@ module RuboCop
85
86
  end
86
87
 
87
88
  def self.cache_root(config_store, cache_root_override = nil)
88
- CacheConfig.root_dir do
89
+ return @cache_root if @cache_root && !cache_root_override
90
+
91
+ result = CacheConfig.root_dir do
89
92
  cache_root_override || config_store.for_pwd.for_all_cops['CacheRootDirectory']
90
93
  end
94
+ @cache_root = result unless cache_root_override
95
+ result
91
96
  end
92
97
 
93
98
  def self.allow_symlinks_in_cache_location?(config_store)
94
99
  config_store.for_pwd.for_all_cops['AllowSymlinksInCacheRootDirectory']
95
100
  end
96
101
 
102
+ def self.reset_config_cache
103
+ @cache_root = nil
104
+ end
105
+
97
106
  attr_reader :path
98
107
 
99
108
  def initialize(file, team, options, config_store, cache_root_override = nil)
@@ -196,6 +205,17 @@ module RuboCop
196
205
  end
197
206
  end
198
207
 
208
+ # Return a hash of the options given at invocation, minus the ones that have
209
+ # no effect on which offenses and disabled line ranges are found, and thus
210
+ # don't affect caching.
211
+ def relevant_options_digest(options)
212
+ @relevant_options_digest ||= {}
213
+ @relevant_options_digest[options] ||= begin
214
+ options = options.reject { |key, _| NON_CHANGING.include?(key) }
215
+ options.to_s.gsub(/[^a-z]+/i, '_')
216
+ end
217
+ end
218
+
199
219
  private
200
220
 
201
221
  def digest(path)
@@ -227,20 +247,12 @@ module RuboCop
227
247
  end
228
248
  end
229
249
 
230
- # Return a hash of the options given at invocation, minus the ones that have
231
- # no effect on which offenses and disabled line ranges are found, and thus
232
- # don't affect caching.
233
- def relevant_options_digest(options)
234
- options = options.reject { |key, _| NON_CHANGING.include?(key) }
235
- options.to_s.gsub(/[^a-z]+/i, '_')
236
- end
237
-
238
250
  # We combine team and options into a single "context" checksum to avoid
239
251
  # making file names that are too long for some filesystems to handle.
240
252
  # This context is for anything that's not (1) the RuboCop executable
241
253
  # checksum or (2) the inspected file checksum.
242
254
  def context_checksum(team, options)
243
- keys = [team.external_dependency_checksum, relevant_options_digest(options)]
255
+ keys = [team.external_dependency_checksum, self.class.relevant_options_digest(options)]
244
256
  Digest::SHA1.hexdigest(keys.join)
245
257
  end
246
258
  end
@@ -6,6 +6,12 @@ require 'tempfile'
6
6
  module CopHelper
7
7
  extend RSpec::SharedContext
8
8
 
9
+ @integrated_plugins = false
10
+
11
+ class << self
12
+ attr_accessor :integrated_plugins
13
+ end
14
+
9
15
  let(:ruby_version) do
10
16
  # The minimum version Prism can parse is 3.3.
11
17
  ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
@@ -18,11 +24,13 @@ module CopHelper
18
24
 
19
25
  before(:all) do
20
26
  next if ENV['RUBOCOP_CORE_DEVELOPMENT']
27
+ next if CopHelper.integrated_plugins
21
28
 
22
29
  plugins = Gem.loaded_specs.filter_map do |feature_name, feature_specification|
23
30
  feature_name if feature_specification.metadata['default_lint_roller_plugin']
24
31
  end
25
32
  RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, plugins)
33
+ CopHelper.integrated_plugins = true
26
34
  end
27
35
 
28
36
  def inspect_source(source, file = nil)
@@ -2,8 +2,12 @@
2
2
 
3
3
  require 'tmpdir'
4
4
 
5
+ # Reset cached PathUtil.pwd before each example so that tests using Dir.chdir
6
+ # or stubbing Dir.pwd get a fresh value.
7
+ RSpec.configure { |c| c.before { RuboCop::PathUtil.reset_pwd } }
8
+
5
9
  RSpec.shared_context 'isolated environment' do # rubocop:disable Metrics/BlockLength
6
- around do |example|
10
+ around do |example| # rubocop:disable Metrics/BlockLength
7
11
  Dir.mktmpdir do |tmpdir|
8
12
  original_home = Dir.home
9
13
  original_xdg_config_home = ENV.fetch('XDG_CONFIG_HOME', nil)
@@ -26,11 +30,16 @@ RSpec.shared_context 'isolated environment' do # rubocop:disable Metrics/BlockLe
26
30
  begin
27
31
  FileUtils.mkdir_p(working_dir)
28
32
 
29
- Dir.chdir(working_dir) { example.run }
33
+ Dir.chdir(working_dir) do
34
+ RuboCop::PathUtil.reset_pwd
35
+ RuboCop::ResultCache.reset_config_cache
36
+ example.run
37
+ end
30
38
  ensure
31
39
  ENV['HOME'] = original_home
32
40
  ENV['XDG_CONFIG_HOME'] = original_xdg_config_home
33
41
 
42
+ RuboCop::ResultCache.reset_config_cache
34
43
  RuboCop::ConfigLoader.clear_options # This also resets RuboCop::FileFinder.root_level
35
44
  end
36
45
  end
@@ -203,6 +212,27 @@ RSpec.shared_context 'lsp' do
203
212
  end
204
213
  end
205
214
 
215
+ RSpec.shared_context 'with exclude limit tracking' do
216
+ around do |example|
217
+ Dir.mktmpdir('rubocop-exclude-limit') do |dir|
218
+ RuboCop::ExcludeLimit.tmp_dir = Pathname.new(dir)
219
+ example.run
220
+ ensure
221
+ RuboCop::ExcludeLimit.tmp_dir = nil
222
+ end
223
+ end
224
+
225
+ # Reads exclude_limit values from the tmp files written by ExcludeLimit.
226
+ # Returns a hash like { 'Max' => 81 } or nil if no values were written.
227
+ def read_exclude_limit(cop, parameter_name = nil)
228
+ if parameter_name
229
+ read_exclude_limit(cop)[parameter_name]
230
+ else
231
+ RuboCop::ExcludeLimit.read_limits(cop.class.badge.to_s)
232
+ end
233
+ end
234
+ end
235
+
206
236
  RSpec.shared_context 'ruby 2.0' do
207
237
  # Prism supports parsing Ruby 3.3+.
208
238
  let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.0 }
@@ -62,6 +62,8 @@ module RuboCop
62
62
  @errors = []
63
63
  @warnings = []
64
64
  @aborting = false
65
+ @inspected_files = []
66
+ @report_queue = {}
65
67
  end
66
68
 
67
69
  def run(paths)
@@ -73,7 +75,6 @@ module RuboCop
73
75
  if @options[:list_target_files]
74
76
  list_files(target_files)
75
77
  else
76
- warm_cache(target_files) if @options[:parallel]
77
78
  inspect_files(target_files)
78
79
  end
79
80
  rescue Interrupt
@@ -90,21 +91,6 @@ module RuboCop
90
91
 
91
92
  private
92
93
 
93
- # Warms up the RuboCop cache by forking a suitable number of RuboCop
94
- # instances that each inspects its allotted group of files.
95
- def warm_cache(target_files)
96
- saved_options = @options.dup
97
- if target_files.length <= 1
98
- puts 'Skipping parallel inspection: only a single file needs inspection' if @options[:debug]
99
- return
100
- end
101
- puts 'Running parallel inspection' if @options[:debug]
102
- %i[autocorrect safe_autocorrect].each { |opt| @options[opt] = false }
103
- Parallel.each(target_files) { |target_file| file_offenses(target_file) }
104
- ensure
105
- @options = saved_options
106
- end
107
-
108
94
  def find_target_files(paths)
109
95
  target_finder = TargetFinder.new(@config_store, @options)
110
96
  mode = if @options[:only_recognized_file_types]
@@ -116,12 +102,15 @@ module RuboCop
116
102
  target_files.each(&:freeze).freeze
117
103
  end
118
104
 
119
- def inspect_files(files)
120
- inspected_files = []
121
-
105
+ def inspect_files(files) # rubocop:disable Metrics/AbcSize
122
106
  formatter_set.started(files)
107
+ file_iterator(files) do |file|
108
+ offenses = process_file(file)
109
+ succeeded = offenses.none? { |o| considered_failure?(o) && offense_displayed?(o) }
110
+ raise Parallel::Break if @options[:fail_fast] && !succeeded
123
111
 
124
- each_inspected_file(files) { |file| inspected_files << file }
112
+ [offenses, succeeded]
113
+ end
125
114
  ensure
126
115
  # OPTIMIZE: Calling `ResultCache.cleanup` takes time. This optimization
127
116
  # mainly targets editors that integrates RuboCop. When RuboCop is run
@@ -129,23 +118,74 @@ module RuboCop
129
118
  if files.size > 1 && cached_run?
130
119
  ResultCache.cleanup(@config_store, @options[:debug], @options[:cache_root])
131
120
  end
132
-
133
- formatter_set.finished(inspected_files.freeze)
121
+ formatter_set.finished(@inspected_files.freeze)
134
122
  formatter_set.close_output_files
135
123
  end
136
124
 
137
- def each_inspected_file(files)
138
- files.reduce(true) do |all_passed, file|
139
- offenses = process_file(file)
140
- yield file
125
+ def file_iterator(files, &block)
126
+ all_passed = true
141
127
 
142
- if offenses.any? { |o| considered_failure?(o) && offense_displayed?(o) }
143
- break false if @options[:fail_fast]
128
+ on_start = ->(file, _index) { file_started(file) }
129
+ on_finish = lambda do |file, index, (offenses, passed)|
130
+ all_passed &&= passed
131
+ finished_report(file, index, offenses)
132
+ end
144
133
 
145
- next false
146
- end
134
+ if run_in_parallel?(files)
135
+ parallel_file_iterator(files, on_start, on_finish, &block)
136
+ else
137
+ serial_file_iterator(files, on_start, on_finish, &block)
138
+ end
139
+
140
+ process_remaining_report_queue
141
+
142
+ all_passed
143
+ end
144
+
145
+ def finished_report(file, index, offenses)
146
+ @report_queue[index] = [file, offenses]
147
+ @next_index_to_report ||= 0
148
+ while @report_queue.key?(@next_index_to_report)
149
+ process_report_queue_entry(@next_index_to_report)
150
+ @next_index_to_report += 1
151
+ end
152
+ end
147
153
 
148
- all_passed
154
+ def process_report_queue_entry(index)
155
+ file, offenses = @report_queue.delete(index)
156
+ file_finished(file, offenses)
157
+ end
158
+
159
+ def process_remaining_report_queue
160
+ @report_queue.keys.sort.each do |index|
161
+ process_report_queue_entry(index)
162
+ end
163
+ end
164
+
165
+ def run_in_parallel?(files)
166
+ return false if @options[:auto_gen_config]
167
+ return false unless @options[:parallel]
168
+
169
+ if files.size <= 1
170
+ puts 'Skipping parallel inspection: only a single file needs inspection' if @options[:debug]
171
+ return false
172
+ end
173
+
174
+ puts 'Running parallel inspection' if @options[:debug]
175
+ true
176
+ end
177
+
178
+ def parallel_file_iterator(files, on_start, on_finish, &block)
179
+ Parallel.each(files, start: on_start, finish: on_finish, &block)
180
+ end
181
+
182
+ def serial_file_iterator(files, on_start, on_finish, &block)
183
+ files.each_with_index do |file, index|
184
+ on_start.call(file, index)
185
+ result = yield file
186
+ on_finish.call(file, index, result)
187
+ rescue Parallel::Break
188
+ break
149
189
  end
150
190
  end
151
191
 
@@ -154,16 +194,13 @@ module RuboCop
154
194
  end
155
195
 
156
196
  def process_file(file)
157
- file_started(file)
158
- offenses = file_offenses(file)
197
+ file_offenses(file)
159
198
  rescue InfiniteCorrectionLoop => e
160
199
  raise e if @options[:raise_cop_error]
161
200
 
162
201
  errors << e
163
202
  warn Rainbow(e.message).red
164
- offenses = e.offenses.compact.sort.freeze
165
- ensure
166
- file_finished(file, offenses || [])
203
+ e.offenses.compact.sort.freeze
167
204
  end
168
205
 
169
206
  def file_offenses(file)
@@ -194,7 +231,7 @@ module RuboCop
194
231
 
195
232
  if real_run_needed
196
233
  offenses = yield
197
- save_in_cache(cache, offenses)
234
+ save_in_cache(cache, offenses) unless Cop::Registry.global.warnings?(file)
198
235
  end
199
236
 
200
237
  offenses
@@ -250,6 +287,7 @@ module RuboCop
250
287
  end
251
288
 
252
289
  def file_finished(file, offenses)
290
+ @inspected_files << file
253
291
  offenses = offenses_to_report(offenses)
254
292
  formatter_set.file_finished(file, offenses)
255
293
  end
@@ -258,10 +296,6 @@ module RuboCop
258
296
  @cached_run ||=
259
297
  (@options[:cache] == 'true' ||
260
298
  (@options[:cache] != 'false' && @config_store.for_pwd.for_all_cops['UseCache'])) &&
261
- # When running --auto-gen-config, there's some processing done in the
262
- # cops related to calculating the Max parameters for Metrics cops. We
263
- # need to do that processing and cannot use caching.
264
- !@options[:auto_gen_config] &&
265
299
  # We can't cache results from code which is piped in to stdin
266
300
  !@options[:stdin]
267
301
  end
@@ -350,16 +384,9 @@ module RuboCop
350
384
 
351
385
  def inspect_file(processed_source, team = mobilize_team(processed_source))
352
386
  extracted_ruby_sources = extract_ruby_sources(processed_source)
353
- offenses = extracted_ruby_sources.flat_map do |extracted_ruby_source|
354
- report = team.investigate(
355
- extracted_ruby_source[:processed_source],
356
- offset: extracted_ruby_source[:offset],
357
- original: processed_source
358
- )
359
- @errors.concat(team.errors)
360
- @warnings.concat(team.warnings)
361
- report.offenses
362
- end
387
+ offenses = team.investigate_fragments(extracted_ruby_sources, original: processed_source)
388
+ @errors.concat(team.errors)
389
+ @warnings.concat(team.warnings)
363
390
  [offenses, team.updated_source_file?]
364
391
  end
365
392
 
@@ -114,13 +114,11 @@ module RuboCop
114
114
  end
115
115
 
116
116
  def acquire_lock
117
- lock_file = File.open(lock_path, File::CREAT)
118
- # flock returns 0 if successful, and false if not.
119
- flock_result = lock_file.flock(File::LOCK_EX | File::LOCK_NB)
120
- yield flock_result != false
121
- ensure
122
- lock_file.flock(File::LOCK_UN)
123
- lock_file.close
117
+ File.open(lock_path, File::CREAT) do |lock_file|
118
+ # flock returns 0 if successful, and false if not.
119
+ flock_result = lock_file.flock(File::LOCK_EX | File::LOCK_NB)
120
+ yield flock_result != false
121
+ end
124
122
  end
125
123
 
126
124
  def write_port_and_token_files(port:, token:)
@@ -45,6 +45,8 @@ module RuboCop
45
45
  write_port_and_token_files
46
46
 
47
47
  pid = fork do
48
+ # NOTE: As of Ruby 4.0.0, ZJIT is still under development, while YJIT is production-ready,
49
+ # so support for ZJIT is deferred.
48
50
  if defined?(RubyVM::YJIT.enable)
49
51
  RubyVM::YJIT.enable
50
52
  end
@@ -38,16 +38,16 @@ module RuboCop
38
38
  # @param base_dir Root directory under which to search for
39
39
  # ruby source files
40
40
  # @return [Array] Array of filenames
41
- def target_files_in_dir(base_dir = Dir.pwd)
41
+ def target_files_in_dir(base_dir = PathUtil.pwd)
42
42
  # Support Windows: Backslashes from command-line -> forward slashes
43
43
  base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
44
44
  all_files = find_files(base_dir, File::FNM_DOTMATCH)
45
45
  base_dir_config = @config_store.for(base_dir)
46
46
 
47
- target_files = if hidden_path?(base_dir)
47
+ target_files = if hidden_dir?(base_dir)
48
48
  all_files.select { |file| ruby_file?(file) }
49
49
  else
50
- all_files.select { |file| to_inspect?(file, base_dir_config) }
50
+ all_files.select { |file| to_inspect?(file, base_dir, base_dir_config) }
51
51
  end
52
52
 
53
53
  target_files.sort_by!(&order)
@@ -72,15 +72,22 @@ module RuboCop
72
72
 
73
73
  private
74
74
 
75
- def to_inspect?(file, base_dir_config)
75
+ def to_inspect?(file, base_dir, base_dir_config)
76
76
  return false if base_dir_config.file_to_exclude?(file)
77
- return true if !hidden_path?(file) && ruby_file?(file)
77
+ return true if !hidden_file_in_dir?(file, base_dir) && ruby_file?(file)
78
78
 
79
79
  base_dir_config.file_to_include?(file)
80
80
  end
81
81
 
82
- def hidden_path?(path)
83
- path.include?(HIDDEN_PATH_SUBSTRING)
82
+ def hidden_dir?(dir)
83
+ basename = File.basename(dir)
84
+ basename.start_with?('.') && basename != '.' && basename != '..'
85
+ end
86
+
87
+ def hidden_file_in_dir?(file, base_dir)
88
+ base_dir = "#{base_dir}#{File::SEPARATOR}" unless base_dir.end_with?(File::SEPARATOR)
89
+ relative = file.delete_prefix(base_dir)
90
+ relative.start_with?('.') || relative.include?(HIDDEN_PATH_SUBSTRING)
84
91
  end
85
92
 
86
93
  def wanted_dir_patterns(base_dir, exclude_pattern, flags)
@@ -160,21 +160,18 @@ module RuboCop
160
160
  # The target ruby version may be found in a .ruby-version file.
161
161
  # @api private
162
162
  class RubyVersionFile < Source
163
- RUBY_VERSION_FILENAME = '.ruby-version'
164
- RUBY_VERSION_PATTERN = /\A(?:ruby-)?(?<version>\d+\.\d+)/.freeze
165
-
166
163
  def name
167
- "`#{RUBY_VERSION_FILENAME}`"
164
+ "`#{filename}`"
168
165
  end
169
166
 
170
167
  private
171
168
 
172
169
  def filename
173
- RUBY_VERSION_FILENAME
170
+ '.ruby-version'
174
171
  end
175
172
 
176
173
  def pattern
177
- RUBY_VERSION_PATTERN
174
+ /\A(?:ruby-)?(?<version>\d+\.\d+)/.freeze
178
175
  end
179
176
 
180
177
  def find_version
@@ -193,21 +190,29 @@ module RuboCop
193
190
  # starting with `ruby`.
194
191
  # @api private
195
192
  class ToolVersionsFile < RubyVersionFile
196
- TOOL_VERSIONS_FILENAME = '.tool-versions'
197
- TOOL_VERSIONS_PATTERN = /^(?:ruby )(?<version>\d+\.\d+)/.freeze
193
+ private
198
194
 
199
- def name
200
- "`#{TOOL_VERSIONS_FILENAME}`"
195
+ def filename
196
+ '.tool-versions'
201
197
  end
202
198
 
199
+ def pattern
200
+ /^(?:ruby )(?<version>\d+\.\d+)/.freeze
201
+ end
202
+ end
203
+
204
+ # The target ruby version may be found in a mise.toml file, in a line
205
+ # starting with `ruby = "`.
206
+ # @api private
207
+ class MiseTomlFile < RubyVersionFile
203
208
  private
204
209
 
205
210
  def filename
206
- TOOL_VERSIONS_FILENAME
211
+ 'mise.toml'
207
212
  end
208
213
 
209
214
  def pattern
210
- TOOL_VERSIONS_PATTERN
215
+ /^ruby = "(?<version>\d+\.\d+)/.freeze
211
216
  end
212
217
  end
213
218
 
@@ -275,6 +280,7 @@ module RuboCop
275
280
  RuboCopConfig,
276
281
  GemspecFile,
277
282
  RubyVersionFile,
283
+ MiseTomlFile,
278
284
  ToolVersionsFile,
279
285
  BundlerLockFile,
280
286
  Default
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.84.2'
6
+ STRING = '1.86.2'
7
7
 
8
8
  MSG = '%<version>s (using %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
@@ -55,7 +55,7 @@ module RuboCop
55
55
 
56
56
  # @api private
57
57
  def self.parser_version(target_ruby_version)
58
- config_path = ConfigFinder.find_config_path(Dir.pwd)
58
+ config_path = ConfigFinder.find_config_path(PathUtil.pwd)
59
59
  yaml = Util.silence_warnings do
60
60
  ConfigLoader.load_yaml_configuration(config_path)
61
61
  end