dart-decimate 0.0.12 → 0.0.14
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 +75 -1
- package/Cargo.toml +2 -1
- package/README.md +31 -27
- package/npm/scripts/postinstall.js +33 -12
- package/npm/scripts/test-npx-local.js +7 -2
- package/npm/scripts/test-npx-mcp-local.js +3 -1
- package/npm/scripts/test-npx-prebuilt.js +33 -13
- package/npm/scripts/test-postinstall-prebuilt.js +33 -13
- package/package.json +11 -1
- package/src/changed_scope.rs +4 -7
- package/src/cli/audit_run.rs +1 -3
- package/src/cli/decision_surface_run.rs +1 -3
- package/src/cli/entry_points.rs +75 -13
- package/src/dart_parser.rs +1 -3
- package/src/dependencies/codegen.rs +26 -0
- package/src/dependencies.rs +1 -0
- package/src/dependency_scripts.rs +42 -8
- package/src/extract.rs +6 -0
- package/src/git_command.rs +58 -0
- package/src/graph.rs +5 -0
- package/src/lib.rs +1 -0
- package/src/manifest.rs +1 -1
- package/src/output/graph_findings.rs +117 -43
- package/src/output/route_cycle/classification.rs +862 -0
- package/src/output/route_cycle/navigation.rs +6 -1
- package/src/output/route_cycle/registry_api.rs +268 -0
- package/src/output/route_cycle.rs +28 -162
- package/src/output/types.rs +3 -3
- package/src/output.rs +2 -2
- package/src/scan/tests.rs +133 -0
- package/src/scan.rs +122 -47
- package/src/security/detect.rs +1275 -10
- package/src/security_gate.rs +3 -5
- package/src/trace.rs +22 -13
- package/src/widgets/inheritance.rs +250 -0
- package/src/widgets/params.rs +81 -1
- package/src/widgets/patterns.rs +32 -25
- package/src/widgets/reads.rs +61 -11
- package/src/widgets/resolution.rs +281 -0
- package/src/widgets/unrendered.rs +111 -47
- package/src/widgets.rs +69 -11
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use crate::graph::normalize_against;
|
|
5
|
+
use crate::{DartCombinator, DartCombinatorKind, DependencyKind, DependencyVisibility};
|
|
6
|
+
use crate::{ResolvedDependency, ScannedProject};
|
|
7
|
+
|
|
8
|
+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
9
|
+
pub(super) struct ClassKey {
|
|
10
|
+
pub(super) path: PathBuf,
|
|
11
|
+
pub(super) name: String,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#[derive(Debug)]
|
|
15
|
+
pub(super) struct DeclarationResolver {
|
|
16
|
+
declarations: BTreeMap<PathBuf, BTreeSet<String>>,
|
|
17
|
+
imports: BTreeMap<PathBuf, Vec<ResolvedDependency>>,
|
|
18
|
+
exports: BTreeMap<PathBuf, Vec<ResolvedDependency>>,
|
|
19
|
+
library_by_path: BTreeMap<PathBuf, PathBuf>,
|
|
20
|
+
paths_by_library: BTreeMap<PathBuf, BTreeSet<PathBuf>>,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
impl DeclarationResolver {
|
|
24
|
+
pub(super) fn new(
|
|
25
|
+
project: &ScannedProject,
|
|
26
|
+
declarations: impl IntoIterator<Item = ClassKey>,
|
|
27
|
+
) -> Self {
|
|
28
|
+
let declarations = declarations.into_iter().fold(
|
|
29
|
+
BTreeMap::<PathBuf, BTreeSet<String>>::new(),
|
|
30
|
+
|mut declarations, class| {
|
|
31
|
+
declarations
|
|
32
|
+
.entry(class.path)
|
|
33
|
+
.or_default()
|
|
34
|
+
.insert(class.name);
|
|
35
|
+
declarations
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
let dependencies = project
|
|
39
|
+
.graph
|
|
40
|
+
.dependencies()
|
|
41
|
+
.into_iter()
|
|
42
|
+
.map(|mut dependency| {
|
|
43
|
+
dependency.from_path = normalize_against(&project.root, &dependency.from_path);
|
|
44
|
+
dependency.to_path = normalize_against(&project.root, &dependency.to_path);
|
|
45
|
+
dependency
|
|
46
|
+
})
|
|
47
|
+
.collect::<Vec<_>>();
|
|
48
|
+
let mut library_by_path = declarations
|
|
49
|
+
.keys()
|
|
50
|
+
.map(|path| (path.clone(), path.clone()))
|
|
51
|
+
.collect::<BTreeMap<_, _>>();
|
|
52
|
+
for dependency in dependencies
|
|
53
|
+
.iter()
|
|
54
|
+
.filter(|dependency| dependency.kind == DependencyKind::Part)
|
|
55
|
+
{
|
|
56
|
+
library_by_path.insert(dependency.to_path.clone(), dependency.from_path.clone());
|
|
57
|
+
}
|
|
58
|
+
let paths_by_library = library_by_path.iter().fold(
|
|
59
|
+
BTreeMap::<PathBuf, BTreeSet<PathBuf>>::new(),
|
|
60
|
+
|mut paths, (path, library)| {
|
|
61
|
+
paths
|
|
62
|
+
.entry(library.clone())
|
|
63
|
+
.or_default()
|
|
64
|
+
.insert(path.clone());
|
|
65
|
+
paths
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
let mut imports = BTreeMap::<PathBuf, Vec<ResolvedDependency>>::new();
|
|
69
|
+
let mut exports = BTreeMap::<PathBuf, Vec<ResolvedDependency>>::new();
|
|
70
|
+
for dependency in dependencies {
|
|
71
|
+
let index = match dependency.kind {
|
|
72
|
+
DependencyKind::Import => &mut imports,
|
|
73
|
+
DependencyKind::Export => &mut exports,
|
|
74
|
+
_ => continue,
|
|
75
|
+
};
|
|
76
|
+
index
|
|
77
|
+
.entry(dependency.from_path.clone())
|
|
78
|
+
.or_default()
|
|
79
|
+
.push(dependency);
|
|
80
|
+
}
|
|
81
|
+
Self {
|
|
82
|
+
declarations,
|
|
83
|
+
imports,
|
|
84
|
+
exports,
|
|
85
|
+
library_by_path,
|
|
86
|
+
paths_by_library,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
pub(super) fn resolve(&self, from: &Path, reference: &str) -> BTreeSet<ClassKey> {
|
|
91
|
+
let reference = compact_reference(reference);
|
|
92
|
+
let segments = reference.split('.').collect::<Vec<_>>();
|
|
93
|
+
let Some(first) = segments.first().copied() else {
|
|
94
|
+
return BTreeSet::new();
|
|
95
|
+
};
|
|
96
|
+
let library = self.library_path(from);
|
|
97
|
+
let imports = self.imports.get(&library).map_or(&[][..], Vec::as_slice);
|
|
98
|
+
|
|
99
|
+
if let Some(name) = segments.get(1).filter(|name| type_like_name(name)) {
|
|
100
|
+
let has_prefixed_import = imports
|
|
101
|
+
.iter()
|
|
102
|
+
.any(|dependency| dependency.visibility.prefix.as_deref() == Some(first));
|
|
103
|
+
if has_prefixed_import {
|
|
104
|
+
if name.starts_with('_') {
|
|
105
|
+
return BTreeSet::new();
|
|
106
|
+
}
|
|
107
|
+
return self.imported_classes(
|
|
108
|
+
imports
|
|
109
|
+
.iter()
|
|
110
|
+
.filter(|dependency| dependency.visibility.prefix.as_deref() == Some(first))
|
|
111
|
+
.filter(|dependency| visible(name, &dependency.visibility)),
|
|
112
|
+
name,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let local = self.library_classes(&library, first);
|
|
118
|
+
if !local.is_empty() {
|
|
119
|
+
return exactly_one(local).into_iter().collect();
|
|
120
|
+
}
|
|
121
|
+
if first.starts_with('_') {
|
|
122
|
+
return BTreeSet::new();
|
|
123
|
+
}
|
|
124
|
+
self.imported_classes(
|
|
125
|
+
imports
|
|
126
|
+
.iter()
|
|
127
|
+
.filter(|dependency| dependency.visibility.prefix.is_none())
|
|
128
|
+
.filter(|dependency| visible(first, &dependency.visibility)),
|
|
129
|
+
first,
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn imported_classes<'a>(
|
|
134
|
+
&self,
|
|
135
|
+
imports: impl IntoIterator<Item = &'a ResolvedDependency>,
|
|
136
|
+
name: &str,
|
|
137
|
+
) -> BTreeSet<ClassKey> {
|
|
138
|
+
let mut candidates_by_directive = BTreeMap::<(usize, usize), BTreeSet<ClassKey>>::new();
|
|
139
|
+
for dependency in imports {
|
|
140
|
+
candidates_by_directive
|
|
141
|
+
.entry((dependency.location.line, dependency.location.column))
|
|
142
|
+
.or_default()
|
|
143
|
+
.extend(self.exported_classes(&dependency.to_path, name));
|
|
144
|
+
}
|
|
145
|
+
let mut resolved = BTreeSet::new();
|
|
146
|
+
let mut matching_directives = 0usize;
|
|
147
|
+
for candidates in candidates_by_directive
|
|
148
|
+
.into_values()
|
|
149
|
+
.filter(|candidates| !candidates.is_empty())
|
|
150
|
+
{
|
|
151
|
+
matching_directives += 1;
|
|
152
|
+
resolved.extend(candidates);
|
|
153
|
+
}
|
|
154
|
+
if matching_directives <= 1 || resolved.len() <= 1 {
|
|
155
|
+
resolved
|
|
156
|
+
} else {
|
|
157
|
+
BTreeSet::new()
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn exported_classes(&self, library: &Path, name: &str) -> BTreeSet<ClassKey> {
|
|
162
|
+
let mut visited = BTreeSet::new();
|
|
163
|
+
self.collect_exported_classes(library, name, &mut visited)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
fn collect_exported_classes(
|
|
167
|
+
&self,
|
|
168
|
+
library: &Path,
|
|
169
|
+
name: &str,
|
|
170
|
+
visited: &mut BTreeSet<PathBuf>,
|
|
171
|
+
) -> BTreeSet<ClassKey> {
|
|
172
|
+
let library = self.library_path(library);
|
|
173
|
+
if !visited.insert(library.clone()) {
|
|
174
|
+
return BTreeSet::new();
|
|
175
|
+
}
|
|
176
|
+
let mut classes = self.library_classes(&library, name);
|
|
177
|
+
for dependency in self
|
|
178
|
+
.exports
|
|
179
|
+
.get(&library)
|
|
180
|
+
.into_iter()
|
|
181
|
+
.flatten()
|
|
182
|
+
.filter(|dependency| visible(name, &dependency.visibility))
|
|
183
|
+
{
|
|
184
|
+
classes.extend(self.collect_exported_classes(&dependency.to_path, name, visited));
|
|
185
|
+
}
|
|
186
|
+
classes
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn library_classes(&self, library: &Path, name: &str) -> BTreeSet<ClassKey> {
|
|
190
|
+
self.paths_by_library
|
|
191
|
+
.get(library)
|
|
192
|
+
.into_iter()
|
|
193
|
+
.flat_map(|paths| paths.iter())
|
|
194
|
+
.filter(|path| {
|
|
195
|
+
self.declarations
|
|
196
|
+
.get(*path)
|
|
197
|
+
.is_some_and(|declarations| declarations.contains(name))
|
|
198
|
+
})
|
|
199
|
+
.map(|path| ClassKey {
|
|
200
|
+
path: path.clone(),
|
|
201
|
+
name: name.to_owned(),
|
|
202
|
+
})
|
|
203
|
+
.collect()
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
fn library_path(&self, path: &Path) -> PathBuf {
|
|
207
|
+
self.library_by_path
|
|
208
|
+
.get(path)
|
|
209
|
+
.cloned()
|
|
210
|
+
.unwrap_or_else(|| path.to_path_buf())
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
fn compact_reference(reference: &str) -> String {
|
|
215
|
+
reference
|
|
216
|
+
.trim()
|
|
217
|
+
.trim_end_matches('?')
|
|
218
|
+
.split('<')
|
|
219
|
+
.next()
|
|
220
|
+
.unwrap_or(reference)
|
|
221
|
+
.chars()
|
|
222
|
+
.filter(|character| !character.is_whitespace())
|
|
223
|
+
.collect()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
fn type_like_name(name: &&str) -> bool {
|
|
227
|
+
name.chars()
|
|
228
|
+
.next()
|
|
229
|
+
.is_some_and(|character| character == '_' || character.is_ascii_uppercase())
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
fn visible(name: &str, visibility: &DependencyVisibility) -> bool {
|
|
233
|
+
visible_through_combinators(name, &visibility.combinators)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
fn visible_through_combinators(name: &str, combinators: &[DartCombinator]) -> bool {
|
|
237
|
+
let mut is_visible = true;
|
|
238
|
+
for combinator in combinators {
|
|
239
|
+
match combinator.kind {
|
|
240
|
+
DartCombinatorKind::Show => {
|
|
241
|
+
is_visible &= combinator.names.iter().any(|shown| shown == name);
|
|
242
|
+
}
|
|
243
|
+
DartCombinatorKind::Hide => {
|
|
244
|
+
if combinator.names.iter().any(|hidden| hidden == name) {
|
|
245
|
+
is_visible = false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
is_visible
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
fn exactly_one(mut classes: BTreeSet<ClassKey>) -> Option<ClassKey> {
|
|
254
|
+
let class = classes.pop_first()?;
|
|
255
|
+
classes.is_empty().then_some(class)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#[cfg(test)]
|
|
259
|
+
mod tests {
|
|
260
|
+
use super::*;
|
|
261
|
+
use crate::Location;
|
|
262
|
+
|
|
263
|
+
#[test]
|
|
264
|
+
fn combinators_cannot_revive_a_hidden_declaration() {
|
|
265
|
+
let combinators = [
|
|
266
|
+
combinator(DartCombinatorKind::Show, &["SearchInput"]),
|
|
267
|
+
combinator(DartCombinatorKind::Hide, &["SearchInput"]),
|
|
268
|
+
combinator(DartCombinatorKind::Show, &["SearchInput"]),
|
|
269
|
+
];
|
|
270
|
+
|
|
271
|
+
assert!(!visible_through_combinators("SearchInput", &combinators));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn combinator(kind: DartCombinatorKind, names: &[&str]) -> DartCombinator {
|
|
275
|
+
DartCombinator {
|
|
276
|
+
kind,
|
|
277
|
+
names: names.iter().map(|name| (*name).to_owned()).collect(),
|
|
278
|
+
location: Location { line: 1, column: 0 },
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
@@ -1,47 +1,58 @@
|
|
|
1
1
|
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
-
use std::fs;
|
|
3
2
|
use std::path::{Path, PathBuf};
|
|
4
3
|
|
|
5
|
-
use rayon::prelude::*;
|
|
6
4
|
use tree_sitter::Node;
|
|
7
5
|
|
|
8
6
|
use crate::graph::normalize_against;
|
|
9
7
|
use crate::{DartCombinatorKind, DependencyKind, ScannedProject, TopLevelDeclaration};
|
|
10
8
|
|
|
9
|
+
#[cfg(test)]
|
|
10
|
+
use super::parse_tree;
|
|
11
|
+
use super::resolution::{ClassKey, DeclarationResolver};
|
|
11
12
|
use super::{
|
|
12
|
-
UnrenderedWidgetClass,
|
|
13
|
-
|
|
13
|
+
UnrenderedWidgetClass, WidgetFileFacts, has_ancestor_kind, inheritance::class_superclasses,
|
|
14
|
+
simple_type_name, visit_named, widget_kind,
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
|
17
|
-
struct FileReachabilityFacts {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
pub(super) struct FileReachabilityFacts {
|
|
19
|
+
pub(super) path: PathBuf,
|
|
20
|
+
pub(super) class_names: BTreeSet<String>,
|
|
21
|
+
pub(super) widgets: Vec<UnrenderedWidgetClass>,
|
|
22
|
+
pub(super) object_constructors: Vec<String>,
|
|
23
|
+
pub(super) superclasses: BTreeMap<String, String>,
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
pub(super) fn unrendered_widgets(
|
|
23
27
|
project: &ScannedProject,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
.
|
|
29
|
-
.
|
|
28
|
+
files: &[WidgetFileFacts],
|
|
29
|
+
resolver: &DeclarationResolver,
|
|
30
|
+
) -> Vec<UnrenderedWidgetClass> {
|
|
31
|
+
let files = files
|
|
32
|
+
.iter()
|
|
33
|
+
.map(|file| &file.reachability)
|
|
34
|
+
.collect::<Vec<_>>();
|
|
30
35
|
let exported = public_reexported_declarations(project);
|
|
31
36
|
let mut candidates = files
|
|
32
37
|
.iter()
|
|
33
38
|
.flat_map(|file| file.widgets.iter().cloned())
|
|
34
39
|
.filter(|widget| !exported.contains(&(widget.path.clone(), widget.widget_class.clone())))
|
|
35
40
|
.collect::<Vec<_>>();
|
|
36
|
-
let
|
|
41
|
+
let candidate_keys = candidates
|
|
37
42
|
.iter()
|
|
38
|
-
.map(|widget|
|
|
43
|
+
.map(|widget| ClassKey {
|
|
44
|
+
path: widget.path.clone(),
|
|
45
|
+
name: widget.widget_class.clone(),
|
|
46
|
+
})
|
|
39
47
|
.collect::<BTreeSet<_>>();
|
|
40
|
-
let render_counts = render_counts(&files, &
|
|
48
|
+
let render_counts = render_counts(&files, &candidate_keys, resolver);
|
|
41
49
|
|
|
42
50
|
candidates.retain_mut(|widget| {
|
|
43
51
|
widget.render_reference_count = render_counts
|
|
44
|
-
.get(&
|
|
52
|
+
.get(&ClassKey {
|
|
53
|
+
path: widget.path.clone(),
|
|
54
|
+
name: widget.widget_class.clone(),
|
|
55
|
+
})
|
|
45
56
|
.copied()
|
|
46
57
|
.unwrap_or_default();
|
|
47
58
|
widget.render_reference_count == 0
|
|
@@ -60,29 +71,37 @@ pub(super) fn unrendered_widgets(
|
|
|
60
71
|
&right.widget_class,
|
|
61
72
|
))
|
|
62
73
|
});
|
|
63
|
-
|
|
74
|
+
candidates
|
|
64
75
|
}
|
|
65
76
|
|
|
66
|
-
fn reachability_facts(
|
|
67
|
-
|
|
77
|
+
pub(super) fn reachability_facts(
|
|
78
|
+
path: &Path,
|
|
79
|
+
root: Node<'_>,
|
|
80
|
+
classes: &[Node<'_>],
|
|
81
|
+
source: &str,
|
|
82
|
+
) -> FileReachabilityFacts {
|
|
83
|
+
FileReachabilityFacts {
|
|
68
84
|
path: path.to_path_buf(),
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
85
|
+
class_names: classes
|
|
86
|
+
.iter()
|
|
87
|
+
.filter_map(|class| {
|
|
88
|
+
class
|
|
89
|
+
.child_by_field_name("name")?
|
|
90
|
+
.utf8_text(source.as_bytes())
|
|
91
|
+
.ok()
|
|
92
|
+
.map(str::to_owned)
|
|
93
|
+
})
|
|
94
|
+
.collect(),
|
|
95
|
+
widgets: widget_classes(path, classes, source),
|
|
96
|
+
object_constructors: object_constructor_names(root, source),
|
|
97
|
+
superclasses: class_superclasses(classes, source),
|
|
98
|
+
}
|
|
78
99
|
}
|
|
79
100
|
|
|
80
|
-
fn widget_classes(path: &Path,
|
|
81
|
-
let mut classes = Vec::new();
|
|
82
|
-
collect_class_declarations(root, &mut classes);
|
|
101
|
+
fn widget_classes(path: &Path, classes: &[Node<'_>], source: &str) -> Vec<UnrenderedWidgetClass> {
|
|
83
102
|
classes
|
|
84
|
-
.
|
|
85
|
-
.filter_map(|class| widget_class(path, class, source))
|
|
103
|
+
.iter()
|
|
104
|
+
.filter_map(|class| widget_class(path, *class, source))
|
|
86
105
|
.collect()
|
|
87
106
|
}
|
|
88
107
|
|
|
@@ -118,11 +137,13 @@ fn object_constructor_names(root: Node<'_>, source: &str) -> Vec<String> {
|
|
|
118
137
|
visit_named(root, &mut |node| {
|
|
119
138
|
if is_object_constructor(node) && !has_ancestor_kind(node, &["annotation"]) {
|
|
120
139
|
if let Some(constructor) = constructor_type_name(node, source) {
|
|
121
|
-
|
|
140
|
+
if !constructor_name_candidates(&constructor).is_empty() {
|
|
141
|
+
constructors.push(constructor);
|
|
142
|
+
}
|
|
122
143
|
}
|
|
123
144
|
} else if is_arrow_body_constructor_identifier(node, source) {
|
|
124
145
|
if let Ok(constructor) = node.utf8_text(source.as_bytes()) {
|
|
125
|
-
constructors.
|
|
146
|
+
constructors.push(constructor.to_owned());
|
|
126
147
|
}
|
|
127
148
|
}
|
|
128
149
|
});
|
|
@@ -273,20 +294,58 @@ fn constructor_name_candidates(constructor: &str) -> Vec<String> {
|
|
|
273
294
|
}
|
|
274
295
|
|
|
275
296
|
fn render_counts(
|
|
276
|
-
files: &[FileReachabilityFacts],
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
.
|
|
284
|
-
|
|
285
|
-
|
|
297
|
+
files: &[&FileReachabilityFacts],
|
|
298
|
+
candidate_keys: &BTreeSet<ClassKey>,
|
|
299
|
+
resolver: &DeclarationResolver,
|
|
300
|
+
) -> BTreeMap<ClassKey, usize> {
|
|
301
|
+
let superclasses = resolved_superclasses(files, resolver);
|
|
302
|
+
let mut counts = BTreeMap::<ClassKey, usize>::new();
|
|
303
|
+
for file in files {
|
|
304
|
+
for constructed in &file.object_constructors {
|
|
305
|
+
let mut pending = resolver
|
|
306
|
+
.resolve(&file.path, constructed)
|
|
307
|
+
.into_iter()
|
|
308
|
+
.collect::<Vec<_>>();
|
|
309
|
+
let mut visited = BTreeSet::new();
|
|
310
|
+
while let Some(class) = pending.pop() {
|
|
311
|
+
if !visited.insert(class.clone()) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if candidate_keys.contains(&class) {
|
|
315
|
+
*counts.entry(class.clone()).or_default() += 1;
|
|
316
|
+
}
|
|
317
|
+
pending.extend(
|
|
318
|
+
superclasses
|
|
319
|
+
.get(&class)
|
|
320
|
+
.into_iter()
|
|
321
|
+
.flat_map(|parents| parents.iter().cloned()),
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
286
325
|
}
|
|
287
326
|
counts
|
|
288
327
|
}
|
|
289
328
|
|
|
329
|
+
fn resolved_superclasses(
|
|
330
|
+
files: &[&FileReachabilityFacts],
|
|
331
|
+
resolver: &DeclarationResolver,
|
|
332
|
+
) -> BTreeMap<ClassKey, BTreeSet<ClassKey>> {
|
|
333
|
+
let mut superclasses = BTreeMap::<ClassKey, BTreeSet<ClassKey>>::new();
|
|
334
|
+
for file in files {
|
|
335
|
+
for (class, parent) in &file.superclasses {
|
|
336
|
+
let class = ClassKey {
|
|
337
|
+
path: file.path.clone(),
|
|
338
|
+
name: class.clone(),
|
|
339
|
+
};
|
|
340
|
+
superclasses
|
|
341
|
+
.entry(class)
|
|
342
|
+
.or_default()
|
|
343
|
+
.extend(resolver.resolve(&file.path, parent));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
superclasses
|
|
347
|
+
}
|
|
348
|
+
|
|
290
349
|
fn public_reexported_declarations(project: &ScannedProject) -> BTreeSet<(PathBuf, String)> {
|
|
291
350
|
let declarations = project
|
|
292
351
|
.files
|
|
@@ -437,7 +496,12 @@ void main() {
|
|
|
437
496
|
|
|
438
497
|
assert_eq!(
|
|
439
498
|
names,
|
|
440
|
-
vec![
|
|
499
|
+
vec![
|
|
500
|
+
"LiveCard",
|
|
501
|
+
"LegacyCard",
|
|
502
|
+
"ui.PrefixedCard",
|
|
503
|
+
"DeadCard.named"
|
|
504
|
+
]
|
|
441
505
|
);
|
|
442
506
|
assert!(
|
|
443
507
|
!names.iter().any(|name| name == "BareIifeCard"),
|
package/src/widgets.rs
CHANGED
|
@@ -12,19 +12,23 @@ use crate::graph::normalize_against;
|
|
|
12
12
|
use crate::{DeadCodeReport, Location, ScannedProject};
|
|
13
13
|
|
|
14
14
|
mod forwarding;
|
|
15
|
+
mod inheritance;
|
|
15
16
|
mod lifecycle;
|
|
16
17
|
mod params;
|
|
17
18
|
mod patterns;
|
|
18
19
|
mod reads;
|
|
20
|
+
mod resolution;
|
|
19
21
|
mod top_level;
|
|
20
22
|
mod unrendered;
|
|
21
23
|
|
|
22
24
|
use forwarding::{forwarded_param_used, widget_forwarded_param_uses};
|
|
25
|
+
use inheritance::{inherited_param_uses, inherited_param_uses_across_files};
|
|
23
26
|
pub use lifecycle::MissingContextMountedAfterAwait;
|
|
24
27
|
use lifecycle::lifecycle_findings;
|
|
25
|
-
use params::constructor_params;
|
|
28
|
+
use params::{constructor_initializers_use_param, constructor_params};
|
|
26
29
|
use patterns::object_pattern_field_reads_for_widget;
|
|
27
30
|
use reads::{state_body_uses_param, widget_body_uses_param};
|
|
31
|
+
use resolution::{ClassKey, DeclarationResolver};
|
|
28
32
|
use top_level::top_level_widget_functions;
|
|
29
33
|
use unrendered::unrendered_widgets;
|
|
30
34
|
|
|
@@ -152,13 +156,35 @@ pub fn analyze_widgets(
|
|
|
152
156
|
dead_code: Option<&DeadCodeReport>,
|
|
153
157
|
) -> Result<WidgetReport, WidgetAnalysisError> {
|
|
154
158
|
let paths = widget_analysis_paths(project, dead_code);
|
|
155
|
-
let
|
|
159
|
+
let mut file_facts = paths
|
|
156
160
|
.par_iter()
|
|
157
161
|
.map(|path| analyze_file(path))
|
|
158
162
|
.collect::<Result<Vec<_>, _>>()?;
|
|
159
|
-
let
|
|
163
|
+
let resolver = DeclarationResolver::new(
|
|
164
|
+
project,
|
|
165
|
+
file_facts.iter().flat_map(|file| {
|
|
166
|
+
file.reachability.class_names.iter().map(|name| ClassKey {
|
|
167
|
+
path: file.reachability.path.clone(),
|
|
168
|
+
name: name.clone(),
|
|
169
|
+
})
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
let mut findings = merge_file_widget_findings(
|
|
173
|
+
file_facts
|
|
174
|
+
.iter_mut()
|
|
175
|
+
.map(|file| std::mem::take(&mut file.findings))
|
|
176
|
+
.collect(),
|
|
177
|
+
);
|
|
178
|
+
let inherited_uses = inherited_param_uses_across_files(&file_facts, &resolver);
|
|
179
|
+
findings.unused_params.retain(|finding| {
|
|
180
|
+
!inherited_uses.contains(&(
|
|
181
|
+
finding.path.clone(),
|
|
182
|
+
finding.widget_class.clone(),
|
|
183
|
+
finding.param_name.clone(),
|
|
184
|
+
))
|
|
185
|
+
});
|
|
160
186
|
sort_file_widget_findings(&mut findings);
|
|
161
|
-
let unrendered_widgets = unrendered_widgets(project, &
|
|
187
|
+
let unrendered_widgets = unrendered_widgets(project, &file_facts, &resolver);
|
|
162
188
|
|
|
163
189
|
Ok(WidgetReport {
|
|
164
190
|
analyzed_files: paths.len(),
|
|
@@ -281,15 +307,21 @@ fn sort_lifecycle_findings(findings: &mut FileWidgetFindings) {
|
|
|
281
307
|
});
|
|
282
308
|
}
|
|
283
309
|
|
|
284
|
-
fn analyze_file(path: &Path) -> Result<
|
|
310
|
+
fn analyze_file(path: &Path) -> Result<WidgetFileFacts, WidgetAnalysisError> {
|
|
285
311
|
let source = fs::read_to_string(path).map_err(|source| WidgetAnalysisError::ReadFile {
|
|
286
312
|
path: path.to_path_buf(),
|
|
287
313
|
source,
|
|
288
314
|
})?;
|
|
289
315
|
let parsed = parse_tree(path, &source)?;
|
|
290
316
|
let root = parsed.tree().root_node();
|
|
317
|
+
let mut classes = Vec::new();
|
|
318
|
+
collect_class_declarations(root, &mut classes);
|
|
291
319
|
|
|
292
|
-
Ok(
|
|
320
|
+
Ok(WidgetFileFacts {
|
|
321
|
+
findings: findings_for_classes(path, root, &classes, parsed.source()),
|
|
322
|
+
classes: inheritance::class_facts(path, &classes, parsed.source()),
|
|
323
|
+
reachability: unrendered::reachability_facts(path, root, &classes, parsed.source()),
|
|
324
|
+
})
|
|
293
325
|
}
|
|
294
326
|
|
|
295
327
|
fn parse_tree<'source>(
|
|
@@ -319,20 +351,38 @@ struct FileWidgetFindings {
|
|
|
319
351
|
missing_context_mounted_after_await: Vec<MissingContextMountedAfterAwait>,
|
|
320
352
|
}
|
|
321
353
|
|
|
354
|
+
#[derive(Debug)]
|
|
355
|
+
struct WidgetFileFacts {
|
|
356
|
+
findings: FileWidgetFindings,
|
|
357
|
+
classes: Vec<inheritance::ProjectClassFact>,
|
|
358
|
+
reachability: unrendered::FileReachabilityFacts,
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
#[cfg(test)]
|
|
322
362
|
fn findings_in_source(path: &Path, root: Node<'_>, source: &str) -> FileWidgetFindings {
|
|
323
363
|
let mut classes = Vec::new();
|
|
324
364
|
collect_class_declarations(root, &mut classes);
|
|
325
|
-
|
|
326
|
-
|
|
365
|
+
findings_for_classes(path, root, &classes, source)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
fn findings_for_classes(
|
|
369
|
+
path: &Path,
|
|
370
|
+
root: Node<'_>,
|
|
371
|
+
classes: &[Node<'_>],
|
|
372
|
+
source: &str,
|
|
373
|
+
) -> FileWidgetFindings {
|
|
374
|
+
let states = state_classes_by_widget(classes, source);
|
|
375
|
+
let forwarded_uses = widget_forwarded_param_uses(classes, source);
|
|
376
|
+
let inherited_uses = inherited_param_uses(classes, source);
|
|
327
377
|
let mut findings = FileWidgetFindings::default();
|
|
328
378
|
let has_widget_class = classes
|
|
329
379
|
.iter()
|
|
330
380
|
.any(|class| widget_kind(*class, source).is_some());
|
|
331
381
|
findings.top_level_functions = top_level_widget_functions(path, root, source, has_widget_class);
|
|
332
|
-
let lifecycle = lifecycle_findings(path,
|
|
382
|
+
let lifecycle = lifecycle_findings(path, classes, source);
|
|
333
383
|
findings.missing_context_mounted_after_await = lifecycle.missing_context_mounted_after_await;
|
|
334
384
|
|
|
335
|
-
for class in classes {
|
|
385
|
+
for class in classes.iter().copied() {
|
|
336
386
|
let Some(widget_kind) = widget_kind(class, source) else {
|
|
337
387
|
continue;
|
|
338
388
|
};
|
|
@@ -362,6 +412,14 @@ fn findings_in_source(path: &Path, root: Node<'_>, source: &str) -> FileWidgetFi
|
|
|
362
412
|
);
|
|
363
413
|
for param in constructor_params(class, &widget_class, source) {
|
|
364
414
|
if widget_body_uses_param(body, ¶m.field_name, source)
|
|
415
|
+
|| constructor_initializers_use_param(
|
|
416
|
+
class,
|
|
417
|
+
&widget_class,
|
|
418
|
+
¶m.field_name,
|
|
419
|
+
¶m.name,
|
|
420
|
+
source,
|
|
421
|
+
)
|
|
422
|
+
|| inherited_uses.contains(&(widget_class.clone(), param.field_name.clone()))
|
|
365
423
|
|| object_pattern_reads.contains(¶m.field_name)
|
|
366
424
|
|| states.get(&widget_class).is_some_and(|state_bodies| {
|
|
367
425
|
state_bodies.iter().any(|state_body| {
|
|
@@ -443,7 +501,7 @@ pub(super) fn state_widget_class(class: Node<'_>, source: &str) -> Option<String
|
|
|
443
501
|
generic.split(',').next().map(simple_type_name)
|
|
444
502
|
}
|
|
445
503
|
|
|
446
|
-
fn superclass_base_name(class: Node<'_>, source: &str) -> Option<String> {
|
|
504
|
+
pub(super) fn superclass_base_name(class: Node<'_>, source: &str) -> Option<String> {
|
|
447
505
|
superclass_type_text(class, source).map(|text| {
|
|
448
506
|
let compact = strip_whitespace(&text);
|
|
449
507
|
simple_type_name(compact.split('<').next().unwrap_or(&compact))
|