eslint語法規範

 

原文:https://blog.csdn.net/momDIY/article/details/78620534?utm_source=copy 

eslint定義值含義
0 禁用此規則
1 不符合規則即給出警告
2 不符合規則即報錯
規則定義
camelcase:1(使用駝峯)
variable_name(試用下換線)
comma-dangle: [2, ‘never’](對象最後一項省略逗號)
comma-spacing:0 (逗號前後使用空格的限定)
space-before-blocks:1(語句塊前後的空格)
consistent-return:0 (總是需要返回值)
curly :0 (是否允許省略大括號,如if語句)
default-case:0 (是否允許不寫swich默認語句)
func-names:0 (是否允許省略函數名)
//省略
Foo.prototype.bar = function() {};

(function() {
    // ...
}())

//不省略
Foo.prototype.bar1 = function bar1() {};

(function bar1() {
    // ...
}())
1
2
3
4
5
6
7
8
9
10
11
12
13
guard-for-in 是否約束for-in循環 
在使用 for in 遍歷對象時,會把從原型鏈繼承來的屬性也包括進來。這樣會導致意想不到的項出現。
var foo = [5, 6, 7];
foo.__proto__.name = 'jiayou';
for(key in foo) {
    console.log(key);   //0 1 2 name
}
1
2
3
4
5
dot-notation (要求使用點號訪問屬性而不是方括號)
indent (強制一致的縮進)
key-spacing (鍵值空格一致)
keyword-spacing (關鍵字空格一致)
max-len:1 (強制限定行長度,超過80警告)
new-cap:0 (構造函數首字母大寫)
no-bitwise:0 (禁止使用位操作符,如 | & ~ ^ << >> >>>)
no-caller (禁止使用arguments.caller 和 arguments.callee,ECMAScript 5嚴格模式下方法已經被禁用)
no-console (不使用控制檯輸出)
no-else-return (禁止在else前有return)
no-multi-spaces (禁止多個空格)
no-nested-ternary (禁止嵌套使用三元表達式)
no-param-reassign (禁止對函數參數再賦值)
no-shadow (關鍵字無法被重寫)
no-throw-literal (限制可以被拋出的異常)
throw new Error(); //推薦
throw "error";      //不推薦
1
2
no-undef (禁用未聲明的變量)
no-unneeded-ternary (使用更簡潔的三元運算符)
var isYes = answer === 1 ? true : false;    //不推薦
var isYes = answer === 1;   //推薦
1
2
no-unreachable (禁止出現return/break/continue/throw後不可到達的代碼)
function fn() {
    x = 1;
    return x;
    x = 3; // 無法到達的代碼
}
1
2
3
4
5
no-underscore-dangle (不使用下劃線作爲變量名)
no-var (未定義不可使用)
object-curly-spacing (大括號內空格一致)
one-var (聲明變量不換行)
one-var-declaration-per-line (聲明變量換行)
padded-blocks (要求或禁止塊內填充 ,一些風格指南要求塊語句以空行開始並且以空行結束)
space-before-function-paren (函數圓括號前有個空格)
space-in-parens (圓括號內的空格)
spaced-comment (註釋前有空白)
space-infix-ops(運算符周圍有空格)
no-trailing-spaces (禁止行尾的留白或空格)
strict (要求使用嚴格模式)
quote-props (要求屬性名使用引號)
quote (對於單引號’、雙引號”、模板字符串`的限定)
wrap-iife (是否要求將自執行函數包裹起來)
vars-on-top (頂部聲明變量)
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-----------------------

 

規則

  • 縮進使用兩個空格。

    eslint: indent

    1.  

      function hello (name) {

    2.  

      console.log('hi', name)

    3.  

      }

    • 1
    • 2
    • 3
  • 字符串使用單引號,除非是爲了避免轉義。

    eslint: quotes

    1.  

      console.log('hello there')

    2.  

      $("<div class='box'>")

    • 1
    • 2
  • 無未使用的變量。

    eslint: no-unused-vars

    1.  

      function myFunction () {

    2.  

      var result = something() // ✗ avoid

    3.  

      }

    • 1
    • 2
    • 3
  • 關鍵字後面要有一個空格。

    eslint: keyword-spacing

    1.  

      if (condition) { ... } // ✓ ok

    2.  

      if(condition) { ... } // ✗ avoid

    • 1
    • 2
  • 函數參數列表括號前面要有一個空格。

    eslint: space-before-function-paren

    1.  

      function name (arg) { ... } // ✓ ok

    2.  

      function name(arg) { ... } // ✗ avoid

    3.  

       

    4.  

      run(function () { ... }) // ✓ ok

    5.  

      run(function() { ... }) // ✗ avoid

    • 1
    • 2
    • 3
    • 4
    • 5
  • 始終使用 === 不使用 ==。 
    例外:可以使用 obj == null 檢測 null || undefined

    eslint: eqeqeq

    1.  

      if (name === 'John') // ✓ ok

    2.  

      if (name == 'John') // ✗ avoid

    • 1
    • 2
    1.  

      if (name !== 'John') // ✓ ok

    2.  

      if (name != 'John') // ✗ avoid

    • 1
    • 2
  • 中綴操作符(infix operators)前後要有一個空格。

    eslint: space-infix-ops

    1.  

      // ✓ ok

    2.  

      var x = 2

    3.  

      var message = 'hello, ' + name + '!'

    • 1
    • 2
    • 3
    1.  

      // ✗ avoid

    2.  

      var x=2

    3.  

      var message = 'hello, '+name+'!'

    • 1
    • 2
    • 3
  • 逗號後面有一個空格。

    eslint: comma-spacing

    1.  

      // ✓ ok

    2.  

      var list = [1, 2, 3, 4]

    3.  

      function greet (name, options) { ... }

    • 1
    • 2
    • 3
    1.  

      // ✗ avoid

    2.  

      var list = [1,2,3,4]

    3.  

      function greet (name,options) { ... }

    • 1
    • 2
    • 3
  • else 與它的大括號同行。

    eslint: brace-style

    1.  

      // ✓ ok

    2.  

      if (condition) {

    3.  

      // ...

    4.  

      } else {

    5.  

      // ...

    6.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1.  

      // ✗ avoid

    2.  

      if (condition) {

    3.  

      // ...

    4.  

      }

    5.  

      else {

    6.  

      // ...

    7.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • if 語句如果包含多個語句則使用大括號。

    eslint: curly

    1.  

      // ✓ ok

    2.  

      if (options.quiet !== true) console.log('done')

    • 1
    • 2
    1.  

      // ✓ ok

    2.  

      if (options.quiet !== true) {

    3.  

      console.log('done')

    4.  

      }

    • 1
    • 2
    • 3
    • 4
    1.  

      // ✗ avoid

    2.  

      if (options.quiet !== true)

    3.  

      console.log('done')

    • 1
    • 2
    • 3
  • 始終處理函數的 err 參數。

    eslint: handle-callback-err

    1.  

      // ✓ ok

    2.  

      run(function (err) {

    3.  

      if (err) throw err

    4.  

      window.alert('done')

    5.  

      })

    • 1
    • 2
    • 3
    • 4
    • 5
    1.  

      // ✗ avoid

    2.  

      run(function (err) {

    3.  

      window.alert('done')

    4.  

      })

    • 1
    • 2
    • 3
    • 4
  • 瀏覽器全局變量始終添加前綴 window.。 
    例外: documentconsole 和 navigator

    eslint: no-undef

    window.alert('hi')   // ✓ ok
    • 1
  • 不要有多個連續空行。

    eslint: no-multiple-empty-lines

    1.  

      // ✓ ok

    2.  

      var value = 'hello world'

    3.  

      console.log(value)

    • 1
    • 2
    • 3
    1.  

      // ✗ avoid

    2.  

      var value = 'hello world'

    3.  

       

    4.  

      console.log(value)

    • 1
    • 2
    • 3
    • 4
  • 三元表達式如果是多行,則 ? 和 : 放在各自的行上。

    eslint: operator-linebreak

    1.  

      // ✓ ok

    2.  

      var location = env.development ? 'localhost' : 'www.api.com'

    3.  

       

    4.  

      // ✓ ok

    5.  

      var location = env.development

    6.  

      ? 'localhost'

    7.  

      : 'www.api.com'

    8.  

       

    9.  

      // ✗ avoid

    10.  

      var location = env.development ?

    11.  

      'localhost' :

    12.  

      'www.api.com'

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • var 聲明,每個聲明佔一行。

    eslint: one-var

    1.  

      // ✓ ok

    2.  

      var silent = true

    3.  

      var verbose = true

    4.  

       

    5.  

      // ✗ avoid

    6.  

      var silent = true, verbose = true

    7.  

       

    8.  

      // ✗ avoid

    9.  

      var silent = true,

    10.  

      verbose = true

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 用括號包裹條件中的賦值表達式。這是爲了清楚的表明它是一個賦值表達式 (=),而不是一個等式 (===) 的誤寫。

    eslint: no-cond-assign

    1.  

      // ✓ ok

    2.  

      while ((m = text.match(expr))) {

    3.  

      // ...

    4.  

      }

    5.  

       

    6.  

      // ✗ avoid

    7.  

      while (m = text.match(expr)) {

    8.  

      // ...

    9.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 單行語句塊的內側要有空格。

    eslint: block-spacing

    1.  

      function foo () {return true} // ✗ avoid

    2.  

      function foo () { return true } // ✓ ok

    • 1
    • 2
  • 變量和函數的名字使用 camelCase 格式。

    eslint: camelcase

    1.  

      function my_function () { } // ✗ avoid

    2.  

      function myFunction () { } // ✓ ok

    3.  

       

    4.  

      var my_var = 'hello' // ✗ avoid

    5.  

      var myVar = 'hello' // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
  • 無多餘逗號。

    eslint: comma-dangle

    1.  

      var obj = {

    2.  

      message: 'hello', // ✗ avoid

    3.  

      }

    • 1
    • 2
    • 3
  • 逗號必須放在當前行的末尾。

    eslint: comma-style

    1.  

      var obj = {

    2.  

      foo: 'foo'

    3.  

      ,bar: 'bar' // ✗ avoid

    4.  

      }

    5.  

       

    6.  

      var obj = {

    7.  

      foo: 'foo',

    8.  

      bar: 'bar' // ✓ ok

    9.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • . 應當與屬性同行。

    eslint: dot-location

    1.  

      console.

    2.  

      log('hello') // ✗ avoid

    3.  

       

    4.  

      console

    5.  

      .log('hello') // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
  • 文件以空行結尾。

    elint: eol-last

  • 函數名字和調用括號之間沒有空格。

    eslint: func-call-spacing

    1.  

      console.log ('hello') // ✗ avoid

    2.  

      console.log('hello') // ✓ ok

    • 1
    • 2
  • 鍵名和鍵值之間要有空格。

    eslint: key-spacing

    1.  

      var obj = { 'key' : 'value' } // ✗ avoid

    2.  

      var obj = { 'key' :'value' } // ✗ avoid

    3.  

      var obj = { 'key':'value' } // ✗ avoid

    4.  

      var obj = { 'key': 'value' } // ✓ ok

    • 1
    • 2
    • 3
    • 4
  • 構造函數的名字以大寫字母開始。

    eslint: new-cap

    1.  

      function animal () {}

    2.  

      var dog = new animal() // ✗ avoid

    3.  

       

    4.  

      function Animal () {}

    5.  

      var dog = new Animal() // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
  • 沒有參數的構造函數在調用時必須有括號。

    eslint: new-parens

    1.  

      function Animal () {}

    2.  

      var dog = new Animal // ✗ avoid

    3.  

      var dog = new Animal() // ✓ ok

    • 1
    • 2
    • 3
  • 對象若定義了 setter 則必須定義相應的 getter。

    eslint: accessor-pairs

    1.  

      var person = {

    2.  

      set name (value) { // ✗ avoid

    3.  

      this.name = value

    4.  

      }

    5.  

      }

    6.  

       

    7.  

      var person = {

    8.  

      set name (value) {

    9.  

      this.name = value

    10.  

      },

    11.  

      get name () { // ✓ ok

    12.  

      return this.name

    13.  

      }

    14.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 子類的構造器必須調用 super

    eslint: constructor-super

    1.  

      class Dog {

    2.  

      constructor () {

    3.  

      super() // ✗ avoid

    4.  

      }

    5.  

      }

    6.  

       

    7.  

      class Dog extends Mammal {

    8.  

      constructor () {

    9.  

      super() // ✓ ok

    10.  

      }

    11.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 使用對象字面量,不使用對象構造函數。

    eslint: no-array-constructor

    1.  

      var nums = new Array(1, 2, 3) // ✗ avoid

    2.  

      var nums = [1, 2, 3] // ✓ ok

    • 1
    • 2
  • 不使用 arguments.callee 和 arguments.caller

    eslint: no-caller

    1.  

      function foo (n) {

    2.  

      if (n <= 0) return

    3.  

       

    4.  

      arguments.callee(n - 1) // ✗ avoid

    5.  

      }

    6.  

       

    7.  

      function foo (n) {

    8.  

      if (n <= 0) return

    9.  

       

    10.  

      foo(n - 1)

    11.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 不要給 class 賦值。

    eslint: no-class-assign

    1.  

      class Dog {}

    2.  

      Dog = 'Fido' // ✗ avoid

    • 1
    • 2
  • 不要修改由 const 聲明的變量。

    eslint: no-const-assign

    1.  

      const score = 100

    2.  

      score = 125 // ✗ avoid

    • 1
    • 2
  • 在條件句中不要使用常量,循環語句除外。

    eslint: no-constant-condition

    1.  

      if (false) { // ✗ avoid

    2.  

      // ...

    3.  

      }

    4.  

       

    5.  

      if (x === 0) { // ✓ ok

    6.  

      // ...

    7.  

      }

    8.  

       

    9.  

      while (true) { // ✓ ok

    10.  

      // ...

    11.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 正則表達式不要使用控制字符。

    eslint: no-control-regex

    1.  

      var pattern = /\x1f/ // ✗ avoid

    2.  

      var pattern = /\x20/ // ✓ ok

    • 1
    • 2
  • 不使用 debugger 語句。

    eslint: no-debugger

    1.  

      function sum (a, b) {

    2.  

      debugger // ✗ avoid

    3.  

      return a + b

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • 不要對變量使用 delete 操作符。

    eslint: no-delete-var

    1.  

      var name

    2.  

      delete name // ✗ avoid

    • 1
    • 2
  • 函數定義無重複參數。

    eslint: no-dupe-args

    1.  

      function sum (a, b, a) { // ✗ avoid

    2.  

      // ...

    3.  

      }

    4.  

       

    5.  

      function sum (a, b, c) { // ✓ ok

    6.  

      // ...

    7.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • class 定義無重複成員。

    eslint: no-dupe-class-members

    1.  

      class Dog {

    2.  

      bark () {}

    3.  

      bark () {} // ✗ avoid

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • 對象字面量無重複鍵名。

    eslint: no-dupe-keys

    1.  

      var user = {

    2.  

      name: 'Jane Doe',

    3.  

      name: 'John Doe' // ✗ avoid

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • switch 語句無重複 case 從句。

    eslint: no-duplicate-case

    1.  

      switch (id) {

    2.  

      case 1:

    3.  

      // ...

    4.  

      case 1: // ✗ avoid

    5.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
  • 每個模塊只使用一個 import 語句。

    eslint: no-duplicate-imports

    1.  

      import { myFunc1 } from 'module'

    2.  

      import { myFunc2 } from 'module' // ✗ avoid

    3.  

       

    4.  

      import { myFunc1, myFunc2 } from 'module' // ✓ ok

    • 1
    • 2
    • 3
    • 4
  • 正則表達式無空的字符組。

    eslint: no-empty-character-class

    1.  

      const myRegex = /^abc[]/ // ✗ avoid

    2.  

      const myRegex = /^abc[a-z]/ // ✓ ok

    • 1
    • 2
  • 解構賦值不使用空的 pattern。

    eslint: no-empty-pattern

    1.  

      const { a: {} } = foo // ✗ avoid

    2.  

      const { a: { b } } = foo // ✓ ok

    • 1
    • 2
  • 不使用 eval()

    eslint: no-eval

    1.  

      eval( "var result = user." + propName ) // ✗ avoid

    2.  

      var result = user[propName] // ✓ ok

    • 1
    • 2
  • catch 語句中不要對錯誤對象重新賦值。

    eslint: no-ex-assign

    1.  

      try {

    2.  

      // ...

    3.  

      } catch (e) {

    4.  

      e = 'new value' // ✗ avoid

    5.  

      }

    6.  

       

    7.  

      try {

    8.  

      // ...

    9.  

      } catch (e) {

    10.  

      const newVal = 'new value' // ✓ ok

    11.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 不要擴展原生對象。

    eslint: no-extend-native

    Object.prototype.age = 21     // ✗ avoid
    • 1
  • 不使用非必要的 .bind()

    eslint: no-extra-bind

    1.  

      const name = function () {

    2.  

      getName()

    3.  

      }.bind(user) // ✗ avoid

    4.  

       

    5.  

      const name = function () {

    6.  

      this.getName()

    7.  

      }.bind(user) // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 不使用非必要的布爾值轉換。

    eslint: no-extra-boolean-cast

    1.  

      const result = true

    2.  

      if (!!result) { // ✗ avoid

    3.  

      // ...

    4.  

      }

    5.  

       

    6.  

      const result = true

    7.  

      if (result) { // ✓ ok

    8.  

      // ...

    9.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 函數表達式不使用非必要的包裹括號。

    eslint: no-extra-parens

    1.  

      const myFunc = (function () { }) // ✗ avoid

    2.  

      const myFunc = function () { } // ✓ ok

    • 1
    • 2
  • switch 語句使用 break,避免運行到下一個 case

    eslint: no-fallthrough

    1.  

      switch (filter) {

    2.  

      case 1:

    3.  

      doSomething() // ✗ avoid

    4.  

      case 2:

    5.  

      doSomethingElse()

    6.  

      }

    7.  

       

    8.  

      switch (filter) {

    9.  

      case 1:

    10.  

      doSomething()

    11.  

      break // ✓ ok

    12.  

      case 2:

    13.  

      doSomethingElse()

    14.  

      }

    15.  

       

    16.  

      switch (filter) {

    17.  

      case 1:

    18.  

      doSomething()

    19.  

      // fallthrough // ✓ ok

    20.  

      case 2:

    21.  

      doSomethingElse()

    22.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 浮點數應包含整數和小數。

    eslint: no-floating-decimal

    1.  

      const discount = .5 // ✗ avoid

    2.  

      const discount = 0.5 // ✓ ok

    • 1
    • 2
  • 不給聲明過的函數重新賦值。

    eslint: no-func-assign

    1.  

      function myFunc () { }

    2.  

      myFunc = myOtherFunc // ✗ avoid

    • 1
    • 2
  • 不給只讀的全局變量重新賦值。

    eslint: no-global-assign

    window = {}     // ✗ avoid
    • 1
  • 不使用隱式 eval()

    eslint: no-implied-eval

    1.  

      setTimeout("alert('Hello world')") // ✗ avoid

    2.  

      setTimeout(function () { alert('Hello world') }) // ✓ ok

    • 1
    • 2
  • 不在嵌套語句中使用函數聲明。

    eslint: no-inner-declarations

    1.  

      if (authenticated) {

    2.  

      function setAuthUser () {} // ✗ avoid

    3.  

      }

    • 1
    • 2
    • 3
  • RegExp 構造器不使用非法的正則表達式字符串。

    eslint: no-invalid-regexp

    1.  

      RegExp('[a-z') // ✗ avoid

    2.  

      RegExp('[a-z]') // ✓ ok

    • 1
    • 2
  • 不使用非法空白。

    eslint: no-irregular-whitespace

    function myFunc () /*<NBSP>*/{}   // ✗ avoid
    • 1
  • 不使用 __iterator__

    eslint: no-iterator

    Foo.prototype.__iterator__ = function () {}   // ✗ avoid
    • 1
  • label 不使用作用域內變量的名字。

    eslint: no-label-var

    1.  

      var score = 100

    2.  

      function game () {

    3.  

      score: 50 // ✗ avoid

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • 不使用 label 語句。

    eslint: no-labels

    1.  

      label:

    2.  

      while (true) {

    3.  

      break label // ✗ avoid

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • 不使用非必要的嵌套語句塊。

    eslint: no-lone-blocks

    1.  

      function myFunc () {

    2.  

      { // ✗ avoid

    3.  

      myOtherFunc()

    4.  

      }

    5.  

      }

    6.  

       

    7.  

      function myFunc () {

    8.  

      myOtherFunc() // ✓ ok

    9.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 縮進不混用空格和製表符。

    eslint: no-mixed-spaces-and-tabs

  • 不使用多個連續空格,縮進除外。

    eslint: no-multi-spaces

    1.  

      const id = 1234 // ✗ avoid

    2.  

      const id = 1234 // ✓ ok

    • 1
    • 2
  • 不使用多行字符串。

    eslint: no-multi-str

    1.  

      const message = 'Hello \

    2.  

      world' // ✗ avoid

    • 1
    • 2
  • 如果不是賦值則不使用 new

    eslint: no-new

    1.  

      new Character() // ✗ avoid

    2.  

      const character = new Character() // ✓ ok

    • 1
    • 2
  • 不使用 Function 構造器。

    eslint: no-new-func

    var sum = new Function('a', 'b', 'return a + b')    // ✗ avoid
    • 1
  • 不使用 Object 構造器。

    eslint: no-new-object

    let config = new Object()   // ✗ avoid
    • 1
  • 不使用 new require

    eslint: no-new-require

    const myModule = new require('my-module')    // ✗ avoid
    • 1
  • 不使用 Symbol 構造器。

    eslint: no-new-symbol

    const foo = new Symbol('foo')   // ✗ avoid
    • 1
  • 不使用原始類型的包裝對象。

    eslint: no-new-wrappers

    const message = new String('hello')   // ✗ avoid
    • 1
  • 全局對象的屬性不用於函數調用。

    eslint: no-obj-calls

    const math = Math()   // ✗ avoid
    • 1
  • 不使用八進制字面量。

    eslint: no-octal

    1.  

      const num = 042 // ✗ avoid

    2.  

      const num = '042' // ✓ ok

    • 1
    • 2
  • 字符串不使用八進制轉義。

    eslint: no-octal-escape

    const copyright = 'Copyright \251'  // ✗ avoid
    • 1
  • __dirname 和 __filename 不用於字符串拼接。

    eslint: no-path-concat

    1.  

      const pathToFile = __dirname + '/app.js' // ✗ avoid

    2.  

      const pathToFile = path.join(__dirname, 'app.js') // ✓ ok

    • 1
    • 2
  • 不使用 __proto__,應使用 getPrototypeOf

    eslint: no-proto

    1.  

      const foo = obj.__proto__ // ✗ avoid

    2.  

      const foo = Object.getPrototypeOf(obj) // ✓ ok

    • 1
    • 2
  • 不重複聲明變量。

    eslint: no-redeclare

    1.  

      let name = 'John'

    2.  

      let name = 'Jane' // ✗ avoid

    3.  

       

    4.  

      let name = 'John'

    5.  

      name = 'Jane' // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
  • 正則表達式中不使用多個連續空白。

    eslint: no-regex-spaces

    1.  

      const regexp = /test value/ // ✗ avoid

    2.  

       

    3.  

      const regexp = /test {3}value/ // ✓ ok

    4.  

      const regexp = /test value/ // ✓ ok

    • 1
    • 2
    • 3
    • 4
  • 在 return 語句中賦值表達式要用括號包裹。

    eslint: no-return-assign

    1.  

      function sum (a, b) {

    2.  

      return result = a + b // ✗ avoid

    3.  

      }

    4.  

       

    5.  

      function sum (a, b) {

    6.  

      return (result = a + b) // ✓ ok

    7.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 不將變量賦值給它自身。

    eslint: no-self-assign

    name = name   // ✗ avoid
    • 1
  • 不將變量跟它自身相比。

    esint: no-self-compare

    if (score === score) {}   // ✗ avoid
    • 1
  • 不使用逗號操作符。

    eslint: no-sequences

    if (doSomething(), !!test) {}   // ✗ avoid
    • 1
  • 不修改關鍵字的值。

    eslint: no-shadow-restricted-names

    let undefined = 'value'     // ✗ avoid
    • 1
  • 不使用稀疏數組(Sparse arrays)。

    eslint: no-sparse-arrays

    let fruits = ['apple',, 'orange']       // ✗ avoid
    • 1
  • 不使用製表符。

    eslint: no-tabs

  • 普通字符串不要包含模板字符串佔位符。

    eslint: no-template-curly-in-string

    1.  

      const message = 'Hello ${name}' // ✗ avoid

    2.  

      const message = `Hello ${name}` // ✓ ok

    • 1
    • 2
  • super() 必須在訪問 this 之前調用。

    eslint: no-this-before-super

    1.  

      class Dog extends Animal {

    2.  

      constructor () {

    3.  

      this.legs = 4 // ✗ avoid

    4.  

      super()

    5.  

      }

    6.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • throw 應當拋出一個 Error 對象。

    eslint: no-throw-literal

    1.  

      throw 'error' // ✗ avoid

    2.  

      throw new Error('error') // ✓ ok

    • 1
    • 2
  • 行末不要有空白。

    eslint: no-trailing-spaces

  • 變量不初始化爲 undefined

    eslint: no-undef-init

    1.  

      let name = undefined // ✗ avoid

    2.  

       

    3.  

      let name

    4.  

      name = 'value' // ✓ ok

    • 1
    • 2
    • 3
    • 4
  • 循環語句要更新循環變量。

    eslint: no-unmodified-loop-condition

    1.  

      for (let i = 0; i < items.length; j++) {...} // ✗ avoid

    2.  

      for (let i = 0; i < items.length; i++) {...} // ✓ ok

    • 1
    • 2
  • 簡單的存在賦值不使用三元操作符。

    eslint: no-unneeded-ternary

    1.  

      let score = val ? val : 0 // ✗ avoid

    2.  

      let score = val || 0 // ✓ ok

    • 1
    • 2
  • returnthrowcontinuebreak 語句後面不要有代碼。

    eslint: no-unreachable

    1.  

      function doSomething () {

    2.  

      return true

    3.  

      console.log('never called') // ✗ avoid

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • finally 語句塊無流程控制語句。

    eslint: no-unsafe-finally

    1.  

      try {

    2.  

      // ...

    3.  

      } catch (e) {

    4.  

      // ...

    5.  

      } finally {

    6.  

      return 42 // ✗ avoid

    7.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • in 操作符的左操作數不要使用 !

    eslint: no-unsafe-negation

    if (!key in obj) {}       // ✗ avoid
    • 1
  • 無非必要的 .call() 和 .apply()

    eslint: no-useless-call

    sum.call(null, 1, 2, 3)   // ✗ avoid
    • 1
  • 無非必要的計算屬性。

    eslint: no-useless-computed-key

    1.  

      const user = { ['name']: 'John Doe' } // ✗ avoid

    2.  

      const user = { name: 'John Doe' } // ✓ ok

    • 1
    • 2
  • 無非必要的構造器。

    eslint: no-useless-constructor

    1.  

      class Car {

    2.  

      constructor () { // ✗ avoid

    3.  

      }

    4.  

      }

    • 1
    • 2
    • 3
    • 4
  • 無非必要的轉義。

    eslint: no-useless-escape

    let message = 'Hell\o'  // ✗ avoid
    • 1
  • import, export, 解構賦值不可重命名爲同名變量。

    eslint: no-useless-rename

    1.  

      import { config as config } from './config' // ✗ avoid

    2.  

      import { config } from './config' // ✓ ok

    • 1
    • 2
  • 屬性前面無空白。

    eslint: no-whitespace-before-property

    1.  

      user .name // ✗ avoid

    2.  

      user.name // ✓ ok

    • 1
    • 2
  • 不使用 with 語句。

    eslint: no-with

    with (val) {...}    // ✗ avoid
    • 1
  • 對象屬性的換行應一致。

    eslint: object-property-newline

    1.  

      const user = {

    2.  

      name: 'Jane Doe', age: 30,

    3.  

      username: 'jdoe86' // ✗ avoid

    4.  

      }

    5.  

       

    6.  

      const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok

    7.  

       

    8.  

      const user = {

    9.  

      name: 'Jane Doe',

    10.  

      age: 30,

    11.  

      username: 'jdoe86'

    12.  

      } // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 語句塊內部首尾無空行。

    eslint: padded-blocks

    1.  

      if (user) {

    2.  

      // ✗ avoid

    3.  

      const name = getName()

    4.  

       

    5.  

      }

    6.  

       

    7.  

      if (user) {

    8.  

      const name = getName() // ✓ ok

    9.  

      }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 展開操作符後面無空格。

    eslint: rest-spread-spacing

    1.  

      fn(... args) // ✗ avoid

    2.  

      fn(...args) // ✓ ok

    • 1
    • 2
  • 分號後面要有一個空格,前面無空格。

    eslint: semi-spacing

    1.  

      for (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid

    2.  

      for (let i = 0; i < items.length; i++) {...} // ✓ ok

    • 1
    • 2
  • 語句塊前面要有一個空格。

    eslint: space-before-blocks

    1.  

      if (admin){...} // ✗ avoid

    2.  

      if (admin) {...} // ✓ ok

    • 1
    • 2
  • 函數參數列表括號內側無空格。

    eslint: space-in-parens

    1.  

      getName( name ) // ✗ avoid

    2.  

      getName(name) // ✓ ok

    • 1
    • 2
  • 一元操作符後面要有一個空格。

    eslint: space-unary-ops

    1.  

      typeof!admin // ✗ avoid

    2.  

      typeof !admin // ✓ ok

    • 1
    • 2
  • 註釋符號後面要有空白。

    eslint: spaced-comment

    1.  

      //comment // ✗ avoid

    2.  

      // comment // ✓ ok

    3.  

       

    4.  

      /*comment*/ // ✗ avoid

    5.  

      /* comment */ // ✓ ok

    • 1
    • 2
    • 3
    • 4
    • 5
  • 模板字符串大括號內側無空格。

    eslint: template-curly-spacing

    1.  

      const message = `Hello, ${ name }` // ✗ avoid

    2.  

      const message = `Hello, ${name}` // ✓ ok

    • 1
    • 2
  • 使用 isNaN() 檢查 NaN

    eslint: use-isnan

    1.  

      if (price === NaN) { } // ✗ avoid

    2.  

      if (isNaN(price)) { } // ✓ ok

    • 1
    • 2
  • typeof 必須跟合法的字符串比較。

    eslint: valid-typeof

    1.  

      typeof name === 'undefimed' // ✗ avoid

    2.  

      typeof name === 'undefined' // ✓ ok

    • 1
    • 2
  • 立即調用函數 (IIFEs) 必須用括號包裹。

    eslint: wrap-iife

    1.  

      const getName = function () { }() // ✗ avoid

    2.  

       

    3.  

      const getName = (function () { }()) // ✓ ok

    4.  

      const getName = (function () { })() // ✓ ok

    • 1
    • 2
    • 3
    • 4
  • yield* 的 * 前後要有一個空格。

    eslint: yield-star-spacing

    1.  

      yield* increment() // ✗ avoid

    2.  

      yield * increment() // ✓ ok

    • 1
    • 2
  • 不使用 Yoda 式條件句比較。

    eslint: yoda

    1.  

      if (42 === age) { } // ✗ avoid

    2.  

      if (age === 42) { } // ✓ ok

    • 1
    • 2

分號

  • 不使用分號。 (查看: 123)

    eslint: semi

    1.  

      window.alert('hi') // ✓ ok

    2.  

      window.alert('hi'); // ✗ avoid

    • 1
    • 2
  • 不以 ([, “` 開始行。這是省略分號時唯一的陷阱。standard 會保護你不落入陷阱。

    eslint: no-unexpected-multiline

    1.  

      // ✓ ok

    2.  

      ;(function () {

    3.  

      window.alert('ok')

    4.  

      }())

    5.  

       

    6.  

      // ✗ avoid

    7.  

      (function () {

    8.  

      window.alert('ok')

    9.  

      }())

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1.  

      // ✓ ok

    2.  

      ;[1, 2, 3].forEach(bar)

    3.  

       

    4.  

      // ✗ avoid

    5.  

      [1, 2, 3].forEach(bar)

    • 1
    • 2
    • 3
    • 4
    • 5
    1.  

      // ✓ ok

    2.  

      ;`hello`.indexOf('o')

    3.  

       

    4.  

      // ✗ avoid

    5.  

      `hello`.indexOf('o')

    • 1
    • 2
    • 3
    • 4
    • 5

    提示:如果你經常這樣寫代碼,你可能是過於聰明瞭。

    不鼓勵過於聰明的簡寫,表達式應儘可能清晰且容易閱讀:

    不要這樣:

    ;[1, 2, 3].forEach(bar)
    • 1

    這樣更好:

    1.  

      var nums = [1, 2, 3]

    2.  

      nums.forEach(bar)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章