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,837 @@
1
+ use std::fs;
2
+ use std::path::{Path, PathBuf};
3
+
4
+ use tempfile::TempDir;
5
+
6
+ use super::*;
7
+ use crate::{
8
+ DartCombinator, DartCombinatorKind, DartExport, DartImport, DartLibrary, DartPart, DartPartOf,
9
+ DartUriCondition,
10
+ };
11
+
12
+ #[test]
13
+ fn builds_edges_for_relative_imports_and_exports() -> Result<(), Box<dyn std::error::Error>> {
14
+ let fixture = Fixture::new()?;
15
+ fixture.write("pubspec.yaml", "name: app\n")?;
16
+
17
+ let main = fixture.file(
18
+ "lib/main.dart",
19
+ vec![import("src/service.dart"), import("../tool.dart")],
20
+ vec![export("src/public.dart")],
21
+ );
22
+ let service = fixture.file("lib/src/service.dart", vec![], vec![]);
23
+ let tool = fixture.file("tool.dart", vec![], vec![]);
24
+ let public = fixture.file("lib/src/public.dart", vec![], vec![]);
25
+
26
+ let graph = build_module_graph(fixture.root(), &[main, service, tool, public])?;
27
+
28
+ assert_eq!(graph.node_count(), 4);
29
+ assert_eq!(graph.edge_count(), 3);
30
+ assert!(graph.unresolved().is_empty());
31
+ assert_eq!(
32
+ graph
33
+ .dependencies()
34
+ .into_iter()
35
+ .map(|dependency| (
36
+ dependency.specifier,
37
+ dependency.kind,
38
+ strip_root(fixture.root(), &dependency.to_path)
39
+ ))
40
+ .collect::<Vec<_>>(),
41
+ vec![
42
+ (
43
+ "src/service.dart".to_owned(),
44
+ DependencyKind::Import,
45
+ "lib/src/service.dart".to_owned()
46
+ ),
47
+ (
48
+ "../tool.dart".to_owned(),
49
+ DependencyKind::Import,
50
+ "tool.dart".to_owned()
51
+ ),
52
+ (
53
+ "src/public.dart".to_owned(),
54
+ DependencyKind::Export,
55
+ "lib/src/public.dart".to_owned()
56
+ ),
57
+ ]
58
+ );
59
+
60
+ Ok(())
61
+ }
62
+
63
+ #[test]
64
+ fn resolves_package_imports_to_pub_workspace_members() -> Result<(), Box<dyn std::error::Error>> {
65
+ let fixture = Fixture::new()?;
66
+ fixture.write("pubspec.yaml", "name: app\nworkspace:\n - packages/*\n")?;
67
+ fixture.write("packages/shared/pubspec.yaml", "name: shared\n")?;
68
+
69
+ let main = fixture.file(
70
+ "lib/main.dart",
71
+ vec![import("package:shared/shared.dart")],
72
+ vec![],
73
+ );
74
+ let shared = fixture.file("packages/shared/lib/shared.dart", vec![], vec![]);
75
+
76
+ let graph = build_module_graph(fixture.root(), &[main, shared])?;
77
+
78
+ assert_eq!(graph.package_names(), vec!["app", "shared"]);
79
+ assert_eq!(graph.edge_count(), 1);
80
+ assert_eq!(
81
+ strip_root(fixture.root(), &graph.dependencies()[0].to_path),
82
+ "packages/shared/lib/shared.dart"
83
+ );
84
+
85
+ Ok(())
86
+ }
87
+
88
+ #[test]
89
+ fn platform_environment_selects_one_configurable_import_branch()
90
+ -> Result<(), Box<dyn std::error::Error>> {
91
+ let fixture = Fixture::new()?;
92
+ fixture.write("pubspec.yaml", "name: app\n")?;
93
+ let mut conditional = import("web.dart");
94
+ conditional.condition = Some(condition("dart.library.html", "true", 1));
95
+
96
+ let main = fixture.file(
97
+ "lib/main.dart",
98
+ vec![import("io.dart"), conditional],
99
+ vec![],
100
+ );
101
+ let io = fixture.file("lib/io.dart", vec![], vec![]);
102
+ let web = fixture.file("lib/web.dart", vec![], vec![]);
103
+
104
+ let graph = build_module_graph_with_options(
105
+ fixture.root(),
106
+ &[main, io, web],
107
+ &GraphOptions {
108
+ conditional_environment: [("dart.library.html".to_owned(), "true".to_owned())].into(),
109
+ },
110
+ )?;
111
+
112
+ let dependencies = graph.dependencies();
113
+ assert_eq!(dependencies.len(), 1);
114
+ assert_eq!(dependencies[0].specifier, "web.dart");
115
+ assert_eq!(
116
+ dependencies[0]
117
+ .visibility
118
+ .condition
119
+ .as_ref()
120
+ .map(|condition| condition.variable.as_str()),
121
+ Some("dart.library.html")
122
+ );
123
+ assert_eq!(
124
+ strip_root(fixture.root(), &dependencies[0].to_path),
125
+ "lib/web.dart"
126
+ );
127
+ assert!(graph.unresolved().is_empty());
128
+
129
+ Ok(())
130
+ }
131
+
132
+ #[test]
133
+ fn platform_environment_falls_back_to_default_configurable_import()
134
+ -> Result<(), Box<dyn std::error::Error>> {
135
+ let fixture = Fixture::new()?;
136
+ fixture.write("pubspec.yaml", "name: app\n")?;
137
+ let mut conditional = import("web.dart");
138
+ conditional.condition = Some(condition("dart.library.html", "true", 1));
139
+
140
+ let main = fixture.file(
141
+ "lib/main.dart",
142
+ vec![import("io.dart"), conditional],
143
+ vec![],
144
+ );
145
+ let io = fixture.file("lib/io.dart", vec![], vec![]);
146
+ let web = fixture.file("lib/web.dart", vec![], vec![]);
147
+
148
+ let graph = build_module_graph_with_options(
149
+ fixture.root(),
150
+ &[main, io, web],
151
+ &GraphOptions {
152
+ conditional_environment: [("dart.library.html".to_owned(), "false".to_owned())].into(),
153
+ },
154
+ )?;
155
+
156
+ let dependencies = graph.dependencies();
157
+ assert_eq!(dependencies.len(), 1);
158
+ assert_eq!(dependencies[0].specifier, "io.dart");
159
+ assert!(dependencies[0].visibility.condition.is_none());
160
+ assert_eq!(
161
+ strip_root(fixture.root(), &dependencies[0].to_path),
162
+ "lib/io.dart"
163
+ );
164
+ assert!(graph.unresolved().is_empty());
165
+
166
+ Ok(())
167
+ }
168
+
169
+ #[test]
170
+ fn platform_environment_selects_package_uri_branches_before_resolution()
171
+ -> Result<(), Box<dyn std::error::Error>> {
172
+ let fixture = Fixture::new()?;
173
+ fixture.write("pubspec.yaml", "name: app\nworkspace:\n - packages/*\n")?;
174
+ fixture.write("packages/shared/pubspec.yaml", "name: shared\n")?;
175
+ let mut conditional = import("package:shared/web.dart");
176
+ conditional.condition = Some(condition("dart.library.html", "true", 1));
177
+
178
+ let main = fixture.file(
179
+ "lib/main.dart",
180
+ vec![import("stub.dart"), conditional],
181
+ vec![],
182
+ );
183
+ let stub = fixture.file("lib/stub.dart", vec![], vec![]);
184
+ let web = fixture.file("packages/shared/lib/web.dart", vec![], vec![]);
185
+
186
+ let graph = build_module_graph_with_options(
187
+ fixture.root(),
188
+ &[main, stub, web],
189
+ &GraphOptions {
190
+ conditional_environment: [("dart.library.html".to_owned(), "true".to_owned())].into(),
191
+ },
192
+ )?;
193
+
194
+ let dependencies = graph.dependencies();
195
+ assert_eq!(dependencies.len(), 1);
196
+ assert_eq!(dependencies[0].specifier, "package:shared/web.dart");
197
+ assert_eq!(
198
+ strip_root(fixture.root(), &dependencies[0].to_path),
199
+ "packages/shared/lib/web.dart"
200
+ );
201
+ assert!(graph.unresolved().is_empty());
202
+
203
+ Ok(())
204
+ }
205
+
206
+ #[test]
207
+ fn default_graph_keeps_all_configurable_import_branches() -> Result<(), Box<dyn std::error::Error>>
208
+ {
209
+ let fixture = Fixture::new()?;
210
+ fixture.write("pubspec.yaml", "name: app\n")?;
211
+ let mut conditional = import("web.dart");
212
+ conditional.condition = Some(condition("dart.library.html", "true", 1));
213
+
214
+ let main = fixture.file(
215
+ "lib/main.dart",
216
+ vec![import("io.dart"), conditional],
217
+ vec![],
218
+ );
219
+ let io = fixture.file("lib/io.dart", vec![], vec![]);
220
+ let web = fixture.file("lib/web.dart", vec![], vec![]);
221
+
222
+ let graph = build_module_graph(fixture.root(), &[main, io, web])?;
223
+
224
+ assert_eq!(
225
+ graph
226
+ .dependencies()
227
+ .into_iter()
228
+ .map(|dependency| dependency.specifier)
229
+ .collect::<Vec<_>>(),
230
+ vec!["io.dart".to_owned(), "web.dart".to_owned()]
231
+ );
232
+ assert!(graph.unresolved().is_empty());
233
+
234
+ Ok(())
235
+ }
236
+
237
+ #[test]
238
+ fn resolves_package_imports_using_package_config_root_uri_and_package_uri()
239
+ -> Result<(), Box<dyn std::error::Error>> {
240
+ let fixture = Fixture::new()?;
241
+ fixture.write(
242
+ ".dart_tool/package_config.json",
243
+ r#"{
244
+ "configVersion": 2,
245
+ "packages": [
246
+ {"name": "app", "rootUri": "../", "packageUri": "lib/"},
247
+ {"name": "shared", "rootUri": "../packages/shared", "packageUri": "lib/"},
248
+ {"name": "generated", "rootUri": "../packages/generated", "packageUri": "src/"}
249
+ ]
250
+ }
251
+ "#,
252
+ )?;
253
+
254
+ let main = fixture.file(
255
+ "lib/main.dart",
256
+ vec![
257
+ import("package:shared/src/api.dart"),
258
+ import("package:generated/api.dart"),
259
+ ],
260
+ vec![],
261
+ );
262
+ let shared = fixture.file("packages/shared/lib/src/api.dart", vec![], vec![]);
263
+ let generated = fixture.file("packages/generated/src/api.dart", vec![], vec![]);
264
+
265
+ let graph = build_module_graph(fixture.root(), &[main, shared, generated])?;
266
+
267
+ assert_eq!(graph.package_names(), vec!["app", "generated", "shared"]);
268
+ assert_eq!(
269
+ graph
270
+ .dependencies()
271
+ .into_iter()
272
+ .map(|dependency| strip_root(fixture.root(), &dependency.to_path))
273
+ .collect::<Vec<_>>(),
274
+ vec![
275
+ "packages/shared/lib/src/api.dart".to_owned(),
276
+ "packages/generated/src/api.dart".to_owned(),
277
+ ]
278
+ );
279
+ assert!(graph.unresolved().is_empty());
280
+
281
+ Ok(())
282
+ }
283
+
284
+ #[test]
285
+ fn package_config_takes_precedence_over_stale_pubspec_paths()
286
+ -> Result<(), Box<dyn std::error::Error>> {
287
+ let fixture = Fixture::new()?;
288
+ fixture.write(
289
+ "pubspec.yaml",
290
+ "name: app\ndependencies:\n shared:\n path: old_shared\n",
291
+ )?;
292
+ fixture.write(
293
+ ".dart_tool/package_config.json",
294
+ r#"{
295
+ "configVersion": 2,
296
+ "packages": [
297
+ {"name": "app", "rootUri": "../", "packageUri": "lib/"},
298
+ {"name": "shared", "rootUri": "../actual_shared", "packageUri": "lib/"}
299
+ ]
300
+ }
301
+ "#,
302
+ )?;
303
+ let main = fixture.file(
304
+ "lib/main.dart",
305
+ vec![import("package:shared/api.dart")],
306
+ vec![],
307
+ );
308
+ let actual = fixture.file("actual_shared/lib/api.dart", vec![], vec![]);
309
+ let stale = fixture.file("old_shared/lib/api.dart", vec![], vec![]);
310
+
311
+ let graph = build_module_graph(fixture.root(), &[main, actual, stale])?;
312
+
313
+ assert_eq!(graph.edge_count(), 1);
314
+ assert_eq!(
315
+ strip_root(fixture.root(), &graph.dependencies()[0].to_path),
316
+ "actual_shared/lib/api.dart"
317
+ );
318
+
319
+ Ok(())
320
+ }
321
+
322
+ #[test]
323
+ fn package_config_uri_paths_are_percent_decoded() -> Result<(), Box<dyn std::error::Error>> {
324
+ let fixture = Fixture::new()?;
325
+ fixture.write(
326
+ ".dart_tool/package_config.json",
327
+ r#"{
328
+ "configVersion": 2,
329
+ "packages": [
330
+ {"name": "shared", "rootUri": "../packages/shared%20pkg", "packageUri": "custom%20lib/"}
331
+ ]
332
+ }
333
+ "#,
334
+ )?;
335
+ let main = fixture.file(
336
+ "lib/main.dart",
337
+ vec![import("package:shared/api.dart")],
338
+ vec![],
339
+ );
340
+ let shared = fixture.file("packages/shared pkg/custom lib/api.dart", vec![], vec![]);
341
+
342
+ let graph = build_module_graph(fixture.root(), &[main, shared])?;
343
+
344
+ assert_eq!(graph.edge_count(), 1);
345
+ assert_eq!(
346
+ strip_root(fixture.root(), &graph.dependencies()[0].to_path),
347
+ "packages/shared pkg/custom lib/api.dart"
348
+ );
349
+
350
+ Ok(())
351
+ }
352
+
353
+ #[test]
354
+ fn package_config_hosted_entries_remain_external() -> Result<(), Box<dyn std::error::Error>> {
355
+ let fixture = Fixture::new()?;
356
+ fixture.write(
357
+ ".dart_tool/package_config.json",
358
+ r#"{
359
+ "configVersion": 2,
360
+ "packages": [
361
+ {"name": "hosted", "rootUri": "file:///tmp/.pub-cache/hosted/hosted-1.0.0", "packageUri": "lib/"}
362
+ ]
363
+ }
364
+ "#,
365
+ )?;
366
+ let main = fixture.file(
367
+ "lib/main.dart",
368
+ vec![import("package:hosted/hosted.dart")],
369
+ vec![],
370
+ );
371
+
372
+ let graph = build_module_graph(fixture.root(), &[main])?;
373
+
374
+ assert_eq!(graph.edge_count(), 0);
375
+ assert!(graph.unresolved().is_empty());
376
+
377
+ Ok(())
378
+ }
379
+
380
+ #[test]
381
+ fn package_config_local_missing_target_is_unresolved() -> Result<(), Box<dyn std::error::Error>> {
382
+ let fixture = Fixture::new()?;
383
+ fixture.write(
384
+ ".dart_tool/package_config.json",
385
+ r#"{
386
+ "configVersion": 2,
387
+ "packages": [
388
+ {"name": "shared", "rootUri": "../packages/shared", "packageUri": "lib/"}
389
+ ]
390
+ }
391
+ "#,
392
+ )?;
393
+ let main = fixture.file(
394
+ "lib/main.dart",
395
+ vec![import("package:shared/missing.dart")],
396
+ vec![],
397
+ );
398
+
399
+ let graph = build_module_graph(fixture.root(), &[main])?;
400
+
401
+ assert_eq!(graph.edge_count(), 0);
402
+ assert_eq!(graph.unresolved().len(), 1);
403
+ assert_eq!(
404
+ strip_root(fixture.root(), &graph.unresolved()[0].attempted_path),
405
+ "packages/shared/lib/missing.dart"
406
+ );
407
+
408
+ Ok(())
409
+ }
410
+
411
+ #[test]
412
+ fn preserves_import_and_export_visibility_metadata_on_edges()
413
+ -> Result<(), Box<dyn std::error::Error>> {
414
+ let fixture = Fixture::new()?;
415
+ fixture.write("pubspec.yaml", "name: app\n")?;
416
+ let main = DartFile {
417
+ path: fixture.path("lib/main.dart"),
418
+ library: None,
419
+ part_of: None,
420
+ imports: vec![DartImport {
421
+ uri: "src/service.dart".to_owned(),
422
+ condition: None,
423
+ prefix: Some("svc".to_owned()),
424
+ deferred: true,
425
+ combinators: vec![
426
+ combinator(DartCombinatorKind::Show, &["Service"]),
427
+ combinator(DartCombinatorKind::Hide, &["Hidden"]),
428
+ ],
429
+ location: Location { line: 1, column: 0 },
430
+ }],
431
+ exports: vec![DartExport {
432
+ uri: "src/public.dart".to_owned(),
433
+ condition: None,
434
+ combinators: vec![combinator(DartCombinatorKind::Show, &["PublicApi"])],
435
+ location: Location { line: 2, column: 0 },
436
+ }],
437
+ parts: vec![],
438
+ declarations: vec![],
439
+ members: vec![],
440
+ references: vec![],
441
+ signature_references: vec![],
442
+ routes: vec![],
443
+ };
444
+ let service = fixture.file("lib/src/service.dart", vec![], vec![]);
445
+ let public = fixture.file("lib/src/public.dart", vec![], vec![]);
446
+
447
+ let graph = build_module_graph(fixture.root(), &[main, service, public])?;
448
+ let dependencies = graph.dependencies();
449
+
450
+ assert_eq!(dependencies[0].visibility.prefix.as_deref(), Some("svc"));
451
+ assert!(dependencies[0].visibility.deferred);
452
+ assert_eq!(
453
+ dependencies[0]
454
+ .visibility
455
+ .combinators
456
+ .iter()
457
+ .map(|combinator| (combinator.kind, combinator.names.as_slice()))
458
+ .collect::<Vec<_>>(),
459
+ vec![
460
+ (DartCombinatorKind::Show, &["Service".to_owned()][..]),
461
+ (DartCombinatorKind::Hide, &["Hidden".to_owned()][..]),
462
+ ]
463
+ );
464
+ assert_eq!(
465
+ dependencies[1].visibility.combinators[0].names,
466
+ vec!["PublicApi"]
467
+ );
468
+
469
+ Ok(())
470
+ }
471
+
472
+ #[test]
473
+ fn builds_edges_for_part_directives() -> Result<(), Box<dyn std::error::Error>> {
474
+ let fixture = Fixture::new()?;
475
+ fixture.write("pubspec.yaml", "name: app\n")?;
476
+ let mut library = fixture.file("lib/model.dart", vec![], vec![]);
477
+ library.parts = vec![part("src/model.g.dart")];
478
+ let mut generated = fixture.file("lib/src/model.g.dart", vec![], vec![]);
479
+ generated.part_of = Some(part_of_uri("../model.dart"));
480
+
481
+ let graph = build_module_graph(fixture.root(), &[library, generated])?;
482
+
483
+ assert_eq!(graph.edge_count(), 1);
484
+ assert!(graph.invalid_part_relationships().is_empty());
485
+ assert_eq!(graph.dependencies()[0].kind, DependencyKind::Part);
486
+ assert_eq!(
487
+ strip_root(fixture.root(), &graph.dependencies()[0].to_path),
488
+ "lib/src/model.g.dart"
489
+ );
490
+
491
+ Ok(())
492
+ }
493
+
494
+ #[test]
495
+ fn reports_missing_part_of_for_resolved_part() -> Result<(), Box<dyn std::error::Error>> {
496
+ let fixture = Fixture::new()?;
497
+ fixture.write("pubspec.yaml", "name: app\n")?;
498
+ let mut library = fixture.file("lib/model.dart", vec![], vec![]);
499
+ library.parts = vec![part("src/model.g.dart")];
500
+ let generated = fixture.file("lib/src/model.g.dart", vec![], vec![]);
501
+
502
+ let graph = build_module_graph(fixture.root(), &[library, generated])?;
503
+
504
+ assert_eq!(graph.invalid_part_relationships().len(), 1);
505
+ assert_eq!(
506
+ graph.invalid_part_relationships()[0].reason,
507
+ InvalidPartReason::MissingPartOf
508
+ );
509
+ assert_eq!(graph.edge_count(), 0);
510
+
511
+ Ok(())
512
+ }
513
+
514
+ #[test]
515
+ fn reports_part_of_uri_mismatch_for_resolved_part() -> Result<(), Box<dyn std::error::Error>> {
516
+ let fixture = Fixture::new()?;
517
+ fixture.write("pubspec.yaml", "name: app\n")?;
518
+ let mut library = fixture.file("lib/model.dart", vec![], vec![]);
519
+ library.parts = vec![part("src/model.g.dart")];
520
+ let mut generated = fixture.file("lib/src/model.g.dart", vec![], vec![]);
521
+ generated.part_of = Some(part_of_uri("../other.dart"));
522
+
523
+ let graph = build_module_graph(fixture.root(), &[library, generated])?;
524
+
525
+ assert_eq!(
526
+ graph.invalid_part_relationships()[0].reason,
527
+ InvalidPartReason::PartOfUriMismatch {
528
+ expected_path: fixture.path("lib/model.dart"),
529
+ actual_path: fixture.path("lib/other.dart"),
530
+ actual_specifier: "../other.dart".to_owned()
531
+ }
532
+ );
533
+
534
+ Ok(())
535
+ }
536
+
537
+ #[test]
538
+ fn reports_part_of_name_mismatch_for_resolved_part() -> Result<(), Box<dyn std::error::Error>> {
539
+ let fixture = Fixture::new()?;
540
+ fixture.write("pubspec.yaml", "name: app\n")?;
541
+ let mut library = fixture.file("lib/model.dart", vec![], vec![]);
542
+ library.library = Some(DartLibrary {
543
+ name: Some("app.model".to_owned()),
544
+ augment_uri: None,
545
+ location: Location { line: 1, column: 0 },
546
+ });
547
+ library.parts = vec![part("src/model.g.dart")];
548
+ let mut generated = fixture.file("lib/src/model.g.dart", vec![], vec![]);
549
+ generated.part_of = Some(part_of_name("app.other"));
550
+
551
+ let graph = build_module_graph(fixture.root(), &[library, generated])?;
552
+
553
+ assert_eq!(
554
+ graph.invalid_part_relationships()[0].reason,
555
+ InvalidPartReason::PartOfNameMismatch {
556
+ expected_name: Some("app.model".to_owned()),
557
+ actual_name: "app.other".to_owned()
558
+ }
559
+ );
560
+
561
+ Ok(())
562
+ }
563
+
564
+ #[test]
565
+ fn reports_orphan_part_of_without_owning_part_directive() -> Result<(), Box<dyn std::error::Error>>
566
+ {
567
+ let fixture = Fixture::new()?;
568
+ fixture.write("pubspec.yaml", "name: app\n")?;
569
+ let library = fixture.file("lib/model.dart", vec![], vec![]);
570
+ let mut generated = fixture.file("lib/src/model.g.dart", vec![], vec![]);
571
+ generated.part_of = Some(part_of_uri("../model.dart"));
572
+
573
+ let graph = build_module_graph(fixture.root(), &[library, generated])?;
574
+
575
+ assert_eq!(
576
+ graph.invalid_part_relationships()[0].reason,
577
+ InvalidPartReason::OrphanPartOf {
578
+ actual_name: None,
579
+ actual_specifier: Some("../model.dart".to_owned())
580
+ }
581
+ );
582
+
583
+ Ok(())
584
+ }
585
+
586
+ #[test]
587
+ fn resolves_package_imports_to_path_dependencies() -> Result<(), Box<dyn std::error::Error>> {
588
+ let fixture = Fixture::new()?;
589
+ fixture.write(
590
+ "pubspec.yaml",
591
+ "name: app\ndependencies:\n shared:\n path: ../shared\n",
592
+ )?;
593
+ let shared_root = fixture.path("../shared");
594
+ fs::create_dir_all(shared_root.join("lib"))?;
595
+ fs::write(shared_root.join("pubspec.yaml"), "name: shared\n")?;
596
+
597
+ let main = fixture.file(
598
+ "lib/main.dart",
599
+ vec![import("package:shared/src/api.dart")],
600
+ vec![],
601
+ );
602
+ let shared = DartFile {
603
+ path: shared_root.join("lib/src/api.dart"),
604
+ library: None,
605
+ part_of: None,
606
+ imports: vec![],
607
+ exports: vec![],
608
+ parts: vec![],
609
+ declarations: vec![],
610
+ members: vec![],
611
+ references: vec![],
612
+ signature_references: vec![],
613
+ routes: vec![],
614
+ };
615
+
616
+ let graph = build_module_graph(fixture.root(), &[main, shared])?;
617
+
618
+ assert_eq!(graph.package_names(), vec!["app", "shared"]);
619
+ assert_eq!(graph.edge_count(), 1);
620
+
621
+ Ok(())
622
+ }
623
+
624
+ #[test]
625
+ fn resolves_package_imports_to_nested_local_pubspecs() -> Result<(), Box<dyn std::error::Error>> {
626
+ let fixture = Fixture::new()?;
627
+ fixture.write("pubspec.yaml", "name: repo\n")?;
628
+ fixture.write("examples/app/pubspec.yaml", "name: app\n")?;
629
+
630
+ let main = fixture.file(
631
+ "examples/app/lib/main.dart",
632
+ vec![import("package:app/src/widget.dart")],
633
+ vec![],
634
+ );
635
+ let widget = fixture.file("examples/app/lib/src/widget.dart", vec![], vec![]);
636
+
637
+ let graph = build_module_graph(fixture.root(), &[main, widget])?;
638
+
639
+ assert_eq!(graph.package_names(), vec!["app", "repo"]);
640
+ assert_eq!(graph.edge_count(), 1);
641
+ assert!(graph.unresolved().is_empty());
642
+
643
+ Ok(())
644
+ }
645
+
646
+ #[test]
647
+ fn excludes_external_packages_and_records_missing_local_targets()
648
+ -> Result<(), Box<dyn std::error::Error>> {
649
+ let fixture = Fixture::new()?;
650
+ fixture.write("pubspec.yaml", "name: app\n")?;
651
+ let main = fixture.file(
652
+ "lib/main.dart",
653
+ vec![
654
+ import("dart:async"),
655
+ import("package:http/http.dart"),
656
+ import("src/missing.dart"),
657
+ ],
658
+ vec![],
659
+ );
660
+
661
+ let graph = build_module_graph(fixture.root(), &[main])?;
662
+
663
+ assert_eq!(graph.edge_count(), 0);
664
+ assert_eq!(graph.unresolved().len(), 1);
665
+ assert_eq!(graph.unresolved()[0].specifier, "src/missing.dart");
666
+ assert_eq!(
667
+ strip_root(fixture.root(), &graph.unresolved()[0].attempted_path),
668
+ "lib/src/missing.dart"
669
+ );
670
+
671
+ Ok(())
672
+ }
673
+
674
+ #[test]
675
+ fn records_unresolved_known_package_import_target() -> Result<(), Box<dyn std::error::Error>> {
676
+ let fixture = Fixture::new()?;
677
+ fixture.write("pubspec.yaml", "name: app\nworkspace:\n - packages/*\n")?;
678
+ fixture.write("packages/shared/pubspec.yaml", "name: shared\n")?;
679
+ let main = fixture.file(
680
+ "lib/main.dart",
681
+ vec![import("package:shared/missing.dart")],
682
+ vec![],
683
+ );
684
+
685
+ let graph = build_module_graph(fixture.root(), &[main])?;
686
+
687
+ assert_eq!(graph.edge_count(), 0);
688
+ assert_eq!(graph.unresolved().len(), 1);
689
+ assert_eq!(
690
+ graph.unresolved()[0].specifier,
691
+ "package:shared/missing.dart"
692
+ );
693
+ assert_eq!(
694
+ strip_root(fixture.root(), &graph.unresolved()[0].attempted_path),
695
+ "packages/shared/lib/missing.dart"
696
+ );
697
+
698
+ Ok(())
699
+ }
700
+
701
+ #[test]
702
+ fn malformed_package_config_is_a_graph_error() -> Result<(), Box<dyn std::error::Error>> {
703
+ let fixture = Fixture::new()?;
704
+ fixture.write(
705
+ ".dart_tool/package_config.json",
706
+ r#"{ "configVersion": 2, "packages": ["#,
707
+ )?;
708
+ let main = fixture.file("lib/main.dart", vec![], vec![]);
709
+
710
+ match build_module_graph(fixture.root(), &[main]) {
711
+ Err(GraphError::ParsePackageConfig { .. }) => {}
712
+ other => panic!("expected package config parse error, got {other:?}"),
713
+ }
714
+
715
+ Ok(())
716
+ }
717
+
718
+ #[test]
719
+ fn keeps_current_directory_when_normalizing_dot() {
720
+ assert_eq!(normalize_path(Path::new(".")), PathBuf::from("."));
721
+ }
722
+
723
+ #[test]
724
+ fn does_not_join_paths_that_already_start_with_root() {
725
+ assert_eq!(
726
+ normalize_against(Path::new("repo/app"), Path::new("repo/app/lib/main.dart")),
727
+ PathBuf::from("repo/app/lib/main.dart")
728
+ );
729
+ }
730
+
731
+ fn import(uri: &str) -> DartImport {
732
+ DartImport {
733
+ uri: uri.to_owned(),
734
+ condition: None,
735
+ prefix: None,
736
+ deferred: false,
737
+ combinators: Vec::new(),
738
+ location: Location { line: 1, column: 0 },
739
+ }
740
+ }
741
+
742
+ fn export(uri: &str) -> DartExport {
743
+ DartExport {
744
+ uri: uri.to_owned(),
745
+ condition: None,
746
+ combinators: Vec::new(),
747
+ location: Location { line: 1, column: 0 },
748
+ }
749
+ }
750
+
751
+ fn part(uri: &str) -> DartPart {
752
+ DartPart {
753
+ uri: uri.to_owned(),
754
+ location: Location { line: 1, column: 0 },
755
+ }
756
+ }
757
+
758
+ fn part_of_uri(uri: &str) -> DartPartOf {
759
+ DartPartOf {
760
+ name: None,
761
+ uri: Some(uri.to_owned()),
762
+ location: Location { line: 1, column: 0 },
763
+ }
764
+ }
765
+
766
+ fn part_of_name(name: &str) -> DartPartOf {
767
+ DartPartOf {
768
+ name: Some(name.to_owned()),
769
+ uri: None,
770
+ location: Location { line: 1, column: 0 },
771
+ }
772
+ }
773
+
774
+ fn combinator(kind: DartCombinatorKind, names: &[&str]) -> DartCombinator {
775
+ DartCombinator {
776
+ kind,
777
+ names: names.iter().map(|name| (*name).to_owned()).collect(),
778
+ location: Location { line: 1, column: 0 },
779
+ }
780
+ }
781
+
782
+ fn condition(variable: &str, expected_value: &str, line: usize) -> DartUriCondition {
783
+ DartUriCondition {
784
+ variable: variable.to_owned(),
785
+ expected_value: expected_value.to_owned(),
786
+ location: Location { line, column: 0 },
787
+ }
788
+ }
789
+
790
+ fn strip_root(root: &Path, path: &Path) -> String {
791
+ path.strip_prefix(root)
792
+ .unwrap_or(path)
793
+ .to_string_lossy()
794
+ .replace('\\', "/")
795
+ }
796
+
797
+ struct Fixture {
798
+ temp: TempDir,
799
+ }
800
+
801
+ impl Fixture {
802
+ fn new() -> Result<Self, std::io::Error> {
803
+ tempfile::tempdir().map(|temp| Self { temp })
804
+ }
805
+
806
+ fn root(&self) -> &Path {
807
+ self.temp.path()
808
+ }
809
+
810
+ fn path(&self, path: &str) -> PathBuf {
811
+ normalize_path(&self.root().join(path))
812
+ }
813
+
814
+ fn write(&self, path: &str, source: &str) -> Result<(), std::io::Error> {
815
+ let path = self.path(path);
816
+ if let Some(parent) = path.parent() {
817
+ fs::create_dir_all(parent)?;
818
+ }
819
+ fs::write(path, source)
820
+ }
821
+
822
+ fn file(&self, path: &str, imports: Vec<DartImport>, exports: Vec<DartExport>) -> DartFile {
823
+ DartFile {
824
+ path: self.path(path),
825
+ library: None,
826
+ part_of: None,
827
+ imports,
828
+ exports,
829
+ parts: vec![],
830
+ declarations: vec![],
831
+ members: vec![],
832
+ references: vec![],
833
+ signature_references: vec![],
834
+ routes: vec![],
835
+ }
836
+ }
837
+ }