Comment by rickcarlino

Comment by rickcarlino 16 hours ago

6 replies

Will it remove exports that are only imported for the sake of testing? Eg: it is only imported by files ending in .test.ts or with __test__ as a parent directory?

I’ve tried tools like this in the past within projects that have high test coverage but I have never had any luck because of this edge case.

kazushisan 15 hours ago

I may be opinionated but I believe that the best practice is to configure a separate tsconfig for test files with project references. As long as the test files are not included in the tsconfig passed to ts-remove-unused, it should remove exports that are only used in test files.

https://www.typescriptlang.org/docs/handbook/project-referen...

  • rickcarlino 13 hours ago

    In the JavaScript API, it would be nice if there was the ability to have custom comparative functions for this sort of use case. A user defined function that gets called right before final output which allows the user to create custom filtering rules and return a boolean to indicate inclusion or exclusion. I could see this being useful not only for the case I just presented, but also other cases such as NextJS projects that export default functions in pages that are never imported elsewhere in the project.

    • kazushisan 12 hours ago

      I’ve been using it for my next.js project and passing something like `/pages\//` to `skip` was good enough for me… Is there something I’m missing out?

  • 3523582908 10 hours ago

    hey! I was wondering if you'd be willing to explain what project references are and why you would do test config this way?

rty32 15 hours ago

I think this can get very nuanced --

If you are providing a library, it's possible you are exporting a function that is meant to be used by downstream code, and that function is isolated from other parts of the code (so never used by other functions but only tests)

If you are writing "product" code, most likely this is just dead code. But there are also edge cases where a function is used as the entry point for other code outside the current repository etc.

Put it this way -- if you are given a codebase you have never seen before, and you see a function only imported by test on the first day. Would you remove it without hesitation? Probably not.

I feel this is likely something that must require human experience to be done "correctly".

  • rickcarlino 13 hours ago

    A custom comparative function that returns true or false based on the special requirements of the project might fix that. I don’t think the project supports this based on a quick look at the documentation but I would love to see this added as a feature.