1.eslint简介
eslint是用来管理和检测js代码风格的工具,可以和编辑器搭配使用,如vscode的eslint插件
当有不符合配置文件内容的代码出现就会报错或者警告
2.安装eslint
(1)npm init -y
(2)npm install eslint –save-dev
(3)./node_modules/.bin/eslint –init 初始化配置文件
(4)init初始化配置
How would you like to use ESLint? (Use arrow keys)
To check syntax only //只检查语法
> To check syntax and find problems//检查语法、发现问题
To check syntax, find problems, and enforce code style//检查语法、发现问题并执行代码样式
您的项目使用什么类型的模块?
What type of modules does your project use? (Use arrow keys)
> JavaScript modules (import/export)
CommonJS (require/exports)
None of these
项目中使用的什么框架?
Which framework does your project use? (Use arrow keys)
> React
Vue.js
None of these
你的代码运行在什么地方?
Where does your code run? (Press <space> to select, <a> to toggle all, <i> to invert selection)
>(*) Browser
( ) Node
How would you like to define a style for your project? (Use arrow keys)
> Use a popular style guide // 1. 使用大厂的
Answer questions about your style //2. 问问题
Inspect your JavaScript file(s) //3. 检查现有的代码自动生成
您希望配置文件的格式是什么?
What format do you want your config file to be in?
> JavaScript
YAML
JSON
(5).eslintrc.js,后续可以修改
module.exports = {
env: {
browser: true,
es6: true,
},
extends: 'plugin:vue/essential',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
parser: 'babel-eslint',
},
plugins: ['vue'],
rules: {
'no-console': 0,
'no-alert': 1, //提示alert
'no-var': 2, //禁用var,用let和const代替
'no-constant-condition': 2, //禁止在条件中使用常量表达式 if(true) if(1)
'no-empty': 1, //块语句中的内容不能为空
'no-irregular-whitespace': 2, //不能有不规则的空格
'no-multi-spaces': 1, //不能用多余的空格
'no-redeclare': 2, //禁止重复声明变量
indent: [2, 2], // 使用缩进量为2
// semi: [2, 'never'], // 不使用分号
// quotes: [2, 'single'], // 使用单引号
},
};