1.extends屬性
一個(gè)配置文件可以被基礎(chǔ)配置中的已啟用的規(guī)則繼承??梢允褂靡韵乱?guī)則繼承:
(1)'eslint:recommended'
繼承Eslint中推薦的(打鉤的)規(guī)則項(xiàng)
module.exports = {
'extends': 'eslint:recommended',
'rules': { }}
(2)使用別人寫好的規(guī)則包(以eslint-config-開頭的npm包),如eslint-config-standard
module.exports = {
'extends': 'standard',
'rules': { }
}
(3)使用Eslint插件中命名的配置
module.exports = {
'plugins': [
'react'
],
'extends': [
'eslint:recommended',
'plugin:react/recommended'
],
'rules': {
'no-set-state': 'off'
}
}
(4)使用'eslint:all',繼承Eslint中所有的核心規(guī)則項(xiàng)
module.exports = {
'extends': 'eslint:all',
'rules': {
// override default options
'comma-dangle': ['error', 'always'],
'indent': ['error', 2],
'no-cond-assign': ['error', 'always'],
// disable now, but enable in the future
'one-var': 'off', // ['error', 'never'] // disable
'init-declarations': 'off',
'no-console': 'off',
'no-inline-comments': 'off',
}
}
規(guī)則列表
// 禁用 alert、confirm 和 prompt
’no-alert’:0,
// 禁用 arguments.caller 或 arguments.callee
’no-caller’:2,
// 不允許在 case 子句中使用詞法聲明
’no-case-declarations’:2,
// 禁止除法操作符顯式的出現(xiàn)在正則表達(dá)式開始的位置
’no-div-regex’:2,
// 禁止 if 語句中有 return 之后有 else
’no-else-return’:0,
// 禁止出現(xiàn)空函數(shù).如果一個(gè)函數(shù)包含了一條注釋,它將不會(huì)被認(rèn)為有問題。
’no-empty-function’:2,
// 禁止使用空解構(gòu)模式no-empty-pattern
’no-empty-pattern’:2,
// 禁止在沒有類型檢查操作符的情況下與 null 進(jìn)行比較
’no-eq-null’:1,
// 禁用 eval()
’no-eval’:2,
// 禁止擴(kuò)展原生類型
’no-extend-native’:2,
// 禁止不必要的 .bind() 調(diào)用
’no-extra-bind’:2,
// 禁用不必要的標(biāo)簽
’no-extra-label:’:0,
// 禁止 case 語句落空
’no-fallthrough’:2,
// 禁止數(shù)字字面量中使用前導(dǎo)和末尾小數(shù)點(diǎn)
’no-floating-decimal’:2,
// 禁止使用短符號進(jìn)行類型轉(zhuǎn)換(!!fOO)
’no-implicit-coercion’:0,
// 禁止在全局范圍內(nèi)使用 var 和命名的 function 聲明
’no-implicit-globals’:1,
// 禁止使用類似 eval() 的方法
’no-implied-eval’:2,
// 禁止 this 關(guān)鍵字出現(xiàn)在類和類對象之外
’no-invalid-this’:0,
// 禁用 iterator 屬性
’no-iterator’:2,
// 禁用標(biāo)簽語句
’no-labels’:2,
// 禁用不必要的嵌套塊
’no-lone-blocks’:2,
// 禁止在循環(huán)中出現(xiàn) function 聲明和表達(dá)式
’no-loop-func’:1,
// 禁用魔術(shù)數(shù)字(3.14什么的用常量代替)
’no-magic-numbers’:[1,{’ignore’: [0,-1,1] }],
// 禁止使用多個(gè)空格
’no-multi-spaces’:2,
// 禁止使用多行字符串,在 JavaScript 中,可以在新行之前使用斜線創(chuàng)建多行字符串
’no-multi-str’:2,
// 禁止對原生對象賦值
’no-native-reassign’:2,
// 禁止在非賦值或條件語句中使用 new 操作符
’no-new’:2,
// 禁止對 Function 對象使用 new 操作符
’no-new-func’:0,
// 禁止對 String,Number 和 Boolean 使用 new 操作符
’no-new-wrappers’:2,
// 禁用八進(jìn)制字面量
’no-octal’:2,
// 禁止在字符串中使用八進(jìn)制轉(zhuǎn)義序列
’no-octal-escape’:2,
// 不允許對 function 的參數(shù)進(jìn)行重新賦值
’no-param-reassign’:0,
// 禁用 proto 屬性
’no-proto’:2,
// 禁止使用 var 多次聲明同一變量
’no-redeclare’:2,
// 禁用指定的通過 require 加載的模塊
’no-return-assign’:0,
// 禁止使用 javascript: url
’no-script-url’:0,
// 禁止自我賦值
’no-self-assign’:2,
// 禁止自身比較
’no-self-compare’:2,
// 禁用逗號操作符
’no-sequences’:2,
// 禁止拋出非異常字面量
’no-throw-literal’:2,
// 禁用一成不變的循環(huán)條件
’no-unmodified-loop-condition’:2,
// 禁止出現(xiàn)未使用過的表達(dá)式
’no-unused-expressions’:0,
// 禁用未使用過的標(biāo)簽
’no-unused-labels’:2,
// 禁止不必要的 .call() 和 .apply()
’no-useless-call’:2,
// 禁止不必要的字符串字面量或模板字面量的連接
’no-useless-concat’:2,
// 禁用不必要的轉(zhuǎn)義字符
’no-useless-escape’:0,
// 禁用 void 操作符
’no-void’:0,
// 禁止在注釋中使用特定的警告術(shù)語
’no-warning-comments’:0,
// 禁用 with 語句
’no-with’:2,
// 強(qiáng)制在parseInt()使用基數(shù)參數(shù)
’radix’:2,
// 要求所有的 var 聲明出現(xiàn)在它們所在的作用域頂部
’vars-on-top’:0,
// 要求 IIFE 使用括號括起來
’wrap-iife’: [2,’any’],
// 要求或禁止 ’Yoda” 條件
’yoda’: [2,’never’],
// 要求或禁止使用嚴(yán)格模式指令
’strict’:0,
//////////////
// 變量聲明 //
//////////////
// 要求或禁止 var 聲明中的初始化(初值)
’init-declarations’:0,
// 不允許 catch 子句的參數(shù)與外層作用域中的變量同名
’no-catch-shadow’:0,
// 禁止刪除變量
’no-delete-var’:2,
// 不允許標(biāo)簽與變量同名
’no-label-var’:2,
// 禁用特定的全局變量
’no-restricted-globals’:0,
// 禁止 var 聲明 與外層作用域的變量同名
’no-shadow’:0,
// 禁止覆蓋受限制的標(biāo)識符
’no-shadow-restricted-names’:2,
// 禁用未聲明的變量,除非它們在 /*global */ 注釋中被提到
’no-undef’:2,
// 禁止將變量初始化為 undefined
’no-undef-init’:2,
// 禁止將 undefined 作為標(biāo)識符
’no-undefined’:0,
// 禁止出現(xiàn)未使用過的變量
’no-unused-vars’: [2, {’vars’:’all’,’args’:’none’}],
// 不允許在變量定義之前使用它們
’no-use-before-define’:0,
//////////////////////////
// Node.js and CommonJS //
//////////////////////////
// require return statements after callbacks
’callback-return’:0,
// 要求 require() 出現(xiàn)在頂層模塊作用域中
’global-require’:1,
// 要求回調(diào)函數(shù)中有容錯(cuò)處理
’handle-callback-err’: [2,’^(err|error)$’],
// 禁止混合常規(guī) var 聲明和 require 調(diào)用
’no-mixed-requires’:0,
// 禁止調(diào)用 require 時(shí)使用 new 操作符
’no-new-require’:2,
// 禁止對 __dirname 和 __filename進(jìn)行字符串連接
’no-path-concat’:0,
// 禁用 process.env
’no-process-env’:0,
// 禁用 process.exit()
’no-process-exit’:0,
// 禁用同步方法
’no-sync’:0,
//////////////
// 風(fēng)格指南 //
//////////////
// 指定數(shù)組的元素之間要以空格隔開(, 后面), never參數(shù):[ 之前和 ] 之后不能帶空格,always參數(shù):[ 之前和 ] 之后必須帶空格
’array-bracket-spacing’: [2,’never’],
// 禁止或強(qiáng)制在單行代碼塊中使用空格(禁用)
’block-spacing’:[1,’never’],
//強(qiáng)制使用一致的縮進(jìn) 第二個(gè)參數(shù)為 ’tab’ 時(shí),會(huì)使用tab,
// if while function 后面的{必須與if在同一行,java風(fēng)格。
’brace-style’: [2,’1tbs’, {’allowSingleLine’:true}],
// 雙峰駝命名格式
’camelcase’:2,
// 控制逗號前后的空格
’comma-spacing’: [2, {’before’:false,’after’:true}],
// 控制逗號在行尾出現(xiàn)還是在行首出現(xiàn) (默認(rèn)行尾)
// http://eslint.org/docs/rules/comma-style
’comma-style’: [2,’last’],
//’SwitchCase’ (默認(rèn):0) 強(qiáng)制 switch 語句中的 case 子句的縮進(jìn)水平
// 以方括號取對象屬性時(shí),[ 后面和 ] 前面是否需要空格, 可選參數(shù) never, always
’computed-property-spacing’: [2,’never’],
// 用于指統(tǒng)一在回調(diào)函數(shù)中指向this的變量名,箭頭函數(shù)中的this已經(jīng)可以指向外層調(diào)用者,應(yīng)該沒卵用了
// e.g [0,’that’] 指定只能 var that = this. that不能指向其他任何值,this也不能賦值給that以外的其他值
’consistent-this’: [1,’that’],
// 強(qiáng)制使用命名的 function 表達(dá)式
’func-names’:0,
// 文件末尾強(qiáng)制換行
’eol-last’:2,
’indent’: [2,4, {’SwitchCase’:1}],
// 強(qiáng)制在對象字面量的屬性中鍵和值之間使用一致的間距
’key-spacing’: [2, {’beforeColon’:false,’afterColon’:true}],
// 強(qiáng)制使用一致的換行風(fēng)格
’linebreak-style’: [1,’unix’],
// 要求在注釋周圍有空行 ( 要求在塊級注釋之前有一空行)
’lines-around-comment’: [1,{’beforeBlockComment’:true}],
// 強(qiáng)制一致地使用函數(shù)聲明或函數(shù)表達(dá)式,方法定義風(fēng)格,參數(shù):
// declaration: 強(qiáng)制使用方法聲明的方式,function f(){} e.g [2, ’declaration’]
// expression:強(qiáng)制使用方法表達(dá)式的方式,var f = function() {} e.g [2, ’expression’]
// allowArrowFunctions: declaration風(fēng)格中允許箭頭函數(shù)。 e.g [2, ’declaration’, { ’allowArrowFunctions’: true }]
’func-style’:0,
// 強(qiáng)制回調(diào)函數(shù)最大嵌套深度 5層
’max-nested-callbacks’: [1,5],
// 禁止使用指定的標(biāo)識符
’id-blacklist’:0,
// 強(qiáng)制標(biāo)識符的最新和最大長度
’id-length’:0,
// 要求標(biāo)識符匹配一個(gè)指定的正則表達(dá)式
’id-match’:0,
// 強(qiáng)制在 JSX 屬性中一致地使用雙引號或單引號
’jsx-quotes’:0,
// 強(qiáng)制在關(guān)鍵字前后使用一致的空格 (前后腰需要)
’keyword-spacing’:2,
// 強(qiáng)制一行的最大長度
’max-len’:[1,200],
// 強(qiáng)制最大行數(shù)
’max-lines’:0,
// 強(qiáng)制 function 定義中最多允許的參數(shù)數(shù)量
’max-params’:[1,7],
// 強(qiáng)制 function 塊最多允許的的語句數(shù)量
’max-statements’:[1,200],
// 強(qiáng)制每一行中所允許的最大語句數(shù)量
’max-statements-per-line’:0,
// 要求構(gòu)造函數(shù)首字母大寫 (要求調(diào)用 new 操作符時(shí)有首字母大小的函數(shù),允許調(diào)用首字母大寫的函數(shù)時(shí)沒有 new 操作符。)
’new-cap’: [2, {’newIsCap’:true,’capIsNew’:false}],
// 要求調(diào)用無參構(gòu)造函數(shù)時(shí)有圓括號
’new-parens’:2,
// 要求或禁止 var 聲明語句后有一行空行
’newline-after-var’:0,
// 禁止使用 Array 構(gòu)造函數(shù)
’no-array-constructor’:2,
// 禁用按位運(yùn)算符
’no-bitwise’:0,
// 要求 return 語句之前有一空行
’newline-before-return’:0,
// 要求方法鏈中每個(gè)調(diào)用都有一個(gè)換行符
’newline-per-chained-call’:1,
// 禁用 continue 語句
’no-continue’:0,
// 禁止在代碼行后使用內(nèi)聯(lián)注釋
’no-inline-comments’:0,
// 禁止 if 作為唯一的語句出現(xiàn)在 else 語句中
’no-lonely-if’:0,
// 禁止混合使用不同的操作符
’no-mixed-operators’:0,
// 不允許空格和 tab 混合縮進(jìn)
’no-mixed-spaces-and-tabs’:2,
// 不允許多個(gè)空行
’no-multiple-empty-lines’: [2, {’max’:2}],
// 不允許否定的表達(dá)式
’no-negated-condition’:0,
// 不允許使用嵌套的三元表達(dá)式
’no-nested-ternary’:0,
// 禁止使用 Object 的構(gòu)造函數(shù)
’no-new-object’:2,
// 禁止使用一元操作符 ++ 和 --
’no-plusplus’:0,
// 禁止使用特定的語法
’no-restricted-syntax’:0,
// 禁止 function 標(biāo)識符和括號之間出現(xiàn)空格
’no-spaced-func’:2,
// 不允許使用三元操作符
’no-ternary’:0,
// 禁用行尾空格
’no-trailing-spaces’:2,
// 禁止標(biāo)識符中有懸空下劃線_bar
’no-underscore-dangle’:0,
// 禁止可以在有更簡單的可替代的表達(dá)式時(shí)使用三元操作符
’no-unneeded-ternary’:2,
// 禁止屬性前有空白
’no-whitespace-before-property’:0,
// 強(qiáng)制花括號內(nèi)換行符的一致性
’object-curly-newline’:0,
// 強(qiáng)制在花括號中使用一致的空格
’object-curly-spacing’:0,
// 強(qiáng)制將對象的屬性放在不同的行上
’object-property-newline’:0,
// 強(qiáng)制函數(shù)中的變量要么一起聲明要么分開聲明
’one-var’: [2, {’initialized’:’never’}],
// 要求或禁止在 var 聲明周圍換行
’one-var-declaration-per-line’:0,
// 要求或禁止在可能的情況下要求使用簡化的賦值操作符
’operator-assignment’:0,
// 強(qiáng)制操作符使用一致的換行符
’operator-linebreak’: [2,’after’, {’overrides’: {’?’:’before’,’:’:’before’} }],
// 要求或禁止塊內(nèi)填充
’padded-blocks’:0,
// 要求對象字面量屬性名稱用引號括起來
’quote-props’:0,
// 強(qiáng)制使用一致的反勾號、雙引號或單引號
’quotes’: [2,’single’,’avoid-escape’],
// 要求使用 JSDoc 注釋
’require-jsdoc’:1,
// 要求或禁止使用分號而不是 ASI(這個(gè)才是控制行尾部分號的,)
’semi’: [2,’always’],
// 強(qiáng)制分號之前和之后使用一致的空格
’semi-spacing’:0,
// 要求同一個(gè)聲明塊中的變量按順序排列
’sort-vars’:0,
// 強(qiáng)制在塊之前使用一致的空格
’space-before-blocks’: [2,’always’],
// 強(qiáng)制在 function的左括號之前使用一致的空格
’space-before-function-paren’: [2,’always’],
// 強(qiáng)制在圓括號內(nèi)使用一致的空格
’space-in-parens’: [2,’never’],
// 要求操作符周圍有空格
’space-infix-ops’:2,
// 強(qiáng)制在一元操作符前后使用一致的空格
’space-unary-ops’: [2, {’words’:true,’nonwords’:false}],
// 強(qiáng)制在注釋中 // 或 /* 使用一致的空格
’spaced-comment’: [2,’always’, {’markers’: [’global’,’globals’,’eslint’,’eslint-disable’,’*package’,’!’] }],
// 要求或禁止 Unicode BOM
’unicode-bom’:0,
// 要求正則表達(dá)式被括號括起來
’wrap-regex’:0,
//////////////
// ES6.相關(guān) //
//////////////
// 要求箭頭函數(shù)體使用大括號
’arrow-body-style’:2,
// 要求箭頭函數(shù)的參數(shù)使用圓括號
’arrow-parens’:2,
’arrow-spacing’:[2,{’before’:true,’after’:true}],
// 強(qiáng)制在子類構(gòu)造函數(shù)中用super()調(diào)用父類構(gòu)造函數(shù),TypeScrip的編譯器也會(huì)提示
’constructor-super’:0,
// 強(qiáng)制 generator 函數(shù)中 * 號周圍使用一致的空格
’generator-star-spacing’: [2, {’before’:true,’after’:true}],
// 禁止修改類聲明的變量
’no-class-assign’:2,
// 不允許箭頭功能,在那里他們可以混淆的比較
’no-confusing-arrow’:0,
// 禁止修改 const 聲明的變量
’no-const-assign’:2,
// 禁止類成員中出現(xiàn)重復(fù)的名稱
’no-dupe-class-members’:2,
// 不允許復(fù)制模塊的進(jìn)口
’no-duplicate-imports’:0,
// 禁止 Symbol 的構(gòu)造函數(shù)
’no-new-symbol’:2,
// 允許指定模塊加載時(shí)的進(jìn)口
’no-restricted-imports’:0,
// 禁止在構(gòu)造函數(shù)中,在調(diào)用 super() 之前使用 this 或 super
’no-this-before-super’:2,
// 禁止不必要的計(jì)算性能鍵對象的文字
’no-useless-computed-key’:0,
// 要求使用 let 或 const 而不是 var
’no-var’:0,
// 要求或禁止對象字面量中方法和屬性使用簡寫語法
’object-shorthand’:0,
// 要求使用箭頭函數(shù)作為回調(diào)
’prefer-arrow-callback’:0,
// 要求使用 const 聲明那些聲明后不再被修改的變量
’prefer-const’:0,
// 要求在合適的地方使用 Reflect 方法
’prefer-reflect’:0,
// 要求使用擴(kuò)展運(yùn)算符而非 .apply()
’prefer-spread’:0,
// 要求使用模板字面量而非字符串連接
’prefer-template’:0,
// Suggest using the rest parameters instead of arguments
’prefer-rest-params’:0,
// 要求generator 函數(shù)內(nèi)有 yield
’require-yield’:0,
// enforce spacing between rest and spread operators and their expressions
’rest-spread-spacing’:0,
// 強(qiáng)制模塊內(nèi)的 import 排序
’sort-imports’:0,
// 要求或禁止模板字符串中的嵌入表達(dá)式周圍空格的使用
’template-curly-spacing’:1,
// 強(qiáng)制在 yield* 表達(dá)式中 * 周圍使用空格
’yield-star-spacing’:2
}
}
extends屬性
一個(gè)配置文件可以被基礎(chǔ)配置中的已啟用的規(guī)則繼承。可以使用以下規(guī)則繼承:
(1)’eslint:recommended’
繼承Eslint中推薦的(打鉤的)規(guī)則項(xiàng)
module.exports = {
’extends’: ’eslint:recommended’,
’rules’: {
}
}
(2)使用別人寫好的規(guī)則包(以eslint-config-開頭的npm包),如eslint-config-standard
module.exports = {
’extends’: ’standard’,
’rules’: {
}
}
(3)使用Eslint插件中命名的配置
module.exports = {
’plugins’: [
’react’
],
’extends’: [
’eslint:recommended’,
’plugin:react/recommended’
],
’rules’: {
’no-set-state’: ’off’
}
}
(4)使用’eslint:all’,繼承Eslint中所有的核心規(guī)則項(xiàng)
module.exports = {
’extends’: ’eslint:all’,
’rules’: {
// override default options
’comma-dangle’: [’error’, ’always’],
’indent’: [’error’, 2],
’no-cond-assign’: [’error’, ’always’],
// disable now, but enable in the future
’one-var’: ’off’, // [’error’, ’never’]
// disable
’init-declarations’: ’off’,
’no-console’: ’off’,
’no-inline-comments’: ’off’,
}
}
/* for vue */
// 禁止重復(fù)的二級鍵名
// @off 沒必要限制
'vue/no-dupe-keys': 'off',
// 禁止出現(xiàn)語法錯(cuò)誤
'vue/no-parsing-error': 'error',
// 禁止覆蓋保留字
'vue/no-reservered-keys': 'error',
// 組件的 data 屬性的值必須是一個(gè)函數(shù)
// @off 沒必要限制
'vue/no-shared-component-data': 'off',
// 禁止 <template> 使用 key 屬性
// @off 太嚴(yán)格了
'vue/no-template-key': 'off',
// render 函數(shù)必須有返回值
'vue/require-render-return': 'error',
// prop 的默認(rèn)值必須匹配它的類型
// @off 太嚴(yán)格了
'vue/require-valid-default-prop': 'off',
// 計(jì)算屬性必須有返回值
'vue/return-in-computed-property': 'error',
// template 的根節(jié)點(diǎn)必須合法
'vue/valid-template-root': 'error',
// v-bind 指令必須合法
'vue/valid-v-bind': 'error',
// v-cloak 指令必須合法
'vue/valid-v-cloak': 'error',
// v-else-if 指令必須合法
'vue/valid-v-else-if': 'error',
// v-else 指令必須合法
'vue/valid-v-else': 'error',
// v-for 指令必須合法
'vue/valid-v-for': 'error',
// v-html 指令必須合法
'vue/valid-v-html': 'error',
// v-if 指令必須合法
'vue/valid-v-if': 'error',
// v-model 指令必須合法
'vue/valid-v-model': 'error',
// v-on 指令必須合法
'vue/valid-v-on': 'error',
// v-once 指令必須合法
'vue/valid-v-once': 'error',
// v-pre 指令必須合法
'vue/valid-v-pre': 'error',
// v-show 指令必須合法
'vue/valid-v-show': 'error',
// v-text 指令必須合法
'vue/valid-v-text': 'error', //
// 最佳實(shí)踐 //
// @fixable html 的結(jié)束標(biāo)簽必須符合規(guī)定
// @off 有的標(biāo)簽不必嚴(yán)格符合規(guī)定,如 <br> 或 <br/> 都應(yīng)該是合法的
'vue/html-end-tags': 'off',
// 計(jì)算屬性禁止包含異步方法
'vue/no-async-in-computed-properties': 'error',
// 禁止出現(xiàn)難以理解的 v-if 和 v-for
'vue/no-confusing-v-for-v-if': 'error',
// 禁止出現(xiàn)重復(fù)的屬性
'vue/no-duplicate-attributes': 'error',
// 禁止在計(jì)算屬性中對屬性修改
// @off 太嚴(yán)格了
'vue/no-side-effects-in-computed-properties': 'off',
// 禁止在 <textarea> 中出現(xiàn) {{message}}
'vue/no-textarea-mustache': 'error',
// 組件的屬性必須為一定的順序
'vue/order-in-components': 'error',
// <component> 必須有 v-bind:is
'vue/require-component-is': 'error',
// prop 必須有類型限制
// @off 沒必要限制
'vue/require-prop-types': 'off',
// v-for 指令的元素必須有 v-bind:key
'vue/require-v-for-key': 'error', //
// 風(fēng)格問題 //
// @fixable 限制自定義組件的屬性風(fēng)格 // @off 沒必要限制
'vue/attribute-hyphenation': 'off',
// html 屬性值必須用雙引號括起來
'vue/html-quotes': 'error',
// @fixable 沒有內(nèi)容時(shí),組件必須自閉和 // @off 沒必要限制
'vue/html-self-closing': 'off',
// 限制每行允許的最多屬性數(shù)量 // @off 沒必要限制
'vue/max-attributes-per-line': 'off',
// @fixable 限制組件的 name 屬性的值的風(fēng)格 // @off 沒必要限制
'vue/name-property-casing': 'off',
// @fixable 禁止出現(xiàn)連續(xù)空格 // TODO: 李德廣 觸發(fā) 新版本 typeerror:get 'range' of undefined
// 'vue/no-multi-spaces': 'error',
// @fixable 限制 v-bind 的風(fēng)格 // @off 沒必要限制
'vue/v-bind-style': 'off',
// @fixable 限制 v-on 的風(fēng)格 // @off 沒必要限制
'vue/v-on-style': 'off',
// 定義了的 jsx element 必須使用
'vue/jsx-uses-vars': 'error'