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
package/src/widgets.rs
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::path::{Path, PathBuf};
|
|
4
|
+
|
|
5
|
+
use rayon::prelude::*;
|
|
6
|
+
use serde::{Deserialize, Serialize};
|
|
7
|
+
use thiserror::Error;
|
|
8
|
+
use tree_sitter::Node;
|
|
9
|
+
|
|
10
|
+
use crate::generated::is_generated_dart_path;
|
|
11
|
+
use crate::graph::normalize_against;
|
|
12
|
+
use crate::{DeadCodeReport, Location, ScannedProject};
|
|
13
|
+
|
|
14
|
+
mod lifecycle;
|
|
15
|
+
mod params;
|
|
16
|
+
mod top_level;
|
|
17
|
+
mod unrendered;
|
|
18
|
+
|
|
19
|
+
pub use lifecycle::MissingContextMountedAfterAwait;
|
|
20
|
+
use lifecycle::lifecycle_findings;
|
|
21
|
+
use params::constructor_params;
|
|
22
|
+
use top_level::top_level_widget_functions;
|
|
23
|
+
use unrendered::unrendered_widgets;
|
|
24
|
+
|
|
25
|
+
/// Flutter widget framework analysis.
|
|
26
|
+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
27
|
+
pub struct WidgetReport {
|
|
28
|
+
/// Dart files included in widget analysis.
|
|
29
|
+
pub analyzed_files: usize,
|
|
30
|
+
/// Widget constructor parameters that are never read.
|
|
31
|
+
pub unused_params: Vec<UnusedWidgetParam>,
|
|
32
|
+
/// Private Flutter widget classes.
|
|
33
|
+
pub private_widget_classes: Vec<PrivateWidgetClass>,
|
|
34
|
+
/// Top-level Flutter widget helper functions.
|
|
35
|
+
pub top_level_functions: Vec<WidgetTopLevelFunction>,
|
|
36
|
+
/// Flutter widget classes with no reachable object construction.
|
|
37
|
+
pub unrendered_widgets: Vec<UnrenderedWidgetClass>,
|
|
38
|
+
/// Widget or `State` awaits missing an immediate `context.mounted` guard.
|
|
39
|
+
pub missing_context_mounted_after_await: Vec<MissingContextMountedAfterAwait>,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// A widget constructor parameter that is not used by the widget.
|
|
43
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
44
|
+
pub struct UnusedWidgetParam {
|
|
45
|
+
/// Dart file containing the widget declaration.
|
|
46
|
+
pub path: PathBuf,
|
|
47
|
+
/// Widget class that owns the parameter.
|
|
48
|
+
pub widget_class: String,
|
|
49
|
+
/// Flutter widget base class.
|
|
50
|
+
pub widget_kind: WidgetClassKind,
|
|
51
|
+
/// Constructor parameter name.
|
|
52
|
+
pub param_name: String,
|
|
53
|
+
/// Location of the constructor parameter identifier.
|
|
54
|
+
pub location: Location,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// A private class that extends a Flutter widget base.
|
|
58
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
59
|
+
pub struct PrivateWidgetClass {
|
|
60
|
+
/// Dart file containing the widget declaration.
|
|
61
|
+
pub path: PathBuf,
|
|
62
|
+
/// Private widget class name.
|
|
63
|
+
pub widget_class: String,
|
|
64
|
+
/// Flutter widget base class.
|
|
65
|
+
pub widget_kind: WidgetClassKind,
|
|
66
|
+
/// Location of the class identifier.
|
|
67
|
+
pub location: Location,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// A top-level function that should be owned by a widget class or helper owner.
|
|
71
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
72
|
+
pub struct WidgetTopLevelFunction {
|
|
73
|
+
/// Dart file containing the function declaration.
|
|
74
|
+
pub path: PathBuf,
|
|
75
|
+
/// Top-level function name.
|
|
76
|
+
pub function_name: String,
|
|
77
|
+
/// Function return type when declared.
|
|
78
|
+
pub return_type: Option<String>,
|
|
79
|
+
/// Location of the function identifier.
|
|
80
|
+
pub location: Location,
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// A widget class that is never constructed from reachable production code.
|
|
84
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
85
|
+
pub struct UnrenderedWidgetClass {
|
|
86
|
+
/// Dart file containing the widget declaration.
|
|
87
|
+
pub path: PathBuf,
|
|
88
|
+
/// Widget class name.
|
|
89
|
+
pub widget_class: String,
|
|
90
|
+
/// Flutter widget base class.
|
|
91
|
+
pub widget_kind: WidgetClassKind,
|
|
92
|
+
/// Location of the class identifier.
|
|
93
|
+
pub location: Location,
|
|
94
|
+
/// Reachable object construction references for this widget.
|
|
95
|
+
pub render_reference_count: usize,
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Supported Flutter widget base classes.
|
|
99
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
100
|
+
#[serde(rename_all = "kebab-case")]
|
|
101
|
+
pub enum WidgetClassKind {
|
|
102
|
+
StatelessWidget,
|
|
103
|
+
StatefulWidget,
|
|
104
|
+
ConsumerWidget,
|
|
105
|
+
ConsumerStatefulWidget,
|
|
106
|
+
HookWidget,
|
|
107
|
+
HookConsumerWidget,
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// Errors returned while analyzing Flutter widgets.
|
|
111
|
+
#[derive(Debug, Error)]
|
|
112
|
+
pub enum WidgetAnalysisError {
|
|
113
|
+
/// A Dart file could not be read.
|
|
114
|
+
#[error("failed to read Dart file {path}: {source}")]
|
|
115
|
+
ReadFile {
|
|
116
|
+
/// File path.
|
|
117
|
+
path: PathBuf,
|
|
118
|
+
/// Underlying IO error.
|
|
119
|
+
source: std::io::Error,
|
|
120
|
+
},
|
|
121
|
+
/// Tree-Sitter rejected the Dart grammar.
|
|
122
|
+
#[error("failed to load Dart grammar: {0}")]
|
|
123
|
+
Language(#[from] tree_sitter::LanguageError),
|
|
124
|
+
/// Tree-Sitter did not produce a parse tree.
|
|
125
|
+
#[error("tree-sitter did not return a parse tree for {path}")]
|
|
126
|
+
ParseCancelled {
|
|
127
|
+
/// Path being parsed.
|
|
128
|
+
path: PathBuf,
|
|
129
|
+
},
|
|
130
|
+
/// The source parsed with syntax errors.
|
|
131
|
+
#[error("Dart syntax errors found in {path}")]
|
|
132
|
+
Syntax {
|
|
133
|
+
/// Path being parsed.
|
|
134
|
+
path: PathBuf,
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Detect Flutter widget framework issues.
|
|
139
|
+
///
|
|
140
|
+
/// # Errors
|
|
141
|
+
///
|
|
142
|
+
/// Returns [`WidgetAnalysisError`] if a scanned Dart file cannot be read or
|
|
143
|
+
/// parsed during widget analysis.
|
|
144
|
+
pub fn analyze_widgets(
|
|
145
|
+
project: &ScannedProject,
|
|
146
|
+
dead_code: Option<&DeadCodeReport>,
|
|
147
|
+
) -> Result<WidgetReport, WidgetAnalysisError> {
|
|
148
|
+
let paths = widget_analysis_paths(project, dead_code);
|
|
149
|
+
let file_findings = paths
|
|
150
|
+
.par_iter()
|
|
151
|
+
.map(|path| analyze_file(path))
|
|
152
|
+
.collect::<Result<Vec<_>, _>>()?;
|
|
153
|
+
let mut findings = merge_file_widget_findings(file_findings);
|
|
154
|
+
sort_file_widget_findings(&mut findings);
|
|
155
|
+
let unrendered_widgets = unrendered_widgets(project, &paths)?;
|
|
156
|
+
|
|
157
|
+
Ok(WidgetReport {
|
|
158
|
+
analyzed_files: paths.len(),
|
|
159
|
+
unused_params: findings.unused_params,
|
|
160
|
+
private_widget_classes: findings.private_widget_classes,
|
|
161
|
+
top_level_functions: findings.top_level_functions,
|
|
162
|
+
unrendered_widgets,
|
|
163
|
+
missing_context_mounted_after_await: findings.missing_context_mounted_after_await,
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
fn widget_analysis_paths(
|
|
168
|
+
project: &ScannedProject,
|
|
169
|
+
dead_code: Option<&DeadCodeReport>,
|
|
170
|
+
) -> Vec<PathBuf> {
|
|
171
|
+
let dead_files = dead_code
|
|
172
|
+
.map(|report| {
|
|
173
|
+
report
|
|
174
|
+
.dead_files
|
|
175
|
+
.iter()
|
|
176
|
+
.map(|dead| dead.path.clone())
|
|
177
|
+
.collect::<BTreeSet<_>>()
|
|
178
|
+
})
|
|
179
|
+
.unwrap_or_default();
|
|
180
|
+
project
|
|
181
|
+
.files
|
|
182
|
+
.iter()
|
|
183
|
+
.map(|file| normalize_against(&project.root, &file.path))
|
|
184
|
+
.filter(|path| path.starts_with(&project.root))
|
|
185
|
+
.filter(|path| !dead_files.contains(path))
|
|
186
|
+
.filter(|path| !is_generated_dart_path(path) && !is_test_path(path))
|
|
187
|
+
.collect()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn merge_file_widget_findings(file_findings: Vec<FileWidgetFindings>) -> FileWidgetFindings {
|
|
191
|
+
let mut merged = FileWidgetFindings::default();
|
|
192
|
+
for mut findings in file_findings {
|
|
193
|
+
merged.unused_params.append(&mut findings.unused_params);
|
|
194
|
+
merged
|
|
195
|
+
.private_widget_classes
|
|
196
|
+
.append(&mut findings.private_widget_classes);
|
|
197
|
+
merged
|
|
198
|
+
.top_level_functions
|
|
199
|
+
.append(&mut findings.top_level_functions);
|
|
200
|
+
merged
|
|
201
|
+
.missing_context_mounted_after_await
|
|
202
|
+
.append(&mut findings.missing_context_mounted_after_await);
|
|
203
|
+
}
|
|
204
|
+
merged
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn sort_file_widget_findings(findings: &mut FileWidgetFindings) {
|
|
208
|
+
sort_widget_core_findings(findings);
|
|
209
|
+
sort_lifecycle_findings(findings);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
fn sort_widget_core_findings(findings: &mut FileWidgetFindings) {
|
|
213
|
+
findings.unused_params.sort_by(|left, right| {
|
|
214
|
+
(
|
|
215
|
+
&left.path,
|
|
216
|
+
left.location.line,
|
|
217
|
+
left.location.column,
|
|
218
|
+
&left.widget_class,
|
|
219
|
+
&left.param_name,
|
|
220
|
+
)
|
|
221
|
+
.cmp(&(
|
|
222
|
+
&right.path,
|
|
223
|
+
right.location.line,
|
|
224
|
+
right.location.column,
|
|
225
|
+
&right.widget_class,
|
|
226
|
+
&right.param_name,
|
|
227
|
+
))
|
|
228
|
+
});
|
|
229
|
+
findings.private_widget_classes.sort_by(|left, right| {
|
|
230
|
+
(
|
|
231
|
+
&left.path,
|
|
232
|
+
left.location.line,
|
|
233
|
+
left.location.column,
|
|
234
|
+
&left.widget_class,
|
|
235
|
+
)
|
|
236
|
+
.cmp(&(
|
|
237
|
+
&right.path,
|
|
238
|
+
right.location.line,
|
|
239
|
+
right.location.column,
|
|
240
|
+
&right.widget_class,
|
|
241
|
+
))
|
|
242
|
+
});
|
|
243
|
+
findings.top_level_functions.sort_by(|left, right| {
|
|
244
|
+
(
|
|
245
|
+
&left.path,
|
|
246
|
+
left.location.line,
|
|
247
|
+
left.location.column,
|
|
248
|
+
&left.function_name,
|
|
249
|
+
)
|
|
250
|
+
.cmp(&(
|
|
251
|
+
&right.path,
|
|
252
|
+
right.location.line,
|
|
253
|
+
right.location.column,
|
|
254
|
+
&right.function_name,
|
|
255
|
+
))
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
fn sort_lifecycle_findings(findings: &mut FileWidgetFindings) {
|
|
260
|
+
findings
|
|
261
|
+
.missing_context_mounted_after_await
|
|
262
|
+
.sort_by(|left, right| {
|
|
263
|
+
(
|
|
264
|
+
&left.path,
|
|
265
|
+
left.location.line,
|
|
266
|
+
left.location.column,
|
|
267
|
+
&left.owner,
|
|
268
|
+
)
|
|
269
|
+
.cmp(&(
|
|
270
|
+
&right.path,
|
|
271
|
+
right.location.line,
|
|
272
|
+
right.location.column,
|
|
273
|
+
&right.owner,
|
|
274
|
+
))
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
fn analyze_file(path: &Path) -> Result<FileWidgetFindings, WidgetAnalysisError> {
|
|
279
|
+
let source = fs::read_to_string(path).map_err(|source| WidgetAnalysisError::ReadFile {
|
|
280
|
+
path: path.to_path_buf(),
|
|
281
|
+
source,
|
|
282
|
+
})?;
|
|
283
|
+
let parsed = parse_tree(path, &source)?;
|
|
284
|
+
let root = parsed.tree().root_node();
|
|
285
|
+
|
|
286
|
+
Ok(findings_in_source(path, root, parsed.source()))
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
fn parse_tree<'source>(
|
|
290
|
+
path: &Path,
|
|
291
|
+
source: &'source str,
|
|
292
|
+
) -> Result<crate::dart_parser::ParsedDart<'source>, WidgetAnalysisError> {
|
|
293
|
+
crate::dart_parser::parse_dart_source_strict(path, source).map_err(widget_parse_error)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn widget_parse_error(error: crate::dart_parser::DartParseError) -> WidgetAnalysisError {
|
|
297
|
+
match error {
|
|
298
|
+
crate::dart_parser::DartParseError::Language(source) => {
|
|
299
|
+
WidgetAnalysisError::Language(source)
|
|
300
|
+
}
|
|
301
|
+
crate::dart_parser::DartParseError::ParseCancelled { path } => {
|
|
302
|
+
WidgetAnalysisError::ParseCancelled { path }
|
|
303
|
+
}
|
|
304
|
+
crate::dart_parser::DartParseError::Syntax { path } => WidgetAnalysisError::Syntax { path },
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
|
309
|
+
struct FileWidgetFindings {
|
|
310
|
+
unused_params: Vec<UnusedWidgetParam>,
|
|
311
|
+
private_widget_classes: Vec<PrivateWidgetClass>,
|
|
312
|
+
top_level_functions: Vec<WidgetTopLevelFunction>,
|
|
313
|
+
missing_context_mounted_after_await: Vec<MissingContextMountedAfterAwait>,
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
fn findings_in_source(path: &Path, root: Node<'_>, source: &str) -> FileWidgetFindings {
|
|
317
|
+
let mut classes = Vec::new();
|
|
318
|
+
collect_class_declarations(root, &mut classes);
|
|
319
|
+
let states = state_classes_by_widget(&classes, source);
|
|
320
|
+
let mut findings = FileWidgetFindings::default();
|
|
321
|
+
let has_widget_class = classes
|
|
322
|
+
.iter()
|
|
323
|
+
.any(|class| widget_kind(*class, source).is_some());
|
|
324
|
+
findings.top_level_functions = top_level_widget_functions(path, root, source, has_widget_class);
|
|
325
|
+
let lifecycle = lifecycle_findings(path, &classes, source);
|
|
326
|
+
findings.missing_context_mounted_after_await = lifecycle.missing_context_mounted_after_await;
|
|
327
|
+
|
|
328
|
+
for class in classes {
|
|
329
|
+
let Some(widget_kind) = widget_kind(class, source) else {
|
|
330
|
+
continue;
|
|
331
|
+
};
|
|
332
|
+
let Some(name_node) = class.child_by_field_name("name") else {
|
|
333
|
+
continue;
|
|
334
|
+
};
|
|
335
|
+
let Ok(widget_class) = name_node.utf8_text(source.as_bytes()).map(str::to_owned) else {
|
|
336
|
+
continue;
|
|
337
|
+
};
|
|
338
|
+
if widget_class.starts_with('_') {
|
|
339
|
+
findings.private_widget_classes.push(PrivateWidgetClass {
|
|
340
|
+
path: path.to_path_buf(),
|
|
341
|
+
widget_class: widget_class.clone(),
|
|
342
|
+
widget_kind,
|
|
343
|
+
location: name_node.start_position().into(),
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
let Some(body) = class.child_by_field_name("body") else {
|
|
347
|
+
continue;
|
|
348
|
+
};
|
|
349
|
+
for param in constructor_params(class, &widget_class, source) {
|
|
350
|
+
if widget_body_uses_param(body, ¶m.field_name, source)
|
|
351
|
+
|| states.get(&widget_class).is_some_and(|state_bodies| {
|
|
352
|
+
state_bodies.iter().any(|state_body| {
|
|
353
|
+
state_body_uses_param(*state_body, ¶m.field_name, source)
|
|
354
|
+
})
|
|
355
|
+
})
|
|
356
|
+
{
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
findings.unused_params.push(UnusedWidgetParam {
|
|
360
|
+
path: path.to_path_buf(),
|
|
361
|
+
widget_class: widget_class.clone(),
|
|
362
|
+
widget_kind,
|
|
363
|
+
param_name: param.name,
|
|
364
|
+
location: param.location,
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
findings
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
fn collect_class_declarations<'tree>(node: Node<'tree>, classes: &mut Vec<Node<'tree>>) {
|
|
373
|
+
if node.kind() == "class_declaration" {
|
|
374
|
+
classes.push(node);
|
|
375
|
+
}
|
|
376
|
+
let mut cursor = node.walk();
|
|
377
|
+
for child in node.named_children(&mut cursor) {
|
|
378
|
+
collect_class_declarations(child, classes);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
fn state_classes_by_widget<'tree>(
|
|
383
|
+
classes: &[Node<'tree>],
|
|
384
|
+
source: &str,
|
|
385
|
+
) -> BTreeMap<String, Vec<Node<'tree>>> {
|
|
386
|
+
let mut states = BTreeMap::<String, Vec<Node<'tree>>>::new();
|
|
387
|
+
for class in classes {
|
|
388
|
+
let Some(widget_class) = state_widget_class(*class, source) else {
|
|
389
|
+
continue;
|
|
390
|
+
};
|
|
391
|
+
if let Some(body) = class.child_by_field_name("body") {
|
|
392
|
+
states.entry(widget_class).or_default().push(body);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
states
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
pub(super) fn widget_kind(class: Node<'_>, source: &str) -> Option<WidgetClassKind> {
|
|
399
|
+
let base = superclass_base_name(class, source)?;
|
|
400
|
+
match base.as_str() {
|
|
401
|
+
"StatelessWidget" => Some(WidgetClassKind::StatelessWidget),
|
|
402
|
+
"StatefulWidget" => Some(WidgetClassKind::StatefulWidget),
|
|
403
|
+
"ConsumerWidget" => Some(WidgetClassKind::ConsumerWidget),
|
|
404
|
+
"ConsumerStatefulWidget" => Some(WidgetClassKind::ConsumerStatefulWidget),
|
|
405
|
+
"HookWidget" => Some(WidgetClassKind::HookWidget),
|
|
406
|
+
"HookConsumerWidget" => Some(WidgetClassKind::HookConsumerWidget),
|
|
407
|
+
_ => None,
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
pub(super) fn state_widget_class(class: Node<'_>, source: &str) -> Option<String> {
|
|
412
|
+
let type_text = superclass_type_text(class, source)?;
|
|
413
|
+
let compact = strip_whitespace(&type_text);
|
|
414
|
+
let base = simple_type_name(compact.split('<').next().unwrap_or(&compact));
|
|
415
|
+
if !matches!(base.as_str(), "State" | "ConsumerState") {
|
|
416
|
+
return None;
|
|
417
|
+
}
|
|
418
|
+
let generic = compact
|
|
419
|
+
.split_once('<')
|
|
420
|
+
.and_then(|(_, rest)| rest.rsplit_once('>').map(|(inside, _)| inside))?;
|
|
421
|
+
generic.split(',').next().map(simple_type_name)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
fn superclass_base_name(class: Node<'_>, source: &str) -> Option<String> {
|
|
425
|
+
superclass_type_text(class, source).map(|text| {
|
|
426
|
+
let compact = strip_whitespace(&text);
|
|
427
|
+
simple_type_name(compact.split('<').next().unwrap_or(&compact))
|
|
428
|
+
})
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
pub(super) fn superclass_type_text(class: Node<'_>, source: &str) -> Option<String> {
|
|
432
|
+
let superclass = class.child_by_field_name("superclass")?;
|
|
433
|
+
let type_text = superclass
|
|
434
|
+
.child_by_field_name("type")?
|
|
435
|
+
.utf8_text(source.as_bytes())
|
|
436
|
+
.ok()
|
|
437
|
+
.map(str::to_owned)?;
|
|
438
|
+
if type_text.contains('<') {
|
|
439
|
+
return Some(type_text);
|
|
440
|
+
}
|
|
441
|
+
superclass
|
|
442
|
+
.utf8_text(source.as_bytes())
|
|
443
|
+
.ok()
|
|
444
|
+
.and_then(|text| {
|
|
445
|
+
let without_extends = text.trim().strip_prefix("extends")?.trim();
|
|
446
|
+
Some(
|
|
447
|
+
without_extends
|
|
448
|
+
.split(" with ")
|
|
449
|
+
.next()
|
|
450
|
+
.unwrap_or(without_extends)
|
|
451
|
+
.to_owned(),
|
|
452
|
+
)
|
|
453
|
+
})
|
|
454
|
+
.or(Some(type_text))
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
fn strip_whitespace(text: &str) -> String {
|
|
458
|
+
text.chars()
|
|
459
|
+
.filter(|character| !character.is_whitespace())
|
|
460
|
+
.collect()
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
pub(super) fn simple_type_name(text: &str) -> String {
|
|
464
|
+
text.trim_end_matches('?')
|
|
465
|
+
.rsplit('.')
|
|
466
|
+
.next()
|
|
467
|
+
.unwrap_or(text)
|
|
468
|
+
.to_owned()
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
fn widget_body_uses_param(body: Node<'_>, name: &str, source: &str) -> bool {
|
|
472
|
+
let mut found = false;
|
|
473
|
+
visit_named(body, &mut |node| {
|
|
474
|
+
if !found && is_body_identifier_use(node, name, source) {
|
|
475
|
+
found = true;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
found
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
fn is_body_identifier_use(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
482
|
+
if !matches!(node.kind(), "identifier" | "identifier_dollar_escaped") {
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
if node.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
if has_ancestor_kind(node, BODY_USAGE_SKIP_ANCESTORS) {
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
let Some(parent) = node.parent() else {
|
|
492
|
+
return true;
|
|
493
|
+
};
|
|
494
|
+
if parent.kind() == "label" || name_field_of(parent, node) {
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
!(parent.kind() == "identifier_list" && has_ancestor_kind(parent, &["declaration"]))
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const BODY_USAGE_SKIP_ANCESTORS: &[&str] = &[
|
|
501
|
+
"constructor_signature",
|
|
502
|
+
"constant_constructor_signature",
|
|
503
|
+
"factory_constructor_signature",
|
|
504
|
+
"redirecting_factory_constructor_signature",
|
|
505
|
+
"constructor_param",
|
|
506
|
+
"super_formal_parameter",
|
|
507
|
+
"initializers",
|
|
508
|
+
"initializer_list_entry",
|
|
509
|
+
"field_initializer",
|
|
510
|
+
"type",
|
|
511
|
+
"typed_identifier",
|
|
512
|
+
];
|
|
513
|
+
|
|
514
|
+
fn state_body_uses_param(body: Node<'_>, name: &str, source: &str) -> bool {
|
|
515
|
+
let mut found = false;
|
|
516
|
+
visit_named(body, &mut |node| {
|
|
517
|
+
if !found && is_widget_member_access(node, name, source) {
|
|
518
|
+
found = true;
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
found
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
fn is_widget_member_access(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
525
|
+
if !matches!(
|
|
526
|
+
node.kind(),
|
|
527
|
+
"member_expression" | "null_aware_member_expression" | "assignable_expression"
|
|
528
|
+
) {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
let Some(property) = node.child_by_field_name("property") else {
|
|
532
|
+
return false;
|
|
533
|
+
};
|
|
534
|
+
if property.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
let Some(object) = node.child_by_field_name("object") else {
|
|
538
|
+
return false;
|
|
539
|
+
};
|
|
540
|
+
matches!(
|
|
541
|
+
object.utf8_text(source.as_bytes()).ok(),
|
|
542
|
+
Some("widget" | "oldWidget")
|
|
543
|
+
)
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
547
|
+
visitor(node);
|
|
548
|
+
let mut cursor = node.walk();
|
|
549
|
+
for child in node.named_children(&mut cursor) {
|
|
550
|
+
visit_named(child, visitor);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
fn name_field_of(parent: Node<'_>, child: Node<'_>) -> bool {
|
|
555
|
+
let mut cursor = parent.walk();
|
|
556
|
+
parent
|
|
557
|
+
.children_by_field_name("name", &mut cursor)
|
|
558
|
+
.any(|field| same_node(field, child))
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
fn has_ancestor_kind(node: Node<'_>, kinds: &[&str]) -> bool {
|
|
562
|
+
let mut parent = node.parent();
|
|
563
|
+
while let Some(ancestor) = parent {
|
|
564
|
+
if kinds.contains(&ancestor.kind()) {
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
parent = ancestor.parent();
|
|
568
|
+
}
|
|
569
|
+
false
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
|
|
573
|
+
left.kind() == right.kind()
|
|
574
|
+
&& left.start_byte() == right.start_byte()
|
|
575
|
+
&& left.end_byte() == right.end_byte()
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
fn is_test_path(path: &Path) -> bool {
|
|
579
|
+
path.file_name()
|
|
580
|
+
.and_then(|name| name.to_str())
|
|
581
|
+
.is_some_and(|name| name.ends_with("_test.dart"))
|
|
582
|
+
|| path.components().any(|component| {
|
|
583
|
+
component.as_os_str().to_str().is_some_and(|segment| {
|
|
584
|
+
matches!(segment, "test" | "integration_test" | "test_driver")
|
|
585
|
+
})
|
|
586
|
+
})
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
#[cfg(test)]
|
|
590
|
+
mod tests;
|