dart-decimate 0.0.28 → 0.0.30
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 +1 -1
- package/Cargo.toml +1 -1
- package/README.md +36 -60
- package/npm/bin/runner.js +26 -14
- package/npm/scripts/postinstall.js +89 -63
- package/package.json +6 -5
- package/src/baseline.rs +38 -0
- package/src/cli/health_args.rs +36 -7
- package/src/cli/html_open.rs +4 -0
- package/src/cli/issue_filter_args.rs +1 -0
- package/src/cli/security_summary_run.rs +1 -0
- package/src/config/health.rs +22 -2
- package/src/config/rule_aliases.rs +43 -0
- package/src/config/rules.rs +46 -1
- package/src/config.rs +2 -1
- package/src/dart_parser/test_support.rs +51 -0
- package/src/dart_parser.rs +8 -0
- package/src/dependencies/analyze.rs +3 -1
- package/src/dependencies/tests.rs +111 -7
- package/src/dependencies/usage.rs +5 -1
- package/src/dependencies.rs +7 -3
- package/src/explain/flutter_style.rs +43 -0
- package/src/explain/misc.rs +72 -0
- package/src/explain.rs +23 -67
- package/src/flutter_style/scanner.rs +883 -0
- package/src/flutter_style/types.rs +90 -0
- package/src/flutter_style/usage.rs +349 -0
- package/src/flutter_style.rs +72 -0
- package/src/generated.rs +154 -0
- package/src/health/large_units.rs +49 -0
- package/src/health/tests.rs +212 -1
- package/src/health/threshold_types.rs +31 -1
- package/src/health/thresholds.rs +49 -0
- package/src/health/types.rs +35 -0
- package/src/health.rs +72 -6
- package/src/inspect.rs +8 -0
- package/src/lib.rs +17 -5
- package/src/manifest.rs +7 -3
- package/src/output/dependency_findings.rs +14 -1
- package/src/output/flutter_style.rs +189 -0
- package/src/output/health_findings.rs +93 -10
- package/src/output/health_inventory.rs +121 -0
- package/src/output/html/tests.rs +20 -0
- package/src/output/html.rs +10 -1
- package/src/output/human.rs +85 -1
- package/src/output/human_details.rs +93 -12
- package/src/output/scope.rs +62 -2
- package/src/output/security_findings.rs +101 -76
- package/src/output/security_sarif.rs +24 -13
- package/src/output/suppressions.rs +4 -0
- package/src/output/types.rs +38 -2
- package/src/output/verdict.rs +12 -0
- package/src/output.rs +77 -74
- package/src/report_schema/inventory.rs +268 -4
- package/src/report_schema.rs +94 -60
- package/src/security/catalogue.rs +103 -0
- package/src/security/detect/weak_randomness.rs +762 -0
- package/src/security/detect.rs +1535 -221
- package/src/security/security_matchers.toml +172 -0
- package/src/security/tests.rs +336 -2
- package/src/security.rs +292 -56
- package/src/security_gate.rs +28 -0
- package/src/semantic.rs +276 -0
- package/src/symbols/semantic_evidence.rs +342 -0
- package/src/symbols.rs +20 -10
- package/src/trace/rendering.rs +54 -0
- package/src/trace/semantic.rs +126 -0
- package/src/trace.rs +54 -62
- package/src/widgets/inheritance.rs +63 -0
- package/src/widgets.rs +33 -3
package/src/generated.rs
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
+
use std::collections::BTreeMap;
|
|
2
|
+
use std::fs;
|
|
1
3
|
use std::path::Path;
|
|
4
|
+
use std::path::PathBuf;
|
|
5
|
+
use std::sync::{LazyLock, Mutex};
|
|
6
|
+
use std::time::SystemTime;
|
|
7
|
+
|
|
8
|
+
#[derive(Clone)]
|
|
9
|
+
struct FlutterL10nConfig {
|
|
10
|
+
output_dir: PathBuf,
|
|
11
|
+
output_file: String,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#[derive(Clone)]
|
|
15
|
+
struct CachedFlutterL10nConfig {
|
|
16
|
+
length: u64,
|
|
17
|
+
modified: Option<SystemTime>,
|
|
18
|
+
config: Option<FlutterL10nConfig>,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static FLUTTER_L10N_CONFIGS: LazyLock<Mutex<BTreeMap<PathBuf, CachedFlutterL10nConfig>>> =
|
|
22
|
+
LazyLock::new(|| Mutex::new(BTreeMap::new()));
|
|
2
23
|
|
|
3
24
|
pub(crate) const GENERATED_DART_SUFFIXES: &[&str] = &[
|
|
4
25
|
".g.dart",
|
|
@@ -6,6 +27,10 @@ pub(crate) const GENERATED_DART_SUFFIXES: &[&str] = &[
|
|
|
6
27
|
".gen.dart",
|
|
7
28
|
".gr.dart",
|
|
8
29
|
".mocks.dart",
|
|
30
|
+
".pb.dart",
|
|
31
|
+
".pbenum.dart",
|
|
32
|
+
".pbjson.dart",
|
|
33
|
+
".pbgrpc.dart",
|
|
9
34
|
];
|
|
10
35
|
|
|
11
36
|
#[must_use]
|
|
@@ -15,6 +40,8 @@ pub(crate) fn is_generated_dart_path(path: &Path) -> bool {
|
|
|
15
40
|
};
|
|
16
41
|
is_generated_dart_file_name(file_name)
|
|
17
42
|
|| is_flutter_gen_l10n_path(path, file_name)
|
|
43
|
+
|| is_drift_generated_schema_path(path, file_name)
|
|
44
|
+
|| is_configured_flutter_gen_l10n_path(path, file_name)
|
|
18
45
|
|| is_flutterfire_options_path(path)
|
|
19
46
|
}
|
|
20
47
|
|
|
@@ -38,6 +65,91 @@ fn is_flutter_gen_l10n_path(path: &Path, file_name: &str) -> bool {
|
|
|
38
65
|
})
|
|
39
66
|
}
|
|
40
67
|
|
|
68
|
+
fn is_drift_generated_schema_path(path: &Path, file_name: &str) -> bool {
|
|
69
|
+
let Some(version) = file_name
|
|
70
|
+
.strip_prefix("schema_v")
|
|
71
|
+
.and_then(|name| name.strip_suffix(".dart"))
|
|
72
|
+
else {
|
|
73
|
+
return false;
|
|
74
|
+
};
|
|
75
|
+
!version.is_empty()
|
|
76
|
+
&& version.bytes().all(|byte| byte.is_ascii_digit())
|
|
77
|
+
&& path
|
|
78
|
+
.parent()
|
|
79
|
+
.and_then(Path::file_name)
|
|
80
|
+
.is_some_and(|parent| parent == "generated")
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fn is_configured_flutter_gen_l10n_path(path: &Path, file_name: &str) -> bool {
|
|
84
|
+
let Some(parent) = path.parent() else {
|
|
85
|
+
return false;
|
|
86
|
+
};
|
|
87
|
+
let Some(config) = flutter_l10n_config(parent) else {
|
|
88
|
+
return false;
|
|
89
|
+
};
|
|
90
|
+
if parent != config.output_dir {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if file_name == config.output_file {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
let Some(stem) = config.output_file.strip_suffix(".dart") else {
|
|
97
|
+
return false;
|
|
98
|
+
};
|
|
99
|
+
file_name
|
|
100
|
+
.strip_prefix(&format!("{stem}_"))
|
|
101
|
+
.is_some_and(is_flutter_gen_l10n_locale_file)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn flutter_l10n_config(directory: &Path) -> Option<FlutterL10nConfig> {
|
|
105
|
+
for project_root in directory.ancestors() {
|
|
106
|
+
let config_path = project_root.join("l10n.yaml");
|
|
107
|
+
let Ok(metadata) = fs::metadata(&config_path) else {
|
|
108
|
+
continue;
|
|
109
|
+
};
|
|
110
|
+
let modified = metadata.modified().ok();
|
|
111
|
+
let Ok(mut cache) = FLUTTER_L10N_CONFIGS.lock() else {
|
|
112
|
+
return None;
|
|
113
|
+
};
|
|
114
|
+
if let Some(cached) = cache.get(&config_path)
|
|
115
|
+
&& cached.length == metadata.len()
|
|
116
|
+
&& cached.modified == modified
|
|
117
|
+
{
|
|
118
|
+
return cached.config.clone();
|
|
119
|
+
}
|
|
120
|
+
let config = read_flutter_l10n_config(project_root, &config_path);
|
|
121
|
+
cache.insert(
|
|
122
|
+
config_path,
|
|
123
|
+
CachedFlutterL10nConfig {
|
|
124
|
+
length: metadata.len(),
|
|
125
|
+
modified,
|
|
126
|
+
config: config.clone(),
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
return config;
|
|
130
|
+
}
|
|
131
|
+
None
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn read_flutter_l10n_config(project_root: &Path, config_path: &Path) -> Option<FlutterL10nConfig> {
|
|
135
|
+
let source = fs::read_to_string(config_path).ok()?;
|
|
136
|
+
let config = serde_yaml_ng::from_str::<serde_yaml_ng::Value>(&source).ok()?;
|
|
137
|
+
let output_file = config
|
|
138
|
+
.get("output-localization-file")
|
|
139
|
+
.and_then(serde_yaml_ng::Value::as_str)
|
|
140
|
+
.unwrap_or("app_localizations.dart")
|
|
141
|
+
.to_owned();
|
|
142
|
+
let output_dir = config
|
|
143
|
+
.get("output-dir")
|
|
144
|
+
.and_then(serde_yaml_ng::Value::as_str)
|
|
145
|
+
.or_else(|| config.get("arb-dir").and_then(serde_yaml_ng::Value::as_str))
|
|
146
|
+
.unwrap_or("lib/l10n");
|
|
147
|
+
Some(FlutterL10nConfig {
|
|
148
|
+
output_dir: project_root.join(output_dir),
|
|
149
|
+
output_file,
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
|
|
41
153
|
fn is_flutter_gen_l10n_locale_file(name: &str) -> bool {
|
|
42
154
|
let Some(locale) = name.strip_suffix(".dart") else {
|
|
43
155
|
return false;
|
|
@@ -86,6 +198,7 @@ pub(crate) fn is_flutterfire_options_path(path: &Path) -> bool {
|
|
|
86
198
|
|
|
87
199
|
#[cfg(test)]
|
|
88
200
|
mod tests {
|
|
201
|
+
use std::fs;
|
|
89
202
|
use std::path::Path;
|
|
90
203
|
|
|
91
204
|
use super::*;
|
|
@@ -98,6 +211,10 @@ mod tests {
|
|
|
98
211
|
"l10n.gen.dart",
|
|
99
212
|
"routes.gr.dart",
|
|
100
213
|
"service.mocks.dart",
|
|
214
|
+
"messages.pb.dart",
|
|
215
|
+
"messages.pbenum.dart",
|
|
216
|
+
"messages.pbjson.dart",
|
|
217
|
+
"messages.pbgrpc.dart",
|
|
101
218
|
] {
|
|
102
219
|
assert!(is_generated_dart_file_name(file_name), "{file_name}");
|
|
103
220
|
assert!(is_generated_dart_path(
|
|
@@ -123,6 +240,7 @@ mod tests {
|
|
|
123
240
|
Path::new("lib/l10n/app_localizations_sr_Latn_RS.dart"),
|
|
124
241
|
Path::new("lib/l10n/app_localizations_es_419.dart"),
|
|
125
242
|
Path::new("lib/gen_l10n/app_localizations_es.dart"),
|
|
243
|
+
Path::new("test/drift/generated/schema_v31.dart"),
|
|
126
244
|
Path::new("lib/firebase_options.dart"),
|
|
127
245
|
Path::new("packages/foo/lib/firebase_options.dart"),
|
|
128
246
|
] {
|
|
@@ -137,10 +255,46 @@ mod tests {
|
|
|
137
255
|
Path::new("lib/l10n/app_localizations_en_US_helper.dart"),
|
|
138
256
|
Path::new("lib/l10n/app_localizations_.dart"),
|
|
139
257
|
Path::new("lib/l10n/app_localizations_en-us.dart"),
|
|
258
|
+
Path::new("test/drift/schema_v31.dart"),
|
|
259
|
+
Path::new("test/drift/generated/schema_version.dart"),
|
|
140
260
|
Path::new("lib/src/firebase_options.dart"),
|
|
141
261
|
Path::new("packages/foo/lib/config/firebase_options.dart"),
|
|
142
262
|
] {
|
|
143
263
|
assert!(!is_generated_dart_path(path), "{}", path.display());
|
|
144
264
|
}
|
|
145
265
|
}
|
|
266
|
+
|
|
267
|
+
#[test]
|
|
268
|
+
fn recognizes_configured_flutter_generated_localizations()
|
|
269
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
270
|
+
let fixture = tempfile::tempdir()?;
|
|
271
|
+
fs::create_dir_all(fixture.path().join("lib/l10n"))?;
|
|
272
|
+
fs::write(
|
|
273
|
+
fixture.path().join("l10n.yaml"),
|
|
274
|
+
"arb-dir: lib/l10n\noutput-localization-file: l10n.dart\n",
|
|
275
|
+
)?;
|
|
276
|
+
|
|
277
|
+
for file_name in ["l10n.dart", "l10n_en.dart", "l10n_zh_Hant.dart"] {
|
|
278
|
+
assert!(is_generated_dart_path(
|
|
279
|
+
&fixture.path().join("lib/l10n").join(file_name)
|
|
280
|
+
));
|
|
281
|
+
}
|
|
282
|
+
assert!(!is_generated_dart_path(
|
|
283
|
+
&fixture.path().join("lib/l10n/l10n_repository.dart")
|
|
284
|
+
));
|
|
285
|
+
|
|
286
|
+
fs::write(
|
|
287
|
+
fixture.path().join("l10n.yaml"),
|
|
288
|
+
"arb-dir: lib/l10n\noutput-localization-file: generated_localizations.dart\n",
|
|
289
|
+
)?;
|
|
290
|
+
assert!(is_generated_dart_path(
|
|
291
|
+
&fixture
|
|
292
|
+
.path()
|
|
293
|
+
.join("lib/l10n/generated_localizations_en.dart")
|
|
294
|
+
));
|
|
295
|
+
assert!(!is_generated_dart_path(
|
|
296
|
+
&fixture.path().join("lib/l10n/l10n_en.dart")
|
|
297
|
+
));
|
|
298
|
+
Ok(())
|
|
299
|
+
}
|
|
146
300
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
use super::FunctionMetrics;
|
|
2
|
+
use super::thresholds::ThresholdContext;
|
|
3
|
+
use super::types::LargeFunction;
|
|
4
|
+
|
|
5
|
+
pub(super) fn large_functions(
|
|
6
|
+
functions: &[FunctionMetrics],
|
|
7
|
+
threshold_context: &mut ThresholdContext,
|
|
8
|
+
) -> Vec<LargeFunction> {
|
|
9
|
+
let mut findings = functions
|
|
10
|
+
.iter()
|
|
11
|
+
.filter_map(|function| {
|
|
12
|
+
let line_count = function
|
|
13
|
+
.end_line
|
|
14
|
+
.saturating_sub(function.location.line)
|
|
15
|
+
.saturating_add(1);
|
|
16
|
+
let thresholds = threshold_context.unit_size_thresholds(function, line_count);
|
|
17
|
+
let max_unit_size = thresholds.effective.max_unit_size?;
|
|
18
|
+
(line_count > max_unit_size).then(|| LargeFunction {
|
|
19
|
+
path: function.path.clone(),
|
|
20
|
+
symbol: function.symbol.clone(),
|
|
21
|
+
kind: function.kind,
|
|
22
|
+
location: function.location,
|
|
23
|
+
end_line: function.end_line,
|
|
24
|
+
line_count,
|
|
25
|
+
max_unit_size,
|
|
26
|
+
threshold_source: thresholds.source,
|
|
27
|
+
threshold_reason: thresholds.reason,
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
.collect::<Vec<_>>();
|
|
31
|
+
|
|
32
|
+
findings.sort_by(|left, right| {
|
|
33
|
+
(
|
|
34
|
+
std::cmp::Reverse(left.line_count),
|
|
35
|
+
&left.path,
|
|
36
|
+
left.location.line,
|
|
37
|
+
left.location.column,
|
|
38
|
+
&left.symbol,
|
|
39
|
+
)
|
|
40
|
+
.cmp(&(
|
|
41
|
+
std::cmp::Reverse(right.line_count),
|
|
42
|
+
&right.path,
|
|
43
|
+
right.location.line,
|
|
44
|
+
right.location.column,
|
|
45
|
+
&right.symbol,
|
|
46
|
+
))
|
|
47
|
+
});
|
|
48
|
+
findings
|
|
49
|
+
}
|
package/src/health/tests.rs
CHANGED
|
@@ -2,7 +2,10 @@ use std::fs;
|
|
|
2
2
|
|
|
3
3
|
use tempfile::TempDir;
|
|
4
4
|
|
|
5
|
-
use crate::{
|
|
5
|
+
use crate::{
|
|
6
|
+
ComplexityFunctionKind, HealthOptions, HealthThresholdOverride, HealthThresholdOverrideStatus,
|
|
7
|
+
analyze_health, scan_project,
|
|
8
|
+
};
|
|
6
9
|
|
|
7
10
|
#[test]
|
|
8
11
|
fn health_counts_branch_constructs() -> Result<(), Box<dyn std::error::Error>> {
|
|
@@ -217,6 +220,192 @@ end_of_record
|
|
|
217
220
|
Ok(())
|
|
218
221
|
}
|
|
219
222
|
|
|
223
|
+
#[test]
|
|
224
|
+
fn reports_functions_above_the_default_unit_size() -> Result<(), Box<dyn std::error::Error>> {
|
|
225
|
+
let fixture = tempfile::tempdir()?;
|
|
226
|
+
write(
|
|
227
|
+
&fixture,
|
|
228
|
+
"lib/main.dart",
|
|
229
|
+
&large_function_source("oversized", 61),
|
|
230
|
+
)?;
|
|
231
|
+
write(
|
|
232
|
+
&fixture,
|
|
233
|
+
"lib/exact.dart",
|
|
234
|
+
&large_function_source("exactLimit", 60),
|
|
235
|
+
)?;
|
|
236
|
+
let project = scan_project(fixture.path())?;
|
|
237
|
+
|
|
238
|
+
let report = analyze_health(&project, &HealthOptions::default())?;
|
|
239
|
+
|
|
240
|
+
assert_eq!(report.large_functions.len(), 1);
|
|
241
|
+
assert_eq!(report.large_functions[0].symbol, "oversized");
|
|
242
|
+
assert_eq!(report.large_functions[0].line_count, 61);
|
|
243
|
+
assert_eq!(report.large_functions[0].max_unit_size, 60);
|
|
244
|
+
assert_eq!(
|
|
245
|
+
report.large_functions[0].kind,
|
|
246
|
+
ComplexityFunctionKind::Function
|
|
247
|
+
);
|
|
248
|
+
assert!(report.complexity.is_empty());
|
|
249
|
+
|
|
250
|
+
Ok(())
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
#[test]
|
|
254
|
+
fn unit_size_override_can_suppress_a_matching_function() -> Result<(), Box<dyn std::error::Error>> {
|
|
255
|
+
let fixture = tempfile::tempdir()?;
|
|
256
|
+
write(
|
|
257
|
+
&fixture,
|
|
258
|
+
"lib/main.dart",
|
|
259
|
+
&large_function_source("legacyRoute", 61),
|
|
260
|
+
)?;
|
|
261
|
+
let project = scan_project(fixture.path())?;
|
|
262
|
+
let options = HealthOptions {
|
|
263
|
+
threshold_overrides: vec![HealthThresholdOverride {
|
|
264
|
+
files: vec!["lib/main.dart".to_owned()],
|
|
265
|
+
functions: vec!["legacyRoute".to_owned()],
|
|
266
|
+
max_cyclomatic: None,
|
|
267
|
+
max_cognitive: None,
|
|
268
|
+
max_crap: None,
|
|
269
|
+
max_unit_size: Some(120),
|
|
270
|
+
reason: Some("framework adapter".to_owned()),
|
|
271
|
+
}],
|
|
272
|
+
..HealthOptions::default()
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
let report = analyze_health(&project, &options)?;
|
|
276
|
+
|
|
277
|
+
assert!(report.large_functions.is_empty());
|
|
278
|
+
assert_eq!(
|
|
279
|
+
report.threshold_overrides[0].status,
|
|
280
|
+
HealthThresholdOverrideStatus::Active
|
|
281
|
+
);
|
|
282
|
+
assert_eq!(
|
|
283
|
+
report.threshold_overrides[0].matched_functions,
|
|
284
|
+
["lib/main.dart:legacyRoute"]
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
Ok(())
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
#[test]
|
|
291
|
+
fn classifies_only_flutter_widget_build_methods() -> Result<(), Box<dyn std::error::Error>> {
|
|
292
|
+
let fixture = tempfile::tempdir()?;
|
|
293
|
+
write(
|
|
294
|
+
&fixture,
|
|
295
|
+
"lib/main.dart",
|
|
296
|
+
&format!(
|
|
297
|
+
"{}\n\n{}\n\n{}\n\n{}\n\n{}",
|
|
298
|
+
large_build_method_source("Dashboard", "StatelessWidget", 61),
|
|
299
|
+
large_build_method_source("Presenter", "Object", 61),
|
|
300
|
+
large_build_method_source("DashboardState", "State<Dashboard>", 61),
|
|
301
|
+
large_build_method_source("StatefulOwner", "StatefulWidget", 61),
|
|
302
|
+
large_build_method_source("ConsumerStatefulOwner", "ConsumerStatefulWidget", 61)
|
|
303
|
+
),
|
|
304
|
+
)?;
|
|
305
|
+
let project = scan_project(fixture.path())?;
|
|
306
|
+
|
|
307
|
+
let report = analyze_health(&project, &HealthOptions::default())?;
|
|
308
|
+
|
|
309
|
+
assert_eq!(report.large_functions.len(), 5);
|
|
310
|
+
let dashboard = &report.large_functions[0];
|
|
311
|
+
assert_eq!(dashboard.kind, ComplexityFunctionKind::FlutterBuildMethod);
|
|
312
|
+
let presenter = &report.large_functions[1];
|
|
313
|
+
assert_eq!(presenter.kind, ComplexityFunctionKind::Method);
|
|
314
|
+
assert_eq!(
|
|
315
|
+
report.large_functions[2].kind,
|
|
316
|
+
ComplexityFunctionKind::FlutterBuildMethod
|
|
317
|
+
);
|
|
318
|
+
assert_eq!(
|
|
319
|
+
report.large_functions[3].kind,
|
|
320
|
+
ComplexityFunctionKind::Method
|
|
321
|
+
);
|
|
322
|
+
assert_eq!(
|
|
323
|
+
report.large_functions[4].kind,
|
|
324
|
+
ComplexityFunctionKind::Method
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
Ok(())
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
#[test]
|
|
331
|
+
fn classifies_build_methods_through_project_widget_inheritance()
|
|
332
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
333
|
+
let fixture = tempfile::tempdir()?;
|
|
334
|
+
write(
|
|
335
|
+
&fixture,
|
|
336
|
+
"lib/base.dart",
|
|
337
|
+
"abstract class AppWidget extends StatelessWidget {}\n",
|
|
338
|
+
)?;
|
|
339
|
+
write(
|
|
340
|
+
&fixture,
|
|
341
|
+
"lib/main.dart",
|
|
342
|
+
&format!(
|
|
343
|
+
"import 'base.dart';\n\n{}",
|
|
344
|
+
large_build_method_source("Dashboard", "AppWidget", 61)
|
|
345
|
+
),
|
|
346
|
+
)?;
|
|
347
|
+
let project = scan_project(fixture.path())?;
|
|
348
|
+
let parse_count = crate::dart_parser::track_parse_count(&fixture.path().join("lib/main.dart"));
|
|
349
|
+
|
|
350
|
+
let report = analyze_health(&project, &HealthOptions::default())?;
|
|
351
|
+
|
|
352
|
+
assert_eq!(report.large_functions.len(), 1);
|
|
353
|
+
assert_eq!(
|
|
354
|
+
report.large_functions[0].kind,
|
|
355
|
+
ComplexityFunctionKind::FlutterBuildMethod
|
|
356
|
+
);
|
|
357
|
+
assert_eq!(
|
|
358
|
+
parse_count.count(),
|
|
359
|
+
1,
|
|
360
|
+
"health analysis must parse each Dart source only once"
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
Ok(())
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
#[test]
|
|
367
|
+
fn classifies_animated_widget_build_methods() -> Result<(), Box<dyn std::error::Error>> {
|
|
368
|
+
let fixture = tempfile::tempdir()?;
|
|
369
|
+
write(
|
|
370
|
+
&fixture,
|
|
371
|
+
"lib/main.dart",
|
|
372
|
+
&large_build_method_source("Spinner", "AnimatedWidget", 61),
|
|
373
|
+
)?;
|
|
374
|
+
let project = scan_project(fixture.path())?;
|
|
375
|
+
|
|
376
|
+
let report = analyze_health(&project, &HealthOptions::default())?;
|
|
377
|
+
|
|
378
|
+
assert_eq!(report.large_functions.len(), 1);
|
|
379
|
+
assert_eq!(
|
|
380
|
+
report.large_functions[0].kind,
|
|
381
|
+
ComplexityFunctionKind::FlutterBuildMethod
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
Ok(())
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
#[test]
|
|
388
|
+
fn does_not_classify_build_methods_in_test_named_sources() -> Result<(), Box<dyn std::error::Error>>
|
|
389
|
+
{
|
|
390
|
+
let fixture = tempfile::tempdir()?;
|
|
391
|
+
write(
|
|
392
|
+
&fixture,
|
|
393
|
+
"lib/dashboard_test.dart",
|
|
394
|
+
&large_build_method_source("Dashboard", "StatelessWidget", 61),
|
|
395
|
+
)?;
|
|
396
|
+
let project = scan_project(fixture.path())?;
|
|
397
|
+
|
|
398
|
+
let report = analyze_health(&project, &HealthOptions::default())?;
|
|
399
|
+
|
|
400
|
+
assert_eq!(report.large_functions.len(), 1);
|
|
401
|
+
assert_eq!(
|
|
402
|
+
report.large_functions[0].kind,
|
|
403
|
+
ComplexityFunctionKind::Method
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
Ok(())
|
|
407
|
+
}
|
|
408
|
+
|
|
220
409
|
fn write_coverage_source(fixture: &TempDir) -> Result<(), std::io::Error> {
|
|
221
410
|
write(
|
|
222
411
|
fixture,
|
|
@@ -231,6 +420,28 @@ fn write_coverage_source(fixture: &TempDir) -> Result<(), std::io::Error> {
|
|
|
231
420
|
)
|
|
232
421
|
}
|
|
233
422
|
|
|
423
|
+
fn large_function_source(name: &str, lines: usize) -> String {
|
|
424
|
+
let mut source = vec![format!("void {name}() {{")];
|
|
425
|
+
source.extend(
|
|
426
|
+
(0..lines.saturating_sub(2)).map(|index| format!(" final value{index} = {index};")),
|
|
427
|
+
);
|
|
428
|
+
source.push("}".to_owned());
|
|
429
|
+
source.join("\n")
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
fn large_build_method_source(class_name: &str, base: &str, lines: usize) -> String {
|
|
433
|
+
let mut source = vec![
|
|
434
|
+
format!("class {class_name} extends {base} {{"),
|
|
435
|
+
" Widget build(BuildContext context) {".to_owned(),
|
|
436
|
+
];
|
|
437
|
+
source.extend(
|
|
438
|
+
(0..lines.saturating_sub(2)).map(|index| format!(" final value{index} = {index};")),
|
|
439
|
+
);
|
|
440
|
+
source.push(" }".to_owned());
|
|
441
|
+
source.push("}".to_owned());
|
|
442
|
+
source.join("\n")
|
|
443
|
+
}
|
|
444
|
+
|
|
234
445
|
fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
|
|
235
446
|
let path = fixture.path().join(path);
|
|
236
447
|
if let Some(parent) = path.parent() {
|
|
@@ -18,6 +18,9 @@ pub struct HealthThresholdOverride {
|
|
|
18
18
|
/// Local CRAP ceiling.
|
|
19
19
|
#[serde(default, alias = "maxCrap")]
|
|
20
20
|
pub max_crap: Option<usize>,
|
|
21
|
+
/// Local inclusive physical-line ceiling.
|
|
22
|
+
#[serde(default, alias = "maxUnitSize")]
|
|
23
|
+
pub max_unit_size: Option<usize>,
|
|
21
24
|
/// Reason shown to agents when this override is active.
|
|
22
25
|
#[serde(default)]
|
|
23
26
|
pub reason: Option<String>,
|
|
@@ -27,7 +30,10 @@ impl HealthThresholdOverride {
|
|
|
27
30
|
/// Whether this override contains at least one local ceiling.
|
|
28
31
|
#[must_use]
|
|
29
32
|
pub const fn has_threshold(&self) -> bool {
|
|
30
|
-
self.max_cyclomatic.is_some()
|
|
33
|
+
self.max_cyclomatic.is_some()
|
|
34
|
+
|| self.max_cognitive.is_some()
|
|
35
|
+
|| self.max_crap.is_some()
|
|
36
|
+
|| self.max_unit_size.is_some()
|
|
31
37
|
}
|
|
32
38
|
|
|
33
39
|
pub(super) const fn has_static_threshold(&self) -> bool {
|
|
@@ -37,6 +43,10 @@ impl HealthThresholdOverride {
|
|
|
37
43
|
pub(super) const fn has_crap_threshold(&self) -> bool {
|
|
38
44
|
self.max_crap.is_some()
|
|
39
45
|
}
|
|
46
|
+
|
|
47
|
+
pub(super) const fn has_unit_size_threshold(&self) -> bool {
|
|
48
|
+
self.max_unit_size.is_some()
|
|
49
|
+
}
|
|
40
50
|
}
|
|
41
51
|
|
|
42
52
|
/// Status for one configured threshold override.
|
|
@@ -66,6 +76,8 @@ pub struct HealthThresholdOverrideReport {
|
|
|
66
76
|
pub max_cognitive: Option<usize>,
|
|
67
77
|
/// Local CRAP ceiling.
|
|
68
78
|
pub max_crap: Option<usize>,
|
|
79
|
+
/// Local inclusive physical-line ceiling.
|
|
80
|
+
pub max_unit_size: Option<usize>,
|
|
69
81
|
/// Configured reason.
|
|
70
82
|
pub reason: Option<String>,
|
|
71
83
|
/// Override status.
|
|
@@ -83,6 +95,8 @@ pub struct EffectiveThresholds {
|
|
|
83
95
|
pub max_cognitive: Option<usize>,
|
|
84
96
|
/// CRAP ceiling used for this function.
|
|
85
97
|
pub max_crap: Option<usize>,
|
|
98
|
+
/// Inclusive physical-line ceiling used for this function.
|
|
99
|
+
pub max_unit_size: Option<usize>,
|
|
86
100
|
}
|
|
87
101
|
|
|
88
102
|
/// Source of the effective thresholds.
|
|
@@ -110,6 +124,7 @@ impl AppliedThresholds {
|
|
|
110
124
|
max_cyclomatic: Some(max_cyclomatic),
|
|
111
125
|
max_cognitive: Some(max_cognitive),
|
|
112
126
|
max_crap: None,
|
|
127
|
+
max_unit_size: None,
|
|
113
128
|
},
|
|
114
129
|
}
|
|
115
130
|
}
|
|
@@ -122,6 +137,20 @@ impl AppliedThresholds {
|
|
|
122
137
|
max_cyclomatic: None,
|
|
123
138
|
max_cognitive: None,
|
|
124
139
|
max_crap: Some(max_crap),
|
|
140
|
+
max_unit_size: None,
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
pub(super) fn default_unit_size(max_unit_size: usize) -> Self {
|
|
146
|
+
Self {
|
|
147
|
+
source: None,
|
|
148
|
+
reason: None,
|
|
149
|
+
effective: EffectiveThresholds {
|
|
150
|
+
max_cyclomatic: None,
|
|
151
|
+
max_cognitive: None,
|
|
152
|
+
max_crap: None,
|
|
153
|
+
max_unit_size: Some(max_unit_size),
|
|
125
154
|
},
|
|
126
155
|
}
|
|
127
156
|
}
|
|
@@ -140,6 +169,7 @@ pub(super) fn override_report(
|
|
|
140
169
|
max_cyclomatic: rule.max_cyclomatic,
|
|
141
170
|
max_cognitive: rule.max_cognitive,
|
|
142
171
|
max_crap: rule.max_crap,
|
|
172
|
+
max_unit_size: rule.max_unit_size,
|
|
143
173
|
reason: rule.reason.clone(),
|
|
144
174
|
status,
|
|
145
175
|
matched_functions,
|
package/src/health/thresholds.rs
CHANGED
|
@@ -16,6 +16,7 @@ pub(super) struct ThresholdContext {
|
|
|
16
16
|
max_cyclomatic: usize,
|
|
17
17
|
max_cognitive: usize,
|
|
18
18
|
max_crap: Option<usize>,
|
|
19
|
+
max_unit_size: usize,
|
|
19
20
|
overrides: Vec<CompiledOverride>,
|
|
20
21
|
states: Vec<OverrideState>,
|
|
21
22
|
}
|
|
@@ -32,6 +33,7 @@ impl ThresholdContext {
|
|
|
32
33
|
max_cyclomatic: options.max_cyclomatic,
|
|
33
34
|
max_cognitive: options.max_cognitive,
|
|
34
35
|
max_crap: options.max_crap,
|
|
36
|
+
max_unit_size: options.max_unit_size,
|
|
35
37
|
states: vec![OverrideState::default(); overrides.len()],
|
|
36
38
|
overrides,
|
|
37
39
|
}
|
|
@@ -54,6 +56,7 @@ impl ThresholdContext {
|
|
|
54
56
|
max_cyclomatic: Some(max_cyclomatic),
|
|
55
57
|
max_cognitive: Some(max_cognitive),
|
|
56
58
|
max_crap: None,
|
|
59
|
+
max_unit_size: None,
|
|
57
60
|
},
|
|
58
61
|
};
|
|
59
62
|
if self.static_active(function, &thresholds) {
|
|
@@ -89,6 +92,7 @@ impl ThresholdContext {
|
|
|
89
92
|
max_cyclomatic: None,
|
|
90
93
|
max_cognitive: None,
|
|
91
94
|
max_crap: Some(max_crap),
|
|
95
|
+
max_unit_size: None,
|
|
92
96
|
},
|
|
93
97
|
};
|
|
94
98
|
if self.crap_active(crap_score, max_crap) {
|
|
@@ -105,6 +109,40 @@ impl ThresholdContext {
|
|
|
105
109
|
applied
|
|
106
110
|
}
|
|
107
111
|
|
|
112
|
+
pub(super) fn unit_size_thresholds(
|
|
113
|
+
&mut self,
|
|
114
|
+
function: &FunctionMetrics,
|
|
115
|
+
line_count: usize,
|
|
116
|
+
) -> AppliedThresholds {
|
|
117
|
+
let global = AppliedThresholds::default_unit_size(self.max_unit_size);
|
|
118
|
+
let mut applied = None;
|
|
119
|
+
|
|
120
|
+
for index in self.unit_size_matches(function) {
|
|
121
|
+
let rule = &self.overrides[index].rule;
|
|
122
|
+
let max_unit_size = rule.max_unit_size.unwrap_or(self.max_unit_size);
|
|
123
|
+
let reason = rule.reason.clone();
|
|
124
|
+
self.mark_matched(index, function);
|
|
125
|
+
let thresholds = AppliedThresholds {
|
|
126
|
+
source: Some(ThresholdSource::Override),
|
|
127
|
+
reason,
|
|
128
|
+
effective: EffectiveThresholds {
|
|
129
|
+
max_cyclomatic: None,
|
|
130
|
+
max_cognitive: None,
|
|
131
|
+
max_crap: None,
|
|
132
|
+
max_unit_size: Some(max_unit_size),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
if line_count > self.max_unit_size || line_count > max_unit_size {
|
|
136
|
+
self.states[index].active = true;
|
|
137
|
+
}
|
|
138
|
+
if applied.is_none() {
|
|
139
|
+
applied = Some(thresholds);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
applied.unwrap_or(global)
|
|
144
|
+
}
|
|
145
|
+
|
|
108
146
|
pub(super) fn reports(&self) -> Vec<HealthThresholdOverrideReport> {
|
|
109
147
|
self.overrides
|
|
110
148
|
.iter()
|
|
@@ -143,6 +181,17 @@ impl ThresholdContext {
|
|
|
143
181
|
.collect()
|
|
144
182
|
}
|
|
145
183
|
|
|
184
|
+
fn unit_size_matches(&self, function: &FunctionMetrics) -> Vec<usize> {
|
|
185
|
+
self.overrides
|
|
186
|
+
.iter()
|
|
187
|
+
.enumerate()
|
|
188
|
+
.filter(|(_, compiled)| {
|
|
189
|
+
compiled.rule.has_unit_size_threshold() && compiled.matches(&self.root, function)
|
|
190
|
+
})
|
|
191
|
+
.map(|(index, _)| index)
|
|
192
|
+
.collect()
|
|
193
|
+
}
|
|
194
|
+
|
|
146
195
|
fn static_active(&self, function: &FunctionMetrics, thresholds: &AppliedThresholds) -> bool {
|
|
147
196
|
let max_cyclomatic = thresholds
|
|
148
197
|
.effective
|