Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find dead JavaScript code?

We are refactoring a legacy web app and as a result are "killing" quite a lot of JavaScript code but we're afraid of deleting what we think is dead code due to not being sure. Is there any tool / technique for positively identifying dead code in JavaScript?

like image 643
JoelFan Avatar asked Nov 09 '10 17:11

JoelFan


People also ask

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.

Where is the unused code in VSCode?

It is currently not possible to detect unused public methods in VSCode (you can check for updates here). However, if it's a private method, you can mark it as private and VSCode is able to look for whether it is used or not within the scope (although do remember: methods used in the HTML template are public).


1 Answers

Without looking for anything too complex:

  • JSLint (not really a static analyzer, but if you give it your concatenated development code, you'll see what methods are never called, at least in obvious scoping contexts)
  • Google Closure Compiler
  • Google Closure Linter
like image 140
haylem Avatar answered Oct 19 '22 00:10

haylem