agentleFS
suxrobGMsuxrobGM/jobpilot/.claude/skills/knip-cleanup/SKILL.md

knip-cleanup

Skill113 starsChanged 10 days agoMITView on GitHub

Add knip to a TypeScript project and use it to remove dead exports, collapse pass-through barrels, and narrow every export to what another file actually imports. Trigger on "add knip", "set up knip", "find dead code", "remove unused exports", "clean up barrel files", "get rid of export *", "why is this exported".

1 other project uses a version of this.

Adopted by

Most recently taken up by

  • Other versions1 project uses a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 1

    1. iwritec0de/app-dev★ 3same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repository9 other files it gives its agents.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 5 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not start at phase 3. A knip baseline full of false positives will delete something real.

    line 14
  2. Distinctive:

    If the user also reports line-ending churn on every format, check before changing anything:

    line 18
  3. Distinctive:

    Commit the config and the script separately from any deletion.

    line 69
  4. Distinctive:

    Verify with a lint run and hand-edit the mangled file rather than tuning the heuristic.

    line 93
  5. Very specific:

    Verify with a real build, not just typecheck.

    line 123
  6. Very specific:

    Leave the flag at false in the committed config so the rule holds for new code, and write it into the project's own rules file, e.g.:

    line 138
  7. Distinctive:

    After every phase, all of it, in this order — a later step catches what an earlier one cannot:

    line 148

≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Knip Integration and Barrel Cleanup

Four phases, each one committed and verified on its own. Later phases depend on earlier ones: every deletion exposes another layer of dead code, so knip is re-run between them rather than once at the start.

Do not start at phase 3. A knip baseline full of false positives will delete something real.

Phase 0: measure before believing the complaint

If the user also reports line-ending churn on every format, check before changing anything:

git ls-files --eol | awk '{print $1, $2}' | sort | uniq -c | sort -rn | head
<formatter> --check .    # biome check . / prettier --check .

i/lf w/lf across the board with a clean formatter run means the tracked tree is already normalized and the churn is in newly created files. The fix is prevention, not renormalizing: .editorconfig with end_of_line = lf, the editor's own EOL setting ("files.eol": "\n" for VS Code), and git config core.autocrlf false locally. Renormalizing a tree that is already LF produces a huge no-op diff and fixes nothing.

Phase 1: a baseline that is actually true

Install knip at the repo root, one config, one script. In a monorepo never give each package its own config: a per-workspace run cannot see that another workspace imports the symbol, so every shared export reads as dead.

{
  "$schema": "https://unpkg.com/knip@<major>/schema.json",
  "tags": ["-knipignore"],
  "workspaces": {
    "packages/lib": {
      "entry": ["src/index.ts", "src/**/*.test.ts"],
      "project": ["src/**/*.ts"],
      "includeEntryExports": true,
      "ignoreExportsUsedInFile": true
    }
  }
}

Then iterate until the report contains only real findings. Expect these, in this order:

  • Every compiled entrypoint must be listed. Grep the Dockerfiles, CI, and build scripts for --outfile, --entry, bun build, esbuild. A second binary built from src/worker.ts is invisible to knip and its whole dependency tree reports as unused.
  • Config files that read env at load time (prisma.config.ts and friends) crash knip's plugin loader. Disable that plugin ("prisma": false) rather than faking the variable.
  • includeEntryExports: true wherever package.json exports uses wildcards. A map like "./utils/*": "./src/utils/*.ts" makes every file an entry, and knip then reports nothing at all for that package. Verify by adding a deliberately unused export and confirming knip sees it — silence is the failure mode here, so it will not announce itself.
  • Framework-indirect dependencies (@prisma/client, pg, a JWT lib reached through a plugin) go in ignoreDependencies. Check each one is genuinely reached before ignoring it.
  • Type-level assertions are not dead code. A type XInSync = [Assert<A, B>, Assert<B, A>] drift guard has no runtime caller by design. Tag it @knipignore (with "tags": ["-knipignore"] in the config) and say in a comment what it guards. Deleting it silently removes a check.

Commit the config and the script separately from any deletion.

Phase 2: dead exports, before touching barrels

Pure subtraction, no import churn, so it lands cleanly and reviews easily.

Cross-check every finding against a repo-wide grep before deleting. files=1 refs=1 (the declaration is the only occurrence) is safe. More than that needs a look, and the reasons split into four:

What grep showsWhat it meansWhat to do
Only the declarationTruly deadDelete
A same-named declaration in another workspaceDuplicate, this copy is deadDelete this one
Hits only in a barrel's re-export lineThe forwarding line is dead, not the symbolHandle in phase 3
Hits in a commentNot a referenceDelete

Then delete, and re-run knip after deleting: removing a function usually orphans the data tables and helper types it used, which were invisible while it existed.

Two traps when automating the deletion:

  • Deleting a declaration by brace balance breaks on chained expressions (export const x = new Builder().use(a).derive(b)) — the balance returns to zero mid-chain. Verify with a lint run and hand-edit the mangled file rather than tuning the heuristic.
  • Deletions strand imports and module-level state. Run the formatter's unsafe autofix (biome check --write --unsafe, eslint --fix) on the touched files, then read what it could not fix.

Phase 3: barrels, by whether they earn their keep

State the rule before writing any code, because it decides the size of the diff. A barrel stays only when it aggregates more than one file and has more than one importer outside its own directory. Everything else is indirection: export *, single-file forwarders, and barrels with one caller.

Classify first and show the counts — the split is usually lopsided and tells you where the real work is. In one monorepo it was 168 barrels: 74 export *, 75 named, 18 with real code; after the rule, all 45 survivors were frontend component aggregators and the backend kept none.

Two barrels must never be deleted:

  • Anything package.json exports points at. Deleting src/utils/index.ts when the map says "./utils": "./src/utils/index.ts" breaks every consumer, and typecheck inside that package still passes — the break only shows up in the packages that import it. export * is defensible in exactly this position, since the subpath is the unit of API.
  • A barrel that a surviving barrel re-exports from. Repoint the parent's export ... from lines at the concrete files first, then drop the child.

references/debarrel.py does the rewrite: it builds an export table for every file, follows re-exports transitively to the declaring module, rewrites importers, and refuses to delete a barrel anything still references. Run it in batches by directory, and run it twice — a nested barrel is still referenced by its parent until the parent is processed.

Verify with a real build, not just typecheck. Barrel changes affect bundling and, in Next.js, server/client boundaries.

Phase 4: export only what is needed

Flip ignoreExportsUsedInFile to false. What it reports is every symbol exported but never imported anywhere else — the actual answer to "why is this exported".

Strip the export keyword from those declarations. Two guards:

  • Skip .test.ts files. Tests are entries; their exports are reported but harmless.
  • Never demote a destructured declaration. export const { a, b } = createThing() cannot lose export for one member, and knip reports members individually. Restore the keyword and tag the declaration @knipignore with a comment saying the members are destructured together.

Leave the flag at false in the committed config so the rule holds for new code, and write it into the project's own rules file, e.g.:

Export only what another file imports. A helper used solely inside its own module stays unexported. A barrel is justified only when it carries a directory's public API for several outside importers — never as a pass-through for a single file, and always named re-exports rather than export *.

Verification gate

After every phase, all of it, in this order — a later step catches what an earlier one cannot:

<formatter> check --write .   # then read what it refused to fix
<typecheck across all workspaces>
<tests in every workspace that has them>
<a real production build>
<knip>                        # deletions expose the next layer

Typecheck alone is not enough: it passes on a deleted package entry point, and it cannot see a bundler boundary.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.