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/symbols.rs
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use serde::{Deserialize, Serialize};
|
|
5
|
+
|
|
6
|
+
use crate::generated::is_generated_dart_path;
|
|
7
|
+
use crate::graph::normalize_against;
|
|
8
|
+
use crate::{
|
|
9
|
+
DartCombinatorKind, DeadCodeReport, DeclarationKind, DependencyKind, Location,
|
|
10
|
+
MemberDeclaration, MemberKind, ScannedProject, TopLevelDeclaration,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
mod path_filters;
|
|
14
|
+
mod private_type_leaks;
|
|
15
|
+
mod riverpod;
|
|
16
|
+
use path_filters::{is_library_source, is_private, is_public_library_entry};
|
|
17
|
+
pub use private_type_leaks::PrivateTypeLeak;
|
|
18
|
+
use private_type_leaks::private_type_leaks;
|
|
19
|
+
use riverpod::extend_generated_provider_owner_references;
|
|
20
|
+
|
|
21
|
+
/// Symbol-level dead-code result.
|
|
22
|
+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
23
|
+
pub struct SymbolReport {
|
|
24
|
+
/// Public top-level declarations with no reachable syntactic references.
|
|
25
|
+
pub unused_exports: Vec<UnusedExport>,
|
|
26
|
+
/// Unused enum constants and conservative private class-like members.
|
|
27
|
+
pub unused_members: Vec<UnusedMember>,
|
|
28
|
+
/// Public signatures exposing same-library private types.
|
|
29
|
+
pub private_type_leaks: Vec<PrivateTypeLeak>,
|
|
30
|
+
/// Public API entries that expose multiple declarations with the same name.
|
|
31
|
+
pub duplicate_exports: Vec<DuplicateExport>,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// Options for symbol-level dead-code analysis.
|
|
35
|
+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
36
|
+
pub struct SymbolAnalysisOptions {
|
|
37
|
+
/// Report public declarations in entry libraries when they are otherwise unused.
|
|
38
|
+
pub include_entry_exports: bool,
|
|
39
|
+
/// Report exported signatures that expose same-library private types.
|
|
40
|
+
pub private_type_leaks: bool,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
44
|
+
pub struct UnusedExport {
|
|
45
|
+
pub path: PathBuf,
|
|
46
|
+
pub kind: DeclarationKind,
|
|
47
|
+
pub name: String,
|
|
48
|
+
pub location: Location,
|
|
49
|
+
pub reference_count: usize,
|
|
50
|
+
pub safe_to_delete: bool,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
54
|
+
pub struct UnusedMember {
|
|
55
|
+
pub path: PathBuf,
|
|
56
|
+
pub owner: String,
|
|
57
|
+
pub kind: MemberKind,
|
|
58
|
+
pub name: String,
|
|
59
|
+
pub location: Location,
|
|
60
|
+
pub reference_count: usize,
|
|
61
|
+
pub safe_to_delete: bool,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
65
|
+
pub struct DuplicateExport {
|
|
66
|
+
pub entry_path: PathBuf,
|
|
67
|
+
pub name: String,
|
|
68
|
+
pub declarations: Vec<DuplicateExportDeclaration>,
|
|
69
|
+
pub safe_to_delete: bool,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
73
|
+
pub struct DuplicateExportDeclaration {
|
|
74
|
+
pub path: PathBuf,
|
|
75
|
+
pub kind: DeclarationKind,
|
|
76
|
+
pub location: Location,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
80
|
+
pub struct SymbolIndex {
|
|
81
|
+
declarations: Vec<IndexedDeclaration>,
|
|
82
|
+
members: Vec<IndexedMember>,
|
|
83
|
+
references_by_name: BTreeMap<String, Vec<IndexedReference>>,
|
|
84
|
+
library_by_path: BTreeMap<PathBuf, PathBuf>,
|
|
85
|
+
public_exports: BTreeSet<(PathBuf, String)>,
|
|
86
|
+
public_exported_declarations: Vec<PublicExportedDeclaration>,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
90
|
+
struct IndexedDeclaration {
|
|
91
|
+
path: PathBuf,
|
|
92
|
+
declaration: TopLevelDeclaration,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
96
|
+
struct IndexedMember {
|
|
97
|
+
path: PathBuf,
|
|
98
|
+
member: MemberDeclaration,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
102
|
+
struct IndexedReference {
|
|
103
|
+
path: PathBuf,
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
107
|
+
struct PublicExportedDeclaration {
|
|
108
|
+
entry_path: PathBuf,
|
|
109
|
+
path: PathBuf,
|
|
110
|
+
declaration: TopLevelDeclaration,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
impl SymbolIndex {
|
|
114
|
+
/// Build a symbol index from parsed Dart files.
|
|
115
|
+
#[must_use]
|
|
116
|
+
pub fn from_project(project: &ScannedProject) -> Self {
|
|
117
|
+
let mut declarations = Vec::new();
|
|
118
|
+
let mut members = Vec::new();
|
|
119
|
+
let mut references_by_name = BTreeMap::<String, Vec<IndexedReference>>::new();
|
|
120
|
+
let library_by_path = library_by_path(project);
|
|
121
|
+
let public_exported_declarations = public_exported_declarations(project);
|
|
122
|
+
let public_exports = public_exported_declarations
|
|
123
|
+
.iter()
|
|
124
|
+
.map(|exported| (exported.path.clone(), exported.declaration.name.clone()))
|
|
125
|
+
.collect();
|
|
126
|
+
|
|
127
|
+
for file in &project.files {
|
|
128
|
+
let path = normalize_against(&project.root, &file.path);
|
|
129
|
+
declarations.extend(file.declarations.iter().cloned().map(|declaration| {
|
|
130
|
+
IndexedDeclaration {
|
|
131
|
+
path: path.clone(),
|
|
132
|
+
declaration,
|
|
133
|
+
}
|
|
134
|
+
}));
|
|
135
|
+
members.extend(file.members.iter().cloned().map(|member| IndexedMember {
|
|
136
|
+
path: path.clone(),
|
|
137
|
+
member,
|
|
138
|
+
}));
|
|
139
|
+
for reference in &file.references {
|
|
140
|
+
references_by_name
|
|
141
|
+
.entry(reference.name.clone())
|
|
142
|
+
.or_default()
|
|
143
|
+
.push(IndexedReference { path: path.clone() });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
extend_generated_provider_owner_references(&mut references_by_name, &declarations);
|
|
147
|
+
|
|
148
|
+
Self {
|
|
149
|
+
declarations,
|
|
150
|
+
members,
|
|
151
|
+
references_by_name,
|
|
152
|
+
library_by_path,
|
|
153
|
+
public_exports,
|
|
154
|
+
public_exported_declarations,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
fn reference_count(&self, name: &str, reachable_files: &BTreeSet<PathBuf>) -> usize {
|
|
159
|
+
self.references_by_name.get(name).map_or(0, |references| {
|
|
160
|
+
references
|
|
161
|
+
.iter()
|
|
162
|
+
.filter(|reference| reachable_files.contains(&reference.path))
|
|
163
|
+
.count()
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
fn library_reference_count(
|
|
168
|
+
&self,
|
|
169
|
+
name: &str,
|
|
170
|
+
library: &Path,
|
|
171
|
+
reachable_files: &BTreeSet<PathBuf>,
|
|
172
|
+
) -> usize {
|
|
173
|
+
self.references_by_name.get(name).map_or(0, |references| {
|
|
174
|
+
references
|
|
175
|
+
.iter()
|
|
176
|
+
.filter(|reference| reachable_files.contains(&reference.path))
|
|
177
|
+
.filter(|reference| self.library_path(&reference.path) == library)
|
|
178
|
+
.count()
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
fn library_path<'a>(&'a self, path: &'a Path) -> &'a Path {
|
|
183
|
+
self.library_by_path
|
|
184
|
+
.get(path)
|
|
185
|
+
.map_or(path, std::path::PathBuf::as_path)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
fn is_public_export(&self, path: &Path, name: &str) -> bool {
|
|
189
|
+
self.public_exports
|
|
190
|
+
.contains(&(path.to_path_buf(), name.to_owned()))
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/// Find the first conservative unused-export symbol findings.
|
|
194
|
+
#[must_use]
|
|
195
|
+
pub fn analyze_symbols(
|
|
196
|
+
project: &ScannedProject,
|
|
197
|
+
dead_code: Option<&DeadCodeReport>,
|
|
198
|
+
) -> SymbolReport {
|
|
199
|
+
analyze_symbols_with_options(project, dead_code, SymbolAnalysisOptions::default())
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/// Find conservative symbol findings with explicit analysis options.
|
|
203
|
+
#[must_use]
|
|
204
|
+
pub fn analyze_symbols_with_options(
|
|
205
|
+
project: &ScannedProject,
|
|
206
|
+
dead_code: Option<&DeadCodeReport>,
|
|
207
|
+
options: SymbolAnalysisOptions,
|
|
208
|
+
) -> SymbolReport {
|
|
209
|
+
let index = SymbolIndex::from_project(project);
|
|
210
|
+
let duplicate_exports = duplicate_exports_from_public(&index.public_exported_declarations);
|
|
211
|
+
let private_type_leaks = if options.private_type_leaks {
|
|
212
|
+
private_type_leaks(project, &index)
|
|
213
|
+
} else {
|
|
214
|
+
Vec::new()
|
|
215
|
+
};
|
|
216
|
+
let Some(dead_code) = dead_code else {
|
|
217
|
+
return SymbolReport {
|
|
218
|
+
unused_exports: Vec::new(),
|
|
219
|
+
unused_members: Vec::new(),
|
|
220
|
+
private_type_leaks,
|
|
221
|
+
duplicate_exports,
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
let unused_exports = unused_exports(project, dead_code, &index, options);
|
|
226
|
+
let unused_members = unused_members(project, dead_code, &index);
|
|
227
|
+
|
|
228
|
+
SymbolReport {
|
|
229
|
+
unused_exports,
|
|
230
|
+
unused_members,
|
|
231
|
+
private_type_leaks,
|
|
232
|
+
duplicate_exports,
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/// Find conservative unused-export and duplicate-export symbol findings.
|
|
237
|
+
#[must_use]
|
|
238
|
+
pub fn analyze_unused_exports(
|
|
239
|
+
project: &ScannedProject,
|
|
240
|
+
dead_code: &DeadCodeReport,
|
|
241
|
+
) -> SymbolReport {
|
|
242
|
+
analyze_symbols_with_options(project, Some(dead_code), SymbolAnalysisOptions::default())
|
|
243
|
+
}
|
|
244
|
+
fn unused_exports(
|
|
245
|
+
project: &ScannedProject,
|
|
246
|
+
dead_code: &DeadCodeReport,
|
|
247
|
+
index: &SymbolIndex,
|
|
248
|
+
options: SymbolAnalysisOptions,
|
|
249
|
+
) -> Vec<UnusedExport> {
|
|
250
|
+
let reachable_files = dead_code
|
|
251
|
+
.reachable_files
|
|
252
|
+
.iter()
|
|
253
|
+
.cloned()
|
|
254
|
+
.collect::<BTreeSet<_>>();
|
|
255
|
+
let entry_points = dead_code
|
|
256
|
+
.entry_points
|
|
257
|
+
.iter()
|
|
258
|
+
.cloned()
|
|
259
|
+
.collect::<BTreeSet<_>>();
|
|
260
|
+
let dead_files = dead_code
|
|
261
|
+
.dead_files
|
|
262
|
+
.iter()
|
|
263
|
+
.map(|dead_file| dead_file.path.clone())
|
|
264
|
+
.collect::<BTreeSet<_>>();
|
|
265
|
+
|
|
266
|
+
let mut unused_exports = index
|
|
267
|
+
.declarations
|
|
268
|
+
.iter()
|
|
269
|
+
.filter_map(|declaration| {
|
|
270
|
+
unused_export_from_declaration(
|
|
271
|
+
project,
|
|
272
|
+
index,
|
|
273
|
+
&reachable_files,
|
|
274
|
+
&entry_points,
|
|
275
|
+
&dead_files,
|
|
276
|
+
declaration,
|
|
277
|
+
options,
|
|
278
|
+
)
|
|
279
|
+
})
|
|
280
|
+
.collect::<Vec<_>>();
|
|
281
|
+
|
|
282
|
+
unused_exports.sort_by(|left, right| {
|
|
283
|
+
(
|
|
284
|
+
&left.path,
|
|
285
|
+
left.location.line,
|
|
286
|
+
left.location.column,
|
|
287
|
+
&left.name,
|
|
288
|
+
)
|
|
289
|
+
.cmp(&(
|
|
290
|
+
&right.path,
|
|
291
|
+
right.location.line,
|
|
292
|
+
right.location.column,
|
|
293
|
+
&right.name,
|
|
294
|
+
))
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
unused_exports
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
fn unused_members(
|
|
301
|
+
project: &ScannedProject,
|
|
302
|
+
dead_code: &DeadCodeReport,
|
|
303
|
+
index: &SymbolIndex,
|
|
304
|
+
) -> Vec<UnusedMember> {
|
|
305
|
+
let reachable_files = dead_code
|
|
306
|
+
.reachable_files
|
|
307
|
+
.iter()
|
|
308
|
+
.cloned()
|
|
309
|
+
.collect::<BTreeSet<_>>();
|
|
310
|
+
let dead_files = dead_code
|
|
311
|
+
.dead_files
|
|
312
|
+
.iter()
|
|
313
|
+
.map(|dead_file| dead_file.path.clone())
|
|
314
|
+
.collect::<BTreeSet<_>>();
|
|
315
|
+
|
|
316
|
+
let mut unused_members = index
|
|
317
|
+
.members
|
|
318
|
+
.iter()
|
|
319
|
+
.filter_map(|member| {
|
|
320
|
+
unused_member_from_declaration(project, index, &reachable_files, &dead_files, member)
|
|
321
|
+
})
|
|
322
|
+
.collect::<Vec<_>>();
|
|
323
|
+
|
|
324
|
+
unused_members.sort_by(|left, right| {
|
|
325
|
+
(
|
|
326
|
+
&left.path,
|
|
327
|
+
left.location.line,
|
|
328
|
+
left.location.column,
|
|
329
|
+
&left.owner,
|
|
330
|
+
&left.name,
|
|
331
|
+
)
|
|
332
|
+
.cmp(&(
|
|
333
|
+
&right.path,
|
|
334
|
+
right.location.line,
|
|
335
|
+
right.location.column,
|
|
336
|
+
&right.owner,
|
|
337
|
+
&right.name,
|
|
338
|
+
))
|
|
339
|
+
});
|
|
340
|
+
let mut seen = BTreeSet::new();
|
|
341
|
+
unused_members.retain(|member| {
|
|
342
|
+
seen.insert((
|
|
343
|
+
member.path.clone(),
|
|
344
|
+
member.owner.clone(),
|
|
345
|
+
member.name.clone(),
|
|
346
|
+
))
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
unused_members
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
fn unused_export_from_declaration(
|
|
353
|
+
project: &ScannedProject,
|
|
354
|
+
index: &SymbolIndex,
|
|
355
|
+
reachable_files: &BTreeSet<PathBuf>,
|
|
356
|
+
entry_points: &BTreeSet<PathBuf>,
|
|
357
|
+
dead_files: &BTreeSet<PathBuf>,
|
|
358
|
+
indexed: &IndexedDeclaration,
|
|
359
|
+
options: SymbolAnalysisOptions,
|
|
360
|
+
) -> Option<UnusedExport> {
|
|
361
|
+
if !indexed.path.starts_with(&project.root)
|
|
362
|
+
|| !reachable_files.contains(&indexed.path)
|
|
363
|
+
|| (entry_points.contains(&indexed.path)
|
|
364
|
+
&& (!options.include_entry_exports || indexed.declaration.name == "main"))
|
|
365
|
+
|| dead_files.contains(&indexed.path)
|
|
366
|
+
|| is_private(&indexed.declaration.name)
|
|
367
|
+
|| is_generated_dart_path(&indexed.path)
|
|
368
|
+
|| !is_library_source(&project.root, &indexed.path)
|
|
369
|
+
{
|
|
370
|
+
return None;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
let reference_count = index.reference_count(&indexed.declaration.name, reachable_files);
|
|
374
|
+
let public_export = index.is_public_export(&indexed.path, &indexed.declaration.name)
|
|
375
|
+
&& !(options.include_entry_exports && entry_points.contains(&indexed.path));
|
|
376
|
+
if reference_count != 0 || public_export {
|
|
377
|
+
return None;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
Some(UnusedExport {
|
|
381
|
+
path: indexed.path.clone(),
|
|
382
|
+
kind: indexed.declaration.kind,
|
|
383
|
+
name: indexed.declaration.name.clone(),
|
|
384
|
+
location: indexed.declaration.location,
|
|
385
|
+
reference_count,
|
|
386
|
+
safe_to_delete: is_simple_unused_declaration(index, indexed),
|
|
387
|
+
})
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
fn is_simple_unused_declaration(index: &SymbolIndex, indexed: &IndexedDeclaration) -> bool {
|
|
391
|
+
let range = indexed.declaration.range;
|
|
392
|
+
range.start_line == range.end_line
|
|
393
|
+
&& index
|
|
394
|
+
.declarations
|
|
395
|
+
.iter()
|
|
396
|
+
.filter(|candidate| {
|
|
397
|
+
candidate.path == indexed.path
|
|
398
|
+
&& candidate.declaration.location.line == indexed.declaration.location.line
|
|
399
|
+
})
|
|
400
|
+
.take(2)
|
|
401
|
+
.count()
|
|
402
|
+
== 1
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
fn unused_member_from_declaration(
|
|
406
|
+
project: &ScannedProject,
|
|
407
|
+
index: &SymbolIndex,
|
|
408
|
+
reachable_files: &BTreeSet<PathBuf>,
|
|
409
|
+
dead_files: &BTreeSet<PathBuf>,
|
|
410
|
+
indexed: &IndexedMember,
|
|
411
|
+
) -> Option<UnusedMember> {
|
|
412
|
+
if !indexed.path.starts_with(&project.root)
|
|
413
|
+
|| !reachable_files.contains(&indexed.path)
|
|
414
|
+
|| dead_files.contains(&indexed.path)
|
|
415
|
+
|| is_generated_dart_path(&indexed.path)
|
|
416
|
+
|| !is_library_source(&project.root, &indexed.path)
|
|
417
|
+
|| !should_check_member(index, indexed)
|
|
418
|
+
{
|
|
419
|
+
return None;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
let library = index.library_path(&indexed.path);
|
|
423
|
+
let reference_count =
|
|
424
|
+
index.library_reference_count(&indexed.member.name, library, reachable_files);
|
|
425
|
+
if reference_count != 0 {
|
|
426
|
+
return None;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
Some(UnusedMember {
|
|
430
|
+
path: indexed.path.clone(),
|
|
431
|
+
owner: indexed.member.owner.clone(),
|
|
432
|
+
kind: indexed.member.kind,
|
|
433
|
+
name: indexed.member.name.clone(),
|
|
434
|
+
location: indexed.member.location,
|
|
435
|
+
reference_count,
|
|
436
|
+
safe_to_delete: false,
|
|
437
|
+
})
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
fn should_check_member(index: &SymbolIndex, indexed: &IndexedMember) -> bool {
|
|
441
|
+
match indexed.member.kind {
|
|
442
|
+
MemberKind::EnumConstant => {
|
|
443
|
+
!is_private(&indexed.member.name)
|
|
444
|
+
&& !index.is_public_export(&indexed.path, &indexed.member.owner)
|
|
445
|
+
}
|
|
446
|
+
MemberKind::Field | MemberKind::Getter | MemberKind::Setter | MemberKind::Method => {
|
|
447
|
+
is_private(&indexed.member.name)
|
|
448
|
+
}
|
|
449
|
+
MemberKind::Constructor | MemberKind::Operator => false,
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
fn library_by_path(project: &ScannedProject) -> BTreeMap<PathBuf, PathBuf> {
|
|
454
|
+
let dependencies = project.graph.dependencies();
|
|
455
|
+
let part_edges = dependencies
|
|
456
|
+
.iter()
|
|
457
|
+
.filter(|edge| edge.kind == DependencyKind::Part)
|
|
458
|
+
.cloned()
|
|
459
|
+
.collect::<Vec<_>>();
|
|
460
|
+
let mut libraries = project
|
|
461
|
+
.files
|
|
462
|
+
.iter()
|
|
463
|
+
.map(|file| {
|
|
464
|
+
let path = normalize_against(&project.root, &file.path);
|
|
465
|
+
(path.clone(), path)
|
|
466
|
+
})
|
|
467
|
+
.collect::<BTreeMap<_, _>>();
|
|
468
|
+
|
|
469
|
+
for edge in &part_edges {
|
|
470
|
+
for path in library_paths(&part_edges, &edge.from_path) {
|
|
471
|
+
libraries.insert(path, edge.from_path.clone());
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
libraries
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
fn public_exported_declarations(project: &ScannedProject) -> Vec<PublicExportedDeclaration> {
|
|
479
|
+
let declarations = declarations_by_path(project);
|
|
480
|
+
let public_entry_files = project
|
|
481
|
+
.files
|
|
482
|
+
.iter()
|
|
483
|
+
.map(|file| normalize_against(&project.root, &file.path))
|
|
484
|
+
.filter(|path| is_public_library_entry(&project.root, path))
|
|
485
|
+
.collect::<BTreeSet<_>>();
|
|
486
|
+
|
|
487
|
+
let dependencies = project.graph.dependencies();
|
|
488
|
+
let export_edges = dependencies
|
|
489
|
+
.iter()
|
|
490
|
+
.filter(|edge| edge.kind == DependencyKind::Export)
|
|
491
|
+
.cloned()
|
|
492
|
+
.collect::<Vec<_>>();
|
|
493
|
+
let part_edges = dependencies
|
|
494
|
+
.iter()
|
|
495
|
+
.filter(|edge| edge.kind == DependencyKind::Part)
|
|
496
|
+
.cloned()
|
|
497
|
+
.collect::<Vec<_>>();
|
|
498
|
+
let mut exported = Vec::new();
|
|
499
|
+
for public_entry in public_entry_files {
|
|
500
|
+
extend_library_declarations(
|
|
501
|
+
&declarations,
|
|
502
|
+
&part_edges,
|
|
503
|
+
&public_entry,
|
|
504
|
+
&public_entry,
|
|
505
|
+
None,
|
|
506
|
+
&mut exported,
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
collect_public_exports(
|
|
510
|
+
&export_edges,
|
|
511
|
+
&part_edges,
|
|
512
|
+
&declarations,
|
|
513
|
+
&public_entry,
|
|
514
|
+
&public_entry,
|
|
515
|
+
&[],
|
|
516
|
+
&mut exported,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
exported
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
fn collect_public_exports(
|
|
524
|
+
export_edges: &[crate::ResolvedDependency],
|
|
525
|
+
part_edges: &[crate::ResolvedDependency],
|
|
526
|
+
declarations: &BTreeMap<PathBuf, Vec<TopLevelDeclaration>>,
|
|
527
|
+
entry_path: &Path,
|
|
528
|
+
from_path: &Path,
|
|
529
|
+
chain: &[crate::DependencyVisibility],
|
|
530
|
+
exported: &mut Vec<PublicExportedDeclaration>,
|
|
531
|
+
) {
|
|
532
|
+
if chain.len() > 8 {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
for edge in export_edges
|
|
537
|
+
.iter()
|
|
538
|
+
.filter(|edge| edge.from_path == from_path)
|
|
539
|
+
{
|
|
540
|
+
let mut next_chain = chain.to_owned();
|
|
541
|
+
next_chain.push(edge.visibility.clone());
|
|
542
|
+
|
|
543
|
+
extend_library_declarations(
|
|
544
|
+
declarations,
|
|
545
|
+
part_edges,
|
|
546
|
+
entry_path,
|
|
547
|
+
&edge.to_path,
|
|
548
|
+
Some(&next_chain),
|
|
549
|
+
exported,
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
collect_public_exports(
|
|
553
|
+
export_edges,
|
|
554
|
+
part_edges,
|
|
555
|
+
declarations,
|
|
556
|
+
entry_path,
|
|
557
|
+
&edge.to_path,
|
|
558
|
+
&next_chain,
|
|
559
|
+
exported,
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
fn extend_library_declarations(
|
|
565
|
+
declarations: &BTreeMap<PathBuf, Vec<TopLevelDeclaration>>,
|
|
566
|
+
part_edges: &[crate::ResolvedDependency],
|
|
567
|
+
entry_path: &Path,
|
|
568
|
+
library_path: &Path,
|
|
569
|
+
chain: Option<&[crate::DependencyVisibility]>,
|
|
570
|
+
exported: &mut Vec<PublicExportedDeclaration>,
|
|
571
|
+
) {
|
|
572
|
+
for path in library_paths(part_edges, library_path) {
|
|
573
|
+
let Some(declarations) = declarations.get(&path) else {
|
|
574
|
+
continue;
|
|
575
|
+
};
|
|
576
|
+
exported.extend(
|
|
577
|
+
declarations
|
|
578
|
+
.iter()
|
|
579
|
+
.filter(|declaration| {
|
|
580
|
+
chain.is_none_or(|chain| is_visible_through_chain(&declaration.name, chain))
|
|
581
|
+
})
|
|
582
|
+
.cloned()
|
|
583
|
+
.map(|declaration| PublicExportedDeclaration {
|
|
584
|
+
entry_path: entry_path.to_path_buf(),
|
|
585
|
+
path: path.clone(),
|
|
586
|
+
declaration,
|
|
587
|
+
}),
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
fn library_paths(part_edges: &[crate::ResolvedDependency], library_path: &Path) -> Vec<PathBuf> {
|
|
593
|
+
let mut paths = Vec::from([library_path.to_path_buf()]);
|
|
594
|
+
let mut seen = BTreeSet::from([library_path.to_path_buf()]);
|
|
595
|
+
let mut index = 0;
|
|
596
|
+
|
|
597
|
+
while let Some(path) = paths.get(index).cloned() {
|
|
598
|
+
index += 1;
|
|
599
|
+
for edge in part_edges.iter().filter(|edge| edge.from_path == path) {
|
|
600
|
+
if seen.insert(edge.to_path.clone()) {
|
|
601
|
+
paths.push(edge.to_path.clone());
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
paths
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
fn duplicate_exports_from_public(
|
|
610
|
+
exported_declarations: &[PublicExportedDeclaration],
|
|
611
|
+
) -> Vec<DuplicateExport> {
|
|
612
|
+
let mut grouped =
|
|
613
|
+
BTreeMap::<(PathBuf, String), BTreeMap<PathBuf, DuplicateExportDeclaration>>::new();
|
|
614
|
+
|
|
615
|
+
for exported in exported_declarations {
|
|
616
|
+
if is_private(&exported.declaration.name) {
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
grouped
|
|
621
|
+
.entry((
|
|
622
|
+
exported.entry_path.clone(),
|
|
623
|
+
exported.declaration.name.clone(),
|
|
624
|
+
))
|
|
625
|
+
.or_default()
|
|
626
|
+
.entry(exported.path.clone())
|
|
627
|
+
.or_insert_with(|| DuplicateExportDeclaration {
|
|
628
|
+
path: exported.path.clone(),
|
|
629
|
+
kind: exported.declaration.kind,
|
|
630
|
+
location: exported.declaration.location,
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
let mut duplicates = grouped
|
|
635
|
+
.into_iter()
|
|
636
|
+
.filter_map(|((entry_path, name), declarations_by_path)| {
|
|
637
|
+
if declarations_by_path.len() < 2 {
|
|
638
|
+
return None;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
Some(DuplicateExport {
|
|
642
|
+
entry_path,
|
|
643
|
+
name,
|
|
644
|
+
declarations: declarations_by_path.into_values().collect(),
|
|
645
|
+
safe_to_delete: false,
|
|
646
|
+
})
|
|
647
|
+
})
|
|
648
|
+
.collect::<Vec<_>>();
|
|
649
|
+
|
|
650
|
+
duplicates.sort_by(|left, right| {
|
|
651
|
+
(&left.entry_path, &left.name).cmp(&(&right.entry_path, &right.name))
|
|
652
|
+
});
|
|
653
|
+
duplicates
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
fn declarations_by_path(project: &ScannedProject) -> BTreeMap<PathBuf, Vec<TopLevelDeclaration>> {
|
|
657
|
+
project
|
|
658
|
+
.files
|
|
659
|
+
.iter()
|
|
660
|
+
.map(|file| {
|
|
661
|
+
(
|
|
662
|
+
normalize_against(&project.root, &file.path),
|
|
663
|
+
file.declarations.clone(),
|
|
664
|
+
)
|
|
665
|
+
})
|
|
666
|
+
.collect()
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
fn is_visible_through_export(name: &str, combinators: &[crate::DartCombinator]) -> bool {
|
|
670
|
+
let mut visible = true;
|
|
671
|
+
for combinator in combinators {
|
|
672
|
+
match combinator.kind {
|
|
673
|
+
DartCombinatorKind::Show => {
|
|
674
|
+
visible = combinator.names.iter().any(|shown| shown == name);
|
|
675
|
+
}
|
|
676
|
+
DartCombinatorKind::Hide => {
|
|
677
|
+
if combinator.names.iter().any(|hidden| hidden == name) {
|
|
678
|
+
visible = false;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
visible
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
fn is_visible_through_chain(name: &str, chain: &[crate::DependencyVisibility]) -> bool {
|
|
687
|
+
chain
|
|
688
|
+
.iter()
|
|
689
|
+
.all(|visibility| is_visible_through_export(name, &visibility.combinators))
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
#[cfg(test)]
|
|
693
|
+
mod tests;
|