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,691 @@
|
|
|
1
|
+
use std::collections::BTreeMap;
|
|
2
|
+
|
|
3
|
+
use serde::{Deserialize, Serialize};
|
|
4
|
+
use tree_sitter::Node;
|
|
5
|
+
|
|
6
|
+
use super::{Location, field_text};
|
|
7
|
+
|
|
8
|
+
/// A `GoRouter` route declaration found in Dart metadata or route trees.
|
|
9
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
10
|
+
pub struct DartRouteDeclaration {
|
|
11
|
+
/// Generic route data class, such as `HomeRoute`.
|
|
12
|
+
pub route_class: String,
|
|
13
|
+
/// Canonical full path when the route path is statically known.
|
|
14
|
+
pub path: Option<String>,
|
|
15
|
+
/// Raw `path:` argument expression.
|
|
16
|
+
pub path_expression: String,
|
|
17
|
+
/// Optional literal route name.
|
|
18
|
+
pub name: Option<String>,
|
|
19
|
+
/// Location of the route annotation or constructor call.
|
|
20
|
+
pub location: Location,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
pub(super) fn extract_route_declarations(
|
|
24
|
+
root: Node<'_>,
|
|
25
|
+
source: &str,
|
|
26
|
+
) -> Vec<DartRouteDeclaration> {
|
|
27
|
+
let constants = collect_string_constants(root, source);
|
|
28
|
+
let mut routes = Vec::new();
|
|
29
|
+
let mut cursor = root.walk();
|
|
30
|
+
for child in root.named_children(&mut cursor) {
|
|
31
|
+
if child.kind() != "class_declaration" {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let class_name = field_text(child, "name", source);
|
|
35
|
+
let mut class_cursor = child.walk();
|
|
36
|
+
for annotation in child
|
|
37
|
+
.named_children(&mut class_cursor)
|
|
38
|
+
.filter(|node| node.kind() == "annotation")
|
|
39
|
+
{
|
|
40
|
+
let Ok(text) = annotation.utf8_text(source.as_bytes()) else {
|
|
41
|
+
continue;
|
|
42
|
+
};
|
|
43
|
+
collect_routes_in_text(
|
|
44
|
+
text,
|
|
45
|
+
0,
|
|
46
|
+
None,
|
|
47
|
+
annotation.start_position().into(),
|
|
48
|
+
class_name.as_deref(),
|
|
49
|
+
&constants,
|
|
50
|
+
&mut routes,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
collect_raw_route_declarations(root, source, &constants, &mut routes);
|
|
55
|
+
routes
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fn collect_raw_route_declarations(
|
|
59
|
+
root: Node<'_>,
|
|
60
|
+
source: &str,
|
|
61
|
+
constants: &BTreeMap<String, String>,
|
|
62
|
+
routes: &mut Vec<DartRouteDeclaration>,
|
|
63
|
+
) {
|
|
64
|
+
collect_raw_routes_in(root, source, constants, None, true, routes);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fn collect_raw_routes_in(
|
|
68
|
+
node: Node<'_>,
|
|
69
|
+
source: &str,
|
|
70
|
+
constants: &BTreeMap<String, String>,
|
|
71
|
+
parent_path: Option<&str>,
|
|
72
|
+
parent_resolved: bool,
|
|
73
|
+
routes: &mut Vec<DartRouteDeclaration>,
|
|
74
|
+
) {
|
|
75
|
+
if is_object_constructor(node)
|
|
76
|
+
&& let Some(route_kind) = raw_route_constructor_kind(node, source)
|
|
77
|
+
{
|
|
78
|
+
let next_parent = match route_kind {
|
|
79
|
+
RawRouteKind::GoRoute => collect_go_route(
|
|
80
|
+
node,
|
|
81
|
+
source,
|
|
82
|
+
constants,
|
|
83
|
+
parent_path,
|
|
84
|
+
parent_resolved,
|
|
85
|
+
routes,
|
|
86
|
+
),
|
|
87
|
+
RawRouteKind::RouteContainer => None,
|
|
88
|
+
};
|
|
89
|
+
let next_parent_resolved = match route_kind {
|
|
90
|
+
RawRouteKind::GoRoute => next_parent.is_some(),
|
|
91
|
+
RawRouteKind::RouteContainer => parent_resolved,
|
|
92
|
+
};
|
|
93
|
+
for child_routes_arg in route_child_argument_nodes(node, source) {
|
|
94
|
+
collect_raw_routes_in(
|
|
95
|
+
child_routes_arg,
|
|
96
|
+
source,
|
|
97
|
+
constants,
|
|
98
|
+
next_parent.as_deref().or(parent_path),
|
|
99
|
+
next_parent_resolved,
|
|
100
|
+
routes,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let mut cursor = node.walk();
|
|
107
|
+
for child in node.named_children(&mut cursor) {
|
|
108
|
+
collect_raw_routes_in(
|
|
109
|
+
child,
|
|
110
|
+
source,
|
|
111
|
+
constants,
|
|
112
|
+
parent_path,
|
|
113
|
+
parent_resolved,
|
|
114
|
+
routes,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
120
|
+
enum RawRouteKind {
|
|
121
|
+
GoRoute,
|
|
122
|
+
RouteContainer,
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn is_object_constructor(node: Node<'_>) -> bool {
|
|
126
|
+
matches!(
|
|
127
|
+
node.kind(),
|
|
128
|
+
"call_expression" | "constructor_invocation" | "const_object_expression" | "new_expression"
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fn raw_route_constructor_kind(node: Node<'_>, source: &str) -> Option<RawRouteKind> {
|
|
133
|
+
let route_type = constructor_type_name(node, source)?;
|
|
134
|
+
if route_type.split('.').any(|segment| segment == "GoRoute") {
|
|
135
|
+
return Some(RawRouteKind::GoRoute);
|
|
136
|
+
}
|
|
137
|
+
if route_type.split('.').any(|segment| {
|
|
138
|
+
matches!(
|
|
139
|
+
segment,
|
|
140
|
+
"ShellRoute" | "StatefulShellRoute" | "StatefulShellBranch"
|
|
141
|
+
)
|
|
142
|
+
}) {
|
|
143
|
+
return Some(RawRouteKind::RouteContainer);
|
|
144
|
+
}
|
|
145
|
+
None
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn constructor_type_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
149
|
+
let arguments = node.child_by_field_name("arguments")?;
|
|
150
|
+
let prefix = source.get(node.start_byte()..arguments.start_byte())?;
|
|
151
|
+
let route_type = prefix
|
|
152
|
+
.trim()
|
|
153
|
+
.strip_prefix("const ")
|
|
154
|
+
.or_else(|| prefix.trim().strip_prefix("new "))
|
|
155
|
+
.unwrap_or(prefix.trim())
|
|
156
|
+
.split('<')
|
|
157
|
+
.next()
|
|
158
|
+
.unwrap_or("")
|
|
159
|
+
.replace(' ', "");
|
|
160
|
+
(!route_type.is_empty()).then_some(route_type)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
fn route_child_argument_nodes<'a>(node: Node<'a>, source: &str) -> Vec<Node<'a>> {
|
|
164
|
+
["routes", "branches"]
|
|
165
|
+
.into_iter()
|
|
166
|
+
.filter_map(|name| named_argument_node(node, name, source))
|
|
167
|
+
.collect()
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn collect_go_route(
|
|
171
|
+
node: Node<'_>,
|
|
172
|
+
source: &str,
|
|
173
|
+
constants: &BTreeMap<String, String>,
|
|
174
|
+
parent_path: Option<&str>,
|
|
175
|
+
parent_resolved: bool,
|
|
176
|
+
routes: &mut Vec<DartRouteDeclaration>,
|
|
177
|
+
) -> Option<String> {
|
|
178
|
+
let path_node = named_argument_node(node, "path", source)?;
|
|
179
|
+
let path_expression = expression_text_from_named_argument(path_node, source)?;
|
|
180
|
+
let path_segment = resolve_path_expression(&path_expression, constants);
|
|
181
|
+
let full_path = path_segment
|
|
182
|
+
.as_deref()
|
|
183
|
+
.filter(|_| parent_resolved)
|
|
184
|
+
.map(|segment| join_route_path(parent_path, segment));
|
|
185
|
+
let name = named_argument_node(node, "name", source)
|
|
186
|
+
.and_then(|name_node| expression_text_from_named_argument(name_node, source))
|
|
187
|
+
.and_then(|value| unquote_dart_string(value.trim()));
|
|
188
|
+
routes.push(DartRouteDeclaration {
|
|
189
|
+
route_class: "GoRoute".to_owned(),
|
|
190
|
+
path: full_path.clone(),
|
|
191
|
+
path_expression,
|
|
192
|
+
name,
|
|
193
|
+
location: node.start_position().into(),
|
|
194
|
+
});
|
|
195
|
+
full_path
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
fn named_argument_node<'a>(node: Node<'a>, name: &str, source: &str) -> Option<Node<'a>> {
|
|
199
|
+
let arguments = node.child_by_field_name("arguments")?;
|
|
200
|
+
let mut cursor = arguments.walk();
|
|
201
|
+
arguments.named_children(&mut cursor).find(|child| {
|
|
202
|
+
child.kind() == "named_argument" && named_argument_label(*child, source) == Some(name)
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
fn named_argument_label<'a>(node: Node<'a>, source: &'a str) -> Option<&'a str> {
|
|
207
|
+
let mut cursor = node.walk();
|
|
208
|
+
node.named_children(&mut cursor)
|
|
209
|
+
.find(|child| child.kind() == "label")
|
|
210
|
+
.and_then(|label| label.utf8_text(source.as_bytes()).ok())
|
|
211
|
+
.map(str::trim)
|
|
212
|
+
.and_then(|label| label.strip_suffix(':'))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fn expression_text_from_named_argument(node: Node<'_>, source: &str) -> Option<String> {
|
|
216
|
+
let mut cursor = node.walk();
|
|
217
|
+
node.named_children(&mut cursor)
|
|
218
|
+
.find(|child| child.kind() != "label")
|
|
219
|
+
.and_then(|expression| expression.utf8_text(source.as_bytes()).ok())
|
|
220
|
+
.map(str::to_owned)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn collect_string_constants(root: Node<'_>, source: &str) -> BTreeMap<String, String> {
|
|
224
|
+
let mut constants = BTreeMap::new();
|
|
225
|
+
collect_string_constants_in(root, source, &mut constants);
|
|
226
|
+
constants
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
fn collect_string_constants_in(
|
|
230
|
+
node: Node<'_>,
|
|
231
|
+
source: &str,
|
|
232
|
+
constants: &mut BTreeMap<String, String>,
|
|
233
|
+
) {
|
|
234
|
+
if matches!(
|
|
235
|
+
node.kind(),
|
|
236
|
+
"static_final_declaration" | "initialized_identifier"
|
|
237
|
+
) && is_const_string_declaration(node, source)
|
|
238
|
+
&& let Some(name) = field_text(node, "name", source)
|
|
239
|
+
&& let Some(value) = node.child_by_field_name("value").and_then(|value| {
|
|
240
|
+
value
|
|
241
|
+
.utf8_text(source.as_bytes())
|
|
242
|
+
.ok()
|
|
243
|
+
.and_then(unquote_dart_string)
|
|
244
|
+
})
|
|
245
|
+
{
|
|
246
|
+
constants.insert(name.clone(), value.clone());
|
|
247
|
+
if let Some(class_name) = containing_class_name(node, source) {
|
|
248
|
+
constants.insert(format!("{class_name}.{name}"), value);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
let mut cursor = node.walk();
|
|
253
|
+
for child in node.named_children(&mut cursor) {
|
|
254
|
+
collect_string_constants_in(child, source, constants);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
fn is_const_string_declaration(node: Node<'_>, source: &str) -> bool {
|
|
259
|
+
let mut current = node.parent();
|
|
260
|
+
for _ in 0..5 {
|
|
261
|
+
let Some(parent) = current else {
|
|
262
|
+
return false;
|
|
263
|
+
};
|
|
264
|
+
if matches!(
|
|
265
|
+
parent.kind(),
|
|
266
|
+
"declaration" | "top_level_variable_declaration" | "local_variable_declaration"
|
|
267
|
+
) {
|
|
268
|
+
return parent
|
|
269
|
+
.utf8_text(source.as_bytes())
|
|
270
|
+
.is_ok_and(|text| text.contains("const") && text.contains('='));
|
|
271
|
+
}
|
|
272
|
+
current = parent.parent();
|
|
273
|
+
}
|
|
274
|
+
false
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
fn containing_class_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
278
|
+
let mut current = node.parent();
|
|
279
|
+
while let Some(parent) = current {
|
|
280
|
+
if parent.kind() == "class_declaration" {
|
|
281
|
+
return field_text(parent, "name", source);
|
|
282
|
+
}
|
|
283
|
+
current = parent.parent();
|
|
284
|
+
}
|
|
285
|
+
None
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
fn collect_routes_in_text(
|
|
289
|
+
text: &str,
|
|
290
|
+
offset: usize,
|
|
291
|
+
parent_path: Option<&str>,
|
|
292
|
+
base: Location,
|
|
293
|
+
default_class: Option<&str>,
|
|
294
|
+
constants: &BTreeMap<String, String>,
|
|
295
|
+
routes: &mut Vec<DartRouteDeclaration>,
|
|
296
|
+
) {
|
|
297
|
+
let mut cursor = 0;
|
|
298
|
+
while let Some((found, constructor)) = find_next_route_call(text, cursor) {
|
|
299
|
+
let Some(call) = parse_route_call(
|
|
300
|
+
text,
|
|
301
|
+
found,
|
|
302
|
+
constructor,
|
|
303
|
+
offset,
|
|
304
|
+
parent_path,
|
|
305
|
+
base,
|
|
306
|
+
constants,
|
|
307
|
+
) else {
|
|
308
|
+
cursor = found + constructor.len();
|
|
309
|
+
continue;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
let route_class = call
|
|
313
|
+
.route_class
|
|
314
|
+
.clone()
|
|
315
|
+
.or_else(|| default_class.map(str::to_owned))
|
|
316
|
+
.unwrap_or_else(|| "<unknown-route>".to_owned());
|
|
317
|
+
routes.push(DartRouteDeclaration {
|
|
318
|
+
route_class,
|
|
319
|
+
path: call.full_path.clone(),
|
|
320
|
+
path_expression: call.path_expression.clone(),
|
|
321
|
+
name: call.name.clone(),
|
|
322
|
+
location: call.location,
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
if let Some(routes_arg) = named_argument_region(call.arguments, "routes") {
|
|
326
|
+
collect_routes_in_text(
|
|
327
|
+
&call.arguments[routes_arg.clone()],
|
|
328
|
+
offset + call.arguments_start + routes_arg.start,
|
|
329
|
+
call.full_path.as_deref(),
|
|
330
|
+
base,
|
|
331
|
+
None,
|
|
332
|
+
constants,
|
|
333
|
+
routes,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
cursor = call.end;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
#[derive(Debug)]
|
|
341
|
+
struct ParsedRouteCall<'a> {
|
|
342
|
+
route_class: Option<String>,
|
|
343
|
+
path_expression: String,
|
|
344
|
+
full_path: Option<String>,
|
|
345
|
+
name: Option<String>,
|
|
346
|
+
location: Location,
|
|
347
|
+
arguments: &'a str,
|
|
348
|
+
arguments_start: usize,
|
|
349
|
+
end: usize,
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
fn parse_route_call<'a>(
|
|
353
|
+
text: &'a str,
|
|
354
|
+
start: usize,
|
|
355
|
+
constructor: &str,
|
|
356
|
+
offset: usize,
|
|
357
|
+
parent_path: Option<&str>,
|
|
358
|
+
base: Location,
|
|
359
|
+
constants: &BTreeMap<String, String>,
|
|
360
|
+
) -> Option<ParsedRouteCall<'a>> {
|
|
361
|
+
let name_end = start + constructor.len();
|
|
362
|
+
let (route_class, after_type_args) = parse_type_arguments(text, name_end)?;
|
|
363
|
+
let open = text[after_type_args..].find('(')? + after_type_args;
|
|
364
|
+
let close = matching_delimiter(text, open, '(', ')')?;
|
|
365
|
+
let arguments = &text[open + 1..close];
|
|
366
|
+
let path_expression = named_argument(arguments, "path")?.trim().to_owned();
|
|
367
|
+
let path_segment = resolve_path_expression(&path_expression, constants);
|
|
368
|
+
let full_path = path_segment
|
|
369
|
+
.as_deref()
|
|
370
|
+
.map(|segment| join_route_path(parent_path, segment));
|
|
371
|
+
let name =
|
|
372
|
+
named_argument(arguments, "name").and_then(|value| unquote_dart_string(value.trim()));
|
|
373
|
+
Some(ParsedRouteCall {
|
|
374
|
+
route_class,
|
|
375
|
+
path_expression,
|
|
376
|
+
full_path,
|
|
377
|
+
name,
|
|
378
|
+
location: location_for_offset(text, base, route_location_offset(text, start) + offset),
|
|
379
|
+
arguments,
|
|
380
|
+
arguments_start: open + 1,
|
|
381
|
+
end: close + 1,
|
|
382
|
+
})
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
fn find_next_route_call(text: &str, start: usize) -> Option<(usize, &'static str)> {
|
|
386
|
+
const CONSTRUCTORS: [&str; 2] = ["TypedGoRoute", "TypedRelativeGoRoute"];
|
|
387
|
+
let mut index = start;
|
|
388
|
+
while index < text.len() {
|
|
389
|
+
for constructor in CONSTRUCTORS {
|
|
390
|
+
if starts_identifier_at(text, index, constructor) {
|
|
391
|
+
return Some((index, constructor));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
index += text[index..].chars().next()?.len_utf8();
|
|
395
|
+
}
|
|
396
|
+
None
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
fn starts_identifier_at(text: &str, index: usize, ident: &str) -> bool {
|
|
400
|
+
text[index..].starts_with(ident)
|
|
401
|
+
&& text[..index]
|
|
402
|
+
.chars()
|
|
403
|
+
.next_back()
|
|
404
|
+
.is_none_or(|ch| !is_identifier_char(ch))
|
|
405
|
+
&& text[index + ident.len()..]
|
|
406
|
+
.chars()
|
|
407
|
+
.next()
|
|
408
|
+
.is_none_or(|ch| !is_identifier_char(ch))
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
fn is_identifier_char(ch: char) -> bool {
|
|
412
|
+
ch == '_' || ch.is_ascii_alphanumeric()
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
fn parse_type_arguments(text: &str, start: usize) -> Option<(Option<String>, usize)> {
|
|
416
|
+
let open = text[start..].find('<')? + start;
|
|
417
|
+
let close = matching_delimiter(text, open, '<', '>')?;
|
|
418
|
+
let route_class = text[open + 1..close]
|
|
419
|
+
.split(',')
|
|
420
|
+
.next()
|
|
421
|
+
.map(str::trim)
|
|
422
|
+
.filter(|value| !value.is_empty())
|
|
423
|
+
.map(str::to_owned);
|
|
424
|
+
Some((route_class, close + 1))
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
fn named_argument(arguments: &str, name: &str) -> Option<String> {
|
|
428
|
+
named_argument_region(arguments, name).map(|range| arguments[range].to_owned())
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
fn named_argument_region(arguments: &str, name: &str) -> Option<std::ops::Range<usize>> {
|
|
432
|
+
let index = find_label(arguments, name, 0)?;
|
|
433
|
+
let expression_start = index + name.len() + 1;
|
|
434
|
+
let expression_end = end_of_expression(arguments, expression_start);
|
|
435
|
+
Some(expression_start..expression_end)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
fn find_label(text: &str, name: &str, start: usize) -> Option<usize> {
|
|
439
|
+
let needle = format!("{name}:");
|
|
440
|
+
let mut cursor = start;
|
|
441
|
+
while let Some(index) = text[cursor..].find(&needle).map(|index| index + cursor) {
|
|
442
|
+
if text[..index]
|
|
443
|
+
.chars()
|
|
444
|
+
.next_back()
|
|
445
|
+
.is_none_or(|ch| !is_identifier_char(ch))
|
|
446
|
+
{
|
|
447
|
+
return Some(index);
|
|
448
|
+
}
|
|
449
|
+
cursor = index + needle.len();
|
|
450
|
+
}
|
|
451
|
+
None
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
fn end_of_expression(text: &str, start: usize) -> usize {
|
|
455
|
+
let mut depth = Vec::new();
|
|
456
|
+
let mut quote = None;
|
|
457
|
+
let mut index = start;
|
|
458
|
+
while index < text.len() {
|
|
459
|
+
let ch = text[index..].chars().next().unwrap_or_default();
|
|
460
|
+
if let Some(active) = quote {
|
|
461
|
+
if ch == active {
|
|
462
|
+
quote = None;
|
|
463
|
+
}
|
|
464
|
+
} else {
|
|
465
|
+
match ch {
|
|
466
|
+
'\'' | '"' => quote = Some(ch),
|
|
467
|
+
'(' | '[' | '{' | '<' => depth.push(ch),
|
|
468
|
+
')' | ']' | '}' | '>' => {
|
|
469
|
+
depth.pop();
|
|
470
|
+
}
|
|
471
|
+
',' if depth.is_empty() => return index,
|
|
472
|
+
_ => {}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
index += ch.len_utf8();
|
|
476
|
+
}
|
|
477
|
+
text.len()
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
fn matching_delimiter(text: &str, open: usize, left: char, right: char) -> Option<usize> {
|
|
481
|
+
let mut depth = 0usize;
|
|
482
|
+
let mut quote = None;
|
|
483
|
+
let mut index = open;
|
|
484
|
+
while index < text.len() {
|
|
485
|
+
let ch = text[index..].chars().next()?;
|
|
486
|
+
if let Some(active) = quote {
|
|
487
|
+
if ch == active {
|
|
488
|
+
quote = None;
|
|
489
|
+
}
|
|
490
|
+
} else if ch == '\'' || ch == '"' {
|
|
491
|
+
quote = Some(ch);
|
|
492
|
+
} else if ch == left {
|
|
493
|
+
depth += 1;
|
|
494
|
+
} else if ch == right {
|
|
495
|
+
depth = depth.saturating_sub(1);
|
|
496
|
+
if depth == 0 {
|
|
497
|
+
return Some(index);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
index += ch.len_utf8();
|
|
501
|
+
}
|
|
502
|
+
None
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
fn resolve_path_expression(
|
|
506
|
+
expression: &str,
|
|
507
|
+
constants: &BTreeMap<String, String>,
|
|
508
|
+
) -> Option<String> {
|
|
509
|
+
unquote_dart_string(expression.trim()).or_else(|| {
|
|
510
|
+
let key = expression.trim().replace(' ', "");
|
|
511
|
+
constants.get(&key).cloned()
|
|
512
|
+
})
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
fn join_route_path(parent: Option<&str>, child: &str) -> String {
|
|
516
|
+
let joined = if child.starts_with('/') {
|
|
517
|
+
child.to_owned()
|
|
518
|
+
} else if let Some(parent) = parent {
|
|
519
|
+
format!(
|
|
520
|
+
"{}/{}",
|
|
521
|
+
parent.trim_end_matches('/'),
|
|
522
|
+
child.trim_start_matches('/')
|
|
523
|
+
)
|
|
524
|
+
} else {
|
|
525
|
+
format!("/{}", child.trim_start_matches('/'))
|
|
526
|
+
};
|
|
527
|
+
normalize_route_path(&joined)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
fn normalize_route_path(path: &str) -> String {
|
|
531
|
+
let mut normalized = path.replace("//", "/");
|
|
532
|
+
while normalized.len() > 1 && normalized.ends_with('/') {
|
|
533
|
+
normalized.pop();
|
|
534
|
+
}
|
|
535
|
+
if normalized.is_empty() {
|
|
536
|
+
"/".to_owned()
|
|
537
|
+
} else if normalized.starts_with('/') {
|
|
538
|
+
normalized
|
|
539
|
+
} else {
|
|
540
|
+
format!("/{normalized}")
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
fn route_location_offset(text: &str, start: usize) -> usize {
|
|
545
|
+
if start > 0 && text[..start].trim() == "@" {
|
|
546
|
+
start - 1
|
|
547
|
+
} else {
|
|
548
|
+
start
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
fn location_for_offset(text: &str, base: Location, offset: usize) -> Location {
|
|
553
|
+
let prefix = &text[..offset.min(text.len())];
|
|
554
|
+
let newlines = prefix.bytes().filter(|byte| *byte == b'\n').count();
|
|
555
|
+
if newlines == 0 {
|
|
556
|
+
return Location {
|
|
557
|
+
line: base.line,
|
|
558
|
+
column: base.column + prefix.len(),
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
let column = prefix
|
|
562
|
+
.rsplit_once('\n')
|
|
563
|
+
.map_or(0, |(_, suffix)| suffix.len());
|
|
564
|
+
Location {
|
|
565
|
+
line: base.line + newlines,
|
|
566
|
+
column,
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
fn unquote_dart_string(raw: &str) -> Option<String> {
|
|
571
|
+
let trimmed = raw.trim();
|
|
572
|
+
let without_raw_prefix = trimmed
|
|
573
|
+
.strip_prefix('r')
|
|
574
|
+
.or_else(|| trimmed.strip_prefix('R'))
|
|
575
|
+
.unwrap_or(trimmed);
|
|
576
|
+
without_raw_prefix
|
|
577
|
+
.strip_prefix("'''")
|
|
578
|
+
.and_then(|inner| inner.strip_suffix("'''"))
|
|
579
|
+
.or_else(|| {
|
|
580
|
+
without_raw_prefix
|
|
581
|
+
.strip_prefix("\"\"\"")
|
|
582
|
+
.and_then(|inner| inner.strip_suffix("\"\"\""))
|
|
583
|
+
})
|
|
584
|
+
.or_else(|| {
|
|
585
|
+
without_raw_prefix
|
|
586
|
+
.strip_prefix('\'')
|
|
587
|
+
.and_then(|inner| inner.strip_suffix('\''))
|
|
588
|
+
})
|
|
589
|
+
.or_else(|| {
|
|
590
|
+
without_raw_prefix
|
|
591
|
+
.strip_prefix('"')
|
|
592
|
+
.and_then(|inner| inner.strip_suffix('"'))
|
|
593
|
+
})
|
|
594
|
+
.map(str::to_owned)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
#[cfg(test)]
|
|
598
|
+
mod tests {
|
|
599
|
+
use crate::extract_dart_source;
|
|
600
|
+
|
|
601
|
+
#[test]
|
|
602
|
+
fn extracts_typed_go_routes_and_nested_full_paths() -> Result<(), Box<dyn std::error::Error>> {
|
|
603
|
+
let source = "
|
|
604
|
+
@TypedGoRoute<HomeRoute>(
|
|
605
|
+
path: '/',
|
|
606
|
+
routes: <TypedRoute<RouteData>>[
|
|
607
|
+
TypedGoRoute<FamilyRoute>(path: 'family/:fid'),
|
|
608
|
+
],
|
|
609
|
+
)
|
|
610
|
+
class HomeRoute extends GoRouteData {}
|
|
611
|
+
";
|
|
612
|
+
let file = extract_dart_source("lib/routes.dart", source)?;
|
|
613
|
+
|
|
614
|
+
assert_eq!(file.routes.len(), 2);
|
|
615
|
+
assert_eq!(file.routes[0].route_class, "HomeRoute");
|
|
616
|
+
assert_eq!(file.routes[0].path.as_deref(), Some("/"));
|
|
617
|
+
assert_eq!(file.routes[1].route_class, "FamilyRoute");
|
|
618
|
+
assert_eq!(file.routes[1].path.as_deref(), Some("/family/:fid"));
|
|
619
|
+
Ok(())
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
#[test]
|
|
623
|
+
fn ignores_comments_strings_and_extracts_raw_go_route_calls()
|
|
624
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
625
|
+
let source = r#"
|
|
626
|
+
// @TypedGoRoute<FakeRoute>(path: '/settings')
|
|
627
|
+
const text = "@TypedGoRoute<FakeRoute>(path: '/settings')";
|
|
628
|
+
final route = GoRoute(path: '/settings', builder: (_, _) => const SizedBox());
|
|
629
|
+
@TypedGoRoute<RealRoute>(path: '/settings')
|
|
630
|
+
class RealRoute extends GoRouteData {}
|
|
631
|
+
"#;
|
|
632
|
+
let file = extract_dart_source("lib/routes.dart", source)?;
|
|
633
|
+
|
|
634
|
+
assert_eq!(file.routes.len(), 2);
|
|
635
|
+
assert_eq!(file.routes[0].route_class, "RealRoute");
|
|
636
|
+
assert_eq!(file.routes[1].route_class, "GoRoute");
|
|
637
|
+
assert_eq!(file.routes[1].path.as_deref(), Some("/settings"));
|
|
638
|
+
Ok(())
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
#[test]
|
|
642
|
+
fn resolves_same_file_static_const_paths() -> Result<(), Box<dyn std::error::Error>> {
|
|
643
|
+
let source = "
|
|
644
|
+
class AppRoutePaths {
|
|
645
|
+
static const String home = '/home';
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
@TypedGoRoute<HomeRoute>(path: AppRoutePaths.home)
|
|
649
|
+
class HomeRoute extends GoRouteData {}
|
|
650
|
+
";
|
|
651
|
+
let file = extract_dart_source("lib/routes.dart", source)?;
|
|
652
|
+
|
|
653
|
+
assert_eq!(file.routes[0].path.as_deref(), Some("/home"));
|
|
654
|
+
Ok(())
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
#[test]
|
|
658
|
+
fn extracts_raw_go_routes_and_nested_full_paths() -> Result<(), Box<dyn std::error::Error>> {
|
|
659
|
+
let source = "
|
|
660
|
+
final router = GoRouter(
|
|
661
|
+
routes: [
|
|
662
|
+
ShellRoute(
|
|
663
|
+
routes: [
|
|
664
|
+
GoRoute(
|
|
665
|
+
path: '/home',
|
|
666
|
+
name: 'home',
|
|
667
|
+
routes: [
|
|
668
|
+
GoRoute(path: 'details', builder: (_, _) => const SizedBox()),
|
|
669
|
+
],
|
|
670
|
+
builder: (_, _, child) => child,
|
|
671
|
+
),
|
|
672
|
+
],
|
|
673
|
+
builder: (_, _, child) => child,
|
|
674
|
+
),
|
|
675
|
+
],
|
|
676
|
+
);
|
|
677
|
+
";
|
|
678
|
+
let file = extract_dart_source("lib/routes.dart", source)?;
|
|
679
|
+
|
|
680
|
+
let raw_routes = file
|
|
681
|
+
.routes
|
|
682
|
+
.iter()
|
|
683
|
+
.filter(|route| route.route_class == "GoRoute")
|
|
684
|
+
.collect::<Vec<_>>();
|
|
685
|
+
assert_eq!(raw_routes.len(), 2);
|
|
686
|
+
assert_eq!(raw_routes[0].path.as_deref(), Some("/home"));
|
|
687
|
+
assert_eq!(raw_routes[0].name.as_deref(), Some("home"));
|
|
688
|
+
assert_eq!(raw_routes[1].path.as_deref(), Some("/home/details"));
|
|
689
|
+
Ok(())
|
|
690
|
+
}
|
|
691
|
+
}
|