metz-scan 0.5.3 → 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/inheritance_descendants.rb +1 -1
- data/lib/metz_scan/analyzers/namespace_leak_pressure.rb +1 -1
- data/lib/metz_scan/analyzers/package_dependency_pressure.rb +1 -1
- data/lib/metz_scan/analyzers/package_map.rb +2 -2
- data/lib/metz_scan/analyzers/test_calls_private_method/call_site_collector.rb +79 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/finding.rb +23 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/metadata.rb +27 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/node_predicates.rb +57 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/receiver_resolution.rb +80 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/scope_facts.rb +76 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/scope_resolution.rb +56 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/triage.rb +23 -0
- data/lib/metz_scan/analyzers/test_calls_private_method.rb +125 -0
- data/lib/metz_scan/cli.rb +7 -13
- data/lib/metz_scan/commands/explain.rb +11 -2
- data/lib/metz_scan/commands/project_analyzers.rb +3 -2
- data/lib/metz_scan/commands/report.rb +22 -8
- data/lib/metz_scan/commands/rules.rb +16 -4
- data/lib/metz_scan/commands/scan/auto_fix.rb +54 -54
- 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 +112 -0
- data/lib/metz_scan/commands/scan/exclude_entries.rb +64 -0
- data/lib/metz_scan/commands/scan/exclude_entry_locator.rb +84 -0
- data/lib/metz_scan/commands/scan/exclude_provenance.rb +53 -0
- 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 +80 -0
- 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 +25 -24
- data/lib/metz_scan/commands/scan/project_config_scope.rb +105 -26
- 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 +94 -75
- data/lib/metz_scan/commands/scan/sarif_rule_descriptors.rb +6 -24
- data/lib/metz_scan/commands/scan/suppression_ledger.rb +113 -0
- data/lib/metz_scan/commands/scan/suppression_ledger_formatter.rb +54 -0
- data/lib/metz_scan/commands/scan/target_ruby_version.rb +3 -2
- data/lib/metz_scan/commands/scan/text_renderer.rb +5 -4
- data/lib/metz_scan/commands/scan.rb +48 -26
- data/lib/metz_scan/project_index/rubydex_backend/method_declarations.rb +33 -12
- data/lib/metz_scan/project_index/rubydex_backend/method_visibility.rb +155 -0
- data/lib/metz_scan/project_index/rubydex_backend.rb +2 -1
- data/lib/metz_scan/project_index.rb +1 -1
- data/lib/metz_scan/version.rb +1 -1
- data/metz-scan.gemspec +20 -12
- metadata +33 -14
- 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 -123
- 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,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "erb"
|
|
4
|
+
require "psych"
|
|
5
|
+
|
|
6
|
+
module MetzScan
|
|
7
|
+
module Commands
|
|
8
|
+
class Scan
|
|
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
|
+
# their rendered line numbers do not match the file, so no line is given.
|
|
12
|
+
class ExcludeEntryLocator
|
|
13
|
+
Location = Struct.new(:line, :reason, :reason_status, keyword_init: true)
|
|
14
|
+
UNCHECKED = Location.new(line: nil, reason: nil, reason_status: "unchecked").freeze
|
|
15
|
+
COMMENT_LINE = /\A\s*#/
|
|
16
|
+
TRAILING_COMMENT = /(?:\A|\s)#(.*)\z/
|
|
17
|
+
|
|
18
|
+
def initialize(config_path)
|
|
19
|
+
raw = File.read(config_path, encoding: Encoding::UTF_8)
|
|
20
|
+
rendered = Dir.chdir(File.dirname(config_path)) { ERB.new(raw).result }
|
|
21
|
+
@erb = rendered != raw
|
|
22
|
+
@lines = rendered.lines.map(&:chomp)
|
|
23
|
+
@root = Psych.parse(rendered).root
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def locate(key, pattern)
|
|
27
|
+
exclude_key, entry = entry_nodes(key, pattern)
|
|
28
|
+
return UNCHECKED unless entry
|
|
29
|
+
|
|
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))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
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
|
+
|
|
47
|
+
def entry_nodes(key, pattern)
|
|
48
|
+
exclude_key, excludes = mapping_pair(mapping_pair(@root, key)&.last, "Exclude")
|
|
49
|
+
return unless excludes.is_a?(Psych::Nodes::Sequence)
|
|
50
|
+
|
|
51
|
+
[exclude_key, excludes.children.find { |node| node.is_a?(Psych::Nodes::Scalar) && node.value == pattern }]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def mapping_pair(node, key)
|
|
55
|
+
return unless node.is_a?(Psych::Nodes::Mapping)
|
|
56
|
+
|
|
57
|
+
node.children.each_slice(2).find { |name, _value| name.is_a?(Psych::Nodes::Scalar) && name.value == key }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def entry_reason(entry)
|
|
61
|
+
trailing_comment(entry) || comment_block_above(entry.start_line)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def key_reason(exclude_key)
|
|
65
|
+
trailing_comment(exclude_key) || comment_block_above(exclude_key.start_line)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def trailing_comment(node)
|
|
69
|
+
comment_text(@lines.fetch(node.end_line)[node.end_column..][TRAILING_COMMENT, 1])
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def comment_block_above(line_index)
|
|
73
|
+
block = @lines.first(line_index).reverse.take_while { |line| line.match?(COMMENT_LINE) }
|
|
74
|
+
comment_text(block.reverse.map { |line| line.sub(COMMENT_LINE, "").strip }.join(" "))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def comment_text(text)
|
|
78
|
+
stripped = text.to_s.strip
|
|
79
|
+
stripped unless stripped.empty?
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
|
|
5
|
+
require "metz_scan/commands/scan/exclude_entries"
|
|
6
|
+
|
|
7
|
+
module MetzScan
|
|
8
|
+
module Commands
|
|
9
|
+
class Scan
|
|
10
|
+
module ProjectConfigScope
|
|
11
|
+
# Remembers which config file wrote each Exclude entry the scope-only
|
|
12
|
+
# loader reads, so the suppression ledger can credit the entry a cop
|
|
13
|
+
# actually applies. Raw YAML entries are tracked by object identity:
|
|
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).
|
|
17
|
+
class ExcludeProvenance
|
|
18
|
+
def initialize
|
|
19
|
+
@sources = {}.compare_by_identity
|
|
20
|
+
@entries = {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def load_yaml_configuration(absolute_path)
|
|
24
|
+
RuboCop::ConfigLoader.load_yaml_configuration(absolute_path).tap do |raw_hash|
|
|
25
|
+
record_sources(raw_hash, absolute_path)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def make_excludes_absolute(config)
|
|
30
|
+
raw_patterns = ExcludeEntries.patterns(config)
|
|
31
|
+
config.make_excludes_absolute
|
|
32
|
+
@entries[config.loaded_path] = ExcludeEntries.new(raw_patterns, ExcludeEntries.patterns(config), @sources)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def entries_for(config)
|
|
36
|
+
@entries.fetch(config.loaded_path) { ExcludeEntries.new }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def record_sources(raw_hash, absolute_path)
|
|
42
|
+
raw_hash.each_value do |value|
|
|
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")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -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
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
|
|
5
|
+
module MetzScan
|
|
6
|
+
module Commands
|
|
7
|
+
class Scan
|
|
8
|
+
# Credits a suppressed offense to the innermost real directive comment
|
|
9
|
+
# covering it. Ranges RuboCop opens at -Infinity for opt-in cops are
|
|
10
|
+
# synthetic (CommentConfig::ConfigDisabledCopDirectiveComment), not a
|
|
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.
|
|
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
|
+
|
|
23
|
+
def initialize
|
|
24
|
+
@ranges = {}
|
|
25
|
+
end
|
|
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
|
+
|
|
33
|
+
def record_fields(path, offense)
|
|
34
|
+
directive = covering_directive(path, offense)
|
|
35
|
+
return unless directive
|
|
36
|
+
|
|
37
|
+
reason = directive.reason
|
|
38
|
+
{ "suppressed_by" => "inline_disable", "reason" => reason, "reason_status" => reason ? "present" : "missing",
|
|
39
|
+
"directive" => { "line" => directive.line_number }, "config" => nil }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def covering_directive(path, offense)
|
|
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
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def covers?(range, location, analyzer)
|
|
52
|
+
range.is_a?(RuboCop::CommentConfig::DirectiveRange) && range.begin.finite? &&
|
|
53
|
+
(!analyzer || range.directive.mode == "disable") &&
|
|
54
|
+
range.end >= location.fetch("start_line") && range.begin <= location.fetch("last_line")
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def analyzer?(offense)
|
|
58
|
+
offense.fetch("cop_name").start_with?(ANALYZER_PREFIX)
|
|
59
|
+
end
|
|
60
|
+
|
|
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)
|
|
66
|
+
config = RuboCop::ConfigLoader.default_configuration
|
|
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
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
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
|