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

🐛 False positive on Unused exported enum members when used with Object.values #927

Closed
6 tasks done
BreakBB opened this issue Jan 27, 2025 · 9 comments
Closed
6 tasks done
Labels
bug Something isn't working

Comments

@BreakBB
Copy link

BreakBB commented Jan 27, 2025

Prerequisites

Reproduction url

https://codesandbox.io/p/devbox/r3m3jy

Reproduction access

  • I've made sure the reproduction is publicly accessible

Description of the issue

Knip is reporting Unused exported enum members when an enum is used in Object.values and not all of the members are explicitly used somewhere else.

In the repo knip reports Test.SECOND to be unused, because it is only implicitly used in Object.values while Test.FIRST is fine, because it is explicitly used.

@BreakBB BreakBB added the bug Something isn't working label Jan 27, 2025
@webpro
Copy link
Collaborator

webpro commented Jan 27, 2025

This needs to be documented better, but currently it's expected behavior. If I'm not mistaken it currently works just like namespace imports (https://knip.dev/guides/namespace-imports), i.e. if there's even just a single explicit "property access" like Test.FIRST then all members are individually handled.

In the case of namespace imports we can disable that behavior (and have Knip always track all individual exports by including the nsExports/nsTypes issue type), but there's no such option yet for enum members.

I guess ideally there'd be three modes:

  • current heuristic
  • always consider all members used if there's one or more Object.values or other implicit member iteration/usage
  • always check all enum members individually regardless of implicit iterations

Happy to hear thoughts on this if you have any.

What you could already use is jsDoc tags and/or ignoreMembers to circumvent reports.

@BreakBB
Copy link
Author

BreakBB commented Jan 27, 2025

I forgot to mention this behavior was added/changed with fc5982e (in the latest release 5.43.3).

I guess my thoughts on this are rather simple as I am far from deep into this topic or similar ones regarding knip.

In my opinion knip should be as precise as possible. Since Object.values uses all enum members, I would expect them all to be marked as referenced. If there is no usage of Object.values (or similar interactions), then I would expect knip to check all members individually.

@webpro
Copy link
Collaborator

webpro commented Jan 27, 2025

I see. I think the behavior you expect by default makes sense. Still I'd like to offer the option to have Knip report unused members regardless of implicit member iteration/usage.

Also I don't like breaking changes so I'll work something out :)

@BreakBB
Copy link
Author

BreakBB commented Jan 27, 2025

Still I'd like to offer the option to have Knip report unused members regardless of implicit member iteration/usage.

Sounds more than fair to me. It's always good to have knobs to individualize things 👌🏻

Also I don't like breaking changes so I'll work something out :)

Neither do I. Especially not in a patch version release - hence my report 😬

@webpro webpro closed this as completed in d02db68 Jan 27, 2025
@webpro
Copy link
Collaborator

webpro commented Jan 27, 2025

Turns out, was basically a missing test I still had stashed.

@webpro
Copy link
Collaborator

webpro commented Jan 27, 2025

🚀 This issue has been resolved in v5.43.4. See Release 5.43.4 for release notes.

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

@webpro
Copy link
Collaborator

webpro commented Jan 27, 2025

A fix in two parts, really. Things should be alright in v5.43.5.

@BreakBB
Copy link
Author

BreakBB commented Jan 27, 2025

Pipeline is green again ✅

Thanks for these fast responses, awesome job @webpro

@webpro
Copy link
Collaborator

webpro commented Jan 27, 2025

Thanks for the report! That's a win-win.

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