Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 1, 2024
1 parent d90134f commit 31be659
Showing 1 changed file with 135 additions and 28 deletions.
163 changes: 135 additions & 28 deletions packages/docs/src/content/docs/features/auto-fix.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
---
title: Auto-fix
sidebar:
badge:
text: Experimental
variant: caution
---

import { Tabs, TabItem } from '@astrojs/starlight/components';
Expand All @@ -14,6 +10,9 @@ with the `--fix` flag to let Knip automatically apply fixes.
Knip currently can fix these [issue types][1]:

- Remove `export` keyword for unused exports and exported types
- Remove `export default` keywords for unused default exports
- Remove exports, re-exports and export types
- Remove unused enum and class members
- Remove unused `dependencies` and `devDependencies` from `package.json`
- Remove unused files
- Works across workspaces in a monorepo
Expand Down Expand Up @@ -46,22 +45,25 @@ knip --fix-type exports,types
knip --fix-type exports --fix-type types # same as above
```

Use VCS/Git to review and undo changes if necessary.

## Post-fix

After Knip has fixed issues, there are two things to consider:
After Knip has fixed issues, there are four things to consider:

### 1. Use version control

### Unused variables
Use a VCS (version control system) like Git to review and undo changes as
necessary.

### 2. Unused variables

Use tools like ESLint or Biome to find and remove unused variables inside files.
This may result in more deleted code, and Knip may then find more unused code.
Rinse and repeat!

### Unused dependencies
### 3. Unused dependencies

Verify changes in `package.json` and update dependencies using your package
manager:
manager.

<Tabs syncKey="pm">
<TabItem label="npm">
Expand Down Expand Up @@ -97,11 +99,51 @@ manager:
</TabItem>
</Tabs>

### 4. Install unlisted dependencies

If Knip reports unlisted dependencies or binaries, they should be installed.

<Tabs syncKey="pm">
<TabItem label="npm">

```shell
npm install unlisted-package
```

</TabItem>

<TabItem label="pnpm">

```shell
pnpm add unlisted-package
```

</TabItem>

<TabItem label="bun">

```shell
bun add unlisted-package
```

</TabItem>

<TabItem label="yarn">

```shell
yarn add unlisted-package
```

</TabItem>
</Tabs>

## Example results

### Exports

The `export` keyword for unused exports is removed:

```diff title="file.js"
```diff title="module.ts"
-export const unused = 1;
-export default class MyClass {}
+const unused = 1
Expand All @@ -110,35 +152,61 @@ The `export` keyword for unused exports is removed:

The `default` keyword was also removed here.

Knip cleans up the whole or part of export declarations:
Knip removes the whole or part of export declarations:

```diff title="file.js"
const Snake = 'snake';
const Owl = 'owl';
const Hawk = 'tuah';
-export { Snake, Owl };
-export { Hawk };
```diff title="module.ts"
type Snake = 'python' | 'anaconda';
const Owl = 'Hedwig';
const Hawk = 'Tuah';
-export type { Snake };
-export { Owl, Hawk };
+;
+;
```
Knip cleans up the whole or part of re-exports:
### Re-exports
Knip removes the whole or part of re-exports:
```diff title="file.js"
-export { Cat, Dog } from './pets';
-export { Lion, Elephant } from './jungle';
+export { Elephant } from './jungle'
```
Lines like the following are removed completely:
### Enum members
Unused members of enums are removed:
```diff title="file.ts"
export enum Directions {
North = 1,
East = 2,
- South = 3,
+
West = 4,
}
```
### CommonJS
Knip supports CommonJS and removes unused exports:
```diff title="common.js"
-module.exports = { identifier, unused };
+module.exports = { identifier, };
```diff title="file.js"
-module.exports.UNUSED = 1;
-module.exports['ACCESS'] = 1;
+
+
```
Warning: the right-hand side of such an assignment might have side-effects. Knip
currently removes the whole declaration (feel free to open an issue/RFC).
### Dependencies
Unused dependencies are removed from `package.json`:
```diff title="package.json"
Expand All @@ -156,22 +224,61 @@ Unused dependencies are removed from `package.json`:
}
```
Knip does not remove empty export declaration bindings, it only removes the
individual exported items:
### Class members
Unused members of classes can be removed:
```diff title="file.ts"
export class Rectangle {
constructor(public width: number, public height: number) {}
- static Key = 1;
+
area() {
return this.width * this.height;
}
- public get unusedGetter(): string {
- return 'unusedGetter';
- }
}
```
Warning: currently this might be too eager in codebases where class members
could be called by an external library. For instance, Knip might think
`componentDidMount` and `render` in React class component are unused and will
remove those.
Note that [`classMembers` aren't included by default][2].
### Export assignments
Knip removes individual exported items in "export assignments", but does not
remove the entire export declaration if it's empty:
```diff title="file.js"
-export const { a, b } = { a, b };
+export const { , } = { a, b };
-export const { a, b } = fn();
+export const { } = fn();
-export const [c, d] = [c, d];
+export const [, ] = [c, d];
```
Reason: the right-hand side of the assignment might have side-effects. It's not
safe to always remove the whole declaration. This could be improved in the
future (feel free to open an issue/RFC).
## What's not included
Operations that auto-fix does not yet perform include:
Operations that auto-fix does not (yet) perform and why:
- Add unlisted (dev) dependencies to `package.json`
- Remove unused class and enum members
- Add unlisted (dev) dependencies to `package.json` (should it go into
`dependencies` or `devDependencies`? For monorepos in current workspace or
root?)
- Add unlisted binaries (which package and package version contains the used
binary?)
- Fix duplicate exports (which one should removed?)
[1]: ../reference/issue-types.md
[2]: ../guides/handling-issues.mdx#class-members

0 comments on commit 31be659

Please sign in to comment.