-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathanalyze.test.ts
More file actions
5050 lines (4639 loc) · 203 KB
/
Copy pathanalyze.test.ts
File metadata and controls
5050 lines (4639 loc) · 203 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { before, describe, it, test } from "node:test";
import assert from "node:assert/strict";
import { resolve, relative } from "node:path";
import { analyze, defineConfig } from "../src/index.js";
import type { ScanResult } from "../src/types.js";
import { FIXTURES_DIR } from "./helpers/fixtures-dir.js";
const scanFixture = async (
fixtureName: string,
overrides: Record<string, unknown> = {},
): Promise<ScanResult> => {
const fixtureDir = resolve(FIXTURES_DIR, fixtureName);
const config = defineConfig({
rootDir: fixtureDir,
...overrides,
});
return analyze(config);
};
const orphanPaths = (result: ScanResult, fixtureDir: string): string[] =>
result.unusedFiles.map((unusedFile) => relative(fixtureDir, unusedFile.path)).sort();
const deadExportNames = (result: ScanResult): string[] =>
result.unusedExports.map((unusedExport) => unusedExport.name).sort();
const deadExportsByFile = (result: ScanResult, fixtureDir: string): Record<string, string[]> => {
const byFile: Record<string, string[]> = {};
for (const unusedExport of result.unusedExports) {
const relativePath = relative(fixtureDir, unusedExport.path);
if (!byFile[relativePath]) byFile[relativePath] = [];
byFile[relativePath].push(unusedExport.name);
}
for (const key of Object.keys(byFile)) {
byFile[key].sort();
}
return byFile;
};
const staleDependencyNames = (result: ScanResult): string[] =>
result.unusedDependencies.map((dep) => dep.name).sort();
describe("simple-app", () => {
let result: ScanResult;
const fixtureDir = resolve(FIXTURES_DIR, "simple-app");
before(async () => {
result = await scanFixture("simple-app");
});
it("should detect orphan file", () => {
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`orphan.ts should be unused, got: ${unusedFilePaths}`,
);
});
it("should detect unused exports in utils", () => {
const exportsByFile = deadExportsByFile(result, fixtureDir);
assert.ok(
exportsByFile["src/utils.ts"]?.includes("unusedFunction"),
`unusedFunction should be flagged, got: ${JSON.stringify(exportsByFile["src/utils.ts"])}`,
);
});
it("should detect unused dependency", () => {
const deps = staleDependencyNames(result);
assert.ok(deps.includes("unused-dep"), `unused-dep should be flagged, got: ${deps}`);
});
it("should explain each unused dependency with a reason that names the package", () => {
const unusedDep = result.unusedDependencies.find((dep) => dep.name === "unused-dep");
assert.ok(unusedDep, `unused-dep finding should exist, got: ${staleDependencyNames(result)}`);
assert.equal(unusedDep.isDevDependency, false);
assert.match(unusedDep.reason, /"unused-dep"/);
assert.match(unusedDep.reason, /declared in dependencies\b/);
});
it("should not flag usedFunction as unused", () => {
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("usedFunction"), "usedFunction should not be unused");
});
it("should flag react as unused (declared but never imported)", () => {
const deps = staleDependencyNames(result);
assert.ok(deps.includes("react"), `react should be unused since never imported, got: ${deps}`);
});
});
describe("gitignore-app", () => {
it("suppresses reports for gitignored files without dropping their import edges", async () => {
const result = await scanFixture("gitignore-app");
const fixtureDir = resolve(FIXTURES_DIR, "gitignore-app");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`genuine orphan should still be reported, got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.some((filePath) => filePath.includes("generated")),
`gitignored files must not be reported as unused, got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.includes("src/home-route.ts"),
`a file reachable only through a gitignored importer must stay used (no cascade), got: ${unusedFilePaths}`,
);
const unusedExportPaths = result.unusedExports.map((unusedExport) =>
relative(fixtureDir, unusedExport.path),
);
assert.ok(
!unusedExportPaths.some((filePath) => filePath.includes("generated")),
`exports inside gitignored files must not be reported, got: ${unusedExportPaths}`,
);
});
});
describe("dependency-tooling", () => {
it("should keep peer dependencies, script binaries, overrides, and Nx project refs used", async () => {
const result = await scanFixture("dependency-tooling");
const deps = staleDependencyNames(result);
const expectedUsedDeps = [
"@babel/cli",
"@formatjs/cli",
"@hookform/resolvers",
"@nx/js",
"@tauri-apps/cli",
"@tinacms/cli",
"@typescript/native-preview",
"babel-eslint",
"chart.js",
"chokidar-cli",
"jest-cli",
"prompt",
"react-chartjs-2",
"react-redux",
"redux",
"replace-in-file",
"tsc-alias",
"zod",
];
for (const dependencyName of expectedUsedDeps) {
assert.ok(
!deps.includes(dependencyName),
`${dependencyName} should be treated as used, got: ${deps}`,
);
}
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
assert.ok(deps.includes("unused-tool"), `unused-tool should be unused, got: ${deps}`);
assert.ok(deps.includes("redux-thunk"), `redux-thunk should be unused, got: ${deps}`);
});
it("should name the package and devDependencies section for unused dev dependencies", async () => {
const result = await scanFixture("dependency-tooling");
const unusedTool = result.unusedDependencies.find((dep) => dep.name === "unused-tool");
assert.ok(unusedTool, `unused-tool finding should exist, got: ${staleDependencyNames(result)}`);
assert.equal(unusedTool.isDevDependency, true);
assert.match(unusedTool.reason, /"unused-tool"/);
assert.match(unusedTool.reason, /declared in devDependencies\b/);
});
it("should keep pnpm-workspace override targets used", async () => {
const result = await scanFixture("pnpm-workspace-override");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("@voidzero-dev/vite-plus-core"),
`@voidzero-dev/vite-plus-core should be treated as used via pnpm-workspace overrides, got: ${deps}`,
);
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
});
it("resolves script-invoked CLIs without installed metadata only via same-name binaries, prefixes, or implicit deps", async () => {
const result = await scanFixture("script-cli-deps");
const deps = staleDependencyNames(result);
// turbo's binary is `turbo` (same name), tsx is implicit, and `@changesets/*`
// is an always-used prefix — all resolve with no node_modules present.
for (const dependencyName of ["turbo", "tsx", "@changesets/cli"]) {
assert.ok(
!deps.includes(dependencyName),
`${dependencyName} should still be treated as used, got: ${deps}`,
);
}
// vite-plus exposes the `vp` binary, whose name differs from the package; with
// no installed bin metadata there is nothing to map `vp` -> vite-plus, so it is
// flagged. Installing it (real bin metadata) resolves it — see dependency-tooling.
assert.ok(
deps.includes("vite-plus"),
`vite-plus (bin "vp") is unresolvable without installed metadata, got: ${deps}`,
);
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
});
it("should keep nested package.json override targets used", async () => {
const result = await scanFixture("nested-overrides");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("@typescript/native-preview"),
`@typescript/native-preview should be treated as used via nested overrides, got: ${deps}`,
);
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
});
it("should keep nested pnpm-workspace override targets used", async () => {
const result = await scanFixture("pnpm-nested-overrides");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("@typescript/native-preview"),
`@typescript/native-preview should be treated as used via nested pnpm-workspace overrides, got: ${deps}`,
);
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
});
it("should keep vitest override targets used", async () => {
const result = await scanFixture("vitest-override-target");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("@voidzero-dev/vite-plus-test"),
`@voidzero-dev/vite-plus-test should be treated as used via pnpm-workspace overrides, got: ${deps}`,
);
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
});
});
describe("css-tilde-import", () => {
it("should detect Sass tilde package imports as dependency usage", async () => {
const result = await scanFixture("css-tilde-import");
const deps = staleDependencyNames(result);
assert.ok(!deps.includes("bootstrap"), `bootstrap should be used from SCSS, got: ${deps}`);
assert.ok(deps.includes("unused-dep"), `unused-dep should be unused, got: ${deps}`);
});
});
describe("tailwind-v4-plugin", () => {
it('should treat Tailwind v4 `@plugin "pkg"` directives as dependency usage', async () => {
const result = await scanFixture("tailwind-v4-plugin");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("tailwindcss-animate"),
`tailwindcss-animate is loaded via @plugin in CSS and must not be flagged, got: ${deps}`,
);
assert.ok(deps.includes("unused-dep"), `unused-dep should still be unused, got: ${deps}`);
});
});
describe("workspace-local-bin", () => {
it("should resolve script binaries from workspace-local node_modules (pnpm isolation)", async () => {
const result = await scanFixture("workspace-local-bin");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("react-email"),
`react-email provides the 'email' bin used by email:preview script and must not be flagged, got: ${deps}`,
);
assert.ok(
deps.includes("unused-dev-tool"),
`unused-dev-tool should still be unused, got: ${deps}`,
);
});
it("should not flag a package that ships a binary even when no script references it", async () => {
const result = await scanFixture("workspace-local-bin");
const deps = staleDependencyNames(result);
assert.ok(
!deps.includes("bin-only-tool"),
`bin-only-tool declares a bin and is invokable outside the static scan (npx, hooks, CI) — it must not be flagged, got: ${deps}`,
);
});
});
describe("monorepo-script-entry", () => {
it("should treat files referenced by parent monorepo scripts as workspace entry points", async () => {
const fixtureDir = resolve(FIXTURES_DIR, "monorepo-script-entry", "packages", "sub");
const config = defineConfig({ rootDir: fixtureDir });
const result = await analyze(config);
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("internal-tools/tui.ts"),
`tui.ts is referenced by parent monorepo script and must not be flagged unused, got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.includes("internal-tools/renderer.ts"),
`renderer.ts is transitively reachable from parent monorepo script entry, got: ${unusedFilePaths}`,
);
});
});
describe("workspace-subpath-import", () => {
it("should treat files imported by sibling workspaces via package subpaths as entry points", async () => {
const fixtureDir = resolve(FIXTURES_DIR, "workspace-subpath-import", "packages", "ui");
const config = defineConfig({ rootDir: fixtureDir });
const result = await analyze(config);
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("button.tsx"),
`button.tsx is imported by a sibling workspace via @subpath-fixture/ui/button and must not be flagged, got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.includes("utils.ts"),
`utils.ts is transitively reachable from button.tsx, got: ${unusedFilePaths}`,
);
assert.ok(
unusedFilePaths.includes("orphan.ts"),
`orphan.ts is not imported anywhere and should still be flagged, got: ${unusedFilePaths}`,
);
});
});
describe("workspace-subpath-import-built", () => {
it("should map built dist subpath targets back to source files for sibling workspace imports", async () => {
const fixtureDir = resolve(FIXTURES_DIR, "workspace-subpath-import-built", "packages", "ui");
const config = defineConfig({ rootDir: fixtureDir });
const result = await analyze(config);
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/button.ts"),
`src/button.ts must not be flagged even when the built dist artifact exists on disk, got: ${unusedFilePaths}`,
);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`src/orphan.ts is not imported anywhere and should still be flagged, got: ${unusedFilePaths}`,
);
});
});
describe("workspace-subpath-wildcard-export", () => {
it("should resolve sibling workspace subpath imports through wildcard exports patterns", async () => {
const fixtureDir = resolve(FIXTURES_DIR, "workspace-subpath-wildcard-export", "packages", "ui");
const config = defineConfig({ rootDir: fixtureDir });
const result = await analyze(config);
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/components/button.tsx"),
`src/components/button.tsx is imported via the "./*" exports pattern (dist target mapped back to src) and must not be flagged, got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.includes("src/components/helpers.ts"),
`src/components/helpers.ts is transitively reachable from button.tsx, got: ${unusedFilePaths}`,
);
assert.ok(
unusedFilePaths.includes("src/components/orphan.ts"),
`src/components/orphan.ts is not imported anywhere and should still be flagged, got: ${unusedFilePaths}`,
);
});
});
describe("vercel-config-app", () => {
it("should not flag vercel.ts as an unused file", async () => {
const result = await scanFixture("vercel-config-app");
const fixtureDir = resolve(FIXTURES_DIR, "vercel-config-app");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("vercel.ts"),
`vercel.ts is a deploy-time config file and must not be flagged, got: ${unusedFilePaths}`,
);
});
});
describe("duplicate-import-type-value", () => {
it("should not flag a type-only import + a value import of the same module as duplicates", async () => {
const result = await scanFixture("duplicate-import-type-value");
const fixtureDir = resolve(FIXTURES_DIR, "duplicate-import-type-value");
const dupes = result.duplicateImports.filter(
(dup) => dup.path === resolve(fixtureDir, "src/consumer-split.ts"),
);
const valueDupes = dupes.filter((dup) =>
dup.occurrences.every((occurrence) => !occurrence.isTypeOnly),
);
const typeDupes = dupes.filter((dup) =>
dup.occurrences.every((occurrence) => occurrence.isTypeOnly),
);
const mixedDupes = dupes.filter(
(dup) =>
dup.occurrences.some((occurrence) => occurrence.isTypeOnly) &&
dup.occurrences.some((occurrence) => !occurrence.isTypeOnly),
);
assert.strictEqual(
mixedDupes.length,
0,
`type-only + value imports must NOT be grouped together: ${JSON.stringify(mixedDupes)}`,
);
assert.strictEqual(
typeDupes.length,
0,
`single type-only import should not be flagged: ${JSON.stringify(typeDupes)}`,
);
assert.ok(
valueDupes.some((dup) => dup.specifier === "./api"),
`3 value-imports of "./api" SHOULD be flagged as duplicates: ${JSON.stringify(dupes)}`,
);
});
});
describe("jsx-block-arrow", () => {
it("should not flag arrow components returning JSX as block-arrow-single-return", async () => {
const result = await scanFixture("jsx-block-arrow");
const simplifiable = result.simplifiableFunctions.filter(
(item) => item.kind === "block-arrow-single-return",
);
const jsxNames = ["HrComponent", "FragmentComponent"];
for (const componentName of jsxNames) {
assert.ok(
!simplifiable.some((item) => item.functionName === componentName),
`JSX-returning arrow ${componentName} must not be flagged: ${JSON.stringify(simplifiable)}`,
);
}
assert.ok(
simplifiable.some((item) => item.functionName === "shouldFlagIdentity"),
`non-JSX single-return arrow shouldFlagIdentity SHOULD still be flagged: ${JSON.stringify(simplifiable)}`,
);
});
});
describe("filename-registry-entries", () => {
it("should treat unique filename string literals in source as soft entries (dynamic-loading pattern)", async () => {
const result = await scanFixture("filename-registry-entries");
const fixtureDir = resolve(FIXTURES_DIR, "filename-registry-entries");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("tools/diagnose-user.ts"),
`diagnose-user.ts is registered by basename string and must be treated as entry, got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.includes("tools/export-data.ts"),
`export-data.ts is registered by basename string and must be treated as entry, got: ${unusedFilePaths}`,
);
assert.ok(
unusedFilePaths.includes("tools/genuinely-dead.ts"),
`genuinely-dead.ts has no string-literal references and SHOULD still be flagged, got: ${unusedFilePaths}`,
);
});
});
describe("expo-config-plugins", () => {
it("should treat local Expo config plugins as entry points", async () => {
const result = await scanFixture("expo-config-plugins");
const fixtureDir = resolve(FIXTURES_DIR, "expo-config-plugins");
const unusedFilePaths = orphanPaths(result, fixtureDir);
for (const expectedReachableFile of [
// A plain template string in app.config.ts.
"plugins/template-literal-plugin.ts",
// A tuple entry that points at a directory with index.ts.
"plugins/directory-index-plugin/index.ts",
// An extensionless local path in expo.plugins.
"plugins/expo-json-extensionless-plugin.ts",
// A root-level plugins array in app.json.
"plugins/root-json-plugin.ts",
// A workspace app.config.js can point at a plugin outside its package.
"apps/shared/cross-workspace-plugin.ts",
]) {
assert.ok(
!unusedFilePaths.includes(expectedReachableFile),
`${expectedReachableFile} is referenced by Expo config plugins and must not be flagged unused, got: ${unusedFilePaths}`,
);
}
assert.ok(
unusedFilePaths.includes("expo-camera.ts"),
`package-name lookalikes must not be treated as local plugin files, got: ${unusedFilePaths}`,
);
assert.ok(
unusedFilePaths.includes("plugins/false-positive-target.ts"),
`nested non-Expo plugin arrays, dynamic tuple entries, absolute paths, and wildcard paths must not mark false-positive-target.ts reachable, got: ${unusedFilePaths}`,
);
});
});
describe("nested-dist-non-workspace", () => {
it("should exclude `dist/` directories at ANY depth, not just at workspace roots", async () => {
const result = await scanFixture("nested-dist-non-workspace");
const fixtureDir = resolve(FIXTURES_DIR, "nested-dist-non-workspace");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("apps/orphan/dist/index.mjs"),
`apps/orphan/dist/ must be globally excluded (no package.json so dir isn't a workspace), got: ${unusedFilePaths}`,
);
});
});
describe("empty-and-binary-files", () => {
it("should not flag minified/binary files as unusedFiles (parser can't see their imports)", async () => {
const result = await scanFixture("empty-and-binary-files");
const fixtureDir = resolve(FIXTURES_DIR, "empty-and-binary-files");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/minified-bundle.js"),
`minified bundle must not be in unusedFiles (analysisError already signals it), got: ${unusedFilePaths}`,
);
assert.ok(
!unusedFilePaths.includes("src/binary-file.ts"),
`binary file must not be in unusedFiles, got: ${unusedFilePaths}`,
);
});
});
describe("reexport-star", () => {
it("should not flag foo as unused (used via barrel)", async () => {
const result = await scanFixture("reexport-star");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("foo"), "foo is used through barrel");
});
it("should flag fooUnused as unused", async () => {
const result = await scanFixture("reexport-star");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("fooUnused"),
`fooUnused should be unused, got: ${allUnusedNames}`,
);
});
it("should not flag module-b.ts as unused file (file-level: re-exported by barrel makes it reachable)", async () => {
const result = await scanFixture("reexport-star");
const fixtureDir = resolve(FIXTURES_DIR, "reexport-star");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.some((filePath) => filePath === "src/module-b.ts"),
`module-b.ts should be reachable via barrel re-export (file-level), got: ${unusedFilePaths}`,
);
});
it("should not flag module-c.ts as unused file (file-level: star re-exported by barrel makes it reachable)", async () => {
const result = await scanFixture("reexport-star");
const fixtureDir = resolve(FIXTURES_DIR, "reexport-star");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.some((filePath) => filePath === "src/module-c.ts"),
`module-c.ts should be reachable via barrel star re-export (file-level), got: ${unusedFilePaths}`,
);
});
});
describe("reexport-chains (3-level barrel chain)", () => {
it("should not flag alpha and beta (used via 3-level chain)", async () => {
const result = await scanFixture("reexport-chains");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("alpha"), "alpha is used via chain");
assert.ok(!allUnusedNames.includes("beta"), "beta is used via chain");
});
it("should flag gamma and delta as unused", async () => {
const result = await scanFixture("reexport-chains");
const allUnusedNames = deadExportNames(result);
assert.ok(allUnusedNames.includes("gamma"), `gamma should be unused, got: ${allUnusedNames}`);
assert.ok(allUnusedNames.includes("delta"), `delta should be unused, got: ${allUnusedNames}`);
});
it("should not flag any file as unused", async () => {
const result = await scanFixture("reexport-chains");
assert.equal(result.unusedFiles.length, 0, "all files are reachable via chain");
});
});
describe("ns-imports", () => {
it("should flag exports not accessed via namespace member access", async () => {
const result = await scanFixture("ns-imports");
const fixtureDir = resolve(FIXTURES_DIR, "ns-imports");
const exportsByFile = deadExportsByFile(result, fixtureDir);
assert.deepStrictEqual(exportsByFile["src/utils.ts"], ["bar", "baz"]);
});
it("should not flag any files as unused", async () => {
const result = await scanFixture("ns-imports");
assert.equal(result.unusedFiles.length, 0);
});
});
describe("ns-partial", () => {
it("should flag only the exports not accessed via member access", async () => {
const result = await scanFixture("ns-partial");
const fixtureDir = resolve(FIXTURES_DIR, "ns-partial");
const exportsByFile = deadExportsByFile(result, fixtureDir);
const unusedMathExports = (exportsByFile["src/math.ts"] ?? []).sort();
assert.deepStrictEqual(unusedMathExports, ["divide", "subtract"]);
});
});
describe("ns-whole", () => {
it("should not flag any exports when Object.values is used on namespace", async () => {
const result = await scanFixture("ns-whole");
assert.equal(
result.unusedExports.length,
0,
`expected 0 unused exports, got: ${deadExportNames(result)}`,
);
});
});
describe("ns-spread", () => {
it("should not flag any exports when namespace is spread into object", async () => {
const result = await scanFixture("ns-spread");
assert.equal(
result.unusedExports.length,
0,
`expected 0 unused exports, got: ${deadExportNames(result)}`,
);
});
});
describe("ns-forin", () => {
it("should not flag any exports when namespace is used in for..in", async () => {
const result = await scanFixture("ns-forin");
assert.equal(
result.unusedExports.length,
0,
`expected 0 unused exports, got: ${deadExportNames(result)}`,
);
});
});
describe("ns-reexport", () => {
it("should flag only the exports not accessed through barrel via namespace member access", async () => {
const result = await scanFixture("ns-reexport");
const fixtureDir = resolve(FIXTURES_DIR, "ns-reexport");
const exportsByFile = deadExportsByFile(result, fixtureDir);
assert.deepStrictEqual(exportsByFile["src/lib/helpers.ts"], ["helperB", "helperC"]);
});
it("should not flag any files as unused", async () => {
const result = await scanFixture("ns-reexport");
assert.equal(result.unusedFiles.length, 0);
});
});
describe("export-default", () => {
it("should flag default export of component.ts (only named is used)", async () => {
const result = await scanFixture("export-default");
const fixtureDir = resolve(FIXTURES_DIR, "export-default");
const exportsByFile = deadExportsByFile(result, fixtureDir);
assert.ok(
exportsByFile["src/component.ts"]?.includes("default"),
`default should be unused in component.ts, got: ${JSON.stringify(exportsByFile)}`,
);
});
it("should flag unused-default.ts as unused file", async () => {
const result = await scanFixture("export-default");
const fixtureDir = resolve(FIXTURES_DIR, "export-default");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/unused-default.ts"),
`unused-default.ts should be unused file, got: ${unusedFilePaths}`,
);
});
it("should not flag usedNamed as unused", async () => {
const result = await scanFixture("export-default");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("usedNamed"), "usedNamed is imported");
});
});
describe("import-side-effect", () => {
it("should keep setup.ts reachable (side-effect import)", async () => {
const result = await scanFixture("import-side-effect");
const fixtureDir = resolve(FIXTURES_DIR, "import-side-effect");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(!unusedFilePaths.includes("src/setup.ts"), "setup.ts is side-effect imported");
});
it("should flag orphan.ts as unused", async () => {
const result = await scanFixture("import-side-effect");
const fixtureDir = resolve(FIXTURES_DIR, "import-side-effect");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`orphan.ts should be unused, got: ${unusedFilePaths}`,
);
});
});
describe("cycle-reexport", () => {
it("should not flag fromA or fromB (used despite circular re-exports)", async () => {
const result = await scanFixture("cycle-reexport");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("fromA"), "fromA is used");
assert.ok(!allUnusedNames.includes("fromB"), "fromB is used");
});
it("should not hang or crash from circular re-export", async () => {
const result = await scanFixture("cycle-reexport");
assert.ok(result.totalFiles > 0, "analysis should complete");
});
});
describe("star-reexport-chain", () => {
it("should not flag used as unused (via star re-export chain)", async () => {
const result = await scanFixture("star-reexport-chain");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("used"), "used should be found through star chain");
});
it("should flag unused export in source", async () => {
const result = await scanFixture("star-reexport-chain");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("unused"),
`unused should be flagged, got: ${allUnusedNames}`,
);
});
});
describe("star-selective", () => {
it("should not flag usedOne and usedTwo (selectively imported via star barrel)", async () => {
const result = await scanFixture("star-selective");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("usedOne"), "usedOne is used");
assert.ok(!allUnusedNames.includes("usedTwo"), "usedTwo is used");
});
it("should flag unusedThree and unusedFour", async () => {
const result = await scanFixture("star-selective");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("unusedThree"),
`unusedThree should be flagged, got: ${allUnusedNames}`,
);
assert.ok(
allUnusedNames.includes("unusedFour"),
`unusedFour should be flagged, got: ${allUnusedNames}`,
);
});
});
describe("reexport-multi-hop", () => {
it("should not flag used (imported through two barrel hops)", async () => {
const result = await scanFixture("reexport-multi-hop");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("used"), "used is consumed through 2-hop barrel");
});
it("should flag unused1 and unused2", async () => {
const result = await scanFixture("reexport-multi-hop");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("unused1"),
`unused1 should be unused, got: ${allUnusedNames}`,
);
assert.ok(
allUnusedNames.includes("unused2"),
`unused2 should be unused, got: ${allUnusedNames}`,
);
});
});
describe("reexport-multi-level", () => {
it("should not flag alpha and beta (used through 3-level named re-export chain)", async () => {
const result = await scanFixture("reexport-multi-level");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("alpha"), "alpha is used");
assert.ok(!allUnusedNames.includes("beta"), "beta is used");
});
it("should flag gamma (re-exported in barrel-a but not imported)", async () => {
const result = await scanFixture("reexport-multi-level");
const allUnusedNames = deadExportNames(result);
assert.ok(allUnusedNames.includes("gamma"), `gamma should be unused, got: ${allUnusedNames}`);
});
it("should flag delta (only in barrel-b, not re-exported by barrel-a)", async () => {
const result = await scanFixture("reexport-multi-level");
const allUnusedNames = deadExportNames(result);
assert.ok(allUnusedNames.includes("delta"), `delta should be unused, got: ${allUnusedNames}`);
});
it("should flag epsilon (not re-exported at all)", async () => {
const result = await scanFixture("reexport-multi-level");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("epsilon"),
`epsilon should be unused, got: ${allUnusedNames}`,
);
});
});
describe("reexport-default", () => {
it("should not flag Button (used via default re-export through barrel)", async () => {
const result = await scanFixture("reexport-default");
const fixtureDir = resolve(FIXTURES_DIR, "reexport-default");
const exportsByFile = deadExportsByFile(result, fixtureDir);
const buttonExports = exportsByFile["src/components/Button.ts"];
assert.ok(!buttonExports?.includes("default"), "Button default export is used");
});
});
describe("reexport-unused", () => {
it("should not flag UsedComponent", async () => {
const result = await scanFixture("reexport-unused");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("UsedComponent"), "UsedComponent is imported");
});
it("should not flag unused-source.ts as unused file (file-level: barrel re-export makes it reachable)", async () => {
const result = await scanFixture("reexport-unused");
const fixtureDir = resolve(FIXTURES_DIR, "reexport-unused");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.some((filePath) => filePath === "src/components/unused-source.ts"),
`unused-source.ts should be reachable via barrel re-export (file-level), got: ${unusedFilePaths}`,
);
});
});
describe("deep-reexport-tracking", () => {
it("should keep used-source.ts reachable (usedHelper consumed through two barrel layers)", async () => {
const result = await scanFixture("deep-reexport-tracking");
const fixtureDir = resolve(FIXTURES_DIR, "deep-reexport-tracking");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/used-source.ts"),
"used-source.ts should be reachable via barrel-mid → barrel-top → index",
);
});
it("should not flag unused-source.ts as unused file (file-level: barrel re-export chain makes it reachable)", async () => {
const result = await scanFixture("deep-reexport-tracking");
const fixtureDir = resolve(FIXTURES_DIR, "deep-reexport-tracking");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/unused-source.ts"),
`unused-source.ts should be reachable via barrel re-export chain (file-level), got: ${unusedFilePaths}`,
);
});
it("should flag orphan.ts as unused file", async () => {
const result = await scanFixture("deep-reexport-tracking");
const fixtureDir = resolve(FIXTURES_DIR, "deep-reexport-tracking");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`orphan.ts should be unused, got: ${unusedFilePaths}`,
);
});
it("should flag usedHelperSibling as unused export", async () => {
const result = await scanFixture("deep-reexport-tracking");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("usedHelperSibling"),
`usedHelperSibling should be unused, got: ${allUnusedNames}`,
);
});
});
describe("wildcard-late-consume", () => {
it("should keep color-picker reachable when consumed via plugin that imports from sibling component barrel", async () => {
const result = await scanFixture("wildcard-late-consume");
const fixtureDir = resolve(FIXTURES_DIR, "wildcard-late-consume");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/components/color-picker/color-picker.ts"),
`color-picker.ts should be reachable via plugin → components barrel → color-picker barrel, got unused: ${unusedFilePaths}`,
);
});
it("should keep color-picker/index.ts reachable as intermediate barrel", async () => {
const result = await scanFixture("wildcard-late-consume");
const fixtureDir = resolve(FIXTURES_DIR, "wildcard-late-consume");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/components/color-picker/index.ts"),
`color-picker/index.ts should be reachable, got unused: ${unusedFilePaths}`,
);
});
it("should flag unused-widget.ts as unused file (never imported by any plugin)", async () => {
const result = await scanFixture("wildcard-late-consume");
const fixtureDir = resolve(FIXTURES_DIR, "wildcard-late-consume");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/components/unused-widget.ts"),
`unused-widget.ts should be unused, got: ${unusedFilePaths}`,
);
});
it("should flag ColorUtils as unused export (only ColorPicker consumed from color-picker.ts)", async () => {
const result = await scanFixture("wildcard-late-consume");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("ColorUtils"),
`ColorUtils should be unused export, got: ${allUnusedNames}`,
);
});
});
describe("import-reexport-same", () => {
it("should create both direct import and re-export edges when a file imports from and re-exports the same module", async () => {
const result = await scanFixture("import-reexport-same");
const fixtureDir = resolve(FIXTURES_DIR, "import-reexport-same");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/components/widget.ts"),
`widget.ts should be reachable via re-export through components barrel (export * from), got unused: ${unusedFilePaths}`,
);
});
it("should keep helper.ts reachable via both direct import and re-export", async () => {
const result = await scanFixture("import-reexport-same");
const fixtureDir = resolve(FIXTURES_DIR, "import-reexport-same");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
!unusedFilePaths.includes("src/components/helper.ts"),
`helper.ts should be reachable, got unused: ${unusedFilePaths}`,
);
});
it("should flag orphan.ts as unused (not imported or re-exported by anyone)", async () => {
const result = await scanFixture("import-reexport-same");
const fixtureDir = resolve(FIXTURES_DIR, "import-reexport-same");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`orphan.ts should be unused, got: ${unusedFilePaths}`,
);
});
});
describe("reexport-alias", () => {
it("should not flag original and renamed (used via aliased re-export chain)", async () => {
const result = await scanFixture("reexport-alias");
const allUnusedNames = deadExportNames(result);
assert.ok(!allUnusedNames.includes("original"), "original is used via aliasC");
assert.ok(!allUnusedNames.includes("renamed"), "renamed is used via doubleAlias");
});
it("should flag unusedOriginal (aliased but never consumed)", async () => {
const result = await scanFixture("reexport-alias");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("unusedOriginal"),
`unusedOriginal should be unused, got: ${allUnusedNames}`,
);
});
it("should flag neverExported (not re-exported by any barrel)", async () => {
const result = await scanFixture("reexport-alias");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("neverExported"),
`neverExported should be unused, got: ${allUnusedNames}`,
);
});
});
describe("import-dynamic", () => {
it("should keep lazy.ts reachable via dynamic import", async () => {
const result = await scanFixture("import-dynamic");
const fixtureDir = resolve(FIXTURES_DIR, "import-dynamic");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(!unusedFilePaths.includes("src/lazy.ts"), "lazy.ts is dynamically imported");
});
it("should flag orphan.ts as unused file", async () => {
const result = await scanFixture("import-dynamic");
const fixtureDir = resolve(FIXTURES_DIR, "import-dynamic");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/orphan.ts"),
`orphan.ts should be unused, got: ${unusedFilePaths}`,
);
});
it("should flag unused export in utils", async () => {
const result = await scanFixture("import-dynamic");
const allUnusedNames = deadExportNames(result);
assert.ok(
allUnusedNames.includes("unused"),
`unused should be flagged, got: ${allUnusedNames}`,
);
});
});
describe("type-deps", () => {
it("should detect type-only imports", async () => {
const result = await scanFixture("type-deps");
const deps = staleDependencyNames(result);
assert.ok(!deps.includes("express"), `express is imported as a value, got: ${deps}`);
assert.ok(!deps.includes("zod"), `zod is imported as a type, got: ${deps}`);
});
});
describe("orphan-barrel-subtree", () => {
it("should flag all files in the dead subtree", async () => {
const result = await scanFixture("orphan-barrel-subtree");
const fixtureDir = resolve(FIXTURES_DIR, "orphan-barrel-subtree");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.deepEqual(
unusedFilePaths.filter((filePath) => filePath.startsWith("src/subtree/")),
["src/subtree/setup.ts", "src/subtree/tabs/helpers.ts", "src/subtree/tabs/index.ts"],
);
});
});
describe("orphan-mixed-exports", () => {
it("should flag both files in unreachable test-utils", async () => {
const result = await scanFixture("orphan-mixed-exports");
const fixtureDir = resolve(FIXTURES_DIR, "orphan-mixed-exports");
const unusedFilePaths = orphanPaths(result, fixtureDir);
assert.ok(
unusedFilePaths.includes("src/test-utils/helpers.ts"),