如何从 Javascript 中删除死代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6730358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:39:53  来源:igfitidea点击:

How to remove dead code from Javascript

javascriptdead-code

提问by Yuvals

I am trying to remove unused functions from my project. Since it has thousands of lines, this takes forever.

我正在尝试从我的项目中删除未使用的功能。由于它有数千行,这需要永远。

Code coverage tools may suggest functions that are not used in a given test case, but it may be dangerous to rely only on this.

代码覆盖工具可能会建议给定测试用例中未使用的函数,但仅依赖于此可能会很危险。

Is there another tool which can help with this?

有没有其他工具可以帮助解决这个问题?

回答by daveoncode

You can try Google Closure compiler. It has different settings and you can also remove dead code automatically. You can try it here: http://closure-compiler.appspot.com/home

你可以试试 Google Closure 编译器。它有不同的设置,您还可以自动删除死代码。你可以在这里尝试:http: //closure-compiler.appspot.com/home

For more info read here: http://code.google.com/closure/compiler/docs/compilation_levels.html

有关更多信息,请阅读此处:http: //code.google.com/closure/compiler/docs/compilation_levels.html

回答by Nico Huysamen

Have a look at thisquestion regarding Javascript static analysis tools.

看看这个关于 Javascript 静态分析工具的问题。