dart-decimate 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
use std::path::Path;
|
|
2
|
+
|
|
3
|
+
use tree_sitter::Node;
|
|
4
|
+
|
|
5
|
+
use crate::WidgetTopLevelFunction;
|
|
6
|
+
|
|
7
|
+
pub(super) fn top_level_widget_functions(
|
|
8
|
+
path: &Path,
|
|
9
|
+
root: Node<'_>,
|
|
10
|
+
source: &str,
|
|
11
|
+
has_widget_class: bool,
|
|
12
|
+
) -> Vec<WidgetTopLevelFunction> {
|
|
13
|
+
if !has_widget_class && !path_looks_like_widget_file(path) && !imports_flutter_ui(root, source)
|
|
14
|
+
{
|
|
15
|
+
return Vec::new();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let mut functions = Vec::new();
|
|
19
|
+
let mut cursor = root.walk();
|
|
20
|
+
for child in root.named_children(&mut cursor) {
|
|
21
|
+
if !matches!(
|
|
22
|
+
child.kind(),
|
|
23
|
+
"function_declaration" | "external_function_declaration"
|
|
24
|
+
) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
let Some(function) = function_candidate(path, child, source) else {
|
|
28
|
+
continue;
|
|
29
|
+
};
|
|
30
|
+
functions.push(function);
|
|
31
|
+
}
|
|
32
|
+
functions
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn function_candidate(path: &Path, node: Node<'_>, source: &str) -> Option<WidgetTopLevelFunction> {
|
|
36
|
+
let signature = node.child_by_field_name("signature")?;
|
|
37
|
+
let name_node = signature.child_by_field_name("name")?;
|
|
38
|
+
let name = name_node.utf8_text(source.as_bytes()).ok()?.to_owned();
|
|
39
|
+
if name == "main" {
|
|
40
|
+
return None;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let signature_text = signature.utf8_text(source.as_bytes()).ok()?;
|
|
44
|
+
let return_type = declared_return_type(signature, name_node, source);
|
|
45
|
+
let returns_widget = return_type.as_deref().is_some_and(return_type_has_ui_type);
|
|
46
|
+
let build_helper =
|
|
47
|
+
is_build_helper_name(&name) && (signature_text.contains("BuildContext") || returns_widget);
|
|
48
|
+
if !build_helper && !returns_widget {
|
|
49
|
+
return None;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Some(WidgetTopLevelFunction {
|
|
53
|
+
path: path.to_path_buf(),
|
|
54
|
+
function_name: name,
|
|
55
|
+
return_type,
|
|
56
|
+
location: name_node.start_position().into(),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn declared_return_type(signature: Node<'_>, name: Node<'_>, source: &str) -> Option<String> {
|
|
61
|
+
let before_name = source
|
|
62
|
+
.get(signature.start_byte()..name.start_byte())?
|
|
63
|
+
.trim();
|
|
64
|
+
(!before_name.is_empty()).then(|| before_name.to_owned())
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fn return_type_has_ui_type(return_type: &str) -> bool {
|
|
68
|
+
return_type
|
|
69
|
+
.split(|character: char| !(character.is_ascii_alphanumeric() || character == '_'))
|
|
70
|
+
.any(|token| {
|
|
71
|
+
matches!(
|
|
72
|
+
token,
|
|
73
|
+
"Widget" | "PreferredSizeWidget" | "SliverPersistentHeaderDelegate"
|
|
74
|
+
)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn is_build_helper_name(name: &str) -> bool {
|
|
79
|
+
name.strip_prefix("_build")
|
|
80
|
+
.and_then(|suffix| suffix.chars().next())
|
|
81
|
+
.is_some_and(|character| character.is_ascii_uppercase() || character.is_ascii_digit())
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn path_looks_like_widget_file(path: &Path) -> bool {
|
|
85
|
+
let file_name = path
|
|
86
|
+
.file_name()
|
|
87
|
+
.and_then(|name| name.to_str())
|
|
88
|
+
.unwrap_or("");
|
|
89
|
+
["_screen.dart", "_page.dart", "_view.dart", "_widget.dart"]
|
|
90
|
+
.iter()
|
|
91
|
+
.any(|suffix| file_name.ends_with(suffix))
|
|
92
|
+
|| path.components().any(|component| {
|
|
93
|
+
component.as_os_str().to_str().is_some_and(|segment| {
|
|
94
|
+
matches!(segment, "presentation" | "screen" | "screens" | "widgets")
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn imports_flutter_ui(root: Node<'_>, source: &str) -> bool {
|
|
100
|
+
let mut cursor = root.walk();
|
|
101
|
+
root.named_children(&mut cursor)
|
|
102
|
+
.filter(|child| child.kind().contains("import"))
|
|
103
|
+
.filter_map(|child| child.utf8_text(source.as_bytes()).ok())
|
|
104
|
+
.any(|text| {
|
|
105
|
+
text.contains("package:flutter/")
|
|
106
|
+
|| text.contains("package:flutter_hooks/")
|
|
107
|
+
|| text.contains("package:flutter_riverpod/")
|
|
108
|
+
|| text.contains("package:hooks_riverpod/")
|
|
109
|
+
})
|
|
110
|
+
}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::path::{Path, PathBuf};
|
|
4
|
+
|
|
5
|
+
use rayon::prelude::*;
|
|
6
|
+
use tree_sitter::Node;
|
|
7
|
+
|
|
8
|
+
use crate::graph::normalize_against;
|
|
9
|
+
use crate::{DartCombinatorKind, DependencyKind, ScannedProject, TopLevelDeclaration};
|
|
10
|
+
|
|
11
|
+
use super::{
|
|
12
|
+
UnrenderedWidgetClass, WidgetAnalysisError, collect_class_declarations, has_ancestor_kind,
|
|
13
|
+
parse_tree, simple_type_name, visit_named, widget_kind,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
|
17
|
+
struct FileReachabilityFacts {
|
|
18
|
+
widgets: Vec<UnrenderedWidgetClass>,
|
|
19
|
+
object_constructors: Vec<String>,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
pub(super) fn unrendered_widgets(
|
|
23
|
+
project: &ScannedProject,
|
|
24
|
+
paths: &[PathBuf],
|
|
25
|
+
) -> Result<Vec<UnrenderedWidgetClass>, WidgetAnalysisError> {
|
|
26
|
+
let files = paths
|
|
27
|
+
.par_iter()
|
|
28
|
+
.map(|path| reachability_facts(path))
|
|
29
|
+
.collect::<Result<Vec<_>, _>>()?;
|
|
30
|
+
let exported = public_reexported_declarations(project);
|
|
31
|
+
let mut candidates = files
|
|
32
|
+
.iter()
|
|
33
|
+
.flat_map(|file| file.widgets.iter().cloned())
|
|
34
|
+
.filter(|widget| !exported.contains(&(widget.path.clone(), widget.widget_class.clone())))
|
|
35
|
+
.collect::<Vec<_>>();
|
|
36
|
+
let candidate_names = candidates
|
|
37
|
+
.iter()
|
|
38
|
+
.map(|widget| widget.widget_class.clone())
|
|
39
|
+
.collect::<BTreeSet<_>>();
|
|
40
|
+
let render_counts = render_counts(&files, &candidate_names);
|
|
41
|
+
|
|
42
|
+
candidates.retain_mut(|widget| {
|
|
43
|
+
widget.render_reference_count = render_counts
|
|
44
|
+
.get(&widget.widget_class)
|
|
45
|
+
.copied()
|
|
46
|
+
.unwrap_or_default();
|
|
47
|
+
widget.render_reference_count == 0
|
|
48
|
+
});
|
|
49
|
+
candidates.sort_by(|left, right| {
|
|
50
|
+
(
|
|
51
|
+
&left.path,
|
|
52
|
+
left.location.line,
|
|
53
|
+
left.location.column,
|
|
54
|
+
&left.widget_class,
|
|
55
|
+
)
|
|
56
|
+
.cmp(&(
|
|
57
|
+
&right.path,
|
|
58
|
+
right.location.line,
|
|
59
|
+
right.location.column,
|
|
60
|
+
&right.widget_class,
|
|
61
|
+
))
|
|
62
|
+
});
|
|
63
|
+
Ok(candidates)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fn reachability_facts(path: &Path) -> Result<FileReachabilityFacts, WidgetAnalysisError> {
|
|
67
|
+
let source = fs::read_to_string(path).map_err(|source| WidgetAnalysisError::ReadFile {
|
|
68
|
+
path: path.to_path_buf(),
|
|
69
|
+
source,
|
|
70
|
+
})?;
|
|
71
|
+
let parsed = parse_tree(path, &source)?;
|
|
72
|
+
let root = parsed.tree().root_node();
|
|
73
|
+
|
|
74
|
+
Ok(FileReachabilityFacts {
|
|
75
|
+
widgets: widget_classes(path, root, parsed.source()),
|
|
76
|
+
object_constructors: object_constructor_names(root, parsed.source()),
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn widget_classes(path: &Path, root: Node<'_>, source: &str) -> Vec<UnrenderedWidgetClass> {
|
|
81
|
+
let mut classes = Vec::new();
|
|
82
|
+
collect_class_declarations(root, &mut classes);
|
|
83
|
+
classes
|
|
84
|
+
.into_iter()
|
|
85
|
+
.filter_map(|class| widget_class(path, class, source))
|
|
86
|
+
.collect()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn widget_class(path: &Path, class: Node<'_>, source: &str) -> Option<UnrenderedWidgetClass> {
|
|
90
|
+
if is_abstract_class(class, source) {
|
|
91
|
+
return None;
|
|
92
|
+
}
|
|
93
|
+
let widget_kind = widget_kind(class, source)?;
|
|
94
|
+
let name_node = class.child_by_field_name("name")?;
|
|
95
|
+
let widget_class = name_node.utf8_text(source.as_bytes()).ok()?.to_owned();
|
|
96
|
+
Some(UnrenderedWidgetClass {
|
|
97
|
+
path: path.to_path_buf(),
|
|
98
|
+
widget_class,
|
|
99
|
+
widget_kind,
|
|
100
|
+
location: name_node.start_position().into(),
|
|
101
|
+
render_reference_count: 0,
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fn is_abstract_class(class: Node<'_>, source: &str) -> bool {
|
|
106
|
+
let Some(name) = class.child_by_field_name("name") else {
|
|
107
|
+
return false;
|
|
108
|
+
};
|
|
109
|
+
source
|
|
110
|
+
.get(class.start_byte()..name.start_byte())
|
|
111
|
+
.unwrap_or_default()
|
|
112
|
+
.split_whitespace()
|
|
113
|
+
.any(|token| token == "abstract")
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn object_constructor_names(root: Node<'_>, source: &str) -> Vec<String> {
|
|
117
|
+
let mut constructors = Vec::new();
|
|
118
|
+
visit_named(root, &mut |node| {
|
|
119
|
+
if is_object_constructor(node)
|
|
120
|
+
&& !has_ancestor_kind(node, &["annotation"])
|
|
121
|
+
&& let Some(constructor) = constructor_type_name(node, source)
|
|
122
|
+
{
|
|
123
|
+
constructors.extend(constructor_name_candidates(&constructor));
|
|
124
|
+
} else if is_arrow_body_constructor_identifier(node, source)
|
|
125
|
+
&& let Ok(constructor) = node.utf8_text(source.as_bytes())
|
|
126
|
+
{
|
|
127
|
+
constructors.extend(constructor_name_candidates(constructor));
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
constructors
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn is_object_constructor(node: Node<'_>) -> bool {
|
|
134
|
+
matches!(
|
|
135
|
+
node.kind(),
|
|
136
|
+
"call_expression"
|
|
137
|
+
| "constructor_invocation"
|
|
138
|
+
| "const_object_expression"
|
|
139
|
+
| "function_expression_invocation"
|
|
140
|
+
| "new_expression"
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fn constructor_type_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
145
|
+
let arguments = node.child_by_field_name("arguments")?;
|
|
146
|
+
let prefix = source.get(node.start_byte()..arguments.start_byte())?;
|
|
147
|
+
let constructor = prefix
|
|
148
|
+
.trim()
|
|
149
|
+
.strip_prefix("const ")
|
|
150
|
+
.or_else(|| prefix.trim().strip_prefix("new "))
|
|
151
|
+
.unwrap_or(prefix.trim())
|
|
152
|
+
.split('<')
|
|
153
|
+
.next()
|
|
154
|
+
.unwrap_or("")
|
|
155
|
+
.replace(' ', "");
|
|
156
|
+
(!constructor.is_empty()).then_some(constructor)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
fn is_arrow_body_constructor_identifier(node: Node<'_>, source: &str) -> bool {
|
|
160
|
+
if !matches!(node.kind(), "identifier" | "type_identifier")
|
|
161
|
+
|| node
|
|
162
|
+
.parent()
|
|
163
|
+
.is_none_or(|parent| parent.kind() != "function_expression_body")
|
|
164
|
+
{
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
let Some(suffix) = source.get(node.end_byte()..) else {
|
|
168
|
+
return false;
|
|
169
|
+
};
|
|
170
|
+
suffix.trim_start().starts_with("()")
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fn constructor_name_candidates(constructor: &str) -> Vec<String> {
|
|
174
|
+
constructor
|
|
175
|
+
.split('.')
|
|
176
|
+
.map(simple_type_name)
|
|
177
|
+
.filter(|segment| {
|
|
178
|
+
segment
|
|
179
|
+
.chars()
|
|
180
|
+
.next()
|
|
181
|
+
.is_some_and(|first| first == '_' || first.is_ascii_uppercase())
|
|
182
|
+
})
|
|
183
|
+
.collect()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
fn render_counts(
|
|
187
|
+
files: &[FileReachabilityFacts],
|
|
188
|
+
candidate_names: &BTreeSet<String>,
|
|
189
|
+
) -> BTreeMap<String, usize> {
|
|
190
|
+
let mut counts = BTreeMap::<String, usize>::new();
|
|
191
|
+
for name in files
|
|
192
|
+
.iter()
|
|
193
|
+
.flat_map(|file| file.object_constructors.iter())
|
|
194
|
+
.filter(|name| candidate_names.contains(*name))
|
|
195
|
+
{
|
|
196
|
+
*counts.entry(name.clone()).or_default() += 1;
|
|
197
|
+
}
|
|
198
|
+
counts
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
fn public_reexported_declarations(project: &ScannedProject) -> BTreeSet<(PathBuf, String)> {
|
|
202
|
+
let declarations = project
|
|
203
|
+
.files
|
|
204
|
+
.iter()
|
|
205
|
+
.map(|file| {
|
|
206
|
+
(
|
|
207
|
+
normalize_against(&project.root, &file.path),
|
|
208
|
+
file.declarations.clone(),
|
|
209
|
+
)
|
|
210
|
+
})
|
|
211
|
+
.collect::<BTreeMap<_, _>>();
|
|
212
|
+
let export_edges = project
|
|
213
|
+
.graph
|
|
214
|
+
.dependencies()
|
|
215
|
+
.into_iter()
|
|
216
|
+
.filter(|edge| edge.kind == DependencyKind::Export)
|
|
217
|
+
.collect::<Vec<_>>();
|
|
218
|
+
let public_entries = project
|
|
219
|
+
.files
|
|
220
|
+
.iter()
|
|
221
|
+
.map(|file| normalize_against(&project.root, &file.path))
|
|
222
|
+
.filter(|path| is_public_library_entry(&project.root, path))
|
|
223
|
+
.collect::<BTreeSet<_>>();
|
|
224
|
+
let mut exported = BTreeSet::new();
|
|
225
|
+
for entry in public_entries {
|
|
226
|
+
collect_reexports(&declarations, &export_edges, &entry, &[], 0, &mut exported);
|
|
227
|
+
}
|
|
228
|
+
exported
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
fn collect_reexports(
|
|
232
|
+
declarations: &BTreeMap<PathBuf, Vec<TopLevelDeclaration>>,
|
|
233
|
+
export_edges: &[crate::ResolvedDependency],
|
|
234
|
+
from_path: &Path,
|
|
235
|
+
chain: &[crate::DependencyVisibility],
|
|
236
|
+
depth: usize,
|
|
237
|
+
exported: &mut BTreeSet<(PathBuf, String)>,
|
|
238
|
+
) {
|
|
239
|
+
if depth > 8 {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
for edge in export_edges
|
|
244
|
+
.iter()
|
|
245
|
+
.filter(|edge| edge.from_path == from_path)
|
|
246
|
+
{
|
|
247
|
+
let mut next_chain = chain.to_owned();
|
|
248
|
+
next_chain.push(edge.visibility.clone());
|
|
249
|
+
if let Some(declarations) = declarations.get(&edge.to_path) {
|
|
250
|
+
exported.extend(
|
|
251
|
+
declarations
|
|
252
|
+
.iter()
|
|
253
|
+
.filter(|declaration| is_visible(&declaration.name, &next_chain))
|
|
254
|
+
.map(|declaration| (edge.to_path.clone(), declaration.name.clone())),
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
collect_reexports(
|
|
258
|
+
declarations,
|
|
259
|
+
export_edges,
|
|
260
|
+
&edge.to_path,
|
|
261
|
+
&next_chain,
|
|
262
|
+
depth + 1,
|
|
263
|
+
exported,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
fn is_visible(name: &str, chain: &[crate::DependencyVisibility]) -> bool {
|
|
269
|
+
chain
|
|
270
|
+
.iter()
|
|
271
|
+
.all(|visibility| is_visible_through_export(name, &visibility.combinators))
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn is_visible_through_export(name: &str, combinators: &[crate::DartCombinator]) -> bool {
|
|
275
|
+
let mut visible = true;
|
|
276
|
+
for combinator in combinators {
|
|
277
|
+
match combinator.kind {
|
|
278
|
+
DartCombinatorKind::Show => {
|
|
279
|
+
visible = combinator.names.iter().any(|shown| shown == name);
|
|
280
|
+
}
|
|
281
|
+
DartCombinatorKind::Hide => {
|
|
282
|
+
if combinator.names.iter().any(|hidden| hidden == name) {
|
|
283
|
+
visible = false;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
visible
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
fn is_public_library_entry(root: &Path, path: &Path) -> bool {
|
|
292
|
+
path.strip_prefix(root).is_ok_and(|relative| {
|
|
293
|
+
let mut components = relative.components();
|
|
294
|
+
components
|
|
295
|
+
.next()
|
|
296
|
+
.is_some_and(|component| component.as_os_str() == "lib")
|
|
297
|
+
&& components
|
|
298
|
+
.next()
|
|
299
|
+
.is_none_or(|component| component.as_os_str() != "src")
|
|
300
|
+
&& relative.extension().is_some_and(|ext| ext == "dart")
|
|
301
|
+
})
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
#[cfg(test)]
|
|
305
|
+
mod tests {
|
|
306
|
+
use super::*;
|
|
307
|
+
|
|
308
|
+
#[test]
|
|
309
|
+
fn constructor_name_candidates_handle_prefixes_and_named_constructors() {
|
|
310
|
+
assert_eq!(constructor_name_candidates("DeadCard"), vec!["DeadCard"]);
|
|
311
|
+
assert_eq!(constructor_name_candidates("ui.LiveCard"), vec!["LiveCard"]);
|
|
312
|
+
assert_eq!(
|
|
313
|
+
constructor_name_candidates("DeadCard.named"),
|
|
314
|
+
vec!["DeadCard"]
|
|
315
|
+
);
|
|
316
|
+
assert_eq!(
|
|
317
|
+
constructor_name_candidates("ui.LiveCard.named"),
|
|
318
|
+
vec!["LiveCard"]
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#[test]
|
|
323
|
+
fn constructor_name_candidates_ignore_lowercase_calls() {
|
|
324
|
+
assert!(constructor_name_candidates("buildHeader").is_empty());
|
|
325
|
+
assert!(constructor_name_candidates("context.watch").is_empty());
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
#[test]
|
|
329
|
+
fn object_constructor_names_ignore_type_only_references()
|
|
330
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
331
|
+
let source = r"
|
|
332
|
+
class Host extends StatefulWidget {
|
|
333
|
+
State<DeadCard>? cachedState;
|
|
334
|
+
Widget build(BuildContext context) => const LiveCard();
|
|
335
|
+
}
|
|
336
|
+
final Type marker = DeadCard;
|
|
337
|
+
List<DeadCard> cached = [];
|
|
338
|
+
void main() {
|
|
339
|
+
new LegacyCard();
|
|
340
|
+
ui.PrefixedCard();
|
|
341
|
+
DeadCard.named();
|
|
342
|
+
DeadCard.route;
|
|
343
|
+
}
|
|
344
|
+
";
|
|
345
|
+
let parsed = parse_tree(Path::new("lib/widgets.dart"), source)?;
|
|
346
|
+
let names = object_constructor_names(parsed.tree().root_node(), parsed.source());
|
|
347
|
+
|
|
348
|
+
assert_eq!(
|
|
349
|
+
names,
|
|
350
|
+
vec!["LiveCard", "LegacyCard", "PrefixedCard", "DeadCard"]
|
|
351
|
+
);
|
|
352
|
+
Ok(())
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
#[test]
|
|
356
|
+
fn object_constructor_names_include_material_page_route_builders()
|
|
357
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
358
|
+
let source = r"
|
|
359
|
+
void open(BuildContext context) {
|
|
360
|
+
Navigator.push(
|
|
361
|
+
context,
|
|
362
|
+
MaterialPageRoute(
|
|
363
|
+
builder: (BuildContext context) => CartScreen(),
|
|
364
|
+
),
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
";
|
|
368
|
+
let parsed = parse_tree(Path::new("lib/catalog.dart"), source)?;
|
|
369
|
+
let names = object_constructor_names(parsed.tree().root_node(), parsed.source());
|
|
370
|
+
|
|
371
|
+
assert!(names.iter().any(|name| name == "CartScreen"));
|
|
372
|
+
Ok(())
|
|
373
|
+
}
|
|
374
|
+
}
|