日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ESLint 规则

發(fā)布時(shí)間:2025/4/16 67 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ESLint 规则 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ESLint由 JavaScript 紅寶書 作者 Nicholas C.Zakas 編寫, 2013 年發(fā)布第一個(gè)版本。

ESLint是一個(gè)以可擴(kuò)展、每條規(guī)則獨(dú)立的,被設(shè)計(jì)為完全可配置的lint工具,一個(gè)QA工具,用來作為靜態(tài)代碼檢查,避免低級(jí)錯(cuò)誤和統(tǒng)一代碼的風(fēng)格。

主要有以下特點(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

npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise

?

使用

$ eslint --init

將會(huì)初始化和生成.eslintrc.*文件

?

?

執(zhí)行檢查命令

$ eslint add.js

?

指定配置文件

$ eslint - c config.json add.js?

?

打印出eslint所使用的配置和規(guī)則

$?eslint --print-config ?xx.js > 1 ?

?

配置

?

有三種方法可以配置ESLint

? ? > 使用.eslintrc.*文件( 支持JSON和YAML兩種語(yǔ)法)

? ? > 在package.json中添加eslintConfig配置塊

? ? > 使用JavaScript注釋直接把配置嵌入到文件中

?

?

?

ESLint規(guī)則的設(shè)置具體格式如下:

? ? "quotes": "error"

#第一部分是規(guī)則名

#第二部分包含:

規(guī)則的嚴(yán)重性(rule severity)

"off"

or 0 - turn the rule off 不驗(yàn)證 "warn"

or 1 - turn the rule on as a warning(doesn’ t affect exit code) 警告 "error"

or 2 - turn the rule on as an error(exit code is 1 when triggered) 錯(cuò)誤

( 如有) 規(guī)則的選項(xiàng)(additional options)

"quotes": [2, "double"]

?

?

?

配置文件:

?

//表示使用默認(rèn)的規(guī)則進(jìn)行校驗(yàn)

"extends": "eslint:recommended"

?

文件中關(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 */

?

Migrating to v2.0.0

?All ECMAScript 6 ecmaFeatures flags have been removed

?

?The ECMAScript 6 feature flags :

arrowFunctions - enable arrow functions

binaryLiterals - enable binary literals

blockBindings - enable let and const (aka block bindings)

classes - enable classes

defaultParams - enable default function parameters

destructuring - enable destructuring

forOf - enable for-of loops

generators - enable generators

modules - enable modules and global strict mode

objectLiteralComputedProperties - enable computed object literal property names

objectLiteralDuplicateProperties - enable duplicate object literal properties in strict mode

objectLiteralShorthandMethods - enable object literal shorthand methods

objectLiteralShorthandProperties - enable object literal shorthand properties

octalLiterals - enable octal literals

regexUFlag - enable the regular expression u flag

regexYFlag - enable the regular expression y flag

restParams - enable the rest parameters

spread - enable the spread operator for arrays

superInFunctions - enable super references inside of functions

templateStrings - enable template strings

unicodeCodePointEscapes - enable code point escapes

?

"ecmaFeatures": {

?// lambda表達(dá)式 ?

"arrowFunctions": true,

?// 塊級(jí)作用域,允許使用let const ?

"blockBindings": true,

// class ?

"classes": true,

?// http://es6.ruanyifeng.com/#docs/function#函數(shù)參數(shù)的默認(rèn)值 ?

"defaultParams": true,

?// 解構(gòu)賦值 ?

"destructuring": true,

// http://es6.ruanyifeng.com/#docs/iterator#for---of循環(huán) ?

"forOf": true,

?// http://es6.ruanyifeng.com/#docs/generator ?

?"generators": true,

// 允許使用模塊,模塊內(nèi)默認(rèn)嚴(yán)格模式 ?

"modules": true,

// 允許字面量定義對(duì)象時(shí),用表達(dá)式做屬性名 ?

// http://es6.ruanyifeng.com/#docs/object#屬性名表達(dá)式 ?

"objectLiteralComputedProperties": true,

// 允許對(duì)象字面量方法名簡(jiǎn)寫?

?"objectLiteralShorthandMethods": true,

// 對(duì)象字面量屬性名簡(jiǎn)寫 ? ? ??

"objectLiteralShorthandProperties": true,

// http://es6.ruanyifeng.com/#docs/function#rest參數(shù) ?

"restParams": true,

// http://es6.ruanyifeng.com/#docs/function#擴(kuò)展運(yùn)算符 ?

"spread": true,

"superInFunctions": true,

// http://es6.ruanyifeng.com/#docs/string#模板字符串 ?

"templateStrings": true,

"unicodeCodePointEscapes": true,

} ? ? ? ? ??

? ? ? ? ? ??

?

?

規(guī)則說明?? ? ESLint v2.4.0

"rules": {

?

? ? ? ? /*Possible Errors*/

? ? ? ? // 數(shù)組和對(duì)象鍵值對(duì)最后一個(gè)逗號(hào),?

? ? ? ? // never參數(shù):不能帶末尾的逗號(hào),?

? ? ? ? // always參數(shù):必須帶末尾的逗號(hào), ?

? ? ? ? // always-multiline:多行模式必須帶逗號(hào),單行模式不能帶逗號(hào) ?

? ? ? ? "comma-dangle": [2, "never"],

? ? ? ? //禁止在條件表達(dá)式中使用賦值語(yǔ)句

? ? ? ? "no-cond-assign": 2,

? ? ? ? //禁止使用console?

? ? ? ? "no-console": 2,

? ? ? ? //禁止在條件中使用常量表達(dá)式 if(true) if(1)

? ? ? ? "no-constant-condition": 2,

? ? ? ? //禁止在正則表達(dá)式中使用控制符

? ? ? ? "no-control-regex": 2,

? ? ? ? //禁止使用debugger語(yǔ)句

? ? ? ? "no-debugger": 2,

? ? ? ? //函數(shù)參數(shù)禁止重名

? ? ? ? "no-dupe-args": 2,

? ? ? ? //在創(chuàng)建對(duì)象字面量時(shí)不允許鍵重復(fù)?

? ? ? ? "no-dupe-keys": 2,

? ? ? ? //在switch語(yǔ)句中禁止重復(fù)的case

? ? ? ? "no-duplicate-case": 2,

? ? ? ? //代碼塊的內(nèi)容不能為空,禁止空代碼塊

? ? ? ? "no-empty": 2,

? ? ? ? //正則表達(dá)式的內(nèi)容不能為空,禁止使用不匹配任何字符串的正則表達(dá)式

? ? ? ? "no-empty-character-class": 2,

? ? ? ? //禁止對(duì)catch語(yǔ)句中的異常進(jìn)行賦值

? ? ? ? "no-ex-assign": 2,

? ? ? ? //禁止不必要的bool轉(zhuǎn)換

? ? ? ? "no-extra-boolean-cast": 2,

? ? ? ? //禁止使用多余的圓括號(hào)

? ? ? ? "no-extra-parens": 2,

? ? ? ? //禁止多余的冒號(hào)

? ? ? ? "no-extra-semi": 2,

? ? ? ? //禁止重復(fù)的函數(shù)聲明

? ? ? ? "no-func-assign": 2,

? ? ? ? //禁止在塊語(yǔ)句中聲明變量或函數(shù)

? ? ? ? "no-inner-declarations": 2,

? ? ? ? //禁止使用無效的正則語(yǔ)句

? ? ? ? "no-invalid-regexp": 2,

? ? ? ? //禁止使用不合法或者不規(guī)則的空白符

? ? ? ? "no-irregular-whitespace": 2,

? ? ? ? //在in操作符左邊的操作項(xiàng)不能用! 例如這樣寫不對(duì)的:if ( !a in b) { //dosomething }

? ? ? ? "no-negated-in-lhs": 2,

? ? ? ? //禁止把全局對(duì)象當(dāng)函數(shù)調(diào)用,比如下面寫法錯(cuò)誤的:Math(), JSON()

? ? ? ? "no-obj-calls": 2,

? ? ? ? //禁止在正則表達(dá)式字面量中使用多個(gè)空格 /foo bar/

? ? ? ? "no-regex-spaces": 2,

? ? ? ? //禁止稀疏數(shù)組,清除多余的逗號(hào)申明 ?比如[1,,2]

? ? ? ? "no-sparse-arrays": 2,

? ? ? ? //為了保證兩行不相關(guān)的代碼不會(huì)意外的被當(dāng)做一行代碼來解析

? ? ? ? "no-unexpected-multiline": 2,

? ? ? ? //禁止有執(zhí)行不到的代碼

? ? ? ? "no-unreachable": 2,

? ? ? ? //禁止和NaN作比較,推薦使用isNaN方法

? ? ? ? "use-isnan": 2,

? ? ? ? //用來檢測(cè)JSDoc是否完整和合法

? ? ? ? "valid-jsdoc": 2,

? ? ? ? //typeof操作符返回的結(jié)果會(huì)是 "undefined", ?"object", ?"boolean", "number", "string", 和 ?"function"之一。

? ? ? ? //保證typeof 操作符返回的結(jié)果必須和上面六個(gè)字符串作比較

? ? ? ? "valid-typeof": 2,

?

?

?

? ? ? ? /*Best Practices*/

? ? ? ? //在聲明對(duì)象時(shí)getter和setter需成對(duì)出現(xiàn)

? ? ? ? "accessor-pairs": 2,

? ? ? ? //數(shù)值方法的回調(diào)函數(shù)中強(qiáng)制寫return語(yǔ)句

? ? ? ? "array-callback-return": 2,

? ? ? ? //當(dāng)在代碼塊中用var聲明變量,并在代碼塊外使用時(shí)報(bào)錯(cuò)?

? ? ? ? "block-scoped-var": 0,

? ? ? ? //用來控制函數(shù)的復(fù)雜度,分支超過5時(shí)報(bào)錯(cuò)

? ? ? ? "complexity": [2, 5],

? ? ? ? //不同分支的return語(yǔ)句不能返回不同的類型,要么一致要么都沒有 ?

? ? ? ? "consistent-return": 0,

? ? ? ? // if else while for do后面的代碼塊是否需要{ }包圍,參數(shù): ?

? ? ? ? // multi ? ? ? ? 只有塊中有多行語(yǔ)句時(shí)才需要{ }包圍 ?

? ? ? ? // multi-line ? ?只有塊中有多行語(yǔ)句時(shí)才需要{ }包圍, 但是塊中的執(zhí)行語(yǔ)句只有一行時(shí),塊中的語(yǔ)句只能跟和if語(yǔ)句在同一行。

? ? ? ? // ? ? ? ? ? ? ? ?if (foo) foo++; else doSomething(); ?

? ? ? ? // multi-or-nest 只有塊中有多行語(yǔ)句時(shí)才需要{ }包圍, 如果塊中的執(zhí)行語(yǔ)句只有一行,執(zhí)行語(yǔ)句可以另起一行也可以跟在if語(yǔ)句后面 ? ?

? ? ? ? // [2, "multi", "consistent"] 保持前后語(yǔ)句的{ }一致 ?

? ? ? ? // default: [2, "all"] 全都需要{ }包圍 ?

? ? ? ? "curly": 2,

? ? ? ? //所有的switch語(yǔ)句都必須要有一個(gè)default分支

? ? ? ? "default-case": 2,

? ? ? ? // 在書寫對(duì)象的屬性或方法時(shí),新的一行代碼可以以. 開頭,也可以以. 結(jié)束。

? ? ? ? // 強(qiáng)制統(tǒng)一object.key中 . 的位置,參數(shù): ?

? ? ? ? // ? ? ?property,'.'號(hào)應(yīng)與屬性在同一行 ?

? ? ? ? // ? ? ?object, '.' 號(hào)應(yīng)與對(duì)象名在同一行 ?

? ? ? ? "dot-location": [2, "property"],

? ? ? ? // 強(qiáng)制使用.號(hào)取屬性 ?

? ? ? ? // 參數(shù): allowKeywords:true ?使用保留字做屬性名時(shí),只能使用.方式取屬性 ?

? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? false 使用保留字做屬性名時(shí), 只能使用[]方式取屬性?

? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? e.g [2, {"allowKeywords": false}] ?

? ? ? ? // ? ? ? ?allowPattern: ?當(dāng)屬性名匹配提供的正則表達(dá)式時(shí),允許使用[]方式取值,否則只能用.號(hào)取值?

? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? e.g [2, {"allowPattern": "^[a-z]+(_[a-z]+)+$"}] ?

? ? ? ? "dot-notation": [2, { "allowKeywords": true }],

? ? ? ? //在進(jìn)行比較時(shí),必須使用全等=== 和完全不等!==

? ? ? ? "eqeqeq": [2, "allow-null"],

? ? ? ? //在for-in 循環(huán)中要使用if語(yǔ)句

? ? ? ? "guard-for-in": 2,

? ? ? ? //代碼中禁止使用alert, confirm, and prompt

? ? ? ? "no-alert": 2,

? ? ? ? //禁止使用arguments.caller和arguments.callee

? ? ? ? "no-caller": 2,

? ? ? ? //禁止在case/default語(yǔ)句中使用lexical declarations,例如let, const, function and class

? ? ? ? //因?yàn)樵赾ase/default中的聲明,在整個(gè)switch語(yǔ)句中都能夠訪問到,如果需要聲明變量,可以加大括號(hào)。

? ? ? ? "no-case-declarations": 2,

? ? ? ? //不能使用看起來像除法的正則表達(dá)式

? ? ? ? //用來消除/ (除號(hào))操作符對(duì)程序員的迷惑,比如在正則表達(dá)式/=foo/中,我們并不能夠確定第一個(gè)/是除號(hào)還是正則表達(dá)式,因此我們需要在等號(hào)前面加一個(gè)轉(zhuǎn)移符/\=foo/

? ? ? ? "no-div-regex": 2,

? ? ? ? //在if else語(yǔ)句中,如果else語(yǔ)句中只含有一個(gè)return語(yǔ)句,那么完全可以不使用else語(yǔ)句,直接return。

? ? ? ? "no-else-return": 2,

? ? ? ? //不允許空函數(shù)

? ? ? ? "no-empty-function": 2,

? ? ? ? //在結(jié)構(gòu)賦值時(shí),模式不能為空。在ECMAScript2015的結(jié)構(gòu)賦值中,模式為空是不會(huì)報(bào)錯(cuò)的,只是這樣的結(jié)構(gòu)賦值沒有任何效果,該條規(guī)則就保證了模式不能為空,也就保證了結(jié)構(gòu)賦值的有效性。

? ? ? ? "no-empty-pattern": 2,

? ? ? ? //保證了在和null比較時(shí)使用===和!==,而不能夠使用==和!=

? ? ? ? "no-eq-null": 2,

? ? ? ? //禁止使用eval函數(shù)

? ? ? ? "no-eval": 2,

? ? ? ? //禁止擴(kuò)展native對(duì)象,不能向native的對(duì)象上面添加屬性

? ? ? ? "no-extend-native": 2,

? ? ? ? //保證了調(diào)用bind方法的函數(shù)體內(nèi)有this對(duì)象。規(guī)避了不必要的使用bind方法的情況。

? ? ? ? //箭頭函數(shù)中沒有this對(duì)象,也就不能夠使用bind()方法。該規(guī)則保證了在所有的箭頭函數(shù)中使用bind方法將被視為錯(cuò)誤。

? ? ? ? "no-extra-bind": 2,

? ? ? ? //如果 loop中沒有內(nèi)嵌的loops或switches, loop標(biāo)簽是不必要的.

? ? ? ? "no-extra-label": 2,

? ? ? ? //在case語(yǔ)句中盡量加break,避免不必要的fallthrough錯(cuò)誤,消除從一個(gè)case到另一個(gè)case的非故意的「fall through」。

? ? ? ? //如果沒有添加break等終止語(yǔ)句或者沒有添加注釋語(yǔ)句,將會(huì)拋出錯(cuò)誤

? ? ? ? "no-fallthrough": 2,

? ? ? ? //在使用浮點(diǎn)小數(shù)時(shí),不能夠省略小數(shù)點(diǎn)前面的數(shù)或者后面的數(shù),必須寫。比如.2 2. 應(yīng)該寫2.2 2.0?

? ? ? ? "no-floating-decimal": 2,

? ? ? ? //禁止隱式轉(zhuǎn)換,為了消除簡(jiǎn)寫的類型轉(zhuǎn)換

? ? ? ? "no-implicit-coercion": 2,

? ? ? ? //禁止在全局作用域里聲明變量或函數(shù)

? ? ? ? "no-implicit-globals": 2,

? ? ? ? //在setTimeout(), setInterval() or execScript()中消除隱式eval的使用

? ? ? ? "no-implied-eval": 2,

? ? ? ? //禁止無效的this,只能用在構(gòu)造器,類,對(duì)象字面量

? ? ? ? "no-invalid-this": 2,

? ? ? ? //禁止使用__iterator__屬性

? ? ? ? "no-iterator": 2,

? ? ? ? //禁止使用label語(yǔ)句,以避免無限循環(huán)

? ? ? ? "no-labels": [2, { "allowLoop": false, "allowSwitch": false }],

? ? ? ? //禁止使用不必要的嵌套代碼塊

? ? ? ? "no-lone-blocks": 2,

? ? ? ? //禁止在循環(huán)體中定義函數(shù)并且函數(shù)引用了外部變量

? ? ? ? //在循環(huán)中定義了函數(shù),但是函數(shù)內(nèi)部沒有引用外部變量,或者使用let定義的代碼塊變量,視為合法

? ? ? ? "no-loop-func": 2,

? ? ? ? //禁止使用魔法數(shù)字,建議使用常量來代替

? ? ? ? "no-magic-numbers": 2,

? ? ? ? //保證了在邏輯表達(dá)式、條件表達(dá)式、申明語(yǔ)句、數(shù)組元素、對(duì)象屬性、sequences、函數(shù)參數(shù)中不使用超過一個(gè)的空白符。

? ? ? ? "no-multi-spaces": 2,

? ? ? ? //該規(guī)則保證了字符串不分行書寫。

? ? ? ? "no-multi-str": 2,

? ? ? ? //該規(guī)則保證了不重寫原生對(duì)象。

? ? ? ? "no-native-reassign": 2,

? ? ? ? //在使用new來調(diào)用構(gòu)造函數(shù)后,必須把生成的實(shí)例賦值給一個(gè)變量

? ? ? ? "no-new": 2,

? ? ? ? //禁止使用new Function(); 語(yǔ)句。

? ? ? ? "no-new-func": 2,

? ? ? ? //禁止使用new創(chuàng)建String,Number, and Boolean實(shí)例

? ? ? ? "no-new-wrappers": 2,

? ? ? ? //禁止使用八進(jìn)制數(shù)字

? ? ? ? "no-octal": 2,

? ? ? ? //禁止使用八進(jìn)制轉(zhuǎn)義序列,比如 var foo = "Copyright \251";

? ? ? ? "no-octal-escape": 2,

? ? ? ? //禁止對(duì)函數(shù)的參數(shù)重新進(jìn)行無意義的賦值

? ? ? ? "no-param-reassign": 2,

? ? ? ? //禁止使用__proto__屬性

? ? ? ? "no-proto": 2,

? ? ? ? //避免重復(fù)聲明一個(gè)變量

? ? ? ? "no-redeclare": [2, { "builtinGlobals": true }],

? ? ? ? //不要在return語(yǔ)句中使用賦值語(yǔ)句

? ? ? ? "no-return-assign": [2, "always"],

? ? ? ? //禁止代碼中使用類似javascript:void(0)的javascript: urls.

? ? ? ? "no-script-url": 2,

? ? ? ? //禁止給自身賦值

? ? ? ? "no-self-assign": 2,

? ? ? ? //禁止和自身作比較

? ? ? ? "no-self-compare": 2,

? ? ? ? //禁止可能導(dǎo)致結(jié)果不明確的逗號(hào)操作符

? ? ? ? "no-sequences": 2,

? ? ? ? //通過throw語(yǔ)句拋出的對(duì)象必須是Error對(duì)象本身或者通過Error對(duì)象定義的對(duì)象。有些情況除外,見官網(wǎng)

? ? ? ? "no-throw-literal": 2,

? ? ? ? //禁止使用不被修改的循環(huán)條件

? ? ? ? "no-unmodified-loop-condition": 2,

? ? ? ? //禁止在代碼中出現(xiàn)沒有被使用到的表達(dá)式或值

? ? ? ? "no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true }],

? ? ? ? //禁止在代碼中出現(xiàn)沒有被使用到的標(biāo)簽

? ? ? ? "no-unused-labels": 2,

? ? ? ? //避免使用沒有意義的call() 和 apply()

? ? ? ? "no-useless-call": 2,

? ? ? ? //避免使用不必要的字符串拼接

? ? ? ? "no-useless-concat": 2,

? ? ? ? //不要使用void操作符

? ? ? ? "no-void": 2,

? ? ? ? //生產(chǎn)代碼中不能出現(xiàn)warning-comments包含的注釋

? ? ? ? "no-warning-comments": [2, { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }],

? ? ? ? //不要使用with語(yǔ)句

? ? ? ? "no-with": 2,

? ? ? ? //在使用parseInt()方法時(shí),必須要傳遞第二個(gè)參數(shù)來幫助解析。

? ? ? ? "radix": 2,

? ? ? ? //在通過var聲明變量時(shí),應(yīng)該放在代碼所在作用域的頂部

? ? ? ? "vars-on-top": 2,

? ? ? ? //立即執(zhí)行函數(shù)需要通過圓括號(hào)包圍

? ? ? ? "wrap-iife": 2,

? ? ? ? //yoda條件語(yǔ)句就是對(duì)象字面量應(yīng)該寫在比較操作符的左邊,而變量應(yīng)該寫在比較操作符的右邊

? ? ? ? //默認(rèn)的規(guī)則要求,變量寫在左邊而字面量寫在右邊

? ? ? ? "yoda": 2,

?

? ? ? ? /*Strict Mode*/

? ? ? ? //使用嚴(yán)格模式

? ? ? ? "strict": 2,

?

?

? ? ? ? /*Variables*/

? ? ? ? //變量聲明時(shí)必須賦初值

? ? ? ? "init-declarations": 2,

? ? ? ? //In IE 8 and earlier,禁止catch子句參數(shù)與外部作用域變量同名

? ? ? ? "no-catch-shadow": 2,

? ? ? ? //禁止使用delete刪除var聲明的變量

? ? ? ? "no-delete-var": 2,

? ? ? ? //防止label和聲明的變量重名

? ? ? ? "no-label-var": 2,

? ? ? ? //禁止使用某些全局變量

? ? ? ? "no-restricted-globals": [2, "event"],

? ? ? ? //禁止聲明外部作用域中已定義的變量

? ? ? ? "no-shadow": 2,

? ? ? ? //聲明變量時(shí)禁止覆蓋JavaScript中的一些保留關(guān)鍵字,比如NaN、Infinity、undefined、eval、arguments等。

? ? ? ? "no-shadow-restricted-names": 2,

? ? ? ? //禁止使用未被定義的變量,除非已在配置文件的global中進(jìn)行了說明。

? ? ? ? "no-undef": 2,

? ? ? ? //禁止初始化變量為undefined

? ? ? ? "no-undef-init": 2,

? ? ? ? //禁止把undefined作為變量名

? ? ? ? "no-undefined": 2,

? ? ? ? //不允許定義的變量在后面的代碼中沒有被使用到

? ? ? ? "no-unused-vars": 2,

? ? ? ? //所有的變量都應(yīng)該先定義后使用

? ? ? ? "no-use-before-define": 2,

?

?

?

? ? ? ? /*Node.js and CommonJS*/

? ? ? ? //強(qiáng)制回調(diào)后return,避免多次調(diào)用回調(diào)

? ? ? ? "callback-return": 2,

? ? ? ? //強(qiáng)制require()出現(xiàn)在模塊作用域的頂部

? ? ? ? "global-require": 2,

? ? ? ? // 如果函數(shù)有err入?yún)?err或者error),在函數(shù)體內(nèi)必須進(jìn)行處理

? ? ? ? "handle-callback-err": [2, "^(err|error)$"],

? ? ? ? //聲明時(shí)不能混用聲明類型

? ? ? ? "no-mixed-requires": 2,

? ? ? ? //禁止把require方法和new操作符一起使用。

? ? ? ? "no-new-require": 2,

? ? ? ? //不能使用__dirname或__filename做路徑拼接

? ? ? ? "no-path-concat": 2,

? ? ? ? //禁止使用process.env

? ? ? ? "no-process-env": 2,

? ? ? ? //禁止使用process.exit()

? ? ? ? "no-process-exit": 2,

? ? ? ? //禁用使用指定模塊,使用了就會(huì)報(bào)錯(cuò)

? ? ? ? "no-restricted-modules": [2, "fs"],

? ? ? ? //禁止使用同步方法,建議使用異步方法

? ? ? ? "no-sync": 2,

?

?

? ? ? ? /*Stylistic Issues*/

? ? ? ? // 用數(shù)組字面量定義數(shù)組時(shí)數(shù)組元素前后是否加空格,?

? ? ? ? // never參數(shù): 數(shù)組元素前后不能帶空格,

? ? ? ? // always參數(shù):數(shù)組元素前后必須留空格 ?

? ? ? ? "array-bracket-spacing": [2, "never"],

? ? ? ? //在單行代碼塊中,代碼塊前后是否需要留空格

? ? ? ? // always參數(shù):默認(rèn),前后必須留空格

? ? ? ? // never參數(shù): 前后不能帶空格 ?

? ? ? ? "block-spacing": [2, "always"],

? ? ? ? //大括號(hào)的樣式,比如下面的大括號(hào)語(yǔ)法采用『1tbs』,允許單行樣式

? ? ? ? "brace-style": [2, "1tbs", { "allowSingleLine": true }],

? ? ? ? //強(qiáng)制使用駝峰命名 ?

? ? ? ? "camelcase": 2,

? ? ? ? //規(guī)定了逗號(hào)前后的空白,默認(rèn)配置規(guī)定逗號(hào)前面沒有空白,而逗號(hào)后面需要留空白

? ? ? ? "comma-spacing": [2, { "before": false, "after": true }],

? ? ? ? //規(guī)定了逗號(hào)放的位置,默認(rèn)配置逗號(hào)應(yīng)該放在行末,如果設(shè)置為first,逗號(hào)就應(yīng)放在行首

? ? ? ? "comma-style": [2, "last"],

? ? ? ? //是否在對(duì)象的動(dòng)態(tài)屬性(computed properties: ES6引入)中添加空白,默認(rèn)配置不添加空白

? ? ? ? "computed-property-spacing": [2, "never"],

? ? ? ? //統(tǒng)一this的別名(this賦值的變量名)保證整個(gè)應(yīng)用程序代碼的統(tǒng)一。

? ? ? ? //如果一個(gè)變量被指定為this對(duì)象的別名,那么這個(gè)變量就不能夠用來賦其他值,只能夠用來保存this對(duì)象。

? ? ? ? //如果this對(duì)象明確被賦值給了一個(gè)變量,那么這個(gè)變量應(yīng)該是配置中指定的那個(gè)變量名。 ? ??

? ? ? ? "consistent-this": [2, "self"],

? ? ? ? //該規(guī)則規(guī)定文件最后強(qiáng)制換行,僅需留一空行

? ? ? ? "eol-last": 2,

? ? ? ? //要求給函數(shù)表達(dá)式命名,便于debug

? ? ? ? "func-names": 2,

? ? ? ? //在JavaScript中有兩種方式定義函數(shù):函數(shù)聲明和函數(shù)表達(dá)式。

? ? ? ? //函數(shù)聲明就是把function關(guān)鍵詞寫在最前面,后面跟一個(gè)函數(shù)名。我們可以在函數(shù)申明代碼前調(diào)用函數(shù)

? ? ? ? //函數(shù)表達(dá)式是通過var等聲明變量的關(guān)鍵字開頭,然后跟函數(shù)名,再后面是function本身。在使用函數(shù)表達(dá)式定義函數(shù)前調(diào)用函數(shù)會(huì)報(bào)錯(cuò)

? ? ? ? // 統(tǒng)一定義函數(shù)是所采用的方式,參數(shù): ?

? ? ? ? // ? ?declaration: 強(qiáng)制使用方法聲明的方式,function f(){} e.g [2, "declaration"] ?

? ? ? ? // ? ?expression:強(qiáng)制使用方法表達(dá)式的方式,默認(rèn)方式,var f = function() {} ?e.g [2, "expression"] ?

? ? ? ? // ? ?allowArrowFunctions: declaration風(fēng)格中允許箭頭函數(shù)。 e.g [2, "declaration", {"allowArrowFunctions":true}] ?

? ? ? ? "func-style": [2, "expression"],

? ? ? ? //規(guī)定了標(biāo)識(shí)符命名的黑名單

? ? ? ? "id-blacklist": [2, "data", "err", "e", "cb", "callback"],

? ? ? ? //規(guī)定標(biāo)識(shí)符的長(zhǎng)度,默認(rèn)配置標(biāo)識(shí)符最少兩個(gè)字符

? ? ? ? "id-length": [2, { "min": 2 }],

? ? ? ? //命名檢測(cè),標(biāo)識(shí)符命名需和配置中的正則表達(dá)式匹配,但是該規(guī)則對(duì)函數(shù)調(diào)用無效。

? ? ? ? "id-match": [2, "^[a-z]+([A-Z][a-z]+)*$", { "properties": false }],

? ? ? ? // 統(tǒng)一代碼縮進(jìn)方式,默認(rèn)值是4 spaces.

? ? ? ? "indent": 2,

? ? ? ? //規(guī)定了在JSX中的屬性值是使用單引號(hào)還是雙引號(hào),默認(rèn)使用雙引號(hào)

? ? ? ? "jsx-quotes": [2, "prefer-double"],

? ? ? ? //該規(guī)則規(guī)定了在對(duì)象字面量語(yǔ)法中key和value之間的空白,冒號(hào)前不要留空格,冒號(hào)后面需留一個(gè)空格

? ? ? ? "key-spacing": [2, { "beforeColon": false, "afterColon": true }],

? ? ? ? // 規(guī)定了keyword前后是否需要留一個(gè)空格

? ? ? ? "keyword-spacing": [2, { "before": true, "after": true, "overrides": {} }],

? ? ? ? //統(tǒng)一換行符,"\n" unix(for LF) and "\r\n" for windows(CRLF),默認(rèn)unix

? ? ? ? "linebreak-style": 2,

? ? ? ? //規(guī)定注釋和代碼塊之間是否留空行

? ? ? ? "lines-around-comment": 2,

? ? ? ? //規(guī)定代碼最多可以嵌套多少層

? ? ? ? "max-depth": [2, 4],

? ? ? ? //規(guī)定了代碼單行的最大長(zhǎng)度

? ? ? ? "max-len": [2, 80, 4],

? ? ? ? //規(guī)定了回調(diào)的最大嵌套層數(shù)

? ? ? ? "max-nested-callbacks": [2, 10],

? ? ? ? //規(guī)定了函數(shù)參數(shù)的最大個(gè)數(shù)

? ? ? ? "max-params": [2, 3],

? ? ? ? //規(guī)定了函數(shù)中代碼不能夠超過多少行

? ? ? ? "max-statements": [2, 10],

? ? ? ? //使用構(gòu)造函數(shù)(new)時(shí)首字母需大寫,首字母大寫的函數(shù)需用new操作符

? ? ? ? "new-cap": 2,

? ? ? ? //使用構(gòu)造函數(shù)(new)時(shí)必須圓括號(hào)不能省略

? ? ? ? "new-parens": 2,

? ? ? ? //規(guī)定了變量聲明后是否需要空行

? ? ? ? "newline-after-var": 2,

? ? ? ? //規(guī)定了return語(yǔ)句前是否是否需要空行

? ? ? ? "newline-before-return": 2,

? ? ? ? //規(guī)定了方法鏈?zhǔn)秸{(diào)用時(shí)是否需換行

? ? ? ? "newline-per-chained-call": 2,

? ? ? ? //禁止使用Array構(gòu)造函數(shù)

? ? ? ? "no-array-constructor": 2,

? ? ? ? //禁止使用位操作符

? ? ? ? "no-bitwise": 2,

? ? ? ? //禁止使用continue

? ? ? ? "no-continue": 2,

? ? ? ? //禁止使用行內(nèi)注釋

? ? ? ? "no-inline-comments": 2,

? ? ? ? //禁止在if-else控制語(yǔ)句中,else代碼塊中僅包含一個(gè)if語(yǔ)句

? ? ? ? "no-lonely-if": 2,

? ? ? ? //禁止混用tab和空格

? ? ? ? "no-mixed-spaces-and-tabs": 2,

? ? ? ? //不要留超過規(guī)定數(shù)目的空白行

? ? ? ? "no-multiple-empty-lines": [2, { "max": 2 }],

? ? ? ? //在if語(yǔ)句中使用了否定表達(dá)式,同時(shí)else語(yǔ)句又不為空,那么這樣的if-else語(yǔ)句將被視為不合法,為什么不將其反過來這樣代碼更容易理解,該規(guī)則同樣適用于三元操作符

? ? ? ? "no-negated-condition": 2,

? ? ? ? //三元操作符禁止嵌套

? ? ? ? "no-nested-ternary": 2,

? ? ? ? //禁止使用new Object()來構(gòu)造對(duì)象

? ? ? ? "no-new-object": 2,

? ? ? ? //禁止使用++,--

? ? ? ? "no-plusplus": 2,

? ? ? ? //禁止使用某些特定的JavaScript語(yǔ)法,例如FunctionDeclaration 和 WithStatement

? ? ? ? "no-restricted-syntax": [2, "FunctionExpression", "WithStatement"],

? ? ? ? //函數(shù)調(diào)用時(shí),函數(shù)名和圓括號(hào)之間不能有空格

? ? ? ? "no-spaced-func": 2,

? ? ? ? //禁止使用三元操作符

? ? ? ? "no-ternary": 2,

? ? ? ? //禁止行末加空格

? ? ? ? "no-trailing-spaces": 2,

? ? ? ? //禁止在標(biāo)識(shí)符前后使用下劃線

? ? ? ? "no-underscore-dangle": 2,

? ? ? ? //禁止使用沒有必要的三元操作符,因?yàn)橛糜行┤僮鞣梢允褂闷渌Z(yǔ)句替換

? ? ? ? "no-unneeded-ternary": [2, { "defaultAssignment": false }],

? ? ? ? //禁止屬性操作符.的前后和[之前有空格

? ? ? ? "no-whitespace-before-property": 2,

? ? ? ? //規(guī)定對(duì)象字面量中大括號(hào)內(nèi)是否允許加空格,也適用于ES6中的結(jié)構(gòu)賦值和模塊import和export

? ? ? ? "object-curly-spacing": [2, "never"],

? ? ? ? //規(guī)定了在每個(gè)函數(shù)中聲明變量是否只使用一次var,該規(guī)則同樣適用于let和const

? ? ? ? "one-var": [2, { "initialized": "never" }],

? ? ? ? //規(guī)定了使用賦值操作符的簡(jiǎn)寫形式

? ? ? ? "operator-assignment": [2, "always"],

? ? ? ? //在換行時(shí)操作符應(yīng)該放在行首還是行尾。還可對(duì)某些操作符進(jìn)行重寫。

? ? ? ? "operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],

? ? ? ? //在代碼塊中,代碼塊的開始和結(jié)尾是否應(yīng)該留一個(gè)空行

? ? ? ? "padded-blocks": 0,

? ? ? ? //對(duì)象的屬性名是否強(qiáng)制加雙引號(hào)

? ? ? ? "quote-props": [2, "always"],

? ? ? ? //在JavaScript中有三種方式定義字符串,雙引號(hào)、單引號(hào)、反義符(ECMAScript2015)。規(guī)定了字符串定義的方式

? ? ? ? "quotes": [2, "single", "avoid-escape"],

? ? ? ? //注釋格式要求JSDoc格式

? ? ? ? "require-jsdoc": [2, {

? ? ? ? ? ? "require": {

? ? ? ? ? ? ? ? "FunctionDeclaration": true,

? ? ? ? ? ? ? ? "MethodDefinition": false,

? ? ? ? ? ? ? ? "ClassDeclaration": false

? ? ? ? ? ? }

? ? ? ? }],

? ? ? ? //JavaScript不要求在每行末尾加上分號(hào),這是因?yàn)镴avaScript引擎會(huì)決定是否需要在行末加上分號(hào),然后自動(dòng)幫我們?cè)谛心┘由戏痔?hào),這一特性被成為ASI(automatic semicolon insertion),也是JavaScript語(yǔ)言最富爭(zhēng)議的特性之一

? ? ? ? //盡管ASI允許我們使用更加自由的代碼風(fēng)格,但是它也可能使得你的代碼并不是按你期許的方式運(yùn)行

? ? ? ? //兩個(gè)可選參數(shù),always 和never?

? ? ? ? //默認(rèn)配置always,要求在行末加上分號(hào)。

? ? ? ? "semi": [2, "always"],

? ? ? ? //該規(guī)則用來規(guī)定分號(hào)前后是否加空格,默認(rèn)配置如下

? ? ? ? "semi-spacing": [2, { "before": false, "after": true }],

? ? ? ? //要求對(duì)同一個(gè)模塊里的import聲明按字母排序

? ? ? ? "sort-imports": 2,

? ? ? ? //規(guī)定在同一個(gè)變量聲明代碼塊中,要對(duì)變量的聲明按字母排序

? ? ? ? "sort-vars": 2,

? ? ? ? //規(guī)定了在代碼塊前是否需要加空格

? ? ? ? "space-before-blocks": [2, "always"],

? ? ? ? //函數(shù)定義時(shí),function關(guān)鍵字后面的小括號(hào)前是否需要加空格

? ? ? ? "space-before-function-paren": [2, "always"],

? ? ? ? //規(guī)定圓括號(hào)內(nèi)部的空格。規(guī)定是否需要在(右邊,或者)左邊加空格。

? ? ? ? "space-in-parens": [2, "never"],

? ? ? ? //中綴操作符左右是否添加空格

? ? ? ? "space-infix-ops": 2,

? ? ? ? //規(guī)定在一元操作符前后是否需要加空格,單詞類操作符需要加,而非單詞類操作符不用加

? ? ? ? //words - applies to unary word operators such as: new, delete, typeof, void, yield

? ? ? ? //nonwords - applies to unary operators such as: -, +, --, ++, !, !!

? ? ? ? "space-unary-ops": [2, { "words": true, "nonwords": false }],

? ? ? ? //規(guī)定是否需要在代碼注釋起始符// or /*后面至少緊跟一個(gè)空格

? ? ? ? "spaced-comment": [2, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],

? ? ? ? //要求在正則表達(dá)式的雙斜杠外面加一個(gè)圓括號(hào),來消除歧義

? ? ? ? "wrap-regex": 2,

?

?

? ? ? ? /*ECMAScript 6*/

? ? ? ? //箭頭函數(shù)中,如果函數(shù)體里只有一句代碼時(shí)可以省略大括號(hào)

? ? ? ? //規(guī)定是否可以省略大括號(hào)

? ? ? ? "arrow-body-style": 2,

? ? ? ? //箭頭函數(shù)中,只有一個(gè)參數(shù)時(shí)可以省略圓括號(hào)

? ? ? ? //規(guī)定了參數(shù)是否需要圓括號(hào)包圍

? ? ? ? "arrow-parens": [2, "always"],

? ? ? ? //規(guī)定了箭頭函數(shù)的箭頭前后是否加空格

? ? ? ? "arrow-spacing": [2, { "before": true, "after": true }],

? ? ? ? //保證constructor函數(shù)中super()應(yīng)正確出現(xiàn),比如在繼承的classes中(派生類)必須使用super,否則(非派生類)不要使用super。

? ? ? ? "constructor-super": 2,

? ? ? ? //規(guī)定generator函數(shù)中星號(hào)前后的空白

? ? ? ? "generator-star-spacing": [2, { "before": true, "after": true }],

? ? ? ? //禁止覆蓋class命名,也就是說變量名不要和class名重名

? ? ? ? "no-class-assign": 2,

? ? ? ? //箭頭函數(shù)的箭頭和比較操作符 (>, <, <=, and >=)很相似,該規(guī)則要求在和比較操作符容易發(fā)生混淆時(shí)禁止使用箭頭函數(shù)語(yǔ)法

? ? ? ? "no-confusing-arrow": 2,

? ? ? ? //禁止修改const聲明的變量

? ? ? ? "no-const-assign": 2,

? ? ? ? //class中的成員不允許有相同的名字

? ? ? ? "no-dupe-class-members": 2,

? ? ? ? //禁止在Symbol對(duì)象前使用new操作符

? ? ? ? "no-new-symbol": 2,

? ? ? ? //該規(guī)則可以定義不允許在應(yīng)用中導(dǎo)入的模塊

? ? ? ? "no-restricted-imports": [2,

? ? ? ? ? ? "assert", "buffer", "child_process", "cluster", "crypto", "dgram", "dns", "domain", "events", "freelist", "fs", "http", "https", "module", "net", "os", "path", "punycode", "querystring", "readline", "repl", "smalloc", "stream", "string_decoder", "sys", "timers", "tls", "tracing", "tty", "url", "util", "vm", "zlib"

? ? ? ? ],

? ? ? ? //在構(gòu)造函數(shù)中,禁止在super()調(diào)用前使用this/super對(duì)象

? ? ? ? "no-this-before-super": 2,

? ? ? ? //ES2015提供了默認(rèn)的空構(gòu)造函數(shù),禁止使用不必要的空構(gòu)造函數(shù)

? ? ? ? "no-useless-constructor": 2,

? ? ? ? //禁用var,用let和const代替var

? ? ? ? "no-var": 2,

? ? ? ? //ES6中提供了定義對(duì)象字面量的方法和屬性的簡(jiǎn)寫形式。強(qiáng)制要求在對(duì)象字面量中使用方法和屬性的簡(jiǎn)寫形式

? ? ? ? "object-shorthand": 2,

? ? ? ? //函數(shù)作為函數(shù)的參數(shù)傳入時(shí),傳入的函數(shù)需要是箭頭函數(shù)

? ? ? ? //箭頭函數(shù)中的this對(duì)象直接綁定到了其外面包圍的函數(shù)的this對(duì)象。

? ? ? ? "prefer-arrow-callback": 2,

? ? ? ? //如果一個(gè)變量聲明后不再被修改,那么應(yīng)使用const來聲明該變量

? ? ? ? "prefer-const": 2,

? ? ? ? //推薦使用Reflect上的方法替代以前老方法

? ? ? ? "prefer-reflect": 2,

? ? ? ? // 在ES2015(ES6)中推薦使用剩余參數(shù)(...rest)代替arguments變量

? ? ? ? "prefer-rest-params": 2,

? ? ? ? //在ES2015(ES6)中推薦使用擴(kuò)展符替代apply()方法

? ? ? ? "prefer-spread": 2,

? ? ? ? //在ES2015(ES6)中推薦使用模板代替以前的字符串拼接

? ? ? ? "prefer-template ": 2,

? ? ? ? //生成器函數(shù)中必須有yield關(guān)鍵字,如果沒有會(huì)報(bào)錯(cuò)。

? ? ? ? "require-yield": 2,

? ? ? ? //模板字符串中使用${ 和 } 包含的表達(dá)式前后是否需要留空格,默認(rèn)規(guī)則禁止花括號(hào)內(nèi)有空格

? ? ? ? "template-curly-spacing": [2, "never"],

? ? ? ? //yield*表達(dá)式中的*號(hào)前后是否留空格,默認(rèn)after,比如yield* other()

? ? ? ? "yield-star-spacing": [2, "after"],

?

?

?

?

? ? ? ? /*eslint-plugin-standard*/

? ? ? ? "standard/object-curly-even-spacing": [2, "either"],

? ? ? ? "standard/array-bracket-even-spacing": [2, "either"],

? ? ? ? "standard/computed-property-even-spacing": [2, "even"],

?

?

? ? ? ? /* eslint-plugin-promise*/

? ? ? ? "promise/param-names": 2,

? ? ? ? "promise/always-return": 2,

? ? ? ? "promise/catch-or-return": 2,

?

? ? ? ? /*eslint-plugin-react*/

? ? ? ? "react/jsx-boolean-value": 2,

? ? ? ? "react/jsx-quotes": 2,

? ? ? ? "react/jsx-no-undef": 2,

? ? ? ? "react/jsx-sort-props": 0,

? ? ? ? "react/jsx-sort-prop-types": 0,

? ? ? ? "react/jsx-uses-react": 2,

? ? ? ? "react/jsx-uses-vars": 2,

? ? ? ? "react/no-did-mount-set-state": 2,

? ? ? ? "react/no-did-update-set-state": 2,

? ? ? ? "react/no-multi-comp": 2,

? ? ? ? "react/no-unknown-property": 1,

? ? ? ? "react/prop-types": 1,

? ? ? ? "react/react-in-jsx-scope": 2,

? ? ? ? "react/self-closing-comp": 2,

? ? ? ? "react/wrap-multilines": 0

? ? }

?

?.eslintrc.js 示例:

?

module.exports = {

?

? ? "parserOptions": {

? ? ? ? //set to 3, 5 (default), 6, or 7 to specify the version of ECMAScript you want to use

? ? ? ? "ecmaVersion": 6,

? ? ? ? //set to "script" (default) or "module" if your code is in ECMAScript modules.

? ? ? ? "sourceType": "module",

? ? ? ? "ecmaFeatures": {

? ? ? ? ? ? //enable global strict mode (if ecmaVersion is 5 or greater)

? ? ? ? ? ? "impliedStrict": true,

? ? ? ? ? ? // http://es6.ruanyifeng.com/#docs/object#對(duì)象的擴(kuò)展運(yùn)算符 ?

? ? ? ? ? ? "experimentalObjectRestSpread": true,

? ? ? ? ? ? // enable JSX support

? ? ? ? ? ? "jsx": true

? ? ? ? }

? ? },

?

? ? "env": {

? ? ? ? // I write for browser

? ? ? ? "browser": true,

? ? ? ? // in CommonJS

? ? ? ? "commonjs": true,

? ? ? ? "node": true,

? ? ? ? // use ES6

? ? ? ? "es6": true

? ? },

?

? ? "globals": {

? ? ? ? //"document": false, ? 默認(rèn)已有

? ? ? ?// "navigator": false,?默認(rèn)已有

? ? ? ?// "window": false,?默認(rèn)已有

? ? ? ? //these global variables should never be written to (only read), then you can set each with a false flag

? ? ? ? "$": false

? ? },

?

? ? "plugins": [

? ? ? ? //The eslint-plugin- prefix can be omitted from the plugin name.

? ? ? ? "standard",

? ? ? ? "promise",

? ? ? ? // enable react plugin,eslint-plugin-react

? ? ? ? "react"

? ? ],

?

? ? // I want to use babel-eslint for parsing!

? ? "parser": "babel-eslint",

?

? ? "extends": [

? ? ? ? ?//eslint-config-standard 中包含了eslint-plugin-standard和eslint-plugin-promise的規(guī)則

? ? ? ? ? ?"standard",

? ? ? ? ?//eslint-config-google?

? ? ? ? ? ?"google",

? ? ? ? ?//eslint

? ? ? ? ? "eslint:recommended",

? ? ? ? //?eslint-plugin-react

? ? ? ? ? "plugin:react/recommended"

? ? ],

?

? ? // To give you an idea how to override rule options

? ? "rules": {

?

? ? ?}

};

?

轉(zhuǎn)載于:https://www.cnblogs.com/allin123/p/5754500.html

總結(jié)

以上是生活随笔為你收集整理的ESLint 规则的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。