webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
7.06k stars 177 forks source link

💡 (TypeScript plugin): Add references path to config #814

Closed taro-28 closed 4 weeks ago

taro-28 commented 1 month ago

Suggest an idea for this project

Hello!

I encountered a issue where the dependencies used in tsconfig specified in the references of tsconfig are detected as unused dependencies.

Reproduction

https://stackblitz.com/edit/knip-typescript-references?file=tsconfig.json

❯ npm run knip

> knip
> knip

Unused devDependencies (1)
@tsconfig/node22  package.json

Suggestion

I thought I could just add the references path to the config file path in the TypeScript plugin's resolveConfig as well as extends.

https://github.com/webpro-nl/knip/blob/c9afd52e4e778b57caa2b811798851c794a7df92/packages/knip/src/plugins/typescript/index.ts#L24-L26


I will create a PR if the above suggested methods are acceptable!

webpro commented 1 month ago

Thanks for catching this, nice.

I did just quite the refactoring last week and now there's toConfig indeed to mark such references as configuration files.

There's just one catch: path value can be a reference to a folder as well, and Knip doesn't resolve that to the tsconfig.json file inside that folder. So I think we can just filter out everything that doesn't end with .json. I had the same issue when parsing the -p/--project argument on the command-line here: https://github.com/webpro-nl/knip/blob/c9afd52e4e778b57caa2b811798851c794a7df92/packages/knip/src/plugins/typescript/index.ts#L45

We could actually resolve a specifier like ./some/workspace to that workspace's tsconfig.json file, but my assumption is that the file is already handled in that workspace.

A PR would be greatly appreciated!

taro-28 commented 4 weeks ago

Thank you for your reply.

So I think we can just filter out everything that doesn't end with .json. I had the same issue when parsing the -p/--project argument on the command-line here

I see, I hadn't noticed this, so it was helpful to have your comment in advance.

my assumption is that the file is already handled in that workspace.

I agree with you.

I will now try to make a PR!