dart-decimate 0.0.1
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.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
use std::collections::BTreeMap;
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::path::{Path, PathBuf};
|
|
4
|
+
|
|
5
|
+
use crate::Location;
|
|
6
|
+
use crate::graph::normalize_against;
|
|
7
|
+
|
|
8
|
+
use super::thresholds::ThresholdContext;
|
|
9
|
+
use super::{CoverageGapFinding, CoverageGapReason, CrapFinding, FunctionMetrics, HealthError};
|
|
10
|
+
|
|
11
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
12
|
+
pub(super) struct CoverageMap {
|
|
13
|
+
files: BTreeMap<PathBuf, CoverageFile>,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
17
|
+
pub(super) struct CoverageFile {
|
|
18
|
+
lines: BTreeMap<usize, usize>,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
pub(super) fn load_lcov(root: &Path, path: &Path) -> Result<CoverageMap, HealthError> {
|
|
22
|
+
let source = fs::read_to_string(path).map_err(|source| HealthError::ReadCoverage {
|
|
23
|
+
path: path.to_path_buf(),
|
|
24
|
+
source,
|
|
25
|
+
})?;
|
|
26
|
+
parse_lcov(root, path, &source)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
pub(super) fn coverage_gap_findings(
|
|
30
|
+
functions: &[FunctionMetrics],
|
|
31
|
+
coverage: &CoverageMap,
|
|
32
|
+
) -> Vec<CoverageGapFinding> {
|
|
33
|
+
let mut first_function_by_path = BTreeMap::<PathBuf, &FunctionMetrics>::new();
|
|
34
|
+
for function in functions {
|
|
35
|
+
first_function_by_path
|
|
36
|
+
.entry(function.path.clone())
|
|
37
|
+
.or_insert(function);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
first_function_by_path
|
|
41
|
+
.into_iter()
|
|
42
|
+
.filter_map(|(path, function)| coverage_gap_for_file(path, function, coverage))
|
|
43
|
+
.collect()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pub(super) fn crap_findings(
|
|
47
|
+
functions: &[FunctionMetrics],
|
|
48
|
+
coverage: &CoverageMap,
|
|
49
|
+
threshold_context: &mut ThresholdContext,
|
|
50
|
+
) -> Vec<CrapFinding> {
|
|
51
|
+
functions
|
|
52
|
+
.iter()
|
|
53
|
+
.filter_map(|function| crap_finding(function, coverage, threshold_context))
|
|
54
|
+
.collect()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
impl CoverageMap {
|
|
58
|
+
pub(super) fn file_count(&self) -> usize {
|
|
59
|
+
self.files.len()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
pub(super) fn file(&self, path: &Path) -> Option<&CoverageFile> {
|
|
63
|
+
self.files.get(path)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
impl CoverageFile {
|
|
68
|
+
fn new() -> Self {
|
|
69
|
+
Self {
|
|
70
|
+
lines: BTreeMap::new(),
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
pub(super) fn covered_lines(&self) -> usize {
|
|
75
|
+
self.lines.values().filter(|hits| **hits > 0).count()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
pub(super) fn executable_lines(&self) -> usize {
|
|
79
|
+
self.lines.len()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
pub(super) fn first_executable_line(&self) -> Option<usize> {
|
|
83
|
+
self.lines.keys().next().copied()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
pub(super) fn covered_lines_in_range(&self, start: usize, end: usize) -> usize {
|
|
87
|
+
self.lines
|
|
88
|
+
.range(start..=end)
|
|
89
|
+
.filter(|(_, hits)| **hits > 0)
|
|
90
|
+
.count()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub(super) fn executable_lines_in_range(&self, start: usize, end: usize) -> usize {
|
|
94
|
+
self.lines.range(start..=end).count()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fn add_line(&mut self, line: usize, hits: usize) {
|
|
98
|
+
*self.lines.entry(line).or_default() += hits;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
fn parse_lcov(root: &Path, path: &Path, source: &str) -> Result<CoverageMap, HealthError> {
|
|
103
|
+
let mut files = BTreeMap::<PathBuf, CoverageFile>::new();
|
|
104
|
+
let mut current_path = None::<PathBuf>;
|
|
105
|
+
let mut current_file = CoverageFile::new();
|
|
106
|
+
|
|
107
|
+
for (index, raw_line) in source.lines().enumerate() {
|
|
108
|
+
let line_number = index + 1;
|
|
109
|
+
let line = raw_line.trim();
|
|
110
|
+
if line.is_empty() {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if let Some(value) = line.strip_prefix("SF:") {
|
|
115
|
+
commit_record(&mut files, &mut current_path, &mut current_file);
|
|
116
|
+
current_path = Some(coverage_path(root, path, value, line_number)?);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if let Some(value) = line.strip_prefix("DA:") {
|
|
121
|
+
let Some(_) = current_path else {
|
|
122
|
+
return Err(parse_error(
|
|
123
|
+
path,
|
|
124
|
+
line_number,
|
|
125
|
+
"DA record appeared before SF",
|
|
126
|
+
));
|
|
127
|
+
};
|
|
128
|
+
let (line, hits) = parse_da(path, line_number, value)?;
|
|
129
|
+
current_file.add_line(line, hits);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if line == "end_of_record" {
|
|
134
|
+
commit_record(&mut files, &mut current_path, &mut current_file);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
commit_record(&mut files, &mut current_path, &mut current_file);
|
|
139
|
+
Ok(CoverageMap { files })
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
fn coverage_path(
|
|
143
|
+
root: &Path,
|
|
144
|
+
lcov_path: &Path,
|
|
145
|
+
value: &str,
|
|
146
|
+
line: usize,
|
|
147
|
+
) -> Result<PathBuf, HealthError> {
|
|
148
|
+
if value.is_empty() {
|
|
149
|
+
return Err(parse_error(lcov_path, line, "SF path was empty"));
|
|
150
|
+
}
|
|
151
|
+
let path = Path::new(value);
|
|
152
|
+
Ok(normalize_against(root, path))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fn parse_da(path: &Path, line: usize, value: &str) -> Result<(usize, usize), HealthError> {
|
|
156
|
+
let mut fields = value.split(',');
|
|
157
|
+
let source_line = parse_positive_usize(path, line, fields.next(), "DA line")?;
|
|
158
|
+
let hits = parse_usize(path, line, fields.next(), "DA hit count")?;
|
|
159
|
+
Ok((source_line, hits))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
fn parse_positive_usize(
|
|
163
|
+
path: &Path,
|
|
164
|
+
line: usize,
|
|
165
|
+
value: Option<&str>,
|
|
166
|
+
label: &str,
|
|
167
|
+
) -> Result<usize, HealthError> {
|
|
168
|
+
let value = parse_usize(path, line, value, label)?;
|
|
169
|
+
if value == 0 {
|
|
170
|
+
return Err(parse_error(
|
|
171
|
+
path,
|
|
172
|
+
line,
|
|
173
|
+
&format!("{label} must be positive"),
|
|
174
|
+
));
|
|
175
|
+
}
|
|
176
|
+
Ok(value)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
fn parse_usize(
|
|
180
|
+
path: &Path,
|
|
181
|
+
line: usize,
|
|
182
|
+
value: Option<&str>,
|
|
183
|
+
label: &str,
|
|
184
|
+
) -> Result<usize, HealthError> {
|
|
185
|
+
let Some(value) = value else {
|
|
186
|
+
return Err(parse_error(path, line, &format!("{label} was missing")));
|
|
187
|
+
};
|
|
188
|
+
value
|
|
189
|
+
.parse::<usize>()
|
|
190
|
+
.map_err(|_| parse_error(path, line, &format!("{label} was not a number")))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
fn commit_record(
|
|
194
|
+
files: &mut BTreeMap<PathBuf, CoverageFile>,
|
|
195
|
+
current_path: &mut Option<PathBuf>,
|
|
196
|
+
current_file: &mut CoverageFile,
|
|
197
|
+
) {
|
|
198
|
+
let Some(path) = current_path.take() else {
|
|
199
|
+
return;
|
|
200
|
+
};
|
|
201
|
+
if path.extension().and_then(|extension| extension.to_str()) != Some("dart") {
|
|
202
|
+
*current_file = CoverageFile::new();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
let record = std::mem::replace(current_file, CoverageFile::new());
|
|
206
|
+
let entry = files.entry(path).or_insert_with(CoverageFile::new);
|
|
207
|
+
for (line, hits) in record.lines {
|
|
208
|
+
entry.add_line(line, hits);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
fn coverage_gap_for_file(
|
|
213
|
+
path: PathBuf,
|
|
214
|
+
function: &FunctionMetrics,
|
|
215
|
+
coverage: &CoverageMap,
|
|
216
|
+
) -> Option<CoverageGapFinding> {
|
|
217
|
+
let Some(file) = coverage.file(&path) else {
|
|
218
|
+
return Some(CoverageGapFinding {
|
|
219
|
+
path,
|
|
220
|
+
location: function.location,
|
|
221
|
+
reason: CoverageGapReason::MissingFromCoverage,
|
|
222
|
+
covered_lines: 0,
|
|
223
|
+
executable_lines: 0,
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
let covered_lines = file.covered_lines();
|
|
228
|
+
let executable_lines = file.executable_lines();
|
|
229
|
+
let reason = if executable_lines == 0 {
|
|
230
|
+
CoverageGapReason::NoExecutableLines
|
|
231
|
+
} else if covered_lines == 0 {
|
|
232
|
+
CoverageGapReason::ZeroCoveredLines
|
|
233
|
+
} else {
|
|
234
|
+
return None;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
Some(CoverageGapFinding {
|
|
238
|
+
path,
|
|
239
|
+
location: file
|
|
240
|
+
.first_executable_line()
|
|
241
|
+
.map_or(function.location, |line| Location { line, column: 0 }),
|
|
242
|
+
reason,
|
|
243
|
+
covered_lines,
|
|
244
|
+
executable_lines,
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
fn crap_finding(
|
|
249
|
+
function: &FunctionMetrics,
|
|
250
|
+
coverage: &CoverageMap,
|
|
251
|
+
threshold_context: &mut ThresholdContext,
|
|
252
|
+
) -> Option<CrapFinding> {
|
|
253
|
+
let file = coverage.file(&function.path)?;
|
|
254
|
+
let executable_lines =
|
|
255
|
+
file.executable_lines_in_range(function.location.line, function.end_line);
|
|
256
|
+
if executable_lines == 0 {
|
|
257
|
+
return None;
|
|
258
|
+
}
|
|
259
|
+
let covered_lines = file.covered_lines_in_range(function.location.line, function.end_line);
|
|
260
|
+
let crap_score = crap_score(function.cyclomatic, covered_lines, executable_lines);
|
|
261
|
+
let thresholds = threshold_context.crap_thresholds(function, crap_score)?;
|
|
262
|
+
let max_crap = thresholds.effective.max_crap?;
|
|
263
|
+
if crap_score <= max_crap {
|
|
264
|
+
return None;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
Some(CrapFinding {
|
|
268
|
+
path: function.path.clone(),
|
|
269
|
+
symbol: function.symbol.clone(),
|
|
270
|
+
kind: function.kind,
|
|
271
|
+
location: function.location,
|
|
272
|
+
cyclomatic_complexity: function.cyclomatic,
|
|
273
|
+
cognitive_complexity: function.cognitive,
|
|
274
|
+
covered_lines,
|
|
275
|
+
executable_lines,
|
|
276
|
+
line_coverage_percent: coverage_percent(covered_lines, executable_lines),
|
|
277
|
+
crap_score,
|
|
278
|
+
effective_thresholds: thresholds.source.map(|_| thresholds.effective.clone()),
|
|
279
|
+
threshold_source: thresholds.source,
|
|
280
|
+
threshold_reason: thresholds.reason,
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
fn crap_score(cyclomatic: usize, covered_lines: usize, executable_lines: usize) -> usize {
|
|
285
|
+
let complexity = cyclomatic as u128;
|
|
286
|
+
let missed_lines = executable_lines.saturating_sub(covered_lines) as u128;
|
|
287
|
+
let executable_lines = executable_lines as u128;
|
|
288
|
+
let numerator = complexity
|
|
289
|
+
.saturating_mul(complexity)
|
|
290
|
+
.saturating_mul(missed_lines.pow(3));
|
|
291
|
+
let denominator = executable_lines.pow(3);
|
|
292
|
+
let uncovered_risk = numerator.saturating_add(denominator - 1) / denominator;
|
|
293
|
+
usize::try_from(uncovered_risk.saturating_add(complexity)).unwrap_or(usize::MAX)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn coverage_percent(covered_lines: usize, executable_lines: usize) -> usize {
|
|
297
|
+
covered_lines
|
|
298
|
+
.saturating_mul(100)
|
|
299
|
+
.saturating_add(executable_lines / 2)
|
|
300
|
+
/ executable_lines
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
fn parse_error(path: &Path, line: usize, message: &str) -> HealthError {
|
|
304
|
+
HealthError::ParseCoverage {
|
|
305
|
+
path: path.to_path_buf(),
|
|
306
|
+
line,
|
|
307
|
+
message: message.to_owned(),
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
use std::collections::BTreeMap;
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::path::{Path, PathBuf};
|
|
4
|
+
|
|
5
|
+
use glob::Pattern;
|
|
6
|
+
|
|
7
|
+
use super::{FileHealthScore, HealthHotspot, RefactoringTarget};
|
|
8
|
+
use crate::graph::normalize_path;
|
|
9
|
+
|
|
10
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
11
|
+
struct CodeownersRule {
|
|
12
|
+
pattern: String,
|
|
13
|
+
owners: Vec<String>,
|
|
14
|
+
source: String,
|
|
15
|
+
section: Option<String>,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
19
|
+
struct OwnerMatch {
|
|
20
|
+
owners: Vec<String>,
|
|
21
|
+
source: String,
|
|
22
|
+
section: Option<String>,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pub(super) fn apply_ownership(
|
|
26
|
+
root: &Path,
|
|
27
|
+
scores: &mut [FileHealthScore],
|
|
28
|
+
hotspots: &mut [HealthHotspot],
|
|
29
|
+
targets: &mut [RefactoringTarget],
|
|
30
|
+
) {
|
|
31
|
+
let rules = codeowners_rules(root);
|
|
32
|
+
if rules.is_empty() {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let matches = scores
|
|
36
|
+
.iter()
|
|
37
|
+
.filter_map(|score| {
|
|
38
|
+
owner_match(root, &score.path, &rules).map(|owner| (score.path.clone(), owner))
|
|
39
|
+
})
|
|
40
|
+
.collect::<BTreeMap<_, _>>();
|
|
41
|
+
|
|
42
|
+
for score in scores {
|
|
43
|
+
apply_owner(&matches, &score.path, |owner| {
|
|
44
|
+
score.owners.clone_from(&owner.owners);
|
|
45
|
+
score.owner_source = Some(owner.source.clone());
|
|
46
|
+
score.owner_section.clone_from(&owner.section);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
for hotspot in hotspots {
|
|
50
|
+
apply_owner(&matches, &hotspot.path, |owner| {
|
|
51
|
+
hotspot.owners.clone_from(&owner.owners);
|
|
52
|
+
hotspot.owner_source = Some(owner.source.clone());
|
|
53
|
+
hotspot.owner_section.clone_from(&owner.section);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
for target in targets {
|
|
57
|
+
apply_owner(&matches, &target.path, |owner| {
|
|
58
|
+
target.owners.clone_from(&owner.owners);
|
|
59
|
+
target.owner_source = Some(owner.source.clone());
|
|
60
|
+
target.owner_section.clone_from(&owner.section);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn apply_owner(
|
|
66
|
+
matches: &BTreeMap<PathBuf, OwnerMatch>,
|
|
67
|
+
path: &Path,
|
|
68
|
+
apply: impl FnOnce(&OwnerMatch),
|
|
69
|
+
) {
|
|
70
|
+
if let Some(owner) = matches.get(path) {
|
|
71
|
+
apply(owner);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn codeowners_rules(root: &Path) -> Vec<CodeownersRule> {
|
|
76
|
+
codeowners_paths(root)
|
|
77
|
+
.into_iter()
|
|
78
|
+
.find_map(|path| {
|
|
79
|
+
let source = display_path(root, &path);
|
|
80
|
+
fs::read_to_string(&path)
|
|
81
|
+
.ok()
|
|
82
|
+
.map(|text| parse_codeowners(&text, &source))
|
|
83
|
+
})
|
|
84
|
+
.unwrap_or_default()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fn display_path(root: &Path, path: &Path) -> String {
|
|
88
|
+
path.strip_prefix(root)
|
|
89
|
+
.unwrap_or(path)
|
|
90
|
+
.to_string_lossy()
|
|
91
|
+
.replace('\\', "/")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn codeowners_paths(root: &Path) -> Vec<PathBuf> {
|
|
95
|
+
[
|
|
96
|
+
".github/CODEOWNERS",
|
|
97
|
+
".gitlab/CODEOWNERS",
|
|
98
|
+
"CODEOWNERS",
|
|
99
|
+
"docs/CODEOWNERS",
|
|
100
|
+
]
|
|
101
|
+
.into_iter()
|
|
102
|
+
.map(|path| root.join(path))
|
|
103
|
+
.filter(|path| path.is_file())
|
|
104
|
+
.collect()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn parse_codeowners(source: &str, source_path: &str) -> Vec<CodeownersRule> {
|
|
108
|
+
let mut section = None;
|
|
109
|
+
source
|
|
110
|
+
.lines()
|
|
111
|
+
.filter_map(|line| {
|
|
112
|
+
let trimmed = line.trim();
|
|
113
|
+
if trimmed.is_empty() || trimmed.starts_with('#') {
|
|
114
|
+
return None;
|
|
115
|
+
}
|
|
116
|
+
if let Some(header) = section_header(trimmed) {
|
|
117
|
+
section = Some(header.to_owned());
|
|
118
|
+
return None;
|
|
119
|
+
}
|
|
120
|
+
let mut parts = trimmed.split_whitespace();
|
|
121
|
+
let pattern = parts.next()?.to_owned();
|
|
122
|
+
let owners = parts.map(ToOwned::to_owned).collect::<Vec<_>>();
|
|
123
|
+
(!owners.is_empty()).then(|| CodeownersRule {
|
|
124
|
+
pattern,
|
|
125
|
+
owners,
|
|
126
|
+
source: source_path.to_owned(),
|
|
127
|
+
section: section.clone(),
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
.collect()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn section_header(line: &str) -> Option<&str> {
|
|
134
|
+
line.strip_prefix('[')
|
|
135
|
+
.and_then(|rest| rest.split_once(']'))
|
|
136
|
+
.map(|(section, _)| section.trim())
|
|
137
|
+
.filter(|section| !section.is_empty())
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
fn owner_match(root: &Path, path: &Path, rules: &[CodeownersRule]) -> Option<OwnerMatch> {
|
|
141
|
+
let relative = relative_path(root, path);
|
|
142
|
+
rules
|
|
143
|
+
.iter()
|
|
144
|
+
.rfind(|rule| pattern_matches(&rule.pattern, &relative))
|
|
145
|
+
.map(|rule| OwnerMatch {
|
|
146
|
+
owners: rule.owners.clone(),
|
|
147
|
+
source: rule.source.clone(),
|
|
148
|
+
section: rule.section.clone(),
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
fn relative_path(root: &Path, path: &Path) -> String {
|
|
153
|
+
normalize_path(path)
|
|
154
|
+
.strip_prefix(normalize_path(root))
|
|
155
|
+
.unwrap_or(path)
|
|
156
|
+
.to_string_lossy()
|
|
157
|
+
.replace('\\', "/")
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
fn pattern_matches(pattern: &str, relative: &str) -> bool {
|
|
161
|
+
let pattern = pattern.trim();
|
|
162
|
+
if pattern.is_empty() {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
let pattern = pattern.strip_prefix('/').unwrap_or(pattern);
|
|
166
|
+
if let Some(prefix) = pattern.strip_suffix('/') {
|
|
167
|
+
return relative.starts_with(prefix);
|
|
168
|
+
}
|
|
169
|
+
glob_matches(pattern, relative)
|
|
170
|
+
|| (!pattern.contains('/') && glob_matches(&format!("**/{pattern}"), relative))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fn glob_matches(pattern: &str, relative: &str) -> bool {
|
|
174
|
+
Pattern::new(pattern).is_ok_and(|pattern| pattern.matches(relative))
|
|
175
|
+
}
|