找到死 JavaScript 代码?

发布于 2024-10-01 23:50:53 字数 113 浏览 13 评论 0原文

我们正在重构一个遗留的 Web 应用程序,结果“杀死”了相当多的 JavaScript 代码,但我们担心由于不确定而删除我们认为是死代码的内容。有没有任何工具/技术可以主动识别 JavaScript 中的死代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

尘世孤行 2024-10-08 23:50:53

无需寻找任何太复杂的东西:

Without looking for anything too complex:

薄荷梦 2024-10-08 23:50:53

您可以使用 deadfile 库:
https://m-izadmehr.github.io/deadfile/

它可以简单地找到未使用的文件,在任何 JS 项目中。

无需任何配置,它支持 ES6、JSX 和 Vue 文件:
输入图像描述这里

You can use deadfile library:
https://m-izadmehr.github.io/deadfile/

It can simply find unused files, in any JS project.

Without any config, it supports ES6, JSX, and Vue files:
enter image description here

尘世孤行 2024-10-08 23:50:53

grep。用它来查找函数调用。假设您有一个名为 dostuff() 的方法。在项目的根目录中使用 grep -r "dostuff()" * --color 。除非您发现定义以外的任何内容,否则您可以安全地删除它。

ack 也是 grep 的一个著名替代品。

There's grep. Use it to find function calls. Suppose you have a method called dostuff(). Use grep -r "dostuff()" * --color on your project's root directory. Unless you find anything other than the definition, you can safely erase it.

ack is also a notable alternative to grep.

岁月打碎记忆 2024-10-08 23:50:53

JetBrains 的 WebStorm IDE 可以突出显示项目中的死代码和未使用的变量。

WebStorm IDE from JetBrains can highlight deadcode and unused variables in your project.

傲鸠 2024-10-08 23:50:53

您可以将代码优化器用作 Google Closure 编译器,但它通常用于最小化代码。

function hello(name) {
alert('Hello, ' + name);
}

function test(){
alert('hi');
}

hello('New user');

将导致

alert("Hello, New user");

例如。

您可以做的另一件事是使用 Chrome 的开发人员工具(或 Firebug)来查看所有函数调用。在配置文件下,您可以看到哪些函数随着时间的推移被调用,哪些没有被调用。

DT 配置文件

You could use code optimizers as Google Closure Compiler, however it's often used for minimizing code.

function hello(name) {
alert('Hello, ' + name);
}

function test(){
alert('hi');
}

hello('New user');

Will result in

alert("Hello, New user");

For example.

Another thing you could do is to use Chrome's Developer Tools (or Firebug) to see all function calls. Under Profiles you can see which functions are being called over time and which are not.

DT Profiles

停滞 2024-10-08 23:50:53

Chrome 推出了新功能,可以让开发人员查看代码覆盖率,即执行了哪些代码行。

这当然不是一站式解决方案,但可以帮助开发人员获得代码见解。

检查此链接了解详细信息

作为 Chrome 59 版本的一部分推出

链接 了解详细信息sstatic.net/ruldR.png" rel="noreferrer">工具

Chrome has come up with new feature which lets developer see the code coverage, ie., which lines of codes were executed.

This certainly is not a one stop solution, but can extend a helping hand to developers to get code insights.

Check this link for details

Rolled as apart of Chrome 59 release

tools

栖竹 2024-10-08 23:50:53

如果你想自动化这个我会看看 https://github.com/joelgriffith/navalia,它公开了一个自动化 API 来执行此操作:

const { Chrome } = require('navalia');
const chrome = new Chrome();

chrome.goto('http://joelgriffith.net/', { coverage: true })
  .then(() => chrome.coverage('http://joelgriffith.net/main.bundle.js'))
  .then((stats) => console.log(stats)) // Prints { total: 45913, unused: 5572, 
  percentUnused: 0.12135996340905626 }
  .then(() => chrome.done());

更多信息:https://joelgriffith .github.io/navalia/Chrome/coverage/

If you want to automate this I'd take a look at https://github.com/joelgriffith/navalia, which exposes an automated API to do just that:

const { Chrome } = require('navalia');
const chrome = new Chrome();

chrome.goto('http://joelgriffith.net/', { coverage: true })
  .then(() => chrome.coverage('http://joelgriffith.net/main.bundle.js'))
  .then((stats) => console.log(stats)) // Prints { total: 45913, unused: 5572, 
  percentUnused: 0.12135996340905626 }
  .then(() => chrome.done());

More here: https://joelgriffith.github.io/navalia/Chrome/coverage/

丑疤怪 2024-10-08 23:50:53

我讨厌这个问题,并且没有好的工具来解决它,尽管 javascript 生态系统有大量的解析。正如另一个答案中提到的,deadfile 非常简洁,但我无法让它适用于我的代码库,它使用来自 src 目录的绝对导入。下面的 bash 足以了解是否有任何文件未在任何地方导入(我发现了一些!),这很容易手动验证。

for f in $(find src -name '*.js' | grep -E 'src/(app|modules|components).*\.js

我不关心测试/资源,因此应用程序|模块|组件位。字符串替换也可以被显着清理,但希望这对某人有用。

| grep -v '.test.js'); do f=${f/src\//}; f=${f/\/index.js/}; f=${f/.js/}; echo "$f imported in"$(grep -rl "$f" src | wc -l)" files" done

我不关心测试/资源,因此应用程序|模块|组件位。字符串替换也可以被显着清理,但希望这对某人有用。

I hate this problem, and that there are no good tools for solving it, despite the parse-heavy javascript ecosystem. As mentioned in another answer, deadfile is pretty neat, but I couldn't make it work for my codebase, which uses absolute imports from a src directory. The following bash was good enough to get an idea of whether any files weren't imported anywhere (I found some!), which was easily hand-verifiable.

for f in $(find src -name '*.js' | grep -E 'src/(app|modules|components).*\.js

I didn't care about tests/resources, hence the app|modules|components bit. The string replacements could be cleaned up significantly too, but hopefully this will be useful to someone.

| grep -v '.test.js'); do f=${f/src\//}; f=${f/\/index.js/}; f=${f/.js/}; echo "$f imported in"$(grep -rl "$f" src | wc -l)" files" done

I didn't care about tests/resources, hence the app|modules|components bit. The string replacements could be cleaned up significantly too, but hopefully this will be useful to someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文