ts-unused 2.0.0 → 2.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 (2) hide show
  1. package/README.md +111 -7
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -25,7 +25,7 @@ A CLI tool that analyzes TypeScript projects to find unused exports and unused p
25
25
  ## Limitations
26
26
 
27
27
  - **Dynamic Access**: Properties accessed via computed property names (`obj[key]`) may not be detected
28
- - **Re-exports**: Items that are re-exported through barrel files are considered used
28
+ - **Re-exports**: By default, items that are only re-exported through barrel files are reported as unused. Use `packageMode` to treat public API exports as used.
29
29
  - **Type Narrowing**: Properties accessed after type guards or conditional checks may not always be tracked through complex control flow
30
30
 
31
31
  ## Comparison With Similar Tools
@@ -162,6 +162,10 @@ export default defineConfig({
162
162
  analyzeProperties: true,
163
163
  analyzeNeverReturnedTypes: true,
164
164
  detectUnusedFiles: true,
165
+
166
+ // Enable package mode for library development
167
+ // Treats exports re-exported through barrel files to package entry points as "used"
168
+ packageMode: true,
165
169
  });
166
170
  ```
167
171
 
@@ -179,6 +183,7 @@ export default defineConfig({
179
183
  | `analyzeProperties` | `boolean` | `true` | Enable/disable unused property detection |
180
184
  | `analyzeNeverReturnedTypes` | `boolean` | `true` | Enable/disable never-returned type detection |
181
185
  | `detectUnusedFiles` | `boolean` | `true` | Enable/disable completely unused file detection |
186
+ | `packageMode` | `boolean` | `false` | Treat exports re-exported to package entry points as used (for libraries) |
182
187
 
183
188
  ### Pattern Syntax
184
189
 
@@ -415,6 +420,49 @@ export function processData(): SuccessResult {
415
420
  }
416
421
  ```
417
422
 
423
+ ### Package Mode (Library Development)
424
+
425
+ When developing npm packages or libraries, you typically export a public API through barrel files (index.ts) that re-export from internal modules. By default, ts-unused would report these internal exports as "unused" since they're only re-exported, not directly used.
426
+
427
+ Enable `packageMode: true` to treat exports as "used" if they are part of your package's public API:
428
+
429
+ ```typescript
430
+ // unused.config.ts
431
+ import { defineConfig } from "ts-unused";
432
+
433
+ export default defineConfig({
434
+ packageMode: true,
435
+ });
436
+ ```
437
+
438
+ **How it works:**
439
+
440
+ 1. Finds `package.json` in your project directory
441
+ 2. Parses entry points from `main`, `module`, or `exports` fields
442
+ 3. Traces all re-exports from entry point files through barrel chains
443
+ 4. Marks exports that reach a package entry point as "used"
444
+
445
+ **Example project structure:**
446
+
447
+ ```
448
+ package.json # { "main": "./dist/index.js", "exports": { ".": "./dist/index.js" } }
449
+ src/
450
+ index.ts # export { formatDate } from "./utils";
451
+ utils.ts # export function formatDate() { ... } ← Considered USED
452
+ internal.ts # export function helper() { ... } ← Reported as UNUSED
453
+ ```
454
+
455
+ In this example:
456
+ - `formatDate` is NOT reported as unused because it's re-exported through `src/index.ts` (the package entry point)
457
+ - `helper` IS reported as unused because it's not part of the public API
458
+
459
+ **Supports:**
460
+ - Multiple entry points via `exports` field (e.g., `"."`, `"./utils"`)
461
+ - Nested barrel files (`src/utils/index.ts` → `src/index.ts`)
462
+ - `export *` statements
463
+ - Mixed named and star exports
464
+ - Types and interfaces
465
+
418
466
  ### Structural Property Equivalence
419
467
 
420
468
  The analyzer handles cases where properties are re-declared across multiple interfaces with the same name and type. This commonly occurs with:
@@ -575,7 +623,7 @@ const config: UnusedConfig = await loadConfig("./tsconfig.json");
575
623
  const configSync: UnusedConfig = loadConfigSync("./tsconfig.json");
576
624
 
577
625
  // Use with analyzeProject
578
- const results = an![alt text](image.png)alyzeProject("./tsconfig.json", undefined, undefined, { config });
626
+ const results = analyzeProject("./tsconfig.json", undefined, undefined, { config });
579
627
  ```
580
628
 
581
629
  #### `defineConfig(config)`
@@ -592,20 +640,53 @@ export default defineConfig({
592
640
  });
593
641
  ```
594
642
 
595
- #### `createIsTestFile(patterns)`
643
+ #### `mergeConfig(userConfig)`
596
644
 
597
- Creates a custom test file detection function from glob patterns.
645
+ Merges user configuration with default values. Useful when programmatically building config.
598
646
 
599
647
  ```typescript
600
- import { analyzeProject, createIsTestFile } from "ts-unused";
648
+ import { mergeConfig, defaultConfig, type UnusedConfig } from "ts-unused";
601
649
 
602
- const isTestFile = createIsTestFile([
650
+ const partialConfig: UnusedConfig = {
651
+ packageMode: true,
652
+ ignoreExports: ["internal*"],
653
+ };
654
+
655
+ // Returns complete config with all defaults filled in
656
+ const fullConfig = mergeConfig(partialConfig);
657
+ ```
658
+
659
+ #### `defaultConfig`
660
+
661
+ The default configuration values used when no config is provided.
662
+
663
+ ```typescript
664
+ import { defaultConfig } from "ts-unused";
665
+
666
+ console.log(defaultConfig.testFilePatterns);
667
+ // ["**/*.test.ts", "**/*.test.tsx", "**/*.spec.ts", "**/*.spec.tsx", "**/__tests__/**"]
668
+ ```
669
+
670
+ #### `createIsTestFile(patterns)` / `isTestFile`
671
+
672
+ Creates a custom test file detection function from glob patterns, or use the default `isTestFile`.
673
+
674
+ ```typescript
675
+ import { analyzeProject, createIsTestFile, isTestFile } from "ts-unused";
676
+
677
+ // Use default test file detection
678
+ isTestFile(sourceFile); // checks against default patterns
679
+
680
+ // Create custom test file detector
681
+ const customIsTestFile = createIsTestFile([
603
682
  "**/*.test.ts",
604
683
  "**/*.spec.ts",
605
684
  "**/test/**",
606
685
  ]);
607
686
 
608
- const results = analyzeProject("./tsconfig.json", undefined, undefined, { isTestFile });
687
+ const results = analyzeProject("./tsconfig.json", undefined, undefined, {
688
+ isTestFile: customIsTestFile
689
+ });
609
690
  ```
610
691
 
611
692
  #### Pattern Matching Utilities
@@ -625,6 +706,29 @@ const regex = patternToRegex("**/*.test.ts");
625
706
  regex.test("src/utils.test.ts"); // true
626
707
  ```
627
708
 
709
+ #### Exported Types
710
+
711
+ ```typescript
712
+ import type {
713
+ // Main result types
714
+ AnalysisResults, // Return type of analyzeProject()
715
+ UnusedExportResult, // Individual unused export finding
716
+ UnusedPropertyResult, // Individual unused property finding
717
+ NeverReturnedTypeResult,// Individual never-returned type finding
718
+ FixResults, // Return type of fixProject()
719
+
720
+ // Configuration types
721
+ UnusedConfig, // Configuration options interface
722
+ AnalyzeProjectOptions, // Options for analyzeProject()
723
+
724
+ // Utility types
725
+ ExportKind, // "function" | "class" | "interface" | "type" | ...
726
+ Severity, // "error" | "warning" | "info"
727
+ IsTestFileFn, // (sourceFile: SourceFile) => boolean
728
+ PropertyUsageResult, // Result of property usage check
729
+ } from "ts-unused";
730
+ ```
731
+
628
732
  ## License
629
733
 
630
734
  MIT License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-unused",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Find unused exports and properties in TypeScript projects",
5
5
  "license": "MIT",
6
6
  "bin": {