簡(jiǎn)介
ESLint是一個(gè)以可擴(kuò)展甥雕、每條規(guī)則獨(dú)立孝赫、不內(nèi)置編碼風(fēng)格為理念編寫(xiě)一個(gè) lint 工具锣咒。
主要的特點(diǎn)有:
- 默認(rèn)規(guī)則包含所有 JSLint、JSHint 中存在的規(guī)則牺勾,易遷移
- 規(guī)則可配置性高:可設(shè)置「警告」正罢、「錯(cuò)誤」兩個(gè) error 等級(jí),或者直接禁用
- 包含代碼風(fēng)格檢測(cè)的規(guī)則(可以丟掉 JSCS 了)
- 支持插件擴(kuò)展驻民、自定義規(guī)則
起步
安裝
$ npm install -g eslint
使用
$ eslint --init
將會(huì)生成.eslintrc.js文件
添加一個(gè)待檢查的文件add.js
function add (a, b) {
return a + b;
}
執(zhí)行檢查命令
$ eslint add.js
指定配置文件
$ eslint -c config.json add.js
配置
ESLint的配置最重要的是規(guī)則的配置翻具,具體的格式如下
"quotes": [2, "double"]
# 第一部分是規(guī)則名
# 第二部分是表示級(jí)別:0-不驗(yàn)證;1-警告回还;2-錯(cuò)誤
有三種方法可以使用ESLint的規(guī)則
- 使用 .eslintrc 文件(支持 JSON 和 YAML 兩種語(yǔ)法)
- 在 package.json 中添加 eslintConfig 配置塊
- 直接在代碼文件中定義
配置文件中的:
"extends": "eslint:recommended"
表示使用默認(rèn)的規(guī)則進(jìn)行校驗(yàn)裆泳,如果自定義規(guī)則需要將此設(shè)置去掉!
- 文件中關(guān)閉驗(yàn)證
/*eslint-disable */
//suppress all warnings between comments
alert('foo');
/*eslint-enable */
- 文件中指定規(guī)則不驗(yàn)證
/*eslint-disable no-alert, no-console */
alert('foo');
console.log('bar');
/*eslint-enable no-alert */
規(guī)則說(shuō)明
"no-alert": 0,//禁止使用alert confirm prompt
"no-array-constructor": 2,//禁止使用數(shù)組構(gòu)造器
"no-bitwise": 0,//禁止使用按位運(yùn)算符
"no-caller": 1,//禁止使用arguments.caller或arguments.callee
"no-catch-shadow": 2,//禁止catch子句參數(shù)與外部作用域變量同名
"no-class-assign": 2,//禁止給類賦值
"no-cond-assign": 2,//禁止在條件表達(dá)式中使用賦值語(yǔ)句
"no-console": 2,//禁止使用console
"no-const-assign": 2,//禁止修改const聲明的變量
"no-constant-condition": 2,//禁止在條件中使用常量表達(dá)式 if(true) if(1)
"no-continue": 0,//禁止使用continue
"no-control-regex": 2,//禁止在正則表達(dá)式中使用控制字符
"no-debugger": 2,//禁止使用debugger
"no-delete-var": 2,//不能對(duì)var聲明的變量使用delete操作符
"no-div-regex": 1,//不能使用看起來(lái)像除法的正則表達(dá)式/=foo/
"no-dupe-keys": 2,//在創(chuàng)建對(duì)象字面量時(shí)不允許鍵重復(fù) {a:1,a:1}
"no-dupe-args": 2,//函數(shù)參數(shù)不能重復(fù)
"no-duplicate-case": 2,//switch中的case標(biāo)簽不能重復(fù)
"no-else-return": 2,//如果if語(yǔ)句里面有return,后面不能跟else語(yǔ)句
"no-empty": 2,//塊語(yǔ)句中的內(nèi)容不能為空
"no-empty-character-class": 2,//正則表達(dá)式中的[]內(nèi)容不能為空
"no-empty-label": 2,//禁止使用空l(shuí)abel
"no-eq-null": 2,//禁止對(duì)null使用==或!=運(yùn)算符
"no-eval": 1,//禁止使用eval
"no-ex-assign": 2,//禁止給catch語(yǔ)句中的異常參數(shù)賦值
"no-extend-native": 2,//禁止擴(kuò)展native對(duì)象
"no-extra-bind": 2,//禁止不必要的函數(shù)綁定
"no-extra-boolean-cast": 2,//禁止不必要的bool轉(zhuǎn)換
"no-extra-parens": 2,//禁止非必要的括號(hào)
"no-extra-semi": 2,//禁止多余的冒號(hào)
"no-fallthrough": 1,//禁止switch穿透
"no-floating-decimal": 2,//禁止省略浮點(diǎn)數(shù)中的0 .5 3.
"no-func-assign": 2,//禁止重復(fù)的函數(shù)聲明
"no-implicit-coercion": 1,//禁止隱式轉(zhuǎn)換
"no-implied-eval": 2,//禁止使用隱式eval
"no-inline-comments": 0,//禁止行內(nèi)備注
"no-inner-declarations": [2, "functions"],//禁止在塊語(yǔ)句中使用聲明(變量或函數(shù))
"no-invalid-regexp": 2,//禁止無(wú)效的正則表達(dá)式
"no-invalid-this": 2,//禁止無(wú)效的this柠硕,只能用在構(gòu)造器工禾,類运提,對(duì)象字面量
"no-irregular-whitespace": 2,//不能有不規(guī)則的空格
"no-iterator": 2,//禁止使用__iterator__ 屬性
"no-label-var": 2,//label名不能與var聲明的變量名相同
"no-labels": 2,//禁止標(biāo)簽聲明
"no-lone-blocks": 2,//禁止不必要的嵌套塊
"no-lonely-if": 2,//禁止else語(yǔ)句內(nèi)只有if語(yǔ)句
"no-loop-func": 1,//禁止在循環(huán)中使用函數(shù)(如果沒(méi)有引用外部變量不形成閉包就可以)
"no-mixed-requires": [0, false],//聲明時(shí)不能混用聲明類型
"no-mixed-spaces-and-tabs": [2, false],//禁止混用tab和空格
"linebreak-style": [0, "windows"],//換行風(fēng)格
"no-multi-spaces": 1,//不能用多余的空格
"no-multi-str": 2,//字符串不能用\換行
"no-multiple-empty-lines": [1, {"max": 2}],//空行最多不能超過(guò)2行
"no-native-reassign": 2,//不能重寫(xiě)native對(duì)象
"no-negated-in-lhs": 2,//in 操作符的左邊不能有!
"no-nested-ternary": 0,//禁止使用嵌套的三目運(yùn)算
"no-new": 1,//禁止在使用new構(gòu)造一個(gè)實(shí)例后不賦值
"no-new-func": 1,//禁止使用new Function
"no-new-object": 2,//禁止使用new Object()
"no-new-require": 2,//禁止使用new require
"no-new-wrappers": 2,//禁止使用new創(chuàng)建包裝實(shí)例,new String new Boolean new Number
"no-obj-calls": 2,//不能調(diào)用內(nèi)置的全局對(duì)象闻葵,比如Math() JSON()
"no-octal": 2,//禁止使用八進(jìn)制數(shù)字
"no-octal-escape": 2,//禁止使用八進(jìn)制轉(zhuǎn)義序列
"no-param-reassign": 2,//禁止給參數(shù)重新賦值
"no-path-concat": 0,//node中不能使用__dirname或__filename做路徑拼接
"no-plusplus": 0,//禁止使用++民泵,--
"no-process-env": 0,//禁止使用process.env
"no-process-exit": 0,//禁止使用process.exit()
"no-proto": 2,//禁止使用__proto__屬性
"no-redeclare": 2,//禁止重復(fù)聲明變量
"no-regex-spaces": 2,//禁止在正則表達(dá)式字面量中使用多個(gè)空格 /foo bar/
"no-restricted-modules": 0,//如果禁用了指定模塊,使用就會(huì)報(bào)錯(cuò)
"no-return-assign": 1,//return 語(yǔ)句中不能有賦值表達(dá)式
"no-script-url": 0,//禁止使用javascript:void(0)
"no-self-compare": 2,//不能比較自身
"no-sequences": 0,//禁止使用逗號(hào)運(yùn)算符
"no-shadow": 2,//外部作用域中的變量不能與它所包含的作用域中的變量或參數(shù)同名
"no-shadow-restricted-names": 2,//嚴(yán)格模式中規(guī)定的限制標(biāo)識(shí)符不能作為聲明時(shí)的變量名使用
"no-spaced-func": 2,//函數(shù)調(diào)用時(shí) 函數(shù)名與()之間不能有空格
"no-sparse-arrays": 2,//禁止稀疏數(shù)組槽畔, [1,,2]
"no-sync": 0,//nodejs 禁止同步方法
"no-ternary": 0,//禁止使用三目運(yùn)算符
"no-trailing-spaces": 1,//一行結(jié)束后面不要有空格
"no-this-before-super": 0,//在調(diào)用super()之前不能使用this或super
"no-throw-literal": 2,//禁止拋出字面量錯(cuò)誤 throw "error";
"no-undef": 1,//不能有未定義的變量
"no-undef-init": 2,//變量初始化時(shí)不能直接給它賦值為undefined
"no-undefined": 2,//不能使用undefined
"no-unexpected-multiline": 2,//避免多行表達(dá)式
"no-underscore-dangle": 1,//標(biāo)識(shí)符不能以_開(kāi)頭或結(jié)尾
"no-unneeded-ternary": 2,//禁止不必要的嵌套 var isYes = answer === 1 ? true : false;
"no-unreachable": 2,//不能有無(wú)法執(zhí)行的代碼
"no-unused-expressions": 2,//禁止無(wú)用的表達(dá)式
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],//不能有聲明后未被使用的變量或參數(shù)
"no-use-before-define": 2,//未定義前不能使用
"no-useless-call": 2,//禁止不必要的call和apply
"no-void": 2,//禁用void操作符
"no-var": 0,//禁用var栈妆,用let和const代替
"no-warning-comments": [1, { "terms": ["todo", "fixme", "xxx"], "location": "start" }],//不能有警告?zhèn)渥? "no-with": 2,//禁用with
"array-bracket-spacing": [2, "never"],//是否允許非空數(shù)組里面有多余的空格
"arrow-parens": 0,//箭頭函數(shù)用小括號(hào)括起來(lái)
"arrow-spacing": 0,//=>的前/后括號(hào)
"accessor-pairs": 0,//在對(duì)象中使用getter/setter
"block-scoped-var": 0,//塊語(yǔ)句中使用var
"brace-style": [1, "1tbs"],//大括號(hào)風(fēng)格
"callback-return": 1,//避免多次調(diào)用回調(diào)什么的
"camelcase": 2,//強(qiáng)制駝峰法命名
"comma-dangle": [2, "never"],//對(duì)象字面量項(xiàng)尾不能有逗號(hào)
"comma-spacing": 0,//逗號(hào)前后的空格
"comma-style": [2, "last"],//逗號(hào)風(fēng)格,換行時(shí)在行首還是行尾
"complexity": [0, 11],//循環(huán)復(fù)雜度
"computed-property-spacing": [0, "never"],//是否允許計(jì)算后的鍵名什么的
"consistent-return": 0,//return 后面是否允許省略
"consistent-this": [2, "that"],//this別名
"constructor-super": 0,//非派生類不能調(diào)用super厢钧,派生類必須調(diào)用super
"curly": [2, "all"],//必須使用 if(){} 中的{}
"default-case": 2,//switch語(yǔ)句最后必須有default
"dot-location": 0,//對(duì)象訪問(wèn)符的位置鳞尔,換行的時(shí)候在行首還是行尾
"dot-notation": [0, { "allowKeywords": true }],//避免不必要的方括號(hào)
"eol-last": 0,//文件以單一的換行符結(jié)束
"eqeqeq": 2,//必須使用全等
"func-names": 0,//函數(shù)表達(dá)式必須有名字
"func-style": [0, "declaration"],//函數(shù)風(fēng)格,規(guī)定只能使用函數(shù)聲明/函數(shù)表達(dá)式
"generator-star-spacing": 0,//生成器函數(shù)*的前后空格
"guard-for-in": 0,//for in循環(huán)要用if語(yǔ)句過(guò)濾
"handle-callback-err": 0,//nodejs 處理錯(cuò)誤
"id-length": 0,//變量名長(zhǎng)度
"indent": [2, 4],//縮進(jìn)風(fēng)格
"init-declarations": 0,//聲明時(shí)必須賦初值
"key-spacing": [0, { "beforeColon": false, "afterColon": true }],//對(duì)象字面量中冒號(hào)的前后空格
"lines-around-comment": 0,//行前/行后備注
"max-depth": [0, 4],//嵌套塊深度
"max-len": [0, 80, 4],//字符串最大長(zhǎng)度
"max-nested-callbacks": [0, 2],//回調(diào)嵌套深度
"max-params": [0, 3],//函數(shù)最多只能有3個(gè)參數(shù)
"max-statements": [0, 10],//函數(shù)內(nèi)最多有幾個(gè)聲明
"new-cap": 2,//函數(shù)名首行大寫(xiě)必須使用new方式調(diào)用早直,首行小寫(xiě)必須用不帶new方式調(diào)用
"new-parens": 2,//new時(shí)必須加小括號(hào)
"newline-after-var": 2,//變量聲明后是否需要空一行
"object-curly-spacing": [0, "never"],//大括號(hào)內(nèi)是否允許不必要的空格
"object-shorthand": 0,//強(qiáng)制對(duì)象字面量縮寫(xiě)語(yǔ)法
"one-var": 1,//連續(xù)聲明
"operator-assignment": [0, "always"],//賦值運(yùn)算符 += -=什么的
"operator-linebreak": [2, "after"],//換行時(shí)運(yùn)算符在行尾還是行首
"padded-blocks": 0,//塊語(yǔ)句內(nèi)行首行尾是否要空行
"prefer-const": 0,//首選const
"prefer-spread": 0,//首選展開(kāi)運(yùn)算
"prefer-reflect": 0,//首選Reflect的方法
"quotes": [1, "single"],//引號(hào)類型 `` "" ''
"quote-props":[2, "always"],//對(duì)象字面量中的屬性名是否強(qiáng)制雙引號(hào)
"radix": 2,//parseInt必須指定第二個(gè)參數(shù)
"id-match": 0,//命名檢測(cè)
"require-yield": 0,//生成器函數(shù)必須有yield
"semi": [2, "always"],//語(yǔ)句強(qiáng)制分號(hào)結(jié)尾
"semi-spacing": [0, {"before": false, "after": true}],//分號(hào)前后空格
"sort-vars": 0,//變量聲明時(shí)排序
"space-after-keywords": [0, "always"],//關(guān)鍵字后面是否要空一格
"space-before-blocks": [0, "always"],//不以新行開(kāi)始的塊{前面要不要有空格
"space-before-function-paren": [0, "always"],//函數(shù)定義時(shí)括號(hào)前面要不要有空格
"space-in-parens": [0, "never"],//小括號(hào)里面要不要有空格
"space-infix-ops": 0,//中綴操作符周圍要不要有空格
"space-return-throw-case": 2,//return throw case后面要不要加空格
"space-unary-ops": [0, { "words": true, "nonwords": false }],//一元運(yùn)算符的前/后要不要加空格
"spaced-comment": 0,//注釋風(fēng)格要不要有空格什么的
"strict": 2,//使用嚴(yán)格模式
"use-isnan": 2,//禁止比較時(shí)使用NaN铅檩,只能用isNaN()
"valid-jsdoc": 0,//jsdoc規(guī)則
"valid-typeof": 2,//必須使用合法的typeof的值
"vars-on-top": 2,//var必須放在作用域頂部
"wrap-iife": [2, "inside"],//立即執(zhí)行函數(shù)表達(dá)式的小括號(hào)風(fēng)格
"wrap-regex": 0,//正則表達(dá)式字面量用小括號(hào)包起來(lái)
"yoda": [2, "never"]//禁止尤達(dá)條件
范例
這是一個(gè)例子,可以直接使用莽鸿,具體規(guī)格可以自己指定
{
"globals": {
"$": true //zepto
},
"env": {
"browser": true,
"node": true
},
"rules": {
//警告
// "quotes": [1, "single"], //建議使用單引號(hào)
// "no-inner-declarations": [1, "both"], //不建議在{}代碼塊內(nèi)部聲明變量或函數(shù)
"no-extra-boolean-cast": 1, //多余的感嘆號(hào)轉(zhuǎn)布爾型
"no-extra-semi": 1, //多余的分號(hào)
"no-extra-parens": 1, //多余的括號(hào)
"no-empty": 1, //空代碼塊
"no-use-before-define": [1, "nofunc"], //使用前未定義
"complexity": [1, 10], //圈復(fù)雜度大于10 警告
//常見(jiàn)錯(cuò)誤
"comma-dangle": [2, "never"], //定義數(shù)組或?qū)ο笞詈蠖嘤嗟亩禾?hào)
"no-debugger": 2, //debugger 調(diào)試代碼未刪除
"no-constant-condition": 2, //常量作為條件
"no-dupe-args": 2, //參數(shù)重復(fù)
"no-dupe-keys": 2, //對(duì)象屬性重復(fù)
"no-duplicate-case": 2, //case重復(fù)
"no-empty-character-class": 2, //正則無(wú)法匹配任何值
"no-invalid-regexp": 2, //無(wú)效的正則
"no-func-assign": 2, //函數(shù)被賦值
"valid-typeof": 2, //無(wú)效的類型判斷
"no-unreachable": 2, //不可能執(zhí)行到的代碼
"no-unexpected-multiline": 2, //行尾缺少分號(hào)可能導(dǎo)致一些意外情況
"no-sparse-arrays": 2, //數(shù)組中多出逗號(hào)
"no-shadow-restricted-names": 2, //關(guān)鍵詞與命名沖突
"no-undef": 2, //變量未定義
"no-unused-vars": 2, //變量定義后未使用
"no-cond-assign": 2, //條件語(yǔ)句中禁止賦值操作
"no-native-reassign": 2, //禁止覆蓋原生對(duì)象
//代碼風(fēng)格優(yōu)化
"no-else-return": 1, //在else代碼塊中return,else是多余的
"no-multi-spaces": 1, //不允許多個(gè)空格
"key-spacing": [1, {"beforeColon": false, "afterColon": true}],//object直接量建議寫(xiě)法 : 后一個(gè)空格簽名不留空格
"block-scoped-var": 2, //變量定義后未使用
"consistent-return": 2, //函數(shù)返回值可能是不同類型
"accessor-pairs": 2, //object getter/setter方法需要成對(duì)出現(xiàn)
"dot-location": [2, "property"], //換行調(diào)用對(duì)象方法 點(diǎn)操作符應(yīng)寫(xiě)在行首
"no-lone-blocks": 2, //多余的{}嵌套
"no-empty-label": 2, //無(wú)用的標(biāo)記
"no-extend-native": 2, //禁止擴(kuò)展原生對(duì)象
"no-floating-decimal": 2, //浮點(diǎn)型需要寫(xiě)全 禁止.1 或 2.寫(xiě)法
"no-loop-func": 2, //禁止在循環(huán)體中定義函數(shù)
"no-new-func": 2, //禁止new Function(...) 寫(xiě)法
"no-self-compare": 2, //不允與自己比較作為條件
"no-sequences": 2, //禁止可能導(dǎo)致結(jié)果不明確的逗號(hào)操作符
"no-throw-literal": 2, //禁止拋出一個(gè)直接量 應(yīng)是Error對(duì)象
"no-return-assign": [2, "always"], //不允return時(shí)有賦值操作
"no-redeclare": [2, {"builtinGlobals": true}],//不允許重復(fù)聲明
"no-unused-expressions": [2, {"allowShortCircuit": true, "allowTernary": true}],//未使用的表達(dá)式
"no-useless-call": 2, //無(wú)意義的函數(shù)call或apply
"no-useless-concat": 2, //無(wú)意義的string concat
"no-void": 2, //禁用void
"no-with": 2, //禁用with
"no-warning-comments": [2, { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }],//標(biāo)記未寫(xiě)注釋
"curly": 2 //if拾给、else祥得、while、for代碼塊用{}包圍
}
}