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.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,12 @@
1
+ use clap::{Arg, ArgAction, Command};
2
+
3
+ pub(super) fn summary_command(command: Command) -> Command {
4
+ command.arg(summary_arg())
5
+ }
6
+
7
+ fn summary_arg() -> Arg {
8
+ Arg::new("summary")
9
+ .long("summary")
10
+ .help("Show summary counts")
11
+ .action(ArgAction::SetTrue)
12
+ }
@@ -0,0 +1,516 @@
1
+ use std::fs;
2
+
3
+ use serde_json::Value;
4
+ use tempfile::TempDir;
5
+
6
+ use super::*;
7
+
8
+ #[test]
9
+ fn dead_code_command_emits_json_contract() -> Result<(), Box<dyn std::error::Error>> {
10
+ let fixture = tempfile::tempdir()?;
11
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
12
+ write(
13
+ &fixture,
14
+ "lib/main.dart",
15
+ "import 'live.dart';\nvoid main() {}\n",
16
+ )?;
17
+ write(&fixture, "lib/live.dart", "class Live {}\n")?;
18
+ write(&fixture, "lib/dead.dart", "class Dead {}\n")?;
19
+ let mut output = Vec::new();
20
+
21
+ let code = run_from(
22
+ [
23
+ "dart-decimate",
24
+ "dead-code",
25
+ fixture.path().to_str().unwrap_or("."),
26
+ "--format",
27
+ "json",
28
+ "--entry",
29
+ "lib/main.dart",
30
+ ],
31
+ &mut output,
32
+ )?;
33
+
34
+ let json = serde_json::from_slice::<Value>(&output)?;
35
+ assert_eq!(code, 1);
36
+ assert_eq!(json["schema_version"], "dart-decimate.report.v1");
37
+ assert_eq!(json["kind"], "dead-code");
38
+ assert_eq!(json["command"], "dead-code");
39
+ assert_eq!(json["verdict"], "fail");
40
+ assert_eq!(json["summary"]["dead_files"], 1);
41
+ assert_eq!(json["findings"][0]["rule_id"], "dart-decimate/dead-file");
42
+ assert_eq!(json["findings"][0]["path"], "lib/dead.dart");
43
+ assert_eq!(json["findings"][0]["safe_to_delete"], true);
44
+ assert_eq!(json["findings"][0]["actions"][0]["type"], "delete-file");
45
+ assert_eq!(
46
+ json["findings"][0]["actions"][0]["target_path"],
47
+ "lib/dead.dart"
48
+ );
49
+ assert_eq!(
50
+ json["findings"][0]["actions"][0]["command"],
51
+ "dart-decimate inspect --format json --file lib/dead.dart"
52
+ );
53
+ assert_eq!(json["findings"][0]["actions"][0]["auto_fixable"], true);
54
+
55
+ Ok(())
56
+ }
57
+
58
+ #[test]
59
+ fn cycles_command_reports_circular_dependencies() -> Result<(), Box<dyn std::error::Error>> {
60
+ let fixture = tempfile::tempdir()?;
61
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
62
+ write(&fixture, "lib/a.dart", "import 'b.dart';\nclass A {}\n")?;
63
+ write(&fixture, "lib/b.dart", "import 'a.dart';\nclass B {}\n")?;
64
+ let mut output = Vec::new();
65
+
66
+ let code = run_from(
67
+ [
68
+ "dart-decimate",
69
+ "cycles",
70
+ fixture.path().to_str().unwrap_or("."),
71
+ "--format",
72
+ "json",
73
+ ],
74
+ &mut output,
75
+ )?;
76
+
77
+ let json = serde_json::from_slice::<Value>(&output)?;
78
+ assert_eq!(code, 1);
79
+ assert_eq!(json["command"], "cycles");
80
+ assert_eq!(json["summary"]["cycles"], 1);
81
+ assert_eq!(
82
+ json["findings"][0]["rule_id"],
83
+ "dart-decimate/circular-dependency"
84
+ );
85
+ assert_eq!(json["findings"][0]["files"][0], "lib/a.dart");
86
+ assert_eq!(json["findings"][0]["files"][1], "lib/b.dart");
87
+
88
+ Ok(())
89
+ }
90
+
91
+ #[test]
92
+ fn cycles_command_reports_re_export_cycles_separately() -> Result<(), Box<dyn std::error::Error>> {
93
+ let fixture = tempfile::tempdir()?;
94
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
95
+ write(&fixture, "lib/a.dart", "export 'b.dart';\n")?;
96
+ write(&fixture, "lib/b.dart", "export 'a.dart';\n")?;
97
+ let mut output = Vec::new();
98
+
99
+ let code = run_from(
100
+ [
101
+ "dart-decimate",
102
+ "cycles",
103
+ fixture.path().to_str().unwrap_or("."),
104
+ "--format",
105
+ "json",
106
+ ],
107
+ &mut output,
108
+ )?;
109
+
110
+ let json = serde_json::from_slice::<Value>(&output)?;
111
+ let Some(findings) = json["findings"].as_array() else {
112
+ panic!("findings array");
113
+ };
114
+ let rule_ids = findings
115
+ .iter()
116
+ .map(|finding| finding["rule_id"].as_str().unwrap_or_default())
117
+ .collect::<Vec<_>>();
118
+ assert_eq!(code, 1);
119
+ assert_eq!(json["summary"]["re_export_cycles"], 1);
120
+ assert!(rule_ids.contains(&"dart-decimate/circular-dependency"));
121
+ assert!(rule_ids.contains(&"dart-decimate/re-export-cycle"));
122
+
123
+ Ok(())
124
+ }
125
+
126
+ #[test]
127
+ fn dead_code_command_reports_unused_exports_with_safe_fix_action()
128
+ -> Result<(), Box<dyn std::error::Error>> {
129
+ let fixture = tempfile::tempdir()?;
130
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
131
+ write(
132
+ &fixture,
133
+ "lib/main.dart",
134
+ "import 'src/live.dart';\nvoid main() { Used(); }\n",
135
+ )?;
136
+ write(
137
+ &fixture,
138
+ "lib/src/live.dart",
139
+ "class Used {}\nclass Unused {}\n",
140
+ )?;
141
+ let mut output = Vec::new();
142
+
143
+ let code = run_from(
144
+ [
145
+ "dart-decimate",
146
+ "dead-code",
147
+ fixture.path().to_str().unwrap_or("."),
148
+ "--format",
149
+ "json",
150
+ "--entry",
151
+ "lib/main.dart",
152
+ ],
153
+ &mut output,
154
+ )?;
155
+
156
+ let json = serde_json::from_slice::<Value>(&output)?;
157
+ let Some(finding) = json["findings"].as_array().and_then(|findings| {
158
+ findings
159
+ .iter()
160
+ .find(|finding| finding["rule_id"] == "dart-decimate/unused-export")
161
+ }) else {
162
+ panic!("unused export finding");
163
+ };
164
+ assert_eq!(code, 1);
165
+ assert_eq!(json["summary"]["unused_exports"], 1);
166
+ assert_eq!(finding["path"], "lib/src/live.dart");
167
+ assert_eq!(finding["line"], 2);
168
+ assert_eq!(finding["safe_to_delete"], true);
169
+ assert_eq!(finding["actions"][0]["action"], "remove-declaration");
170
+ assert_eq!(finding["actions"][0]["type"], "remove-declaration");
171
+ assert_eq!(finding["actions"][0]["target_path"], "lib/src/live.dart");
172
+ assert_eq!(finding["actions"][0]["target_symbol"], "Unused");
173
+ assert_eq!(finding["actions"][0]["target_end_line"], 2);
174
+ assert_eq!(finding["actions"][0]["auto_fixable"], true);
175
+ assert_eq!(finding["actions"][1]["action"], "trace-symbol");
176
+ assert_eq!(finding["actions"][1]["type"], "trace-symbol");
177
+ assert_eq!(finding["actions"][1]["target_path"], "lib/src/live.dart");
178
+ assert_eq!(finding["actions"][1]["target_symbol"], "Unused");
179
+ assert_eq!(
180
+ finding["actions"][1]["command"],
181
+ "dart-decimate inspect --format json --symbol lib/src/live.dart:Unused"
182
+ );
183
+ assert_eq!(
184
+ finding["actions"][1]["suppression_comment"],
185
+ "// dart-decimate-ignore-next-line unused-export"
186
+ );
187
+ assert_eq!(finding["actions"][1]["auto_fixable"], false);
188
+ assert_eq!(json["next_steps"][0]["id"], "trace-unused-export");
189
+ assert_eq!(
190
+ json["next_steps"][0]["command"],
191
+ "dart-decimate trace-symbol --format json --symbol lib/src/live.dart:Unused"
192
+ );
193
+
194
+ Ok(())
195
+ }
196
+
197
+ #[test]
198
+ fn check_command_reports_duplicate_exports() -> Result<(), Box<dyn std::error::Error>> {
199
+ let fixture = tempfile::tempdir()?;
200
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
201
+ write(
202
+ &fixture,
203
+ "lib/package.dart",
204
+ "export 'src/a.dart';\nexport 'src/b.dart';\n",
205
+ )?;
206
+ write(&fixture, "lib/src/a.dart", "class Api {}\n")?;
207
+ write(&fixture, "lib/src/b.dart", "class Api {}\n")?;
208
+ let mut output = Vec::new();
209
+
210
+ let code = run_from(
211
+ [
212
+ "dart-decimate",
213
+ "check",
214
+ fixture.path().to_str().unwrap_or("."),
215
+ "--format",
216
+ "json",
217
+ ],
218
+ &mut output,
219
+ )?;
220
+
221
+ let json = serde_json::from_slice::<Value>(&output)?;
222
+ assert_eq!(code, 1);
223
+ assert_eq!(json["summary"]["duplicate_exports"], 1);
224
+ assert_eq!(
225
+ json["findings"][0]["rule_id"],
226
+ "dart-decimate/duplicate-export"
227
+ );
228
+ assert_eq!(json["findings"][0]["kind"], "duplicate-export");
229
+ assert_eq!(json["findings"][0]["path"], "lib/package.dart");
230
+ assert_eq!(json["findings"][0]["safe_to_delete"], false);
231
+ assert_eq!(json["findings"][0]["files"][0], "lib/src/a.dart");
232
+ assert_eq!(json["findings"][0]["files"][1], "lib/src/b.dart");
233
+ assert_eq!(
234
+ json["findings"][0]["actions"][0]["action"],
235
+ "inspect-export-surface"
236
+ );
237
+ assert_eq!(json["findings"][0]["actions"][0]["auto_fixable"], false);
238
+
239
+ Ok(())
240
+ }
241
+
242
+ #[test]
243
+ fn check_command_reports_boundaries_and_unresolved_imports()
244
+ -> Result<(), Box<dyn std::error::Error>> {
245
+ let fixture = tempfile::tempdir()?;
246
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
247
+ write(
248
+ &fixture,
249
+ "lib/main.dart",
250
+ "import 'domain/service.dart';\nvoid main() {}\n",
251
+ )?;
252
+ write(
253
+ &fixture,
254
+ "lib/domain/service.dart",
255
+ "import '../ui/widget.dart';\nimport 'missing.dart';\nclass Service {}\n",
256
+ )?;
257
+ write(&fixture, "lib/ui/widget.dart", "class Widget {}\n")?;
258
+ let mut output = Vec::new();
259
+
260
+ let code = run_from(
261
+ [
262
+ "dart-decimate",
263
+ "check",
264
+ fixture.path().to_str().unwrap_or("."),
265
+ "--format",
266
+ "json",
267
+ "--boundary",
268
+ "lib/domain:lib/ui",
269
+ ],
270
+ &mut output,
271
+ )?;
272
+
273
+ let json = serde_json::from_slice::<Value>(&output)?;
274
+ let Some(findings) = json["findings"].as_array() else {
275
+ panic!("findings array");
276
+ };
277
+ let rule_ids = findings
278
+ .iter()
279
+ .map(|finding| finding["rule_id"].as_str().unwrap_or_default())
280
+ .collect::<Vec<_>>();
281
+ assert_eq!(code, 1);
282
+ assert!(rule_ids.contains(&"dart-decimate/boundary-violation"));
283
+ assert!(rule_ids.contains(&"dart-decimate/unresolved-dependency"));
284
+ assert_eq!(json["summary"]["boundary_violations"], 1);
285
+ assert_eq!(json["summary"]["unresolved_dependencies"], 1);
286
+
287
+ Ok(())
288
+ }
289
+
290
+ #[test]
291
+ fn dead_code_command_includes_pub_dependency_hygiene() -> Result<(), Box<dyn std::error::Error>> {
292
+ let fixture = tempfile::tempdir()?;
293
+ write(
294
+ &fixture,
295
+ "pubspec.yaml",
296
+ "name: app\n\
297
+ dependencies:\n http: ^1.0.0\n path: ^1.0.0\n",
298
+ )?;
299
+ write(
300
+ &fixture,
301
+ "lib/main.dart",
302
+ "import 'package:http/http.dart';\n\
303
+ import 'package:collection/collection.dart';\n\
304
+ void main() {}\n",
305
+ )?;
306
+ let mut output = Vec::new();
307
+
308
+ let code = run_from(
309
+ [
310
+ "dart-decimate",
311
+ "dead-code",
312
+ fixture.path().to_str().unwrap_or("."),
313
+ "--format",
314
+ "json",
315
+ "--entry",
316
+ "lib/main.dart",
317
+ ],
318
+ &mut output,
319
+ )?;
320
+
321
+ let json = serde_json::from_slice::<Value>(&output)?;
322
+ let Some(findings) = json["findings"].as_array() else {
323
+ panic!("findings array");
324
+ };
325
+ let rule_ids = findings
326
+ .iter()
327
+ .map(|finding| finding["rule_id"].as_str().unwrap_or_default())
328
+ .collect::<Vec<_>>();
329
+ assert_eq!(code, 1);
330
+ assert!(rule_ids.contains(&"dart-decimate/unused-dependency"));
331
+ assert!(rule_ids.contains(&"dart-decimate/unlisted-dependency"));
332
+ assert_eq!(json["summary"]["unused_dependencies"], 1);
333
+ assert_eq!(json["summary"]["unlisted_dependencies"], 1);
334
+ let Some(next_steps) = json["next_steps"].as_array() else {
335
+ panic!("next_steps array");
336
+ };
337
+ assert!(next_steps.iter().any(|step| {
338
+ step["id"] == "trace-unused-dependency"
339
+ && step["command"] == "dart-decimate trace-dependency --format json --dependency path"
340
+ }));
341
+
342
+ Ok(())
343
+ }
344
+
345
+ #[test]
346
+ fn unlisted_dependency_json_edge_kind_preserves_export() -> Result<(), Box<dyn std::error::Error>> {
347
+ let fixture = tempfile::tempdir()?;
348
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
349
+ write(
350
+ &fixture,
351
+ "lib/main.dart",
352
+ "export 'package:collection/collection.dart';\nvoid main() {}\n",
353
+ )?;
354
+ let mut output = Vec::new();
355
+
356
+ let code = run_from(
357
+ [
358
+ "dart-decimate",
359
+ "dead-code",
360
+ fixture.path().to_str().unwrap_or("."),
361
+ "--format",
362
+ "json",
363
+ "--entry",
364
+ "lib/main.dart",
365
+ ],
366
+ &mut output,
367
+ )?;
368
+
369
+ let json = serde_json::from_slice::<Value>(&output)?;
370
+ let Some(finding) = json["findings"].as_array().and_then(|findings| {
371
+ findings
372
+ .iter()
373
+ .find(|finding| finding["rule_id"] == "dart-decimate/unlisted-dependency")
374
+ }) else {
375
+ panic!("unlisted dependency finding");
376
+ };
377
+ assert_eq!(code, 1);
378
+ assert_eq!(finding["edge"]["kind"], "export");
379
+
380
+ Ok(())
381
+ }
382
+
383
+ #[test]
384
+ fn check_command_uses_path_dependencies_without_reporting_their_findings()
385
+ -> Result<(), Box<dyn std::error::Error>> {
386
+ let fixture = tempfile::tempdir()?;
387
+ write(
388
+ &fixture,
389
+ "app/pubspec.yaml",
390
+ "name: app\n\
391
+ dependencies:\n shared:\n path: ../shared\n",
392
+ )?;
393
+ write(
394
+ &fixture,
395
+ "app/lib/main.dart",
396
+ "import 'package:shared/shared.dart';\nvoid main() {}\n",
397
+ )?;
398
+ write(&fixture, "shared/pubspec.yaml", "name: shared\n")?;
399
+ write(
400
+ &fixture,
401
+ "shared/lib/shared.dart",
402
+ "import 'loop.dart';\nimport 'missing.dart';\nclass Shared {}\n",
403
+ )?;
404
+ write(
405
+ &fixture,
406
+ "shared/lib/loop.dart",
407
+ "import 'shared.dart';\nclass Loop {}\n",
408
+ )?;
409
+ write(&fixture, "shared/lib/dead.dart", "class SharedDead {}\n")?;
410
+ let mut output = Vec::new();
411
+
412
+ let code = run_from(
413
+ [
414
+ "dart-decimate",
415
+ "check",
416
+ fixture.path().join("app").to_str().unwrap_or("."),
417
+ "--format",
418
+ "json",
419
+ ],
420
+ &mut output,
421
+ )?;
422
+
423
+ let json = serde_json::from_slice::<Value>(&output)?;
424
+ assert_eq!(code, 0);
425
+ assert_eq!(json["verdict"], "pass");
426
+ assert_eq!(json["summary"]["unresolved_dependencies"], 0);
427
+ assert_eq!(json["summary"]["dead_files"], 0);
428
+ assert_eq!(json["summary"]["cycles"], 0);
429
+ assert_eq!(json["summary"]["findings"], 0);
430
+
431
+ Ok(())
432
+ }
433
+
434
+ #[test]
435
+ fn check_command_treats_tests_as_default_entry_points() -> Result<(), Box<dyn std::error::Error>> {
436
+ let fixture = tempfile::tempdir()?;
437
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
438
+ write(&fixture, "lib/main.dart", "void main() {}\n")?;
439
+ write(&fixture, "test/all_tests.dart", "void main() {}\n")?;
440
+ write(&fixture, "test/home_test.dart", "void main() {}\n")?;
441
+ write(
442
+ &fixture,
443
+ "integration_test/app_test.dart",
444
+ "void main() {}\n",
445
+ )?;
446
+ write(
447
+ &fixture,
448
+ "test_driver/integration_test.dart",
449
+ "void main() {}\n",
450
+ )?;
451
+ write(&fixture, "tool/grind.dart", "void main() {}\n")?;
452
+ write(&fixture, "pigeon/schema.dart", "class Schema {}\n")?;
453
+ let mut output = Vec::new();
454
+
455
+ let code = run_from(
456
+ [
457
+ "dart-decimate",
458
+ "check",
459
+ fixture.path().to_str().unwrap_or("."),
460
+ "--format",
461
+ "json",
462
+ ],
463
+ &mut output,
464
+ )?;
465
+
466
+ let json = serde_json::from_slice::<Value>(&output)?;
467
+ assert_eq!(code, 0);
468
+ assert_eq!(json["summary"]["dead_files"], 0);
469
+ assert_eq!(json["summary"]["findings"], 0);
470
+
471
+ Ok(())
472
+ }
473
+
474
+ #[test]
475
+ fn check_command_treats_public_library_files_as_default_entry_points()
476
+ -> Result<(), Box<dyn std::error::Error>> {
477
+ let fixture = tempfile::tempdir()?;
478
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
479
+ write(
480
+ &fixture,
481
+ "lib/package.dart",
482
+ "import 'src/live.dart';\nclass Package {}\n",
483
+ )?;
484
+ write(&fixture, "lib/src/live.dart", "class Live {}\n")?;
485
+ write(&fixture, "lib/src/dead.dart", "class Dead {}\n")?;
486
+ let mut output = Vec::new();
487
+
488
+ let code = run_from(
489
+ [
490
+ "dart-decimate",
491
+ "check",
492
+ fixture.path().to_str().unwrap_or("."),
493
+ "--format",
494
+ "json",
495
+ ],
496
+ &mut output,
497
+ )?;
498
+
499
+ let json = serde_json::from_slice::<Value>(&output)?;
500
+ let Some(findings) = json["findings"].as_array() else {
501
+ panic!("findings array");
502
+ };
503
+ assert_eq!(code, 1);
504
+ assert_eq!(json["summary"]["dead_files"], 1);
505
+ assert_eq!(findings[0]["path"], "lib/src/dead.dart");
506
+
507
+ Ok(())
508
+ }
509
+
510
+ fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
511
+ let path = fixture.path().join(path);
512
+ if let Some(parent) = path.parent() {
513
+ fs::create_dir_all(parent)?;
514
+ }
515
+ fs::write(path, source)
516
+ }
@@ -0,0 +1,67 @@
1
+ use std::path::PathBuf;
2
+
3
+ use clap::{Arg, Command, value_parser};
4
+
5
+ use super::common_args::{config_arg, entry_arg, format_arg};
6
+
7
+ pub(super) fn trace_file_command(command: Command) -> Command {
8
+ command.arg(
9
+ Arg::new("file")
10
+ .long("file")
11
+ .value_name("PATH")
12
+ .help("Dart file to trace")
13
+ .required(true)
14
+ .value_parser(value_parser!(PathBuf)),
15
+ )
16
+ }
17
+
18
+ pub(super) fn trace_symbol_command(command: Command) -> Command {
19
+ command
20
+ .arg(
21
+ Arg::new("file")
22
+ .long("file")
23
+ .value_name("PATH")
24
+ .help("Dart file declaring the symbol")
25
+ .value_parser(value_parser!(PathBuf)),
26
+ )
27
+ .arg(
28
+ Arg::new("symbol")
29
+ .long("symbol")
30
+ .value_name("SYMBOL")
31
+ .help("Top-level symbol to trace, or FILE:SYMBOL when --file is omitted")
32
+ .required(true),
33
+ )
34
+ }
35
+
36
+ pub(super) fn trace_command(command: Command) -> Command {
37
+ command
38
+ .arg(
39
+ Arg::new("symbol")
40
+ .value_name("FILE:SYMBOL")
41
+ .help("Top-level symbol to trace")
42
+ .required(true),
43
+ )
44
+ .arg(
45
+ Arg::new("root")
46
+ .long("root")
47
+ .value_name("ROOT")
48
+ .help("Project root")
49
+ .default_value(".")
50
+ .value_parser(value_parser!(PathBuf)),
51
+ )
52
+ .arg(format_arg())
53
+ .arg(config_arg())
54
+ .arg(entry_arg())
55
+ .arg(super::mode_args::production_arg())
56
+ .arg(super::mode_args::no_production_arg())
57
+ }
58
+
59
+ pub(super) fn trace_dependency_command(command: Command) -> Command {
60
+ command.arg(
61
+ Arg::new("dependency")
62
+ .long("dependency")
63
+ .value_name("PACKAGE")
64
+ .help("Pub dependency package to trace")
65
+ .required(true),
66
+ )
67
+ }