metz-scan 0.6.0 → 0.7.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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/lib/metz_scan/analyzers/package_map.rb +1 -1
  3. data/lib/metz_scan/analyzers/test_calls_private_method/receiver_resolution.rb +3 -3
  4. data/lib/metz_scan/analyzers/test_calls_private_method/scope_resolution.rb +1 -1
  5. data/lib/metz_scan/commands/report.rb +16 -3
  6. data/lib/metz_scan/commands/scan/auto_fix.rb +49 -51
  7. data/lib/metz_scan/commands/scan/check_failure.rb +42 -0
  8. data/lib/metz_scan/commands/scan/config_disables.rb +83 -0
  9. data/lib/metz_scan/commands/scan/config_exclusions.rb +66 -30
  10. data/lib/metz_scan/commands/scan/exclude_entries.rb +5 -0
  11. data/lib/metz_scan/commands/scan/exclude_entry_locator.rb +15 -5
  12. data/lib/metz_scan/commands/scan/exclude_provenance.rb +7 -2
  13. data/lib/metz_scan/commands/scan/execution_config_store.rb +142 -0
  14. data/lib/metz_scan/commands/scan/ignored_cli_options_note.rb +35 -0
  15. data/lib/metz_scan/commands/scan/inherited_include_rewriter.rb +43 -0
  16. data/lib/metz_scan/commands/scan/inline_directive_locator.rb +38 -11
  17. data/lib/metz_scan/commands/scan/json_guidance.rb +81 -0
  18. data/lib/metz_scan/commands/scan/offense_extractor.rb +29 -7
  19. data/lib/metz_scan/commands/scan/path_scoped_include_note.rb +73 -0
  20. data/lib/metz_scan/commands/scan/project_analyzer_offenses.rb +21 -5
  21. data/lib/metz_scan/commands/scan/project_analyzer_runner.rb +21 -23
  22. data/lib/metz_scan/commands/scan/project_config_scope.rb +85 -28
  23. data/lib/metz_scan/commands/scan/rubocop_failure.rb +38 -0
  24. data/lib/metz_scan/commands/scan/rubocop_invocation.rb +141 -0
  25. data/lib/metz_scan/commands/scan/runner.rb +103 -49
  26. data/lib/metz_scan/commands/scan/sarif_rule_descriptors.rb +6 -24
  27. data/lib/metz_scan/commands/scan/suppression_ledger.rb +51 -14
  28. data/lib/metz_scan/commands/scan/suppression_ledger_formatter.rb +7 -1
  29. data/lib/metz_scan/commands/scan/target_ruby_version.rb +3 -2
  30. data/lib/metz_scan/commands/scan.rb +43 -21
  31. data/lib/metz_scan/project_index/rubydex_backend/method_visibility.rb +3 -0
  32. data/lib/metz_scan/version.rb +1 -1
  33. data/metz-scan.gemspec +9 -2
  34. metadata +16 -15
  35. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/artifact_writer.rb +0 -51
  36. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/baseline_delta.rb +0 -231
  37. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/collaborators.rb +0 -76
  38. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/markdown_renderer.rb +0 -353
  39. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/readiness_catalog.rb +0 -127
  40. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/summary.rb +0 -333
  41. data/lib/metz_scan/calibration/project_analyzer_evidence_runner/target_set.rb +0 -139
  42. data/lib/metz_scan/calibration/project_analyzer_evidence_runner.rb +0 -64
  43. data/lib/metz_scan/calibration/rubydex_drift_formatter.rb +0 -91
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8ab2475c975b0058d11664aeddf01dd91b55538137e55ed65af273c4aa62874
4
- data.tar.gz: d518ad9b70df91a4e74509f03883a911ea5a858cee6bb72a4f17328d7831c291
3
+ metadata.gz: 883fb11d1f38d3ef73542b1c93ef5d891fcfbb1ec2b15d4a406689b49635009e
4
+ data.tar.gz: 6abd6422cf086ea74289ceef92a2e0011f19cba651a24f48eba8ee1626fa1c2a
5
5
  SHA512:
6
- metadata.gz: d63399acc40747d1630dd66a0579d9ea906b71341acfc65ace51f9c39f8dd4e3f1d87b5892a2d0e6b14ddbd115aa8d52ad5eccf69865be695776b5c36ddcc538
7
- data.tar.gz: 5d450a47bc76f4dab47a041b4b278e28568f5d692b14a280e95705b7df4106af4c8f9e98ae3c53d8014211a185728e4668bece96637c846608891a4665604fa8
6
+ metadata.gz: bf2df9dea504d2bd8816a696771437b470234e871305f6f07772bc9fcc130cf81b18492ca0570fdf438aa71f73847985f05e334d96a8a087f007af98f1022ae0
7
+ data.tar.gz: 05d9e75a7e89b8374261082d8550ab3b852f5477dfb85877ef6818d3ae977974f4fb1e4365f36b0225aa05e21919e076af7653173821aee4eec34a555c204ae9
@@ -70,7 +70,7 @@ module MetzScan
70
70
  end
71
71
 
72
72
  def project_root_index(parts)
73
- parts.each_index.reverse_each.find { |index| project_root?(parts[index]) }
73
+ parts.rindex { |part| project_root?(part) }
74
74
  end
75
75
 
76
76
  def project_root?(part)
@@ -45,7 +45,7 @@ module MetzScan
45
45
  end
46
46
 
47
47
  def named_subject_owner(name, scopes)
48
- scope = scopes.reverse.find { |candidate| candidate.subject_definitions.key?(name) }
48
+ scope = scopes.rfind { |candidate| candidate.subject_definitions.key?(name) }
49
49
  scope&.subject_definitions&.fetch(name) == :sut ? scope.sut_name : nil
50
50
  end
51
51
 
@@ -56,7 +56,7 @@ module MetzScan
56
56
  end
57
57
 
58
58
  def explicit_subject_owner(scopes)
59
- scope = scopes.reverse.find { |candidate| candidate.subject_definitions[:subject] == :sut }
59
+ scope = scopes.rfind { |candidate| candidate.subject_definitions[:subject] == :sut }
60
60
  scope&.sut_name
61
61
  end
62
62
 
@@ -65,7 +65,7 @@ module MetzScan
65
65
  end
66
66
 
67
67
  def local_owner_for(name, scopes)
68
- scope = scopes.reverse.find { |candidate| candidate.kind == :local && candidate.assignments.key?(name) }
68
+ scope = scopes.rfind { |candidate| candidate.kind == :local && candidate.assignments.key?(name) }
69
69
  scope&.assignments&.fetch(name)
70
70
  end
71
71
 
@@ -47,7 +47,7 @@ module MetzScan
47
47
  end
48
48
 
49
49
  def current_sut_name(scopes)
50
- scope = scopes.reverse.find { |candidate| SCOPE_KINDS.include?(candidate.kind) }
50
+ scope = scopes.rfind { |candidate| SCOPE_KINDS.include?(candidate.kind) }
51
51
  scope&.sut_name
52
52
  end
53
53
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "json"
4
4
  require "optparse"
5
+ require "rubocop-metz"
5
6
 
6
7
  require_relative "scan/runner"
7
8
  require_relative "scan/compliance_scorecard"
@@ -61,19 +62,26 @@ module MetzScan
61
62
  end
62
63
 
63
64
  def emit(options)
64
- parsed = load_json(options.path)
65
+ parsed = load_report(options.path)
65
66
  return parsed if parsed.is_a?(Integer)
66
67
 
67
68
  render(parsed, options.format)
68
69
  Scan::Runner.exit_code_for(parsed)
69
70
  end
70
71
 
71
- def load_json(path)
72
- JSON.parse(File.read(path))
72
+ def load_report(path)
73
+ parsed = JSON.parse(File.read(path))
74
+ report?(parsed) ? parsed : not_a_report(path)
73
75
  rescue JSON::ParserError => e
74
76
  invalid_json(path, e.message)
75
77
  end
76
78
 
79
+ # The shape shared by `rubocop --format json` and `metz-scan scan --format json`.
80
+ def report?(parsed)
81
+ parsed.is_a?(Hash) && parsed["files"].is_a?(Array) &&
82
+ parsed["files"].all? { |file| file.is_a?(Hash) && file["offenses"].is_a?(Array) }
83
+ end
84
+
77
85
  def render(parsed, format)
78
86
  return render_json(parsed) if format == "json"
79
87
 
@@ -111,6 +119,11 @@ module MetzScan
111
119
  stderr.puts "metz-scan report: invalid JSON in #{path}: #{detail}"
112
120
  CLI::USAGE_ERROR
113
121
  end
122
+
123
+ def not_a_report(path)
124
+ stderr.puts "metz-scan report: not a metz-scan JSON report: #{path}"
125
+ CLI::USAGE_ERROR
126
+ end
114
127
  end
115
128
  end
116
129
  end
@@ -1,55 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
3
4
  require "open3"
4
- require "stringio"
5
5
  require "tempfile"
6
6
 
7
+ require "metz_scan/commands/scan/ignored_cli_options_note"
8
+ require "metz_scan/commands/scan/rubocop_invocation"
9
+
7
10
  module MetzScan
8
11
  module Commands
9
12
  class Scan
10
13
  class AutoFix
11
- CommandResult = Struct.new(:status, :stdout, :stderr, keyword_init: true)
12
-
13
14
  def initialize(stdout:, stderr:)
14
15
  @stdout = stdout
15
16
  @stderr = stderr
16
17
  end
17
18
 
18
19
  def run(options)
19
- require "rubocop-metz"
20
- options.dry_run ? run_dry(options) : run_in_place(options)
20
+ Runner.with_errors { Runner.require_metz_plugin }
21
+ scope = resolve_scope(options)
22
+ status = options.dry_run ? run_dry(options, scope) : run_in_place(options, scope)
23
+ IgnoredCliOptionsNote.print_note(stderr)
24
+ status
21
25
  end
22
26
 
23
27
  private
24
28
 
25
29
  attr_reader :stdout, :stderr
26
30
 
27
- def run_in_place(options)
28
- with_redirected_io { RuboCop::CLI.new.run(rubocop_argv(options)) }
29
- end
31
+ def resolve_scope(options)
32
+ scope = options.all_cops ? {} : TargetFileDiscovery.scan_scope(options.paths)
33
+ paths = Runner.with_errors { Runner.inspection_paths(options.paths, all_cops: options.all_cops, **scope) }
34
+ raise CheckFailure::NoFilesScanned, options.paths if !options.all_cops && paths.empty?
30
35
 
31
- def with_redirected_io
32
- previous = redirect_to(stdout, stderr)
33
- yield
34
- ensure
35
- restore_io(previous) if previous
36
+ { paths: paths, store: scope[:store] }
36
37
  end
37
38
 
38
- def redirect_to(out, err)
39
- previous = [$stdout, $stderr]
40
- $stdout = out
41
- $stderr = err
42
- previous
43
- end
44
-
45
- def restore_io(previous)
46
- $stdout = previous[0]
47
- $stderr = previous[1]
39
+ def run_in_place(options, scope)
40
+ result = checked_rubocop(options, scope)
41
+ stdout.write(result.stdout)
42
+ stderr.write(result.stderr)
43
+ result.status
48
44
  end
49
45
 
50
- def run_dry(options)
51
- snapshot = capture_files(options.paths)
52
- dry_run_result(snapshot, capture_rubocop(rubocop_argv(options)))
46
+ def run_dry(options, scope)
47
+ snapshot = capture_files(scope[:paths])
48
+ dry_run_result(snapshot, checked_rubocop(options, scope))
53
49
  ensure
54
50
  restore_files(snapshot)
55
51
  end
@@ -60,42 +56,44 @@ module MetzScan
60
56
  result.status
61
57
  end
62
58
 
63
- def rubocop_argv(options)
64
- flag = options.unsafe ? "-A" : "-a"
65
- selection = Runner.cop_selection_argv(options.all_cops)
66
- selection += ["--disable-pending-cops"] unless options.all_cops
67
- ["--plugin", "rubocop-metz", *selection, flag, *options.paths]
59
+ # Fails like a plain scan, before any output: RuboCop exits 1 for a cop error and for
60
+ # remaining offenses alike, and only its own report says how many files it inspected.
61
+ def checked_rubocop(options, scope)
62
+ Tempfile.create(["metz-auto-fix-", ".json"]) do |report|
63
+ run_rubocop(options, scope, report.path).tap { |result| check!(result, report, options) }
64
+ end
68
65
  end
69
66
 
70
- def capture_rubocop(argv)
71
- out = StringIO.new
72
- err = StringIO.new
73
- status = with_captured_io(out, err) { RuboCop::CLI.new.run(argv) }
74
- CommandResult.new(status: status, stdout: out.string, stderr: err.string)
67
+ def run_rubocop(options, scope, report_path)
68
+ Runner.with_errors do
69
+ TargetRubyVersion.with_project_config(scope[:paths], all_cops: options.all_cops, store: scope[:store]) do
70
+ invoke_rubocop(options, scope, report_path)
71
+ end
72
+ end
75
73
  end
76
74
 
77
- def with_captured_io(out, err)
78
- previous = redirect_to(out, err)
79
- yield
80
- ensure
81
- restore_io(previous) if previous
75
+ def invoke_rubocop(options, scope, report_path)
76
+ RuboCopInvocation.auto_fix(scope[:paths], options: options, report_path: report_path, store: scope[:store])
82
77
  end
83
78
 
84
- def capture_files(paths)
85
- files_under(paths).to_h { |path| [path, File.binread(path)] }
79
+ def check!(result, report, options)
80
+ raise Runner::Error, RubocopFailure.message(result) if RubocopFailure.failed?(result)
81
+ raise CheckFailure::NoFilesScanned, options.paths if inspected_file_count(result, report.path).zero?
86
82
  end
87
83
 
88
- def files_under(paths)
89
- paths.flat_map { |path| File.directory?(path) ? regular_files_in(path) : [path] }
84
+ def inspected_file_count(result, report_path)
85
+ JSON.parse(File.read(report_path)).dig("summary", "inspected_file_count")
86
+ rescue JSON::ParserError
87
+ raise Runner::Error, RubocopFailure.message(result)
90
88
  end
91
89
 
92
- def regular_files_in(dir)
93
- Dir.glob(File.join(dir, "**/*")).select { |entry| File.file?(entry) }
94
- end
90
+ def capture_files(paths) = files_under(paths).to_h { |path| [path, File.binread(path)] }
95
91
 
96
- def print_diffs(snapshot)
97
- snapshot.each { |path, original| print_file_diff(path, original) }
98
- end
92
+ def files_under(paths) = paths.flat_map { |path| File.directory?(path) ? regular_files_in(path) : [path] }
93
+
94
+ def regular_files_in(dir) = Dir.glob(File.join(dir, "**/*")).select { |entry| File.file?(entry) }
95
+
96
+ def print_diffs(snapshot) = snapshot.each { |path, original| print_file_diff(path, original) }
99
97
 
100
98
  def print_rubocop_failure(result)
101
99
  stderr.print(result.stderr) unless result.stderr.empty?
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MetzScan
4
+ module Commands
5
+ class Scan
6
+ # Every way `scan` can fail to check the code ends here: the cause goes to stderr, stdout
7
+ # gets no report, and the exit status is 2, even when findings were also found.
8
+ class CheckFailure
9
+ EXIT_STATUS = 2
10
+ # Every exception except SystemExit and signals (Interrupt), which must pass through.
11
+ INTERNAL_ERRORS = [StandardError, ScriptError, SystemStackError, NoMemoryError, SecurityError].freeze
12
+
13
+ class NoFilesScanned < StandardError
14
+ def initialize(paths)
15
+ super("no files scanned in #{paths.join(', ')}")
16
+ end
17
+ end
18
+
19
+ def initialize(stderr)
20
+ @stderr = stderr
21
+ end
22
+
23
+ def report(error)
24
+ stderr.puts "metz-scan scan: #{description(error)}"
25
+ EXIT_STATUS
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :stderr
31
+
32
+ def description(error)
33
+ case error
34
+ when Runner::Error then "RuboCop failed: #{error.message}"
35
+ when NoFilesScanned then error.message
36
+ else ["internal error: #{error.class}: #{error.message}", *error.backtrace].join("\n")
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "metz_scan/commands/scan/exclude_entry_locator"
4
+
5
+ module MetzScan
6
+ module Commands
7
+ class Scan
8
+ # Default `scan` strips project `Enabled` via ExecutionConfigStore, so a
9
+ # project's `Enabled: false` would not stop a Metz cop or project
10
+ # analyzer from reporting. Its offenses are hidden here instead, and the
11
+ # project config that disabled it is credited in the suppression ledger.
12
+ class ConfigDisables
13
+ def initialize(store, rubocop_store: nil)
14
+ @store = store
15
+ @rubocop_store = rubocop_store
16
+ @locators = {}
17
+ @raw_configs = {}
18
+ end
19
+
20
+ def scoped_off?(path, offense)
21
+ !disabling_config(path, offense).nil? || rubocop_disabled?(path, offense)
22
+ end
23
+
24
+ def record_fields(path, offense)
25
+ entry = disabling_config(path, offense)
26
+ return local_record_fields(entry) if entry
27
+
28
+ remote_record_fields(rubocop_config(path))
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :store, :rubocop_store
34
+
35
+ def local_record_fields(entry)
36
+ config_path, key = entry
37
+ location = locator(config_path).locate_enabled(key)
38
+ disable_record(config_path, location.line, location.reason, location.reason_status)
39
+ end
40
+
41
+ def remote_record_fields(config)
42
+ disable_record(config.loaded_path, nil, nil, "unchecked")
43
+ end
44
+
45
+ def disable_record(config_path, line, reason, reason_status)
46
+ { "suppressed_by" => "config_disabled", "reason" => reason,
47
+ "reason_status" => reason_status, "directive" => nil,
48
+ "config" => { "path" => Runner.display_path(config_path), "line" => line } }
49
+ end
50
+
51
+ def rubocop_disabled?(path, offense)
52
+ return false unless rubocop_store
53
+
54
+ config = rubocop_config(path)
55
+ cop_name = offense.fetch("cop_name")
56
+ config.for_cop(cop_name)["Enabled"] == false && explicit_rubocop_disable?(config, cop_name)
57
+ end
58
+
59
+ def explicit_rubocop_disable?(config, cop_name)
60
+ return true unless config.for_all_cops["DisabledByDefault"]
61
+
62
+ raw_config(config.loaded_path).to_h.dig(cop_name, "Enabled") == false
63
+ end
64
+
65
+ def raw_config(path)
66
+ @raw_configs[path] ||= RuboCop::ConfigLoader.load_file(path, check: false)
67
+ end
68
+
69
+ def rubocop_config(path)
70
+ rubocop_store.for_file(File.expand_path(path))
71
+ end
72
+
73
+ def disabling_config(path, offense)
74
+ store.disabling_config(offense.fetch("cop_name"), File.expand_path(path))
75
+ end
76
+
77
+ def locator(config_path)
78
+ @locators[config_path] ||= ExcludeEntryLocator.new(config_path)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -7,38 +7,88 @@ require "metz_scan/commands/scan/exclude_entry_locator"
7
7
  module MetzScan
8
8
  module Commands
9
9
  class Scan
10
- # Default mode runs stock Metz config (--force-default-config) but must
11
- # still honor the project's per-cop file scope (Include/Exclude), the
12
- # same way #33 honors AllCops: Exclude. RuboCop's own excluded_file?
13
- # resolves the project config's scope per cop (#37); offenses on files a
14
- # cop is scoped off are hidden, and the project Exclude entry that hid
15
- # one is credited in the suppression ledger.
10
+ # Default mode runs stock Metz config except for project Include, so it
11
+ # must still honor the project's per-cop Exclude (#37); offenses on files
12
+ # a cop is scoped off are hidden, and the project Exclude entry that hid
13
+ # one is credited in the suppression ledger. Per-cop Include both narrows
14
+ # and widens in default mode, and RuboCop's excluded_file? resolves each
15
+ # cop's scope against the project config. Project analyzers have no cop
16
+ # class; only a project or inherited Exclude entry scopes them off.
16
17
  class ConfigExclusions
17
- def initialize(store)
18
+ def initialize(store, rubocop_store: nil)
18
19
  @store = store
20
+ @rubocop_store = rubocop_store
19
21
  @locators = {}
20
22
  end
21
23
 
22
24
  def scoped_off?(path, offense)
25
+ absolute = File.expand_path(path)
23
26
  cop_class = cop_class(offense)
24
- return false unless cop_class
27
+ return analyzer_scoped_off?(offense, absolute) unless cop_class
25
28
 
26
- absolute = File.expand_path(path)
27
- cop_class.new(store.for_file(absolute)).excluded_file?(absolute)
29
+ cop_scoped_off?(cop_class.new(store.for_file(absolute)), absolute)
28
30
  end
29
31
 
30
32
  def record_fields(path, offense)
31
- entry = store.applied_exclude(cop_class(offense).badge, File.expand_path(path))
32
- return unless entry
33
+ absolute = File.expand_path(path)
34
+ entry = applied_exclude(offense, absolute)
35
+ return local_record_fields(entry) if entry
33
36
 
34
- location = locator(entry.config_path).locate(entry.key, entry.pattern)
35
- { "suppressed_by" => "config_exclude", "reason" => location.reason,
36
- "reason_status" => location.reason_status, "directive" => nil, "config" => config(entry, location) }
37
+ remote_record_fields(offense, absolute)
37
38
  end
38
39
 
39
40
  private
40
41
 
41
- attr_reader :store
42
+ attr_reader :store, :rubocop_store
43
+
44
+ def analyzer_scoped_off?(offense, absolute)
45
+ !applied_exclude(offense, absolute).nil? || !rubocop_exclude(offense, absolute).nil?
46
+ end
47
+
48
+ def local_record_fields(entry)
49
+ location = locator(entry.config_path).locate(entry.key, entry.pattern)
50
+ exclude_record(location.reason, location.reason_status, config(entry, location))
51
+ end
52
+
53
+ def remote_record_fields(offense, absolute)
54
+ config_obj, pattern = rubocop_exclude(offense, absolute)
55
+ return unless pattern
56
+
57
+ exclude_record(nil, "unchecked", remote_config(config_obj, pattern))
58
+ end
59
+
60
+ def exclude_record(reason, reason_status, config_hash)
61
+ { "suppressed_by" => "config_exclude", "reason" => reason,
62
+ "reason_status" => reason_status, "directive" => nil, "config" => config_hash }
63
+ end
64
+
65
+ def remote_config(config_obj, pattern)
66
+ { "path" => Runner.display_path(config_obj.loaded_path), "line" => nil, "pattern" => display(pattern) }
67
+ end
68
+
69
+ def rubocop_exclude(offense, absolute)
70
+ return unless rubocop_store
71
+
72
+ config_obj = rubocop_store.for_file(absolute)
73
+ badge = RuboCop::Cop::Badge.parse(offense.fetch("cop_name"))
74
+ pattern = matching_exclude(config_obj, badge, absolute)
75
+ [config_obj, pattern] if pattern
76
+ end
77
+
78
+ def matching_exclude(config_obj, badge, absolute)
79
+ relative = config_obj.path_relative_to_config(absolute)
80
+ Array(config_obj.for_badge(badge)["Exclude"]).find do |pattern|
81
+ RuboCop::PathUtil.match_path?(pattern, relative) || RuboCop::PathUtil.match_path?(pattern, absolute)
82
+ end
83
+ end
84
+
85
+ def cop_scoped_off?(cop, file)
86
+ cop.excluded_file?(file)
87
+ end
88
+
89
+ def applied_exclude(offense, file)
90
+ store.applied_exclude(RuboCop::Cop::Badge.parse(offense.fetch("cop_name")), file)
91
+ end
42
92
 
43
93
  def cop_class(offense)
44
94
  RuboCop::Cop::Registry.global.find_by_cop_name(offense.fetch("cop_name"))
@@ -57,20 +107,6 @@ module MetzScan
57
107
  @locators[config_path] ||= ExcludeEntryLocator.new(config_path)
58
108
  end
59
109
  end
60
-
61
- # Invalid project config leaves no project scope to honor, matching the
62
- # forced-default target-discovery fallback.
63
- module NoConfigExclusions
64
- module_function
65
-
66
- def scoped_off?(_path, _offense)
67
- false
68
- end
69
-
70
- def record_fields(_path, _offense)
71
- nil
72
- end
73
- end
74
110
  end
75
111
  end
76
112
  end
@@ -37,6 +37,11 @@ module MetzScan
37
37
  end.first
38
38
  end
39
39
 
40
+ def any_for?(key)
41
+ department = key.rpartition("/").first
42
+ @entries.each_key.any? { |(entry_key, _)| entry_key == key || entry_key == department }
43
+ end
44
+
40
45
  private
41
46
 
42
47
  def index(key, raw, absolute, sources)
@@ -6,8 +6,8 @@ require "psych"
6
6
  module MetzScan
7
7
  module Commands
8
8
  class Scan
9
- # Finds where a config file writes one Exclude entry and the reason
10
- # comment documenting it. ERB configs are read as RuboCop renders them;
9
+ # Finds where a config file writes one Exclude entry (or a cop's
10
+ # `Enabled` key) and the reason comment documenting it. ERB configs are read as RuboCop renders them;
11
11
  # their rendered line numbers do not match the file, so no line is given.
12
12
  class ExcludeEntryLocator
13
13
  Location = Struct.new(:line, :reason, :reason_status, keyword_init: true)
@@ -27,13 +27,23 @@ module MetzScan
27
27
  exclude_key, entry = entry_nodes(key, pattern)
28
28
  return UNCHECKED unless entry
29
29
 
30
- reason = entry_reason(entry) || key_reason(exclude_key)
31
- Location.new(line: (entry.start_line + 1 unless @erb), reason: reason,
32
- reason_status: reason ? "present" : "missing")
30
+ location(entry, entry_reason(entry) || key_reason(exclude_key))
31
+ end
32
+
33
+ def locate_enabled(key)
34
+ enabled_key, = mapping_pair(mapping_pair(@root, key)&.last, "Enabled")
35
+ return UNCHECKED unless enabled_key
36
+
37
+ location(enabled_key, key_reason(enabled_key))
33
38
  end
34
39
 
35
40
  private
36
41
 
42
+ def location(node, reason)
43
+ Location.new(line: (node.start_line + 1 unless @erb), reason: reason,
44
+ reason_status: reason ? "present" : "missing")
45
+ end
46
+
37
47
  def entry_nodes(key, pattern)
38
48
  exclude_key, excludes = mapping_pair(mapping_pair(@root, key)&.last, "Exclude")
39
49
  return unless excludes.is_a?(Psych::Nodes::Sequence)
@@ -11,7 +11,9 @@ module MetzScan
11
11
  # Remembers which config file wrote each Exclude entry the scope-only
12
12
  # loader reads, so the suppression ledger can credit the entry a cop
13
13
  # actually applies. Raw YAML entries are tracked by object identity:
14
- # two files writing the same pattern stay distinct.
14
+ # two files writing the same pattern stay distinct. A cop's or
15
+ # department's `Enabled` is stamped with its file instead (see
16
+ # ENABLED_SOURCE).
15
17
  class ExcludeProvenance
16
18
  def initialize
17
19
  @sources = {}.compare_by_identity
@@ -38,7 +40,10 @@ module MetzScan
38
40
 
39
41
  def record_sources(raw_hash, absolute_path)
40
42
  raw_hash.each_value do |value|
41
- Array(value["Exclude"]).each { |entry| @sources[entry] = absolute_path } if value.is_a?(Hash)
43
+ next unless value.is_a?(Hash)
44
+
45
+ Array(value["Exclude"]).each { |entry| @sources[entry] = absolute_path }
46
+ value[ENABLED_SOURCE] = absolute_path if value.key?("Enabled")
42
47
  end
43
48
  end
44
49
  end