🐛 Unused `export * from "./b"` line not reported
Prerequisites
- [X] I'm using the latest version
- [X] I've read the relevant documentation
- [X] I've searched for existing issues
- [X] I've checked the list of known issues
- [X] I've read the issue reproduction guide
Reproduction url
https://codesandbox.io/p/devbox/wp5lv3
Reproduction access
- [X] I've made sure the reproduction is publicly accessible
Description of the issue
I found a situation in which an export * from "./b"; line is unused (removing it makes no difference), but Knip doesn’t report anything:
// a.ts
export const a = "a";
// b.ts
export const b = "b";
// aAndB.ts
export * from "./a";
export * from "./b"; // Unused line?
// index.ts
import { a } from "./aAndB";
import { b } from "./b";
console.log(a, b);
Note that both a and b are used, but b is imported directly rather than being imported from the barrel file. This is a sign of a potential issue, that maybe b was meant to be imported from aAndB instead.
It’s not a big issue, as there isn’t any actual code that is unused (except from the export * line which will most likely disappear after bundling anyway), and supporting this use case would maybe require a whole new category of things that can be potentially unused, but I felt like I should report it anyway if you feel like it should be fixed.
Oh, interesting. It's simply not supported yet, but I guess it could be. Won't be trivial at first thought though, because that * in the barrel file is not an exporteded identifier (yet).
Edit: and the * is also often not unique either (like in the example).
I have a similar issue:
// src/not-entrypoint/a.ts
export {X} from './b'
export {Y} from './c'
// src/not-entrypoint/b.ts
export const X = "X"
the file b.ts is actually unused and should be flagged by knip.
Is this the same issue or should I create a separate ticket?
Could be interesting to whip up a reproduction with the latest version of Knip. Recently I've added something that reports unused re-exports, maybe this is (part of) the solution?
Thank you! Indeed, I was running knip only with knip --files which did not show the unused exports. Here's the repro: https://stackblitz.com/edit/knip-case-repro-bkevakau?file=README.md
What knip --files does: report only unused files (not other issues/unused things, etc).
I upgraded now to run all knip checks and add some ignore rules instead 😊
This is one of the reasons why we should not use barrel files:
- https://tkdodo.eu/blog/please-stop-using-barrel-files
- https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/
- https://github.com/thepassle/eslint-plugin-barrel-files
But I agree it would be nice to have support for export from in general.
Closing this issue as part of a general cleanup to keep this project sustainable and optimize my time working on it. If you think this is inappropriate or if there is no workaround and you feel stuck, feel free to open a new issue. Thanks for your understanding.