@prunr/prunr 0.4.0 → 0.6.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 +154 -82
- package/package.json +6 -6
- package/schema.json +8 -0
package/README.md
CHANGED
|
@@ -6,38 +6,46 @@
|
|
|
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
|
-
|
|
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
|
+
Cypress, Storybook, Vite, React, Next.js, NestJS, Nuxt, SvelteKit, Astro, React Router/Remix,
|
|
37
|
+
VitePress, TypeScript (`tsconfig`), `.vue`/`.svelte`/`.astro`/`.mdx` files, ESLint, Prettier,
|
|
38
|
+
Babel, commitlint, husky, lint-staged, Tailwind, Prisma, tsup, Rollup, webpack, Drizzle Kit,
|
|
39
|
+
PostCSS and monorepos (npm, yarn, pnpm, bun, lerna, rush, nx, turbo).
|
|
40
|
+
- **Fixes it for you:** `prunr fix` deletes what is dead and cleans up `package.json`.
|
|
41
|
+
- **Built for CI:** annotations on the PR diff, only new findings with `--since`, and a baseline
|
|
42
|
+
for adopting it on legacy code.
|
|
35
43
|
|
|
36
|
-
##
|
|
44
|
+
## Quick start
|
|
37
45
|
|
|
38
46
|
```sh
|
|
39
|
-
npm i -D @prunr/prunr #
|
|
40
|
-
npx prunr init #
|
|
47
|
+
npm i -D @prunr/prunr # or pnpm add -D / yarn add -D / bun add -d
|
|
48
|
+
npx prunr init # creates a prunr.json from package.json
|
|
41
49
|
npx prunr check
|
|
42
50
|
```
|
|
43
51
|
|
|
@@ -55,16 +63,16 @@ unresolved-require src/index.js: require(process.env.PLUGIN) (warning)
|
|
|
55
63
|
errors: 7, warnings: 1
|
|
56
64
|
```
|
|
57
65
|
|
|
58
|
-
(
|
|
66
|
+
(That is the output for the project in [`tests/fixtures/example/`](tests/fixtures/example/).)
|
|
59
67
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
[release](https://github.com/ViniciusDev26/prunr/releases),
|
|
68
|
+
The package downloads only the binary for your platform (Linux x64/arm64, macOS x64/arm64,
|
|
69
|
+
Windows x64), with no install script. The binaries are also attached to every
|
|
70
|
+
[release](https://github.com/ViniciusDev26/prunr/releases), each with a `.sha256`.
|
|
63
71
|
|
|
64
|
-
##
|
|
72
|
+
## Configuration
|
|
65
73
|
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
`prunr.json` says where the program starts. Everything those entries reach is alive; what only
|
|
75
|
+
the tests reach is dead in production.
|
|
68
76
|
|
|
69
77
|
```json
|
|
70
78
|
{
|
|
@@ -76,42 +84,47 @@ que só os testes alcançam está morto em produção.
|
|
|
76
84
|
}
|
|
77
85
|
```
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
em [Configuração](docs/guide/configuration.md).
|
|
87
|
+
The files the `scripts` of `package.json` run (`node src/server.js`, `tsx watch src/index.ts`)
|
|
88
|
+
are entries already, and with Jest, Vitest, Vite, Next.js or NestJS prunr finds most of the rest
|
|
89
|
+
on its own. Every option is in [Configuration](docs/guide/configuration.md).
|
|
83
90
|
|
|
84
|
-
##
|
|
91
|
+
## Commands
|
|
85
92
|
|
|
86
|
-
|
|
|
93
|
+
| Command | What it does |
|
|
87
94
|
|---|---|
|
|
88
|
-
| `prunr check` |
|
|
89
|
-
| `prunr fix` |
|
|
90
|
-
| `prunr
|
|
91
|
-
| `prunr
|
|
92
|
-
| `prunr
|
|
95
|
+
| `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. |
|
|
96
|
+
| `prunr fix` | Removes dead code and unused dependencies. `--dry-run` shows the changes without making them. |
|
|
97
|
+
| `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). |
|
|
98
|
+
| `prunr init` | Creates a `prunr.json` from `package.json`, and from the Knip config when there is one. |
|
|
99
|
+
| `prunr baseline` | Records the current findings, to adopt prunr without fixing everything at once. |
|
|
100
|
+
| `prunr schema` | Prints the JSON Schema of `prunr.json`. |
|
|
93
101
|
|
|
94
|
-
|
|
95
|
-
|
|
102
|
+
`check` exits with `0` when there are no findings (or only warnings), `1` when there are
|
|
103
|
+
findings, and `2` when it cannot run.
|
|
96
104
|
|
|
97
|
-
##
|
|
105
|
+
## What it finds
|
|
98
106
|
|
|
99
|
-
|
|
|
107
|
+
| Finding | When |
|
|
100
108
|
|---|---|
|
|
101
|
-
| `unused-file` |
|
|
102
|
-
| `unused-export` |
|
|
103
|
-
| `unused-
|
|
104
|
-
| `unused-
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
107
|
-
| `
|
|
108
|
-
| `
|
|
109
|
-
| `
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
109
|
+
| `unused-file` | No production entry reaches the file. |
|
|
110
|
+
| `unused-export` | No production code uses the export. |
|
|
111
|
+
| `unused-enum-member` | No production code reads the member of an exported enum. |
|
|
112
|
+
| `unused-class-member` | No production code calls the method of an exported class, in an application. |
|
|
113
|
+
| `duplicate-export` | The same binding is exported under more than one name. |
|
|
114
|
+
| `unused-import` | A `require` or `import` brings in a name the file never uses. |
|
|
115
|
+
| `unused-dependency` | Nothing imports the package and no script runs it. |
|
|
116
|
+
| `misplaced-dependency` | The package is in `dependencies`, but only the tests use it. |
|
|
117
|
+
| `missing-dependency` | The code imports a package `package.json` does not declare. |
|
|
118
|
+
| `missing-binary` | A script runs a binary of a package `package.json` does not declare. |
|
|
119
|
+
| `circular-import` | There is an import cycle. |
|
|
120
|
+
| `unused-ignore` | A `prunr-ignore` comment that no longer ignores anything. |
|
|
121
|
+
| `unresolved-require` | Warning: a `require` prunr cannot follow. |
|
|
122
|
+
| `unparsed-file` | Warning: a file the graph reaches does not parse, so what it uses is unknown. |
|
|
123
|
+
|
|
124
|
+
When the only use comes from the tests, the finding says `(only tests use it)`. The details of
|
|
125
|
+
each are in [Findings](docs/guide/checks.md).
|
|
126
|
+
|
|
127
|
+
## In CI
|
|
115
128
|
|
|
116
129
|
```yaml
|
|
117
130
|
- uses: actions/checkout@v4
|
|
@@ -121,42 +134,101 @@ estão em [Achados](docs/guide/checks.md).
|
|
|
121
134
|
- run: npx prunr check --github --since origin/${{ github.base_ref }}
|
|
122
135
|
```
|
|
123
136
|
|
|
124
|
-
|
|
125
|
-
`prunr baseline`: o que já existe não falha, e o que surgir depois falha. Veja
|
|
126
|
-
[CI e código legado](docs/guide/ci.md).
|
|
137
|
+
Or with the Action, which needs no install step:
|
|
127
138
|
|
|
128
|
-
|
|
139
|
+
```yaml
|
|
140
|
+
- uses: actions/checkout@v4
|
|
141
|
+
- uses: ViniciusDev26/prunr@v0
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Each new finding becomes an annotation on the right line of the diff. For legacy code, start
|
|
145
|
+
with `prunr baseline`: what is already there does not fail, and anything that shows up later
|
|
146
|
+
does. See [CI and legacy code](docs/guide/ci.md).
|
|
147
|
+
|
|
148
|
+
## Guide
|
|
149
|
+
|
|
150
|
+
The guide is in Portuguese for now.
|
|
151
|
+
|
|
152
|
+
- [Configuration](docs/guide/configuration.md): `prunr.json` and the `scripts` of `package.json`.
|
|
153
|
+
- [Findings](docs/guide/checks.md): what each one means, ESM and TypeScript, dynamic `require`.
|
|
154
|
+
- [`prunr fix`](docs/guide/fix.md): what it removes and what it leaves to you.
|
|
155
|
+
- [Frameworks and tools](docs/guide/frameworks.md): test runners, frameworks, single-file
|
|
156
|
+
components and the tool configs prunr reads.
|
|
157
|
+
- [Monorepos](docs/guide/monorepos.md): how packages are found and checked.
|
|
158
|
+
- [Ignoring in code](docs/guide/ignore.md): exports that are public on purpose.
|
|
159
|
+
- [CI and legacy code](docs/guide/ci.md): annotations, `--since` and the baseline.
|
|
160
|
+
|
|
161
|
+
## What it leaves out
|
|
162
|
+
|
|
163
|
+
- **Unused variables and private members inside a file** are a linter's job. Turn on
|
|
164
|
+
`noUnusedVariables` and `noUnusedPrivateClassMembers` in [Biome](https://biomejs.dev/):
|
|
165
|
+
together with prunr's `unused-import`, they cover what `tsc`'s `noUnusedLocals` catches.
|
|
166
|
+
- **Formatting, style and types:** use Biome and `tsc --noEmit`.
|
|
167
|
+
- **Dependencies named only in the config of a tool prunr does not know** show up as
|
|
168
|
+
`unused-dependency`. List them in `dependencies.ignore`.
|
|
169
|
+
|
|
170
|
+
What comes next is in the [roadmap](docs/roadmap/).
|
|
171
|
+
|
|
172
|
+
## Why prunr, and not Knip or other tools
|
|
173
|
+
|
|
174
|
+
[Knip](https://knip.dev) is the reference for ESM and TypeScript, and tools like `ts-prune` and
|
|
175
|
+
`unimported` were archived pointing to it. prunr exists for the case Knip treats as secondary:
|
|
176
|
+
**Node.js code in CommonJS**, alone or mixed with ESM.
|
|
177
|
+
|
|
178
|
+
**CommonJS as it is actually written.** Knip asks code to follow a convention so it can
|
|
179
|
+
understand the exports: according to its
|
|
180
|
+
[CommonJS guide](https://knip.dev/guides/working-with-commonjs), only `module.exports.x =` and an
|
|
181
|
+
object with shorthand keys only (`{ a, b }`) are named exports; any other object in
|
|
182
|
+
`module.exports` becomes a single default export, and importing the whole module makes every
|
|
183
|
+
named export show up as unused. prunr reads the code you already have, without asking you to
|
|
184
|
+
change it:
|
|
185
|
+
|
|
186
|
+
```js
|
|
187
|
+
// invoices.js
|
|
188
|
+
module.exports = {
|
|
189
|
+
total,
|
|
190
|
+
cancel: () => {},
|
|
191
|
+
format(value) { return value; },
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// index.js
|
|
195
|
+
const invoices = require("./invoices");
|
|
196
|
+
invoices.format(invoices.total());
|
|
197
|
+
const handler = require(`./handlers/${name}`);
|
|
198
|
+
```
|
|
129
199
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
NestJS, Drizzle Kit e PostCSS.
|
|
135
|
-
- [Monorepos](docs/guide/monorepos.md): como os pacotes são encontrados e checados.
|
|
136
|
-
- [Ignorar no código](docs/guide/ignore.md): exports públicos de propósito.
|
|
137
|
-
- [CI e código legado](docs/guide/ci.md): anotações, `--since` e baseline.
|
|
200
|
+
prunr reports only `cancel`: it reads each key of the object, follows the use through
|
|
201
|
+
`invoices.x`, and the `require` built at runtime keeps the files in `handlers/` alive. When in
|
|
202
|
+
doubt (the module passed along, a computed key), it treats everything as used: it is better to
|
|
203
|
+
let a dead export through than to tell you to delete a live one.
|
|
138
204
|
|
|
139
|
-
|
|
205
|
+
This is not a hypothetical case. prunr was born in a production Node.js project, all in
|
|
206
|
+
CommonJS, that adopted Knip, fixed its entry points, deleted what it pointed out, and ended up
|
|
207
|
+
removing it because of too many false positives. In the same project, prunr pointed out 26 real
|
|
208
|
+
problems (dead files and exports, unused dependencies and a queue shutdown that was never
|
|
209
|
+
called), all of them fixed, and it now runs in CI with no pending findings.
|
|
140
210
|
|
|
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`.
|
|
211
|
+
**One tool in place of several.** Unused files, exports, imports and dependencies, undeclared
|
|
212
|
+
or misplaced dependencies and circular imports all come out of the same graph, in one command.
|
|
213
|
+
That is the work usually split across `depcheck`, `madge`, `unimported` and custom scripts.
|
|
147
214
|
|
|
148
|
-
|
|
215
|
+
**Fast, and it runs nothing.** It is a native binary, written in Rust, that reads and resolves
|
|
216
|
+
files in parallel; the npm package only downloads the binary for your platform, with no install
|
|
217
|
+
script. On a monorepo of 13,000 files, the check takes 0.6 s. Tool configs (Jest, Vitest,
|
|
218
|
+
Playwright, Vite...) are read as text, never executed.
|
|
149
219
|
|
|
150
|
-
|
|
220
|
+
**Built to adopt on existing code.** A baseline so you do not have to fix everything at once,
|
|
221
|
+
`--since` and diff annotations to block only what is new in the PR, and a `prunr fix` that
|
|
222
|
+
deletes dead code and cleans up `package.json`.
|
|
151
223
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
224
|
+
**When Knip is the better choice.** On a project that is only ESM and TypeScript, Knip has many
|
|
225
|
+
more plugins (prunr knows about 40 tools and frameworks; Knip, over a hundred) and more mileage. prunr pays off when there is CommonJS in the mix, or when speed
|
|
226
|
+
and a single tool matter more.
|
|
155
227
|
|
|
156
|
-
##
|
|
228
|
+
## Contributing
|
|
157
229
|
|
|
158
|
-
|
|
230
|
+
prunr is built with TDD and 100% test coverage. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
159
231
|
|
|
160
|
-
##
|
|
232
|
+
## License
|
|
161
233
|
|
|
162
234
|
[MIT](LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunr/prunr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.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.6.0",
|
|
30
|
+
"@prunr/darwin-x64": "0.6.0",
|
|
31
|
+
"@prunr/linux-arm64": "0.6.0",
|
|
32
|
+
"@prunr/linux-x64": "0.6.0",
|
|
33
|
+
"@prunr/win32-x64": "0.6.0"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/schema.json
CHANGED
|
@@ -18,6 +18,14 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"$ref": "#/$defs/Dependencies"
|
|
20
20
|
},
|
|
21
|
+
"plugins": {
|
|
22
|
+
"description": "Plugins turned off (`false`) or on (`true`) by name, whatever `package.json` declares:\neach one is turned on by the package it is named after.",
|
|
23
|
+
"type": "object",
|
|
24
|
+
"additionalProperties": {
|
|
25
|
+
"type": "boolean"
|
|
26
|
+
},
|
|
27
|
+
"default": {}
|
|
28
|
+
},
|
|
21
29
|
"baseline": {
|
|
22
30
|
"description": "The baseline file, relative to the root: findings the project already had. They do not\nfail the check, and `prunr baseline` rewrites the file.",
|
|
23
31
|
"type": [
|