@line/ts-remove-unused
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -513,6 +513,7 @@ #!/usr/bin/env node | ||
import chalk2 from "chalk"; | ||
import { stdout } from "node:process"; | ||
import { cwd, stdout } from "node:process"; | ||
// lib/util/CliEditTracker.ts | ||
import chalk from "chalk"; | ||
import { relative } from "node:path"; | ||
var EditTrackerError = class extends Error { | ||
@@ -547,5 +548,7 @@ }; | ||
#spinnerIndex = 0; | ||
constructor(logger, mode) { | ||
#projectRoot; | ||
constructor(logger, mode, projectRoot) { | ||
this.#logger = logger; | ||
this.#mode = mode; | ||
this.#projectRoot = projectRoot; | ||
} | ||
@@ -611,3 +614,3 @@ #getProcessingFile(file) { | ||
this.#logger.write( | ||
`${chalk.yellow("export")} ${file}:${chalk.gray( | ||
`${chalk.yellow("export")} ${this.relativePath(file)}:${chalk.gray( | ||
getLinePosition(item.content, position).padEnd(7) | ||
@@ -626,4 +629,6 @@ )} ${chalk.gray(`'${code}'`)} | ||
this.clearProgressOutput(); | ||
this.#logger.write(`${chalk.yellow("file")} ${file} | ||
`); | ||
this.#logger.write( | ||
`${chalk.yellow("file")} ${this.relativePath(file)} | ||
` | ||
); | ||
} | ||
@@ -675,5 +680,9 @@ removeExport(file, { code, position }) { | ||
} | ||
relativePath(file) { | ||
return relative(this.#projectRoot, file).replaceAll("\\", "/"); | ||
} | ||
}; | ||
// lib/remove.ts | ||
import { relative as relative2 } from "node:path"; | ||
var createNodeJsLogger = () => "isTTY" in stdout && stdout.isTTY ? { | ||
@@ -696,4 +705,5 @@ write: stdout.write.bind(stdout), | ||
}) => { | ||
const editTracker = new CliEditTracker(logger, mode); | ||
const editTracker = new CliEditTracker(logger, mode, projectRoot); | ||
const { config, error } = ts2.readConfigFile(configPath, system.readFile); | ||
const relativeToCwd = (fileName) => relative2(cwd(), fileName).replaceAll("\\", "/"); | ||
const { options, fileNames } = ts2.parseJsonConfigFileContent( | ||
@@ -706,3 +716,3 @@ config, | ||
logger.write( | ||
`${chalk2.blue("tsconfig")} ${chalk2.gray("using")} ${configPath} | ||
`${chalk2.blue("tsconfig")} ${chalk2.gray("using")} ${relativeToCwd(configPath)} | ||
@@ -784,2 +794,3 @@ ` | ||
import { resolve } from "node:path"; | ||
import { cwd as cwd2 } from "node:process"; | ||
var cli = cac("ts-remove-unused"); | ||
@@ -802,3 +813,3 @@ cli.command("", "There are no subcommands. Simply execute ts-remove-unused").option("--project <file>", "Path to your tsconfig.json").option( | ||
mode: options.check ? "check" : "write", | ||
projectRoot: process.cwd() | ||
projectRoot: cwd2() | ||
}); | ||
@@ -805,0 +816,0 @@ }); |
@@ -508,6 +508,7 @@ // lib/remove.ts | ||
import chalk2 from "chalk"; | ||
import { stdout } from "node:process"; | ||
import { cwd, stdout } from "node:process"; | ||
// lib/util/CliEditTracker.ts | ||
import chalk from "chalk"; | ||
import { relative } from "node:path"; | ||
var EditTrackerError = class extends Error { | ||
@@ -542,5 +543,7 @@ }; | ||
#spinnerIndex = 0; | ||
constructor(logger, mode) { | ||
#projectRoot; | ||
constructor(logger, mode, projectRoot) { | ||
this.#logger = logger; | ||
this.#mode = mode; | ||
this.#projectRoot = projectRoot; | ||
} | ||
@@ -606,3 +609,3 @@ #getProcessingFile(file) { | ||
this.#logger.write( | ||
`${chalk.yellow("export")} ${file}:${chalk.gray( | ||
`${chalk.yellow("export")} ${this.relativePath(file)}:${chalk.gray( | ||
getLinePosition(item.content, position).padEnd(7) | ||
@@ -621,4 +624,6 @@ )} ${chalk.gray(`'${code}'`)} | ||
this.clearProgressOutput(); | ||
this.#logger.write(`${chalk.yellow("file")} ${file} | ||
`); | ||
this.#logger.write( | ||
`${chalk.yellow("file")} ${this.relativePath(file)} | ||
` | ||
); | ||
} | ||
@@ -670,5 +675,9 @@ removeExport(file, { code, position }) { | ||
} | ||
relativePath(file) { | ||
return relative(this.#projectRoot, file).replaceAll("\\", "/"); | ||
} | ||
}; | ||
// lib/remove.ts | ||
import { relative as relative2 } from "node:path"; | ||
var createNodeJsLogger = () => "isTTY" in stdout && stdout.isTTY ? { | ||
@@ -691,4 +700,5 @@ write: stdout.write.bind(stdout), | ||
}) => { | ||
const editTracker = new CliEditTracker(logger, mode); | ||
const editTracker = new CliEditTracker(logger, mode, projectRoot); | ||
const { config, error } = ts2.readConfigFile(configPath, system.readFile); | ||
const relativeToCwd = (fileName) => relative2(cwd(), fileName).replaceAll("\\", "/"); | ||
const { options, fileNames } = ts2.parseJsonConfigFileContent( | ||
@@ -701,3 +711,3 @@ config, | ||
logger.write( | ||
`${chalk2.blue("tsconfig")} ${chalk2.gray("using")} ${configPath} | ||
`${chalk2.blue("tsconfig")} ${chalk2.gray("using")} ${relativeToCwd(configPath)} | ||
@@ -704,0 +714,0 @@ ` |
@@ -5,3 +5,3 @@ import { EditTracker } from './EditTracker.js'; | ||
#private; | ||
constructor(logger: Logger, mode: 'check' | 'write'); | ||
constructor(logger: Logger, mode: 'check' | 'write', projectRoot: string); | ||
setTotal(total: number): void; | ||
@@ -19,2 +19,3 @@ clearProgressOutput(): void; | ||
get isClean(): boolean; | ||
relativePath(file: string): string; | ||
} |
@@ -33,2 +33,3 @@ { | ||
"prettier": "^3.3.3", | ||
"strip-ansi": "^7.1.0", | ||
"ts-node": "^10.9.1", | ||
@@ -44,3 +45,3 @@ "typescript-eslint": "^8.5.0" | ||
}, | ||
"version": "0.4.0" | ||
"version": "0.5.0" | ||
} |
@@ -32,36 +32,50 @@ <h1 align="center">ts-remove-unused</h1> | ||
This is when ts-remove-unused comes in handy. ts-remove-unused is a CLI tool made on top of TypeScript that reports/fixes unused exports. | ||
This is when ts-remove-unused comes in handy. ts-remove-unused is a CLI tool made on top of TypeScript that reports/fixes unused exports. Here are some examples of how ts-remove-unused auto-fixes unused code. | ||
Let's say you have the following file: | ||
<!-- prettier-ignore-start --> | ||
```typescript | ||
export const a = 'a'; | ||
When `a2` is not used within the project: | ||
export const b = 'b'; | ||
```diff | ||
--- src/a.ts | ||
+++ src/a.ts | ||
@@ -1,3 +1 @@ | ||
export const a = 'a'; | ||
- | ||
-export const a2 = 'a2'; | ||
``` | ||
export const c = 'c'; | ||
When `b` is not used within the project but `f()` is used within the project: | ||
console.log(b); | ||
```diff | ||
--- src/b.ts | ||
+++ src/b.ts | ||
@@ -1,5 +1,5 @@ | ||
-export const b = 'b'; | ||
+const b = 'b'; | ||
export function f() { | ||
return b; | ||
} | ||
``` | ||
When `a` and `b` are not used in all other files across the project, ts-remove-unused will modify the file to be: | ||
When `f()` is not used within the project and deleting it will result in `import` being unnecessary: | ||
```typescript | ||
const b = 'b'; | ||
export const c = 'c'; | ||
console.log(b); | ||
```diff | ||
--- src/c.ts | ||
+++ src/c.ts | ||
@@ -1,7 +1 @@ | ||
-import { cwd } from "node:process"; | ||
- | ||
export const c = 'c'; | ||
- | ||
-export function f() { | ||
- return cwd(); | ||
-} | ||
``` | ||
Let's say you have another file in your project: | ||
<!-- prettier-ignore-end --> | ||
```typescript | ||
export const d = 'd'; | ||
In addition to the behavior shown in the examples above, ts-remove-unused will delete files that have no used exports. | ||
export const e = 'e'; | ||
``` | ||
When `d` and `e` are not used in all other files across the project, ts-remove-unused will delete the file for you. | ||
ts-remove-unused supports various types of exports including variable declarations (`export const`, `export let`), function declarations, class declarations, interface declarations, type alias declarations, default exports and more... | ||
@@ -100,10 +114,13 @@ | ||
The CLI will respect the `tsconfig.json` for loading source files. | ||
ts-remove-unused's behavior heavily depends on your `tsconfig.json`. TypeScript's compiler internally holds the list of project files by parsing relevant rules such as `include` and `exclude`. ts-remove-unused scans through this list and searches for references to determine if an export/file is "unused". You may need to maintain/update your `tsconfig` (or you can create another one for `--project`) so that the set of covered files are right. | ||
Here's an example of using the cli. Make sure to skip your entrypoint file. | ||
Here's an example of using the CLI. Your entry point file must be skipped or else every file will be removed. | ||
```bash | ||
npx ts-remove-unused --skip 'src\/main\.ts' | ||
npx @line/ts-remove-unused --skip 'src/main\.ts' | ||
``` | ||
> [!WARNING] | ||
> THIS COMMAND WILL DELETE CODE FROM YOUR PROJECT. Using it in a git controlled environment is highly recommended. If you're just playing around use `--check`. | ||
### Check | ||
@@ -114,3 +131,3 @@ | ||
```bash | ||
npx ts-remove-unused --check | ||
npx @line/ts-remove-unused --check | ||
``` | ||
@@ -145,3 +162,3 @@ | ||
```bash | ||
npx ts-remove-unused --skip 'src/main\.ts' --skip '/pages/' | ||
npx @line/ts-remove-unused --skip 'src/main\.ts' --skip '/pages/' | ||
``` | ||
@@ -151,2 +168,8 @@ | ||
## How does ts-remove-unused handle test files? | ||
If you have a separate tsconfig for tests using [Project References](https://www.typescriptlang.org/docs/handbook/project-references.html), that would be great! ts-remove-unused will remove exports/files that exist for the sake of testing. | ||
If you pass a `tsconfig.json` to the CLI that includes both the implementation and the test files, ts-remove-unused will remove your test files since they are not referenced by your entry point file (which is specified in `--skip`). You can avoid tests being deleted by passing a pattern that matches your test files to `--skip` in the meantime, but the recommended way is to use project references to ensure your TypeScript config is more robust and strict (not just for using this tool). | ||
## Author | ||
@@ -153,0 +176,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67089
1660
191
10