Skip to content

Commit

Permalink
Add compatibility for RuboCop v1.38.0 and above
Browse files Browse the repository at this point in the history
Due to rubocop/rubocop#10987 we need to
set `ProcessedSource#config` and `ProcessedSource#registry`.

Not doing so caused `Lint/MissingCopEnableDirective` and
`Lint/RedundantCopDisableDirective` cops to crash.
  • Loading branch information
leoarnold committed Jan 7, 2023
1 parent beac06b commit 8d00fd0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/pkg/
/spec/reports/
/tmp/
Gemfile.lock

# rspec failure tracking
.rspec_status
72 changes: 0 additions & 72 deletions Gemfile.lock

This file was deleted.

20 changes: 18 additions & 2 deletions lib/templatecop/ruby_offense_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,35 @@ def call

private

# @return [RuboCop::Cop::Registry]
def registry
@registry ||= begin
all_cops = if ::RuboCop::Cop::Registry.respond_to?(:all)
::RuboCop::Cop::Registry.all
else
::RuboCop::Cop::Cop.all
end

::RuboCop::Cop::Registry.new(all_cops)
end
end

# @return [RuboCop::ProcessedSource]
def rubocop_processed_source
@rubocop_processed_source ||= ::RuboCop::ProcessedSource.new(
@source,
@rubocop_config.target_ruby_version,
@file_path
)
).tap do |processed_source|
processed_source.config = @rubocop_config if processed_source.respond_to?(:config)
processed_source.registry = registry if processed_source.respond_to?(:registry)
end
end

# @return [RuboCop::Cop::Team]
def rubocop_team
::RuboCop::Cop::Team.new(
::RuboCop::Cop::Registry.new(::RuboCop::Cop::Cop.all),
registry,
@rubocop_config,
auto_correct: @auto_correct,
display_cop_names: true,
Expand Down
18 changes: 16 additions & 2 deletions spec/templatecop/ruby_offense_collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
context 'with rubocop:todo comment' do
let(:source) do
<<~RUBY
"a" \# rubocop:todo Style/StringLiterals
"a" # rubocop:todo Style/StringLiterals
RUBY
end

Expand All @@ -42,13 +42,27 @@
context 'with rubocop:disable comment' do
let(:source) do
<<~RUBY
"a" \# rubocop:disable Style/StringLiterals
"a" # rubocop:disable Style/StringLiterals
RUBY
end

it 'excludes disabled offenses' do
expect(subject).to be_empty
end
end

context 'with rubocop:disable comment with missing cop enable directive' do
let(:source) do
<<~RUBY
# rubocop:disable Style/StringLiterals
"a"
RUBY
end

it 'excludes disabled offenses' do
expect(subject.size).to eq 1
expect(subject.first.cop_name).to eq 'Lint/MissingCopEnableDirective'
end
end
end
end
2 changes: 1 addition & 1 deletion spec/templatecop/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

let(:rubocop_config) do
::RuboCop::ConfigLoader.default_configuration
RuboCop::ConfigLoader.default_configuration
end

let(:ruby_extractor) do
Expand Down

0 comments on commit 8d00fd0

Please sign in to comment.