Skip to content
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

Namespace created from exports reporting that the exports are unused when using namespace imports #690

Closed
shawnmcknight opened this issue Jun 21, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@shawnmcknight
Copy link

I've created a reproduction at https://github.com/shawnmcknight/knip-import-exported-ns.

The scenario I am seeing is that when a namespace is imported via import * as ns and that namespace is created via re-exporting other modules then there is a false positive on those re-exported modules being unused exports.

In my example reproduction, there are three subfolders as follows:

  • ns-not-reexported: This folder contains an index.ts file which directly exports two named exports - apple and orange.
  • ns-named-reexported: This folder contains files named apricot.ts and peach.ts which individually export named apricot and peach. These are then re-exported as named exports in the folder's index.ts.
  • ns-default-reexported: This folder contains files named blueberry.ts and pear.ts which export default blueberry and pear respectively. These default exports and then re-exported as named exports in the folder's index.ts.

In the root folder, there is an index.ts which imports each of these three subfolders using import * as ns syntax and then simply does a console.log of each of those. Based upon my understanding of the namespace import heuristic, this should result in no unused exports during knip's validation.

The namespace in the ns-not-reexported folder is not reported as an unused export. However, the namespaces in the ns-default-reexported and ns-named-reexported folders are reported as unused exports. It seems that the re-exporting process is causing those to be missed.

Here is the knip output from my reproduction:

Unused exports (4)
default  unknown  ns-default-reexported/blueberry.ts:2:8
default  unknown  ns-default-reexported/pear.ts:2:8
apricot  unknown  ns-named-reexported/apricot.ts:1:14
peach    unknown  ns-named-reexported/peach.ts:1:14

The apple and orange exports are properly identified as being used, but the other four are being flagged as unused.

Please let me know if there is anything else I can provide to assist!

@shawnmcknight shawnmcknight added the bug Something isn't working label Jun 21, 2024
@shawnmcknight
Copy link
Author

This is possibly related to #626, but I'm not totally sure.

@webpro
Copy link
Collaborator

webpro commented Jun 22, 2024

Thanks for reproduction, looks clear. There's quite a few cases covered in re-exports-* fixture folders, but apparently this one is missing indeed. To be continued.

@webpro
Copy link
Collaborator

webpro commented Jun 24, 2024

🚀 This issue has been resolved in v5.22.3. See Release 5.22.3 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

@webpro
Copy link
Collaborator

webpro commented Jun 24, 2024

Thanks again @shawnmcknight, did a refactor and it should behave better now!

@shawnmcknight
Copy link
Author

I just tested in my repo and it's working as expected now. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants