eslint语法规范

官方地址:

规则

  • 缩进使用两个空格。

    eslint: indent

    1. function hello (name) {
    2. console.log('hi', name)
    3. }
  • 字符串使用单引号,除非是为了避免转义。

    eslint: quotes

    1. console.log('hello there')
    2. $("<div class='box'>")
  • 1
  • 无未使用的变量。

    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 (ab) {
    2.  
      return result = a + b // ✗ avoid
    3.  
      }
    4.  
       
    5.  
      function sum (ab) {
    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 = // ✗ 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 : // ✗ avoid
    2.  
      let score = val || // ✓ 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)
posted @ 2020-04-17 18:53  朝阳的向日葵  阅读(1706)  评论(0编辑  收藏  举报