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

Find unsed methods of objects #274

Closed
lo1tuma opened this issue Sep 29, 2023 · 3 comments
Closed

Find unsed methods of objects #274

lo1tuma opened this issue Sep 29, 2023 · 3 comments
Labels
feature request Feature request

Comments

@lo1tuma
Copy link

lo1tuma commented Sep 29, 2023

I would like to see a possibility to track and find unused methods on exported objects, or objects created by exported functions.

Use Case:

Most of the time I wrap modules with side-effects in a factory function in order to do dependency-injection, e.g.

export function createMyModule(dependencies: MyDependencies): MyModule {
    const { logger, httpClient } = dependencies;

   return {
      async firstMethod() {
          logger.info('first');
          await httpClient.get('http://example.com')
      },

      async secondMethod() {
           logger.info('second');
          await httpClient.post('http://example.com')
      }
   }
}

Let’s say secondMethod is never used within my project, I want it to be flagged.

I know that this cannot be achieved in all circumstances, for example when the project is a library and those methods might by consumed by the consumers. But for app projects this is not an issue and such dead code be detected reliably. Therefore I think this feature should be disabled by default and needs to be opt-in.

@lo1tuma lo1tuma added the feature request Feature request label Sep 29, 2023
@webpro
Copy link
Member

webpro commented Oct 3, 2023

Knip supports namespaced/star imports and class members, so some of the semantics are probably similar.

Returned values (e.g. objects) created by exported functions is something Knip currently does not analyze. Not sure whether this could be implemented reliably. How to decide whether to include such a function?

Things combined, this feature might have noticeable to significant effects to performance. So yeah, opt-in as you suggest would probably make sense.

Feel free to dive into this and open a pull request, though (or anyone else). This feature request is currently not high on my priority list, but willing to assist with/look into a PR.

@lo1tuma
Copy link
Author

lo1tuma commented Oct 6, 2023

How to decide whether to include such a function?

Maybe we could simple check every function or method that end up to be exported somehow. Or if it is too hard to distinguish between exported and non-exported functions we could simply do it fall all function definitions. Although non-exported unused functions are already flagged by typescript itself and a bunch of ESLint rules. But non of those tools help to detect unused functions that are exported.

@webpro
Copy link
Member

webpro commented Nov 8, 2023

I'm going to close this issue. If anyone wants to pick it up, feel free to reach out and we can look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request
Projects
None yet
Development

No branches or pull requests

2 participants