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.
- checksums.yaml +4 -4
- data/lib/metz_scan/analyzers/package_map.rb +1 -1
- data/lib/metz_scan/analyzers/test_calls_private_method/receiver_resolution.rb +3 -3
- data/lib/metz_scan/analyzers/test_calls_private_method/scope_resolution.rb +1 -1
- data/lib/metz_scan/commands/report.rb +16 -3
- data/lib/metz_scan/commands/scan/auto_fix.rb +49 -51
- data/lib/metz_scan/commands/scan/check_failure.rb +42 -0
- data/lib/metz_scan/commands/scan/config_disables.rb +83 -0
- data/lib/metz_scan/commands/scan/config_exclusions.rb +66 -30
- data/lib/metz_scan/commands/scan/exclude_entries.rb +5 -0
- data/lib/metz_scan/commands/scan/exclude_entry_locator.rb +15 -5
- data/lib/metz_scan/commands/scan/exclude_provenance.rb +7 -2
- data/lib/metz_scan/commands/scan/execution_config_store.rb +142 -0
- data/lib/metz_scan/commands/scan/ignored_cli_options_note.rb +35 -0
- data/lib/metz_scan/commands/scan/inherited_include_rewriter.rb +43 -0
- data/lib/metz_scan/commands/scan/inline_directive_locator.rb +38 -11
- data/lib/metz_scan/commands/scan/json_guidance.rb +81 -0
- data/lib/metz_scan/commands/scan/offense_extractor.rb +29 -7
- data/lib/metz_scan/commands/scan/path_scoped_include_note.rb +73 -0
- data/lib/metz_scan/commands/scan/project_analyzer_offenses.rb +21 -5
- data/lib/metz_scan/commands/scan/project_analyzer_runner.rb +21 -23
- data/lib/metz_scan/commands/scan/project_config_scope.rb +85 -28
- data/lib/metz_scan/commands/scan/rubocop_failure.rb +38 -0
- data/lib/metz_scan/commands/scan/rubocop_invocation.rb +141 -0
- data/lib/metz_scan/commands/scan/runner.rb +103 -49
- data/lib/metz_scan/commands/scan/sarif_rule_descriptors.rb +6 -24
- data/lib/metz_scan/commands/scan/suppression_ledger.rb +51 -14
- data/lib/metz_scan/commands/scan/suppression_ledger_formatter.rb +7 -1
- data/lib/metz_scan/commands/scan/target_ruby_version.rb +3 -2
- data/lib/metz_scan/commands/scan.rb +43 -21
- data/lib/metz_scan/project_index/rubydex_backend/method_visibility.rb +3 -0
- data/lib/metz_scan/version.rb +1 -1
- data/metz-scan.gemspec +9 -2
- metadata +16 -15
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/artifact_writer.rb +0 -51
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/baseline_delta.rb +0 -231
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/collaborators.rb +0 -76
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/markdown_renderer.rb +0 -353
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/readiness_catalog.rb +0 -127
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/summary.rb +0 -333
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/target_set.rb +0 -139
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner.rb +0 -64
- data/lib/metz_scan/calibration/rubydex_drift_formatter.rb +0 -91
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
|
|
5
|
+
require "metz_scan/commands/scan/project_config_scope"
|
|
6
|
+
|
|
7
|
+
module MetzScan
|
|
8
|
+
module Commands
|
|
9
|
+
class Scan
|
|
10
|
+
# Execution-layer ConfigStore for default mode. Keeps AllCops scope and
|
|
11
|
+
# per-cop Include/inherit_mode from the project config. Default `scan`
|
|
12
|
+
# strips project Enabled and per-cop Exclude so RuboCop runs with stock
|
|
13
|
+
# Metz defaults and SuppressionLedger can record project suppressions;
|
|
14
|
+
# `honor_suppressions: true` (--auto-fix) keeps per-cop Exclude and
|
|
15
|
+
# Enabled: false while never enabling opt-in cops.
|
|
16
|
+
class ExecutionConfigStore
|
|
17
|
+
COP_EXECUTION_KEYS = %w[Include Includes inherit_mode].freeze
|
|
18
|
+
TOP_LEVEL_KEYS = %w[AllCops inherit_mode].freeze
|
|
19
|
+
|
|
20
|
+
def initialize(scope_store = ProjectConfigScope.store, honor_suppressions: false)
|
|
21
|
+
@scope_store = scope_store
|
|
22
|
+
@honor_suppressions = honor_suppressions
|
|
23
|
+
@cache = {}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def for_file(file) = for_dir(File.dirname(file))
|
|
27
|
+
|
|
28
|
+
def for_pwd = for_dir(RuboCop::PathUtil.pwd)
|
|
29
|
+
|
|
30
|
+
def for(file_or_dir)
|
|
31
|
+
for_dir(File.directory?(file_or_dir) ? file_or_dir : File.dirname(file_or_dir))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def for_dir(dir)
|
|
35
|
+
scope_config = scope_store.for_dir(dir)
|
|
36
|
+
@cache[scope_config.loaded_path] ||= build_config(scope_config)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
attr_reader :scope_store
|
|
42
|
+
|
|
43
|
+
def build_config(scope_config)
|
|
44
|
+
path = scope_config.loaded_path
|
|
45
|
+
return ProjectConfigScope.ensure_metz_defaults_loaded if path == ProjectConfigScope::DEFAULT_FILE
|
|
46
|
+
|
|
47
|
+
defaults = ProjectConfigScope.ensure_metz_defaults_loaded
|
|
48
|
+
merged_config(execution_hash(scope_config, defaults), path)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def merged_config(hash, path)
|
|
52
|
+
config = RuboCop::Config.new(hash, path)
|
|
53
|
+
config.deprecation_check { |_message| nil }
|
|
54
|
+
config.make_excludes_absolute
|
|
55
|
+
RuboCop::ConfigLoader.merge_with_default(config, path)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def execution_hash(scope_config, defaults)
|
|
59
|
+
hash = top_level_entries(scope_config)
|
|
60
|
+
scope_config.each_key { |key| assign_cop_entry(hash, key, scope_config, defaults) }
|
|
61
|
+
hash
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def cop_hash?(key, value) = !top_level_key?(key) && value.is_a?(Hash)
|
|
65
|
+
|
|
66
|
+
def assign_cop_entry(hash, key, scope_config, defaults)
|
|
67
|
+
value = scope_config[key]
|
|
68
|
+
return unless cop_hash?(key, value)
|
|
69
|
+
|
|
70
|
+
entry = cop_entry(scope_config, defaults, key, value)
|
|
71
|
+
hash[key] = entry unless entry.empty?
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def top_level_entries(scope_config)
|
|
75
|
+
all_cops = slice_keys(scope_config["AllCops"], ProjectConfigScope::ALL_COPS_PROJECT_KEYS)
|
|
76
|
+
{}.tap do |hash|
|
|
77
|
+
hash["inherit_mode"] = copy_value(scope_config["inherit_mode"]) if scope_config["inherit_mode"].is_a?(Hash)
|
|
78
|
+
hash["AllCops"] = all_cops unless all_cops.empty?
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def top_level_key?(key) = TOP_LEVEL_KEYS.include?(key)
|
|
83
|
+
|
|
84
|
+
def cop_entry(scope_config, defaults, key, value)
|
|
85
|
+
entry = slice_keys(value, COP_EXECUTION_KEYS)
|
|
86
|
+
return apply_suppressions(scope_config, defaults, key, entry) if @honor_suppressions
|
|
87
|
+
return entry unless clear_stock_exclude?(scope_config, defaults, key, value)
|
|
88
|
+
|
|
89
|
+
entry.merge("Exclude" => [], "inherit_mode" => override_exclude_mode(entry["inherit_mode"]))
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def apply_suppressions(scope_config, defaults, key, entry)
|
|
93
|
+
value = scope_config[key]
|
|
94
|
+
entry = entry.merge("Exclude" => copy_value(value["Exclude"])) if value.key?("Exclude")
|
|
95
|
+
enabled = suppressed_enabled(scope_config, defaults, key, value)
|
|
96
|
+
enabled.nil? ? entry : entry.merge("Enabled" => enabled)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def suppressed_enabled(scope_config, defaults, key, value)
|
|
100
|
+
return false if value["Enabled"] == false || scope_config.for_cop(key)["Enabled"] == false
|
|
101
|
+
|
|
102
|
+
true if reenabled_default_cop?(scope_config, defaults, key, value)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def reenabled_default_cop?(scope_config, defaults, key, value)
|
|
106
|
+
defaults.dig(key, "Enabled") == true &&
|
|
107
|
+
value[ProjectConfigScope::ENABLED_SOURCE] &&
|
|
108
|
+
scope_config.for_cop(key)["Enabled"] == true
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def clear_stock_exclude?(scope_config, defaults, key, value)
|
|
112
|
+
stock_exclude_dropped?(defaults, key, value) || scope_store.project_exclude?(scope_config, key)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def slice_keys(hash, keys)
|
|
116
|
+
return {} unless hash.is_a?(Hash)
|
|
117
|
+
|
|
118
|
+
hash.slice(*keys).transform_values { |item| copy_value(item) }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def stock_exclude_dropped?(defaults, key, value)
|
|
122
|
+
stock = Array(defaults.dig(key, "Exclude"))
|
|
123
|
+
stock.any? && !(stock - Array(value["Exclude"])).empty?
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def override_exclude_mode(mode)
|
|
127
|
+
updated = mode ? copy_value(mode) : {}
|
|
128
|
+
updated["merge"] = Array(updated["merge"]) - ["Exclude"] if updated.key?("merge")
|
|
129
|
+
updated["override"] = Array(updated["override"]) | ["Exclude"]
|
|
130
|
+
updated
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def copy_value(value)
|
|
134
|
+
return value.map { |item| copy_value(item) } if value.is_a?(Array)
|
|
135
|
+
return value.transform_values { |item| copy_value(item) } if value.is_a?(Hash)
|
|
136
|
+
|
|
137
|
+
value
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetzScan
|
|
4
|
+
module Commands
|
|
5
|
+
class Scan
|
|
6
|
+
# Emits a one-line stderr note when a non-empty `.rubocop` file or
|
|
7
|
+
# `RUBOCOP_OPTS` environment variable is present, since `metz-scan`
|
|
8
|
+
# invokes RuboCop::Runner directly and ignores RuboCop CLI option files.
|
|
9
|
+
module IgnoredCliOptionsNote
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def print_note(stderr)
|
|
13
|
+
sources = active_sources
|
|
14
|
+
stderr.puts(note(sources)) unless sources.empty?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def active_sources
|
|
18
|
+
sources = []
|
|
19
|
+
sources << ".rubocop" if dot_rubocop_active?
|
|
20
|
+
sources << "RUBOCOP_OPTS" unless ENV["RUBOCOP_OPTS"].to_s.strip.empty?
|
|
21
|
+
sources
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def dot_rubocop_active?
|
|
25
|
+
File.file?(".rubocop") && !File.binread(".rubocop").strip.empty?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def note(sources)
|
|
29
|
+
"metz-scan: note: ignoring RuboCop CLI options from #{sources.join(' and ')}; " \
|
|
30
|
+
"configure settings in .rubocop.yml instead"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
|
|
5
|
+
module MetzScan
|
|
6
|
+
module Commands
|
|
7
|
+
class Scan
|
|
8
|
+
module ProjectConfigScope
|
|
9
|
+
# Mirrors RuboCop::ConfigLoaderResolver#fix_include_paths for
|
|
10
|
+
# scope-only config inheritance across `.rubocop*` files.
|
|
11
|
+
module InheritedIncludeRewriter
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def rewrite(hash, from_path, to_path)
|
|
15
|
+
return hash unless File.basename(from_path).start_with?(".rubocop")
|
|
16
|
+
return hash if File.dirname(from_path) == File.dirname(to_path)
|
|
17
|
+
|
|
18
|
+
base_dir = File.dirname(from_path)
|
|
19
|
+
derived_dir = File.dirname(to_path)
|
|
20
|
+
hash.transform_values { |value| rewrite_value(value, base_dir, derived_dir) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def rewrite_value(value, base_dir, derived_dir)
|
|
24
|
+
return value unless value.is_a?(Hash) && value["Include"]
|
|
25
|
+
|
|
26
|
+
rewritten = Array(value["Include"]).map { |pattern| rewrite_pattern(pattern, base_dir, derived_dir) }
|
|
27
|
+
value.merge("Include" => rewritten)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def rewrite_pattern(pattern, base_dir, derived_dir)
|
|
31
|
+
return pattern unless relative_anchored?(pattern)
|
|
32
|
+
|
|
33
|
+
RuboCop::PathUtil.relative_path(File.join(base_dir, pattern), derived_dir)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def relative_anchored?(pattern)
|
|
37
|
+
pattern.is_a?(String) && !Pathname.new(pattern).absolute? && !pattern.start_with?("**/")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -9,11 +9,27 @@ module MetzScan
|
|
|
9
9
|
# covering it. Ranges RuboCop opens at -Infinity for opt-in cops are
|
|
10
10
|
# synthetic (CommentConfig::ConfigDisabledCopDirectiveComment), not a
|
|
11
11
|
# comment anyone wrote, so they credit nothing.
|
|
12
|
+
#
|
|
13
|
+
# Project analyzers are not RuboCop cops, so `# rubocop:disable` does not
|
|
14
|
+
# reach them; their directive is `# metz-scan:disable`. Their ranges come
|
|
15
|
+
# from RuboCop's own directive parser run over a copy of the source in
|
|
16
|
+
# which only metz-scan markers read as rubocop ones; of RuboCop's
|
|
17
|
+
# directive modes, only `disable` counts for them.
|
|
12
18
|
class InlineDirectiveLocator
|
|
19
|
+
ANALYZER_PREFIX = "MetzProject/"
|
|
20
|
+
MARKER = /#\s*\K(?:rubocop|metz-scan)(?=\s*:)/
|
|
21
|
+
ANALYZER_MARKERS = { "rubocop" => "rubocop-ignored", "metz-scan" => "rubocop" }.freeze
|
|
22
|
+
|
|
13
23
|
def initialize
|
|
14
24
|
@ranges = {}
|
|
15
25
|
end
|
|
16
26
|
|
|
27
|
+
# Index-backed findings can point at a constant with no source file
|
|
28
|
+
# (an external root), which holds no directive.
|
|
29
|
+
def analyzer_disabled?(path, offense)
|
|
30
|
+
analyzer?(offense) && File.file?(path) && !covering_directive(path, offense).nil?
|
|
31
|
+
end
|
|
32
|
+
|
|
17
33
|
def record_fields(path, offense)
|
|
18
34
|
directive = covering_directive(path, offense)
|
|
19
35
|
return unless directive
|
|
@@ -26,26 +42,37 @@ module MetzScan
|
|
|
26
42
|
private
|
|
27
43
|
|
|
28
44
|
def covering_directive(path, offense)
|
|
29
|
-
|
|
30
|
-
ranges_for(path).fetch(offense.fetch("cop_name"), [])
|
|
31
|
-
|
|
32
|
-
|
|
45
|
+
analyzer = analyzer?(offense)
|
|
46
|
+
ranges_for(path, analyzer).fetch(offense.fetch("cop_name"), [])
|
|
47
|
+
.select { |range| covers?(range, offense.fetch("location"), analyzer) }
|
|
48
|
+
.max_by(&:begin)&.directive
|
|
33
49
|
end
|
|
34
50
|
|
|
35
|
-
def covers?(range, location)
|
|
51
|
+
def covers?(range, location, analyzer)
|
|
36
52
|
range.is_a?(RuboCop::CommentConfig::DirectiveRange) && range.begin.finite? &&
|
|
53
|
+
(!analyzer || range.directive.mode == "disable") &&
|
|
37
54
|
range.end >= location.fetch("start_line") && range.begin <= location.fetch("last_line")
|
|
38
55
|
end
|
|
39
56
|
|
|
40
|
-
def
|
|
41
|
-
|
|
57
|
+
def analyzer?(offense)
|
|
58
|
+
offense.fetch("cop_name").start_with?(ANALYZER_PREFIX)
|
|
42
59
|
end
|
|
43
60
|
|
|
44
|
-
def
|
|
61
|
+
def ranges_for(path, analyzer)
|
|
62
|
+
@ranges[[path, analyzer]] ||= processed_source(path, analyzer).comment_config.cop_disabled_line_ranges
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def processed_source(path, analyzer)
|
|
45
66
|
config = RuboCop::ConfigLoader.default_configuration
|
|
46
|
-
RuboCop::ProcessedSource.
|
|
47
|
-
|
|
48
|
-
.tap { |
|
|
67
|
+
RuboCop::ProcessedSource.new(source(path, analyzer), config.target_ruby_version, path,
|
|
68
|
+
parser_engine: config.parser_engine)
|
|
69
|
+
.tap { |processed| processed.config = config }
|
|
70
|
+
.tap { |processed| processed.registry = RuboCop::Cop::Registry.global }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def source(path, analyzer)
|
|
74
|
+
text = File.read(path, mode: "rb")
|
|
75
|
+
analyzer ? text.gsub(MARKER, ANALYZER_MARKERS) : text
|
|
49
76
|
end
|
|
50
77
|
end
|
|
51
78
|
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
require_relative "project_analyzer_runner"
|
|
5
|
+
|
|
6
|
+
module MetzScan
|
|
7
|
+
module Commands
|
|
8
|
+
class Scan
|
|
9
|
+
# Moves each reported cop's default guidance into a top-level `guidance`
|
|
10
|
+
# map keyed by cop_name; offenses keep only the fields that differ.
|
|
11
|
+
# Under --all-cops, stock RuboCop cops get guidance from the resolved
|
|
12
|
+
# config of the first file reporting them: Description plus autocorrect
|
|
13
|
+
# safety. The default scan never loads the project config for this (see
|
|
14
|
+
# docs/ddrs/2026-07-08-rubocop-scope-only-config.md), so stock cops such
|
|
15
|
+
# as Lint/Syntax keep empty guidance there.
|
|
16
|
+
module JsonGuidance
|
|
17
|
+
FIELDS = %w[why_it_matters suggested_next_moves fix_safety].freeze
|
|
18
|
+
NO_GUIDANCE = { "why_it_matters" => "", "suggested_next_moves" => [], "fix_safety" => "" }.freeze
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def compact!(parsed, all_cops:)
|
|
23
|
+
store = RuboCop::ConfigStore.new if all_cops
|
|
24
|
+
guidance = reported_cops(parsed).to_h { |name, path| [name, defaults_for(name, store, path)] }
|
|
25
|
+
parsed["files"].flat_map { |file| file["offenses"] }
|
|
26
|
+
.each { |offense| compact_offense(offense, guidance.fetch(offense["cop_name"])) }
|
|
27
|
+
parsed.merge!("guidance" => guidance)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def reported_cops(parsed)
|
|
31
|
+
parsed["files"].flat_map { |file| file["offenses"].map { |offense| [offense["cop_name"], file["path"]] } }
|
|
32
|
+
.uniq(&:first)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def compact_offense(offense, defaults)
|
|
36
|
+
FIELDS.each { |field| offense.delete(field) if offense[field] == defaults.fetch(field) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def defaults_for(cop_name, store, path)
|
|
40
|
+
analyzer = ProjectAnalyzerRunner::ANALYZERS.find { |candidate| cop_name == candidate::RULE_ID }
|
|
41
|
+
return analyzer_defaults(analyzer) if analyzer
|
|
42
|
+
|
|
43
|
+
cop_defaults(RuboCop::Cop::Registry.global.find_by_cop_name(cop_name), store, path)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def analyzer_defaults(analyzer)
|
|
47
|
+
{ "why_it_matters" => analyzer::WHY, "suggested_next_moves" => analyzer::SUGGESTED_NEXT_MOVES,
|
|
48
|
+
"fix_safety" => ProjectAnalyzerOffenses::FIX_SAFETY }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def cop_defaults(cop, store, path)
|
|
52
|
+
return metz_defaults(cop) if cop.respond_to?(:why_it_matters)
|
|
53
|
+
return NO_GUIDANCE unless cop && store
|
|
54
|
+
|
|
55
|
+
stock_defaults(cop, store.for_file(path).for_cop(cop))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def metz_defaults(cop)
|
|
59
|
+
{ "why_it_matters" => cop.why_it_matters, "suggested_next_moves" => Array(cop.suggested_next_moves),
|
|
60
|
+
"fix_safety" => cop.fix_safety.to_s }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def stock_defaults(cop, config)
|
|
64
|
+
{ "why_it_matters" => config["Description"].to_s, "suggested_next_moves" => [],
|
|
65
|
+
"fix_safety" => stock_fix_safety(cop, config) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def stock_fix_safety(cop, config)
|
|
69
|
+
return "manual" unless cop.support_autocorrect? && autocorrect_enabled?(config)
|
|
70
|
+
|
|
71
|
+
config.fetch("Safe", true) && config.fetch("SafeAutoCorrect", true) ? "safe" : "unsafe"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Mirrors RuboCop's Cop::Base#autocorrect_enabled?.
|
|
75
|
+
def autocorrect_enabled?(config)
|
|
76
|
+
!["disabled", false].include?(config["AutoCorrect"])
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -7,24 +7,46 @@ module MetzScan
|
|
|
7
7
|
module_function
|
|
8
8
|
|
|
9
9
|
def offenses(parsed)
|
|
10
|
-
|
|
10
|
+
guidance = parsed["guidance"] || {}
|
|
11
|
+
Array(parsed["files"]).flat_map { |file| file_offenses(file, guidance) }
|
|
11
12
|
end
|
|
12
13
|
|
|
13
|
-
def file_offenses(file)
|
|
14
|
-
Array(file["offenses"]).map { |o| offense_struct(file["path"], o) }
|
|
14
|
+
def file_offenses(file, guidance)
|
|
15
|
+
Array(file["offenses"]).map { |o| offense_struct(file["path"], o, guidance) }
|
|
15
16
|
end
|
|
16
17
|
|
|
17
|
-
def offense_struct(path, offense)
|
|
18
|
+
def offense_struct(path, offense, guidance)
|
|
18
19
|
loc = offense["location"] || {}
|
|
19
|
-
base_offense(path, offense).merge(location_fields(loc))
|
|
20
|
+
base_offense(path, offense, guidance).merge(location_fields(loc))
|
|
20
21
|
end
|
|
21
22
|
|
|
22
|
-
def base_offense(path, offense)
|
|
23
|
+
def base_offense(path, offense, guidance)
|
|
23
24
|
{ path: path, cop_name: offense["cop_name"], severity: offense["severity"],
|
|
24
|
-
message: offense["message"], why_it_matters: offense
|
|
25
|
+
message: offense["message"], why_it_matters: why_it_matters(offense, guidance),
|
|
25
26
|
project_analyzer: offense["project_analyzer"] }
|
|
26
27
|
end
|
|
27
28
|
|
|
29
|
+
# Saved scan JSON keeps per-cop defaults in `guidance`; raw RuboCop JSON falls back to the registry.
|
|
30
|
+
def why_it_matters(offense, guidance)
|
|
31
|
+
return offense["why_it_matters"] if offense.key?("why_it_matters")
|
|
32
|
+
|
|
33
|
+
guidance_why_it_matters(offense["cop_name"], guidance)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def guidance_why_it_matters(cop_name, guidance)
|
|
37
|
+
entry = guidance[cop_name]
|
|
38
|
+
return entry["why_it_matters"] if entry.is_a?(Hash) && entry.key?("why_it_matters")
|
|
39
|
+
|
|
40
|
+
registry_why_it_matters(cop_name)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def registry_why_it_matters(cop_name)
|
|
44
|
+
return unless defined?(RuboCop::Cop::Registry)
|
|
45
|
+
|
|
46
|
+
cop = RuboCop::Cop::Registry.global.find_by_cop_name(cop_name)
|
|
47
|
+
cop.why_it_matters if cop.respond_to?(:why_it_matters)
|
|
48
|
+
end
|
|
49
|
+
|
|
28
50
|
def location_fields(location)
|
|
29
51
|
{ line: location_value(location, "start_line", "line"),
|
|
30
52
|
column: location_value(location, "start_column", "column") }
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "metz_scan/commands/scan/execution_config_store"
|
|
4
|
+
|
|
5
|
+
module MetzScan
|
|
6
|
+
module Commands
|
|
7
|
+
class Scan
|
|
8
|
+
# Default mode resolves anchored cop Include globs (app/operations/**/*.rb)
|
|
9
|
+
# against the governing .rubocop.yml directory (or the current directory
|
|
10
|
+
# when none exists). A file those globs match only from a deeper project
|
|
11
|
+
# root silently loses those cops; this names that once per scan.
|
|
12
|
+
module PathScopedIncludeNote
|
|
13
|
+
FNMATCH_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def print_note(files, stderr, exec_store: nil)
|
|
18
|
+
store = exec_store || ExecutionConfigStore.new
|
|
19
|
+
misses = files.filter_map { |file| miss_for(file, store) }
|
|
20
|
+
stderr.puts(note(misses)) unless misses.empty?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Nested app trees (engines, spec/dummy) land here too, so the remedy
|
|
24
|
+
# names each root without telling the reader to rescan only that root.
|
|
25
|
+
def note(misses)
|
|
26
|
+
cop_name, pattern, = misses.first
|
|
27
|
+
roots = misses.map(&:last).uniq
|
|
28
|
+
"metz-scan: note: path-scoped cops did not check #{file_count(misses.size)} under #{roots.join(', ')} " \
|
|
29
|
+
"(#{cop_name} matches #{pattern} relative to the current directory); " \
|
|
30
|
+
"scan #{roots.one? ? roots.first : 'each root'} from inside it to check them"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def miss_for(file, store)
|
|
34
|
+
config = store.for_file(File.expand_path(file))
|
|
35
|
+
target = relative_target(file, config)
|
|
36
|
+
anchored_includes(config).lazy.filter_map { |cop, pat| miss_entry(cop, pat, target) }.first
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def miss_entry(cop_name, pattern, target)
|
|
40
|
+
root = project_root(target, pattern)
|
|
41
|
+
[cop_name, pattern, root] if root
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def relative_target(file, config)
|
|
45
|
+
rel = config.path_relative_to_config(File.expand_path(file))
|
|
46
|
+
rel.start_with?("..") ? file : rel
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The leading directories whose removal lets `pattern` match `file`;
|
|
50
|
+
# nil when `pattern` already matches `file` or matches no suffix of it.
|
|
51
|
+
def project_root(file, pattern)
|
|
52
|
+
parts = file.split("/")
|
|
53
|
+
index = (0...parts.size).find { |i| File.fnmatch?(pattern, parts.drop(i).join("/"), FNMATCH_FLAGS) }
|
|
54
|
+
parts.take(index).join("/") if index&.positive?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def anchored_includes(config)
|
|
58
|
+
config.to_h.flat_map do |cop_name, cop_config|
|
|
59
|
+
next [] unless cop_name.start_with?("Metz/") && cop_config.is_a?(Hash)
|
|
60
|
+
|
|
61
|
+
Array(cop_config["Include"]).select { |pattern| anchored_pattern?(pattern) }.map { |pat| [cop_name, pat] }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def anchored_pattern?(pattern)
|
|
66
|
+
pattern.is_a?(String) && !pattern.start_with?("**/") && !Pathname.new(pattern).absolute?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def file_count(count) = count == 1 ? "1 file" : "#{count} files"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -1,18 +1,34 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "project_analyzer_metadata"
|
|
4
|
+
require_relative "suppression_ledger"
|
|
4
5
|
|
|
5
6
|
module MetzScan
|
|
6
7
|
module Commands
|
|
7
8
|
class Scan
|
|
8
9
|
module ProjectAnalyzerOffenses
|
|
9
|
-
OffenseSet = Struct.new(:by_path, :offenses, keyword_init: true)
|
|
10
|
+
OffenseSet = Struct.new(:findings, :by_path, :offenses, keyword_init: true)
|
|
11
|
+
FIX_SAFETY = "manual"
|
|
10
12
|
|
|
11
13
|
module_function
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
# With a `ledger_report` (scan), the project's MetzProject/* config
|
|
16
|
+
# and inline directives hide offenses, crediting them in that report's
|
|
17
|
+
# suppression ledger; findings left with no live offense drop out.
|
|
18
|
+
def build(findings, ledger_report: nil, **scope)
|
|
14
19
|
entries = entries_for(findings)
|
|
15
|
-
|
|
20
|
+
return live_set(entries, ledger_report, **scope) if ledger_report
|
|
21
|
+
|
|
22
|
+
OffenseSet.new(findings: findings, by_path: offenses_by_path(entries),
|
|
23
|
+
offenses: entries.map { |entry| entry.fetch(:offense) })
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def live_set(entries, ledger_report, **scope)
|
|
27
|
+
by_path = SuppressionLedger.partition!(ledger_report, offenses_by_path(entries), **scope)
|
|
28
|
+
live = by_path.values.flatten
|
|
29
|
+
kept = Set.new.compare_by_identity.merge(live)
|
|
30
|
+
findings = entries.select { |entry| kept.include?(entry.fetch(:offense)) }.map { |entry| entry[:finding] }
|
|
31
|
+
OffenseSet.new(findings: findings.uniq(&:object_id), by_path: by_path, offenses: live)
|
|
16
32
|
end
|
|
17
33
|
|
|
18
34
|
def entries_for(findings)
|
|
@@ -21,7 +37,7 @@ module MetzScan
|
|
|
21
37
|
|
|
22
38
|
def offenses_for(finding)
|
|
23
39
|
locations_for(finding).map do |location|
|
|
24
|
-
{ path: location.path, offense: offense_for(finding, location) }
|
|
40
|
+
{ path: location.path, finding: finding, offense: offense_for(finding, location) }
|
|
25
41
|
end
|
|
26
42
|
end
|
|
27
43
|
|
|
@@ -47,7 +63,7 @@ module MetzScan
|
|
|
47
63
|
end
|
|
48
64
|
|
|
49
65
|
def explanation_metadata(finding)
|
|
50
|
-
{ "why_it_matters" => finding.why_it_matters, "fix_safety" =>
|
|
66
|
+
{ "why_it_matters" => finding.why_it_matters, "fix_safety" => FIX_SAFETY,
|
|
51
67
|
"suggested_next_moves" => suggested_next_moves_for(finding) }
|
|
52
68
|
end
|
|
53
69
|
|
|
@@ -21,15 +21,9 @@ module MetzScan
|
|
|
21
21
|
# same report shape produced by RuboCop's JSON formatter.
|
|
22
22
|
module ProjectAnalyzerRunner
|
|
23
23
|
ANALYZERS = [
|
|
24
|
-
Analyzers::RepeatedBranching,
|
|
25
|
-
Analyzers::
|
|
26
|
-
Analyzers::
|
|
27
|
-
Analyzers::PackageDependencyPressure,
|
|
28
|
-
Analyzers::NamespaceLeakPressure,
|
|
29
|
-
Analyzers::ImplicitContextPressure,
|
|
30
|
-
Analyzers::RepeatedQueryCriteria,
|
|
31
|
-
Analyzers::SubclassOverridePressure,
|
|
32
|
-
Analyzers::TestCallsPrivateMethod
|
|
24
|
+
Analyzers::RepeatedBranching, Analyzers::ServiceSoup, Analyzers::InheritanceDescendants,
|
|
25
|
+
Analyzers::PackageDependencyPressure, Analyzers::NamespaceLeakPressure, Analyzers::ImplicitContextPressure,
|
|
26
|
+
Analyzers::RepeatedQueryCriteria, Analyzers::SubclassOverridePressure, Analyzers::TestCallsPrivateMethod
|
|
33
27
|
].freeze
|
|
34
28
|
INDEX_BACKED_ANALYZERS = [Analyzers::InheritanceDescendants, Analyzers::PackageDependencyPressure,
|
|
35
29
|
Analyzers::NamespaceLeakPressure, Analyzers::SubclassOverridePressure,
|
|
@@ -40,26 +34,31 @@ module MetzScan
|
|
|
40
34
|
|
|
41
35
|
module_function
|
|
42
36
|
|
|
43
|
-
def merge!(parsed, paths, **)
|
|
44
|
-
|
|
37
|
+
def merge!(parsed, paths, **options)
|
|
38
|
+
scope = TargetFileDiscovery.analyzer_scope(options)
|
|
39
|
+
findings = project_findings_for(paths, **options, **scope)
|
|
45
40
|
return parsed if findings.empty?
|
|
46
41
|
|
|
47
|
-
|
|
42
|
+
merge_live_offenses(parsed, findings, scope)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def merge_live_offenses(parsed, findings, scope)
|
|
46
|
+
offense_set = Runner.with_errors { ProjectAnalyzerOffenses.build(findings, ledger_report: parsed, **scope) }
|
|
47
|
+
merge_offense_set(parsed, offense_set) unless offense_set.findings.empty?
|
|
48
48
|
parsed
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
def project_findings_for(paths, index: nil, default_output: false,
|
|
52
|
-
paths = analyzer_paths(paths, index: index,
|
|
51
|
+
def project_findings_for(paths, index: nil, default_output: false, **)
|
|
52
|
+
paths = analyzer_paths(paths, index: index, **)
|
|
53
53
|
return [] if paths.empty? && !index
|
|
54
54
|
|
|
55
55
|
findings = project_findings(paths, index: index, default_output: default_output)
|
|
56
56
|
default_output ? findings.select { |finding| default_output_finding?(finding) } : findings
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
-
def
|
|
60
|
-
offense_set = ProjectAnalyzerOffenses.build(findings)
|
|
59
|
+
def merge_offense_set(parsed, offense_set)
|
|
61
60
|
merge_offenses(parsed, offense_set.by_path)
|
|
62
|
-
update_summary(parsed, findings, offense_set.offenses)
|
|
61
|
+
update_summary(parsed, offense_set.findings, offense_set.offenses)
|
|
63
62
|
end
|
|
64
63
|
|
|
65
64
|
def project_findings(paths, index: nil, default_output: false)
|
|
@@ -94,16 +93,15 @@ module MetzScan
|
|
|
94
93
|
finding.triage_severity == DEFAULT_OUTPUT_TRIAGE_SEVERITY
|
|
95
94
|
end
|
|
96
95
|
|
|
97
|
-
def analyzer_paths(paths, index: nil,
|
|
96
|
+
def analyzer_paths(paths, index: nil, target_files: nil, **)
|
|
98
97
|
return Array(paths) if index
|
|
98
|
+
return target_files if target_files
|
|
99
99
|
|
|
100
|
-
rubocop_target_files(paths,
|
|
100
|
+
rubocop_target_files(paths, **)
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
-
def rubocop_target_files(paths,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
TargetFileDiscovery.for_project_config(paths)
|
|
103
|
+
def rubocop_target_files(paths, **)
|
|
104
|
+
Runner.with_errors { TargetFileDiscovery.for_analyzer(paths, **) }
|
|
107
105
|
end
|
|
108
106
|
|
|
109
107
|
def merge_offenses(parsed, grouped_offenses)
|