### Install tsr package Source: https://github.com/line/tsr/blob/main/doc/migration.md Installs the latest version of the tsr package using npm. This is the first step in migrating from the previous version. ```bash npm i tsr ``` -------------------------------- ### Execute tsr CLI with specific tsconfig.json and entrypoint Source: https://github.com/line/tsr/blob/main/README.md This example shows how to specify both a custom tsconfig.json file ('tsconfig.client.json') and an entrypoint pattern ('src/main.ts$') when running tsr from the command line. ```bash npx tsr --project tsconfig.client.json 'src/main\.ts$' ``` -------------------------------- ### Example: Removing multiple unused declarations Source: https://github.com/line/tsr/blob/main/README_ko.md This diff demonstrates tsr removing multiple unused declarations (`exported`, `local`, and `f()`) that are dependent on each other in `src/d.ts`. ```diff --- src/d.ts +++ src/d.ts @@ -1,8 +1 @@ -export const exported = "exported"; -const local = "local"; - export const d = "d"; - -export function f() { - return { exported, local }; -} ``` -------------------------------- ### Example: Removing unused variable 'a2' Source: https://github.com/line/tsr/blob/main/README_ko.md This diff shows how tsr removes an unused variable `a2` from `src/a.ts`. ```diff --- src/a.ts +++ src/a.ts @@ -1,3 +1 @@ export const a = 'a'; - -export const a2 = 'a2'; ``` -------------------------------- ### Example: Removing unused import and function 'f' Source: https://github.com/line/tsr/blob/main/README_ko.md This diff shows tsr removing an unused import statement and the associated function `f()` from `src/c.ts`. ```diff --- src/c.ts +++ src/c.ts @@ -1,7 +1 @@ -import { cwd } from "node:process"; - export const c = 'c'; - -export function f() { - return cwd(); -} ``` -------------------------------- ### Example: Modifying unused export 'b' while keeping 'f()' Source: https://github.com/line/tsr/blob/main/README_ko.md This diff illustrates tsr modifying an unused export `b` to a const, while preserving the usage of function `f()` in `src/b.ts`. ```diff --- src/b.ts +++ src/b.ts @@ -1,5 +1,5 @@ -export const b = 'b'; +const b = 'b'; export function f() { return b; } ``` -------------------------------- ### tsr's zero-configuration approach Source: https://github.com/line/tsr/blob/main/README_ko.md tsr works out-of-the-box with just `tsconfig.json`, unlike Knip which requires separate configuration files for optimal performance. ```typescript export const a = 'a'; ``` -------------------------------- ### Use tsr with variadic arguments Source: https://github.com/line/tsr/blob/main/doc/migration.md Shows the updated command-line syntax for specifying files to process. The `--skip` option from v0 is replaced by passing file paths directly as arguments. ```bash npx @line/ts-remove-unused --skip 'src/main\.ts$' ``` ```bash npx tsr 'src/main\.ts$' ``` -------------------------------- ### Execute tsr CLI with multiple entrypoints Source: https://github.com/line/tsr/blob/main/README.md This command shows how to use tsr with multiple entrypoints by providing a regex pattern that matches several files, such as all '.ts' files within the 'src/pages/' directory. ```bash tsr 'src/pages/.*\.ts$' ``` -------------------------------- ### Enable file writing with tsr Source: https://github.com/line/tsr/blob/main/doc/migration.md Demonstrates how to use the `--write` flag with the tsr CLI to modify files in place. This replaces the default behavior of v0 which edited files directly. ```bash npx tsr --write 'src/main\.ts$' ``` -------------------------------- ### Execute tsr CLI to check unused code Source: https://github.com/line/tsr/blob/main/README.md This command executes the tsr CLI to check for unused code in a TypeScript project, using 'src/main.ts$' as the entrypoint pattern. It demonstrates the basic usage of the command-line interface. ```bash npx tsr 'src/main\.ts$' ``` -------------------------------- ### Execute tsr CLI with custom tsconfig.json Source: https://github.com/line/tsr/blob/main/README.md This command demonstrates how to use tsr with a custom tsconfig.json file. The '--project' flag specifies the path to the configuration file, which tsr uses for analysis. ```bash tsr --project tsconfig.app.json 'src/main\.ts$' ``` -------------------------------- ### Use tsr JavaScript API with custom config and project root Source: https://github.com/line/tsr/blob/main/README.md This TypeScript code snippet shows how to use the tsr JavaScript API with custom configuration options. It specifies the entrypoints, mode, a custom tsconfig file ('tsconfig.sample.json'), and the project root directory. ```typescript await tsr({ entrypoints: [/main\.ts/], mode: 'check', configFile: 'tsconfig.sample.json', projectRoot: '/path/to/project', }); ``` -------------------------------- ### CLI Usage Source: https://github.com/line/tsr/blob/main/README.md Documentation for the tsr command-line interface, including options for specifying the project file, writing changes, recursive analysis, and including declaration files. ```APIDOC ## CLI Usage ### Description Command-line interface for tsr to find and remove unused code. ### Method CLI command ### Endpoint N/A ### Parameters #### Options - **-p, --project** (string) - Optional - Path to your tsconfig.json. Defaults to `tsconfig.json` in your project root. - **-w, --write** (boolean) - Optional - Write changes in place. **WARNING:** This will delete code. Using it in a git controlled environment is highly recommended. - **-r, --recursive** (boolean) - Optional - Recursively look into files until the project is clean. Takes longer but helps catch issues from file edits. - **--include-d-ts** (boolean) - Optional - Check for unused code in `.d.ts` files. By default, exported types in `.d.ts` files are not detected. - **-h, --help** (boolean) - Optional - Display the help message. - **-v, --version** (boolean) - Optional - Display the version number. #### Entrypoints - **...entrypoints** (string[]) - Required - Regex patterns that match the entrypoint files of your project. ### Request Example ```bash npx tsr 'src/main\.ts$' npx tsr --write 'src/main\.ts$' npx tsr --project tsconfig.app.json 'src/main\.ts$' npx tsr 'src/pages/.*\.ts$' ``` ### Response #### Success Response (0) - No explicit success response body, but files may be modified if `--write` is used. #### Response Example (No specific response body format for CLI success) ``` -------------------------------- ### Update JavaScript API import Source: https://github.com/line/tsr/blob/main/doc/migration.md Illustrates the change in how to import the core functionality in JavaScript projects. The import path and function name have been updated from `@line/ts-remove-unused` to `tsr`. ```typescript import { remove } from '@line/ts-remove-unused'; // before ``` ```typescript import { tsr } from 'tsr'; // after ``` -------------------------------- ### tsr License Information Source: https://github.com/line/tsr/blob/main/README_ko.md This code block contains the license information for tsr, indicating it is licensed under the Apache License, Version 2.0. ```text Copyright (C) 2023 LINE Corp. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` -------------------------------- ### Execute tsr CLI to remove unused code Source: https://github.com/line/tsr/blob/main/README.md This command executes the tsr CLI with the '--write' flag, which enables it to modify files in place by removing unused code. It's recommended to use this in a git-controlled environment. ```bash tsr --write 'src/main\.ts$' ``` -------------------------------- ### ESLint cannot detect unused exports Source: https://github.com/line/tsr/blob/main/README_ko.md ESLint, by default, operates on a file-by-file basis and does not provide functionality to analyze the usage of exports across the entire project, making it difficult to introduce rules for detecting unused exports. ```typescript // It is difficult to introduce a rule that detects whether this export is actually used within the project. export const a = 'a'; ``` -------------------------------- ### tsr: Remove unused exported and local in src/d.ts Source: https://github.com/line/tsr/blob/main/README.md Demonstrates tsr removing unused 'exported' and 'local' variables, and consequently the function 'f()' that uses them, simplifying the codebase. ```diff --- src/d.ts +++ src/d.ts @@ -1,8 +1 @@ -export const exported = "exported"; -const local = "local"; - export const d = "d"; - -export function f() { - return { exported, local }; -} ``` -------------------------------- ### Execute tsr CLI to include .d.ts files Source: https://github.com/line/tsr/blob/main/README.md This command uses the '--include-d-ts' flag to instruct tsr to analyze type definitions within .d.ts files for unused exported types. By default, these are not checked. ```bash tsr --include-d-ts 'src/main\.ts$' ``` -------------------------------- ### JavaScript API Source: https://github.com/line/tsr/blob/main/README.md Documentation for using tsr as a JavaScript API within your Node.js projects. ```APIDOC ## JavaScript API ### Description Execute tsr programmatically within your JavaScript or TypeScript projects. ### Method JavaScript function call ### Endpoint N/A ### Parameters #### Function Signature `tsr(config: Config): Promise` #### Config Object (`Config`) - **entrypoints** (RegExp[]) - Required - An array of regular expressions matching your project's entrypoint files. - **mode** (string) - Required - Set to `'check'` to find unused code. Use `'clean'` to remove unused code (similar to `--write` in CLI). - **configFile** (string) - Optional - Path to a custom `tsconfig.json` file. Defaults to `tsconfig.json` in the project root. - **projectRoot** (string) - Optional - The root directory of the project. Defaults to the current working directory. ### Request Example ```typescript import { tsr } from 'tsr'; // Basic usage to check for unused code await tsr({ entrypoints: [/main\.ts/], mode: 'check', }).catch(() => { process.exitCode = 1; }); // Usage with custom tsconfig and project root await tsr({ entrypoints: [/main\.ts/], mode: 'check', configFile: 'tsconfig.sample.json', projectRoot: '/path/to/project', }); ``` ### Response #### Success Response - The function returns a `Promise` upon successful execution. #### Response Example ```typescript // No explicit response body for success. // Errors will be thrown and caught by the .catch() block. ``` ``` -------------------------------- ### Excluding Test Files with npx tsr Source: https://github.com/line/tsr/blob/main/README.md When tsr processes files, it can potentially remove test files if they are not referenced by the entry point. Use patterns with the `-w` flag to specify entry points and exclude test files, ensuring they are not deleted. ```bash npx tsr -w 'src/main\.ts$' ## depending on the tsconfig, this will delete test files ``` ```bash npx tsr -w 'src/main\.ts$' '.*\.test\.ts$' ## Specifying test files as entrypoints will avoid deletion ``` -------------------------------- ### Use tsr JavaScript API to check for unused code Source: https://github.com/line/tsr/blob/main/README.md This TypeScript code snippet demonstrates how to use the tsr JavaScript API to check for unused code. It imports the 'tsr' function and calls it with an array of entrypoint patterns and the mode set to 'check'. If an error occurs, it sets the process exit code to 1. ```typescript import { tsr } from 'tsr'; await tsr({ entrypoints: [/main\.ts/], mode: 'check', }).catch(() => { process.exitCode = 1; }); ``` -------------------------------- ### tsr's approach to unused code Source: https://github.com/line/tsr/blob/main/README_ko.md The primary goal of tsr is to remove unused exports and, consequently, delete the associated modules. It also removes unnecessary imports that arise from the removal of exports. ```typescript export const a = 'a'; export const f = () => a2; const a2 = 'a2'; ``` -------------------------------- ### tsr: Remove unused function f and its import in src/c.ts Source: https://github.com/line/tsr/blob/main/README.md Shows tsr removing an unused function 'f()' and its associated import statement, demonstrating its capability to clean up both code and dependencies. ```diff --- src/c.ts +++ src/c.ts @@ -1,7 +1 @@ -import { cwd } from "node:process"; - export const c = 'c'; - -export function f() { - return cwd(); -} ``` -------------------------------- ### tsr's predictable behavior Source: https://github.com/line/tsr/blob/main/README_ko.md tsr uses TypeScript-based module resolution exclusively and is designed for explicit control via `tsconfig.json`, ensuring it functions correctly when TypeScript's type checking passes. ```typescript export const exported = "exported"; const local = "local"; export const d = "d"; export function f() { return { exported, local }; } ``` -------------------------------- ### Recursive editing with tsr Source: https://github.com/line/tsr/blob/main/README_ko.md tsr supports recursive editing using the `--recursive` option to repeatedly modify files until all unused code is removed. ```bash tsr --recursive ``` -------------------------------- ### Execute tsr CLI with recursive mode Source: https://github.com/line/tsr/blob/main/README.md This command enables the '--recursive' flag for tsr, which allows it to continuously scan and clean files until the project is considered 'clean'. This is useful for detecting unused code that might only become apparent after other code modifications. ```bash tsr --recursive 'src/main\.ts$' ``` -------------------------------- ### Skip Unused Exports with // tsr-skip Source: https://github.com/line/tsr/blob/main/README.md Add the comment `// tsr-skip` to an export declaration to prevent tsr from removing it. This is useful for exports that are intentionally unused or managed externally. ```typescript // tsr-skip export const hello = 'world'; ``` -------------------------------- ### tsr: Remove unused const a2 in src/a.ts Source: https://github.com/line/tsr/blob/main/README.md Demonstrates tsr removing an unused constant 'a2' from a TypeScript file. This shows tsr's ability to identify and delete unreferenced variables. ```diff --- src/a.ts +++ src/a.ts @@ -1,3 +1,2 @@ export const a = 'a'; - -export const a2 = 'a2'; ``` -------------------------------- ### Detecting Unused Imports with ESLint Source: https://github.com/line/tsr/blob/main/README.md ESLint, particularly with plugins like `eslint-plugin-unused-imports`, can detect and auto-fix unused imports. However, ESLint typically operates on a file-by-file basis and cannot inherently detect unused exports based on project-wide usage. ```typescript // 'foo' is defined but never used. import { foo } from './foo'; ``` ```typescript // a lint rule that detects if this export is used within the project is unlikely to be introduced export const a = 'a'; ``` -------------------------------- ### Detecting Unused Locals in TypeScript Source: https://github.com/line/tsr/blob/main/README.md TypeScript's `compilerOptions.noUnusedLocals` setting reports declarations that are declared but never read. This helps in identifying unused variables within a file. ```typescript // 'a' is declared but its value is never read. const a = 'a'; ``` -------------------------------- ### tsr: Modify unused const b and keep used function f in src/b.ts Source: https://github.com/line/tsr/blob/main/README.md Illustrates tsr's behavior when a constant 'b' is unused but a function 'f()' that references it is used. tsr removes the 'export' from 'b' to make it local, preserving the functionality of 'f()'. ```diff --- src/b.ts +++ src/b.ts @@ -1,5 +1,5 @@ -export const b = 'b'; +const b = 'b'; export function f() { return b; } ``` -------------------------------- ### Detect unused imports with ESLint plugin Source: https://github.com/line/tsr/blob/main/README_ko.md ESLint, with plugins like `eslint-plugin-unused-imports`, can detect and automatically fix unused imports in TypeScript code. However, it does not detect unused exports. ```typescript // 'foo' is defined but not used. import { foo } from './foo'; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.