日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ESLint 规则

發布時間:2025/4/16 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ESLint 规则 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

ESLint是一個以可擴展、每條規則獨立的,被設計為完全可配置的lint工具,一個QA工具,用來作為靜態代碼檢查,避免低級錯誤和統一代碼的風格。

主要有以下特點:

默認規則包含所有 JSLint、 JSHint 中存在的規則, 易遷移;

規則可配置性高: 可設置「 警告」、「 錯誤」 兩個 error 等級, 或者直接禁用;

包含代碼風格檢測的規則( 可以丟掉 JSCS 了);

支持插件擴展、 自定義規則。

?

安裝

npm install - g eslint

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

?

使用

$ eslint --init

將會初始化和生成.eslintrc.*文件

?

?

執行檢查命令

$ eslint add.js

?

指定配置文件

$ eslint - c config.json add.js?

?

打印出eslint所使用的配置和規則

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

?

配置

?

有三種方法可以配置ESLint

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

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

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

?

?

?

ESLint規則的設置具體格式如下:

? ? "quotes": "error"

#第一部分是規則名

#第二部分包含:

規則的嚴重性(rule severity)

"off"

or 0 - turn the rule off 不驗證 "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) 錯誤

( 如有) 規則的選項(additional options)

"quotes": [2, "double"]

?

?

?

配置文件:

?

//表示使用默認的規則進行校驗

"extends": "eslint:recommended"

?

文件中關閉驗證

/*eslint-disable */

//suppress all warnings between comments

alert('foo');

/*eslint-enable */

?

文件中指定規則不驗證

/*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表達式 ?

"arrowFunctions": true,

?// 塊級作用域,允許使用let const ?

"blockBindings": true,

// class ?

"classes": true,

?// http://es6.ruanyifeng.com/#docs/function#函數參數的默認值 ?

"defaultParams": true,

?// 解構賦值 ?

"destructuring": true,

// http://es6.ruanyifeng.com/#docs/iterator#for---of循環 ?

"forOf": true,

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

?"generators": true,

// 允許使用模塊,模塊內默認嚴格模式 ?

"modules": true,

// 允許字面量定義對象時,用表達式做屬性名 ?

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

"objectLiteralComputedProperties": true,

// 允許對象字面量方法名簡寫?

?"objectLiteralShorthandMethods": true,

// 對象字面量屬性名簡寫 ? ? ??

"objectLiteralShorthandProperties": true,

// http://es6.ruanyifeng.com/#docs/function#rest參數 ?

"restParams": true,

// http://es6.ruanyifeng.com/#docs/function#擴展運算符 ?

"spread": true,

"superInFunctions": true,

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

"templateStrings": true,

"unicodeCodePointEscapes": true,

} ? ? ? ? ??

? ? ? ? ? ??

?

?

規則說明?? ? ESLint v2.4.0

"rules": {

?

? ? ? ? /*Possible Errors*/

? ? ? ? // 數組和對象鍵值對最后一個逗號,?

? ? ? ? // never參數:不能帶末尾的逗號,?

? ? ? ? // always參數:必須帶末尾的逗號, ?

? ? ? ? // always-multiline:多行模式必須帶逗號,單行模式不能帶逗號 ?

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

? ? ? ? //禁止在條件表達式中使用賦值語句

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

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

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

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

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

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

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

? ? ? ? //禁止使用debugger語句

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

? ? ? ? //函數參數禁止重名

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

? ? ? ? //在創建對象字面量時不允許鍵重復?

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

? ? ? ? //在switch語句中禁止重復的case

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

? ? ? ? //代碼塊的內容不能為空,禁止空代碼塊

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

? ? ? ? //正則表達式的內容不能為空,禁止使用不匹配任何字符串的正則表達式

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

? ? ? ? //禁止對catch語句中的異常進行賦值

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

? ? ? ? //禁止不必要的bool轉換

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

? ? ? ? //禁止使用多余的圓括號

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

? ? ? ? //禁止多余的冒號

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

? ? ? ? //禁止重復的函數聲明

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

? ? ? ? //禁止在塊語句中聲明變量或函數

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

? ? ? ? //禁止使用無效的正則語句

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

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

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

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

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

? ? ? ? //禁止把全局對象當函數調用,比如下面寫法錯誤的:Math(), JSON()

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

? ? ? ? //禁止在正則表達式字面量中使用多個空格 /foo bar/

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

? ? ? ? //禁止稀疏數組,清除多余的逗號申明 ?比如[1,,2]

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

? ? ? ? //為了保證兩行不相關的代碼不會意外的被當做一行代碼來解析

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

? ? ? ? //禁止有執行不到的代碼

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

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

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

? ? ? ? //用來檢測JSDoc是否完整和合法

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

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

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

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

?

?

?

? ? ? ? /*Best Practices*/

? ? ? ? //在聲明對象時getter和setter需成對出現

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

? ? ? ? //數值方法的回調函數中強制寫return語句

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

? ? ? ? //當在代碼塊中用var聲明變量,并在代碼塊外使用時報錯?

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

? ? ? ? //用來控制函數的復雜度,分支超過5時報錯

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

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

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

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

? ? ? ? // multi ? ? ? ? 只有塊中有多行語句時才需要{ }包圍 ?

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

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

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

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

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

? ? ? ? "curly": 2,

? ? ? ? //所有的switch語句都必須要有一個default分支

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

? ? ? ? // 在書寫對象的屬性或方法時,新的一行代碼可以以. 開頭,也可以以. 結束。

? ? ? ? // 強制統一object.key中 . 的位置,參數: ?

? ? ? ? // ? ? ?property,'.'號應與屬性在同一行 ?

? ? ? ? // ? ? ?object, '.' 號應與對象名在同一行 ?

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

? ? ? ? // 強制使用.號取屬性 ?

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

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

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

? ? ? ? // ? ? ? ?allowPattern: ?當屬性名匹配提供的正則表達式時,允許使用[]方式取值,否則只能用.號取值?

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

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

? ? ? ? //在進行比較時,必須使用全等=== 和完全不等!==

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

? ? ? ? //在for-in 循環中要使用if語句

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

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

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

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

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

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

? ? ? ? //因為在case/default中的聲明,在整個switch語句中都能夠訪問到,如果需要聲明變量,可以加大括號。

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

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

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

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

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

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

? ? ? ? //不允許空函數

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

? ? ? ? //在結構賦值時,模式不能為空。在ECMAScript2015的結構賦值中,模式為空是不會報錯的,只是這樣的結構賦值沒有任何效果,該條規則就保證了模式不能為空,也就保證了結構賦值的有效性。

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

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

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

? ? ? ? //禁止使用eval函數

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

? ? ? ? //禁止擴展native對象,不能向native的對象上面添加屬性

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

? ? ? ? //保證了調用bind方法的函數體內有this對象。規避了不必要的使用bind方法的情況。

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

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

? ? ? ? //如果 loop中沒有內嵌的loops或switches, loop標簽是不必要的.

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

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

? ? ? ? //如果沒有添加break等終止語句或者沒有添加注釋語句,將會拋出錯誤

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

? ? ? ? //在使用浮點小數時,不能夠省略小數點前面的數或者后面的數,必須寫。比如.2 2. 應該寫2.2 2.0?

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

? ? ? ? //禁止隱式轉換,為了消除簡寫的類型轉換

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

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

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

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

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

? ? ? ? //禁止無效的this,只能用在構造器,類,對象字面量

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

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

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

? ? ? ? //禁止使用label語句,以避免無限循環

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

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

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

? ? ? ? //禁止在循環體中定義函數并且函數引用了外部變量

? ? ? ? //在循環中定義了函數,但是函數內部沒有引用外部變量,或者使用let定義的代碼塊變量,視為合法

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

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

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

? ? ? ? //保證了在邏輯表達式、條件表達式、申明語句、數組元素、對象屬性、sequences、函數參數中不使用超過一個的空白符。

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

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

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

? ? ? ? //該規則保證了不重寫原生對象。

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

? ? ? ? //在使用new來調用構造函數后,必須把生成的實例賦值給一個變量

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

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

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

? ? ? ? //禁止使用new創建String,Number, and Boolean實例

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

? ? ? ? //禁止使用八進制數字

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

? ? ? ? //禁止使用八進制轉義序列,比如 var foo = "Copyright \251";

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

? ? ? ? //禁止對函數的參數重新進行無意義的賦值

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

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

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

? ? ? ? //避免重復聲明一個變量

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

? ? ? ? //不要在return語句中使用賦值語句

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

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

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

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

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

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

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

? ? ? ? //禁止可能導致結果不明確的逗號操作符

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

? ? ? ? //通過throw語句拋出的對象必須是Error對象本身或者通過Error對象定義的對象。有些情況除外,見官網

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

? ? ? ? //禁止使用不被修改的循環條件

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

? ? ? ? //禁止在代碼中出現沒有被使用到的表達式或值

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

? ? ? ? //禁止在代碼中出現沒有被使用到的標簽

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

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

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

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

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

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

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

? ? ? ? //生產代碼中不能出現warning-comments包含的注釋

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

? ? ? ? //不要使用with語句

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

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

? ? ? ? "radix": 2,

? ? ? ? //在通過var聲明變量時,應該放在代碼所在作用域的頂部

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

? ? ? ? //立即執行函數需要通過圓括號包圍

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

? ? ? ? //yoda條件語句就是對象字面量應該寫在比較操作符的左邊,而變量應該寫在比較操作符的右邊

? ? ? ? //默認的規則要求,變量寫在左邊而字面量寫在右邊

? ? ? ? "yoda": 2,

?

? ? ? ? /*Strict Mode*/

? ? ? ? //使用嚴格模式

? ? ? ? "strict": 2,

?

?

? ? ? ? /*Variables*/

? ? ? ? //變量聲明時必須賦初值

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

? ? ? ? //所有的變量都應該先定義后使用

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

?

?

?

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

? ? ? ? //強制回調后return,避免多次調用回調

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

? ? ? ? //強制require()出現在模塊作用域的頂部

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

? ? ? ? // 如果函數有err入參(err或者error),在函數體內必須進行處理

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

? ? ? ? //聲明時不能混用聲明類型

? ? ? ? "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,

? ? ? ? //禁用使用指定模塊,使用了就會報錯

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

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

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

?

?

? ? ? ? /*Stylistic Issues*/

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

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

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

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

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

? ? ? ? // always參數:默認,前后必須留空格

? ? ? ? // never參數: 前后不能帶空格 ?

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

? ? ? ? //大括號的樣式,比如下面的大括號語法采用『1tbs』,允許單行樣式

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

? ? ? ? //強制使用駝峰命名 ?

? ? ? ? "camelcase": 2,

? ? ? ? //規定了逗號前后的空白,默認配置規定逗號前面沒有空白,而逗號后面需要留空白

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

? ? ? ? //規定了逗號放的位置,默認配置逗號應該放在行末,如果設置為first,逗號就應放在行首

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

? ? ? ? //是否在對象的動態屬性(computed properties: ES6引入)中添加空白,默認配置不添加空白

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

? ? ? ? //統一this的別名(this賦值的變量名)保證整個應用程序代碼的統一。

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

? ? ? ? //如果this對象明確被賦值給了一個變量,那么這個變量應該是配置中指定的那個變量名。 ? ??

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

? ? ? ? //該規則規定文件最后強制換行,僅需留一空行

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

? ? ? ? //要求給函數表達式命名,便于debug

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

? ? ? ? //在JavaScript中有兩種方式定義函數:函數聲明和函數表達式。

? ? ? ? //函數聲明就是把function關鍵詞寫在最前面,后面跟一個函數名。我們可以在函數申明代碼前調用函數

? ? ? ? //函數表達式是通過var等聲明變量的關鍵字開頭,然后跟函數名,再后面是function本身。在使用函數表達式定義函數前調用函數會報錯

? ? ? ? // 統一定義函數是所采用的方式,參數: ?

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

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

? ? ? ? // ? ?allowArrowFunctions: declaration風格中允許箭頭函數。 e.g [2, "declaration", {"allowArrowFunctions":true}] ?

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

? ? ? ? //規定了標識符命名的黑名單

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

? ? ? ? //規定標識符的長度,默認配置標識符最少兩個字符

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

? ? ? ? //命名檢測,標識符命名需和配置中的正則表達式匹配,但是該規則對函數調用無效。

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

? ? ? ? // 統一代碼縮進方式,默認值是4 spaces.

? ? ? ? "indent": 2,

? ? ? ? //規定了在JSX中的屬性值是使用單引號還是雙引號,默認使用雙引號

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

? ? ? ? //該規則規定了在對象字面量語法中key和value之間的空白,冒號前不要留空格,冒號后面需留一個空格

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

? ? ? ? // 規定了keyword前后是否需要留一個空格

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

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

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

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

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

? ? ? ? //規定代碼最多可以嵌套多少層

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

? ? ? ? //規定了代碼單行的最大長度

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

? ? ? ? //規定了回調的最大嵌套層數

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

? ? ? ? //規定了函數參數的最大個數

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

? ? ? ? //規定了函數中代碼不能夠超過多少行

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

? ? ? ? //使用構造函數(new)時首字母需大寫,首字母大寫的函數需用new操作符

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

? ? ? ? //使用構造函數(new)時必須圓括號不能省略

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

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

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

? ? ? ? //規定了return語句前是否是否需要空行

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

? ? ? ? //規定了方法鏈式調用時是否需換行

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

? ? ? ? //禁止使用Array構造函數

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

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

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

? ? ? ? //禁止使用continue

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

? ? ? ? //禁止使用行內注釋

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

? ? ? ? //禁止在if-else控制語句中,else代碼塊中僅包含一個if語句

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

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

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

? ? ? ? //不要留超過規定數目的空白行

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

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

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

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

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

? ? ? ? //禁止使用new Object()來構造對象

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

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

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

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

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

? ? ? ? //函數調用時,函數名和圓括號之間不能有空格

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

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

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

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

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

? ? ? ? //禁止在標識符前后使用下劃線

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

? ? ? ? //禁止使用沒有必要的三元操作符,因為用有些三元操作符可以使用其他語句替換

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

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

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

? ? ? ? //規定對象字面量中大括號內是否允許加空格,也適用于ES6中的結構賦值和模塊import和export

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

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

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

? ? ? ? //規定了使用賦值操作符的簡寫形式

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

? ? ? ? //在換行時操作符應該放在行首還是行尾。還可對某些操作符進行重寫。

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

? ? ? ? //在代碼塊中,代碼塊的開始和結尾是否應該留一個空行

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

? ? ? ? //對象的屬性名是否強制加雙引號

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

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

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

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

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

? ? ? ? ? ? "require": {

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

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

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

? ? ? ? ? ? }

? ? ? ? }],

? ? ? ? //JavaScript不要求在每行末尾加上分號,這是因為JavaScript引擎會決定是否需要在行末加上分號,然后自動幫我們在行末加上分號,這一特性被成為ASI(automatic semicolon insertion),也是JavaScript語言最富爭議的特性之一

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

? ? ? ? //兩個可選參數,always 和never?

? ? ? ? //默認配置always,要求在行末加上分號。

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

? ? ? ? //該規則用來規定分號前后是否加空格,默認配置如下

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

? ? ? ? //要求對同一個模塊里的import聲明按字母排序

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

? ? ? ? //規定在同一個變量聲明代碼塊中,要對變量的聲明按字母排序

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

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

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

? ? ? ? //函數定義時,function關鍵字后面的小括號前是否需要加空格

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

? ? ? ? //規定圓括號內部的空格。規定是否需要在(右邊,或者)左邊加空格。

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

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

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

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

? ? ? ? //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 }],

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

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

? ? ? ? //要求在正則表達式的雙斜杠外面加一個圓括號,來消除歧義

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

?

?

? ? ? ? /*ECMAScript 6*/

? ? ? ? //箭頭函數中,如果函數體里只有一句代碼時可以省略大括號

? ? ? ? //規定是否可以省略大括號

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

? ? ? ? //箭頭函數中,只有一個參數時可以省略圓括號

? ? ? ? //規定了參數是否需要圓括號包圍

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

? ? ? ? //規定了箭頭函數的箭頭前后是否加空格

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

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

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

? ? ? ? //規定generator函數中星號前后的空白

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

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

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

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

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

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

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

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

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

? ? ? ? //禁止在Symbol對象前使用new操作符

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

? ? ? ? //該規則可以定義不允許在應用中導入的模塊

? ? ? ? "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"

? ? ? ? ],

? ? ? ? //在構造函數中,禁止在super()調用前使用this/super對象

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

? ? ? ? //ES2015提供了默認的空構造函數,禁止使用不必要的空構造函數

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

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

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

? ? ? ? //ES6中提供了定義對象字面量的方法和屬性的簡寫形式。強制要求在對象字面量中使用方法和屬性的簡寫形式

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

? ? ? ? //函數作為函數的參數傳入時,傳入的函數需要是箭頭函數

? ? ? ? //箭頭函數中的this對象直接綁定到了其外面包圍的函數的this對象。

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

? ? ? ? //如果一個變量聲明后不再被修改,那么應使用const來聲明該變量

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

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

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

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

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

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

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

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

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

? ? ? ? //生成器函數中必須有yield關鍵字,如果沒有會報錯。

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

? ? ? ? //模板字符串中使用${ 和 } 包含的表達式前后是否需要留空格,默認規則禁止花括號內有空格

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

? ? ? ? //yield*表達式中的*號前后是否留空格,默認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#對象的擴展運算符 ?

? ? ? ? ? ? "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, ? 默認已有

? ? ? ?// "navigator": false,?默認已有

? ? ? ?// "window": false,?默認已有

? ? ? ? //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的規則

? ? ? ? ? ?"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": {

?

? ? ?}

};

?

轉載于:https://www.cnblogs.com/allin123/p/5754500.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。