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

Unused functions are not being detected #643

Closed
btd1337 opened this issue May 17, 2024 · 5 comments
Closed

Unused functions are not being detected #643

btd1337 opened this issue May 17, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@btd1337
Copy link

btd1337 commented May 17, 2024

Unused functions are not being detected.

How to reproduce:

  1. Create one file in any folder inside your project.
  2. Create three functions inside this file
// file: src/test.ts

// unused
export function foo() {
  bar();
}

// used by foo
export function bar() {
  console.log('bar'); 
}

// used by other file
export function zoo() {
  console.log('zoo');  
}
  1. import zoo function in other file

  2. Test if Knip detect this unused function

pnpm knip --include exports,types,nsExports,nsTypes

> project@9.1.0 knip /home/helder/project
> knip "--include" "exports,types,nsExports,nsTypes"

✂️  Excellent, Knip found no issues.

Testing with another package that Knip indicates is similar: ts-prune:

ts-prune | grep -v '(used in module)'

src/test.ts:1 - foo

Note that ts-prune detects the unused function, but Knip don't. Why doesn't Knip detect this?

@btd1337 btd1337 added the bug Something isn't working label May 17, 2024
@webpro
Copy link
Collaborator

webpro commented May 18, 2024

By default, Knip does not report unused exports in entry files.

@btd1337
Copy link
Author

btd1337 commented May 21, 2024

@webpro thank you for responding, but that is not the case. This is not about the entry point file. The pattern occurs within other files that are not the entry point file.

@webpro
Copy link
Collaborator

webpro commented May 21, 2024

The src/test.ts file can also be an entry file, depending on the enabled plugins. That's why I'd need a reproduction to properly look into it.

You can also use --debug to get more verbose output, maybe it clears things up.

@btd1337
Copy link
Author

btd1337 commented May 21, 2024

I gave a very simple example. Add any other file as an entrypoint besides this other file as an example that I mentioned above.

You should have as behavior, that the unused function will not be detected.

My case is that I have a very large project, in which Knip is not able to detect many unused functions, and ts-prune can.
I tried to demonstrate a basic example of how to reproduce, because I believe that Knip should be able to replace ts-prune.

@webpro
Copy link
Collaborator

webpro commented May 22, 2024

Sorry, but without a reproduction there's nothing for me to look into. I hope you understand I can't copy-paste everyone's "how to reproduce" dumps.

There's reasons Knip works the way it does by default. There's various ways Knip can be configured to make it behave as you need.

Maybe you're looking for flags like these:

@webpro webpro closed this as completed May 27, 2024
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