Skip to content

Commit

Permalink
Actually run temporarily enabled cops
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeo authored and bbatsov committed Feb 22, 2023
1 parent 52e8e4f commit efce268
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
1 change: 1 addition & 0 deletions changelog/fix_actually_run_temporarily_enabled_cops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11603](https://github.com/rubocop/rubocop/pull/11603): Actually run temporarily enabled cops. ([@tdeo][])
17 changes: 17 additions & 0 deletions lib/rubocop/comment_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def cop_enabled_at_line?(cop, line_number)
disabled_line_ranges.none? { |range| range.include?(line_number) }
end

def cop_opted_in?(cop)
opt_in_cops.include?(cop.cop_name)
end

def cop_disabled_line_ranges
@cop_disabled_line_ranges ||= analyze
end
Expand Down Expand Up @@ -74,6 +78,19 @@ def extra_enabled_comments_with_names(extras:, names:)
extras
end

def opt_in_cops
@opt_in_cops ||= begin
cops = Set.new
each_directive do |directive|
next unless directive.enabled?
next if directive.all_cops?

cops.merge(directive.cop_names)
end
cops
end
end

def analyze # rubocop:todo Metrics/AbcSize
return {} if @no_directives

Expand Down
17 changes: 10 additions & 7 deletions lib/rubocop/cop/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.mobilize(cop_classes, config, options = {})
def self.mobilize_cops(cop_classes, config, options = {})
cop_classes = Registry.new(cop_classes.to_a, options) unless cop_classes.is_a?(Registry)

cop_classes.enabled(config).map do |cop_class|
cop_classes.map do |cop_class|
cop_class.new(config, options)
end
end
Expand Down Expand Up @@ -58,6 +58,7 @@ def initialize(cops, config = nil, options = {})
@options = options
reset
@ready = true
@registry = Registry.new(cops, options.dup)

validate_config
end
Expand All @@ -84,7 +85,7 @@ def investigate(processed_source, offset: 0, original: processed_source)
# until there are no corrections left to perform
# To speed things up, run autocorrecting cops by themselves, and only
# run the other cops when no corrections are left
on_duty = roundup_relevant_cops(processed_source.file_path)
on_duty = roundup_relevant_cops(processed_source)

autocorrect_cops, other_cops = on_duty.partition(&:autocorrect?)
report = investigate_partial(autocorrect_cops, processed_source,
Expand Down Expand Up @@ -156,11 +157,13 @@ def investigate_partial(cops, processed_source, offset:, original:)
end

# @return [Array<cop>]
def roundup_relevant_cops(filename)
cops.reject do |cop|
cop.excluded_file?(filename) ||
!support_target_ruby_version?(cop) ||
!support_target_rails_version?(cop)
def roundup_relevant_cops(processed_source)
cops.select do |cop|
next true if processed_source.comment_config.cop_opted_in?(cop)
next false unless @registry.enabled?(cop, @config)
next false if cop.excluded_file?(processed_source.file_path)

support_target_ruby_version?(cop) && support_target_rails_version?(cop)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/rspec/cop_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def autocorrect_source(source, file = nil)
end

def _investigate(cop, processed_source)
team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
team = RuboCop::Cop::Team.new([cop], configuration, raise_error: true)
report = team.investigate(processed_source)
@last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
report.offenses.reject(&:disabled?)
Expand Down
15 changes: 7 additions & 8 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def and_with_args
end

describe 'for a disabled cop' do
it 'reports no offense when enabled on part of a file' do
it 'reports an offense when explicitly enabled on part of a file' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
SuggestExtensions: false
Expand All @@ -292,18 +292,17 @@ def and_with_args
a = 1
# rubocop:enable Lint/UselessAssignment
b = a
b += 1
b = 2
# rubocop:disable Lint/UselessAssignment
c = 2
c = 3
RUBY

expect(cli.run(['--format', 'offenses', 'example.rb'])).to eq(0)
expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
expect($stdout.string).to eq(<<~RESULT)
== example.rb ==
W: 5: 1: Lint/UselessAssignment: Useless assignment to variable - b.
--
0 Total in 0 files
1 file inspected, 1 offense detected
RESULT
end
end
Expand Down
19 changes: 0 additions & 19 deletions spec/rubocop/cop/team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,6 @@ def self.support_multiple_source?
expect(cops[1].name).to eq('Lint/Void')
end
end

context 'when some classes are disabled with config' do
let(:disabled_config) do
%w[
Lint/Void
Layout/LineLength
].each_with_object(RuboCop::Config.new) do |cop_name, accum|
accum[cop_name] = { 'Enabled' => false }
end
end
let(:config) { RuboCop::ConfigLoader.merge_with_default(disabled_config, '') }
let(:cop_names) { cops.map(&:name) }

it 'does not return instances of the classes' do
expect(cops.empty?).to be(false)
expect(cop_names.include?('Lint/Void')).to be(false)
expect(cop_names.include?('Layout/LineLength')).to be(false)
end
end
end

describe '#forces' do
Expand Down

0 comments on commit efce268

Please sign in to comment.