Skip to content

Commit

Permalink
Replaces use of Cop::investigate in favour of on_new_investigation
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
donk-shopify committed Oct 30, 2024
1 parent 0ed1b5f commit 45186bc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/smart_todo_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions test/smart_todo/smart_todo_cop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45186bc

Please sign in to comment.