【笔记】Eslint学习笔记

前言

通过Eslint检查JS代码的格式,并自动修复

下载依赖

1
npm install -g eslint

初始化项目

1
npm init @eslint/config
1
eslint --init
1
2
3
4
5
6
7
✔ How would you like to use ESLint? · problems
✔ What type of modules does your project use? · esm
✔ Which framework does your project use? · none
✔ Does your project use TypeScript? · No / Yes
✔ Where does your code run? · browser
✔ What format do you want your config file to be in? · JavaScript
Successfully created .eslintrc.js file in demo
  • 初始化项目后会在项目根目录生成.eslintrc.js配置文件

修改规则配置

报错等级

0:忽略
1:警告
2:报错

quotes:是否强制使用双引号
semi:是否强制末尾分号
no-console:是否不允许有console.log()语句
no-unused-vars:是否不允许有没有使用的变量
no-redeclare:是否不允许重复定义变量

.eslintrc.js
1
2
3
4
5
6
7
module.exports = {
"rules": {
"quotes": 2,
"semi": 1,
"no-console": 1,
}
}

检测代码

--fix:将检测到的警告和错误自动修复
<file>.js:被检测的JS文件

1
eslint <file>.js

检测当前目录的所有文件

1
eslint .

完成

参考文献

哔哩哔哩——许泽鸿