问题预防 - sept08/eslint-config-huawei GitHub Wiki

最佳实践

  • get/set方法必须成对出现

规则设置:accessor-pairs

'accessor-pairs': 'error',
  • 具有返回值的数组方法必须要有return语句

规则设置:array-callback-return

'array-callback-return': 'error',
  • 不要在块中声明var变量(在方法前面声明)

规则设置:block-scoped-var

'block-scoped-var': 'error',
  • 类方法中必须使用this,否则可以写在静态方法中

规则设置:class-methods-use-this

'class-methods-use-this': 'error',
  • 最大圈复杂度设置(20)

规则设置:complexity

'complexity': [
  'error',
  {
    max: 20
  }
],
  • 必须的返回值

规则设置:consistent-return

'consistent-return': 'off',
  • 代码结构的换行与大括号规范

@fixed 具体见规则设置

规则设置:curly

'curly': [
  'error',
  'multi-line',
  'consistent'
],
  • switch必须有默认default条件分支

规则设置:default-case

'default-case': 'off',
  • 属性与上一行连续

@fixed object.property不换行

规则设置:dot-location

'dot-location': [
  'error',
  'property'
],
  • 使用.调用对象属性

@fixed 使用foo.bar而非foo['bar']

规则设置:dot-notation

'dot-notation': 'error',
  • 使用===!==

@fixed

规则设置:eqeqeq

'eqeqeq': [
  'error',
  'always',
  {
    null: 'ignore'
  }
],
  • for in遍历中必须判断key是否存在

规则设置:guard-for-in

'guard-for-in': 'error',
  • 不使用原生alert语句

规则设置:no-alert

'no-alert': 'off',
  • 不使用arguments.caller/callee

规则设置:no-caller

'no-caller': 'error',
  • ES6 switch case中lexical declarations必须用大括号包裹

lexical declarations包括let、const、function、class

规则设置:no-case-declarations

'no-case-declarations': 'error',
  • 不允许正则表达式看似除法运算

如:/=foo/,用/\=foo/替换

规则设置:no-div-regex

'no-div-regex': 'off',
  • if中存在return时,else块没有存在的必要(直接接着上面写)

@fixed

规则设置:no-else-return

'no-else-return': 'off',
  • 禁止定义空的方法

规则设置:no-empty-function

'no-empty-function': [
  'error',
  {
    allow: [
      'functions',
      'arrowFunctions'
    ]
   }
],
  • 禁止空的解构赋值

规则设置:no-empty-pattern

'no-empty-pattern': 'error',
  • 使用===/!==null

规则设置:no-eq-null

'no-eq-null': 'off',
  • 不使用eval

规则设置:no-eval

'no-eval': 'error',
  • 禁止扩展原生对象

规则设置:no-extend-native

'no-extend-native': 'error',
  • 禁止无必要的函数绑定

@fixed

规则设置:no-extra-bind

'no-extra-bind': 'error',
  • 多层嵌套才需要label,否则没必要使用label语句

@fixed

规则设置:no-extra-label

'no-extra-label': 'error',
  • switch case中注意添加break与return

规则设置:no-fallthrough

'no-fallthrough': 'error',
  • float类型数字不做简写

@fixed

规则设置:no-floating-decimal

'no-floating-decimal': 'error',
  • 禁止对原生对象和只读全局变量赋值

规则设置:no-global-assign

'no-global-assign': 'error',
  • 使用易理解的方式进行转义

@fixed 如:var b = Boolean(foo)等

规则设置:no-implicit-coercion

'no-implicit-coercion': [
  'error',
  {
    allow: [
      '!!'
    ]
  }
],
  • 禁止在global域中声明变量

可以使用window.foo = 1替换var foo = 1的方式

规则设置:no-implicit-globals

'no-implicit-globals': 'error',
  • 不使用eval,也不使用潜在调用eval的语句

setTimeout(), setInterval() or execScript()可以将字符串作为方法执行; 如setTimeout("alert('Hi')", 100)

规则设置:no-implied-eval

'no-implied-eval': 'error',
  • 不在类或似类对象外使用this关键字

规则设置:no-invalid-this

'no-invalid-this': 'off',
  • 不使用__iterator__属性

规则设置:no-iterator

'no-iterator': 'error',
  • 不使用label语句

规则设置:no-labels

'no-labels': 'error',
  • 禁止出现多余的嵌套块

规则设置:no-lone-blocks

'no-lone-blocks': 'error',
  • 循环中注意使用function

for (let i=0; i<10; i++ ) {...}使用ES6 let避免var出现的作用域问题

规则设置:no-loop-func

'no-loop-func': 'error',
  • 禁止出现魔鬼数字

规则设置:no-magic-numbers

'no-magic-numbers': 'off',
  • 禁止出现多余的空格

@fixed

规则设置:no-multi-spaces

'no-multi-spaces': [
  'error',
  {
    ignoreEOLComments: true,
    exceptions: {
      Property: true,
      BinaryExpression: false,
      VariableDeclarator: true,
      ImportDeclaration: true
    }
  }
],
  • 使用\n为字符串换行而非\

规则设置:no-multi-str

'no-multi-str': 'error',
  • new新建的对象需有个变量来储存

规则设置:no-new

'no-new': 'error',
  • 不使用函数构造器

规则设置:no-new-func

'no-new-func': 'error',
  • 原生对象多余的new

规则设置:no-new-wrappers

'no-new-wrappers': 'error',
  • 不使用八进制表示方式

规则设置:no-octal

'no-octal': 'error',
  • 字符串中使用unicode编码替换八进制编码

规则设置:no-octal-escape

'no-octal-escape': 'error',
  • 不对函数变量重新赋值

规则设置:no-param-reassign

'no-param-reassign': 'error',
  • 不直接访问__proto__属性

通过getPrototypeOf代替

规则设置:no-proto

'no-proto': 'error',
  • 禁止重复声明

规则设置:no-redeclare

'no-redeclare': 'error',
  • 通过配置限制访问类的属性

自定义配置,用于限制某个具体类的具体属性不能调用

规则设置:no-restricted-properties

'no-restricted-properties': 'off',
  • 禁止在return语句中进行赋值操作

规则设置:no-return-assign

'no-return-assign': [
  'error',
  'always'
],
  • return语句中不出现不必要的await语句

规则设置:no-return-await

'no-return-await': 'error',
  • 不使用javascript: URLs

规则设置:no-script-url

'no-script-url': 'error',
  • 不做自赋值操作

规则设置:no-self-assign

'no-self-assign': 'error',
  • 不做自比较操作

规则设置:no-self-compare

'no-self-compare': 'error',
  • 谨慎使用,符号连接

规则设置:no-sequences

'no-sequences': 'error',
  • 限制抛出数据的类型

规则设置:no-throw-literal

'no-throw-literal': 'error',
  • 禁止出现对循环条件变量不做修改的循环(避免死循环)

规则设置:no-unmodified-loop-condition

'no-unmodified-loop-condition': 'error',
  • 禁止出现无作用的语句

规则设置:no-unused-expressions

'no-unused-expressions': [
  'error',
  {
    allowShortCircuit: true,
    allowTernary: true,
    allowTaggedTemplates: true
  }
],
  • 禁止出现无用的label

@fixed, 我们已经默认禁止使用label语法了

规则设置:no-unused-labels

'no-unused-labels': 'error',
  • 禁止出现无用call和apply方法

规则设置:no-useless-call

'no-useless-call': 'error',
  • 禁止出现无用字符串连接

如:"a" + "b"完全可以直接写为"ab"

规则设置:no-useless-concat

'no-useless-concat': 'error',
  • 正则表达式中禁止出现没必要的转义字符

规则设置:no-useless-escape

'no-useless-escape': 'error',
  • 禁止出现无用的return语句

@fixed

规则设置:no-useless-return

'no-useless-return': 'error',
  • 不使用void

规则设置:no-void

'no-void': 'error',
  • 不使用警告类型注释

规则设置:no-warning-comments

'no-warning-comments': 'warn',
  • 不使用with语句

规则设置:no-with

'no-with': 'warn',
  • Promise reject情形参数是个Error对象

规则设置:prefer-promise-reject-errors

'prefer-promise-reject-errors': 'warn',
  • ParseInt需传入转换的数字进制

规则设置:radix

'radix': 'warn',
  • 如果没有await不使用async函数

规则设置:require-await

'require-await': 'off',
  • var变量声明在函数顶部

规则设置:vars-on-top

'vars-on-top': 'off',
  • IIFE用括号包裹

@fixed

规则设置:wrap-iife

'wrap-iife': [
  'error',
  'inside',
  {
    functionPrototypeMethods: true
  }
],
  • 配置是否使用yoda模式

@fixed "red" === color可避免语法错误,而color === "red"更符合日常语法逻辑

规则设置:yoda

'yoda': [
  'error',
  'never',
  {
    onlyEquality: true
  }
],