@prunr/prunr 0.4.0 → 0.5.0
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.
- package/README.md +141 -81
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -6,38 +6,44 @@
|
|
|
6
6
|
</p>
|
|
7
7
|
|
|
8
8
|
<p align="center">
|
|
9
|
-
|
|
9
|
+
Dead code finder for Node.js, with CommonJS as a first-class citizen.
|
|
10
10
|
</p>
|
|
11
11
|
|
|
12
12
|
<p align="center">
|
|
13
13
|
<a href="https://www.npmjs.com/package/@prunr/prunr"><img src="https://img.shields.io/npm/v/@prunr/prunr" alt="npm"></a>
|
|
14
|
-
<a href="LICENSE"><img src="https://img.shields.io/npm/l/@prunr/prunr" alt="
|
|
14
|
+
<a href="LICENSE"><img src="https://img.shields.io/npm/l/@prunr/prunr" alt="MIT license"></a>
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<a href="https://github.com/ViniciusDev26/prunr/blob/main/README.md"><img src="https://img.shields.io/badge/lang-English-blue" alt="Read in English"></a>
|
|
19
|
+
<a href="https://github.com/ViniciusDev26/prunr/blob/main/README.pt-BR.md"><img src="https://img.shields.io/badge/lang-Portugu%C3%AAs%20(BR)-green" alt="Leia em português"></a>
|
|
15
20
|
</p>
|
|
16
21
|
|
|
17
22
|
---
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
prunr builds a module graph from your project's entry points and finds what nothing uses: files,
|
|
25
|
+
exports, imports and dependencies. From the same graph, it points out undeclared dependencies,
|
|
26
|
+
circular imports and `require` calls it cannot follow.
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
It is one Rust binary, one config file and one command, in place of depcheck + madge + custom
|
|
29
|
+
scripts.
|
|
25
30
|
|
|
26
|
-
- **CommonJS
|
|
27
|
-
|
|
28
|
-
- **ESM
|
|
29
|
-
Node.
|
|
30
|
-
- **
|
|
31
|
-
NestJS, Drizzle Kit, PostCSS
|
|
32
|
-
|
|
33
|
-
- **
|
|
34
|
-
|
|
31
|
+
- **Real CommonJS:** `module.exports`, `exports.x`, destructured `require`, static methods and
|
|
32
|
+
`require` with a built path (`` require(`./handlers/${name}`) ``).
|
|
33
|
+
- **ESM and TypeScript in the same graph**, with `tsconfig.json` `paths` and CJS/ESM interop the
|
|
34
|
+
way Node does it.
|
|
35
|
+
- **Knows your stack** with no setup: Jest, Vitest, Mocha, `node --test`, tap, AVA, Playwright,
|
|
36
|
+
Vite, React, Next.js, NestJS, Drizzle Kit, PostCSS and monorepos (npm, yarn, pnpm, bun, lerna,
|
|
37
|
+
rush, nx, turbo).
|
|
38
|
+
- **Fixes it for you:** `prunr fix` deletes what is dead and cleans up `package.json`.
|
|
39
|
+
- **Built for CI:** annotations on the PR diff, only new findings with `--since`, and a baseline
|
|
40
|
+
for adopting it on legacy code.
|
|
35
41
|
|
|
36
|
-
##
|
|
42
|
+
## Quick start
|
|
37
43
|
|
|
38
44
|
```sh
|
|
39
|
-
npm i -D @prunr/prunr #
|
|
40
|
-
npx prunr init #
|
|
45
|
+
npm i -D @prunr/prunr # or pnpm add -D / yarn add -D / bun add -d
|
|
46
|
+
npx prunr init # creates a prunr.json from package.json
|
|
41
47
|
npx prunr check
|
|
42
48
|
```
|
|
43
49
|
|
|
@@ -55,16 +61,16 @@ unresolved-require src/index.js: require(process.env.PLUGIN) (warning)
|
|
|
55
61
|
errors: 7, warnings: 1
|
|
56
62
|
```
|
|
57
63
|
|
|
58
|
-
(
|
|
64
|
+
(That is the output for the project in [`tests/fixtures/example/`](tests/fixtures/example/).)
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
[release](https://github.com/ViniciusDev26/prunr/releases),
|
|
66
|
+
The package downloads only the binary for your platform (Linux x64/arm64, macOS x64/arm64,
|
|
67
|
+
Windows x64), with no install script. The binaries are also attached to every
|
|
68
|
+
[release](https://github.com/ViniciusDev26/prunr/releases), each with a `.sha256`.
|
|
63
69
|
|
|
64
|
-
##
|
|
70
|
+
## Configuration
|
|
65
71
|
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
`prunr.json` says where the program starts. Everything those entries reach is alive; what only
|
|
73
|
+
the tests reach is dead in production.
|
|
68
74
|
|
|
69
75
|
```json
|
|
70
76
|
{
|
|
@@ -76,42 +82,43 @@ que só os testes alcançam está morto em produção.
|
|
|
76
82
|
}
|
|
77
83
|
```
|
|
78
84
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
em [Configuração](docs/guide/configuration.md).
|
|
85
|
+
The files the `scripts` of `package.json` run (`node src/server.js`, `tsx watch src/index.ts`)
|
|
86
|
+
are entries already, and with Jest, Vitest, Vite, Next.js or NestJS prunr finds most of the rest
|
|
87
|
+
on its own. Every option is in [Configuration](docs/guide/configuration.md).
|
|
83
88
|
|
|
84
|
-
##
|
|
89
|
+
## Commands
|
|
85
90
|
|
|
86
|
-
|
|
|
91
|
+
| Command | What it does |
|
|
87
92
|
|---|---|
|
|
88
|
-
| `prunr check` |
|
|
89
|
-
| `prunr fix` |
|
|
90
|
-
| `prunr
|
|
91
|
-
| `prunr
|
|
92
|
-
| `prunr
|
|
93
|
+
| `prunr check` | Lists the findings. `--json` for machines, `--github` for PR annotations, `--sarif` for code scanning, `--codeclimate` for GitLab Code Quality, `--markdown` for a PR comment, `--since <ref>` for only what is new, `--production` for only what ships. |
|
|
94
|
+
| `prunr fix` | Removes dead code and unused dependencies. `--dry-run` shows the changes without making them. |
|
|
95
|
+
| `prunr why <target>` | Explains a file (the chain of imports from an entry), a `file:export` (who uses it) or a package (the files, scripts and tools that need it). |
|
|
96
|
+
| `prunr init` | Creates a `prunr.json` from `package.json`. |
|
|
97
|
+
| `prunr baseline` | Records the current findings, to adopt prunr without fixing everything at once. |
|
|
98
|
+
| `prunr schema` | Prints the JSON Schema of `prunr.json`. |
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
`check` exits with `0` when there are no findings (or only warnings), `1` when there are
|
|
101
|
+
findings, and `2` when it cannot run.
|
|
96
102
|
|
|
97
|
-
##
|
|
103
|
+
## What it finds
|
|
98
104
|
|
|
99
|
-
|
|
|
105
|
+
| Finding | When |
|
|
100
106
|
|---|---|
|
|
101
|
-
| `unused-file` |
|
|
102
|
-
| `unused-export` |
|
|
103
|
-
| `unused-import` |
|
|
104
|
-
| `unused-dependency` |
|
|
105
|
-
| `misplaced-dependency` |
|
|
106
|
-
| `missing-dependency` |
|
|
107
|
-
| `circular-import` |
|
|
108
|
-
| `unused-ignore` |
|
|
109
|
-
| `unresolved-require` |
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
107
|
+
| `unused-file` | No production entry reaches the file. |
|
|
108
|
+
| `unused-export` | No production code uses the export. |
|
|
109
|
+
| `unused-import` | A `require` or `import` brings in a name the file never uses. |
|
|
110
|
+
| `unused-dependency` | Nothing imports the package and no script runs it. |
|
|
111
|
+
| `misplaced-dependency` | The package is in `dependencies`, but only the tests use it. |
|
|
112
|
+
| `missing-dependency` | The code imports a package `package.json` does not declare. |
|
|
113
|
+
| `circular-import` | There is an import cycle. |
|
|
114
|
+
| `unused-ignore` | A `prunr-ignore` comment that no longer ignores anything. |
|
|
115
|
+
| `unresolved-require` | Warning: a `require` prunr cannot follow. |
|
|
116
|
+
| `unparsed-file` | Warning: a file the graph reaches does not parse, so what it uses is unknown. |
|
|
117
|
+
|
|
118
|
+
When the only use comes from the tests, the finding says `(only tests use it)`. The details of
|
|
119
|
+
each are in [Findings](docs/guide/checks.md).
|
|
120
|
+
|
|
121
|
+
## In CI
|
|
115
122
|
|
|
116
123
|
```yaml
|
|
117
124
|
- uses: actions/checkout@v4
|
|
@@ -121,42 +128,95 @@ estão em [Achados](docs/guide/checks.md).
|
|
|
121
128
|
- run: npx prunr check --github --since origin/${{ github.base_ref }}
|
|
122
129
|
```
|
|
123
130
|
|
|
124
|
-
|
|
125
|
-
`prunr baseline`:
|
|
126
|
-
[CI
|
|
131
|
+
Each new finding becomes an annotation on the right line of the diff. For legacy code, start
|
|
132
|
+
with `prunr baseline`: what is already there does not fail, and anything that shows up later
|
|
133
|
+
does. See [CI and legacy code](docs/guide/ci.md).
|
|
134
|
+
|
|
135
|
+
## Guide
|
|
127
136
|
|
|
128
|
-
|
|
137
|
+
The guide is in Portuguese for now.
|
|
129
138
|
|
|
130
|
-
- [
|
|
131
|
-
- [
|
|
132
|
-
- [`prunr fix`](docs/guide/fix.md):
|
|
139
|
+
- [Configuration](docs/guide/configuration.md): `prunr.json` and the `scripts` of `package.json`.
|
|
140
|
+
- [Findings](docs/guide/checks.md): what each one means, ESM and TypeScript, dynamic `require`.
|
|
141
|
+
- [`prunr fix`](docs/guide/fix.md): what it removes and what it leaves to you.
|
|
133
142
|
- [Frameworks](docs/guide/frameworks.md): Jest, Vitest, Playwright, Vite, React, Next.js,
|
|
134
|
-
NestJS, Drizzle Kit
|
|
135
|
-
- [Monorepos](docs/guide/monorepos.md):
|
|
136
|
-
- [
|
|
137
|
-
- [CI
|
|
143
|
+
NestJS, Drizzle Kit and PostCSS.
|
|
144
|
+
- [Monorepos](docs/guide/monorepos.md): how packages are found and checked.
|
|
145
|
+
- [Ignoring in code](docs/guide/ignore.md): exports that are public on purpose.
|
|
146
|
+
- [CI and legacy code](docs/guide/ci.md): annotations, `--since` and the baseline.
|
|
147
|
+
|
|
148
|
+
## What it leaves out
|
|
149
|
+
|
|
150
|
+
- **Unused variables and private members inside a file** are a linter's job. Turn on
|
|
151
|
+
`noUnusedVariables` and `noUnusedPrivateClassMembers` in [Biome](https://biomejs.dev/):
|
|
152
|
+
together with prunr's `unused-import`, they cover what `tsc`'s `noUnusedLocals` catches.
|
|
153
|
+
- **Formatting, style and types:** use Biome and `tsc --noEmit`.
|
|
154
|
+
- **Dependencies named only in another tool's config** (ESLint plugins, Babel presets) show up
|
|
155
|
+
as `unused-dependency`. List them in `dependencies.ignore`.
|
|
156
|
+
|
|
157
|
+
What comes next is in the [roadmap](docs/roadmap/).
|
|
158
|
+
|
|
159
|
+
## Why prunr, and not Knip or other tools
|
|
160
|
+
|
|
161
|
+
[Knip](https://knip.dev) is the reference for ESM and TypeScript, and tools like `ts-prune` and
|
|
162
|
+
`unimported` were archived pointing to it. prunr exists for the case Knip treats as secondary:
|
|
163
|
+
**Node.js code in CommonJS**, alone or mixed with ESM.
|
|
164
|
+
|
|
165
|
+
**CommonJS as it is actually written.** Knip asks code to follow a convention so it can
|
|
166
|
+
understand the exports: according to its
|
|
167
|
+
[CommonJS guide](https://knip.dev/guides/working-with-commonjs), only `module.exports.x =` and an
|
|
168
|
+
object with shorthand keys only (`{ a, b }`) are named exports; any other object in
|
|
169
|
+
`module.exports` becomes a single default export, and importing the whole module makes every
|
|
170
|
+
named export show up as unused. prunr reads the code you already have, without asking you to
|
|
171
|
+
change it:
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
// invoices.js
|
|
175
|
+
module.exports = {
|
|
176
|
+
total,
|
|
177
|
+
cancel: () => {},
|
|
178
|
+
format(value) { return value; },
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// index.js
|
|
182
|
+
const invoices = require("./invoices");
|
|
183
|
+
invoices.format(invoices.total());
|
|
184
|
+
const handler = require(`./handlers/${name}`);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
prunr reports only `cancel`: it reads each key of the object, follows the use through
|
|
188
|
+
`invoices.x`, and the `require` built at runtime keeps the files in `handlers/` alive. When in
|
|
189
|
+
doubt (the module passed along, a computed key), it treats everything as used: it is better to
|
|
190
|
+
let a dead export through than to tell you to delete a live one.
|
|
138
191
|
|
|
139
|
-
|
|
192
|
+
This is not a hypothetical case. prunr was born in a production Node.js project, all in
|
|
193
|
+
CommonJS, that adopted Knip, fixed its entry points, deleted what it pointed out, and ended up
|
|
194
|
+
removing it because of too many false positives. In the same project, prunr pointed out 26 real
|
|
195
|
+
problems (dead files and exports, unused dependencies and a queue shutdown that was never
|
|
196
|
+
called), all of them fixed, and it now runs in CI with no pending findings.
|
|
140
197
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
- **Formatação, estilo e tipos:** use Biome e `tsc --noEmit`.
|
|
145
|
-
- **Dependências citadas só na configuração de outra ferramenta** (plugins do ESLint, presets do
|
|
146
|
-
Babel) aparecem como `unused-dependency`. Liste-as em `dependencies.ignore`.
|
|
198
|
+
**One tool in place of several.** Unused files, exports, imports and dependencies, undeclared
|
|
199
|
+
or misplaced dependencies and circular imports all come out of the same graph, in one command.
|
|
200
|
+
That is the work usually split across `depcheck`, `madge`, `unimported` and custom scripts.
|
|
147
201
|
|
|
148
|
-
|
|
202
|
+
**Fast, and it runs nothing.** It is a native binary, written in Rust, that reads and resolves
|
|
203
|
+
files in parallel; the npm package only downloads the binary for your platform, with no install
|
|
204
|
+
script. On a monorepo of 13,000 files, the check takes 0.6 s. Tool configs (Jest, Vitest,
|
|
205
|
+
Playwright, Vite...) are read as text, never executed.
|
|
149
206
|
|
|
150
|
-
|
|
207
|
+
**Built to adopt on existing code.** A baseline so you do not have to fix everything at once,
|
|
208
|
+
`--since` and diff annotations to block only what is new in the PR, and a `prunr fix` that
|
|
209
|
+
deletes dead code and cleans up `package.json`.
|
|
151
210
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
211
|
+
**When Knip is the better choice.** On a project that is only ESM and TypeScript, Knip has many
|
|
212
|
+
more plugins (prunr knows Jest, Vitest, Playwright, Vite, React, Next.js, NestJS, Drizzle Kit
|
|
213
|
+
and PostCSS) and more mileage. prunr pays off when there is CommonJS in the mix, or when speed
|
|
214
|
+
and a single tool matter more.
|
|
155
215
|
|
|
156
|
-
##
|
|
216
|
+
## Contributing
|
|
157
217
|
|
|
158
|
-
|
|
218
|
+
prunr is built with TDD and 100% test coverage. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
159
219
|
|
|
160
|
-
##
|
|
220
|
+
## License
|
|
161
221
|
|
|
162
222
|
[MIT](LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunr/prunr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Dead code detector for Node.js, with CommonJS as a first-class citizen",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"node": ">=18"
|
|
27
27
|
},
|
|
28
28
|
"optionalDependencies": {
|
|
29
|
-
"@prunr/darwin-arm64": "0.
|
|
30
|
-
"@prunr/darwin-x64": "0.
|
|
31
|
-
"@prunr/linux-arm64": "0.
|
|
32
|
-
"@prunr/linux-x64": "0.
|
|
33
|
-
"@prunr/win32-x64": "0.
|
|
29
|
+
"@prunr/darwin-arm64": "0.5.0",
|
|
30
|
+
"@prunr/darwin-x64": "0.5.0",
|
|
31
|
+
"@prunr/linux-arm64": "0.5.0",
|
|
32
|
+
"@prunr/linux-x64": "0.5.0",
|
|
33
|
+
"@prunr/win32-x64": "0.5.0"
|
|
34
34
|
}
|
|
35
35
|
}
|