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

Object property enumeration methods used on enum reporting as unused exported enum members #699

Open
shawnmcknight opened this issue Jun 25, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@shawnmcknight
Copy link

I've created a reproduction at https://github.com/shawnmcknight/knip-enum-object-values.

In this scenario, there is an enum with initializers:

export enum Fruits {
  apple = "apple",
  orange = "orange",
}

That enum is imported and run through any of the Object property enumerators:

const fruitKeys = Object.keys(Fruits);
const fruitValues = Object.values(Fruits);
const fruitEntries = Object.entries(Fruits);

With this code, each enum member is reported as unused by knip, but effectively every enumerated value is being used. I'm not sure I would consider Object.keys to be "using" the enum member, but both Object.values and Object.entries are consuming the entirety of the enumeration.

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 25, 2024
@webpro
Copy link
Collaborator

webpro commented Jun 26, 2024

Knip doesn't really support any kind of dynamics like the one reported. It's basically mostly just direct references or "dotted" references (for e.g. namespaces, enum members).

However, perhaps we could such handle situations a bit like https://knip.dev/guides/namespace-imports and when an enum (or namespace) is used as an argument in eg. Object.keys and perhaps other enumerations/iterations we consider them all "referenced".

@shawnmcknight
Copy link
Author

This situation does sound a lot like the namespace example. As noted there, you don't really know if the namespace is being used by send, but more than likely you want to consider it to be completely referenced. This situation is pretty much the same -- its unknown whether each enum member is actually being used, but you probably don't want it to be reported.

To elaborate a bit on where this presented itself, we have scenarios where the enum members needed to be turned into an array of strings for validation purposes (e.g. mongoose schema enum constraints). The enum members might not need to be referenced directly since they might simply flow in and out as the initialized strings. The enum makes a nice container for these values and when necessary, individual enum members could be referenced, even though they all don't have to be.

@muuvmuuv
Copy link

muuvmuuv commented Jul 8, 2024

I have the exact same situation and one more where it is used as a type only:

export enum Gender {
	MALE = 'male',
	FEMALE = 'female',
	DIVERS = 'divers',
	ANY = 'any',
}

export const genders = Object.values(Gender)

export interface Profile {
	gender?: Gender
}

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

3 participants