rubocop 1.92.2 → 1.93.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -1
  4. data/lib/rubocop/#directive_comment.rb# +224 -0
  5. data/lib/rubocop/cache_config.rb +3 -0
  6. data/lib/rubocop/cli.rb +5 -4
  7. data/lib/rubocop/config.rb +22 -0
  8. data/lib/rubocop/config_store.rb +1 -0
  9. data/lib/rubocop/config_validator.rb +45 -1
  10. data/lib/rubocop/cop/base.rb +5 -1
  11. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  12. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
  13. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  14. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
  16. data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
  17. data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  19. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  20. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  21. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  22. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  23. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  24. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  25. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
  26. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
  27. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  28. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  29. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  30. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
  31. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  32. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  33. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  34. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +14 -1
  36. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  39. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  40. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  41. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  42. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  43. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  44. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  45. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  46. data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
  47. data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
  48. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  49. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  50. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
  51. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  52. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  53. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
  54. data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
  55. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  56. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  57. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  58. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  59. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  60. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
  61. data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
  62. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  63. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  64. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  65. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  66. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  67. data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
  68. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
  69. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
  70. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
  71. data/lib/rubocop/cop/lint/useless_times.rb +52 -12
  72. data/lib/rubocop/cop/lint/void.rb +20 -9
  73. data/lib/rubocop/cop/message_annotator.rb +6 -8
  74. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  75. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
  76. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  77. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  78. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  79. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  80. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  81. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  82. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  83. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  84. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  85. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  86. data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
  87. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  88. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  89. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  90. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  91. data/lib/rubocop/cop/style/and_or.rb +8 -1
  92. data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
  93. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  94. data/lib/rubocop/cop/style/array_intersect.rb +15 -7
  95. data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
  96. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  97. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  98. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  99. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  100. data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
  101. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  102. data/lib/rubocop/cop/style/dig_chain.rb +13 -2
  103. data/lib/rubocop/cop/style/each_with_object.rb +21 -5
  104. data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
  105. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  106. data/lib/rubocop/cop/style/empty_method.rb +27 -4
  107. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  108. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  109. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  110. data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
  111. data/lib/rubocop/cop/style/file_read.rb +5 -1
  112. data/lib/rubocop/cop/style/file_write.rb +89 -20
  113. data/lib/rubocop/cop/style/for.rb +59 -4
  114. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  115. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  116. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  117. data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
  118. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  119. data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
  121. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  122. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  123. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  124. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  125. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  126. data/lib/rubocop/cop/style/lambda.rb +13 -5
  127. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  128. data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
  129. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  130. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  131. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  132. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  133. data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
  134. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
  135. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  136. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
  137. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
  138. data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
  139. data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
  140. data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
  141. data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
  142. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  143. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  144. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  145. data/lib/rubocop/cop/style/raise_args.rb +15 -0
  146. data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
  147. data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
  148. data/lib/rubocop/cop/style/redundant_each.rb +26 -1
  149. data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
  150. data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
  151. data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
  152. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
  153. data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
  154. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
  155. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  156. data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
  157. data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
  158. data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
  159. data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
  160. data/lib/rubocop/cop/style/select_by_range.rb +1 -6
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
  162. data/lib/rubocop/cop/style/self_assignment.rb +0 -4
  163. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  164. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  165. data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
  166. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  167. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  168. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  169. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  170. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  171. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  172. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  173. data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
  174. data/lib/rubocop/cop/style/swap_values.rb +10 -13
  175. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  176. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  177. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
  178. data/lib/rubocop/cop/style/word_array.rb +1 -3
  179. data/lib/rubocop/cop/util.rb +2 -0
  180. data/lib/rubocop/cop/variable_force.rb +14 -1
  181. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  182. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  183. data/lib/rubocop/result_cache.rb +9 -3
  184. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  185. data/lib/rubocop/rspec/shared_contexts.rb +11 -11
  186. data/lib/rubocop/runner.rb +21 -7
  187. data/lib/rubocop/server/cache.rb +24 -5
  188. data/lib/rubocop/target_ruby.rb +6 -8
  189. data/lib/rubocop/todo_audit.rb +61 -15
  190. data/lib/rubocop/version.rb +18 -8
  191. metadata +9 -5
@@ -8,17 +8,23 @@ module CopHelper
8
8
 
9
9
  @integrated_plugins = false
10
10
 
11
+ # Whether the specs run on one of the Prism parser engines.
12
+ def self.prism_engine?
13
+ %w[parser_prism prism].include?(ENV.fetch('PARSER_ENGINE', nil))
14
+ end
15
+
11
16
  class << self
12
17
  attr_accessor :integrated_plugins
13
18
  end
14
19
 
15
20
  let(:ruby_version) do
16
21
  # The minimum version Prism can parse is 3.3.
17
- ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
22
+ CopHelper.prism_engine? ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
18
23
  end
19
24
  let(:parser_engine) do
25
+ engine = ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym
20
26
  # The maximum version Parser can correctly parse is 3.3.
21
- ruby_version >= 3.4 ? :parser_prism : ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym
27
+ engine == :parser_whitequark && ruby_version >= 3.4 ? :parser_prism : engine
22
28
  end
23
29
  let(:rails_version) { false }
24
30
 
@@ -249,57 +249,57 @@ end
249
249
 
250
250
  RSpec.shared_context 'ruby 2.0' do
251
251
  # Prism supports parsing Ruby 3.3+.
252
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.0 }
252
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.0 }
253
253
  end
254
254
 
255
255
  RSpec.shared_context 'ruby 2.1' do
256
256
  # Prism supports parsing Ruby 3.3+.
257
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.1 }
257
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.1 }
258
258
  end
259
259
 
260
260
  RSpec.shared_context 'ruby 2.2' do
261
261
  # Prism supports parsing Ruby 3.3+.
262
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.2 }
262
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.2 }
263
263
  end
264
264
 
265
265
  RSpec.shared_context 'ruby 2.3' do
266
266
  # Prism supports parsing Ruby 3.3+.
267
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.3 }
267
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.3 }
268
268
  end
269
269
 
270
270
  RSpec.shared_context 'ruby 2.4' do
271
271
  # Prism supports parsing Ruby 3.3+.
272
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.4 }
272
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.4 }
273
273
  end
274
274
 
275
275
  RSpec.shared_context 'ruby 2.5' do
276
276
  # Prism supports parsing Ruby 3.3+.
277
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.5 }
277
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.5 }
278
278
  end
279
279
 
280
280
  RSpec.shared_context 'ruby 2.6' do
281
281
  # Prism supports parsing Ruby 3.3+.
282
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.6 }
282
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.6 }
283
283
  end
284
284
 
285
285
  RSpec.shared_context 'ruby 2.7' do
286
286
  # Prism supports parsing Ruby 3.3+.
287
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 2.7 }
287
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 2.7 }
288
288
  end
289
289
 
290
290
  RSpec.shared_context 'ruby 3.0' do
291
291
  # Prism supports parsing Ruby 3.3+.
292
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 3.0 }
292
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 3.0 }
293
293
  end
294
294
 
295
295
  RSpec.shared_context 'ruby 3.1' do
296
296
  # Prism supports parsing Ruby 3.3+.
297
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 3.1 }
297
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 3.1 }
298
298
  end
299
299
 
300
300
  RSpec.shared_context 'ruby 3.2' do
301
301
  # Prism supports parsing Ruby 3.3+.
302
- let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : 3.2 }
302
+ let(:ruby_version) { CopHelper.prism_engine? ? 3.3 : 3.2 }
303
303
  end
304
304
 
305
305
  RSpec.shared_context 'ruby 3.3' do
@@ -136,6 +136,9 @@ module RuboCop
136
136
  # particular run happens to include, or single-file runs (editors, CI
137
137
  # sharding) would see different offenses than full runs. The project is
138
138
  # rooted at the directory of the configuration that enabled the index.
139
+ #
140
+ # The project's RBS signatures are left out: they declare methods and constants
141
+ # rather than define them, and may declare only part of a namespace.
139
142
  def project_index_files(target_files)
140
143
  root = @config_store.for_pwd.base_dir_for_path_parameters
141
144
  project_files = begin
@@ -143,6 +146,7 @@ module RuboCop
143
146
  rescue Errno::ENOENT
144
147
  target_files
145
148
  end
149
+ project_files = project_files.reject { |file| File.extname(file) == '.rbs' }
146
150
 
147
151
  (project_files | bundled_gem_files).sort
148
152
  end
@@ -343,20 +347,29 @@ module RuboCop
343
347
  end
344
348
 
345
349
  def add_redundant_disables(file, offenses, source)
346
- team_for_redundant_disables(file, offenses, source) do |team|
347
- new_offenses, redundant_updated = inspect_file(source, team)
348
- offenses += new_offenses
349
- if redundant_updated
350
+ seen_sources = Set[source.raw_source]
351
+
352
+ loop do
353
+ redundant_updated = false
354
+
355
+ team_for_redundant_disables(file, offenses, source) do |team|
356
+ new_offenses, redundant_updated = inspect_file(source, team)
357
+ offenses += new_offenses
358
+ next unless redundant_updated
359
+
350
360
  # Do one extra inspection loop if any redundant disables were
351
361
  # removed. This is done in order to find rubocop:enable directives that
352
362
  # have now become useless. It finds the offenses that are left again,
353
363
  # located in the corrected source, so only the corrected ones are kept
354
364
  # from before, as `inspect_and_correct` does.
355
- _source, new_offenses = do_inspection_loop(file, team.updated_source)
365
+ source, new_offenses = do_inspection_loop(file, team.updated_source)
356
366
  offenses = offenses.select(&:corrected?) | new_offenses
367
+
368
+ redundant_updated = seen_sources.add?(source.raw_source)
357
369
  end
370
+
371
+ return offenses unless redundant_updated
358
372
  end
359
- offenses
360
373
  end
361
374
 
362
375
  def team_for_redundant_disables(file, offenses, source)
@@ -623,6 +636,7 @@ module RuboCop
623
636
  def mobilize_cop_badge?(badge, config)
624
637
  return false if badge.department == :Test
625
638
  return false if badge.match_name?(@options[:except])
639
+ return false if config.missing_default_config_for?(badge)
626
640
 
627
641
  if @options[:only]
628
642
  badge.match_name?(@options[:only])
@@ -724,7 +738,7 @@ module RuboCop
724
738
 
725
739
  # rubocop:disable-next Metrics/MethodLength
726
740
  def get_processed_source(file, prism_result, source: nil)
727
- config = @config_store.for_file(file)
741
+ config = @config_store.for_file(file).validate_parser_engine
728
742
  ruby_version = config.target_ruby_version
729
743
  parser_engine = config.parser_engine
730
744
 
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'digest'
4
+ require 'fileutils'
4
5
  require 'pathname'
6
+ require 'securerandom'
5
7
  require 'yaml'
6
8
  require_relative '../cache_config'
7
9
  require_relative '../config_finder'
@@ -79,7 +81,7 @@ module RuboCop
79
81
  CacheConfig.root_dir_from_toplevel_config
80
82
  end
81
83
 
82
- File.expand_path(File.join(cache_root_dir, 'server'))
84
+ File.expand_path(File.join(cache_root_dir, CacheConfig::SERVER_DIR))
83
85
  end
84
86
 
85
87
  def port_path
@@ -110,10 +112,19 @@ module RuboCop
110
112
  dir.join('stderr')
111
113
  end
112
114
 
115
+ def pid
116
+ pid = pid_path(create_dir: false).read.to_i
117
+ pid if pid.positive?
118
+ rescue Errno::ENOENT, Errno::EACCES, Errno::EPERM, Errno::EROFS, Errno::ENAMETOOLONG,
119
+ Errno::ENOTDIR
120
+ nil
121
+ end
122
+
113
123
  def pid_running?
114
- Process.kill(0, pid_path(create_dir: false).read.to_i) == 1
115
- rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES, Errno::EPERM, Errno::EROFS,
116
- Errno::ENAMETOOLONG, Errno::ENOTDIR
124
+ return false unless (pid = self.pid)
125
+
126
+ Process.kill(0, pid) == 1
127
+ rescue Errno::ESRCH, Errno::EPERM
117
128
  false
118
129
  end
119
130
 
@@ -134,7 +145,7 @@ module RuboCop
134
145
  pid_path.write(Process.pid)
135
146
  yield
136
147
  ensure
137
- dir.rmtree
148
+ remove_dir
138
149
  end
139
150
 
140
151
  def write_status_file(status)
@@ -183,6 +194,14 @@ module RuboCop
183
194
  require 'yaml'
184
195
  YAML.safe_load(yaml_code, permitted_classes: [Regexp, Symbol], aliases: true) || {}
185
196
  end
197
+
198
+ def remove_dir
199
+ removed_dir_path = "#{dir_path}.#{SecureRandom.hex(8)}.removed"
200
+ File.rename(dir_path, removed_dir_path)
201
+ FileUtils.rm_rf(removed_dir_path)
202
+ rescue Errno::ENOENT
203
+ nil
204
+ end
186
205
  end
187
206
  end
188
207
  end
@@ -120,14 +120,12 @@ module RuboCop
120
120
  end
121
121
 
122
122
  def version_from_gemspec_file(file)
123
- # When using parser_prism, we need to use a Ruby version that Prism supports (3.3+)
124
- # for parsing the gemspec file. This doesn't affect the detected Ruby version,
125
- # it's just for the parsing step.
126
- ruby_version_for_parsing = if @config.parser_engine == :parser_prism
127
- 3.3
128
- else
129
- DEFAULT_VERSION
130
- end
123
+ # The version is only for parsing the gemspec, it doesn't affect the
124
+ # detected Ruby version. Prism parses Ruby 3.3 and later, and it is
125
+ # what the default engine picks for such a version, so `parser` is
126
+ # only needed here when it was asked for.
127
+ whitequark = @config.parser_engine == :parser_whitequark
128
+ ruby_version_for_parsing = whitequark ? DEFAULT_VERSION : 3.3
131
129
 
132
130
  processed_source = ProcessedSource.from_file(
133
131
  file, ruby_version_for_parsing, parser_engine: @config.parser_engine
@@ -21,8 +21,8 @@ module RuboCop
21
21
  entries = todo_exclude_entries
22
22
  return [] if entries.empty?
23
23
 
24
- offending_cops = offending_cops_without_todo(entries)
25
- entries.reject { |entry| offending_cops[absolute(entry.path)]&.include?(entry.cop_name) }
24
+ @todo_entries = entries
25
+ entries.reject { |entry| needed?(entry) }
26
26
  end
27
27
 
28
28
  def todo_file
@@ -59,37 +59,83 @@ module RuboCop
59
59
  Cop::Registry.global.contains_cop_matching?([cop_name])
60
60
  end
61
61
 
62
- # Inspects every file the todo entries mention, with the todo exclusions
63
- # subtracted from the configuration, and returns the names of the cops
64
- # that still report offenses, keyed by absolute file path.
65
- def offending_cops_without_todo(entries)
66
- files = entries.map { |entry| absolute(entry.path) }.uniq.select { |file| File.file?(file) }
62
+ # An entry is needed when any expanded path still reports an offense for
63
+ # its cop. Stop at the first hit so live glob entries skip most matches.
64
+ def needed?(entry)
65
+ expand_path(entry.path).any? { |file| offending_cops(file).include?(entry.cop_name) }
66
+ end
67
+
68
+ def offending_cops(file)
69
+ @offending_cops ||= {}
70
+ @offending_cops[file] ||= inspect_file(file).to_set(&:cop_name)
71
+ end
72
+
73
+ def expand_path(path)
74
+ absolute_path = absolute(path)
75
+ files = File.file?(absolute_path) ? [absolute_path] : []
76
+ return files unless PathUtil.glob?(path)
67
77
 
68
- files.to_h do |file|
69
- offenses = inspect_file(file, entries)
70
- [file, offenses.to_set(&:cop_name)]
78
+ files | target_files(path).select do |file|
79
+ PathUtil.match_path?(absolute_path, file)
71
80
  end
72
81
  end
73
82
 
74
- def inspect_file(file, entries)
75
- config = audit_config(@config_store.for_file(file), entries)
76
- team = Cop::Team.mobilize(Cop::Registry.global, config, @options.merge(autocorrect: false))
83
+ # Scan from the longest glob-free prefix of the entry as written, so a
84
+ # glob in any directory segment (e.g. `**/models/*.rb`) is covered.
85
+ # Only the entry's own segments are inspected; metacharacters in the
86
+ # todo file's parent directories never move the scan root.
87
+ def target_files(path)
88
+ directory = scan_root(path)
89
+ @target_files ||= {}
90
+ @target_files[directory] ||= TargetFinder.new(@config_store, @options)
91
+ .target_files_in_dir(directory)
92
+ end
93
+
94
+ def scan_root(path)
95
+ segments = path.split('/', -1)
96
+ literal = segments.take_while { |segment| !PathUtil.glob?(segment) }
97
+ prefix = literal.join('/')
98
+ prefix = '/' if prefix.empty? && path.start_with?('/')
99
+ absolute(prefix.empty? ? '.' : prefix)
100
+ end
101
+
102
+ def inspect_file(file)
103
+ config = audit_config(@config_store.for_file(file))
104
+ team = Cop::Team.mobilize(todo_cops, config, @options.merge(autocorrect: false))
77
105
 
78
106
  processed_source = ProcessedSource.from_file(
79
107
  file, config.target_ruby_version, parser_engine: config.parser_engine
80
108
  )
81
109
  processed_source.config = config
110
+ # CommentConfig reads this registry to recognize cops disabled in the
111
+ # config. A filtered registry would treat `# rubocop:enable` of such a
112
+ # cop as redundant and keep a stale todo entry.
82
113
  processed_source.registry = Cop::Registry.global
83
114
 
84
115
  team.investigate(processed_source).offenses.reject(&:disabled?)
85
116
  end
86
117
 
118
+ # Registry limited to cops named in the todo, so each file only pays for
119
+ # the audits that can keep an entry.
120
+ def todo_cops
121
+ @todo_cops ||= begin
122
+ names = @todo_entries.map(&:cop_name).uniq
123
+ Cop::Registry.global.filter_by_badge { |badge| badge.match_name?(names) }
124
+ end
125
+ end
126
+
87
127
  # A copy of the configuration with the todo exclusions removed for the
88
128
  # audited cops, so their offenses in the listed files become visible.
89
- def audit_config(config, entries)
129
+ # Memoized per underlying `Config` so cop lookups stay warm across files.
130
+ def audit_config(config)
131
+ @audit_configs ||= {}.compare_by_identity
132
+ @audit_configs[config] ||= build_audit_config(config)
133
+ end
134
+
135
+ def build_audit_config(config)
90
136
  hash = config.to_hash.dup
91
137
 
92
- entries.group_by(&:cop_name).each do |cop_name, cop_entries|
138
+ @todo_entries.group_by(&:cop_name).each do |cop_name, cop_entries|
93
139
  cop_config = hash[cop_name]
94
140
  next unless cop_config.is_a?(Hash) && cop_config['Exclude'].is_a?(Array)
95
141
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.92.2'
6
+ STRING = '1.93.0'
7
7
 
8
8
  MSG = '%<version>s (using %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
@@ -60,14 +60,24 @@ module RuboCop
60
60
  yaml = Util.silence_warnings do
61
61
  ConfigLoader.load_yaml_configuration(config_path)
62
62
  end
63
- parser_engine = yaml.dig('AllCops', 'ParserEngine')
64
- parser_engine_text = ", #{parser_engine}" if parser_engine
63
+ # Without the `parser` gem its version is that of the copy of its source
64
+ # layer in RuboCop AST, which is not what parses the code.
65
+ parser = "Parser #{Parser::VERSION}" if ProcessedSource.parser_gem_available?
66
+ prism = "Prism #{Prism::VERSION}"
67
+ return parser || prism if target_ruby_version < MINIMUM_PARSABLE_PRISM_VERSION
65
68
 
66
- if target_ruby_version >= MINIMUM_PARSABLE_PRISM_VERSION
67
- "Parser #{Parser::VERSION}, Prism #{Prism::VERSION}#{parser_engine_text}"
68
- else
69
- "Parser #{Parser::VERSION}"
70
- end
69
+ parser_engine = parser_engine(yaml.dig('AllCops', 'ParserEngine'), target_ruby_version)
70
+
71
+ [parser, prism, parser_engine].compact.join(', ')
72
+ end
73
+
74
+ # The default parser engine depends on the target Ruby version and on the
75
+ # version of Prism, so the one that is used is asked from RuboCop AST.
76
+ # @api private
77
+ def self.parser_engine(configured, target_ruby_version)
78
+ return configured unless configured.nil? || configured.to_s == 'default'
79
+
80
+ ProcessedSource.default_parser_engine(target_ruby_version)
71
81
  end
72
82
 
73
83
  # @api private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.92.2
4
+ version: 1.93.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -127,7 +127,7 @@ dependencies:
127
127
  requirements:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
- version: 1.49.0
130
+ version: 1.54.0
131
131
  - - "<"
132
132
  - !ruby/object:Gem::Version
133
133
  version: '2.0'
@@ -137,7 +137,7 @@ dependencies:
137
137
  requirements:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
- version: 1.49.0
140
+ version: 1.54.0
141
141
  - - "<"
142
142
  - !ruby/object:Gem::Version
143
143
  version: '2.0'
@@ -196,6 +196,7 @@ files:
196
196
  - config/obsoletion.yml
197
197
  - exe/rubocop
198
198
  - lib/rubocop.rb
199
+ - lib/rubocop/#directive_comment.rb#
199
200
  - lib/rubocop/arguments_env.rb
200
201
  - lib/rubocop/arguments_file.rb
201
202
  - lib/rubocop/ast_aliases.rb
@@ -300,6 +301,7 @@ files:
300
301
  - lib/rubocop/cop/internal_affairs/example_description.rb
301
302
  - lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb
302
303
  - lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb
304
+ - lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb
303
305
  - lib/rubocop/cop/internal_affairs/itblock_handler.rb
304
306
  - lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
305
307
  - lib/rubocop/cop/internal_affairs/location_exists.rb
@@ -313,6 +315,7 @@ files:
313
315
  - lib/rubocop/cop/internal_affairs/node_pattern_groups.rb
314
316
  - lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb
315
317
  - lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb
318
+ - lib/rubocop/cop/internal_affairs/node_search_as_condition.rb
316
319
  - lib/rubocop/cop/internal_affairs/node_type_group.rb
317
320
  - lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb
318
321
  - lib/rubocop/cop/internal_affairs/node_type_predicate.rb
@@ -322,6 +325,7 @@ files:
322
325
  - lib/rubocop/cop/internal_affairs/operator_keyword.rb
323
326
  - lib/rubocop/cop/internal_affairs/plugin.rb
324
327
  - lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb
328
+ - lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb
325
329
  - lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.rb
326
330
  - lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
327
331
  - lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb
@@ -1157,9 +1161,9 @@ licenses:
1157
1161
  - MIT
1158
1162
  metadata:
1159
1163
  homepage_uri: https://rubocop.org/
1160
- changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.92.2
1164
+ changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.93.0
1161
1165
  source_code_uri: https://github.com/rubocop/rubocop/
1162
- documentation_uri: https://docs.rubocop.org/rubocop/1.92/
1166
+ documentation_uri: https://docs.rubocop.org/rubocop/1.93/
1163
1167
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1164
1168
  rubygems_mfa_required: 'true'
1165
1169
  rdoc_options: []