dart-decimate 0.0.45 → 0.0.46

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 CHANGED
@@ -102,7 +102,7 @@ checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
102
102
 
103
103
  [[package]]
104
104
  name = "dart-decimate"
105
- version = "0.0.45"
105
+ version = "0.0.46"
106
106
  dependencies = [
107
107
  "clap",
108
108
  "glob",
package/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "dart-decimate"
3
- version = "0.0.45"
3
+ version = "0.0.46"
4
4
  edition = "2024"
5
5
  rust-version = "1.90"
6
6
  description = "Rust-native codebase intelligence for Dart and Flutter module graphs."
package/README.md CHANGED
@@ -90,7 +90,7 @@ You do not need to install anything permanently. Use the command above.
90
90
  For repeatable automation, pin the version:
91
91
 
92
92
  ```bash
93
- npx --yes dart-decimate@0.0.45
93
+ npx --yes dart-decimate@0.0.46
94
94
  ```
95
95
 
96
96
  Add this to `package.json` if you want a short project command:
@@ -101,7 +101,7 @@ Add this to `package.json` if you want a short project command:
101
101
  "dart-decimate": "dart-decimate json ."
102
102
  },
103
103
  "devDependencies": {
104
- "dart-decimate": "0.0.45"
104
+ "dart-decimate": "0.0.46"
105
105
  }
106
106
  }
107
107
  ```
@@ -115,7 +115,7 @@ npm run dart-decimate
115
115
  If you prefer Cargo, install the matching tagged source:
116
116
 
117
117
  ```bash
118
- cargo install --git https://github.com/sgaabdu4/dart-decimate.git --tag v0.0.45 --locked
118
+ cargo install --git https://github.com/sgaabdu4/dart-decimate.git --tag v0.0.46 --locked
119
119
  ```
120
120
 
121
121
  The npm release `X.Y.Z` and Cargo tag `vX.Y.Z` are verified against each other
@@ -126,7 +126,7 @@ Confirm a Cargo build's version with:
126
126
 
127
127
  ```bash
128
128
  dart-decimate --version
129
- # dart-decimate 0.0.45
129
+ # dart-decimate 0.0.46
130
130
  ```
131
131
 
132
132
  ## npx
@@ -536,7 +536,7 @@ Example shape:
536
536
  {
537
537
  "schema_version": "dart-decimate.report.v1",
538
538
  "kind": "combined",
539
- "tool": "dart-decimate 0.0.45",
539
+ "tool": "dart-decimate 0.0.46",
540
540
  "command": "check",
541
541
  "verdict": "fail",
542
542
  "summary": {
@@ -552,7 +552,7 @@ Example shape:
552
552
 
553
553
  The `dart-decimate.report.v1` field layout is unchanged. Its existing `tool`
554
554
  string includes the producing version so saved reports identify the exact
555
- build, for example `dart-decimate 0.0.45`.
555
+ build, for example `dart-decimate 0.0.46`.
556
556
 
557
557
  When grouped security findings hide additional occurrences, `next_steps` can
558
558
  include `review-security-surface`, which reruns
@@ -640,7 +640,7 @@ you need the installed binary's exact list.
640
640
  For a complete repository check, including discovered local packages:
641
641
 
642
642
  ```bash
643
- npx --yes dart-decimate@0.0.45 --strict
643
+ npx --yes dart-decimate@0.0.46 --strict
644
644
  ```
645
645
 
646
646
  This runs every enabled check from the repository root. The default duplication
@@ -683,7 +683,7 @@ This repository forbids `unsafe_code`.
683
683
 
684
684
  ## Release Flow
685
685
 
686
- Current source version: `0.0.45`.
686
+ Current source version: `0.0.46`.
687
687
 
688
688
  After the first public release, changes should go through pull requests. Every
689
689
  PR to `main` must bump both `Cargo.toml` and `package.json` above the base
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dart-decimate",
3
- "version": "0.0.45",
3
+ "version": "0.0.46",
4
4
  "description": "Rust-native codebase intelligence for Dart and Flutter module graphs.",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -13,6 +13,7 @@ use super::{
13
13
  registry_api::{VisibleNonRouteRegistryApi, visible_non_route_registry_api},
14
14
  route_extension_navigation_call, route_location_argument, route_location_expression_member,
15
15
  typed_route_navigation_call, visit_named,
16
+ wrappers::helper_has_typed_route_wrapper_call,
16
17
  };
17
18
  use crate::{
18
19
  DartFile, DependencyCycle, DependencyKind, Location, ResolvedDependency, scan::ScannedProject,
@@ -173,6 +174,14 @@ fn is_typed_go_router_navigation_helper(
173
174
  return false;
174
175
  }
175
176
  helper_has_typed_route_navigation_call(root, source, &route_classes)
177
+ || helper_has_typed_route_wrapper_call(
178
+ helper_file,
179
+ root,
180
+ source,
181
+ &route_classes,
182
+ files_by_path,
183
+ dependencies,
184
+ )
176
185
  }
177
186
 
178
187
  fn helper_references_imported_api(
@@ -224,7 +224,7 @@ fn is_route_registry_infrastructure_declaration(name: &str) -> bool {
224
224
  )
225
225
  }
226
226
 
227
- fn dependency_imports_name(dependency: &ResolvedDependency, name: &str) -> bool {
227
+ pub(super) fn dependency_imports_name(dependency: &ResolvedDependency, name: &str) -> bool {
228
228
  let mut show_seen = false;
229
229
  let mut shown = false;
230
230
  for combinator in &dependency.visibility.combinators {
@@ -0,0 +1,238 @@
1
+ use std::collections::{BTreeMap, BTreeSet};
2
+ use std::fs;
3
+ use std::path::PathBuf;
4
+
5
+ use tree_sitter::Node;
6
+
7
+ use super::{
8
+ argument_list,
9
+ navigation::{go_router_symbol_import, term_identifier_shadowed_at},
10
+ registry_api::dependency_imports_name,
11
+ route_constructor_receiver, visit_named,
12
+ };
13
+ use crate::{DartFile, DependencyKind, ResolvedDependency};
14
+
15
+ pub(super) fn helper_has_typed_route_wrapper_call(
16
+ helper_file: &DartFile,
17
+ root: Node<'_>,
18
+ source: &str,
19
+ route_classes: &BTreeSet<String>,
20
+ files_by_path: &BTreeMap<PathBuf, &DartFile>,
21
+ dependencies: &[ResolvedDependency],
22
+ ) -> bool {
23
+ let mut found = false;
24
+ let mut wrapper_results = BTreeMap::new();
25
+ visit_named(root, &mut |call| {
26
+ if found || call.kind() != "call_expression" {
27
+ return;
28
+ }
29
+ let Some(function) = call.child_by_field_name("function") else {
30
+ return;
31
+ };
32
+ let Some((function, prefix)) = wrapper_call_name_nodes(function) else {
33
+ return;
34
+ };
35
+ let Ok(function_name) = function.utf8_text(source.as_bytes()) else {
36
+ return;
37
+ };
38
+ let prefix_name = prefix.and_then(|node| node.utf8_text(source.as_bytes()).ok());
39
+ if prefix.is_some() && prefix_name.is_none() {
40
+ return;
41
+ }
42
+ if (prefix.is_none()
43
+ && helper_file
44
+ .declarations
45
+ .iter()
46
+ .any(|declaration| declaration.name == function_name))
47
+ || prefix.is_some_and(|node| {
48
+ term_identifier_shadowed_at(root, node, prefix_name.unwrap_or_default(), source)
49
+ })
50
+ || (prefix.is_none()
51
+ && term_identifier_shadowed_at(root, function, function_name, source))
52
+ {
53
+ return;
54
+ }
55
+ let Some(arguments) = argument_list(call) else {
56
+ return;
57
+ };
58
+ let mut cursor = arguments.walk();
59
+ for argument in arguments.named_children(&mut cursor) {
60
+ let Some((parameter_name, value)) = named_argument(argument, source) else {
61
+ continue;
62
+ };
63
+ if !route_constructor_receiver(root, value, route_classes, source) {
64
+ continue;
65
+ }
66
+ found = dependencies.iter().any(|import| {
67
+ import.kind == DependencyKind::Import
68
+ && import.from_path == helper_file.path
69
+ && import.visibility.prefix.as_deref() == prefix_name
70
+ && dependency_imports_name(import, function_name)
71
+ && files_by_path
72
+ .get(&import.to_path)
73
+ .is_some_and(|wrapper_file| {
74
+ *wrapper_results
75
+ .entry((
76
+ import.to_path.clone(),
77
+ function_name.to_owned(),
78
+ parameter_name.to_owned(),
79
+ ))
80
+ .or_insert_with(|| {
81
+ wrapper_navigates_parameter(
82
+ wrapper_file,
83
+ function_name,
84
+ parameter_name,
85
+ )
86
+ })
87
+ })
88
+ });
89
+ if found {
90
+ break;
91
+ }
92
+ }
93
+ });
94
+ found
95
+ }
96
+
97
+ fn wrapper_call_name_nodes(function: Node<'_>) -> Option<(Node<'_>, Option<Node<'_>>)> {
98
+ if function.kind() == "identifier" {
99
+ return Some((function, None));
100
+ }
101
+ if function.kind() == "member_expression" {
102
+ let receiver = function.child_by_field_name("object")?;
103
+ let receiver = callback_body_identifier(receiver)
104
+ .or_else(|| (receiver.kind() == "identifier").then_some(receiver))?;
105
+ let method = function.child_by_field_name("property")?;
106
+ return (method.kind() == "identifier").then_some((method, Some(receiver)));
107
+ }
108
+ // Dart Tree-Sitter represents `() => navigate(...)` as a call whose
109
+ // function is the closure and whose body is the callee identifier.
110
+ callback_body_identifier(function).map(|name| (name, None))
111
+ }
112
+
113
+ fn callback_body_identifier(function: Node<'_>) -> Option<Node<'_>> {
114
+ if function.kind() != "function_expression" {
115
+ return None;
116
+ }
117
+ let parameters = function.child_by_field_name("parameters")?;
118
+ if parameters.named_child_count() != 0 {
119
+ return None;
120
+ }
121
+ let body = function.child_by_field_name("body")?;
122
+ if body.kind() != "function_expression_body" || body.named_child_count() != 1 {
123
+ return None;
124
+ }
125
+ let name = body.named_child(0)?;
126
+ (name.kind() == "identifier").then_some(name)
127
+ }
128
+
129
+ fn named_argument<'tree, 'source>(
130
+ argument: Node<'tree>,
131
+ source: &'source str,
132
+ ) -> Option<(&'source str, Node<'tree>)> {
133
+ if argument.kind() != "named_argument" {
134
+ return None;
135
+ }
136
+ let mut cursor = argument.walk();
137
+ let mut children = argument.named_children(&mut cursor);
138
+ let label = children.next()?;
139
+ if label.kind() != "label" {
140
+ return None;
141
+ }
142
+ let name = label.named_child(0)?.utf8_text(source.as_bytes()).ok()?;
143
+ Some((name, children.next()?))
144
+ }
145
+
146
+ fn wrapper_navigates_parameter(file: &DartFile, function_name: &str, parameter_name: &str) -> bool {
147
+ let Ok(source) = fs::read_to_string(&file.path) else {
148
+ return false;
149
+ };
150
+ let Ok(parsed) = crate::dart_parser::parse_dart_source_lossy(&file.path, &source) else {
151
+ return false;
152
+ };
153
+ let root = parsed.tree().root_node();
154
+ let source = parsed.source();
155
+ if !go_router_symbol_import(root, None, "GoRouteData", source) {
156
+ return false;
157
+ }
158
+ let mut cursor = root.walk();
159
+ root.named_children(&mut cursor).any(|function| {
160
+ if function.kind() != "function_declaration" {
161
+ return false;
162
+ }
163
+ let Some(signature) = function.child_by_field_name("signature") else {
164
+ return false;
165
+ };
166
+ if field_text(signature, "name", source) != Some(function_name) {
167
+ return false;
168
+ }
169
+ let Some(parameters) = signature.child_by_field_name("parameters") else {
170
+ return false;
171
+ };
172
+ if !typed_parameter(parameters, parameter_name, "GoRouteData", source) {
173
+ return false;
174
+ }
175
+ let Some(body) = function.child_by_field_name("body") else {
176
+ return false;
177
+ };
178
+ let mut navigates = false;
179
+ visit_named(body, &mut |call| {
180
+ if navigates || call.kind() != "call_expression" {
181
+ return;
182
+ }
183
+ let Some(member) = call.child_by_field_name("function") else {
184
+ return;
185
+ };
186
+ if member.kind() != "member_expression"
187
+ || field_text(member, "object", source) != Some(parameter_name)
188
+ || !matches!(
189
+ field_text(member, "property", source),
190
+ Some("go" | "push" | "pushReplacement" | "replace")
191
+ )
192
+ {
193
+ return;
194
+ }
195
+ let Some(destination) = member.child_by_field_name("object") else {
196
+ return;
197
+ };
198
+ if term_identifier_shadowed_at(body, destination, parameter_name, source) {
199
+ return;
200
+ }
201
+ let Some(arguments) = argument_list(call) else {
202
+ return;
203
+ };
204
+ let Some(context) = arguments.named_child(0) else {
205
+ return;
206
+ };
207
+ let Ok(context_name) = context.utf8_text(source.as_bytes()) else {
208
+ return;
209
+ };
210
+ navigates = context.kind() == "identifier"
211
+ && !term_identifier_shadowed_at(body, context, context_name, source)
212
+ && typed_parameter(parameters, context_name, "BuildContext", source);
213
+ });
214
+ navigates
215
+ })
216
+ }
217
+
218
+ fn typed_parameter(parameters: Node<'_>, name: &str, type_name: &str, source: &str) -> bool {
219
+ let mut found = false;
220
+ visit_named(parameters, &mut |parameter| {
221
+ if found || parameter.kind() != "formal_parameter" {
222
+ return;
223
+ }
224
+ let mut cursor = parameter.walk();
225
+ found = field_text(parameter, "name", source) == Some(name)
226
+ && parameter
227
+ .named_children(&mut cursor)
228
+ .find(|child| child.kind() == "type")
229
+ .is_some_and(|kind| kind.utf8_text(source.as_bytes()).ok() == Some(type_name));
230
+ });
231
+ found
232
+ }
233
+
234
+ fn field_text<'source>(node: Node<'_>, field: &str, source: &'source str) -> Option<&'source str> {
235
+ node.child_by_field_name(field)?
236
+ .utf8_text(source.as_bytes())
237
+ .ok()
238
+ }
@@ -11,6 +11,7 @@ mod registry_api;
11
11
  mod state_context;
12
12
  #[cfg(test)]
13
13
  mod tests;
14
+ mod wrappers;
14
15
 
15
16
  use aliases::{route_alias_receiver_node, route_alias_receiver_text, route_aliases_at};
16
17
  pub(super) use classification::{
@@ -103,7 +103,6 @@ const PATTERN_ANCESTORS: &[&str] = &[
103
103
  "null_assert_pattern",
104
104
  "null_check_pattern",
105
105
  "object_pattern",
106
- "pattern_variable_declaration",
107
106
  "record_pattern",
108
107
  "rest_pattern",
109
108
  "variable_pattern",
@@ -135,6 +134,7 @@ fn is_widget_member_access(body: Node<'_>, node: Node<'_>, name: &str, source: &
135
134
  let Some(object) = node.child_by_field_name("object") else {
136
135
  return false;
137
136
  };
137
+ let object = state_member_receiver(object).unwrap_or(object);
138
138
  let Some(object_name @ ("widget" | "oldWidget")) = object.utf8_text(source.as_bytes()).ok()
139
139
  else {
140
140
  return false;
@@ -142,6 +142,20 @@ fn is_widget_member_access(body: Node<'_>, node: Node<'_>, name: &str, source: &
142
142
  state_root_receiver_available(body, object, object_name, source)
143
143
  }
144
144
 
145
+ fn state_member_receiver(object: Node<'_>) -> Option<Node<'_>> {
146
+ if object.kind() != "function_expression" {
147
+ return None;
148
+ }
149
+ // The Dart grammar attaches `().onSubmit` to the closure instead of its
150
+ // expression body for `() => widget.onSubmit?.call()`.
151
+ let body = object.child_by_field_name("body")?;
152
+ if body.kind() != "function_expression_body" || body.named_child_count() != 1 {
153
+ return None;
154
+ }
155
+ let receiver = body.named_child(0)?;
156
+ matches!(receiver.kind(), "identifier" | "identifier_dollar_escaped").then_some(receiver)
157
+ }
158
+
145
159
  fn state_root_receiver_available(
146
160
  boundary: Node<'_>,
147
161
  node: Node<'_>,
@@ -308,6 +308,36 @@ class _UsedViaStateState extends State<UsedViaState> {
308
308
  Ok(())
309
309
  }
310
310
 
311
+ #[test]
312
+ fn record_rhs_reads_and_state_callback_reads_count_widget_fields()
313
+ -> Result<(), Box<dyn std::error::Error>> {
314
+ let source = r"
315
+ class RecordCard extends StatelessWidget {
316
+ RecordCard({required this.data, required this.unused});
317
+ final String data;
318
+ final String unused;
319
+ Widget build(BuildContext context) {
320
+ final (:value) = (value: data);
321
+ return Text(value);
322
+ }
323
+ }
324
+ class CallbackCard extends StatefulWidget {
325
+ CallbackCard({this.onSubmit, this.unused});
326
+ final void Function()? onSubmit;
327
+ final String? unused;
328
+ State<CallbackCard> createState() => _CallbackCardState();
329
+ }
330
+ class _CallbackCardState extends State<CallbackCard> {
331
+ Widget build(BuildContext context) => Tile(
332
+ onPressed: () => widget.onSubmit?.call(),
333
+ );
334
+ }
335
+ ";
336
+ let targets = unused_param_targets(parse_findings(source)?.unused_params);
337
+ assert_eq!(targets, vec!["RecordCard.unused", "CallbackCard.unused"]);
338
+ Ok(())
339
+ }
340
+
311
341
  #[test]
312
342
  fn state_field_reads_ignore_widget_root_shadows() -> Result<(), Box<dyn std::error::Error>> {
313
343
  let targets =