-
-
Notifications
You must be signed in to change notification settings - Fork 179
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
Resolve JS files referenced by TypeScript declaration files if possible #503
Resolve JS files referenced by TypeScript declaration files if possible #503
Conversation
Thanks @junyi! Feel free to open a pull request. Looks like you know where to look for 👍 Would be awesome! One note: please delete any files that are not relevant to the issue. |
When can we expect this merged and released into knip? I'm having the same issue and for the time being I've mitigated this by adding the javascript file to the configuration ignore list. |
6a8e6a1
to
6a1a2cd
Compare
6a1a2cd
to
b521b15
Compare
Hi @webpro, thanks for the advice! I have updated the PR to include a fix for the module resolution and removed unnecessary files from the tests. |
Thanks for the fix! Interestingly, knip reports this issue in its own repo:
I'll dig some deeper when I have time. |
OK I think I've figured out why it didn't pass in the Knip repo itself, and I've also added the other DTS extensions during TS init. @junyi Could you please format the changes ( |
e99baaa
to
c54d644
Compare
The changes "broke" tests, they're fixed. If you could wrap it up when/however you like (so you're the contributor of it all) I'm happy to merge! |
Thank you for looking into this @webpro! I have pushed a commit to format the changes. Let me know if there's anything else I should do to wrap this up |
🚀 This pull request is included in v5.0.4. See Release 5.0.4 for release notes. |
This doesn't seem to cover the case where the JavaScript file's extension is |
That's right. It also doesn't cover entry files in certain cases. Feel free to open a PR! |
This should be OK in the latest |
Context
First of all, thanks for developing this tool!
I am looking into using this tool at work and am running into a false positive scenario where JavaScript files which are referenced by TypeScript declaration files (
.d.ts
) are showing up as unused.A summary of the issue looks like this:
I have included in the PR here test & fixtures to describe the issue above.
I believe this has something to do with this part of the code in
resolveModuleNames
:https://github.com/webpro/knip/blob/0b02923352940b3129ea8bb411b46e5804b2ce0a/packages/knip/src/typescript/resolveModuleNames.ts#L62-L71
Approach
Create a new function
jsMatchingDeclarationFileExists
where given a TypeScript declaration file (.d.ts
), it checks if there exists a file by replacing the extension to.js
and returns the module.Note: The same behaviour is done for
.d.mts
to.mjs
and.d.cts
to.cjs
but I am not sure if this is necessary.This function is used to augment the existing behaviour in
resolveModuleNames
as follows:https://github.com/webpro/knip/blob/b521b156c7c805c62e17643172634b52e6081def/packages/knip/src/typescript/resolveModuleNames.ts#L86-L101