使用 `--fix` 标志自动化 ESLint 有什么问题?

What problems are associated with automating ESLint with the `--fix` flag?

使用 --fix 标志自动化 ESLint 是一种好习惯,还是会在长期 运行 中产生问题?

例如,我在 "prestart" 脚本中使用 npm。这意味着 ESLint 运行s 每次我 运行 npm start 并修复我的代码中任何可修复的错误。这会导致什么问题?

这可能导致的唯一实际问题是在 npm start 上结束 运行ning 的代码可能与您编写的代码不同。在启动应用程序之前,您会让 ESLint 对代码进行更改,这可能会(可能)导致意外行为。这取决于您的项目是如何设置的以及您的应用程序是如何部署的。

list of ESLint rules the ones that can be autofixed have the wrench next to them; it can change more than just spacing and semicolons. While ESLint (when running with --fix) tries hard not to break your code functionally, any time you're manipulating your code in the time between writing it and running it you are putting yourself at risk of changing the behavior of your codebase. ESLint tries to ensure that they won't autofix your code in a way which will break/change the behavior of anything, but bugs/accidents can occur which may change your code enough to make it behave differently. As an aside, here's an interesting conversation on autofix and how it might change.

如果您 运行将 ESLint 作为 npm test 套件的一部分,并且除非您的 lint 规则通过,否则不会部署您的应用程序,这不是什么大问题(因为这不会这不会发生在您已部署的应用程序上,实际上只发生在本地;如果您的应用程序发现代码与您的风格指南不匹配,您将阻止您的应用程序部署。

否则,--fix 可以让人们按照自己的意愿编写代码,同时保持一致的风格并避免在代码库 semi-automatically 中出现 "problematic patterns"。虽然它不能解决所有规则,但它可以使团队的开发更加顺畅。如果需要,您甚至可以 运行 在 git pre-commit 挂钩中进行自动修复,以确保提交的代码是修复后的版本。

示例:Git pre-commit ESLint hook