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

Resolve JS files referenced by TypeScript declaration files if possible #503

Merged
merged 6 commits into from
Mar 12, 2024

Conversation

junyi
Copy link
Contributor

@junyi junyi commented Feb 8, 2024

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:

// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src"
  }
}
// This is index.ts
// This imports from './utils/fn.js' which has a corresponding './utils/fn.d.ts'
// but Knip is reporting './utils/fn.js' as unused
import { g } from 'utils/fn';

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

@webpro
Copy link
Collaborator

webpro commented Feb 9, 2024

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.

@litera
Copy link

litera commented Feb 9, 2024

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.

@junyi junyi force-pushed the junyi/knip-typescript-declaration branch from 6a8e6a1 to 6a1a2cd Compare February 13, 2024 01:36
@junyi junyi changed the title Add test where JS files referenced by TypeScript declaration files should not be marked as unused Resolve JS files referenced by TypeScript declaration files if possible Feb 13, 2024
@junyi junyi force-pushed the junyi/knip-typescript-declaration branch from 6a1a2cd to b521b15 Compare February 13, 2024 01:41
@junyi
Copy link
Contributor Author

junyi commented Feb 13, 2024

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.

@webpro
Copy link
Collaborator

webpro commented Feb 16, 2024

Thanks for the fix! Interestingly, knip reports this issue in its own repo:

Unused files (1)
packages/eslint-config/index.d.ts

I'll dig some deeper when I have time.

@webpro
Copy link
Collaborator

webpro commented Mar 12, 2024

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 (npm run format from root or workspace)?

@webpro webpro force-pushed the junyi/knip-typescript-declaration branch from e99baaa to c54d644 Compare March 12, 2024 07:23
@webpro
Copy link
Collaborator

webpro commented Mar 12, 2024

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!

@junyi
Copy link
Contributor Author

junyi commented Mar 12, 2024

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

@webpro webpro merged commit 11d1555 into webpro-nl:main Mar 12, 2024
8 of 11 checks passed
@webpro
Copy link
Collaborator

webpro commented Mar 12, 2024

🚀 This pull request is included in v5.0.4. See Release 5.0.4 for release notes.

@alexeyr-ci
Copy link

This doesn't seem to cover the case where the JavaScript file's extension is .jsx.

@webpro
Copy link
Collaborator

webpro commented Mar 13, 2024

This doesn't seem to cover the case where the JavaScript file's extension is .jsx.

That's right. It also doesn't cover entry files in certain cases. Feel free to open a PR!

@webpro
Copy link
Collaborator

webpro commented Mar 18, 2024

This doesn't seem to cover the case where the JavaScript file's extension is .jsx.

This should be OK in the latest canary (v5.1.3-canary.0)

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

Successfully merging this pull request may close these issues.

4 participants