这是我参与11月更文挑战的第11天,活动详情查看:2021最后一次更文挑战
Eslint 是什么
编码规范
每个程序员都有自己的编码习惯,最常见的莫过于:
- 有的人写代码一行代码结尾必须加分号
;
,有的人觉得不加分号;
更好看; - 有的人写代码一行代码不会超过 80 个字符,认为这样看起来简洁明了,有的人喜欢把所有逻辑都写在一行代码上,觉得别人看不懂的代码很牛逼;
- 有的人使用变量必然会先定义
var a = 10;
,而粗心的人写变量可能没有定义过就直接使用b = 10;
;
Lint 的含义
如果你写自己的项目怎么折腾都没关系,但是在公司中老板希望每个人写出的代码都要符合一个统一的规则,这样别人看源码就能够看得懂,因为源码是符合统一的编码规范制定的。
那么问题来了,总不能每个人写的代码老板都要一行行代码去检查吧,这是一件很蠢的事情。凡是重复性的工作,都应该被制作成工具来节约成本。这个工具应该做两件事情:
- 提供编码规范;
- 提供自动检验代码的程序,并打印检验结果:告诉你哪一个文件哪一行代码不符合哪一条编码规范,方便你去修改代码。
Lint 因此而诞生。
现在讲述一下eslint生成报告的详细方式和步骤:
首先安装eslint
npm i -g eslint
然后在终端输入以下命令
eslint -f compact "src"> results.txt
这是将src下的文件检查后导出到results.txt文件中。如果有如下这样的报错,就检查一下.eslintignore文件。
You are linting "src", but all of the files matching the glob pattern "src" are ignored.
If you don't want to lint these files, remove the pattern "src" from the list of arguments passed to ESLint.
If you do want to lint these files, try the following solutions:
* Check your .eslintignore file, or the eslintIgnore property in package.json, to ensure that the files are not configured to be ignored.
* Explicitly list the files from this glob that you'd like to lint on the command-line, rather than providing a glob as an argument.
具体参考链接: