From 45186bc9c7e1ea79e1103d09f3e1fa6d135f8c05 Mon Sep 17 00:00:00 2001 From: Don Kelly Date: Tue, 29 Oct 2024 21:43:06 -0400 Subject: [PATCH] Replaces use of Cop::investigate in favour of on_new_investigation Replacing the deprecated Cop base class with Cop::Base means that we need to avoid investigate. Following the example of comment processing Cops including with rubocop, we should instead use on_new_investigation. The expect_offense test helper also needed reworking. Cop::Base doesn't allow access to the offenses detected. Those should come from the InvestigationReport from the Commissioner instance that invokes the investigation (via Commissioner::investigate). --- lib/smart_todo_cop.rb | 2 +- test/smart_todo/smart_todo_cop_test.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/smart_todo_cop.rb b/lib/smart_todo_cop.rb index 2569d40..4553551 100644 --- a/lib/smart_todo_cop.rb +++ b/lib/smart_todo_cop.rb @@ -15,7 +15,7 @@ class SmartTodoCop < Base # @param processed_source [RuboCop::ProcessedSource] # @return [void] - def investigate(processed_source) + def on_new_investigation processed_source.comments.each do |comment| next unless /^#\sTODO/.match?(comment.text) diff --git a/test/smart_todo/smart_todo_cop_test.rb b/test/smart_todo/smart_todo_cop_test.rb index 6192a27..eecf067 100644 --- a/test/smart_todo/smart_todo_cop_test.rb +++ b/test/smart_todo/smart_todo_cop_test.rb @@ -84,19 +84,19 @@ def expected_message def expect_offense(source) annotated_source = RuboCop::RSpec::ExpectOffense::AnnotatedSource.parse(source) - investigate(annotated_source.plain_source) + report = investigate(annotated_source.plain_source) - actual_annotations = annotated_source.with_offense_annotations(cop.offenses) - assert_equal(actual_annotations.to_s, annotated_source.to_s) + actual_annotations = annotated_source.with_offense_annotations(report.offenses) + assert_equal(annotated_source.to_s, actual_annotations.to_s) end alias_method :expect_no_offense, :expect_offense def investigate(source, ruby_version = 2.5, file = "(file)") processed_source = RuboCop::ProcessedSource.new(source, ruby_version, file) - RuboCop::Cop::Commissioner.new([cop], [], raise_error: true).tap do |commissioner| - commissioner.investigate(processed_source) - end + assert(processed_source.valid_syntax?) + comm = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) + comm.investigate(processed_source) end def cop