New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-exports with usage of barrel files isn't indexed correctly #626
Comments
This is an interesting case, could be a challenge to resolve it. Assuming the intention of And I'm afraid this is a "bridge too far" currently for Knip, the chain breaks here: import * as orderFixtures from './orderFixtures';
export const allFixtures = { orderFixtures }; Interestingly, trying "Find all references" in my IDE on the The issue here is that it's a property on a new export. It's not an explicit or even implicit re-export: // explicit
export * from './orderFixtures';
// implicit
import * as orderFixtures from './orderFixtures';
export { orderFixtures }; This kind of works, would come close: import * as orderFixtures from './orderFixtures';
export { orderFixtures as allFixtures }; Obviously not what you want, but just to show what Knip does support technically. In this last case all exports on the Your use case is totally valid JS/TS, it's just not something I see easily fixed in Knip. If you're interested in working on improving this last case I'm happy to elaborate on the whereabouts of related things :) |
Couldn't resist and went ahead on this myself. This use case should be fixed in v5.14.0 and should properly handle exports individually (before, it assumed all members of the namespace as referenced): import * as orderFixtures from './orderFixtures';
export { orderFixtures as allFixtures }; During the refactor I went ahead and tried whether supporting the original use case is feasible. I've added support in npm install -D knip@keyedreexport Happy to hear your feedback, @glemiron! |
From TS compiler internals Discord:
|
Unfortunately it seems that the fix doesn't work. In order to eliminate set up problems I've created a demo repository here. I also found the second use case, I think it's related and you can also check out it in the repository. And another observation: WebStorm is detects unused code correctly, so it seems as solvable problem. |
Thanks for the demo repo! This is super helpful. Let's break it down a little bit.
import * as orderFixtures from './orderFixtures'
export const allFixtures = { orderFixtures: orderFixtures }; // NOT supported (yet?)
export const allFixtures = { orderFixtures }; // Supported in @keyedreexport To this I mentioned that I was "assuming
import * as orderFixtures from './orderFixtures';
export { orderFixtures as allFixtures };
import * as orderFixtures from './orderFixtures'
export const allFixtures = orderFixtures While valid, but this could also (should?) be written like so: import * as orderFixtures from './orderFixtures'
export { orderFixtures as allFixtures } This case worked fine in main prior to opening this issue. Or maybe there's additional code/syntax missing that we've not seen/covered yet?
import { ValuesType } from 'utility-types'
import * as FEATURES from './constants'
export type Feature = ValuesType<typeof FEATURES> This is a case that wasn't covered yet and is now added in v5.15.1.
Interesting, didn't know about this feature. Do you mean this Find unused code with coverage? That would be a bit different: Knip is a static analysis tool, and I think this feature is instrumenting code and then doing the analysis more dynamically in runtime. That said, it's a solvable problem indeed. For now I'm going to keep it short here by saying that doing "Find all references" for each export takes too long in large projects (also see Slim down to speed up), but this can still be enabled by using |
I found an interesting edge case that could be improved.
Then it'll be used in test the following way:
For this configuration I expect that knip will tell me that I have unused export in
fixture2.ts
, but instead I getting the following result:I'm considering to contribute to the project, but I don't sure what kind of test should be added here and how to name it. I believe that it related to modules resolution, but some guidance could help.
Thank you for the awesome work!
Here the minimal reproduction:
ReactPlayground.zip
The text was updated successfully, but these errors were encountered: