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
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "optparse"
|
|
4
4
|
require "yaml"
|
|
5
|
+
require_relative "../cli"
|
|
5
6
|
|
|
6
7
|
module MetzScan
|
|
7
8
|
module Commands
|
|
@@ -23,6 +24,8 @@ module MetzScan
|
|
|
23
24
|
return missing_argument_error if args.empty?
|
|
24
25
|
|
|
25
26
|
emit_or_error(args.first)
|
|
27
|
+
rescue OptionParser::ParseError => e
|
|
28
|
+
parser_error(e)
|
|
26
29
|
end
|
|
27
30
|
|
|
28
31
|
def emit_or_error(cop_name)
|
|
@@ -52,17 +55,23 @@ module MetzScan
|
|
|
52
55
|
cop.cop_name.start_with?("Metz/") && cop.respond_to?(:metz_metadata)
|
|
53
56
|
end
|
|
54
57
|
|
|
58
|
+
def parser_error(err)
|
|
59
|
+
stderr.puts "metz-scan explain: #{err.message}"
|
|
60
|
+
stderr.puts USAGE
|
|
61
|
+
CLI::USAGE_ERROR
|
|
62
|
+
end
|
|
63
|
+
|
|
55
64
|
def missing_argument_error
|
|
56
65
|
stderr.puts "metz-scan explain: missing required COP_NAME argument."
|
|
57
66
|
stderr.puts USAGE
|
|
58
67
|
stderr.puts "Run `metz-scan rules` to see the list of available Metz cops."
|
|
59
|
-
|
|
68
|
+
CLI::USAGE_ERROR
|
|
60
69
|
end
|
|
61
70
|
|
|
62
71
|
def unknown_cop_error(name)
|
|
63
72
|
stderr.puts "metz-scan explain: no such cop '#{name}'."
|
|
64
73
|
stderr.puts "Run `metz-scan rules` to see the list of available Metz cops."
|
|
65
|
-
|
|
74
|
+
CLI::USAGE_ERROR
|
|
66
75
|
end
|
|
67
76
|
|
|
68
77
|
def emit(cop)
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "optparse"
|
|
5
|
+
require_relative "../cli"
|
|
5
6
|
|
|
6
7
|
require_relative "scan/project_analyzer_runner"
|
|
7
8
|
|
|
@@ -21,7 +22,7 @@ module MetzScan
|
|
|
21
22
|
|
|
22
23
|
def run(argv)
|
|
23
24
|
options = parse_options(argv)
|
|
24
|
-
return
|
|
25
|
+
return CLI::USAGE_ERROR unless options
|
|
25
26
|
|
|
26
27
|
entries = analyzer_entries
|
|
27
28
|
options[:json] ? emit_json(entries) : emit_table(entries)
|
|
@@ -41,7 +42,7 @@ module MetzScan
|
|
|
41
42
|
end
|
|
42
43
|
|
|
43
44
|
def parse_error(error)
|
|
44
|
-
stderr.puts error.message
|
|
45
|
+
stderr.puts "metz-scan project-analyzers: #{error.message}"
|
|
45
46
|
stderr.puts option_parser.help
|
|
46
47
|
nil
|
|
47
48
|
end
|
|
@@ -2,12 +2,14 @@
|
|
|
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"
|
|
8
9
|
require_relative "scan/text_renderer"
|
|
9
10
|
require_relative "scan/sarif_renderer"
|
|
10
11
|
require_relative "scan/github_annotations_renderer"
|
|
12
|
+
require_relative "../cli"
|
|
11
13
|
|
|
12
14
|
module MetzScan
|
|
13
15
|
module Commands
|
|
@@ -60,19 +62,26 @@ module MetzScan
|
|
|
60
62
|
end
|
|
61
63
|
|
|
62
64
|
def emit(options)
|
|
63
|
-
parsed =
|
|
65
|
+
parsed = load_report(options.path)
|
|
64
66
|
return parsed if parsed.is_a?(Integer)
|
|
65
67
|
|
|
66
68
|
render(parsed, options.format)
|
|
67
69
|
Scan::Runner.exit_code_for(parsed)
|
|
68
70
|
end
|
|
69
71
|
|
|
70
|
-
def
|
|
71
|
-
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)
|
|
72
75
|
rescue JSON::ParserError => e
|
|
73
76
|
invalid_json(path, e.message)
|
|
74
77
|
end
|
|
75
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
|
+
|
|
76
85
|
def render(parsed, format)
|
|
77
86
|
return render_json(parsed) if format == "json"
|
|
78
87
|
|
|
@@ -87,28 +96,33 @@ module MetzScan
|
|
|
87
96
|
def parser_error(err)
|
|
88
97
|
stderr.puts "metz-scan report: #{err.message}"
|
|
89
98
|
stderr.puts USAGE
|
|
90
|
-
|
|
99
|
+
CLI::USAGE_ERROR
|
|
91
100
|
end
|
|
92
101
|
|
|
93
102
|
def missing_path_arg
|
|
94
103
|
stderr.puts "metz-scan report: missing PATH argument."
|
|
95
104
|
stderr.puts USAGE
|
|
96
|
-
|
|
105
|
+
CLI::USAGE_ERROR
|
|
97
106
|
end
|
|
98
107
|
|
|
99
108
|
def missing_file(path)
|
|
100
109
|
stderr.puts "metz-scan report: no such file: #{path}"
|
|
101
|
-
|
|
110
|
+
CLI::USAGE_ERROR
|
|
102
111
|
end
|
|
103
112
|
|
|
104
113
|
def invalid_format(fmt)
|
|
105
114
|
stderr.puts "metz-scan report: invalid --format '#{fmt}'. Valid formats: text, json, sarif, gh-annotations."
|
|
106
|
-
|
|
115
|
+
CLI::USAGE_ERROR
|
|
107
116
|
end
|
|
108
117
|
|
|
109
118
|
def invalid_json(path, detail)
|
|
110
119
|
stderr.puts "metz-scan report: invalid JSON in #{path}: #{detail}"
|
|
111
|
-
|
|
120
|
+
CLI::USAGE_ERROR
|
|
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
|
|
112
126
|
end
|
|
113
127
|
end
|
|
114
128
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "optparse"
|
|
5
|
+
require_relative "../cli"
|
|
5
6
|
|
|
6
7
|
module MetzScan
|
|
7
8
|
module Commands
|
|
@@ -23,16 +24,27 @@ module MetzScan
|
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
def run(argv)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
0
|
|
27
|
+
emit(parse_options(argv))
|
|
28
|
+
rescue OptionParser::ParseError => e
|
|
29
|
+
parser_error(e)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
private
|
|
33
33
|
|
|
34
34
|
attr_reader :stdout, :stderr
|
|
35
35
|
|
|
36
|
+
def emit(options)
|
|
37
|
+
cops = load_cops
|
|
38
|
+
options[:json] ? emit_json(cops) : emit_table(cops)
|
|
39
|
+
0
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def parser_error(err)
|
|
43
|
+
stderr.puts "metz-scan rules: #{err.message}"
|
|
44
|
+
stderr.puts USAGE
|
|
45
|
+
CLI::USAGE_ERROR
|
|
46
|
+
end
|
|
47
|
+
|
|
36
48
|
def parse_options(argv)
|
|
37
49
|
options = { json: false }
|
|
38
50
|
OptionParser.new { |opts| configure_options(opts, options) }.parse!(argv)
|
|
@@ -1,57 +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
|
-
SAFE_FLAG = "-a"
|
|
12
|
-
UNSAFE_FLAG = "-A"
|
|
13
|
-
CommandResult = Struct.new(:status, :stdout, :stderr, keyword_init: true)
|
|
14
|
-
|
|
15
14
|
def initialize(stdout:, stderr:)
|
|
16
15
|
@stdout = stdout
|
|
17
16
|
@stderr = stderr
|
|
18
17
|
end
|
|
19
18
|
|
|
20
19
|
def run(options)
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
23
25
|
end
|
|
24
26
|
|
|
25
27
|
private
|
|
26
28
|
|
|
27
29
|
attr_reader :stdout, :stderr
|
|
28
30
|
|
|
29
|
-
def
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def with_redirected_io
|
|
34
|
-
previous = redirect_to(stdout, stderr)
|
|
35
|
-
yield
|
|
36
|
-
ensure
|
|
37
|
-
restore_io(previous) if previous
|
|
38
|
-
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?
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
previous = [$stdout, $stderr]
|
|
42
|
-
$stdout = out
|
|
43
|
-
$stderr = err
|
|
44
|
-
previous
|
|
36
|
+
{ paths: paths, store: scope[:store] }
|
|
45
37
|
end
|
|
46
38
|
|
|
47
|
-
def
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
50
44
|
end
|
|
51
45
|
|
|
52
|
-
def run_dry(options)
|
|
53
|
-
snapshot = capture_files(
|
|
54
|
-
dry_run_result(snapshot,
|
|
46
|
+
def run_dry(options, scope)
|
|
47
|
+
snapshot = capture_files(scope[:paths])
|
|
48
|
+
dry_run_result(snapshot, checked_rubocop(options, scope))
|
|
55
49
|
ensure
|
|
56
50
|
restore_files(snapshot)
|
|
57
51
|
end
|
|
@@ -62,40 +56,44 @@ module MetzScan
|
|
|
62
56
|
result.status
|
|
63
57
|
end
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
78
|
-
|
|
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
|
|
85
|
-
|
|
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
|
|
89
|
-
|
|
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
|
|
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
|
|
97
|
-
|
|
98
|
-
|
|
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?
|
|
@@ -103,8 +101,7 @@ module MetzScan
|
|
|
103
101
|
end
|
|
104
102
|
|
|
105
103
|
def print_file_diff(path, original)
|
|
106
|
-
return unless File.exist?(path)
|
|
107
|
-
return if File.binread(path) == original
|
|
104
|
+
return unless File.exist?(path) && changed?(path, original)
|
|
108
105
|
|
|
109
106
|
stdout.puts diff_against(path, original)
|
|
110
107
|
end
|
|
@@ -130,8 +127,11 @@ module MetzScan
|
|
|
130
127
|
def restore_files(snapshot)
|
|
131
128
|
return unless snapshot
|
|
132
129
|
|
|
133
|
-
snapshot.each { |path, content| File.binwrite(path, content) }
|
|
130
|
+
snapshot.each { |path, content| File.binwrite(path, content) if changed?(path, content) }
|
|
134
131
|
end
|
|
132
|
+
|
|
133
|
+
# Only rewrite what RuboCop touched, so an unchanged read-only file cannot abort the restore.
|
|
134
|
+
def changed?(path, original) = !File.exist?(path) || File.binread(path) != original
|
|
135
135
|
end
|
|
136
136
|
end
|
|
137
137
|
end
|
|
@@ -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
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
|
|
5
|
+
require "metz_scan/commands/scan/exclude_entry_locator"
|
|
6
|
+
|
|
7
|
+
module MetzScan
|
|
8
|
+
module Commands
|
|
9
|
+
class Scan
|
|
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.
|
|
17
|
+
class ConfigExclusions
|
|
18
|
+
def initialize(store, rubocop_store: nil)
|
|
19
|
+
@store = store
|
|
20
|
+
@rubocop_store = rubocop_store
|
|
21
|
+
@locators = {}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def scoped_off?(path, offense)
|
|
25
|
+
absolute = File.expand_path(path)
|
|
26
|
+
cop_class = cop_class(offense)
|
|
27
|
+
return analyzer_scoped_off?(offense, absolute) unless cop_class
|
|
28
|
+
|
|
29
|
+
cop_scoped_off?(cop_class.new(store.for_file(absolute)), absolute)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def record_fields(path, offense)
|
|
33
|
+
absolute = File.expand_path(path)
|
|
34
|
+
entry = applied_exclude(offense, absolute)
|
|
35
|
+
return local_record_fields(entry) if entry
|
|
36
|
+
|
|
37
|
+
remote_record_fields(offense, absolute)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
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
|
|
92
|
+
|
|
93
|
+
def cop_class(offense)
|
|
94
|
+
RuboCop::Cop::Registry.global.find_by_cop_name(offense.fetch("cop_name"))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def config(entry, location)
|
|
98
|
+
{ "path" => Runner.display_path(entry.config_path), "line" => location.line,
|
|
99
|
+
"pattern" => display(entry.pattern) }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def display(pattern)
|
|
103
|
+
pattern.is_a?(Regexp) ? pattern.inspect : pattern
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def locator(config_path)
|
|
107
|
+
@locators[config_path] ||= ExcludeEntryLocator.new(config_path)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
|
|
5
|
+
module MetzScan
|
|
6
|
+
module Commands
|
|
7
|
+
class Scan
|
|
8
|
+
module ProjectConfigScope
|
|
9
|
+
# The per-cop Exclude entries a scope-only config applies, each mapped
|
|
10
|
+
# back to the config file that wrote it. Entries are paired with their
|
|
11
|
+
# source by object identity before make_excludes_absolute rewrites
|
|
12
|
+
# them, so an entry a derived file overrides is never credited.
|
|
13
|
+
class ExcludeEntries
|
|
14
|
+
Entry = Struct.new(:config_path, :key, :pattern, keyword_init: true)
|
|
15
|
+
|
|
16
|
+
def self.patterns(config)
|
|
17
|
+
config.to_h.filter_map { |key, value| [key, value["Exclude"].dup] if per_cop_exclude?(key, value) }.to_h
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.per_cop_exclude?(key, value)
|
|
21
|
+
key != "AllCops" && value.is_a?(Hash) && value["Exclude"].is_a?(Array)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize(raw_patterns = {}, absolute_patterns = {}, sources = {})
|
|
25
|
+
@entries = {}
|
|
26
|
+
raw_patterns.each do |key, patterns|
|
|
27
|
+
patterns.zip(absolute_patterns.fetch(key)).each { |raw, absolute| index(key, raw, absolute, sources) }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The first Exclude pattern a cop applies to `file` that a project
|
|
32
|
+
# config wrote; stock-default patterns have no source and are skipped.
|
|
33
|
+
def applied(config, badge, file)
|
|
34
|
+
relative = config.path_relative_to_config(file)
|
|
35
|
+
Array(config.for_badge(badge)["Exclude"]).lazy.filter_map do |pattern|
|
|
36
|
+
sourced_entry(badge, pattern) if matches?(pattern, relative, file)
|
|
37
|
+
end.first
|
|
38
|
+
end
|
|
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
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def index(key, raw, absolute, sources)
|
|
48
|
+
return unless sources.key?(raw)
|
|
49
|
+
|
|
50
|
+
@entries[[key, absolute]] = Entry.new(config_path: sources.fetch(raw), key: key, pattern: raw)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def sourced_entry(badge, pattern)
|
|
54
|
+
@entries[[badge.to_s, pattern]] || @entries[[badge.department_name, pattern]]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def matches?(pattern, relative, file)
|
|
58
|
+
RuboCop::PathUtil.match_path?(pattern, relative) || RuboCop::PathUtil.match_path?(pattern, file)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|