Fork me on GitHub

ESlint standard

规则
使用两个空格缩进
eslint: indent

1
2
3
function hello (name) {
console.log('hi', name)
}

字符串使用单引号
eslint: quotes

1
2
console.log('hello there')
$("<div class='box'>")

禁止出现未使用过的变量
eslint: no-unused-vars

1
2
3
function myFunction () {
var result = something() // ✗ avoid
}

关键字后使用空格
eslint: keyword-spacing

1
2
if (condition) { ... } // ✓ ok
if(condition) { ... } // ✗ avoid

在function的左括号之前使用一致的空格
eslint: space-before-function-paren

1
2
3
4
5
function name (arg) { ... } // ✓ ok
function name(arg) { ... } // ✗ avoid
run(function () { ... }) // ✓ ok
run(function() { ... }) // ✗ avoid

要求使用 === 和 !==
此外: obj == null 被允许检查 null || undefined
eslint: eqeqeq

1
2
3
4
5
if (name === 'John') // ✓ ok
if (name == 'John') // ✗ avoid
if (name !== 'John') // ✓ ok
if (name != 'John') // ✗ avoid

要求操作符周围有空格
eslint: space-infix-ops

1
2
3
4
5
6
7
// ✓ ok
var x = 2
var message = 'hello, ' + name + '!'
// ✗ avoid
var x=2
var message = 'hello, '+name+'!'

强制在逗号后使用一致的空格
eslint: comma-spacing

1
2
3
4
5
6
7
// ✓ ok
var list = [1, 2, 3, 4]
function greet (name, options) { ... }
// ✗ avoid
var list = [1,2,3,4]
function greet (name,options) { ... }

强制在代码块中使用一致的大括号风格
eslint: brace-style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// ✓ ok
if (condition) {
// ...
} else {
// ...
}
// ✗ avoid
if (condition) {
// ...
}
else {
// ...
}

强制所有控制语句使用一致的括号风格
eslint: curly

1
2
3
4
5
6
7
8
9
10
11
// ✓ ok
if (options.quiet !== true) console.log('done')
// ✓ ok
if (options.quiet !== true) {
console.log('done')
}
// ✗ avoid
if (options.quiet !== true)
console.log('done')

要求回调函数中有容错处理
eslint: handle-callback-err

1
2
3
4
5
6
7
8
9
10
// ✓ ok
run(function (err) {
if (err) throw err
window.alert('done')
})
// ✗ avoid
run(function (err) {
window.alert('done')
})

始终使用window.全局作为前缀
eslint: no-undef

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

禁止出现多行空行
eslint: no-multiple-empty-lines

1
2
3
4
5
6
7
8
// ✓ ok
var value = 'hello world' console.log(value)
// ✗ avoid
var value = 'hello world'
console.log(value)

强制操作符使用一致的换行符
eslint: operator-linebreak

1
2
3
4
5
6
7
8
9
10
11
12
// ✓ ok
var location = env.development ? 'localhost' : 'www.api.com'
// ✓ ok
var location = env.development
? 'localhost'
: 'www.api.com'
// ✗ avoid
var location = env.development ?
'localhost' :
'www.api.com'

变量声明要分开声明
eslint: one-var

1
2
3
4
5
6
7
8
9
10
// ✓ ok
var silent = true
var verbose = true
// ✗ avoid
var silent = true, verbose = true
// ✗ avoid
var silent = true,
verbose = true

用附加的括号包含条件赋值。 这使得这个赋值(=)表达式有意,而不是一个等于(===)的拼写错误。
eslint: no-cond-assign

1
2
3
4
5
6
7
8
9
// ✓ ok
while ((m = text.match(expr))) {
// ...
}
// ✗ avoid
while (m = text.match(expr)) {
// ...
}

强制在单行代码块中使用一致的空格
eslint: block-spacing

1
2
function foo () {return true} // ✗ avoid
function foo () { return true } // ✓ ok

使用驼峰命名变量和方法名
eslint: camelcase

1
2
3
4
5
function my_function () { } // ✗ avoid
function myFunction () { } // ✓ ok
var my_var = 'hello' // ✗ avoid
var myVar = 'hello' // ✓ ok

禁止拖尾逗号
eslint: comma-dangle

1
2
3
var obj = {
message: 'hello', // ✗ avoid
}

强制使用一致的逗号风格
eslint: comma-style

1
2
3
4
5
6
7
8
9
var obj = {
foo: 'foo'
,bar: 'bar' // ✗ avoid
}
var obj = {
foo: 'foo',
bar: 'bar' // ✓ ok
}

点号之前换行(点号应该和属性在一行)
eslint: dot-location

1
2
3
4
5
console.
log('hello') // ✗ avoid
console
.log('hello') // ✓ ok

文件必须以换行符结尾。
eslint: eol-last

在功能表示符和其调用之间不能有空格(No space between function identifiers and their invocations.)
eslint: func-call-spacing

1
2
console.log ('hello') // ✗ avoid
console.log('hello') // ✓ ok

强制在对象字面量的属性中键和值之间使用一致的间距
eslint: key-spacing

1
2
3
4
var obj = { 'key' : 'value' } // ✗ avoid
var obj = { 'key' :'value' } // ✗ avoid
var obj = { 'key':'value' } // ✗ avoid
var obj = { 'key': 'value' } // ✓ ok

要求构造函数首字母大写
eslint: new-cap

1
2
3
4
5
function animal () {}
var dog = new animal() // ✗ avoid
function Animal () {}
var dog = new Animal() // ✓ ok

要求调用无参构造函数时有圆括号
eslint: new-parens

1
2
3
function Animal () {}
var dog = new Animal // ✗ avoid
var dog = new Animal() // ✓ ok

强制 getter 和 setter 在对象中成对出现
eslint: accessor-pairs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var person = {
set name (value) { // ✗ avoid
this.name = value
}
}
var person = {
set name (value) {
this.name = value
},
get name () { // ✓ ok
return this.name
}
}

要求在构造函数中有 super() 的调用
eslint: constructor-super

1
2
3
4
5
6
7
8
9
10
11
class Dog {
constructor () {
super() // ✗ avoid
}
}
class Dog extends Mammal {
constructor () {
super() // ✓ ok
}
}

禁用 Array 构造函数
eslint: no-array-constructor

1
2
var nums = new Array(1, 2, 3) // ✗ avoid
var nums = [1, 2, 3] // ✓ ok

禁用 arguments.caller 或 arguments.callee
eslint: no-caller

1
2
3
4
5
6
7
8
9
10
11
function foo (n) {
if (n <= 0) return
arguments.callee(n - 1) // ✗ avoid
}
function foo (n) {
if (n <= 0) return
foo(n - 1)
}

禁止修改类声明的变量
eslint: no-class-assign

1
2
class Dog {}
Dog = 'Fido' // ✗ avoid

禁止修改 const 声明的变量
eslint: no-const-assign

1
2
const score = 100
score = 125 // ✗ avoid

禁止在条件中使用常量表达式(除了循环)
eslint: no-constant-condition

1
2
3
4
5
6
7
8
9
10
11
if (false) { // ✗ avoid
// ...
}
if (x === 0) { // ✓ ok
// ...
}
while (true) { // ✓ ok
// ...
}

禁止在正则表达式中使用控制字符
eslint: no-control-regex

1
2
var pattern = /\x1f/ // ✗ avoid
var pattern = /\x20/ // ✓ ok

禁用bugger
eslint: no-debugger

1
2
3
4
function sum (a, b) {
debugger // ✗ avoid
return a + b
}

禁止删除变量
eslint: no-delete-var

1
2
var name
delete name // ✗ avoid

禁止 function 定义中出现重名参数
eslint: no-dupe-args

1
2
3
4
5
6
7
function sum (a, b, a) { // ✗ avoid
// ...
}
function sum (a, b, c) { // ✓ ok
// ...
}

禁止类成员中出现重复的名称
eslint: no-dupe-class-members

1
2
3
4
class Dog {
bark () {}
bark () {} // ✗ avoid
}

禁止对象字面量中出现重复的 key
eslint: no-dupe-keys

1
2
3
4
var user = {
name: 'Jane Doe',
name: 'John Doe' // ✗ avoid
}

禁止出现重复的 case 标签
eslint: no-duplicate-case

1
2
3
4
5
switch (id) {
case 1:
// ...
case 1: // ✗ avoid
}

每个module 使用一个import的语句
eslint: no-duplicate-imports

1
2
3
4
import { myFunc1 } from 'module'
import { myFunc2 } from 'module' // ✗ avoid
import { myFunc1, myFunc2 } from 'module' // ✓ ok

禁用eval()
eslint: no-eval

1
2
eval( "var result = user." + propName ) // ✗ avoid
var result = user[propName] // ✓ ok

禁止对 catch 子句的参数重新赋值
eslint: no-ex-assign

1
2
3
4
5
6
7
8
9
10
11
try {
// ...
} catch (e) {
e = 'new value' // ✗ avoid
}
try {
// ...
} catch (e) {
const newVal = 'new value' // ✓ ok
}

禁止扩展原生类型
eslint: no-extend-native

1
Object.prototype.age = 21 // ✗ avoid

禁止不必要的 .bind() 调用
eslint: no-extra-bind

1
2
3
4
5
6
7
const name = function () {
getName()
}.bind(user) // ✗ avoid
const name = function () {
this.getName()
}.bind(user) // ✓ ok

禁止不必要的布尔转换
eslint: no-extra-boolean-cast

1
2
3
4
5
6
7
8
9
const result = true
if (!!result) { // ✗ avoid
// ...
}
const result = true
if (result) { // ✓ ok
// ...
}

禁止不必要的括号
eslint: no-extra-parens

1
2
const myFunc = (function () { }) // ✗ avoid
const myFunc = function () { } // ✓ ok

使用break禁止 case 语句落空
eslint: no-fallthrough

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
switch (filter) {
case 1:
doSomething() // ✗ avoid
case 2:
doSomethingElse()
}
switch (filter) {
case 1:
doSomething()
break // ✓ ok
case 2:
doSomethingElse()
}
switch (filter) {
case 1:
doSomething()
// fallthrough // ✓ ok
case 2:
doSomethingElse()
}

禁止数字字面量中使用前导和末尾小数点
eslint: no-floating-decimal

1
2
const discount = .5 // ✗ avoid
const discount = 0.5 // ✓ ok

禁止对 function 声明重新赋值
eslint: no-func-assign

1
2
function myFunc () { }
myFunc = myOtherFunc // ✗ avoid

不能对只读的全局变量重新赋值
eslint: no-global-assign

1
window = {} // ✗ avoid

禁止使用类似 eval() 的方法
eslint: no-implied-eval

1
2
setTimeout("alert('Hello world')") // ✗ avoid
setTimeout(function () { alert('Hello world') }) // ✓ ok

禁止在嵌套的块中出现变量声明或 function 声明
eslint: no-inner-declarations

1
2
3
if (authenticated) {
function setAuthUser () {} // ✗ avoid
}

禁止 RegExp 构造函数中存在无效的正则表达式字符串
eslint: no-invalid-regexp

1
2
RegExp('[a-z') // ✗ avoid
RegExp('[a-z]') // ✓ ok

禁止在字符串和注释之外不规则的空白
eslint: no-irregular-whitespace

1
function myFunc () /*<NBSP>*/{} // ✗ avoid

禁用 iterator 属性
eslint: no-iterator

1
Foo.prototype.__iterator__ = function () {} // ✗ avoid

不允许标签与变量同名
eslint: no-label-var

1
2
3
4
var score = 100
function game () {
score: 50 // ✗ avoid
}

禁用标签语句
eslint: no-labels

1
2
3
4
label:
while (true) {
break label // ✗ avoid
}

禁用不必要的嵌套块
eslint: no-lone-blocks

1
2
3
4
5
6
7
8
9
function myFunc () {
{ // ✗ avoid
myOtherFunc()
}
}
function myFunc () {
myOtherFunc() // ✓ ok
}

禁止空格和 tab 的混合缩进
eslint: no-mixed-spaces-and-tabs
禁止使用多个空格
eslint: no-multi-spaces

1
2
const id = 1234 // ✗ avoid
const id = 1234 // ✓ ok

禁止使用多行字符串
eslint: no-multi-str

1
2
const message = 'Hello \
world' // ✗ avoid

禁止在非赋值或条件语句中使用 new 操作符
eslint: no-new

1
2
new Character() // ✗ avoid
const character = new Character() // ✓ ok

禁止对 Function 对象使用 new 操作符
eslint: no-new-func

1
var sum = new Function('a', 'b', 'return a + b') // ✗ avoid

禁用 Object 的构造函数
eslint: no-new-object

1
let config = new Object() // ✗ avoid

禁止调用 require 时使用 new 操作符
eslint: no-new-require

1
const myModule = new require('my-module') // ✗ avoid

禁止Symbol构造函数
eslint: no-new-symbol

1
const foo = new Symbol('foo') // ✗ avoid

禁止原函数包装实例
eslint: no-new-wrappers

1
const message = new String('hello') // ✗ avoid

禁止把全局对象作为函数调用
eslint: no-obj-calls

1
const math = Math() // ✗ avoid

禁止在字符串中使用八进制转义序列
eslint: no-octal-escape

1
const copyright = 'Copyright \251' // ✗ avoid

禁用八进制字面量
eslint: no-octal

1
2
const num = 042 // ✗ avoid
const num = '042' // ✓ ok

禁止对 dirname 和 filename 进行字符串连接
eslint: no-path-concat

1
2
const pathToFile = __dirname + '/app.js' // ✗ avoid
const pathToFile = path.join(__dirname, 'app.js') // ✓ ok

禁用 proto 属性
eslint: no-proto

1
2
const foo = obj.__proto__ // ✗ avoid
const foo = Object.getPrototypeOf(obj) // ✓ ok

禁止多次声明同一变量
eslint: no-redeclare

1
2
3
4
5
let name = 'John'
let name = 'Jane' // ✗ avoid
let name = 'John'
name = 'Jane' // ✓ ok

禁止正则表达式字面量中出现多个空格
eslint: no-regex-spaces

1
2
3
4
const regexp = /test value/ // ✗ avoid
const regexp = /test {3}value/ // ✓ ok
const regexp = /test value/ // ✓ ok

禁止在 return 语句中使用赋值语句
eslint: no-return-assign

1
2
3
4
5
6
7
function sum (a, b) {
return result = a + b // ✗ avoid
}
function sum (a, b) {
return (result = a + b) // ✓ ok
}

禁止自我赋值
eslint : no-self-assign

1
name = name // ✗ avoid

禁止自身比较
eslint: no-self-compare

1
if (score === score) {} // ✗ avoid

禁用逗号操作符
eslint: no-sequences

1
if (doSomething(), !!test) {} // ✗ avoid

禁止覆盖受限制的标识符
eslint: no-shadow-restricted-names

1
let undefined = 'value' // ✗ avoid

禁用稀疏数组
eslint: no-sparse-arrays

1
let fruits = ['apple',, 'orange'] // ✗ avoid

disallow tabs in file
eslint: no-tabs

disallow template literal placeholder syntax in regular strings
eslint: no-template-curly-in-string

1
2
const message = 'Hello ${name}' // ✗ avoid
const message = `Hello ${name}` // ✓ ok

禁止在构造函数中,在调用 super() 之前使用 this 或 super
eslint: no-this-before-super

1
2
3
4
5
6
class Dog extends Animal {
constructor () {
this.legs = 4 // ✗ avoid
super()
}
}

禁止抛出异常字面量
eslint: no-throw-literal

1
2
throw 'error' // ✗ avoid
throw new Error('error') // ✓ ok

禁用行尾空格
eslint: no-trailing-spaces

禁止将变量初始化为 undefined
eslin: no-undef-init

1
2
3
4
let name = undefined // ✗ avoid
let name
name = 'value' // ✓ ok

禁用未修改的循环条件
eslint: no-unmodified-loop-condition

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

禁止可以在有更简单的可替代的表达式时使用三元操作符
eslint: no-unneeded-ternary

1
2
let score = val ? val : 0 // ✗ avoid
let score = val || 0 // ✓ ok

禁止在return、throw、continue 和 break 语句之后出现不可达代码
eslint: no-unreachable

1
2
3
4
function doSomething () {
return true
console.log('never called') // ✗ avoid
}

禁止在 finally 语句块中出现控制流语句
eslint: no-unsafe-finally

1
2
3
4
5
6
7
try {
// ...
} catch (e) {
// ...
} finally {
return 42 // ✗ avoid
}

disallow negating the left operand of relational operators
eslint: no-unsafe-negation

1
if (!key in obj) {} // ✗ avoid

禁止不必要的 .call() 和 .apply()

1
sum.call(null, 1, 2, 3) // ✗ avoid

disallow unnecessary computed property keys in object literals
eslint: no-useless-computed-key

1
2
const user = { ['name']: 'John Doe' } // ✗ avoid
const user = { name: 'John Doe' } // ✓ ok

禁用不必要的构造函数
eslint: no-useless-constructor

1
2
3
4
class Car {
constructor () { // ✗ avoid
}
}

禁用不必要的转义字符
eslint: no-useless-escape

1
let message = 'Hell\o' // ✗ avoid

disallow renaming import, export, and destructured assignments to the same name
eslint: no-useless-rename

1
2
import { config as config } from './config' // ✗ avoid
import { config } from './config' // ✓ ok

禁用 with 语句
eslint: no-with

1
with (val) {...} // ✗ avoid

保持一致性将对象的属性放在不同的行上
eslint: object-property-newline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const user = {
name: 'Jane Doe', age: 30,
username: 'jdoe86' // ✗ avoid
}
const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok
const user = {
name: 'Jane Doe',
age: 30,
username: 'jdoe86'
}
```
禁止块内有空白填充
eslint: padded-blocks
``` javascript
if (user) {
// ✗ avoid
const name = getName()
}
if (user) {
const name = getName() // ✓ ok
}

enforce spacing between rest and spread operators and their expressions
eslint: rest-spread-spacing

1
2
fn(... args) // ✗ avoid
fn(...args) // ✓ ok

强制分号前后有空格
eslint: semi-spacing

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

强制在块之前使用一致的空格
eslint:space-before-blocks

1
2
if (admin){...} // ✗ avoid
if (admin) {...} // ✓ ok

强制在圆括号内使用一致的空格
eslint: space-in-parens

1
2
getName( name ) // ✗ avoid
getName(name) // ✓ ok

强制在一元操作符前后使用一致的空格
eslint: space-unary-ops

1
2
typeof!admin // ✗ avoid
typeof !admin // ✓ ok

强制在注释中 // 或 /* 使用一致的空格
eslint: spaced-comment

1
2
3
4
5
//comment // ✗ avoid
// comment // ✓ ok
/*comment*/ // ✗ avoid
/* comment */ // ✓ ok

要求或禁止模板字符串中的嵌入表达式周围空格的使用
eslint: template-curly-spacing

1
2
const message = `Hello, ${ name }` // ✗ avoid
const message = `Hello, ${name}` // ✓ ok

要求使用 isNaN() 检查 NaN
eslint: use-isnan

1
2
if (price === NaN) { } // ✗ avoid
if (isNaN(price)) { } // ✓ ok

强制 typeof 表达式与有效的字符串进行比较
eslint: valid-typeof

1
2
typeof name === 'undefimed' // ✗ avoid
typeof name === 'undefined' // ✓ ok

要求 IIFE 使用括号括起来
eslint: wrap-iife

1
2
3
4
const getName = function () { }() // ✗ avoid
const getName = (function () { }()) // ✓ ok
const getName = (function () { })() // ✓ ok

强制在 yield 表达式中 周围使用空格
eslint: yield-star-spacing

1
2
yield* increment() // ✗ avoid
yield * increment() // ✓ ok

要求或禁止 “Yoda” 条件
eslint: yoda

1
2
if (42 === age) { } // ✗ avoid
if (age === 42) { } // ✓ ok

禁止出现令人困惑的多行表达式
eslint: no-unexpected-multiline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ✓ ok
;(function () {
window.alert('ok')
}())
// ✗ avoid
(function () {
window.alert('ok')
}())
// ✓ ok
;[1, 2, 3].forEach(bar)
// ✗ avoid
[1, 2, 3].forEach(bar)
// ✓ ok
;`hello`.indexOf('o')
// ✗ avoid
`hello`.indexOf('o')

要求或禁止使用分号而不是 ASI
eslint: semi

1
2
window.alert('hi') // ✓ ok
window.alert('hi'); // ✗ avoid

如果想要调过某个规则的校验,可以采用eslint-disabled。

参考资料:
https://standardjs.com/readme-zhcn.html
http://eslint.cn/

坚持原创技术分享,您的支持将鼓励我继续创作!