Tslint: Feature Request: Dead Code Detection

Created on 6 Nov 2015  ·  17Comments  ·  Source: palantir/tslint

Probably relies on https://github.com/palantir/tslint/issues/680 being a thing, but it would be fantastic to specify package entry points (E.g. the bin file, the main file), a glob of files to test and detect dead code paths. For example, unused exports throughout the project, or just completely unused files.

Accepting PRs Needs Proposal Rule Suggestion

Most helpful comment

I could see this being a core rule now (albeit, potentially expensive to run). I like the idea in #2579 of calling it no-unused-export.

All 17 comments

I have already made such script (not tied to TSLint). Would it be useful?

@louy Certainly, I'd love to see it! Thanks :smile:

I've put together an npm module for this.
https://github.com/louy/find-orphans/blob/master/README.md

I'll add more options later if I see demand for it. Right now it should work with typescript out of the box.

TSLint doesn't do any module resolution -- it just does static analysis right now. So this is likely out of scope for us. It's a neat feature though; I'll leave this open for discussion.

This can likely be closed now due to the TS 2.x "allowUnreachableCode": false compiler option

@chrismbarr That's a different feature. It only handles unreachable code and definitely does not detect dead code (either in the current file or across the project). Dead code detection would tell you things like "variable x is not used", while project-wide dead code detection would go beyond that and tell you things like "the exported variable x is not used". FWIW, the TypeScript option for noUnused{Parameters,Locals} get closer to dead code detection than unreachable code does. They are much different features.

@adidahiya Would this be a more possible feature now that the type checker exists? I'd understand if it needs to be an external rule, but it would be valuable.

Would love that too. In my use case, we have to refactor a huge code base and this would be immense helpful saving me at least a day trying to find that myself. Imagine what I can do with one more day ;)

I could see this being a core rule now (albeit, potentially expensive to run). I like the idea in #2579 of calling it no-unused-export.

Could this be as simple as finding all identifiers with 0 external references in a given TS project?

Re. my last comment, here's a short little script (albeit quite slow) that does that: https://mobile.twitter.com/OliverJAsh/status/1060204943594926081

Now to incorporate into a TSLint rule somehow. Can anyone provide some guidance on this? 😄

Nifty, thanks for linking that @OliverJAsh!

https://github.com/bcherny/tslint-no-circular-imports/ might be a good start. It shows how to create a dependency graph through the project.

Till this gets made into a feature, I've got something that does it as a small CLI: https://github.com/nadeesha/ts-prune

@nadeesha actually checked out your cli, works like a charm, thanks!

Cool - given the external ts-prune and upcoming deprecation of TSLint (#4534), closing this issue here for now. Thanks folks!

find-unused-exports was recently published, A Node.js CLI and equivalent JS API to find unused ECMAScript module exports in a project.

It's not specific to TypeScript; it parses project files using Babel (respecting your project’s Babel config). It supports ignoring specific exports known to be unused (e.g. the default export for a Next.js page) via ignore comments, making the tool fit for use in a test script for CI. It has been a bit of a game changer at work; reviewing PRs is less taxing now we don't have to do a lot of manual scanning to verify there are no orphaned or unused exports. It's very fast too. The npx find-unused-exports script is handy for finding redundant code to remove in legacy projects.

find-unused-exports CLI screenshot

🤖 Beep boop! 👉 TSLint is deprecated 👈 and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

Was this page helpful?
0 / 5 - 0 ratings