eslint的好處就不多說了。
1.安裝依賴
npm install --save-dev eslint eslint-friendly-formatter eslint-loader
//或
yarn add eslint eslint-friendly-formatter eslint-loader
2.根目錄創建.eslintrc.js文件,配置eslint規則(爲了便於開發,這裏只列出常用的規則,更多詳細rule規則可進官網查看)
module.exports = {
root: true,
// JavaScript 語言選項
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
//想使用的額外的語言特性:
ecmaFeatures: {
// 允許在全局作用域下使用 return 語句
globalReturn: true,
// impliedStric
impliedStrict: true
}
},
//全局變量
globals: {
"$": true,
"process": true,
"__dirname": true,
"echarts": true
},
// 環境定義了預定義的全局變量。
env: {
browser: true,
es6: true,
amd: true,
browser: true,
},
rules: {
"no-var": 1, //對var警告
"no-unused-vars": [1, { "vars": "all", "args": "none" }], //不能有聲明後未被使用的變量或參數
"no-const-assign": 1, //禁止修改const聲明的變量
"no-dupe-keys": 1, //在創建對象字面量時不允許鍵重複
"no-duplicate-case": 1, //switch中的case標籤不能重複
"no-dupe-args": 1, //函數參數不能重複
"no-func-assign": 1, //禁止重複的函數聲明
"no-redeclare": 1, //禁止重複聲明變量
"no-spaced-func": 1, //函數調用時 函數名與()之間不能有空格
"no-this-before-super": 1, //在調用super()之前不能使用this或super
"no-use-before-define": 1, //未定義前不能使用
"no-else-return": 1,//如果if語句裏面有return,後面不能跟else語句
"no-fallthrough": 1,//禁止switch穿透
"no-multiple-empty-lines": [1, {"max": 2}],//空行最多不能超過2行
"no-return-assign": 1,//return 語句中不能有賦值表達式
"no-shadow": 2,//外部作用域中的變量不能與它所包含的作用域中的變量或參數同名
"consistent-this": [2, "that"],//this別名
"default-case": 2,//switch語句最後必須有default
"eqeqeq": 2,//必須使用全等
}
}
效果: