eslint配置方式有兩種:javascript
有幾種東西是能夠配置的:html
咱們這裏使用配置文件.eslintrc.js來配置,它導出一個模塊供ESLint識別。vue
// http://eslint.org/docs/user-guide/configuring module.exports = { root: true, parser: 'babel-eslint',//解析器,這裏咱們使用babel-eslint parserOptions: { sourceType: 'module'//類型爲module,由於代碼使用了使用了ECMAScript模塊 }, env: { browser: true,//預約義的全局變量,這裏是瀏覽器環境 }, // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style //extends: 'standard', //擴展,能夠經過字符串或者一個數組來擴展規則 // required to lint *.vue files plugins: [ 'html' //插件,此插件用於識別文件中的js代碼,沒有MIME類型標識沒有script標籤也能夠識別到,所以拿來識別.vue文件中的js代碼 ], // add your custom rules here 'rules': { //這裏寫自定義規則 } }
ESLint的規則有三種級別:java
有時候代碼裏有些特殊狀況須要咱們在某一行或者某幾行關閉ESLint檢測,能夠使用註釋:git
下面的代碼會關閉全部規則:github
/* eslint-disable */ alert('foo'); /* eslint-enable */
下面的代碼會關閉某一行的全部規則:正則表達式
alert('foo'); // eslint-disable-line no-alert // eslint-disable-next-line no-alert alert('foo');
經常使用規則:express
'rules': { "comma-dangle": ["error", "never"], //是否容許對象中出現結尾逗號 "no-cond-assign": 2, //條件語句的條件中不容許出現賦值運算符 "no-console": 2, //不容許出現console語句 "no-constant-condition": 2, //條件語句的條件中不容許出現恆定不變的量 "no-control-regex": 2, //正則表達式中不容許出現控制字符 "no-debugger": 2, //不容許出現debugger語句 "no-dupe-args": 2, //函數定義的時候不容許出現重複的參數 "no-dupe-keys": 2, //對象中不容許出現重複的鍵 "no-duplicate-case": 2, //switch語句中不容許出現重複的case標籤 "no-empty": 2, //不容許出現空的代碼塊 "no-empty-character-class": 2, //正則表達式中不容許出現空的字符組 "no-ex-assign": 2, //在try catch語句中不容許從新分配異常變量 "no-extra-boolean-cast": 2, //不容許出現沒必要要的布爾值轉換 "no-extra-parens": 0, //不容許出現沒必要要的圓括號 "no-extra-semi": 2, //不容許出現沒必要要的分號 "no-func-assign": 2, //不容許從新分配函數聲明 "no-inner-declarations": ["error", "functions"], //不容許在嵌套代碼塊裏聲明函數 "no-invalid-regexp": 2, //不容許在RegExp構造函數裏出現無效的正則表達式 "no-irregular-whitespace": 2, //不容許出現不規則的空格 "no-negated-in-lhs": 2, //不容許在in表達式語句中對最左邊的運算數使用取反操做 "no-obj-calls": 2, //不容許把全局對象屬性當作函數來調用 "no-regex-spaces": 2, //正則表達式中不容許出現多個連續空格 "quote-props": 2, //對象中的屬性名是否須要用引號引發來 "no-sparse-arrays": 2, //數組中不容許出現空位置 "no-unreachable": 2, //在return,throw,continue,break語句後不容許出現不可能到達的語句 "use-isnan": 2, //要求檢查NaN的時候使用isNaN() "valid-jsdoc": ["error", { "requireReturn": false, "requireParamDescription": false, "requireReturnDescription": true }], //強制JSDoc註釋 "valid-typeof": ["error", { "requireStringLiterals": true }], //在使用typeof表達式比較的時候強制使用有效的字符串 "block-scoped-var": 2, //將變量聲明放在合適的代碼塊裏 "complexity": 0, //限制條件語句的複雜度 "consistent-return": 2, //不管有沒有返回值都強制要求return語句返回一個值 "curly": ["error", "all"], //強制使用花括號的風格 "default-case": 0, //在switch語句中須要有default語句 "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], //獲取對象屬性的時候使用點號 "eqeqeq": ["error", "smart"], //比較的時候使用嚴格等於 "no-alert": 1, //不容許使用alert,confirm,prompt語句 "no-caller": 2, //不容許使用arguments.callee和arguments.caller屬性 "guard-for-in": 0, //監視for in循環,防止出現不可預料的狀況 "no-div-regex": 2, //不能使用看起來像除法的正則表達式 "no-else-return": 0, //若是if語句有return,else裏的return不用放在else裏 "no-labels": ["error", { "allowLoop": false, "allowSwitch": false }], //不容許標籤語句 "no-eq-null": 2, //不容許對null用==或者!= "no-eval": 2, //不容許使用eval() "no-extend-native": 2, //不容許擴展原生對象 "no-extra-bind": 2, //不容許沒必要要的函數綁定 "no-fallthrough": 2, //不容許switch按順序所有執行全部case "no-floating-decimal": 2, //不容許浮點數缺失數字 "no-implied-eval": 2, //不容許使用隱式eval() "no-iterator": 2, //不容許使用__iterator__屬性 "no-lone-blocks": 2, //不容許沒必要要的嵌套代碼塊 "no-loop-func": 2, //不容許在循環語句中進行函數聲明 "no-multi-spaces": 2, //不容許出現多餘的空格 "no-multi-str": 2, //不容許用\來讓字符串換行 "no-global-assign": 2, //不容許從新分配原生對象 "no-new": 2, //不容許new一個實例後不賦值或者不比較 "no-new-func": 2, //不容許使用new Function "no-new-wrappers": 2, //不容許使用new String,Number和Boolean對象 "no-octal": 2, //不容許使用八進制字面值 "no-octal-escape": 2, //不容許使用八進制轉義序列 "no-param-reassign": 0, //不容許從新分配函數參數"no-proto": 2, //不容許使用__proto__屬性 "no-redeclare": 2, //不容許變量重複聲明 "no-return-assign": 2, //不容許在return語句中使用分配語句 "no-script-url": 2, //不容許使用javascript:void(0) "no-self-compare": 2, //不容許本身和本身比較 "no-sequences": 2, //不容許使用逗號表達式 "no-throw-literal": 2, //不容許拋出字面量錯誤 throw "error" "no-unused-expressions": 2, //不容許無用的表達式 "no-void": 2, //不容許void操做符 "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], //不容許警告備註 "no-with": 2, //不容許使用with語句 "radix": 1, //使用parseInt時強制使用基數來指定是十進制仍是其餘進制 "vars-on-top": 0, //var必須放在做用域頂部 "wrap-iife": [2, "any"], //當即執行表達式的括號風格 "yoda": [2, "never", {"exceptRange": true}], //不容許在if條件中使用yoda條件 "strict": [2, "function"], //使用嚴格模式 "no-catch-shadow": 2, //不容許try catch語句接受的err變量與外部變量重名"no-delete-var": 2, //不容許使用delete操做符 "no-label-var": 2, //不容許標籤和變量同名 "no-shadow": 2, //外部做用域中的變量不能與它所包含的做用域中的變量或參數同名 "no-shadow-restricted-names": 2, //js關鍵字和保留字不能做爲函數名或者變量名 "no-undef": 2, //不容許未聲明的變量 "no-undef-init": 2, //不容許初始化變量時給變量賦值undefined "no-undefined": 2, //不容許把undefined當作標識符使用 "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不容許有聲明後未使用的變量或者參數 "no-use-before-define": [2, "nofunc"], //不容許在未定義以前就使用變量"indent": 2, //強制一致的縮進風格 "brace-style": [2, "1tbs", { "allowSingleLine": false}], //大括號風格 "camelcase": [2, {"properties": "never"}], //強制駝峯命名規則 "comma-style": [2, "last"], //逗號風格 "consistent-this": [0, "self"], //當獲取當前環境的this是用同樣的風格 "eol-last": 2, //文件以換行符結束 "func-names": 0, //函數表達式必須有名字 "func-style": 0, //函數風格,規定只能使用函數聲明或者函數表達式 "key-spacing": [2, {"beforeColon": false, "afterColon": true}], //對象字面量中冒號的先後空格 "max-nested-callbacks": 0, //回調嵌套深度 "new-cap": [2, {"newIsCap": true, "capIsNew": false}], //構造函數名字首字母要大寫 "new-parens": 2, //new時構造函數必須有小括號 "newline-after-var": 0, //變量聲明後必須空一行 "no-array-constructor": 2, //不容許使用數組構造器 "no-inline-comments": 0, //不容許行內註釋 "no-lonely-if": 0, //不容許else語句內只有if語句 "no-mixed-spaces-and-tabs": [2, "smart-tabs"], //不容許混用tab和空格 "no-multiple-empty-lines": [2, {"max": 2}], //空行最多不能超過兩行 "no-nested-ternary": 2, //不容許使用嵌套的三目運算符 "no-new-object": 2, //禁止使用new Object() "fun-call-spacing": 2, //函數調用時,函數名與()之間不能有空格 "no-ternary": 0, //不容許使用三目運算符 "no-trailing-spaces": 2, //一行最後不容許有空格 "no-underscore-dangle": 2, //不容許標識符如下劃線開頭 "no-extra-parens": 0, //不容許出現多餘的括號 "one-var": 0, //強制變量聲明放在一塊兒 "operator-assignment": 0, //賦值運算符的風格 "padded-blocks": [2, "never"], //塊內行首行尾是否空行 "quote-props": 0, //對象字面量中屬性名加引號 "quotes": [1, "single", "avoid-escape"], //引號風格 "semi": [2, "always"], //強制語句分號結尾 "semi-spacing": [2, {"before": false, "after": true}], //分後先後空格 "sort-vars": 0, //變量聲明時排序 "space-before-blocks": [2, "always"], //塊前的空格 "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], //函數定義時括號前的空格 "space-infix-ops": [2, {"int32Hint": true}], //操做符周圍的空格 "keyword-spacing": 2, //關鍵字先後的空格 "space-unary-ops": [2, { "words": true, "nonwords": false}], //一元運算符先後不要加空格 "wrap-regex": 2, //正則表達式字面量用括號括起來 "no-var": 0, //使用let和const代替var "generator-star-spacing": [2, "both"], //生成器函數先後空格 "max-depth": 0, //嵌套塊深度 "max-len": 0, //一行最大長度,單位爲字符 "max-params": 0, //函數最多能有多少個參數 "max-statements": 0, //函數內最多有幾個聲明 "no-bitwise": 0, //不容許使用位運算符 "no-plusplus": 0 //不容許使用++ --運算符 }
更多完整規則:請參閱ESlint官網完整說明json