How to find unused/dead code in web projects (90%

2019-02-21 15:06发布

问题:

I did find a very interesting tool for identify unused css definitions in a web project. http://www.sitepoint.com/dustmeselectors/

Are there similar tools also for javascript projects?

P.S. I know there is no program for deterministically finding unused code. But I am looking for a report to identify possible unused code. Then the last decision will always be your own.

回答1:

Problem is there is no way to be really sure. Suppose the following:

  1. The initial HTML site is practically empty. There is a lot of JS code though, which seems to be unused.
  2. OnLoad, a function is called which launches an AJAX query to the server. The server returns a lot of HTML code, which is the body of the site. This body contains lots of JavaScript functions.
  3. The initial body is replaced with the body received via AJAX. Suddenly, all code is used.

Static analysis utilities are therefore useless. I do not know whether there exists a browser extension that marks all JS usage from a running browser though.



回答2:

You can try using tombstones to safely locate and remove dead code from your JavaScript.

https://blog.bugsnag.com/javascript-refactoring-with-bugsnag-and-tombstones/



回答3:

The one that comes to mind most quickly is Javascript LINT (http://www.javascriptlint.com/) and JSLint (http://www.jslint.com/).

Beware though: the latter hurts your feelings.