ESLint 规则
ESLint由 JavaScript 紅寶書 作者 Nicholas C.Zakas 編寫, 2013 年發(fā)布第一個版本。
ESLint是一個以可擴展、每條規(guī)則獨立的,被設(shè)計為完全可配置的lint工具,一個QA工具,用來作為靜態(tài)代碼檢查,避免低級錯誤和統(tǒng)一代碼的風(fēng)格。
主要有以下特點:
默認規(guī)則包含所有 JSLint、 JSHint 中存在的規(guī)則, 易遷移;
規(guī)則可配置性高: 可設(shè)置「 警告」、「 錯誤」 兩個 error 等級, 或者直接禁用;
包含代碼風(fēng)格檢測的規(guī)則( 可以丟掉 JSCS 了);
支持插件擴展、 自定義規(guī)則。
?
安裝
npm install - g eslint
npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise
?
使用
$ eslint --init
將會初始化和生成.eslintrc.*文件
?
?
執(zhí)行檢查命令
$ eslint add.js
?
指定配置文件
$ eslint - c config.json add.js?
?
打印出eslint所使用的配置和規(guī)則
$?eslint --print-config ?xx.js > 1 ?
?
配置
?
有三種方法可以配置ESLint
? ? > 使用.eslintrc.*文件( 支持JSON和YAML兩種語法)
? ? > 在package.json中添加eslintConfig配置塊
? ? > 使用JavaScript注釋直接把配置嵌入到文件中
?
?
?
ESLint規(guī)則的設(shè)置具體格式如下:
? ? "quotes": "error"
#第一部分是規(guī)則名
#第二部分包含:
規(guī)則的嚴重性(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) 錯誤
( 如有) 規(guī)則的選項(additional options)
"quotes": [2, "double"]
?
?
?
配置文件:
?
//表示使用默認的規(guī)則進行校驗
"extends": "eslint:recommended"
?
文件中關(guān)閉驗證
/*eslint-disable */
//suppress all warnings between comments
alert('foo');
/*eslint-enable */
?
文件中指定規(guī)則不驗證
/*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#函數(shù)參數(shù)的默認值 ?
"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)默認嚴格模式 ?
"modules": true,
// 允許字面量定義對象時,用表達式做屬性名 ?
// http://es6.ruanyifeng.com/#docs/object#屬性名表達式 ?
"objectLiteralComputedProperties": true,
// 允許對象字面量方法名簡寫?
?"objectLiteralShorthandMethods": true,
// 對象字面量屬性名簡寫 ? ? ??
"objectLiteralShorthandProperties": true,
// http://es6.ruanyifeng.com/#docs/function#rest參數(shù) ?
"restParams": true,
// http://es6.ruanyifeng.com/#docs/function#擴展運算符 ?
"spread": true,
"superInFunctions": true,
// http://es6.ruanyifeng.com/#docs/string#模板字符串 ?
"templateStrings": true,
"unicodeCodePointEscapes": true,
} ? ? ? ? ??
? ? ? ? ? ??
?
?
規(guī)則說明?? ? ESLint v2.4.0
"rules": {
?
? ? ? ? /*Possible Errors*/
? ? ? ? // 數(shù)組和對象鍵值對最后一個逗號,?
? ? ? ? // never參數(shù):不能帶末尾的逗號,?
? ? ? ? // always參數(shù):必須帶末尾的逗號, ?
? ? ? ? // 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,
? ? ? ? //函數(shù)參數(shù)禁止重名
? ? ? ? "no-dupe-args": 2,
? ? ? ? //在創(chuàng)建對象字面量時不允許鍵重復(fù)?
? ? ? ? "no-dupe-keys": 2,
? ? ? ? //在switch語句中禁止重復(fù)的case
? ? ? ? "no-duplicate-case": 2,
? ? ? ? //代碼塊的內(nèi)容不能為空,禁止空代碼塊
? ? ? ? "no-empty": 2,
? ? ? ? //正則表達式的內(nèi)容不能為空,禁止使用不匹配任何字符串的正則表達式
? ? ? ? "no-empty-character-class": 2,
? ? ? ? //禁止對catch語句中的異常進行賦值
? ? ? ? "no-ex-assign": 2,
? ? ? ? //禁止不必要的bool轉(zhuǎn)換
? ? ? ? "no-extra-boolean-cast": 2,
? ? ? ? //禁止使用多余的圓括號
? ? ? ? "no-extra-parens": 2,
? ? ? ? //禁止多余的冒號
? ? ? ? "no-extra-semi": 2,
? ? ? ? //禁止重復(fù)的函數(shù)聲明
? ? ? ? "no-func-assign": 2,
? ? ? ? //禁止在塊語句中聲明變量或函數(shù)
? ? ? ? "no-inner-declarations": 2,
? ? ? ? //禁止使用無效的正則語句
? ? ? ? "no-invalid-regexp": 2,
? ? ? ? //禁止使用不合法或者不規(guī)則的空白符
? ? ? ? "no-irregular-whitespace": 2,
? ? ? ? //在in操作符左邊的操作項不能用! 例如這樣寫不對的:if ( !a in b) { //dosomething }
? ? ? ? "no-negated-in-lhs": 2,
? ? ? ? //禁止把全局對象當(dāng)函數(shù)調(diào)用,比如下面寫法錯誤的:Math(), JSON()
? ? ? ? "no-obj-calls": 2,
? ? ? ? //禁止在正則表達式字面量中使用多個空格 /foo bar/
? ? ? ? "no-regex-spaces": 2,
? ? ? ? //禁止稀疏數(shù)組,清除多余的逗號申明 ?比如[1,,2]
? ? ? ? "no-sparse-arrays": 2,
? ? ? ? //為了保證兩行不相關(guān)的代碼不會意外的被當(dāng)做一行代碼來解析
? ? ? ? "no-unexpected-multiline": 2,
? ? ? ? //禁止有執(zhí)行不到的代碼
? ? ? ? "no-unreachable": 2,
? ? ? ? //禁止和NaN作比較,推薦使用isNaN方法
? ? ? ? "use-isnan": 2,
? ? ? ? //用來檢測JSDoc是否完整和合法
? ? ? ? "valid-jsdoc": 2,
? ? ? ? //typeof操作符返回的結(jié)果會是 "undefined", ?"object", ?"boolean", "number", "string", 和 ?"function"之一。
? ? ? ? //保證typeof 操作符返回的結(jié)果必須和上面六個字符串作比較
? ? ? ? "valid-typeof": 2,
?
?
?
? ? ? ? /*Best Practices*/
? ? ? ? //在聲明對象時getter和setter需成對出現(xiàn)
? ? ? ? "accessor-pairs": 2,
? ? ? ? //數(shù)值方法的回調(diào)函數(shù)中強制寫return語句
? ? ? ? "array-callback-return": 2,
? ? ? ? //當(dāng)在代碼塊中用var聲明變量,并在代碼塊外使用時報錯?
? ? ? ? "block-scoped-var": 0,
? ? ? ? //用來控制函數(shù)的復(fù)雜度,分支超過5時報錯
? ? ? ? "complexity": [2, 5],
? ? ? ? //不同分支的return語句不能返回不同的類型,要么一致要么都沒有 ?
? ? ? ? "consistent-return": 0,
? ? ? ? // if else while for do后面的代碼塊是否需要{ }包圍,參數(shù): ?
? ? ? ? // multi ? ? ? ? 只有塊中有多行語句時才需要{ }包圍 ?
? ? ? ? // multi-line ? ?只有塊中有多行語句時才需要{ }包圍, 但是塊中的執(zhí)行語句只有一行時,塊中的語句只能跟和if語句在同一行。
? ? ? ? // ? ? ? ? ? ? ? ?if (foo) foo++; else doSomething(); ?
? ? ? ? // multi-or-nest 只有塊中有多行語句時才需要{ }包圍, 如果塊中的執(zhí)行語句只有一行,執(zhí)行語句可以另起一行也可以跟在if語句后面 ? ?
? ? ? ? // [2, "multi", "consistent"] 保持前后語句的{ }一致 ?
? ? ? ? // default: [2, "all"] 全都需要{ }包圍 ?
? ? ? ? "curly": 2,
? ? ? ? //所有的switch語句都必須要有一個default分支
? ? ? ? "default-case": 2,
? ? ? ? // 在書寫對象的屬性或方法時,新的一行代碼可以以. 開頭,也可以以. 結(jié)束。
? ? ? ? // 強制統(tǒng)一object.key中 . 的位置,參數(shù): ?
? ? ? ? // ? ? ?property,'.'號應(yīng)與屬性在同一行 ?
? ? ? ? // ? ? ?object, '.' 號應(yīng)與對象名在同一行 ?
? ? ? ? "dot-location": [2, "property"],
? ? ? ? // 強制使用.號取屬性 ?
? ? ? ? // 參數(shù): allowKeywords:true ?使用保留字做屬性名時,只能使用.方式取屬性 ?
? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? false 使用保留字做屬性名時, 只能使用[]方式取屬性?
? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? e.g [2, {"allowKeywords": false}] ?
? ? ? ? // ? ? ? ?allowPattern: ?當(dāng)屬性名匹配提供的正則表達式時,允許使用[]方式取值,否則只能用.號取值?
? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? e.g [2, {"allowPattern": "^[a-z]+(_[a-z]+)+$"}] ?
? ? ? ? "dot-notation": [2, { "allowKeywords": true }],
? ? ? ? //在進行比較時,必須使用全等=== 和完全不等!==
? ? ? ? "eqeqeq": [2, "allow-null"],
? ? ? ? //在for-in 循環(huán)中要使用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/中,我們并不能夠確定第一個/是除號還是正則表達式,因此我們需要在等號前面加一個轉(zhuǎn)移符/\=foo/
? ? ? ? "no-div-regex": 2,
? ? ? ? //在if else語句中,如果else語句中只含有一個return語句,那么完全可以不使用else語句,直接return。
? ? ? ? "no-else-return": 2,
? ? ? ? //不允許空函數(shù)
? ? ? ? "no-empty-function": 2,
? ? ? ? //在結(jié)構(gòu)賦值時,模式不能為空。在ECMAScript2015的結(jié)構(gòu)賦值中,模式為空是不會報錯的,只是這樣的結(jié)構(gòu)賦值沒有任何效果,該條規(guī)則就保證了模式不能為空,也就保證了結(jié)構(gòu)賦值的有效性。
? ? ? ? "no-empty-pattern": 2,
? ? ? ? //保證了在和null比較時使用===和!==,而不能夠使用==和!=
? ? ? ? "no-eq-null": 2,
? ? ? ? //禁止使用eval函數(shù)
? ? ? ? "no-eval": 2,
? ? ? ? //禁止擴展native對象,不能向native的對象上面添加屬性
? ? ? ? "no-extend-native": 2,
? ? ? ? //保證了調(diào)用bind方法的函數(shù)體內(nèi)有this對象。規(guī)避了不必要的使用bind方法的情況。
? ? ? ? //箭頭函數(shù)中沒有this對象,也就不能夠使用bind()方法。該規(guī)則保證了在所有的箭頭函數(shù)中使用bind方法將被視為錯誤。
? ? ? ? "no-extra-bind": 2,
? ? ? ? //如果 loop中沒有內(nèi)嵌的loops或switches, loop標簽是不必要的.
? ? ? ? "no-extra-label": 2,
? ? ? ? //在case語句中盡量加break,避免不必要的fallthrough錯誤,消除從一個case到另一個case的非故意的「fall through」。
? ? ? ? //如果沒有添加break等終止語句或者沒有添加注釋語句,將會拋出錯誤
? ? ? ? "no-fallthrough": 2,
? ? ? ? //在使用浮點小數(shù)時,不能夠省略小數(shù)點前面的數(shù)或者后面的數(shù),必須寫。比如.2 2. 應(yīng)該寫2.2 2.0?
? ? ? ? "no-floating-decimal": 2,
? ? ? ? //禁止隱式轉(zhuǎn)換,為了消除簡寫的類型轉(zhuǎn)換
? ? ? ? "no-implicit-coercion": 2,
? ? ? ? //禁止在全局作用域里聲明變量或函數(shù)
? ? ? ? "no-implicit-globals": 2,
? ? ? ? //在setTimeout(), setInterval() or execScript()中消除隱式eval的使用
? ? ? ? "no-implied-eval": 2,
? ? ? ? //禁止無效的this,只能用在構(gòu)造器,類,對象字面量
? ? ? ? "no-invalid-this": 2,
? ? ? ? //禁止使用__iterator__屬性
? ? ? ? "no-iterator": 2,
? ? ? ? //禁止使用label語句,以避免無限循環(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,
? ? ? ? //保證了在邏輯表達式、條件表達式、申明語句、數(shù)組元素、對象屬性、sequences、函數(shù)參數(shù)中不使用超過一個的空白符。
? ? ? ? "no-multi-spaces": 2,
? ? ? ? //該規(guī)則保證了字符串不分行書寫。
? ? ? ? "no-multi-str": 2,
? ? ? ? //該規(guī)則保證了不重寫原生對象。
? ? ? ? "no-native-reassign": 2,
? ? ? ? //在使用new來調(diào)用構(gòu)造函數(shù)后,必須把生成的實例賦值給一個變量
? ? ? ? "no-new": 2,
? ? ? ? //禁止使用new Function(); 語句。
? ? ? ? "no-new-func": 2,
? ? ? ? //禁止使用new創(chuàng)建String,Number, and Boolean實例
? ? ? ? "no-new-wrappers": 2,
? ? ? ? //禁止使用八進制數(shù)字
? ? ? ? "no-octal": 2,
? ? ? ? //禁止使用八進制轉(zhuǎn)義序列,比如 var foo = "Copyright \251";
? ? ? ? "no-octal-escape": 2,
? ? ? ? //禁止對函數(shù)的參數(shù)重新進行無意義的賦值
? ? ? ? "no-param-reassign": 2,
? ? ? ? //禁止使用__proto__屬性
? ? ? ? "no-proto": 2,
? ? ? ? //避免重復(fù)聲明一個變量
? ? ? ? "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,
? ? ? ? //禁止可能導(dǎo)致結(jié)果不明確的逗號操作符
? ? ? ? "no-sequences": 2,
? ? ? ? //通過throw語句拋出的對象必須是Error對象本身或者通過Error對象定義的對象。有些情況除外,見官網(wǎng)
? ? ? ? "no-throw-literal": 2,
? ? ? ? //禁止使用不被修改的循環(huán)條件
? ? ? ? "no-unmodified-loop-condition": 2,
? ? ? ? //禁止在代碼中出現(xiàn)沒有被使用到的表達式或值
? ? ? ? "no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true }],
? ? ? ? //禁止在代碼中出現(xiàn)沒有被使用到的標簽
? ? ? ? "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語句
? ? ? ? "no-with": 2,
? ? ? ? //在使用parseInt()方法時,必須要傳遞第二個參數(shù)來幫助解析。
? ? ? ? "radix": 2,
? ? ? ? //在通過var聲明變量時,應(yīng)該放在代碼所在作用域的頂部
? ? ? ? "vars-on-top": 2,
? ? ? ? //立即執(zhí)行函數(shù)需要通過圓括號包圍
? ? ? ? "wrap-iife": 2,
? ? ? ? //yoda條件語句就是對象字面量應(yīng)該寫在比較操作符的左邊,而變量應(yīng)該寫在比較操作符的右邊
? ? ? ? //默認的規(guī)則要求,變量寫在左邊而字面量寫在右邊
? ? ? ? "yoda": 2,
?
? ? ? ? /*Strict Mode*/
? ? ? ? //使用嚴格模式
? ? ? ? "strict": 2,
?
?
? ? ? ? /*Variables*/
? ? ? ? //變量聲明時必須賦初值
? ? ? ? "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,
? ? ? ? //聲明變量時禁止覆蓋JavaScript中的一些保留關(guān)鍵字,比如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,
? ? ? ? //所有的變量都應(yīng)該先定義后使用
? ? ? ? "no-use-before-define": 2,
?
?
?
? ? ? ? /*Node.js and CommonJS*/
? ? ? ? //強制回調(diào)后return,避免多次調(diào)用回調(diào)
? ? ? ? "callback-return": 2,
? ? ? ? //強制require()出現(xiàn)在模塊作用域的頂部
? ? ? ? "global-require": 2,
? ? ? ? // 如果函數(shù)有err入?yún)?err或者error),在函數(shù)體內(nèi)必須進行處理
? ? ? ? "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*/
? ? ? ? // 用數(shù)組字面量定義數(shù)組時數(shù)組元素前后是否加空格,?
? ? ? ? // never參數(shù): 數(shù)組元素前后不能帶空格,
? ? ? ? // always參數(shù):數(shù)組元素前后必須留空格 ?
? ? ? ? "array-bracket-spacing": [2, "never"],
? ? ? ? //在單行代碼塊中,代碼塊前后是否需要留空格
? ? ? ? // always參數(shù):默認,前后必須留空格
? ? ? ? // never參數(shù): 前后不能帶空格 ?
? ? ? ? "block-spacing": [2, "always"],
? ? ? ? //大括號的樣式,比如下面的大括號語法采用『1tbs』,允許單行樣式
? ? ? ? "brace-style": [2, "1tbs", { "allowSingleLine": true }],
? ? ? ? //強制使用駝峰命名 ?
? ? ? ? "camelcase": 2,
? ? ? ? //規(guī)定了逗號前后的空白,默認配置規(guī)定逗號前面沒有空白,而逗號后面需要留空白
? ? ? ? "comma-spacing": [2, { "before": false, "after": true }],
? ? ? ? //規(guī)定了逗號放的位置,默認配置逗號應(yīng)該放在行末,如果設(shè)置為first,逗號就應(yīng)放在行首
? ? ? ? "comma-style": [2, "last"],
? ? ? ? //是否在對象的動態(tài)屬性(computed properties: ES6引入)中添加空白,默認配置不添加空白
? ? ? ? "computed-property-spacing": [2, "never"],
? ? ? ? //統(tǒng)一this的別名(this賦值的變量名)保證整個應(yīng)用程序代碼的統(tǒng)一。
? ? ? ? //如果一個變量被指定為this對象的別名,那么這個變量就不能夠用來賦其他值,只能夠用來保存this對象。
? ? ? ? //如果this對象明確被賦值給了一個變量,那么這個變量應(yīng)該是配置中指定的那個變量名。 ? ??
? ? ? ? "consistent-this": [2, "self"],
? ? ? ? //該規(guī)則規(guī)定文件最后強制換行,僅需留一空行
? ? ? ? "eol-last": 2,
? ? ? ? //要求給函數(shù)表達式命名,便于debug
? ? ? ? "func-names": 2,
? ? ? ? //在JavaScript中有兩種方式定義函數(shù):函數(shù)聲明和函數(shù)表達式。
? ? ? ? //函數(shù)聲明就是把function關(guān)鍵詞寫在最前面,后面跟一個函數(shù)名。我們可以在函數(shù)申明代碼前調(diào)用函數(shù)
? ? ? ? //函數(shù)表達式是通過var等聲明變量的關(guān)鍵字開頭,然后跟函數(shù)名,再后面是function本身。在使用函數(shù)表達式定義函數(shù)前調(diào)用函數(shù)會報錯
? ? ? ? // 統(tǒng)一定義函數(shù)是所采用的方式,參數(shù): ?
? ? ? ? // ? ?declaration: 強制使用方法聲明的方式,function f(){} e.g [2, "declaration"] ?
? ? ? ? // ? ?expression:強制使用方法表達式的方式,默認方式,var f = function() {} ?e.g [2, "expression"] ?
? ? ? ? // ? ?allowArrowFunctions: declaration風(fēng)格中允許箭頭函數(shù)。 e.g [2, "declaration", {"allowArrowFunctions":true}] ?
? ? ? ? "func-style": [2, "expression"],
? ? ? ? //規(guī)定了標識符命名的黑名單
? ? ? ? "id-blacklist": [2, "data", "err", "e", "cb", "callback"],
? ? ? ? //規(guī)定標識符的長度,默認配置標識符最少兩個字符
? ? ? ? "id-length": [2, { "min": 2 }],
? ? ? ? //命名檢測,標識符命名需和配置中的正則表達式匹配,但是該規(guī)則對函數(shù)調(diào)用無效。
? ? ? ? "id-match": [2, "^[a-z]+([A-Z][a-z]+)*$", { "properties": false }],
? ? ? ? // 統(tǒng)一代碼縮進方式,默認值是4 spaces.
? ? ? ? "indent": 2,
? ? ? ? //規(guī)定了在JSX中的屬性值是使用單引號還是雙引號,默認使用雙引號
? ? ? ? "jsx-quotes": [2, "prefer-double"],
? ? ? ? //該規(guī)則規(guī)定了在對象字面量語法中key和value之間的空白,冒號前不要留空格,冒號后面需留一個空格
? ? ? ? "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
? ? ? ? // 規(guī)定了keyword前后是否需要留一個空格
? ? ? ? "keyword-spacing": [2, { "before": true, "after": true, "overrides": {} }],
? ? ? ? //統(tǒng)一換行符,"\n" unix(for LF) and "\r\n" for windows(CRLF),默認unix
? ? ? ? "linebreak-style": 2,
? ? ? ? //規(guī)定注釋和代碼塊之間是否留空行
? ? ? ? "lines-around-comment": 2,
? ? ? ? //規(guī)定代碼最多可以嵌套多少層
? ? ? ? "max-depth": [2, 4],
? ? ? ? //規(guī)定了代碼單行的最大長度
? ? ? ? "max-len": [2, 80, 4],
? ? ? ? //規(guī)定了回調(diào)的最大嵌套層數(shù)
? ? ? ? "max-nested-callbacks": [2, 10],
? ? ? ? //規(guī)定了函數(shù)參數(shù)的最大個數(shù)
? ? ? ? "max-params": [2, 3],
? ? ? ? //規(guī)定了函數(shù)中代碼不能夠超過多少行
? ? ? ? "max-statements": [2, 10],
? ? ? ? //使用構(gòu)造函數(shù)(new)時首字母需大寫,首字母大寫的函數(shù)需用new操作符
? ? ? ? "new-cap": 2,
? ? ? ? //使用構(gòu)造函數(shù)(new)時必須圓括號不能省略
? ? ? ? "new-parens": 2,
? ? ? ? //規(guī)定了變量聲明后是否需要空行
? ? ? ? "newline-after-var": 2,
? ? ? ? //規(guī)定了return語句前是否是否需要空行
? ? ? ? "newline-before-return": 2,
? ? ? ? //規(guī)定了方法鏈式調(diào)用時是否需換行
? ? ? ? "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控制語句中,else代碼塊中僅包含一個if語句
? ? ? ? "no-lonely-if": 2,
? ? ? ? //禁止混用tab和空格
? ? ? ? "no-mixed-spaces-and-tabs": 2,
? ? ? ? //不要留超過規(guī)定數(shù)目的空白行
? ? ? ? "no-multiple-empty-lines": [2, { "max": 2 }],
? ? ? ? //在if語句中使用了否定表達式,同時else語句又不為空,那么這樣的if-else語句將被視為不合法,為什么不將其反過來這樣代碼更容易理解,該規(guī)則同樣適用于三元操作符
? ? ? ? "no-negated-condition": 2,
? ? ? ? //三元操作符禁止嵌套
? ? ? ? "no-nested-ternary": 2,
? ? ? ? //禁止使用new Object()來構(gòu)造對象
? ? ? ? "no-new-object": 2,
? ? ? ? //禁止使用++,--
? ? ? ? "no-plusplus": 2,
? ? ? ? //禁止使用某些特定的JavaScript語法,例如FunctionDeclaration 和 WithStatement
? ? ? ? "no-restricted-syntax": [2, "FunctionExpression", "WithStatement"],
? ? ? ? //函數(shù)調(diào)用時,函數(shù)名和圓括號之間不能有空格
? ? ? ? "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,
? ? ? ? //規(guī)定對象字面量中大括號內(nèi)是否允許加空格,也適用于ES6中的結(jié)構(gòu)賦值和模塊import和export
? ? ? ? "object-curly-spacing": [2, "never"],
? ? ? ? //規(guī)定了在每個函數(shù)中聲明變量是否只使用一次var,該規(guī)則同樣適用于let和const
? ? ? ? "one-var": [2, { "initialized": "never" }],
? ? ? ? //規(guī)定了使用賦值操作符的簡寫形式
? ? ? ? "operator-assignment": [2, "always"],
? ? ? ? //在換行時操作符應(yīng)該放在行首還是行尾。還可對某些操作符進行重寫。
? ? ? ? "operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],
? ? ? ? //在代碼塊中,代碼塊的開始和結(jié)尾是否應(yīng)該留一個空行
? ? ? ? "padded-blocks": 0,
? ? ? ? //對象的屬性名是否強制加雙引號
? ? ? ? "quote-props": [2, "always"],
? ? ? ? //在JavaScript中有三種方式定義字符串,雙引號、單引號、反義符(ECMAScript2015)。規(guī)定了字符串定義的方式
? ? ? ? "quotes": [2, "single", "avoid-escape"],
? ? ? ? //注釋格式要求JSDoc格式
? ? ? ? "require-jsdoc": [2, {
? ? ? ? ? ? "require": {
? ? ? ? ? ? ? ? "FunctionDeclaration": true,
? ? ? ? ? ? ? ? "MethodDefinition": false,
? ? ? ? ? ? ? ? "ClassDeclaration": false
? ? ? ? ? ? }
? ? ? ? }],
? ? ? ? //JavaScript不要求在每行末尾加上分號,這是因為JavaScript引擎會決定是否需要在行末加上分號,然后自動幫我們在行末加上分號,這一特性被成為ASI(automatic semicolon insertion),也是JavaScript語言最富爭議的特性之一
? ? ? ? //盡管ASI允許我們使用更加自由的代碼風(fēng)格,但是它也可能使得你的代碼并不是按你期許的方式運行
? ? ? ? //兩個可選參數(shù),always 和never?
? ? ? ? //默認配置always,要求在行末加上分號。
? ? ? ? "semi": [2, "always"],
? ? ? ? //該規(guī)則用來規(guī)定分號前后是否加空格,默認配置如下
? ? ? ? "semi-spacing": [2, { "before": false, "after": true }],
? ? ? ? //要求對同一個模塊里的import聲明按字母排序
? ? ? ? "sort-imports": 2,
? ? ? ? //規(guī)定在同一個變量聲明代碼塊中,要對變量的聲明按字母排序
? ? ? ? "sort-vars": 2,
? ? ? ? //規(guī)定了在代碼塊前是否需要加空格
? ? ? ? "space-before-blocks": [2, "always"],
? ? ? ? //函數(shù)定義時,function關(guān)鍵字后面的小括號前是否需要加空格
? ? ? ? "space-before-function-paren": [2, "always"],
? ? ? ? //規(guī)定圓括號內(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 /*后面至少緊跟一個空格
? ? ? ? "spaced-comment": [2, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
? ? ? ? //要求在正則表達式的雙斜杠外面加一個圓括號,來消除歧義
? ? ? ? "wrap-regex": 2,
?
?
? ? ? ? /*ECMAScript 6*/
? ? ? ? //箭頭函數(shù)中,如果函數(shù)體里只有一句代碼時可以省略大括號
? ? ? ? //規(guī)定是否可以省略大括號
? ? ? ? "arrow-body-style": 2,
? ? ? ? //箭頭函數(shù)中,只有一個參數(shù)時可以省略圓括號
? ? ? ? //規(guī)定了參數(shù)是否需要圓括號包圍
? ? ? ? "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ù)中星號前后的空白
? ? ? ? "generator-star-spacing": [2, { "before": true, "after": true }],
? ? ? ? //禁止覆蓋class命名,也就是說變量名不要和class名重名
? ? ? ? "no-class-assign": 2,
? ? ? ? //箭頭函數(shù)的箭頭和比較操作符 (>, <, <=, and >=)很相似,該規(guī)則要求在和比較操作符容易發(fā)生混淆時禁止使用箭頭函數(shù)語法
? ? ? ? "no-confusing-arrow": 2,
? ? ? ? //禁止修改const聲明的變量
? ? ? ? "no-const-assign": 2,
? ? ? ? //class中的成員不允許有相同的名字
? ? ? ? "no-dupe-class-members": 2,
? ? ? ? //禁止在Symbol對象前使用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對象
? ? ? ? "no-this-before-super": 2,
? ? ? ? //ES2015提供了默認的空構(gòu)造函數(shù),禁止使用不必要的空構(gòu)造函數(shù)
? ? ? ? "no-useless-constructor": 2,
? ? ? ? //禁用var,用let和const代替var
? ? ? ? "no-var": 2,
? ? ? ? //ES6中提供了定義對象字面量的方法和屬性的簡寫形式。強制要求在對象字面量中使用方法和屬性的簡寫形式
? ? ? ? "object-shorthand": 2,
? ? ? ? //函數(shù)作為函數(shù)的參數(shù)傳入時,傳入的函數(shù)需要是箭頭函數(shù)
? ? ? ? //箭頭函數(shù)中的this對象直接綁定到了其外面包圍的函數(shù)的this對象。
? ? ? ? "prefer-arrow-callback": 2,
? ? ? ? //如果一個變量聲明后不再被修改,那么應(yīng)使用const來聲明該變量
? ? ? ? "prefer-const": 2,
? ? ? ? //推薦使用Reflect上的方法替代以前老方法
? ? ? ? "prefer-reflect": 2,
? ? ? ? // 在ES2015(ES6)中推薦使用剩余參數(shù)(...rest)代替arguments變量
? ? ? ? "prefer-rest-params": 2,
? ? ? ? //在ES2015(ES6)中推薦使用擴展符替代apply()方法
? ? ? ? "prefer-spread": 2,
? ? ? ? //在ES2015(ES6)中推薦使用模板代替以前的字符串拼接
? ? ? ? "prefer-template ": 2,
? ? ? ? //生成器函數(shù)中必須有yield關(guān)鍵字,如果沒有會報錯。
? ? ? ? "require-yield": 2,
? ? ? ? //模板字符串中使用${ 和 } 包含的表達式前后是否需要留空格,默認規(guī)則禁止花括號內(nèi)有空格
? ? ? ? "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的規(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é)
- 上一篇: 移动端项目开发需要注意的问题
- 下一篇: Android Studio项目结构