'How to detect dead or unused code in Angular?
I and my team are working on an angular application from more than a year now. As part of our code refactoring process we want to delete unused or dead code from our repo.
Is there any technique or tool which automatically detects this unused code from our repo, the it would be helpful for us to it instead of wasting a lot of time and efforts detecting them manually?
Solution 1:[1]
For VS Code you can use this: https://eslint.org/docs/rules/no-unused-vars
What it does
: Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.
There isn't something that delete the code for you, but I think this is a good extension to start with.
If you use sublime there is nothing since is just a text editor
Solution 2:[2]
Thats very simple. if you run your application with " ng serve --aot "
cli command instead of " ng serve "
, it shows you a hole number of these dead codes(like when you leave " ng build --prod "
command).
Solution 3:[3]
I found this approach that removes the unused code when bundling, is not exactly what you and I are looking for but it might help
Tree shaking is an algorithm introduced first by RollupJS and also implemented by Webpack 2 that removes any unused code when bundling your code. It relies on ES2015 modules in order to achieve that.
https://alexjover.com/blog/tree-shaking-with-webpack-2-typescript-and-babel/
Solution 4:[4]
WebStorm can find dead code.
In the menu bar select Code > Inspect Code...
:
In the report that it generates, drill down into JavaScript and Typscript > Unused symbols
:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jacopo Sciampi |
Solution 2 | Mostafa Saadatnia |
Solution 3 | Mauricio Gracia Gutierrez |
Solution 4 |