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,692 @@
1
+ use std::fs;
2
+
3
+ use tempfile::TempDir;
4
+
5
+ use super::*;
6
+ use crate::{find_dead_code, scan_project};
7
+
8
+ #[test]
9
+ fn reports_unreferenced_public_declarations_in_reachable_internal_libraries()
10
+ -> Result<(), Box<dyn std::error::Error>> {
11
+ let fixture = tempfile::tempdir()?;
12
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
13
+ write(
14
+ &fixture,
15
+ "lib/main.dart",
16
+ "import 'src/live.dart';\nvoid main() { Used(); }\n",
17
+ )?;
18
+ write(
19
+ &fixture,
20
+ "lib/src/live.dart",
21
+ "class Used {}\nclass Unused {}\nvoid helper() {}\nclass Review {\n}\n",
22
+ )?;
23
+ let project = scan_project(fixture.path())?;
24
+ let dead_code = find_dead_code(&project.graph, ["lib/main.dart"]);
25
+
26
+ let report = analyze_unused_exports(&project, &dead_code);
27
+
28
+ assert_eq!(
29
+ report
30
+ .unused_exports
31
+ .iter()
32
+ .map(|unused| (unused.name.as_str(), unused.kind, unused.safe_to_delete))
33
+ .collect::<Vec<_>>(),
34
+ vec![
35
+ ("Unused", DeclarationKind::Class, true),
36
+ ("helper", DeclarationKind::Function, true),
37
+ ("Review", DeclarationKind::Class, false),
38
+ ]
39
+ );
40
+
41
+ Ok(())
42
+ }
43
+
44
+ #[test]
45
+ fn skips_private_entry_generated_and_dead_file_declarations()
46
+ -> Result<(), Box<dyn std::error::Error>> {
47
+ let fixture = tempfile::tempdir()?;
48
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
49
+ write(
50
+ &fixture,
51
+ "lib/main.dart",
52
+ "import 'src/live.dart';\nvoid main() { Live(); }\nclass EntryOnly {}\n",
53
+ )?;
54
+ write(
55
+ &fixture,
56
+ "lib/src/live.dart",
57
+ "part 'live.g.dart';\nclass Live {}\nclass _Private {}\n",
58
+ )?;
59
+ write(&fixture, "lib/src/live.g.dart", "class Generated {}\n")?;
60
+ write(&fixture, "lib/src/dead.dart", "class Dead {}\n")?;
61
+ let project = scan_project(fixture.path())?;
62
+ let dead_code = find_dead_code(&project.graph, ["lib/main.dart"]);
63
+
64
+ let report = analyze_unused_exports(&project, &dead_code);
65
+
66
+ assert!(report.unused_exports.is_empty());
67
+
68
+ Ok(())
69
+ }
70
+
71
+ #[test]
72
+ fn include_entry_exports_reports_entry_declarations_except_main()
73
+ -> Result<(), Box<dyn std::error::Error>> {
74
+ let fixture = tempfile::tempdir()?;
75
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
76
+ write(
77
+ &fixture,
78
+ "lib/main.dart",
79
+ "void main() {}\nclass EntryOnly {}\nvoid helper() {}\n",
80
+ )?;
81
+ let project = scan_project(fixture.path())?;
82
+ let dead_code = find_dead_code(&project.graph, ["lib/main.dart"]);
83
+
84
+ let default_report = analyze_symbols(&project, Some(&dead_code));
85
+ assert!(default_report.unused_exports.is_empty());
86
+
87
+ let report = analyze_symbols_with_options(
88
+ &project,
89
+ Some(&dead_code),
90
+ SymbolAnalysisOptions {
91
+ include_entry_exports: true,
92
+ private_type_leaks: false,
93
+ },
94
+ );
95
+
96
+ assert_eq!(
97
+ report
98
+ .unused_exports
99
+ .iter()
100
+ .map(|unused| unused.name.as_str())
101
+ .collect::<Vec<_>>(),
102
+ vec!["EntryOnly", "helper"]
103
+ );
104
+
105
+ Ok(())
106
+ }
107
+
108
+ #[test]
109
+ fn treats_public_library_exports_as_api_usage() -> Result<(), Box<dyn std::error::Error>> {
110
+ let fixture = tempfile::tempdir()?;
111
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
112
+ write(
113
+ &fixture,
114
+ "lib/package.dart",
115
+ "export 'src/api.dart' show PublicApi;\n",
116
+ )?;
117
+ write(
118
+ &fixture,
119
+ "lib/src/api.dart",
120
+ "class PublicApi {}\nclass HiddenApi {}\n",
121
+ )?;
122
+ let project = scan_project(fixture.path())?;
123
+ let dead_code = find_dead_code(&project.graph, ["lib/package.dart"]);
124
+
125
+ let report = analyze_unused_exports(&project, &dead_code);
126
+
127
+ assert_eq!(
128
+ report
129
+ .unused_exports
130
+ .iter()
131
+ .map(|unused| unused.name.as_str())
132
+ .collect::<Vec<_>>(),
133
+ vec!["HiddenApi"]
134
+ );
135
+
136
+ Ok(())
137
+ }
138
+
139
+ #[test]
140
+ fn treats_public_library_part_declarations_as_api_usage() -> Result<(), Box<dyn std::error::Error>>
141
+ {
142
+ let fixture = tempfile::tempdir()?;
143
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
144
+ write(&fixture, "lib/package.dart", "part 'src/api_part.dart';\n")?;
145
+ write(
146
+ &fixture,
147
+ "lib/src/api_part.dart",
148
+ "part of '../package.dart';\nclass PartApi {}\n",
149
+ )?;
150
+ let project = scan_project(fixture.path())?;
151
+ let dead_code = find_dead_code(&project.graph, ["lib/package.dart"]);
152
+
153
+ let report = analyze_unused_exports(&project, &dead_code);
154
+
155
+ assert!(report.unused_exports.is_empty());
156
+
157
+ Ok(())
158
+ }
159
+
160
+ #[test]
161
+ fn re_exported_library_part_declarations_respect_combinators()
162
+ -> Result<(), Box<dyn std::error::Error>> {
163
+ let fixture = tempfile::tempdir()?;
164
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
165
+ write(
166
+ &fixture,
167
+ "lib/package.dart",
168
+ "export 'src/api.dart' show PublicPartApi;\n",
169
+ )?;
170
+ write(&fixture, "lib/src/api.dart", "part 'api_part.dart';\n")?;
171
+ write(
172
+ &fixture,
173
+ "lib/src/api_part.dart",
174
+ "part of 'api.dart';\nclass PublicPartApi {}\nclass HiddenPartApi {}\n",
175
+ )?;
176
+ let project = scan_project(fixture.path())?;
177
+ let dead_code = find_dead_code(&project.graph, ["lib/package.dart"]);
178
+
179
+ let report = analyze_unused_exports(&project, &dead_code);
180
+
181
+ assert_eq!(
182
+ report
183
+ .unused_exports
184
+ .iter()
185
+ .map(|unused| unused.name.as_str())
186
+ .collect::<Vec<_>>(),
187
+ vec!["HiddenPartApi"]
188
+ );
189
+
190
+ Ok(())
191
+ }
192
+
193
+ #[test]
194
+ fn reports_unused_enum_constants_and_private_class_members()
195
+ -> Result<(), Box<dyn std::error::Error>> {
196
+ let fixture = tempfile::tempdir()?;
197
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
198
+ write(
199
+ &fixture,
200
+ "lib/main.dart",
201
+ "import 'src/live.dart';\nvoid main() { runLive(); }\n",
202
+ )?;
203
+ write(
204
+ &fixture,
205
+ "lib/src/live.dart",
206
+ "\
207
+ enum Mode { on, off }
208
+
209
+ class Controller {
210
+ Controller();
211
+ Controller.named();
212
+ final int _unusedField = 1;
213
+ void _used() {}
214
+ void _unused() {}
215
+ void publicUnused() {}
216
+ bool operator ==(Object other) => identical(this, other);
217
+ }
218
+
219
+ extension InternalText on String {
220
+ int get _unusedLength => length;
221
+ }
222
+
223
+ void runLive() {
224
+ final controller = Controller();
225
+ controller._used();
226
+ final mode = Mode.on;
227
+ print(mode);
228
+ }
229
+ ",
230
+ )?;
231
+ let project = scan_project(fixture.path())?;
232
+ let dead_code = find_dead_code(&project.graph, ["lib/main.dart"]);
233
+
234
+ let report = analyze_unused_exports(&project, &dead_code);
235
+
236
+ assert_eq!(
237
+ report
238
+ .unused_members
239
+ .iter()
240
+ .map(|unused| (unused.owner.as_str(), unused.name.as_str(), unused.kind))
241
+ .collect::<Vec<_>>(),
242
+ vec![
243
+ ("Mode", "off", MemberKind::EnumConstant),
244
+ ("Controller", "_unusedField", MemberKind::Field),
245
+ ("Controller", "_unused", MemberKind::Method),
246
+ ("InternalText", "_unusedLength", MemberKind::Getter),
247
+ ]
248
+ );
249
+
250
+ Ok(())
251
+ }
252
+
253
+ #[test]
254
+ fn skips_enum_constants_on_public_api_enums() -> Result<(), Box<dyn std::error::Error>> {
255
+ let fixture = tempfile::tempdir()?;
256
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
257
+ write(&fixture, "lib/package.dart", "export 'src/api.dart';\n")?;
258
+ write(
259
+ &fixture,
260
+ "lib/src/api.dart",
261
+ "enum PublicMode { one, two }\n",
262
+ )?;
263
+ let project = scan_project(fixture.path())?;
264
+ let dead_code = find_dead_code(&project.graph, ["lib/package.dart"]);
265
+
266
+ let report = analyze_unused_exports(&project, &dead_code);
267
+
268
+ assert!(report.unused_members.is_empty());
269
+
270
+ Ok(())
271
+ }
272
+
273
+ #[test]
274
+ fn member_references_are_scoped_to_dart_library_parts() -> Result<(), Box<dyn std::error::Error>> {
275
+ let fixture = tempfile::tempdir()?;
276
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
277
+ write(
278
+ &fixture,
279
+ "lib/main.dart",
280
+ "import 'src/live.dart';\nimport 'src/other.dart';\nvoid main() { runLive(); runOther(); }\n",
281
+ )?;
282
+ write(
283
+ &fixture,
284
+ "lib/src/live.dart",
285
+ "\
286
+ part 'live_part.dart';
287
+ class Live {
288
+ void _fromPart() {}
289
+ void _shadowedElsewhere() {}
290
+ }
291
+ void runLive() { Live()._fromPart(); }
292
+ ",
293
+ )?;
294
+ write(
295
+ &fixture,
296
+ "lib/src/live_part.dart",
297
+ "part of 'live.dart';\nvoid touchPart(Live live) { live._fromPart(); }\n",
298
+ )?;
299
+ write(
300
+ &fixture,
301
+ "lib/src/other.dart",
302
+ "\
303
+ class Other {
304
+ void _shadowedElsewhere() {}
305
+ }
306
+ void runOther() { Other()._shadowedElsewhere(); }
307
+ ",
308
+ )?;
309
+ let project = scan_project(fixture.path())?;
310
+ let dead_code = find_dead_code(&project.graph, ["lib/main.dart"]);
311
+
312
+ let report = analyze_unused_exports(&project, &dead_code);
313
+
314
+ assert_eq!(
315
+ report
316
+ .unused_members
317
+ .iter()
318
+ .map(|unused| (unused.owner.as_str(), unused.name.as_str()))
319
+ .collect::<Vec<_>>(),
320
+ vec![("Live", "_shadowedElsewhere")]
321
+ );
322
+
323
+ Ok(())
324
+ }
325
+
326
+ #[test]
327
+ fn groups_field_getter_and_setter_member_findings() -> Result<(), Box<dyn std::error::Error>> {
328
+ let fixture = tempfile::tempdir()?;
329
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
330
+ write(
331
+ &fixture,
332
+ "lib/main.dart",
333
+ "import 'src/live.dart';\nvoid main() { runLive(); }\n",
334
+ )?;
335
+ write(
336
+ &fixture,
337
+ "lib/src/live.dart",
338
+ "\
339
+ class Live {
340
+ int _value = 0;
341
+ int get _value => 1;
342
+ set _value(int value) {}
343
+ }
344
+ void runLive() { Live(); }
345
+ ",
346
+ )?;
347
+ let project = scan_project(fixture.path())?;
348
+ let dead_code = find_dead_code(&project.graph, ["lib/main.dart"]);
349
+
350
+ let report = analyze_unused_exports(&project, &dead_code);
351
+
352
+ assert_eq!(
353
+ report
354
+ .unused_members
355
+ .iter()
356
+ .filter(|unused| unused.name == "_value")
357
+ .count(),
358
+ 1
359
+ );
360
+
361
+ Ok(())
362
+ }
363
+
364
+ #[test]
365
+ fn propagates_public_api_through_re_export_chains() -> Result<(), Box<dyn std::error::Error>> {
366
+ let fixture = tempfile::tempdir()?;
367
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
368
+ write(&fixture, "lib/package.dart", "export 'src/barrel.dart';\n")?;
369
+ write(
370
+ &fixture,
371
+ "lib/src/barrel.dart",
372
+ "export 'api.dart' show PublicApi;\n",
373
+ )?;
374
+ write(
375
+ &fixture,
376
+ "lib/src/api.dart",
377
+ "class PublicApi {}\nclass HiddenApi {}\n",
378
+ )?;
379
+ let project = scan_project(fixture.path())?;
380
+ let dead_code = find_dead_code(&project.graph, ["lib/package.dart"]);
381
+
382
+ let report = analyze_unused_exports(&project, &dead_code);
383
+
384
+ assert_eq!(
385
+ report
386
+ .unused_exports
387
+ .iter()
388
+ .map(|unused| unused.name.as_str())
389
+ .collect::<Vec<_>>(),
390
+ vec!["HiddenApi"]
391
+ );
392
+
393
+ Ok(())
394
+ }
395
+
396
+ #[test]
397
+ fn reports_duplicate_public_symbol_from_public_barrel() -> Result<(), Box<dyn std::error::Error>> {
398
+ let fixture = tempfile::tempdir()?;
399
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
400
+ write(
401
+ &fixture,
402
+ "lib/package.dart",
403
+ "export 'src/a.dart';\nexport 'src/b.dart';\n",
404
+ )?;
405
+ write(&fixture, "lib/src/a.dart", "class Api {}\nclass OnlyA {}\n")?;
406
+ write(&fixture, "lib/src/b.dart", "class Api {}\nclass OnlyB {}\n")?;
407
+ let project = scan_project(fixture.path())?;
408
+
409
+ let report = analyze_symbols(&project, None);
410
+
411
+ assert_eq!(report.duplicate_exports.len(), 1);
412
+ let duplicate = &report.duplicate_exports[0];
413
+ assert_eq!(
414
+ duplicate.entry_path,
415
+ fixture.path().join("lib/package.dart")
416
+ );
417
+ assert_eq!(duplicate.name, "Api");
418
+ assert_eq!(
419
+ duplicate
420
+ .declarations
421
+ .iter()
422
+ .map(|declaration| declaration.path.clone())
423
+ .collect::<Vec<_>>(),
424
+ vec![
425
+ fixture.path().join("lib/src/a.dart"),
426
+ fixture.path().join("lib/src/b.dart"),
427
+ ]
428
+ );
429
+ assert!(!duplicate.safe_to_delete);
430
+
431
+ Ok(())
432
+ }
433
+
434
+ #[test]
435
+ fn duplicate_exports_respect_show_and_hide() -> Result<(), Box<dyn std::error::Error>> {
436
+ let fixture = tempfile::tempdir()?;
437
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
438
+ write(
439
+ &fixture,
440
+ "lib/package.dart",
441
+ "export 'src/a.dart' show Api;\nexport 'src/b.dart' hide Api;\n",
442
+ )?;
443
+ write(&fixture, "lib/src/a.dart", "class Api {}\n")?;
444
+ write(&fixture, "lib/src/b.dart", "class Api {}\nclass Other {}\n")?;
445
+ let project = scan_project(fixture.path())?;
446
+
447
+ let report = analyze_symbols(&project, None);
448
+
449
+ assert!(report.duplicate_exports.is_empty());
450
+
451
+ Ok(())
452
+ }
453
+
454
+ #[test]
455
+ fn reports_duplicate_public_symbol_through_re_export_chain()
456
+ -> Result<(), Box<dyn std::error::Error>> {
457
+ let fixture = tempfile::tempdir()?;
458
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
459
+ write(
460
+ &fixture,
461
+ "lib/package.dart",
462
+ "export 'src/feature_a.dart';\nexport 'src/feature_b.dart';\n",
463
+ )?;
464
+ write(&fixture, "lib/src/feature_a.dart", "export 'a_api.dart';\n")?;
465
+ write(&fixture, "lib/src/feature_b.dart", "export 'b_api.dart';\n")?;
466
+ write(&fixture, "lib/src/a_api.dart", "class Api {}\n")?;
467
+ write(&fixture, "lib/src/b_api.dart", "class Api {}\n")?;
468
+ let project = scan_project(fixture.path())?;
469
+
470
+ let report = analyze_symbols(&project, None);
471
+
472
+ assert_eq!(report.duplicate_exports.len(), 1);
473
+ assert_eq!(
474
+ report.duplicate_exports[0].entry_path,
475
+ fixture.path().join("lib/package.dart")
476
+ );
477
+ assert_eq!(
478
+ report.duplicate_exports[0]
479
+ .declarations
480
+ .iter()
481
+ .map(|declaration| declaration.path.clone())
482
+ .collect::<Vec<_>>(),
483
+ vec![
484
+ fixture.path().join("lib/src/a_api.dart"),
485
+ fixture.path().join("lib/src/b_api.dart"),
486
+ ]
487
+ );
488
+
489
+ Ok(())
490
+ }
491
+
492
+ #[test]
493
+ fn duplicate_exports_respect_show_hide_at_each_chain_hop() -> Result<(), Box<dyn std::error::Error>>
494
+ {
495
+ let fixture = tempfile::tempdir()?;
496
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
497
+ write(
498
+ &fixture,
499
+ "lib/package.dart",
500
+ "export 'src/feature_a.dart' show Api;\nexport 'src/feature_b.dart';\n",
501
+ )?;
502
+ write(
503
+ &fixture,
504
+ "lib/src/feature_a.dart",
505
+ "export 'a_api.dart' hide Api;\n",
506
+ )?;
507
+ write(
508
+ &fixture,
509
+ "lib/src/feature_b.dart",
510
+ "export 'b_api.dart' show Api;\n",
511
+ )?;
512
+ write(&fixture, "lib/src/a_api.dart", "class Api {}\n")?;
513
+ write(&fixture, "lib/src/b_api.dart", "class Api {}\n")?;
514
+ let project = scan_project(fixture.path())?;
515
+
516
+ let report = analyze_symbols(&project, None);
517
+
518
+ assert!(report.duplicate_exports.is_empty());
519
+
520
+ Ok(())
521
+ }
522
+
523
+ #[test]
524
+ fn does_not_treat_private_src_barrel_as_public_surface() -> Result<(), Box<dyn std::error::Error>> {
525
+ let fixture = tempfile::tempdir()?;
526
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
527
+ write(
528
+ &fixture,
529
+ "lib/src/internal_barrel.dart",
530
+ "export 'a.dart';\nexport 'b.dart';\n",
531
+ )?;
532
+ write(&fixture, "lib/src/a.dart", "class Api {}\n")?;
533
+ write(&fixture, "lib/src/b.dart", "class Api {}\n")?;
534
+ let project = scan_project(fixture.path())?;
535
+
536
+ let report = analyze_symbols(&project, None);
537
+
538
+ assert!(report.duplicate_exports.is_empty());
539
+
540
+ Ok(())
541
+ }
542
+
543
+ #[test]
544
+ fn reports_private_type_leaks_from_public_library_and_parts()
545
+ -> Result<(), Box<dyn std::error::Error>> {
546
+ let fixture = tempfile::tempdir()?;
547
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
548
+ write(
549
+ &fixture,
550
+ "lib/package.dart",
551
+ "\
552
+ part 'src/api_part.dart';
553
+ class Api extends _Hidden {}
554
+ final _Hidden exposed = _Hidden();
555
+ class _Hidden {}
556
+ ",
557
+ )?;
558
+ write(
559
+ &fixture,
560
+ "lib/src/api_part.dart",
561
+ "part of '../package.dart';\ntypedef PartAlias = _Hidden Function();\n",
562
+ )?;
563
+ let project = scan_project(fixture.path())?;
564
+
565
+ let report = analyze_symbols_with_options(
566
+ &project,
567
+ None,
568
+ SymbolAnalysisOptions {
569
+ include_entry_exports: false,
570
+ private_type_leaks: true,
571
+ },
572
+ );
573
+
574
+ assert_eq!(
575
+ report
576
+ .private_type_leaks
577
+ .iter()
578
+ .map(|leak| (leak.declaration.as_str(), leak.private_type.as_str()))
579
+ .collect::<Vec<_>>(),
580
+ vec![
581
+ ("Api", "_Hidden"),
582
+ ("exposed", "_Hidden"),
583
+ ("PartAlias", "_Hidden"),
584
+ ]
585
+ );
586
+
587
+ Ok(())
588
+ }
589
+
590
+ #[test]
591
+ fn private_type_leaks_respect_export_combinators() -> Result<(), Box<dyn std::error::Error>> {
592
+ let fixture = tempfile::tempdir()?;
593
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
594
+ write(
595
+ &fixture,
596
+ "lib/package.dart",
597
+ "export 'src/api.dart' show PublicApi;\n",
598
+ )?;
599
+ write(
600
+ &fixture,
601
+ "lib/src/api.dart",
602
+ "class PublicApi extends _Hidden {}\nclass HiddenApi extends _Hidden {}\nclass _Hidden {}\n",
603
+ )?;
604
+ let project = scan_project(fixture.path())?;
605
+
606
+ let report = analyze_symbols_with_options(
607
+ &project,
608
+ None,
609
+ SymbolAnalysisOptions {
610
+ include_entry_exports: false,
611
+ private_type_leaks: true,
612
+ },
613
+ );
614
+
615
+ assert_eq!(
616
+ report
617
+ .private_type_leaks
618
+ .iter()
619
+ .map(|leak| leak.declaration.as_str())
620
+ .collect::<Vec<_>>(),
621
+ vec!["PublicApi"]
622
+ );
623
+
624
+ Ok(())
625
+ }
626
+
627
+ #[test]
628
+ fn private_type_leaks_require_same_dart_library_private_type()
629
+ -> Result<(), Box<dyn std::error::Error>> {
630
+ let fixture = tempfile::tempdir()?;
631
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
632
+ write(&fixture, "lib/package.dart", "export 'src/api.dart';\n")?;
633
+ write(
634
+ &fixture,
635
+ "lib/src/api.dart",
636
+ "import 'internal.dart';\n_Hidden make() => throw UnimplementedError();\n",
637
+ )?;
638
+ write(&fixture, "lib/src/internal.dart", "class _Hidden {}\n")?;
639
+ let project = scan_project(fixture.path())?;
640
+
641
+ let report = analyze_symbols_with_options(
642
+ &project,
643
+ None,
644
+ SymbolAnalysisOptions {
645
+ include_entry_exports: false,
646
+ private_type_leaks: true,
647
+ },
648
+ );
649
+
650
+ assert!(report.private_type_leaks.is_empty());
651
+
652
+ Ok(())
653
+ }
654
+
655
+ #[test]
656
+ fn private_type_leaks_skip_private_and_anonymous_declarations()
657
+ -> Result<(), Box<dyn std::error::Error>> {
658
+ let fixture = tempfile::tempdir()?;
659
+ write(&fixture, "pubspec.yaml", "name: package\n")?;
660
+ write(
661
+ &fixture,
662
+ "lib/package.dart",
663
+ "\
664
+ class _Api extends _Hidden {}
665
+ typedef _Alias = _Hidden Function();
666
+ extension on _Hidden {}
667
+ class _Hidden {}
668
+ ",
669
+ )?;
670
+ let project = scan_project(fixture.path())?;
671
+
672
+ let report = analyze_symbols_with_options(
673
+ &project,
674
+ None,
675
+ SymbolAnalysisOptions {
676
+ include_entry_exports: false,
677
+ private_type_leaks: true,
678
+ },
679
+ );
680
+
681
+ assert!(report.private_type_leaks.is_empty());
682
+
683
+ Ok(())
684
+ }
685
+
686
+ fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
687
+ let path = fixture.path().join(path);
688
+ if let Some(parent) = path.parent() {
689
+ fs::create_dir_all(parent)?;
690
+ }
691
+ fs::write(path, source)
692
+ }