-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_scanner.py
More file actions
743 lines (595 loc) · 27.1 KB
/
Copy pathtest_scanner.py
File metadata and controls
743 lines (595 loc) · 27.1 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
"""Tests for DeadCode scanner and CLI."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from deadcode.cli import cli
from deadcode.scanner import DeadCodeScanner
@pytest.fixture
def runner():
from click.testing import CliRunner
return CliRunner()
@pytest.fixture
def sample_project(tmp_path):
"""Create a sample TS/React project structure."""
# src/utils.ts - with unused export
utils = tmp_path / "src" / "utils.ts"
utils.parent.mkdir(parents=True, exist_ok=True)
utils.write_text(
"export function usedHelper() { return 1; }\n"
"export function unusedHelper() { return 2; }\n"
'export const USED_CONST = "used";\n'
'export const UNUSED_CONST = "unused";\n'
)
# src/components/Button.tsx - component that imports from utils
button = tmp_path / "src" / "components" / "Button.tsx"
button.parent.mkdir(parents=True, exist_ok=True)
button.write_text(
'import { usedHelper, USED_CONST } from "../utils";\n'
"export function Button() {\n"
' return <button className="btn-primary">{usedHelper()}</button>;\n'
"}\n"
)
# src/components/UnusedWidget.tsx - never imported
widget = tmp_path / "src" / "components" / "UnusedWidget.tsx"
widget.write_text(
"export function UnusedWidget() {\n return <div>Unused</div>;\n}\n"
)
# src/styles/main.css - with orphaned class
css = tmp_path / "src" / "styles" / "main.css"
css.parent.mkdir(parents=True, exist_ok=True)
css.write_text(
".btn-primary {\n background: blue;\n}\n.orphaned-class {\n color: red;\n}\n"
)
# src/app/page.tsx - Next.js page (entry point)
page = tmp_path / "src" / "app" / "page.tsx"
page.parent.mkdir(parents=True, exist_ok=True)
page.write_text(
'import { Button } from "../components/Button";\n'
"export default function Page() {\n"
" return <Button />;\n"
"}\n"
)
# src/app/deadpage/page.tsx - dead route
deadpage = tmp_path / "src" / "app" / "deadpage" / "page.tsx"
deadpage.parent.mkdir(parents=True, exist_ok=True)
deadpage.write_text(
"export default function DeadPage() {\n return <div>Dead</div>;\n}\n"
)
return tmp_path
class TestScanner:
def test_scan_finds_unused_exports(self, sample_project):
scanner = DeadCodeScanner(sample_project)
result = scanner.scan()
unused_names = {f.name for f in result.unused_exports}
assert "unusedHelper" in unused_names
assert "UNUSED_CONST" in unused_names
assert "usedHelper" not in unused_names
assert "USED_CONST" not in unused_names
def test_scan_finds_orphaned_css(self, sample_project):
scanner = DeadCodeScanner(sample_project)
result = scanner.scan()
orphaned_names = {f.name for f in result.orphaned_css}
assert "orphaned-class" in orphaned_names
assert "btn-primary" not in orphaned_names
def test_scan_finds_unreferenced_components(self, sample_project):
scanner = DeadCodeScanner(sample_project)
result = scanner.scan()
comp_names = {f.name for f in result.unreferenced_components}
assert "UnusedWidget" in comp_names
assert "Button" not in comp_names
def test_scan_finds_dead_routes(self, sample_project):
scanner = DeadCodeScanner(sample_project)
result = scanner.scan()
route_names = {f.name for f in result.dead_routes}
assert "/deadpage" in route_names
def test_scan_files_counted(self, sample_project):
scanner = DeadCodeScanner(sample_project)
result = scanner.scan()
assert result.files_scanned > 0
def test_empty_project(self, tmp_path):
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
assert result.files_scanned == 0
assert len(result.findings) == 0
def test_ignore_patterns(self, sample_project):
scanner = DeadCodeScanner(sample_project, ignore_patterns=["src/styles/"])
result = scanner.scan()
# Orphaned CSS should not appear since we're ignoring styles dir
assert len(result.orphaned_css) == 0
def test_scan_result_properties(self, sample_project):
scanner = DeadCodeScanner(sample_project)
result = scanner.scan()
# Verify all findings are categorized
for f in result.findings:
assert f.category in (
"unused_export",
"dead_route",
"orphaned_css",
"unreferenced_component",
)
class TestExportParsing:
def test_named_exports(self, tmp_path):
f = tmp_path / "test.ts"
f.write_text(
"export function foo() {}\n"
"export const bar = 1;\n"
"export type Baz = string;\n"
"export interface Qux {}\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
assert "foo" in export_names
assert "bar" in export_names
assert "Baz" in export_names
assert "Qux" in export_names
def test_export_list(self, tmp_path):
f = tmp_path / "test.ts"
f.write_text("const alpha = 1;\nconst beta = 2;\nexport { alpha, beta };\n")
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
assert "alpha" in export_names
assert "beta" in export_names
def test_used_exports_not_reported(self, tmp_path):
mod = tmp_path / "mod.ts"
mod.write_text("export function myFunc() { return 1; }\n")
app = tmp_path / "app.ts"
app.write_text('import { myFunc } from "./mod";\nmyFunc();\n')
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
unused_names = {f.name for f in result.unused_exports}
assert "myFunc" not in unused_names
def test_type_import_counts_as_used(self, tmp_path):
"""import type { Foo } should mark Foo as used."""
mod = tmp_path / "mod.ts"
mod.write_text('export type Foo = string;\n')
app = tmp_path / "app.ts"
app.write_text('import type { Foo } from "./mod";\nconst x: Foo = "hello";\n')
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
unused_names = {f.name for f in result.unused_exports}
assert "Foo" not in unused_names
def test_mixed_default_and_named_import_counts_as_used(self, tmp_path):
"""Default + named should mark both as used."""
mod = tmp_path / "mod.ts"
mod.write_text('export function myFunc() { return 1; }\n')
app = tmp_path / "app.ts"
app.write_text('import Default, { myFunc } from "./mod";\nmyFunc();\n')
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
unused_names = {f.name for f in result.unused_exports}
assert "myFunc" not in unused_names
assert "Default" not in unused_names
def test_type_only_import_marks_as_used(self, tmp_path):
"""`import { type Foo } from ...` should mark Foo as used."""
mod = tmp_path / "mod.ts"
mod.write_text('export type Foo = string;\n')
app = tmp_path / "app.ts"
app.write_text('import { type Foo } from "./mod";\nconst x: Foo = "hi";\n')
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
unused_names = {f.name for f in result.unused_exports}
assert "Foo" not in unused_names
def test_mixed_default_and_type_only_import_marks_as_used(self, tmp_path):
"""`import Default, { type Foo } from ...` should mark Foo as used."""
mod = tmp_path / "mod.ts"
mod.write_text('export default function Default() { return 1; }\nexport type Foo = string;\n')
app = tmp_path / "app.ts"
app.write_text('import Default, { type Foo } from "./mod";\nconst x: Foo = "hi";\n')
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
unused_names = {f.name for f in result.unused_exports}
assert "Default" not in unused_names
assert "Foo" not in unused_names
class TestCSSParsing:
def test_orphaned_css_detection(self, tmp_path):
css = tmp_path / "styles.css"
css.write_text(
".used-class { color: blue; }\n.orphaned-class { color: red; }\n"
)
component = tmp_path / "Component.tsx"
component.write_text(
"export function Component() {\n"
' return <div className="used-class">Hi</div>;\n'
"}\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
orphaned = {f.name for f in result.orphaned_css}
assert "orphaned-class" in orphaned
assert "used-class" not in orphaned
class TestRouteDetection:
def test_nextjs_app_router_route(self, tmp_path):
page = tmp_path / "app" / "about" / "page.tsx"
page.parent.mkdir(parents=True, exist_ok=True)
page.write_text(
"export default function About() { return <div>About</div>; }\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
route_names = {f.name for f in result.dead_routes}
assert "/about" in route_names
def test_root_route_not_dead(self, tmp_path):
page = tmp_path / "app" / "page.tsx"
page.parent.mkdir(parents=True, exist_ok=True)
page.write_text("export default function Home() { return <div>Home</div>; }\n")
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
# Root route should not be reported as dead
route_names = {f.name for f in result.dead_routes}
assert "/" not in route_names
def test_linked_route_not_dead(self, tmp_path):
page = tmp_path / "app" / "page.tsx"
page.parent.mkdir(parents=True, exist_ok=True)
page.write_text(
'export default function Home() { return <a href="/about">About</a>; }\n'
)
about = tmp_path / "app" / "about" / "page.tsx"
about.parent.mkdir(parents=True, exist_ok=True)
about.write_text(
"export default function About() { return <div>About</div>; }\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
route_names = {f.name for f in result.dead_routes}
assert "/about" not in route_names
class TestCLIIntegration:
def test_version(self, runner):
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0
assert "0.1.1" in result.output
def test_help(self, runner):
result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "scan" in result.output
assert "remove" in result.output
assert "stats" in result.output
def test_scan_command(self, runner, sample_project):
result = runner.invoke(cli, ["-p", str(sample_project), "scan"])
assert result.exit_code == 0
assert "DeadCode Scan" in result.output
def test_scan_json_output(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--json-output"]
)
assert result.exit_code == 0
data = json.loads(result.output, strict=False)
assert "findings" in data
assert "files_scanned" in data
def test_scan_category_filter(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "-c", "orphaned_css"]
)
assert result.exit_code == 0
# Text output should mention the category
assert "Orphaned CSS" in result.output
def test_scan_nonexistent_dir(self, runner):
result = runner.invoke(cli, ["-p", "/nonexistent/path", "scan"])
assert result.exit_code != 0
def test_remove_dry_run(self, runner, sample_project):
result = runner.invoke(cli, ["-p", str(sample_project), "remove", "--dry-run"])
assert result.exit_code == 0
assert (
"WOULD REMOVE" in result.output
or "Nothing removable" in result.output
or result.exit_code == 0
)
def test_stats_command(self, runner, sample_project):
result = runner.invoke(cli, ["-p", str(sample_project), "stats"])
assert result.exit_code == 0
assert "Files scanned" in result.output
assert "Unused exports" in result.output
def test_scan_ignore_option(self, runner, tmp_path):
"""--ignore option should exclude matching files from scan."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text("export function unusedFunc() { return 1; }\n")
result = runner.invoke(
cli, ["-p", str(tmp_path), "-i", "src/", "scan", "--json-output"]
)
assert result.exit_code == 0
import json
data = json.loads(result.output, strict=False)
assert data["files_scanned"] == 0, "src/ ignored, should have 0 files"
def test_remove_category_filter(self, runner, sample_project):
"""remove --dry-run --category should filter by category."""
result = runner.invoke(
cli,
["-p", str(sample_project), "remove", "--dry-run", "-c", "orphaned_css"],
)
assert result.exit_code == 0
# Should mention orphaned class
assert "orphaned-class" in result.output or "Nothing removable" in result.output
def test_remove_nonexistent_dir(self, runner):
"""remove should give graceful error for nonexistent project dir."""
result = runner.invoke(
cli, ["-p", "/nonexistent/test/path", "remove", "--dry-run"]
)
assert result.exit_code != 0
assert "not found" in result.output
def test_stats_nonexistent_dir(self, runner):
"""stats should handle nonexistent project dir gracefully."""
result = runner.invoke(cli, ["-p", "/nonexistent/stats/path", "stats"])
# Should not crash — scan returns 0 files for nonexistent dir
assert result.exit_code == 0
assert "Files scanned: 0" in result.output
def test_main_module_entry_point(self, runner):
"""Test that python -m deadcode works (__main__ entry point fix)."""
import subprocess
import sys
result = subprocess.run(
[sys.executable, "-m", "deadcode", "--help"],
capture_output=True,
text=True,
cwd=str(Path(__file__).parent.parent / "src"),
)
assert result.returncode == 0
assert "DeadCode" in result.stdout
assert "scan" in result.stdout
assert "remove" in result.stdout
assert "stats" in result.stdout
class TestMultiLineExportList:
"""Tests for multi-line export { } blocks (scanner.py fix: apply list pattern to full content)."""
def test_multiline_export_list_detected(self, tmp_path):
"""export { Foo, Bar } split across lines should be detected as unused exports."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text(
"function Alpha() { return 1; }\n"
"function Beta() { return 2; }\n"
"export {\n"
" Alpha,\n"
" Beta,\n"
"}\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
assert "Alpha" in export_names, "Multi-line export Alpha should be detected"
assert "Beta" in export_names, "Multi-line export Beta should be detected"
def test_multiline_export_list_used_not_reported(self, tmp_path):
"""Names from a multi-line export {} that are imported elsewhere should NOT be reported."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text(
"export function usedInApp() { return 1; }\n"
"export function alsoUnused() { return 2; }\n"
"export {\n"
" usedInApp,\n"
"}\n"
)
app = tmp_path / "src" / "app.ts"
app.write_text('import { usedInApp } from "./mod";\nusedInApp();\n')
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
# usedInApp appears in both an inline export and the export-list; it's imported so should be absent
assert "usedInApp" not in export_names, (
"usedInApp is imported — should not be reported"
)
assert "alsoUnused" in export_names, (
"alsoUnused is never imported — should be reported"
)
def test_multiline_export_list_with_aliases(self, tmp_path):
"""export { Foo as Bar } aliases: the local name Foo should be tracked, not the alias."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text(
"function InternalName() { return 1; }\n"
"export {\n"
" InternalName as PublicName,\n"
"}\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
# The scanner tracks the local (pre-alias) name
assert "InternalName" in export_names
# The alias 'PublicName' should not appear as a spurious finding
assert "PublicName" not in export_names
def test_single_line_export_list_still_works(self, tmp_path):
"""Single-line export { Foo, Bar } should continue to work after the fix."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text("const alpha = 1;\nconst beta = 2;\nexport { alpha, beta };\n")
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
assert "alpha" in export_names
assert "beta" in export_names
def test_export_list_with_inline_comments(self, tmp_path):
"""Inline // comments inside export lists should not mask other exports."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text(
"function Alpha() { return 1; }\n"
"function Beta() { return 2; }\n"
"export {\n"
" Alpha, // kept for clarity\n"
" Beta,\n"
"}\n"
)
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
export_names = {f.name for f in result.unused_exports}
assert "Alpha" in export_names
assert "Beta" in export_names
class TestIncludePatterns:
"""Tests for the include_patterns scanner feature."""
def test_include_patterns_filters_files(self, tmp_path):
"""When include_patterns is set, only matching files should be scanned."""
# Create files in two dirs
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text("export function foo() { return 1; }\\n")
lib = tmp_path / "lib" / "helper.ts"
lib.parent.mkdir(parents=True, exist_ok=True)
lib.write_text("export function bar() { return 2; }\\n")
scanner = DeadCodeScanner(tmp_path, include_patterns=["src/"])
result = scanner.scan()
# Only src/mod.ts should be scanned, not lib/helper.ts
assert result.files_scanned == 1
def test_include_patterns_allows_multiple(self, tmp_path):
"""include_patterns can specify multiple directories."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text("export function foo() { return 1; }\\n")
lib = tmp_path / "lib" / "helper.ts"
lib.parent.mkdir(parents=True, exist_ok=True)
lib.write_text("export function bar() { return 2; }\\n")
scanner = DeadCodeScanner(tmp_path, include_patterns=["src/", "lib/"])
result = scanner.scan()
assert result.files_scanned == 2
def test_include_patterns_none_scans_all(self, tmp_path):
"""When include_patterns is None, all scannable files are included."""
mod = tmp_path / "src" / "mod.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text("export function foo() { return 1; }\\n")
lib = tmp_path / "lib" / "helper.ts"
lib.parent.mkdir(parents=True, exist_ok=True)
lib.write_text("export function bar() { return 2; }\\n")
scanner = DeadCodeScanner(tmp_path)
result = scanner.scan()
assert result.files_scanned == 2
class TestScanFormat:
"""Tests for the new --format scan option."""
def test_format_compact(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--format=compact"]
)
assert result.exit_code == 0
def test_format_github(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--format=github"]
)
assert result.exit_code == 0
def test_format_compact_with_findings(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--format=compact"]
)
assert result.exit_code == 0
assert "unusedHelper" in result.output or "No dead code" in result.output
def test_format_github_with_findings(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--format=github"]
)
assert result.exit_code == 0
assert "::" in result.output or "No dead code" in result.output
def test_format_json_legacy_alias(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--json-output"]
)
assert result.exit_code == 0
data = json.loads(result.output, strict=False)
assert "findings" in data
def test_format_json_explicit(self, runner, sample_project):
result = runner.invoke(
cli, ["-p", str(sample_project), "scan", "--format=json"]
)
assert result.exit_code == 0
data = json.loads(result.output, strict=False)
assert "findings" in data
def test_format_compact_empty(self, runner, tmp_path):
result = runner.invoke(cli, ["-p", str(tmp_path), "scan", "--format=compact"])
assert result.exit_code == 0
assert "OK \u2014 0 findings" in result.output
def test_format_github_empty(self, runner, tmp_path):
result = runner.invoke(cli, ["-p", str(tmp_path), "scan", "--format=github"])
assert result.exit_code == 0
assert "deadcode: 0 findings" in result.output
def test_format_pretty_default(self, runner, sample_project):
result = runner.invoke(cli, ["-p", str(sample_project), "scan"])
assert result.exit_code == 0
assert "DeadCode Scan" in result.output
class TestReexportForwarding:
"""Re-exports (barrel/index files) must count the forwarded symbols as used.
A dead-code tool that flags re-exported symbols as removable is dangerous:
removing them breaks the barrel's public API. These tests pin the behaviour.
"""
def test_named_reexport_marks_source_as_used(self, tmp_path):
src = tmp_path / "foo.ts"
src.write_text('export function helper() { return 1; }\n')
index = tmp_path / "index.ts"
index.write_text('export { helper } from "./foo";\n')
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "helper" not in unused
def test_renamed_reexport_marks_source_as_used(self, tmp_path):
src = tmp_path / "foo.ts"
src.write_text('export function helper() { return 1; }\n')
index = tmp_path / "index.ts"
index.write_text('export { helper as primaryHelper } from "./foo";\n')
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "helper" not in unused
def test_type_named_reexport_marks_source_as_used(self, tmp_path):
src = tmp_path / "types.ts"
src.write_text('export type Foo = string;\n')
index = tmp_path / "index.ts"
index.write_text('export { type Foo } from "./types";\n')
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "Foo" not in unused
def test_multiline_named_reexport(self, tmp_path):
src = tmp_path / "foo.ts"
src.write_text(
'export const alpha = 1;\n'
'export const beta = 2;\n'
)
index = tmp_path / "index.ts"
index.write_text(
'export {\n'
' alpha,\n'
' beta,\n'
'} from "./foo";\n'
)
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "alpha" not in unused
assert "beta" not in unused
def test_star_reexport_marks_all_source_exports_as_used(self, tmp_path):
src = tmp_path / "widgets.ts"
src.write_text(
'export function widgetA() {}\n'
'export function widgetB() {}\n'
)
index = tmp_path / "index.ts"
index.write_text('export * from "./widgets";\n')
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "widgetA" not in unused
assert "widgetB" not in unused
def test_star_reexport_from_directory_index(self, tmp_path):
mod = tmp_path / "widgets" / "index.ts"
mod.parent.mkdir(parents=True, exist_ok=True)
mod.write_text('export function widgetA() {}\n')
index = tmp_path / "index.ts"
index.write_text('export * from "./widgets";\n')
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "widgetA" not in unused
def test_local_export_list_still_reported_when_unused(self, tmp_path):
# Regression guard: a *local* `export { ... }` (no `from`) must still be
# tracked and flagged when unused — the re-export fix must not suppress it.
f = tmp_path / "test.ts"
f.write_text(
'const alpha = 1;\n'
'const beta = 2;\n'
'export { alpha, beta };\n'
)
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
assert "alpha" in unused
assert "beta" in unused
def test_reexport_from_package_does_not_crash_or_falsely_mark(self, tmp_path):
# Re-export from a bare package specifier must be ignored for resolution.
src = tmp_path / "foo.ts"
src.write_text('export function localOnly() {}\n')
index = tmp_path / "index.ts"
index.write_text('export { useState } from "react";\n')
result = DeadCodeScanner(tmp_path).scan()
unused = {f.name for f in result.unused_exports}
# The project-local export nobody consumes is still correctly flagged.
assert "localOnly" in unused