eslint-config-tencent 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -177
- package/base.js +4 -1
- package/flat/base.js +4 -1
- package/flat/ts.js +5 -2
- package/package.json +1 -6
- package/ts.js +5 -2
package/README.md
CHANGED
|
@@ -152,7 +152,7 @@ stages:
|
|
|
152
152
|
|
|
153
153
|
## 1. 类型
|
|
154
154
|
|
|
155
|
-
- [1.1](#types--primitives) **【可选】 基本类型**: 当你访问一个基本类型时,直接操作它的值。
|
|
155
|
+
- [1.1](#types--primitives) <a id="types--primitives"></a> **【可选】 基本类型**: 当你访问一个基本类型时,直接操作它的值。
|
|
156
156
|
|
|
157
157
|
- `string`
|
|
158
158
|
- `number`
|
|
@@ -172,7 +172,7 @@ stages:
|
|
|
172
172
|
|
|
173
173
|
- 符号(Symbols)不能完全的被 [polyfill](https://en.wikipedia.org/wiki/Polyfill_(programming)),因此在不能原生支持symbol类型的浏览器或环境中,不应该使用symbol类型。
|
|
174
174
|
|
|
175
|
-
- [1.2](#types--complex) **【可选】 复杂类型**: 当你访问一个复杂类型时,直接操作其值的引用。
|
|
175
|
+
- [1.2](#types--complex) <a id="types--complex"></a> **【可选】 复杂类型**: 当你访问一个复杂类型时,直接操作其值的引用。
|
|
176
176
|
|
|
177
177
|
- `object`
|
|
178
178
|
- `array`
|
|
@@ -189,7 +189,7 @@ stages:
|
|
|
189
189
|
|
|
190
190
|
## 2. 引用
|
|
191
191
|
|
|
192
|
-
- [2.1](#references--prefer-const) **【必须】** 使用 `const` 定义你的所有引用;避免使用 `var`。 eslint: [`prefer-const`](https://eslint.org/docs/rules/prefer-const.html), [`no-const-assign`](https://eslint.org/docs/rules/no-const-assign.html)
|
|
192
|
+
- [2.1](#references--prefer-const) <a id="references--prefer-const"></a> **【必须】** 使用 `const` 定义你的所有引用;避免使用 `var`。 eslint: [`prefer-const`](https://eslint.org/docs/rules/prefer-const.html), [`no-const-assign`](https://eslint.org/docs/rules/no-const-assign.html)
|
|
193
193
|
|
|
194
194
|
> 原因? 这样能够确保你不能重新赋值你的引用,否则可能导致错误或者产生难以理解的代码。
|
|
195
195
|
|
|
@@ -203,7 +203,7 @@ stages:
|
|
|
203
203
|
const b = 2;
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
-
- [2.2](#references--disallow-var) **【必须】** 如果你必须重新赋值你的引用, 使用 `let` 代替 `var`。 eslint: [`no-var`](https://eslint.org/docs/rules/no-var.html)
|
|
206
|
+
- [2.2](#references--disallow-var) <a id="references--disallow-var"></a> **【必须】** 如果你必须重新赋值你的引用, 使用 `let` 代替 `var`。 eslint: [`no-var`](https://eslint.org/docs/rules/no-var.html)
|
|
207
207
|
|
|
208
208
|
> 原因? `let` 是块级作用域,而不像 `var` 是函数作用域。
|
|
209
209
|
|
|
@@ -221,7 +221,7 @@ stages:
|
|
|
221
221
|
}
|
|
222
222
|
```
|
|
223
223
|
|
|
224
|
-
- [2.3](#references--block-scope) **【可选】** 注意,let 和 const 都是块级作用域。
|
|
224
|
+
- [2.3](#references--block-scope) <a id="references--block-scope"></a> **【可选】** 注意,let 和 const 都是块级作用域。
|
|
225
225
|
|
|
226
226
|
```javascript
|
|
227
227
|
// const 和 let 只存在于他们定义的块级作用域中。
|
|
@@ -235,7 +235,7 @@ stages:
|
|
|
235
235
|
|
|
236
236
|
## 3. 对象
|
|
237
237
|
|
|
238
|
-
- [3.1](#objects--no-new) **【必须】** 使用字面量语法创建对象。 eslint: [`no-new-object`](https://eslint.org/docs/rules/no-new-object.html)
|
|
238
|
+
- [3.1](#objects--no-new) <a id="objects--no-new"></a> **【必须】** 使用字面量语法创建对象。 eslint: [`no-new-object`](https://eslint.org/docs/rules/no-new-object.html)
|
|
239
239
|
|
|
240
240
|
```javascript
|
|
241
241
|
// bad
|
|
@@ -245,7 +245,7 @@ stages:
|
|
|
245
245
|
const item = {};
|
|
246
246
|
```
|
|
247
247
|
|
|
248
|
-
- [3.2](#es6-computed-properties) **【推荐】** 在创建具有动态属性名称的对象时使用计算属性名。
|
|
248
|
+
- [3.2](#es6-computed-properties) <a id="es6-computed-properties"></a> **【推荐】** 在创建具有动态属性名称的对象时使用计算属性名。
|
|
249
249
|
|
|
250
250
|
> 原因? 它允许你在一个地方定义对象的所有属性。
|
|
251
251
|
|
|
@@ -269,7 +269,7 @@ stages:
|
|
|
269
269
|
};
|
|
270
270
|
```
|
|
271
271
|
|
|
272
|
-
- [3.3](#es6-object-shorthand) **【推荐】** 用对象方法简写。 eslint: [`object-shorthand`](https://eslint.org/docs/rules/object-shorthand.html)
|
|
272
|
+
- [3.3](#es6-object-shorthand) <a id="es6-object-shorthand"></a> **【推荐】** 用对象方法简写。 eslint: [`object-shorthand`](https://eslint.org/docs/rules/object-shorthand.html)
|
|
273
273
|
|
|
274
274
|
```javascript
|
|
275
275
|
// bad
|
|
@@ -291,7 +291,7 @@ stages:
|
|
|
291
291
|
};
|
|
292
292
|
```
|
|
293
293
|
|
|
294
|
-
- [3.4](#es6-object-concise) **【推荐】** 用属性值简写。 eslint: [`object-shorthand`](https://eslint.org/docs/rules/object-shorthand.html)
|
|
294
|
+
- [3.4](#es6-object-concise) <a id="es6-object-concise"></a> **【推荐】** 用属性值简写。 eslint: [`object-shorthand`](https://eslint.org/docs/rules/object-shorthand.html)
|
|
295
295
|
|
|
296
296
|
> 原因? 它更加简洁并更具描述性。
|
|
297
297
|
|
|
@@ -309,7 +309,7 @@ stages:
|
|
|
309
309
|
};
|
|
310
310
|
```
|
|
311
311
|
|
|
312
|
-
- [3.5](#objects--grouped-shorthand) **【推荐】** 声明对象时,将简写的属性放在前面。
|
|
312
|
+
- [3.5](#objects--grouped-shorthand) <a id="objects--grouped-shorthand"></a> **【推荐】** 声明对象时,将简写的属性放在前面。
|
|
313
313
|
|
|
314
314
|
> 原因? 这样更容易的判断哪些属性使用的简写。
|
|
315
315
|
|
|
@@ -338,7 +338,7 @@ stages:
|
|
|
338
338
|
};
|
|
339
339
|
```
|
|
340
340
|
|
|
341
|
-
- [3.6](#objects--quoted-props) **【必须】** 只使用引号标注无效标识符的属性。 eslint: [`quote-props`](https://eslint.org/docs/rules/quote-props.html)
|
|
341
|
+
- [3.6](#objects--quoted-props) <a id="objects--quoted-props"></a> **【必须】** 只使用引号标注无效标识符的属性。 eslint: [`quote-props`](https://eslint.org/docs/rules/quote-props.html)
|
|
342
342
|
|
|
343
343
|
> 原因? 一般来说,我们认为这样更容易阅读。 它能更好地适配语法高亮显示功能,并且更容易通过许多 JS 引擎进行优化。
|
|
344
344
|
|
|
@@ -358,7 +358,7 @@ stages:
|
|
|
358
358
|
};
|
|
359
359
|
```
|
|
360
360
|
|
|
361
|
-
- [3.7](#objects--prototype-builtins) **【推荐】** 不能直接调用 `Object.prototype` 的方法,如: `hasOwnProperty` 、 `propertyIsEnumerable` 和 `isPrototypeOf`。 eslint: [`no-prototype-builtins`](https://eslint.org/docs/rules/no-prototype-builtins.html)
|
|
361
|
+
- [3.7](#objects--prototype-builtins) <a id="objects--prototype-builtins"></a> **【推荐】** 不能直接调用 `Object.prototype` 的方法,如: `hasOwnProperty` 、 `propertyIsEnumerable` 和 `isPrototypeOf`。 eslint: [`no-prototype-builtins`](https://eslint.org/docs/rules/no-prototype-builtins.html)
|
|
362
362
|
|
|
363
363
|
> 原因? 这些方法可能被有问题的对象上的属性覆盖 - 如 `{ hasOwnProperty: false }` - 或者,对象是一个空对象 (`Object.create(null)`)。
|
|
364
364
|
|
|
@@ -378,7 +378,7 @@ stages:
|
|
|
378
378
|
console.log(has(object, key));
|
|
379
379
|
```
|
|
380
380
|
|
|
381
|
-
- [3.8](#objects--rest-spread) **【推荐】** 使用对象扩展操作符(spread operator)浅拷贝对象,而不是用 [`Object.assign`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) 方法。 使用对象的剩余操作符(rest operator)来获得一个新对象,该对象省略了某些属性。 eslint: [`prefer-object-spread`](https://eslint.org/docs/rules/prefer-object-spread)
|
|
381
|
+
- [3.8](#objects--rest-spread) <a id="objects--rest-spread"></a> **【推荐】** 使用对象扩展操作符(spread operator)浅拷贝对象,而不是用 [`Object.assign`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) 方法。 使用对象的剩余操作符(rest operator)来获得一个新对象,该对象省略了某些属性。 eslint: [`prefer-object-spread`](https://eslint.org/docs/rules/prefer-object-spread)
|
|
382
382
|
|
|
383
383
|
```javascript
|
|
384
384
|
// very bad
|
|
@@ -399,7 +399,7 @@ stages:
|
|
|
399
399
|
|
|
400
400
|
## 4. 数组
|
|
401
401
|
|
|
402
|
-
- [4.1](#arrays--literals) **【必须】** 使用字面量语法创建数组。 eslint: [`no-array-constructor`](https://eslint.org/docs/rules/no-array-constructor.html)
|
|
402
|
+
- [4.1](#arrays--literals) <a id="arrays--literals"></a> **【必须】** 使用字面量语法创建数组。 eslint: [`no-array-constructor`](https://eslint.org/docs/rules/no-array-constructor.html)
|
|
403
403
|
|
|
404
404
|
```javascript
|
|
405
405
|
// bad
|
|
@@ -409,7 +409,7 @@ stages:
|
|
|
409
409
|
const items = [];
|
|
410
410
|
```
|
|
411
411
|
|
|
412
|
-
- [4.2](#arrays--push) **【必须】** 使用 [Array#push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push) 代替直接赋值来给数组添加项。
|
|
412
|
+
- [4.2](#arrays--push) <a id="arrays--push"></a> **【必须】** 使用 [Array#push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push) 代替直接赋值来给数组添加项。
|
|
413
413
|
|
|
414
414
|
```javascript
|
|
415
415
|
const someStack = [];
|
|
@@ -421,7 +421,7 @@ stages:
|
|
|
421
421
|
someStack.push('abracadabra');
|
|
422
422
|
```
|
|
423
423
|
|
|
424
|
-
- [4.3](#es6-array-spreads) **【必须】** 使用数组展开符 `...` 来拷贝数组。
|
|
424
|
+
- [4.3](#es6-array-spreads) <a id="es6-array-spreads"></a> **【必须】** 使用数组展开符 `...` 来拷贝数组。
|
|
425
425
|
|
|
426
426
|
```javascript
|
|
427
427
|
// bad
|
|
@@ -437,7 +437,7 @@ stages:
|
|
|
437
437
|
const itemsCopy = [...items];
|
|
438
438
|
```
|
|
439
439
|
|
|
440
|
-
- [4.4](#arrays--from) **【推荐】** 使用展开符 `...` 代替 [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from),将一个可迭代对象转换成一个数组。
|
|
440
|
+
- [4.4](#arrays--from) <a id="arrays--from"></a> **【推荐】** 使用展开符 `...` 代替 [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from),将一个可迭代对象转换成一个数组。
|
|
441
441
|
|
|
442
442
|
```javascript
|
|
443
443
|
const foo = document.querySelectorAll('.foo');
|
|
@@ -449,7 +449,7 @@ stages:
|
|
|
449
449
|
const nodes = [...foo];
|
|
450
450
|
```
|
|
451
451
|
|
|
452
|
-
- [4.5](#arrays--mapping) **【必须】** 使用 [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) 将一个类数组(array-like)对象转换成一个数组。
|
|
452
|
+
- [4.5](#arrays--mapping) <a id="arrays--mapping"></a> **【必须】** 使用 [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) 将一个类数组(array-like)对象转换成一个数组。
|
|
453
453
|
|
|
454
454
|
```javascript
|
|
455
455
|
const arrLike = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
|
|
@@ -461,7 +461,7 @@ stages:
|
|
|
461
461
|
const arr = Array.from(arrLike);
|
|
462
462
|
```
|
|
463
463
|
|
|
464
|
-
- [4.6](#arrays--mapping) **【必须】** 使用 [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) 代替展开符 `...` 映射迭代器,因为它避免了创建一个中间数组。
|
|
464
|
+
- [4.6](#arrays--mapping) <a id="arrays--mapping"></a> **【必须】** 使用 [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) 代替展开符 `...` 映射迭代器,因为它避免了创建一个中间数组。
|
|
465
465
|
|
|
466
466
|
```javascript
|
|
467
467
|
// bad
|
|
@@ -471,7 +471,7 @@ stages:
|
|
|
471
471
|
const baz = Array.from(foo, bar);
|
|
472
472
|
```
|
|
473
473
|
|
|
474
|
-
- [4.7](#arrays--callback-return) **【推荐】** 在数组回调函数中使用 return 语句。 如果函数体由单个语句的返回表达式组成,并且无副作用,那么可以省略返回值, 具体查看 [8.2](#arrows--implicit-return)
|
|
474
|
+
- [4.7](#arrays--callback-return) <a id="arrays--callback-return"></a> **【推荐】** 在数组回调函数中使用 return 语句。 如果函数体由单个语句的返回表达式组成,并且无副作用,那么可以省略返回值, 具体查看 [8.2](#arrows--implicit-return) <a id="arrows--implicit-return"></a>。 eslint: [`array-callback-return`](https://eslint.org/docs/rules/array-callback-return)
|
|
475
475
|
|
|
476
476
|
```javascript
|
|
477
477
|
// good
|
|
@@ -517,7 +517,7 @@ stages:
|
|
|
517
517
|
});
|
|
518
518
|
```
|
|
519
519
|
|
|
520
|
-
- [4.8](#arrays--bracket-newline) **【推荐】** 如果数组有多行,则在数组开始括号 `[` 的时候换行,然后在数组结束括号 `]` 的时候换行。 eslint: [`array-bracket-newline`](https://eslint.org/docs/rules/array-bracket-newline)
|
|
520
|
+
- [4.8](#arrays--bracket-newline) <a id="arrays--bracket-newline"></a> **【推荐】** 如果数组有多行,则在数组开始括号 `[` 的时候换行,然后在数组结束括号 `]` 的时候换行。 eslint: [`array-bracket-newline`](https://eslint.org/docs/rules/array-bracket-newline)
|
|
521
521
|
|
|
522
522
|
```javascript
|
|
523
523
|
// bad
|
|
@@ -555,7 +555,7 @@ stages:
|
|
|
555
555
|
|
|
556
556
|
## 5. 解构
|
|
557
557
|
|
|
558
|
-
- [5.1](#destructuring--object) **【推荐】** 在访问和使用对象的多个属性时使用对象解构。 eslint: [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring)
|
|
558
|
+
- [5.1](#destructuring--object) <a id="destructuring--object"></a> **【推荐】** 在访问和使用对象的多个属性时使用对象解构。 eslint: [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring)
|
|
559
559
|
|
|
560
560
|
> 原因? 解构可以避免为这些属性创建临时引用。
|
|
561
561
|
|
|
@@ -580,7 +580,7 @@ stages:
|
|
|
580
580
|
}
|
|
581
581
|
```
|
|
582
582
|
|
|
583
|
-
- [5.2](#destructuring--array) **【推荐】** 使用数组解构。 eslint: [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring)
|
|
583
|
+
- [5.2](#destructuring--array) <a id="destructuring--array"></a> **【推荐】** 使用数组解构。 eslint: [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring)
|
|
584
584
|
|
|
585
585
|
```javascript
|
|
586
586
|
const arr = [1, 2, 3, 4];
|
|
@@ -593,7 +593,7 @@ stages:
|
|
|
593
593
|
const [first, second] = arr;
|
|
594
594
|
```
|
|
595
595
|
|
|
596
|
-
- [5.3](#destructuring--object-over-array) **【必须】** 在有多个返回值时, 使用对象解构,而不是数组解构。
|
|
596
|
+
- [5.3](#destructuring--object-over-array) <a id="destructuring--object-over-array"></a> **【必须】** 在有多个返回值时, 使用对象解构,而不是数组解构。
|
|
597
597
|
|
|
598
598
|
> 原因? 你可以随时添加新的属性或者改变属性的顺序,而不用修改调用方。
|
|
599
599
|
|
|
@@ -619,7 +619,7 @@ stages:
|
|
|
619
619
|
|
|
620
620
|
## 6. 字符
|
|
621
621
|
|
|
622
|
-
- [6.1](#strings--quotes) **【推荐】** 使用单引号 `''` 定义字符串。 eslint: [`quotes`](https://eslint.org/docs/rules/quotes.html)
|
|
622
|
+
- [6.1](#strings--quotes) <a id="strings--quotes"></a> **【推荐】** 使用单引号 `''` 定义字符串。 eslint: [`quotes`](https://eslint.org/docs/rules/quotes.html)
|
|
623
623
|
|
|
624
624
|
```javascript
|
|
625
625
|
// bad
|
|
@@ -632,7 +632,7 @@ stages:
|
|
|
632
632
|
const name = 'Capt. Janeway';
|
|
633
633
|
```
|
|
634
634
|
|
|
635
|
-
- [6.2](#strings--line-length) **【必须】** 不应该用字符串跨行连接符的格式来跨行编写,这样会使当前行长度超过100个字符。
|
|
635
|
+
- [6.2](#strings--line-length) <a id="strings--line-length"></a> **【必须】** 不应该用字符串跨行连接符的格式来跨行编写,这样会使当前行长度超过100个字符。
|
|
636
636
|
|
|
637
637
|
> 原因? 断开的字符串维护起来很痛苦,并且会提高索引难度。
|
|
638
638
|
|
|
@@ -652,7 +652,7 @@ stages:
|
|
|
652
652
|
const errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
|
|
653
653
|
```
|
|
654
654
|
|
|
655
|
-
- [6.3](#es6-template-literals) **【必须】** 构建字符串时,使用字符串模板代替字符串拼接。 eslint: [`prefer-template`](https://eslint.org/docs/rules/prefer-template.html) [`template-curly-spacing`](https://eslint.org/docs/rules/template-curly-spacing)
|
|
655
|
+
- [6.3](#es6-template-literals) <a id="es6-template-literals"></a> **【必须】** 构建字符串时,使用字符串模板代替字符串拼接。 eslint: [`prefer-template`](https://eslint.org/docs/rules/prefer-template.html) [`template-curly-spacing`](https://eslint.org/docs/rules/template-curly-spacing)
|
|
656
656
|
|
|
657
657
|
> 原因? 字符串模板为您提供了一种可读的、简洁的语法,具有正确的换行和字符串插值特性。
|
|
658
658
|
|
|
@@ -678,9 +678,9 @@ stages:
|
|
|
678
678
|
}
|
|
679
679
|
```
|
|
680
680
|
|
|
681
|
-
- [6.4](#strings--eval) **【必须】** 永远不要使用 `eval()` 执行放在字符串中的代码,它导致了太多的漏洞。 eslint: [`no-eval`](https://eslint.org/docs/rules/no-eval)
|
|
681
|
+
- [6.4](#strings--eval) <a id="strings--eval"></a> **【必须】** 永远不要使用 `eval()` 执行放在字符串中的代码,它导致了太多的漏洞。 eslint: [`no-eval`](https://eslint.org/docs/rules/no-eval)
|
|
682
682
|
|
|
683
|
-
- [6.5](#strings--escaping) **【必须】** 不要在字符串中转义不必要的字符。 eslint: [`no-useless-escape`](https://eslint.org/docs/rules/no-useless-escape)
|
|
683
|
+
- [6.5](#strings--escaping) <a id="strings--escaping"></a> **【必须】** 不要在字符串中转义不必要的字符。 eslint: [`no-useless-escape`](https://eslint.org/docs/rules/no-useless-escape)
|
|
684
684
|
|
|
685
685
|
> 原因? 反斜杠损害了可读性,因此只有在必要的时候才可以出现。
|
|
686
686
|
|
|
@@ -695,7 +695,7 @@ stages:
|
|
|
695
695
|
|
|
696
696
|
## 7. 函数
|
|
697
697
|
|
|
698
|
-
- [7.1](#functions--declarations) **【可选】** 使用命名的函数表达式代替函数声明。 eslint: [`func-style`](https://eslint.org/docs/rules/func-style)
|
|
698
|
+
- [7.1](#functions--declarations) <a id="functions--declarations"></a> **【可选】** 使用命名的函数表达式代替函数声明。 eslint: [`func-style`](https://eslint.org/docs/rules/func-style)
|
|
699
699
|
|
|
700
700
|
> 原因? 函数声明时作用域被提前了,这意味着在一个文件里函数很容易(太容易了)在其定义之前被引用。这样伤害了代码可读性和可维护性。如果你发现一个函数又大又复杂,并且它干扰了对这个文件其他部分的理解,那么是时候把这个函数单独抽成一个模块了!别忘了给表达式显式的命名,不用管这个名字是不是由一个确定的变量推断出来的(这在现代浏览器和类似babel编译器中很常见)。这消除了由匿名函数在错误调用栈产生的所有假设。 * ([Discussion](https://github.com/airbnb/javascript/issues/794))
|
|
701
701
|
|
|
@@ -716,7 +716,7 @@ stages:
|
|
|
716
716
|
};
|
|
717
717
|
```
|
|
718
718
|
|
|
719
|
-
- [7.2](#functions--iife) **【必须】** 把立即执行函数包裹在圆括号里。 eslint: [`wrap-iife`](https://eslint.org/docs/rules/wrap-iife.html)
|
|
719
|
+
- [7.2](#functions--iife) <a id="functions--iife"></a> **【必须】** 把立即执行函数包裹在圆括号里。 eslint: [`wrap-iife`](https://eslint.org/docs/rules/wrap-iife.html)
|
|
720
720
|
|
|
721
721
|
> 原因? 立即调用的函数表达式是个独立的单元 - 将它和它的调用括号还有入参包装在一起可以非常清晰的表明这一点。请注意,在一个到处都是模块的世界中,您几乎用不到 IIFE。
|
|
722
722
|
|
|
@@ -727,9 +727,9 @@ stages:
|
|
|
727
727
|
}());
|
|
728
728
|
```
|
|
729
729
|
|
|
730
|
-
- [7.3](#functions--in-blocks) **【必须】** 切记不要在非功能块中声明函数 (`if`, `while`, 等)。 请将函数赋值给变量。 浏览器允许你这样做, 但是不同浏览器会有不同的行为, 这并不是什么好事。 eslint: [`no-loop-func`](https://eslint.org/docs/rules/no-loop-func.html)
|
|
730
|
+
- [7.3](#functions--in-blocks) <a id="functions--in-blocks"></a> **【必须】** 切记不要在非功能块中声明函数 (`if`, `while`, 等)。 请将函数赋值给变量。 浏览器允许你这样做, 但是不同浏览器会有不同的行为, 这并不是什么好事。 eslint: [`no-loop-func`](https://eslint.org/docs/rules/no-loop-func.html)
|
|
731
731
|
|
|
732
|
-
- [7.4](#functions--note-on-blocks) **【必须】** ECMA-262 将 `block` 定义为语句列表。 而函数声明并不是语句。
|
|
732
|
+
- [7.4](#functions--note-on-blocks) <a id="functions--note-on-blocks"></a> **【必须】** ECMA-262 将 `block` 定义为语句列表。 而函数声明并不是语句。
|
|
733
733
|
|
|
734
734
|
```javascript
|
|
735
735
|
// bad
|
|
@@ -748,7 +748,7 @@ stages:
|
|
|
748
748
|
}
|
|
749
749
|
```
|
|
750
750
|
|
|
751
|
-
- [7.5](#functions--arguments-shadow) **【必须】** 永远不要给一个参数命名为 `arguments`。 这将会覆盖函数默认的 `arguments` 对象。 eslint: [`no-shadow-restricted-names`](https://eslint.org/docs/rules/no-shadow-restricted-names)
|
|
751
|
+
- [7.5](#functions--arguments-shadow) <a id="functions--arguments-shadow"></a> **【必须】** 永远不要给一个参数命名为 `arguments`。 这将会覆盖函数默认的 `arguments` 对象。 eslint: [`no-shadow-restricted-names`](https://eslint.org/docs/rules/no-shadow-restricted-names)
|
|
752
752
|
|
|
753
753
|
```javascript
|
|
754
754
|
// bad
|
|
@@ -762,7 +762,7 @@ stages:
|
|
|
762
762
|
}
|
|
763
763
|
```
|
|
764
764
|
|
|
765
|
-
- [7.6](#es6-rest) **【推荐】** 使用 rest 语法 `...` 代替 `arguments`。 eslint: [`prefer-rest-params`](https://eslint.org/docs/rules/prefer-rest-params)
|
|
765
|
+
- [7.6](#es6-rest) <a id="es6-rest"></a> **【推荐】** 使用 rest 语法 `...` 代替 `arguments`。 eslint: [`prefer-rest-params`](https://eslint.org/docs/rules/prefer-rest-params)
|
|
766
766
|
|
|
767
767
|
> 原因? `...` 明确了你想要拉取什么参数。 而且, rest 参数是一个真正的数组,而不仅仅是类数组的 `arguments` 。
|
|
768
768
|
|
|
@@ -779,7 +779,7 @@ stages:
|
|
|
779
779
|
}
|
|
780
780
|
```
|
|
781
781
|
|
|
782
|
-
- [7.7](#es6-default-parameters) **【推荐】** 使用默认的参数语法,而不是改变函数参数。
|
|
782
|
+
- [7.7](#es6-default-parameters) <a id="es6-default-parameters"></a> **【推荐】** 使用默认的参数语法,而不是改变函数参数。
|
|
783
783
|
|
|
784
784
|
```javascript
|
|
785
785
|
// really bad
|
|
@@ -805,7 +805,7 @@ stages:
|
|
|
805
805
|
}
|
|
806
806
|
```
|
|
807
807
|
|
|
808
|
-
- [7.8](#functions--default-side-effects) **【必须】** 使用默认参数时避免副作用。
|
|
808
|
+
- [7.8](#functions--default-side-effects) <a id="functions--default-side-effects"></a> **【必须】** 使用默认参数时避免副作用。
|
|
809
809
|
|
|
810
810
|
> 原因? 他们很容易混淆。
|
|
811
811
|
|
|
@@ -821,7 +821,7 @@ stages:
|
|
|
821
821
|
count(); // 3
|
|
822
822
|
```
|
|
823
823
|
|
|
824
|
-
- [7.9](#functions--defaults-last) **【推荐】** 总是把默认参数放在最后。 eslint: [`default-param-last`](https://eslint.org/docs/rules/default-param-last)
|
|
824
|
+
- [7.9](#functions--defaults-last) <a id="functions--defaults-last"></a> **【推荐】** 总是把默认参数放在最后。 eslint: [`default-param-last`](https://eslint.org/docs/rules/default-param-last)
|
|
825
825
|
|
|
826
826
|
```javascript
|
|
827
827
|
// bad
|
|
@@ -835,7 +835,7 @@ stages:
|
|
|
835
835
|
}
|
|
836
836
|
```
|
|
837
837
|
|
|
838
|
-
- [7.10](#functions--constructor) **【推荐】** 永远不要使用函数构造器来创建一个新函数。 eslint: [`no-new-func`](https://eslint.org/docs/rules/no-new-func)
|
|
838
|
+
- [7.10](#functions--constructor) <a id="functions--constructor"></a> **【推荐】** 永远不要使用函数构造器来创建一个新函数。 eslint: [`no-new-func`](https://eslint.org/docs/rules/no-new-func)
|
|
839
839
|
|
|
840
840
|
> 原因? 以这种方式创建一个函数跟 `eval()` 差不多,将会导致漏洞。
|
|
841
841
|
|
|
@@ -847,7 +847,7 @@ stages:
|
|
|
847
847
|
var subtract = Function('a', 'b', 'return a - b');
|
|
848
848
|
```
|
|
849
849
|
|
|
850
|
-
- [7.11](#functions--signature-spacing) **【必须】** 函数声明语句中需要空格。 eslint: [`space-before-function-paren`](https://eslint.org/docs/rules/space-before-function-paren) [`space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks)
|
|
850
|
+
- [7.11](#functions--signature-spacing) <a id="functions--signature-spacing"></a> **【必须】** 函数声明语句中需要空格。 eslint: [`space-before-function-paren`](https://eslint.org/docs/rules/space-before-function-paren) [`space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks)
|
|
851
851
|
|
|
852
852
|
> 原因? 一致性很好,在删除或添加名称时不需要添加或删除空格。
|
|
853
853
|
|
|
@@ -862,7 +862,7 @@ stages:
|
|
|
862
862
|
const y = function a() {};
|
|
863
863
|
```
|
|
864
864
|
|
|
865
|
-
- [7.12](#functions--mutate-params) **【推荐】** 不要改变入参。 eslint: [`no-param-reassign`](https://eslint.org/docs/rules/no-param-reassign.html)
|
|
865
|
+
- [7.12](#functions--mutate-params) <a id="functions--mutate-params"></a> **【推荐】** 不要改变入参。 eslint: [`no-param-reassign`](https://eslint.org/docs/rules/no-param-reassign.html)
|
|
866
866
|
|
|
867
867
|
> 原因? 操作入参对象会导致原始调用位置出现意想不到的副作用。
|
|
868
868
|
|
|
@@ -878,7 +878,7 @@ stages:
|
|
|
878
878
|
}
|
|
879
879
|
```
|
|
880
880
|
|
|
881
|
-
- [7.13](#functions--reassign-params) **【推荐】** 不要对入参重新赋值,也不要给入参的属性赋值。部分要求修改入参的常用库(如 Koa、Vuex)可以豁免。 eslint: [`no-param-reassign`](https://eslint.org/docs/rules/no-param-reassign.html)
|
|
881
|
+
- [7.13](#functions--reassign-params) <a id="functions--reassign-params"></a> **【推荐】** 不要对入参重新赋值,也不要给入参的属性赋值。部分要求修改入参的常用库(如 Koa、Vuex)可以豁免。 eslint: [`no-param-reassign`](https://eslint.org/docs/rules/no-param-reassign.html)
|
|
882
882
|
|
|
883
883
|
> 原因? 重新赋值参数会导致意外的行为,尤其是在访问 `arguments` 对象的时候。 它还可能导致性能优化问题,尤其是在 V8 中。
|
|
884
884
|
|
|
@@ -905,7 +905,7 @@ stages:
|
|
|
905
905
|
}
|
|
906
906
|
```
|
|
907
907
|
|
|
908
|
-
- [7.14](#functions--spread-vs-apply) **【推荐】** 优先使用扩展运算符 `...` 来调用可变参数函数。 eslint: [`prefer-spread`](https://eslint.org/docs/rules/prefer-spread)
|
|
908
|
+
- [7.14](#functions--spread-vs-apply) <a id="functions--spread-vs-apply"></a> **【推荐】** 优先使用扩展运算符 `...` 来调用可变参数函数。 eslint: [`prefer-spread`](https://eslint.org/docs/rules/prefer-spread)
|
|
909
909
|
|
|
910
910
|
> 原因? 它更加清晰,你不需要提供上下文,并且能比用 `apply` 来执行可变参数的 `new` 操作更容易些。
|
|
911
911
|
|
|
@@ -925,7 +925,7 @@ stages:
|
|
|
925
925
|
new Date(...[2016, 8, 5]);
|
|
926
926
|
```
|
|
927
927
|
|
|
928
|
-
- [7.15](#functions--signature-invocation-indentation) **【推荐】** 调用或者书写一个包含多个参数的函数应该像这个指南里的其他多行代码写法一样: 每行值包含一个参数,并且最后一行也要以逗号结尾。eslint: [`function-paren-newline`](https://eslint.org/docs/rules/function-paren-newline)
|
|
928
|
+
- [7.15](#functions--signature-invocation-indentation) <a id="functions--signature-invocation-indentation"></a> **【推荐】** 调用或者书写一个包含多个参数的函数应该像这个指南里的其他多行代码写法一样: 每行值包含一个参数,并且最后一行也要以逗号结尾。eslint: [`function-paren-newline`](https://eslint.org/docs/rules/function-paren-newline)
|
|
929
929
|
|
|
930
930
|
```javascript
|
|
931
931
|
// bad
|
|
@@ -959,7 +959,7 @@ stages:
|
|
|
959
959
|
|
|
960
960
|
## 8. 箭头函数
|
|
961
961
|
|
|
962
|
-
- [8.1](#arrows--use-them) **【推荐】** 当你必须使用匿名函数时 (当传递内联函数时), 使用箭头函数。 eslint: [`prefer-arrow-callback`](https://eslint.org/docs/rules/prefer-arrow-callback.html), [`arrow-spacing`](https://eslint.org/docs/rules/arrow-spacing.html)
|
|
962
|
+
- [8.1](#arrows--use-them) <a id="arrows--use-them"></a> **【推荐】** 当你必须使用匿名函数时 (当传递内联函数时), 使用箭头函数。 eslint: [`prefer-arrow-callback`](https://eslint.org/docs/rules/prefer-arrow-callback.html), [`arrow-spacing`](https://eslint.org/docs/rules/arrow-spacing.html)
|
|
963
963
|
|
|
964
964
|
> 原因? 它创建了一个在 `this` 上下文中执行的函数版本,它通常是你想要的,并且是一个更简洁的语法。
|
|
965
965
|
|
|
@@ -979,7 +979,7 @@ stages:
|
|
|
979
979
|
});
|
|
980
980
|
```
|
|
981
981
|
|
|
982
|
-
- [8.2](#arrows--implicit-return) **【推荐】** 如果函数体由一个没有副作用的 [表达式](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) 语句组成,删除大括号和 `return`。否则,保留括号并继续使用 `return` 语句。 eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens.html), [`arrow-body-style`](https://eslint.org/docs/rules/arrow-body-style.html)
|
|
982
|
+
- [8.2](#arrows--implicit-return) <a id="arrows--implicit-return"></a> **【推荐】** 如果函数体由一个没有副作用的 [表达式](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) 语句组成,删除大括号和 `return`。否则,保留括号并继续使用 `return` 语句。 eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens.html), [`arrow-body-style`](https://eslint.org/docs/rules/arrow-body-style.html)
|
|
983
983
|
|
|
984
984
|
> 原因? 语法糖。 多个函数被链接在一起时,提高可读性。
|
|
985
985
|
|
|
@@ -1023,7 +1023,7 @@ stages:
|
|
|
1023
1023
|
});
|
|
1024
1024
|
```
|
|
1025
1025
|
|
|
1026
|
-
- [8.3](#arrows--paren-wrap) **【推荐】** 如果表达式跨越多个行,用括号将其括起来,以获得更好的可读性。
|
|
1026
|
+
- [8.3](#arrows--paren-wrap) <a id="arrows--paren-wrap"></a> **【推荐】** 如果表达式跨越多个行,用括号将其括起来,以获得更好的可读性。
|
|
1027
1027
|
|
|
1028
1028
|
> 原因? 它清楚地表明了函数的起点和终点。
|
|
1029
1029
|
|
|
@@ -1044,7 +1044,7 @@ stages:
|
|
|
1044
1044
|
));
|
|
1045
1045
|
```
|
|
1046
1046
|
|
|
1047
|
-
- [8.4](#arrows--one-arg-parens) **【推荐】** 如果你的函数只有一个参数并且函数体没有大括号,就删除圆括号。 否则,为了保证清晰和一致性,请给参数加上括号。 注意:总是使用括号是可以接受的,在这种情况下,我们使用 [“always” option](https://eslint.org/docs/rules/arrow-parens#always) 来配置 eslint. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens.html)
|
|
1047
|
+
- [8.4](#arrows--one-arg-parens) <a id="arrows--one-arg-parens"></a> **【推荐】** 如果你的函数只有一个参数并且函数体没有大括号,就删除圆括号。 否则,为了保证清晰和一致性,请给参数加上括号。 注意:总是使用括号是可以接受的,在这种情况下,我们使用 [“always” option](https://eslint.org/docs/rules/arrow-parens#always) 来配置 eslint. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens.html)
|
|
1048
1048
|
|
|
1049
1049
|
> 原因? 让代码看上去不那么乱。
|
|
1050
1050
|
|
|
@@ -1073,7 +1073,7 @@ stages:
|
|
|
1073
1073
|
});
|
|
1074
1074
|
```
|
|
1075
1075
|
|
|
1076
|
-
- [8.5](#arrows--confusing) **【推荐】** 避免搞混箭头函数符号 (`=>`) 和比较运算符 (`<=`, `>=`)。 eslint: [`no-confusing-arrow`](https://eslint.org/docs/rules/no-confusing-arrow)
|
|
1076
|
+
- [8.5](#arrows--confusing) <a id="arrows--confusing"></a> **【推荐】** 避免搞混箭头函数符号 (`=>`) 和比较运算符 (`<=`, `>=`)。 eslint: [`no-confusing-arrow`](https://eslint.org/docs/rules/no-confusing-arrow)
|
|
1077
1077
|
|
|
1078
1078
|
```javascript
|
|
1079
1079
|
// bad
|
|
@@ -1092,7 +1092,7 @@ stages:
|
|
|
1092
1092
|
};
|
|
1093
1093
|
```
|
|
1094
1094
|
|
|
1095
|
-
- [8.6](#whitespace--implicit-arrow-linebreak) **【推荐】** 在箭头函数用隐式 return 时强制将函数体的位置约束在箭头后。 eslint: [`implicit-arrow-linebreak`](https://eslint.org/docs/rules/implicit-arrow-linebreak)
|
|
1095
|
+
- [8.6](#whitespace--implicit-arrow-linebreak) <a id="whitespace--implicit-arrow-linebreak"></a> **【推荐】** 在箭头函数用隐式 return 时强制将函数体的位置约束在箭头后。 eslint: [`implicit-arrow-linebreak`](https://eslint.org/docs/rules/implicit-arrow-linebreak)
|
|
1096
1096
|
|
|
1097
1097
|
```javascript
|
|
1098
1098
|
// bad
|
|
@@ -1112,7 +1112,7 @@ stages:
|
|
|
1112
1112
|
|
|
1113
1113
|
## 9. 类和构造器
|
|
1114
1114
|
|
|
1115
|
-
- [9.1](#constructors--use-class) **【推荐】** 尽量使用 `class`. 避免直接操作 `prototype` .
|
|
1115
|
+
- [9.1](#constructors--use-class) <a id="constructors--use-class"></a> **【推荐】** 尽量使用 `class`. 避免直接操作 `prototype` .
|
|
1116
1116
|
|
|
1117
1117
|
> 原因? `class` 语法更简洁,更容易看懂。
|
|
1118
1118
|
|
|
@@ -1140,7 +1140,7 @@ stages:
|
|
|
1140
1140
|
}
|
|
1141
1141
|
```
|
|
1142
1142
|
|
|
1143
|
-
- [9.2](#constructors--extends) **【推荐】** 使用 `extends` 来实现继承。
|
|
1143
|
+
- [9.2](#constructors--extends) <a id="constructors--extends"></a> **【推荐】** 使用 `extends` 来实现继承。
|
|
1144
1144
|
|
|
1145
1145
|
> 原因? 它是一个内置的方法,可以在不破坏 `instanceof` 的情况下继承原型功能。
|
|
1146
1146
|
|
|
@@ -1163,7 +1163,7 @@ stages:
|
|
|
1163
1163
|
}
|
|
1164
1164
|
```
|
|
1165
1165
|
|
|
1166
|
-
- [9.3](#constructors--chaining) **【可选】** 类的成员方法,可以返回 `this`, 来实现链式调用。
|
|
1166
|
+
- [9.3](#constructors--chaining) <a id="constructors--chaining"></a> **【可选】** 类的成员方法,可以返回 `this`, 来实现链式调用。
|
|
1167
1167
|
|
|
1168
1168
|
```javascript
|
|
1169
1169
|
// bad
|
|
@@ -1199,7 +1199,7 @@ stages:
|
|
|
1199
1199
|
.setHeight(20);
|
|
1200
1200
|
```
|
|
1201
1201
|
|
|
1202
|
-
- [9.4](#constructors--tostring) **【可选】** 只要在确保能正常工作并且不产生任何副作用的情况下,编写一个自定义的 `toString()` 方法也是可以的。
|
|
1202
|
+
- [9.4](#constructors--tostring) <a id="constructors--tostring"></a> **【可选】** 只要在确保能正常工作并且不产生任何副作用的情况下,编写一个自定义的 `toString()` 方法也是可以的。
|
|
1203
1203
|
|
|
1204
1204
|
```javascript
|
|
1205
1205
|
class Jedi {
|
|
@@ -1217,7 +1217,7 @@ stages:
|
|
|
1217
1217
|
}
|
|
1218
1218
|
```
|
|
1219
1219
|
|
|
1220
|
-
- [9.5](#constructors--no-useless) **【推荐】** 如果没有具体说明,类有默认的构造方法。一个空的构造函数或只是代表父类的构造函数是不需要写的。 eslint: [`no-useless-constructor`](https://eslint.org/docs/rules/no-useless-constructor)
|
|
1220
|
+
- [9.5](#constructors--no-useless) <a id="constructors--no-useless"></a> **【推荐】** 如果没有具体说明,类有默认的构造方法。一个空的构造函数或只是代表父类的构造函数是不需要写的。 eslint: [`no-useless-constructor`](https://eslint.org/docs/rules/no-useless-constructor)
|
|
1221
1221
|
|
|
1222
1222
|
```javascript
|
|
1223
1223
|
// bad
|
|
@@ -1246,7 +1246,7 @@ stages:
|
|
|
1246
1246
|
}
|
|
1247
1247
|
```
|
|
1248
1248
|
|
|
1249
|
-
- [9.6](#classes--no-duplicate-members) **【必须】** 避免定义重复的类成员。 eslint: [`no-dupe-class-members`](https://eslint.org/docs/rules/no-dupe-class-members)
|
|
1249
|
+
- [9.6](#classes--no-duplicate-members) <a id="classes--no-duplicate-members"></a> **【必须】** 避免定义重复的类成员。 eslint: [`no-dupe-class-members`](https://eslint.org/docs/rules/no-dupe-class-members)
|
|
1250
1250
|
|
|
1251
1251
|
> 原因? 重复的类成员声明将会默认使用最后一个 - 具有重复的类成员可以说是一个bug。
|
|
1252
1252
|
|
|
@@ -1268,7 +1268,7 @@ stages:
|
|
|
1268
1268
|
|
|
1269
1269
|
## 10. 模块
|
|
1270
1270
|
|
|
1271
|
-
- [10.1](#modules--use-them) **【可选】** 使用ES6的模块 (`import`/`export`) 语法来定义模块。
|
|
1271
|
+
- [10.1](#modules--use-them) <a id="modules--use-them"></a> **【可选】** 使用ES6的模块 (`import`/`export`) 语法来定义模块。
|
|
1272
1272
|
|
|
1273
1273
|
```javascript
|
|
1274
1274
|
// bad
|
|
@@ -1284,7 +1284,7 @@ stages:
|
|
|
1284
1284
|
export default es6;
|
|
1285
1285
|
```
|
|
1286
1286
|
|
|
1287
|
-
- [10.2](#modules--no-wildcard) **【推荐】** 不要使用import * 通配符
|
|
1287
|
+
- [10.2](#modules--no-wildcard) <a id="modules--no-wildcard"></a> **【推荐】** 不要使用import * 通配符
|
|
1288
1288
|
|
|
1289
1289
|
> 原因? 这确保你有单个默认的导出。
|
|
1290
1290
|
|
|
@@ -1296,7 +1296,7 @@ stages:
|
|
|
1296
1296
|
import AirbnbStyleGuide from './AirbnbStyleGuide';
|
|
1297
1297
|
```
|
|
1298
1298
|
|
|
1299
|
-
- [10.3](#modules--no-export-from-import) **【推荐】** 不要在import语句中直接export。
|
|
1299
|
+
- [10.3](#modules--no-export-from-import) <a id="modules--no-export-from-import"></a> **【推荐】** 不要在import语句中直接export。
|
|
1300
1300
|
|
|
1301
1301
|
> 原因? 虽然写在一行很简洁,但是有一个明确的导入和一个明确的导出能够保证一致性。
|
|
1302
1302
|
|
|
@@ -1311,7 +1311,7 @@ stages:
|
|
|
1311
1311
|
export default es6;
|
|
1312
1312
|
```
|
|
1313
1313
|
|
|
1314
|
-
- [10.4](#import/no-duplicates) **【必须】** 对于同一个路径,只在一个地方引入所有需要的东西。
|
|
1314
|
+
- [10.4](#import/no-duplicates) <a id="import/no-duplicates"></a> **【必须】** 对于同一个路径,只在一个地方引入所有需要的东西。
|
|
1315
1315
|
eslint: [`no-duplicates`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-duplicates.md)
|
|
1316
1316
|
|
|
1317
1317
|
> 原因? 对于同一个路径,如果存在多行引入,会使代码更难以维护。
|
|
@@ -1332,7 +1332,7 @@ stages:
|
|
|
1332
1332
|
} from 'foo';
|
|
1333
1333
|
```
|
|
1334
1334
|
|
|
1335
|
-
- [10.5](#modules--no-mutable-exports) **【推荐】** 不要导出可变的引用。
|
|
1335
|
+
- [10.5](#modules--no-mutable-exports) <a id="modules--no-mutable-exports"></a> **【推荐】** 不要导出可变的引用。
|
|
1336
1336
|
eslint: [`import/no-mutable-exports`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-mutable-exports.md)
|
|
1337
1337
|
|
|
1338
1338
|
> 原因? 在一般情况下,应该避免导出可变引用。虽然在某些特殊情况下,可能需要这样,但是一般情况下只需要导出常量引用。
|
|
@@ -1347,7 +1347,7 @@ stages:
|
|
|
1347
1347
|
export { foo };
|
|
1348
1348
|
```
|
|
1349
1349
|
|
|
1350
|
-
- [10.6](#modules--prefer-default-export) **【可选】** 在只有单一导出的模块里,用 export default 更好。
|
|
1350
|
+
- [10.6](#modules--prefer-default-export) <a id="modules--prefer-default-export"></a> **【可选】** 在只有单一导出的模块里,用 export default 更好。
|
|
1351
1351
|
eslint: [`import/prefer-default-export`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/prefer-default-export.md)
|
|
1352
1352
|
|
|
1353
1353
|
> 原因? 鼓励更多的模块只做单一导出,会增强代码的可读性和可维护性。
|
|
@@ -1360,7 +1360,7 @@ stages:
|
|
|
1360
1360
|
export default function foo() {}
|
|
1361
1361
|
```
|
|
1362
1362
|
|
|
1363
|
-
- [10.7](#modules--imports-first) **【必须】** 将所有的 `import`s 语句放在其他语句之前。
|
|
1363
|
+
- [10.7](#modules--imports-first) <a id="modules--imports-first"></a> **【必须】** 将所有的 `import`s 语句放在其他语句之前。
|
|
1364
1364
|
eslint: [`import/first`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/first.md)
|
|
1365
1365
|
|
|
1366
1366
|
> 原因? 将所有的 `import`s 提到顶部,可以防止某些诡异行为的发生。
|
|
@@ -1379,7 +1379,7 @@ stages:
|
|
|
1379
1379
|
foo.init();
|
|
1380
1380
|
```
|
|
1381
1381
|
|
|
1382
|
-
- [10.8](#modules--multiline-imports-over-newlines) **【可选】** 多行引入应该像多行数组和对象字面量一样缩进。
|
|
1382
|
+
- [10.8](#modules--multiline-imports-over-newlines) <a id="modules--multiline-imports-over-newlines"></a> **【可选】** 多行引入应该像多行数组和对象字面量一样缩进。
|
|
1383
1383
|
|
|
1384
1384
|
> 原因? 这里的花括号和其他地方的花括号是一样的,遵循相同的缩进规则。末尾的逗号也是一样的。
|
|
1385
1385
|
|
|
@@ -1397,7 +1397,7 @@ stages:
|
|
|
1397
1397
|
} from 'path';
|
|
1398
1398
|
```
|
|
1399
1399
|
|
|
1400
|
-
- [10.9](#modules--no-webpack-loader-syntax) **【推荐】** 在模块导入语句中禁止使用 Webpack 加载器语法。
|
|
1400
|
+
- [10.9](#modules--no-webpack-loader-syntax) <a id="modules--no-webpack-loader-syntax"></a> **【推荐】** 在模块导入语句中禁止使用 Webpack 加载器语法。
|
|
1401
1401
|
eslint: [`import/no-webpack-loader-syntax`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-webpack-loader-syntax.md)
|
|
1402
1402
|
|
|
1403
1403
|
> 原因? 因为在导入语句中使用 webpack 语法,会将代码和打包工具耦合在一起。应该在 `webpack.config.js` 中使用加载器语法。
|
|
@@ -1414,7 +1414,7 @@ stages:
|
|
|
1414
1414
|
|
|
1415
1415
|
## 11. 迭代器和发生器
|
|
1416
1416
|
|
|
1417
|
-
- [11.1](#iterators--nope) **【推荐】** 不要使用迭代器。 推荐使用 JavaScript 的高阶函数代替 `for-in` 。 eslint: [`no-iterator`](https://eslint.org/docs/rules/no-iterator.html) [`no-restricted-syntax`](https://eslint.org/docs/rules/no-restricted-syntax)
|
|
1417
|
+
- [11.1](#iterators--nope) <a id="iterators--nope"></a> **【推荐】** 不要使用迭代器。 推荐使用 JavaScript 的高阶函数代替 `for-in` 。 eslint: [`no-iterator`](https://eslint.org/docs/rules/no-iterator.html) [`no-restricted-syntax`](https://eslint.org/docs/rules/no-restricted-syntax)
|
|
1418
1418
|
|
|
1419
1419
|
> 原因? 这有助于不可变性原则。 使用带有返回值的纯函数比使用那些带有副作用的方法,更具有可读性。
|
|
1420
1420
|
|
|
@@ -1457,11 +1457,11 @@ stages:
|
|
|
1457
1457
|
const increasedByOne = numbers.map(num => num + 1);
|
|
1458
1458
|
```
|
|
1459
1459
|
|
|
1460
|
-
- [11.2](#generators--nope) **【可选】** 现在不要使用generator。
|
|
1460
|
+
- [11.2](#generators--nope) <a id="generators--nope"></a> **【可选】** 现在不要使用generator。
|
|
1461
1461
|
|
|
1462
1462
|
> 原因? 它们不能很好的转译为 ES5。但可以在Nodejs中使用。*
|
|
1463
1463
|
|
|
1464
|
-
- [11.3](#generators--spacing) **【推荐】** 如果你必须要使用generator,请确保正确使用空格。 eslint: [`generator-star-spacing`](https://eslint.org/docs/rules/generator-star-spacing)
|
|
1464
|
+
- [11.3](#generators--spacing) <a id="generators--spacing"></a> **【推荐】** 如果你必须要使用generator,请确保正确使用空格。 eslint: [`generator-star-spacing`](https://eslint.org/docs/rules/generator-star-spacing)
|
|
1465
1465
|
|
|
1466
1466
|
> 原因? `function` 和 `*` 是同一个概念关键字的一部分 - `*` 不是 `function` 的修饰符, `function*` 是一个不同于 `function` 的构造器。
|
|
1467
1467
|
|
|
@@ -1523,7 +1523,7 @@ stages:
|
|
|
1523
1523
|
|
|
1524
1524
|
## 12. 属性
|
|
1525
1525
|
|
|
1526
|
-
- [12.1](#properties--dot) **【推荐】** 访问属性时使用点符号。 eslint: [`dot-notation`](https://eslint.org/docs/rules/dot-notation.html)
|
|
1526
|
+
- [12.1](#properties--dot) <a id="properties--dot"></a> **【推荐】** 访问属性时使用点符号。 eslint: [`dot-notation`](https://eslint.org/docs/rules/dot-notation.html)
|
|
1527
1527
|
|
|
1528
1528
|
```javascript
|
|
1529
1529
|
const luke = {
|
|
@@ -1538,7 +1538,7 @@ stages:
|
|
|
1538
1538
|
const isJedi = luke.jedi;
|
|
1539
1539
|
```
|
|
1540
1540
|
|
|
1541
|
-
- [12.2](#properties--bracket) **【可选】** 使用变量访问属性时,用 `[]`表示法。
|
|
1541
|
+
- [12.2](#properties--bracket) <a id="properties--bracket"></a> **【可选】** 使用变量访问属性时,用 `[]`表示法。
|
|
1542
1542
|
|
|
1543
1543
|
```javascript
|
|
1544
1544
|
const luke = {
|
|
@@ -1553,7 +1553,7 @@ stages:
|
|
|
1553
1553
|
const isJedi = getProp('jedi');
|
|
1554
1554
|
```
|
|
1555
1555
|
|
|
1556
|
-
- [12.3](#es2016-properties--exponentiation-operator) **【推荐】** 计算指数时,可以使用 `**` 运算符。 eslint: [`no-restricted-properties`](https://eslint.org/docs/rules/no-restricted-properties).
|
|
1556
|
+
- [12.3](#es2016-properties--exponentiation-operator) <a id="es2016-properties--exponentiation-operator"></a> **【推荐】** 计算指数时,可以使用 `**` 运算符。 eslint: [`no-restricted-properties`](https://eslint.org/docs/rules/no-restricted-properties).
|
|
1557
1557
|
|
|
1558
1558
|
```javascript
|
|
1559
1559
|
// bad
|
|
@@ -1565,7 +1565,7 @@ stages:
|
|
|
1565
1565
|
|
|
1566
1566
|
## 13. 变量
|
|
1567
1567
|
|
|
1568
|
-
- [13.1](#variables--const) **【必须】** 变量应先声明再使用,禁止引用任何未声明的变量,除非你明确知道引用的变量存在于当前作用域链上。禁止不带任何关键词定义变量,这样做将会创建一个全局变量,污染全局命名空间,造成程序意料之外的错误。 eslint: [`no-undef`](https://eslint.org/docs/rules/no-undef) [`prefer-const`](https://eslint.org/docs/rules/prefer-const)
|
|
1568
|
+
- [13.1](#variables--const) <a id="variables--const"></a> **【必须】** 变量应先声明再使用,禁止引用任何未声明的变量,除非你明确知道引用的变量存在于当前作用域链上。禁止不带任何关键词定义变量,这样做将会创建一个全局变量,污染全局命名空间,造成程序意料之外的错误。 eslint: [`no-undef`](https://eslint.org/docs/rules/no-undef) [`prefer-const`](https://eslint.org/docs/rules/prefer-const)
|
|
1569
1569
|
|
|
1570
1570
|
```javascript
|
|
1571
1571
|
// bad, 这会创建一个全局变量
|
|
@@ -1619,7 +1619,7 @@ stages:
|
|
|
1619
1619
|
console.log('global', i);
|
|
1620
1620
|
```
|
|
1621
1621
|
|
|
1622
|
-
- [13.2](#variables--one-const) **【推荐】** 声明多个变量应该分开声明,避免使用 `,` 一次声明多个变量。 eslint: [`one-var`](https://eslint.org/docs/rules/one-var.html)
|
|
1622
|
+
- [13.2](#variables--one-const) <a id="variables--one-const"></a> **【推荐】** 声明多个变量应该分开声明,避免使用 `,` 一次声明多个变量。 eslint: [`one-var`](https://eslint.org/docs/rules/one-var.html)
|
|
1623
1623
|
|
|
1624
1624
|
> 原因? 这样更容易添加新的变量声明,不必担心是使用 `;` 还是使用 `,` 所带来的代码差异。使用版本管理工具如 git ,最后那行的 `;` 就不会被标记为修改成 `,`。 并且可以通过 debugger 逐步查看每个声明,而不是立即跳过所有声明。
|
|
1625
1625
|
|
|
@@ -1641,7 +1641,7 @@ stages:
|
|
|
1641
1641
|
const dragonball = 'z';
|
|
1642
1642
|
```
|
|
1643
1643
|
|
|
1644
|
-
- [13.3](#variables--const-let-group) **【推荐】** 把 `const` 声明语句放在一起,把 `let` 声明语句放在一起。
|
|
1644
|
+
- [13.3](#variables--const-let-group) <a id="variables--const-let-group"></a> **【推荐】** 把 `const` 声明语句放在一起,把 `let` 声明语句放在一起。
|
|
1645
1645
|
|
|
1646
1646
|
> 原因? 这在后边如果需要根据前边的赋值变量指定一个变量时很有用,且更容易知道哪些变量是不希望被修改的。
|
|
1647
1647
|
|
|
@@ -1661,7 +1661,7 @@ stages:
|
|
|
1661
1661
|
let length;
|
|
1662
1662
|
```
|
|
1663
1663
|
|
|
1664
|
-
- [13.4](#variables--define-where-used) **【推荐】** 在你真正需要使用到变量的代码块内定义变量。
|
|
1664
|
+
- [13.4](#variables--define-where-used) <a id="variables--define-where-used"></a> **【推荐】** 在你真正需要使用到变量的代码块内定义变量。
|
|
1665
1665
|
|
|
1666
1666
|
> 原因? `let` 和 `const` 是块级作用域而不是函数作用域,不存在变量提升的情况。
|
|
1667
1667
|
|
|
@@ -1699,7 +1699,7 @@ stages:
|
|
|
1699
1699
|
}
|
|
1700
1700
|
```
|
|
1701
1701
|
|
|
1702
|
-
- [13.5](#variables--no-chain-assignment) **【必须】** 不要链式变量赋值。 eslint: [`no-multi-assign`](https://eslint.org/docs/rules/no-multi-assign)
|
|
1702
|
+
- [13.5](#variables--no-chain-assignment) <a id="variables--no-chain-assignment"></a> **【必须】** 不要链式变量赋值。 eslint: [`no-multi-assign`](https://eslint.org/docs/rules/no-multi-assign)
|
|
1703
1703
|
|
|
1704
1704
|
> 原因? 链式变量赋值会创建隐式全局变量。
|
|
1705
1705
|
|
|
@@ -1738,7 +1738,7 @@ stages:
|
|
|
1738
1738
|
// 对于 `const` 也一样
|
|
1739
1739
|
```
|
|
1740
1740
|
|
|
1741
|
-
- [13.6](#variables--unary-increment-decrement) **【必须】** 避免使用不必要的递增和递减操作符 (`++`, `--`)。 eslint [`no-plusplus`](https://eslint.org/docs/rules/no-plusplus)
|
|
1741
|
+
- [13.6](#variables--unary-increment-decrement) <a id="variables--unary-increment-decrement"></a> **【必须】** 避免使用不必要的递增和递减操作符 (`++`, `--`)。 eslint [`no-plusplus`](https://eslint.org/docs/rules/no-plusplus)
|
|
1742
1742
|
|
|
1743
1743
|
> 原因? 在eslint文档中,一元操作符 `++` 和 `--`会自动添加分号,不同的空白可能会改变源代码的语义。建议使用 `num += 1` 这样的语句来做递增和递减,而不是使用 `num++` 或 `num ++` 。同时 `++num` 和 `num++` 的差异也使代码的可读性变差。不必要的增量和减量语句会导致无法预先明确递增/预递减值,这可能会导致程序中的意外行为。
|
|
1744
1744
|
|
|
@@ -1786,7 +1786,7 @@ stages:
|
|
|
1786
1786
|
const truthyCount = array.filter(Boolean).length;
|
|
1787
1787
|
```
|
|
1788
1788
|
|
|
1789
|
-
- [13.7](#variables--linebreak) **【必须】** 避免在赋值语句 `=` 前后换行。如果你的代码单行长度超过了 [`max-len`](https://eslint.org/docs/rules/max-len.html) 定义的长度而不得不换行,那么使用括号包裹。 eslint [`operator-linebreak`](https://eslint.org/docs/rules/operator-linebreak.html).
|
|
1789
|
+
- [13.7](#variables--linebreak) <a id="variables--linebreak"></a> **【必须】** 避免在赋值语句 `=` 前后换行。如果你的代码单行长度超过了 [`max-len`](https://eslint.org/docs/rules/max-len.html) 定义的长度而不得不换行,那么使用括号包裹。 eslint [`operator-linebreak`](https://eslint.org/docs/rules/operator-linebreak.html).
|
|
1790
1790
|
|
|
1791
1791
|
> 原因? 在 `=` 前后换行,可能混淆赋的值。
|
|
1792
1792
|
|
|
@@ -1843,7 +1843,7 @@ stages:
|
|
|
1843
1843
|
|
|
1844
1844
|
```
|
|
1845
1845
|
|
|
1846
|
-
- [13.8](#variables--no-unused-vars) **【必须】** 禁止定义了变量却不使用它。 eslint: [`no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars)
|
|
1846
|
+
- [13.8](#variables--no-unused-vars) <a id="variables--no-unused-vars"></a> **【必须】** 禁止定义了变量却不使用它。 eslint: [`no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars)
|
|
1847
1847
|
|
|
1848
1848
|
> 原因? 在代码里到处定义变量却没有使用它,不完整的代码结构看起来像是个代码错误。即使没有使用,但是定义变量仍然需要消耗资源,并且对阅读代码的人也会造成困惑,不知道这些变量是要做什么的。
|
|
1849
1849
|
|
|
@@ -1904,7 +1904,7 @@ stages:
|
|
|
1904
1904
|
|
|
1905
1905
|
## 14. 变量提升
|
|
1906
1906
|
|
|
1907
|
-
- [14.1](#hoisting--about) **【可选】** `var` 定义的变量会被提升到函数作用域范围内的最顶部,但是对它的赋值是不会被提升的,因此在函数顶部相当于定义了变量,但是值是 `undefined`。`const` 和 `let` 声明的变量受到一个称之为 "暂时性死区" [(Temporal Dead Zones ,简称 TDZ)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone) 的新概念保护,因此在 "暂时性死区" 内部的 `const` 和 `let` 变量,都需要先声明再使用,否则会报错。详情可以阅读 [typeof 不再安全](https://web.archive.org/web/20200121061528/http://es-discourse.com/t/why-typeof-is-no-longer-safe/15) 这篇文章。
|
|
1907
|
+
- [14.1](#hoisting--about) <a id="hoisting--about"></a> **【可选】** `var` 定义的变量会被提升到函数作用域范围内的最顶部,但是对它的赋值是不会被提升的,因此在函数顶部相当于定义了变量,但是值是 `undefined`。`const` 和 `let` 声明的变量受到一个称之为 "暂时性死区" [(Temporal Dead Zones ,简称 TDZ)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone) 的新概念保护,因此在 "暂时性死区" 内部的 `const` 和 `let` 变量,都需要先声明再使用,否则会报错。详情可以阅读 [typeof 不再安全](https://web.archive.org/web/20200121061528/http://es-discourse.com/t/why-typeof-is-no-longer-safe/15) 这篇文章。
|
|
1908
1908
|
|
|
1909
1909
|
```javascript
|
|
1910
1910
|
// notDefined 未定义 (假设没有定义的同名全局变量)
|
|
@@ -1944,7 +1944,7 @@ stages:
|
|
|
1944
1944
|
}
|
|
1945
1945
|
```
|
|
1946
1946
|
|
|
1947
|
-
- [14.2](#hoisting--anon-expressions) **【可选】** 匿名函数赋值表达式提升变量名,而不是函数赋值。
|
|
1947
|
+
- [14.2](#hoisting--anon-expressions) <a id="hoisting--anon-expressions"></a> **【可选】** 匿名函数赋值表达式提升变量名,而不是函数赋值。
|
|
1948
1948
|
|
|
1949
1949
|
```javascript
|
|
1950
1950
|
function example() {
|
|
@@ -1960,7 +1960,7 @@ stages:
|
|
|
1960
1960
|
}
|
|
1961
1961
|
```
|
|
1962
1962
|
|
|
1963
|
-
- [14.3](#hoisting--named-expressions) **【可选】** 命名函数表达式提升的是变量名,而不是函数名或者函数体。
|
|
1963
|
+
- [14.3](#hoisting--named-expressions) <a id="hoisting--named-expressions"></a> **【可选】** 命名函数表达式提升的是变量名,而不是函数名或者函数体。
|
|
1964
1964
|
|
|
1965
1965
|
```javascript
|
|
1966
1966
|
function example() {
|
|
@@ -1992,7 +1992,7 @@ stages:
|
|
|
1992
1992
|
}
|
|
1993
1993
|
```
|
|
1994
1994
|
|
|
1995
|
-
- [14.4](#hoisting--declarations) **【可选】** 函数声明提升其名称和函数体。
|
|
1995
|
+
- [14.4](#hoisting--declarations) <a id="hoisting--declarations"></a> **【可选】** 函数声明提升其名称和函数体。
|
|
1996
1996
|
|
|
1997
1997
|
```javascript
|
|
1998
1998
|
function example() {
|
|
@@ -2009,7 +2009,7 @@ stages:
|
|
|
2009
2009
|
|
|
2010
2010
|
## 15. 比较运算符和等号
|
|
2011
2011
|
|
|
2012
|
-
- [15.1](#comparison--eqeqeq) **【推荐】** 使用 `===` 和 `!==` 而不是 `==` 和 `!=`。 eslint: [`eqeqeq`](https://eslint.org/docs/rules/eqeqeq.html)
|
|
2012
|
+
- [15.1](#comparison--eqeqeq) <a id="comparison--eqeqeq"></a> **【推荐】** 使用 `===` 和 `!==` 而不是 `==` 和 `!=`。 eslint: [`eqeqeq`](https://eslint.org/docs/rules/eqeqeq.html)
|
|
2013
2013
|
|
|
2014
2014
|
> 原因?`==` 和 `!=` 存在类型转换,会得到和 `===`、`!==` 不一样的结果
|
|
2015
2015
|
|
|
@@ -2035,7 +2035,7 @@ stages:
|
|
|
2035
2035
|
'' === false
|
|
2036
2036
|
```
|
|
2037
2037
|
|
|
2038
|
-
- [15.2](#comparison--if) **【可选】** 条件语句,例如 `if` 语句使用 `ToBoolean` 的抽象方法来计算表达式的结果,并始终遵循以下简单的规则:
|
|
2038
|
+
- [15.2](#comparison--if) <a id="comparison--if"></a> **【可选】** 条件语句,例如 `if` 语句使用 `ToBoolean` 的抽象方法来计算表达式的结果,并始终遵循以下简单的规则:
|
|
2039
2039
|
|
|
2040
2040
|
- **Objects** 的取值为: **true**,{} 和 [] 的取值也为: **true**
|
|
2041
2041
|
- **Undefined** 的取值为: **false**
|
|
@@ -2050,7 +2050,7 @@ stages:
|
|
|
2050
2050
|
}
|
|
2051
2051
|
```
|
|
2052
2052
|
|
|
2053
|
-
- [15.3](#comparison--shortcuts) **【推荐】** 对于布尔值(在明确知道是布尔值的情况下)使用简写,但是对于字符串和数字进行显式比较。
|
|
2053
|
+
- [15.3](#comparison--shortcuts) <a id="comparison--shortcuts"></a> **【推荐】** 对于布尔值(在明确知道是布尔值的情况下)使用简写,但是对于字符串和数字进行显式比较。
|
|
2054
2054
|
|
|
2055
2055
|
```javascript
|
|
2056
2056
|
// bad
|
|
@@ -2084,9 +2084,9 @@ stages:
|
|
|
2084
2084
|
}
|
|
2085
2085
|
```
|
|
2086
2086
|
|
|
2087
|
-
- [15.4](#comparison--moreinfo) **【可选】** 关于布尔值转换和条件语句,详细信息可参阅 Angus Croll 的 [Truth Equality and JavaScript](https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) 这篇文章。
|
|
2087
|
+
- [15.4](#comparison--moreinfo) <a id="comparison--moreinfo"></a> **【可选】** 关于布尔值转换和条件语句,详细信息可参阅 Angus Croll 的 [Truth Equality and JavaScript](https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) 这篇文章。
|
|
2088
2088
|
|
|
2089
|
-
- [15.5](#comparison--switch-blocks) **【必须】** 在 `case` 和 `default` 的子句中,如果存在声明 (例如. `let`, `const`, `function`, 和 `class`),使用大括号来创建块级作用域。 eslint: [`no-case-declarations`](https://eslint.org/docs/rules/no-case-declarations.html)
|
|
2089
|
+
- [15.5](#comparison--switch-blocks) <a id="comparison--switch-blocks"></a> **【必须】** 在 `case` 和 `default` 的子句中,如果存在声明 (例如. `let`, `const`, `function`, 和 `class`),使用大括号来创建块级作用域。 eslint: [`no-case-declarations`](https://eslint.org/docs/rules/no-case-declarations.html)
|
|
2090
2090
|
|
|
2091
2091
|
> 原因? 变量声明的作用域在整个 switch 语句内,但是只有在 case 条件为真时变量才会被初始化。 当多个 `case` 语句定义相同的变量时,就会导致变量覆盖的问题。
|
|
2092
2092
|
|
|
@@ -2133,7 +2133,7 @@ stages:
|
|
|
2133
2133
|
}
|
|
2134
2134
|
```
|
|
2135
2135
|
|
|
2136
|
-
- [15.6](#comparison--nested-ternaries) **【推荐】** 三元表达式不应该嵌套,通常是单行表达式,如果确实需要多行表达式,那么应该考虑使用条件语句。 eslint: [`no-nested-ternary`](https://eslint.org/docs/rules/no-nested-ternary.html)
|
|
2136
|
+
- [15.6](#comparison--nested-ternaries) <a id="comparison--nested-ternaries"></a> **【推荐】** 三元表达式不应该嵌套,通常是单行表达式,如果确实需要多行表达式,那么应该考虑使用条件语句。 eslint: [`no-nested-ternary`](https://eslint.org/docs/rules/no-nested-ternary.html)
|
|
2137
2137
|
|
|
2138
2138
|
```javascript
|
|
2139
2139
|
// bad
|
|
@@ -2153,7 +2153,7 @@ stages:
|
|
|
2153
2153
|
const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
|
|
2154
2154
|
```
|
|
2155
2155
|
|
|
2156
|
-
- [15.7](#comparison--unneeded-ternary) **【推荐】** 避免不必要的三元表达式。 eslint: [`no-unneeded-ternary`](https://eslint.org/docs/rules/no-unneeded-ternary.html)
|
|
2156
|
+
- [15.7](#comparison--unneeded-ternary) <a id="comparison--unneeded-ternary"></a> **【推荐】** 避免不必要的三元表达式。 eslint: [`no-unneeded-ternary`](https://eslint.org/docs/rules/no-unneeded-ternary.html)
|
|
2157
2157
|
|
|
2158
2158
|
```javascript
|
|
2159
2159
|
// bad
|
|
@@ -2167,7 +2167,7 @@ stages:
|
|
|
2167
2167
|
const baz = !c;
|
|
2168
2168
|
```
|
|
2169
2169
|
|
|
2170
|
-
- [15.8](#comparison--no-mixed-operators) **【必须】** 使用混合运算符时,使用小括号括起来需要一起计算的部分,只要觉得有必要,那么尽可能地用括号让代码的优先级更明显。大家都能理解的运算符 `+`、`-`、`**` 不要求添加括号。但我们建议在 `*`、`/` 之间添加括号,因为乘除运算符混写写的比较长的时候容易产生歧义。 eslint: [`no-mixed-operators`](https://eslint.org/docs/rules/no-mixed-operators.html)
|
|
2170
|
+
- [15.8](#comparison--no-mixed-operators) <a id="comparison--no-mixed-operators"></a> **【必须】** 使用混合运算符时,使用小括号括起来需要一起计算的部分,只要觉得有必要,那么尽可能地用括号让代码的优先级更明显。大家都能理解的运算符 `+`、`-`、`**` 不要求添加括号。但我们建议在 `*`、`/` 之间添加括号,因为乘除运算符混写写的比较长的时候容易产生歧义。 eslint: [`no-mixed-operators`](https://eslint.org/docs/rules/no-mixed-operators.html)
|
|
2171
2171
|
|
|
2172
2172
|
> 原因? 这能提高可读性并且表明开发人员的意图。
|
|
2173
2173
|
|
|
@@ -2203,7 +2203,7 @@ stages:
|
|
|
2203
2203
|
|
|
2204
2204
|
## 16. 代码块
|
|
2205
2205
|
|
|
2206
|
-
- [16.1](#blocks--braces) **【必须】** 当有多行代码块的时候,应使用大括号包裹。 eslint: [`nonblock-statement-body-position`](https://eslint.org/docs/rules/nonblock-statement-body-position)
|
|
2206
|
+
- [16.1](#blocks--braces) <a id="blocks--braces"></a> **【必须】** 当有多行代码块的时候,应使用大括号包裹。 eslint: [`nonblock-statement-body-position`](https://eslint.org/docs/rules/nonblock-statement-body-position)
|
|
2207
2207
|
|
|
2208
2208
|
```javascript
|
|
2209
2209
|
// bad
|
|
@@ -2235,7 +2235,7 @@ stages:
|
|
|
2235
2235
|
}
|
|
2236
2236
|
```
|
|
2237
2237
|
|
|
2238
|
-
- [16.2](#blocks--cuddled-elses) **【必须】** 如果你使用的是 `if` 和 `else` 的多行代码块,则将 `else` 语句放在 `if` 块闭括号同一行的位置。 eslint: [`brace-style`](https://eslint.org/docs/rules/brace-style.html)
|
|
2238
|
+
- [16.2](#blocks--cuddled-elses) <a id="blocks--cuddled-elses"></a> **【必须】** 如果你使用的是 `if` 和 `else` 的多行代码块,则将 `else` 语句放在 `if` 块闭括号同一行的位置。 eslint: [`brace-style`](https://eslint.org/docs/rules/brace-style.html)
|
|
2239
2239
|
|
|
2240
2240
|
```javascript
|
|
2241
2241
|
// bad
|
|
@@ -2256,7 +2256,7 @@ stages:
|
|
|
2256
2256
|
}
|
|
2257
2257
|
```
|
|
2258
2258
|
|
|
2259
|
-
- [16.3](#blocks--no-else-return) **【推荐】** 如果一个 `if` 块总是会执行 return 语句,那么接下来的 `else` 块就没有必要了。 如果一个包含 `return` 语句的 `else if` 块,在一个包含了 `return` 语句的 `if` 块之后,那么可以拆成多个 `if` 块。 eslint: [`no-else-return`](https://eslint.org/docs/rules/no-else-return)
|
|
2259
|
+
- [16.3](#blocks--no-else-return) <a id="blocks--no-else-return"></a> **【推荐】** 如果一个 `if` 块总是会执行 return 语句,那么接下来的 `else` 块就没有必要了。 如果一个包含 `return` 语句的 `else if` 块,在一个包含了 `return` 语句的 `if` 块之后,那么可以拆成多个 `if` 块。 eslint: [`no-else-return`](https://eslint.org/docs/rules/no-else-return)
|
|
2260
2260
|
|
|
2261
2261
|
```javascript
|
|
2262
2262
|
// bad
|
|
@@ -2322,7 +2322,7 @@ stages:
|
|
|
2322
2322
|
|
|
2323
2323
|
## 17. 控制语句
|
|
2324
2324
|
|
|
2325
|
-
- [17.1](#control-statements) **【推荐】** 如果你的控制语句 (`if`, `while` 等) 太长或者超过了一行最大长度的限制,则可以将每个条件(或组)放入一个新的行。 逻辑运算符应该在行的开始。
|
|
2325
|
+
- [17.1](#control-statements) <a id="control-statements"></a> **【推荐】** 如果你的控制语句 (`if`, `while` 等) 太长或者超过了一行最大长度的限制,则可以将每个条件(或组)放入一个新的行。 逻辑运算符应该在行的开始。
|
|
2326
2326
|
|
|
2327
2327
|
> 原因? 在行的开头要求运算符保持对齐,并遵循类似于方法链的模式。这提高了可读性,并且使更复杂的逻辑更容易直观的被理解。
|
|
2328
2328
|
|
|
@@ -2375,7 +2375,7 @@ stages:
|
|
|
2375
2375
|
}
|
|
2376
2376
|
```
|
|
2377
2377
|
|
|
2378
|
-
- [17.2](#control-statements--value-selection) **【必须】** 不要使用选择操作符代替控制语句。
|
|
2378
|
+
- [17.2](#control-statements--value-selection) <a id="control-statements--value-selection"></a> **【必须】** 不要使用选择操作符代替控制语句。
|
|
2379
2379
|
|
|
2380
2380
|
```javascript
|
|
2381
2381
|
// bad
|
|
@@ -2389,7 +2389,7 @@ stages:
|
|
|
2389
2389
|
|
|
2390
2390
|
## 18. 注释
|
|
2391
2391
|
|
|
2392
|
-
- [18.1](#comments--multiline) **【必须】** 使用 `/* ... */` 来进行多行注释。
|
|
2392
|
+
- [18.1](#comments--multiline) <a id="comments--multiline"></a> **【必须】** 使用 `/* ... */` 来进行多行注释。
|
|
2393
2393
|
|
|
2394
2394
|
```javascript
|
|
2395
2395
|
// bad
|
|
@@ -2418,7 +2418,7 @@ stages:
|
|
|
2418
2418
|
}
|
|
2419
2419
|
```
|
|
2420
2420
|
|
|
2421
|
-
- [18.2](#comments--singleline) **【推荐】** 使用 `//` 进行单行注释。 将单行注释放在需要注释的行的上方新行。 建议在注释之前放一个空行,除非它在块的第一行。但如果一段代码每行都包含注释,允许不加分行。
|
|
2421
|
+
- [18.2](#comments--singleline) <a id="comments--singleline"></a> **【推荐】** 使用 `//` 进行单行注释。 将单行注释放在需要注释的行的上方新行。 建议在注释之前放一个空行,除非它在块的第一行。但如果一段代码每行都包含注释,允许不加分行。
|
|
2422
2422
|
|
|
2423
2423
|
```javascript
|
|
2424
2424
|
// bad
|
|
@@ -2456,7 +2456,7 @@ stages:
|
|
|
2456
2456
|
}
|
|
2457
2457
|
```
|
|
2458
2458
|
|
|
2459
|
-
- [18.3](#comments--spaces) **【必须】** 用一个空格开始所有的注释,使它更容易阅读。 eslint: [`spaced-comment`](https://eslint.org/docs/rules/spaced-comment)
|
|
2459
|
+
- [18.3](#comments--spaces) <a id="comments--spaces"></a> **【必须】** 用一个空格开始所有的注释,使它更容易阅读。 eslint: [`spaced-comment`](https://eslint.org/docs/rules/spaced-comment)
|
|
2460
2460
|
|
|
2461
2461
|
```javascript
|
|
2462
2462
|
// bad
|
|
@@ -2492,9 +2492,9 @@ stages:
|
|
|
2492
2492
|
}
|
|
2493
2493
|
```
|
|
2494
2494
|
|
|
2495
|
-
- [18.4](#comments--actionitems) **【推荐】** 使用 `FIXME` 或者 `TODO` 开始你的注释可以帮助其他开发人员快速了解相应代码,如果你提出了一个需要重新讨论的问题,或者你对需要解决的问题提出的解决方案。 这些不同于其他普通注释,因为它们是可操作的。 这些操作是 `FIXME: -- 需要解决这个问题` 或者 `TODO: -- 需要被实现`。
|
|
2495
|
+
- [18.4](#comments--actionitems) <a id="comments--actionitems"></a> **【推荐】** 使用 `FIXME` 或者 `TODO` 开始你的注释可以帮助其他开发人员快速了解相应代码,如果你提出了一个需要重新讨论的问题,或者你对需要解决的问题提出的解决方案。 这些不同于其他普通注释,因为它们是可操作的。 这些操作是 `FIXME: -- 需要解决这个问题` 或者 `TODO: -- 需要被实现`。
|
|
2496
2496
|
|
|
2497
|
-
- [18.5](#comments--fixme) **【推荐】** 使用 `// FIXME:` 注释问题。
|
|
2497
|
+
- [18.5](#comments--fixme) <a id="comments--fixme"></a> **【推荐】** 使用 `// FIXME:` 注释问题。
|
|
2498
2498
|
|
|
2499
2499
|
```javascript
|
|
2500
2500
|
class Calculator extends Abacus {
|
|
@@ -2507,7 +2507,7 @@ stages:
|
|
|
2507
2507
|
}
|
|
2508
2508
|
```
|
|
2509
2509
|
|
|
2510
|
-
- [18.6](#comments--todo) **【推荐】** 使用 `// TODO:` 注释解决问题的方法。
|
|
2510
|
+
- [18.6](#comments--todo) <a id="comments--todo"></a> **【推荐】** 使用 `// TODO:` 注释解决问题的方法。
|
|
2511
2511
|
|
|
2512
2512
|
```javascript
|
|
2513
2513
|
class Calculator extends Abacus {
|
|
@@ -2520,7 +2520,7 @@ stages:
|
|
|
2520
2520
|
}
|
|
2521
2521
|
```
|
|
2522
2522
|
|
|
2523
|
-
- [18.7](#comments--jsdoc) **【必须】** `/** ... */` 风格(首行有两个 *)的块级多行注释仅能被用于 JSDoc。
|
|
2523
|
+
- [18.7](#comments--jsdoc) <a id="comments--jsdoc"></a> **【必须】** `/** ... */` 风格(首行有两个 *)的块级多行注释仅能被用于 JSDoc。
|
|
2524
2524
|
|
|
2525
2525
|
```javascript
|
|
2526
2526
|
class Calculator {
|
|
@@ -2538,7 +2538,7 @@ stages:
|
|
|
2538
2538
|
|
|
2539
2539
|
## 19. 空白
|
|
2540
2540
|
|
|
2541
|
-
- [19.1](#whitespace--spaces) **【推荐】** 使用 tabs (空格字符) 设置为2个空格。 eslint: [`indent`](https://eslint.org/docs/rules/indent.html)
|
|
2541
|
+
- [19.1](#whitespace--spaces) <a id="whitespace--spaces"></a> **【推荐】** 使用 tabs (空格字符) 设置为2个空格。 eslint: [`indent`](https://eslint.org/docs/rules/indent.html)
|
|
2542
2542
|
|
|
2543
2543
|
```javascript
|
|
2544
2544
|
// bad
|
|
@@ -2557,7 +2557,7 @@ stages:
|
|
|
2557
2557
|
}
|
|
2558
2558
|
```
|
|
2559
2559
|
|
|
2560
|
-
- [19.2](#whitespace--before-blocks) **【必须】** 在花括号前放置一个空格。 eslint: [`space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks.html)
|
|
2560
|
+
- [19.2](#whitespace--before-blocks) <a id="whitespace--before-blocks"></a> **【必须】** 在花括号前放置一个空格。 eslint: [`space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks.html)
|
|
2561
2561
|
|
|
2562
2562
|
```javascript
|
|
2563
2563
|
// bad
|
|
@@ -2583,7 +2583,7 @@ stages:
|
|
|
2583
2583
|
});
|
|
2584
2584
|
```
|
|
2585
2585
|
|
|
2586
|
-
- [19.3](#whitespace--around-keywords) **【必须】** 在控制语句中的左括号前放置1个空格(if,while等)。在函数调用和声明中,参数列表和函数名之间不能留空格。[`keyword-spacing`](https://eslint.org/docs/rules/keyword-spacing.html)
|
|
2586
|
+
- [19.3](#whitespace--around-keywords) <a id="whitespace--around-keywords"></a> **【必须】** 在控制语句中的左括号前放置1个空格(if,while等)。在函数调用和声明中,参数列表和函数名之间不能留空格。[`keyword-spacing`](https://eslint.org/docs/rules/keyword-spacing.html)
|
|
2587
2587
|
|
|
2588
2588
|
```javascript
|
|
2589
2589
|
// bad
|
|
@@ -2607,7 +2607,7 @@ stages:
|
|
|
2607
2607
|
}
|
|
2608
2608
|
```
|
|
2609
2609
|
|
|
2610
|
-
- [19.4](#whitespace--infix-ops) **【必须】** 运算符左右设置各设置一个空格 eslint: [`space-infix-ops`](https://eslint.org/docs/rules/space-infix-ops.html)
|
|
2610
|
+
- [19.4](#whitespace--infix-ops) <a id="whitespace--infix-ops"></a> **【必须】** 运算符左右设置各设置一个空格 eslint: [`space-infix-ops`](https://eslint.org/docs/rules/space-infix-ops.html)
|
|
2611
2611
|
|
|
2612
2612
|
```javascript
|
|
2613
2613
|
// bad
|
|
@@ -2617,7 +2617,7 @@ stages:
|
|
|
2617
2617
|
const x = y + 5;
|
|
2618
2618
|
```
|
|
2619
2619
|
|
|
2620
|
-
- [19.5](#whitespace--newline-at-end) **【必须】** 在文件的结尾需要保留一个空行 eslint: [`eol-last`](https://github.com/eslint/eslint/blob/master/docs/rules/eol-last.md)
|
|
2620
|
+
- [19.5](#whitespace--newline-at-end) <a id="whitespace--newline-at-end"></a> **【必须】** 在文件的结尾需要保留一个空行 eslint: [`eol-last`](https://github.com/eslint/eslint/blob/master/docs/rules/eol-last.md)
|
|
2621
2621
|
|
|
2622
2622
|
```javascript
|
|
2623
2623
|
// bad
|
|
@@ -2641,7 +2641,7 @@ stages:
|
|
|
2641
2641
|
export default es6;↵
|
|
2642
2642
|
```
|
|
2643
2643
|
|
|
2644
|
-
- [19.6](#whitespace--chains) **【推荐】** 在编写多个方法链式调用(超过两个方法链式调用)时。 使用前导点,强调这行是一个方法调用,而不是一个语句。
|
|
2644
|
+
- [19.6](#whitespace--chains) <a id="whitespace--chains"></a> **【推荐】** 在编写多个方法链式调用(超过两个方法链式调用)时。 使用前导点,强调这行是一个方法调用,而不是一个语句。
|
|
2645
2645
|
eslint: [`newline-per-chained-call`](https://eslint.org/docs/rules/newline-per-chained-call) [`no-whitespace-before-property`](https://eslint.org/docs/rules/no-whitespace-before-property)
|
|
2646
2646
|
|
|
2647
2647
|
```javascript
|
|
@@ -2684,7 +2684,7 @@ stages:
|
|
|
2684
2684
|
const leds = stage.selectAll('.led').data(data);
|
|
2685
2685
|
```
|
|
2686
2686
|
|
|
2687
|
-
- [19.7](#whitespace--after-blocks) **【推荐】** 在块和下一个语句之前留下一空白行。
|
|
2687
|
+
- [19.7](#whitespace--after-blocks) <a id="whitespace--after-blocks"></a> **【推荐】** 在块和下一个语句之前留下一空白行。
|
|
2688
2688
|
|
|
2689
2689
|
```javascript
|
|
2690
2690
|
// bad
|
|
@@ -2741,7 +2741,7 @@ stages:
|
|
|
2741
2741
|
return arr;
|
|
2742
2742
|
```
|
|
2743
2743
|
|
|
2744
|
-
- [19.8](#whitespace--padded-blocks) **【必须】** 不要在块的开头使用空白行。 eslint: [`padded-blocks`](https://eslint.org/docs/rules/padded-blocks.html)
|
|
2744
|
+
- [19.8](#whitespace--padded-blocks) <a id="whitespace--padded-blocks"></a> **【必须】** 不要在块的开头使用空白行。 eslint: [`padded-blocks`](https://eslint.org/docs/rules/padded-blocks.html)
|
|
2745
2745
|
|
|
2746
2746
|
```javascript
|
|
2747
2747
|
// bad
|
|
@@ -2781,7 +2781,7 @@ stages:
|
|
|
2781
2781
|
}
|
|
2782
2782
|
```
|
|
2783
2783
|
|
|
2784
|
-
- [19.9](#whitespace--no-multiple-blanks) **【必须】** 不要使用多个空行填充代码。 eslint: [`no-multiple-empty-lines`](https://eslint.org/docs/rules/no-multiple-empty-lines)
|
|
2784
|
+
- [19.9](#whitespace--no-multiple-blanks) <a id="whitespace--no-multiple-blanks"></a> **【必须】** 不要使用多个空行填充代码。 eslint: [`no-multiple-empty-lines`](https://eslint.org/docs/rules/no-multiple-empty-lines)
|
|
2785
2785
|
|
|
2786
2786
|
<!-- markdownlint-disable MD012 -->
|
|
2787
2787
|
```javascript
|
|
@@ -2834,7 +2834,7 @@ stages:
|
|
|
2834
2834
|
}
|
|
2835
2835
|
```
|
|
2836
2836
|
|
|
2837
|
-
- [19.10](#whitespace--in-parens) **【必须】** 不要在括号内添加空格。 eslint: [`space-in-parens`](https://eslint.org/docs/rules/space-in-parens.html)
|
|
2837
|
+
- [19.10](#whitespace--in-parens) <a id="whitespace--in-parens"></a> **【必须】** 不要在括号内添加空格。 eslint: [`space-in-parens`](https://eslint.org/docs/rules/space-in-parens.html)
|
|
2838
2838
|
|
|
2839
2839
|
```javascript
|
|
2840
2840
|
// bad
|
|
@@ -2858,7 +2858,7 @@ stages:
|
|
|
2858
2858
|
}
|
|
2859
2859
|
```
|
|
2860
2860
|
|
|
2861
|
-
- [19.11](#whitespace--in-brackets) **【必须】** 不要在中括号中添加空格。 eslint: [`array-bracket-spacing`](https://eslint.org/docs/rules/array-bracket-spacing.html)
|
|
2861
|
+
- [19.11](#whitespace--in-brackets) <a id="whitespace--in-brackets"></a> **【必须】** 不要在中括号中添加空格。 eslint: [`array-bracket-spacing`](https://eslint.org/docs/rules/array-bracket-spacing.html)
|
|
2862
2862
|
|
|
2863
2863
|
```javascript
|
|
2864
2864
|
// bad
|
|
@@ -2870,7 +2870,7 @@ stages:
|
|
|
2870
2870
|
console.log(foo[0]);
|
|
2871
2871
|
```
|
|
2872
2872
|
|
|
2873
|
-
- [19.12](#whitespace--in-braces) **【推荐】** 在花括号内添加空格。 eslint: [`object-curly-spacing`](https://eslint.org/docs/rules/object-curly-spacing.html)
|
|
2873
|
+
- [19.12](#whitespace--in-braces) <a id="whitespace--in-braces"></a> **【推荐】** 在花括号内添加空格。 eslint: [`object-curly-spacing`](https://eslint.org/docs/rules/object-curly-spacing.html)
|
|
2874
2874
|
|
|
2875
2875
|
```javascript
|
|
2876
2876
|
// bad
|
|
@@ -2880,7 +2880,7 @@ stages:
|
|
|
2880
2880
|
const foo = { clark: 'kent' };
|
|
2881
2881
|
```
|
|
2882
2882
|
|
|
2883
|
-
- [19.13](#whitespace--max-len) **【可选】** 避免让你的代码行超过120个字符(包括空格)。 注意:根据上边的[规则](#strings--line-length)
|
|
2883
|
+
- [19.13](#whitespace--max-len) <a id="whitespace--max-len"></a> **【可选】** 避免让你的代码行超过120个字符(包括空格)。 注意:根据上边的[规则](#strings--line-length) <a id="strings--line-length"></a>,长字符串编写可不受该规则约束,不应该被分解。 eslint: [`max-len`](https://eslint.org/docs/rules/max-len.html)
|
|
2884
2884
|
|
|
2885
2885
|
> 原因? 这样能够提升代码可读性和可维护性。
|
|
2886
2886
|
|
|
@@ -2917,7 +2917,7 @@ stages:
|
|
|
2917
2917
|
.fail(() => console.log('You have failed this city.'));
|
|
2918
2918
|
```
|
|
2919
2919
|
|
|
2920
|
-
- [19.14](#whitespace--block-spacing) **【必须】** 要求打开的块标志和同一行上的标志拥有一致的间距。此规则还会在同一行关闭的块标记和前边的标记强制实施一致的间距。 eslint: [`block-spacing`](https://eslint.org/docs/rules/block-spacing)
|
|
2920
|
+
- [19.14](#whitespace--block-spacing) <a id="whitespace--block-spacing"></a> **【必须】** 要求打开的块标志和同一行上的标志拥有一致的间距。此规则还会在同一行关闭的块标记和前边的标记强制实施一致的间距。 eslint: [`block-spacing`](https://eslint.org/docs/rules/block-spacing)
|
|
2921
2921
|
|
|
2922
2922
|
```javascript
|
|
2923
2923
|
// bad
|
|
@@ -2929,7 +2929,7 @@ stages:
|
|
|
2929
2929
|
if (foo) { bar = 0; }
|
|
2930
2930
|
```
|
|
2931
2931
|
|
|
2932
|
-
- [19.15](#whitespace--comma-spacing) **【必须】** 逗号之前避免使用空格,逗号之后需要使用空格。eslint: [`comma-spacing`](https://eslint.org/docs/rules/comma-spacing)
|
|
2932
|
+
- [19.15](#whitespace--comma-spacing) <a id="whitespace--comma-spacing"></a> **【必须】** 逗号之前避免使用空格,逗号之后需要使用空格。eslint: [`comma-spacing`](https://eslint.org/docs/rules/comma-spacing)
|
|
2933
2933
|
|
|
2934
2934
|
```javascript
|
|
2935
2935
|
// bad
|
|
@@ -2939,7 +2939,7 @@ stages:
|
|
|
2939
2939
|
const arr = [1, 2];
|
|
2940
2940
|
```
|
|
2941
2941
|
|
|
2942
|
-
- [19.16](#whitespace--computed-property-spacing) **【推荐】** 不要在计算属性括号内插入空格。eslint: [`computed-property-spacing`](https://eslint.org/docs/rules/computed-property-spacing)
|
|
2942
|
+
- [19.16](#whitespace--computed-property-spacing) <a id="whitespace--computed-property-spacing"></a> **【推荐】** 不要在计算属性括号内插入空格。eslint: [`computed-property-spacing`](https://eslint.org/docs/rules/computed-property-spacing)
|
|
2943
2943
|
|
|
2944
2944
|
```javascript
|
|
2945
2945
|
// bad
|
|
@@ -2955,7 +2955,7 @@ stages:
|
|
|
2955
2955
|
obj[foo[bar]]
|
|
2956
2956
|
```
|
|
2957
2957
|
|
|
2958
|
-
- [19.17](#whitespace--func-call-spacing) **【必须】** 避免在函数名及其入参括号之间插入空格。 eslint: [`func-call-spacing`](https://eslint.org/docs/rules/func-call-spacing)
|
|
2958
|
+
- [19.17](#whitespace--func-call-spacing) <a id="whitespace--func-call-spacing"></a> **【必须】** 避免在函数名及其入参括号之间插入空格。 eslint: [`func-call-spacing`](https://eslint.org/docs/rules/func-call-spacing)
|
|
2959
2959
|
|
|
2960
2960
|
```javascript
|
|
2961
2961
|
// bad
|
|
@@ -2968,7 +2968,7 @@ stages:
|
|
|
2968
2968
|
func();
|
|
2969
2969
|
```
|
|
2970
2970
|
|
|
2971
|
-
- [19.18](#whitespace--key-spacing) **【必须】** 在对象的属性和值之间的冒号前不加空格,冒号后加空格。 eslint: [`key-spacing`](https://eslint.org/docs/rules/key-spacing)
|
|
2971
|
+
- [19.18](#whitespace--key-spacing) <a id="whitespace--key-spacing"></a> **【必须】** 在对象的属性和值之间的冒号前不加空格,冒号后加空格。 eslint: [`key-spacing`](https://eslint.org/docs/rules/key-spacing)
|
|
2972
2972
|
|
|
2973
2973
|
```javascript
|
|
2974
2974
|
// bad
|
|
@@ -2979,9 +2979,9 @@ stages:
|
|
|
2979
2979
|
var obj = { foo: 42 };
|
|
2980
2980
|
```
|
|
2981
2981
|
|
|
2982
|
-
- [19.19](#whitespace--no-trailing-spaces) **【必须】** 避免在行尾添加空格。 eslint: [`no-trailing-spaces`](https://eslint.org/docs/rules/no-trailing-spaces)
|
|
2982
|
+
- [19.19](#whitespace--no-trailing-spaces) <a id="whitespace--no-trailing-spaces"></a> **【必须】** 避免在行尾添加空格。 eslint: [`no-trailing-spaces`](https://eslint.org/docs/rules/no-trailing-spaces)
|
|
2983
2983
|
|
|
2984
|
-
- [19.20](#whitespace--no-multiple-empty-lines) **【必须】** 在代码开始处不允许存在空行,行间避免出现多个空行,而结尾处必须保留一个空行。 eslint: [`no-multiple-empty-lines`](https://eslint.org/docs/rules/no-multiple-empty-lines)
|
|
2984
|
+
- [19.20](#whitespace--no-multiple-empty-lines) <a id="whitespace--no-multiple-empty-lines"></a> **【必须】** 在代码开始处不允许存在空行,行间避免出现多个空行,而结尾处必须保留一个空行。 eslint: [`no-multiple-empty-lines`](https://eslint.org/docs/rules/no-multiple-empty-lines)
|
|
2985
2985
|
|
|
2986
2986
|
```javascript
|
|
2987
2987
|
// bad
|
|
@@ -2997,11 +2997,11 @@ stages:
|
|
|
2997
2997
|
const y = 2;
|
|
2998
2998
|
```
|
|
2999
2999
|
|
|
3000
|
-
- [19.21](#linebreak-style) **【推荐】** 推荐使用 Unix 的 LF 作为换行符,而不是 Windows 的 CRLF,这样可以统一文件的换行符,避免因为换行符导致的格式混乱。
|
|
3000
|
+
- [19.21](#linebreak-style) <a id="linebreak-style"></a> **【推荐】** 推荐使用 Unix 的 LF 作为换行符,而不是 Windows 的 CRLF,这样可以统一文件的换行符,避免因为换行符导致的格式混乱。
|
|
3001
3001
|
|
|
3002
3002
|
## 20. 逗号
|
|
3003
3003
|
|
|
3004
|
-
- [20.1](#commas--leading-trailing) **【必须】** 逗号**不能**前置 eslint: [`comma-style`](https://eslint.org/docs/rules/comma-style.html)
|
|
3004
|
+
- [20.1](#commas--leading-trailing) <a id="commas--leading-trailing"></a> **【必须】** 逗号**不能**前置 eslint: [`comma-style`](https://eslint.org/docs/rules/comma-style.html)
|
|
3005
3005
|
|
|
3006
3006
|
```javascript
|
|
3007
3007
|
// bad
|
|
@@ -3035,7 +3035,7 @@ stages:
|
|
|
3035
3035
|
};
|
|
3036
3036
|
```
|
|
3037
3037
|
|
|
3038
|
-
- [20.2](#commas--dangling) **【推荐】** 添加尾随逗号: **可以** eslint: [`comma-dangle`](https://eslint.org/docs/rules/comma-dangle.html)
|
|
3038
|
+
- [20.2](#commas--dangling) <a id="commas--dangling"></a> **【推荐】** 添加尾随逗号: **可以** eslint: [`comma-dangle`](https://eslint.org/docs/rules/comma-dangle.html)
|
|
3039
3039
|
|
|
3040
3040
|
> 原因? 在 git diff 时能够更加清晰地查看改动。 另外,像[Babel](https://babeljs.io/)这样的编译器,会在转译时删除代码中的尾逗号,这意味着你不必担心旧版浏览器中的[尾随逗号问题](https://github.com/airbnb/javascript/blob/es5-deprecated/es5/README.md#commas) 。
|
|
3041
3041
|
|
|
@@ -3132,7 +3132,7 @@ stages:
|
|
|
3132
3132
|
|
|
3133
3133
|
## 21. jQuery
|
|
3134
3134
|
|
|
3135
|
-
- [21.1](#jquery--dollar-prefix) **【推荐】** 对于 jQuery 对象一律使用 `$` 符作为前缀。
|
|
3135
|
+
- [21.1](#jquery--dollar-prefix) <a id="jquery--dollar-prefix"></a> **【推荐】** 对于 jQuery 对象一律使用 `$` 符作为前缀。
|
|
3136
3136
|
|
|
3137
3137
|
```javascript
|
|
3138
3138
|
// bad
|
|
@@ -3145,7 +3145,7 @@ stages:
|
|
|
3145
3145
|
const $sidebarBtn = $('.sidebar-btn');
|
|
3146
3146
|
```
|
|
3147
3147
|
|
|
3148
|
-
- [21.2](#jquery--cache) **【推荐】** 缓存 jQuery 查询,节省 DOM 查询开销。
|
|
3148
|
+
- [21.2](#jquery--cache) <a id="jquery--cache"></a> **【推荐】** 缓存 jQuery 查询,节省 DOM 查询开销。
|
|
3149
3149
|
|
|
3150
3150
|
```javascript
|
|
3151
3151
|
// bad
|
|
@@ -3172,7 +3172,7 @@ stages:
|
|
|
3172
3172
|
}
|
|
3173
3173
|
```
|
|
3174
3174
|
|
|
3175
|
-
- [21.3](#jquery--find) **【推荐】** 能通过一次调用查找到的,不要分多次;在已有对象内查询,使用 `find` 函数,减少重复查询。
|
|
3175
|
+
- [21.3](#jquery--find) <a id="jquery--find"></a> **【推荐】** 能通过一次调用查找到的,不要分多次;在已有对象内查询,使用 `find` 函数,减少重复查询。
|
|
3176
3176
|
|
|
3177
3177
|
```javascript
|
|
3178
3178
|
// bad
|
|
@@ -3193,7 +3193,7 @@ stages:
|
|
|
3193
3193
|
|
|
3194
3194
|
## 22. 类型转换和强制类型转换
|
|
3195
3195
|
|
|
3196
|
-
- [22.1](#coercion--strings) **【推荐】** 使用 `String()` 函数将变量转成字符串,比较保险: eslint: [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
|
|
3196
|
+
- [22.1](#coercion--strings) <a id="coercion--strings"></a> **【推荐】** 使用 `String()` 函数将变量转成字符串,比较保险: eslint: [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
|
|
3197
3197
|
|
|
3198
3198
|
```javascript
|
|
3199
3199
|
// => this.reviewScore = 9;
|
|
@@ -3211,7 +3211,7 @@ stages:
|
|
|
3211
3211
|
const totalScore = String(this.reviewScore);
|
|
3212
3212
|
```
|
|
3213
3213
|
|
|
3214
|
-
- [22.2](#coercion--numbers) **【推荐】** 数字类型转换推荐用 `Number()` 或者 `parseInt()` 函数,其中 `parseInt()` 需显式标明底数。 eslint: [`radix`](https://eslint.org/docs/rules/radix) [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
|
|
3214
|
+
- [22.2](#coercion--numbers) <a id="coercion--numbers"></a> **【推荐】** 数字类型转换推荐用 `Number()` 或者 `parseInt()` 函数,其中 `parseInt()` 需显式标明底数。 eslint: [`radix`](https://eslint.org/docs/rules/radix) [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
|
|
3215
3215
|
|
|
3216
3216
|
```javascript
|
|
3217
3217
|
const inputValue = '4';
|
|
@@ -3235,7 +3235,7 @@ stages:
|
|
|
3235
3235
|
const val = parseInt(inputValue, 10);
|
|
3236
3236
|
```
|
|
3237
3237
|
|
|
3238
|
-
- [22.3](#coercion--comment-deviations) **【可选】** 如果你对性能有极高的要求,觉得 [`parseInt` 性能太低](https://jsperf.com/coercion-vs-casting/3) ,可以使用位运算,但请用注释说明代码含义,否则很难看懂。
|
|
3238
|
+
- [22.3](#coercion--comment-deviations) <a id="coercion--comment-deviations"></a> **【可选】** 如果你对性能有极高的要求,觉得 [`parseInt` 性能太低](https://jsperf.com/coercion-vs-casting/3) ,可以使用位运算,但请用注释说明代码含义,否则很难看懂。
|
|
3239
3239
|
|
|
3240
3240
|
```javascript
|
|
3241
3241
|
// good
|
|
@@ -3246,7 +3246,7 @@ stages:
|
|
|
3246
3246
|
const val = inputValue >> 0;
|
|
3247
3247
|
```
|
|
3248
3248
|
|
|
3249
|
-
- [22.4](#coercion--bitwise) **注意:** **【可选】** 谨慎使用位运算符。 在js里,[数字的最大值是64位](https://es5.github.io/#x4.3.19) ,但位运算只能返回32位的整数 ([来源](https://es5.github.io/#x11.7))。 对于大于 32 位的数值,位运算无法得到预期结果。[讨论](https://github.com/airbnb/javascript/issues/109)。 最大的 32 位整数是: 2,147,483,647。
|
|
3249
|
+
- [22.4](#coercion--bitwise) <a id="coercion--bitwise"></a> **注意:** **【可选】** 谨慎使用位运算符。 在js里,[数字的最大值是64位](https://es5.github.io/#x4.3.19) ,但位运算只能返回32位的整数 ([来源](https://es5.github.io/#x11.7))。 对于大于 32 位的数值,位运算无法得到预期结果。[讨论](https://github.com/airbnb/javascript/issues/109)。 最大的 32 位整数是: 2,147,483,647。
|
|
3250
3250
|
|
|
3251
3251
|
```javascript
|
|
3252
3252
|
2147483647 >> 0; // => 2147483647
|
|
@@ -3254,7 +3254,7 @@ stages:
|
|
|
3254
3254
|
2147483649 >> 0; // => -2147483647
|
|
3255
3255
|
```
|
|
3256
3256
|
|
|
3257
|
-
- [22.5](#coercion--booleans) **【推荐】** 用两个叹号来转换布尔类型: eslint: [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
|
|
3257
|
+
- [22.5](#coercion--booleans) <a id="coercion--booleans"></a> **【推荐】** 用两个叹号来转换布尔类型: eslint: [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
|
|
3258
3258
|
|
|
3259
3259
|
```javascript
|
|
3260
3260
|
const age = 0;
|
|
@@ -3271,7 +3271,7 @@ stages:
|
|
|
3271
3271
|
|
|
3272
3272
|
## 23. 命名规范
|
|
3273
3273
|
|
|
3274
|
-
- [23.1](#naming--descriptive) **【可选】** 避免用单个字母来命名函数或变量。 尽量让名字具有可读性。
|
|
3274
|
+
- [23.1](#naming--descriptive) <a id="naming--descriptive"></a> **【可选】** 避免用单个字母来命名函数或变量。 尽量让名字具有可读性。
|
|
3275
3275
|
> 补充说明:由于eslint的 [`id-length`](https://eslint.org/docs/rules/id-length) 规则会把for(let i=0; i < len; i++)这种情况也报错,这不太符合我们的习惯,因此在配置中暂时去掉id-length检查。
|
|
3276
3276
|
|
|
3277
3277
|
```javascript
|
|
@@ -3286,7 +3286,7 @@ stages:
|
|
|
3286
3286
|
}
|
|
3287
3287
|
```
|
|
3288
3288
|
|
|
3289
|
-
- [23.2](#naming--camelCase) **【必须】** 使用驼峰命名法(camelCase)命名对象、函数和实例。 eslint: [`camelcase`](https://eslint.org/docs/rules/camelcase.html)
|
|
3289
|
+
- [23.2](#naming--camelCase) <a id="naming--camelCase"></a> **【必须】** 使用驼峰命名法(camelCase)命名对象、函数和实例。 eslint: [`camelcase`](https://eslint.org/docs/rules/camelcase.html)
|
|
3290
3290
|
|
|
3291
3291
|
```javascript
|
|
3292
3292
|
// bad
|
|
@@ -3299,7 +3299,7 @@ stages:
|
|
|
3299
3299
|
function thisIsMyFunction() {}
|
|
3300
3300
|
```
|
|
3301
3301
|
|
|
3302
|
-
- [23.3](#naming--PascalCase) **【必须】** 只有在命名构造器或者类的时候,才用帕斯卡命名法(PascalCase),即首字母大写。 eslint: [`new-cap`](https://eslint.org/docs/rules/new-cap.html)
|
|
3302
|
+
- [23.3](#naming--PascalCase) <a id="naming--PascalCase"></a> **【必须】** 只有在命名构造器或者类的时候,才用帕斯卡命名法(PascalCase),即首字母大写。 eslint: [`new-cap`](https://eslint.org/docs/rules/new-cap.html)
|
|
3303
3303
|
|
|
3304
3304
|
```javascript
|
|
3305
3305
|
// bad
|
|
@@ -3323,7 +3323,7 @@ stages:
|
|
|
3323
3323
|
});
|
|
3324
3324
|
```
|
|
3325
3325
|
|
|
3326
|
-
- [23.4](#naming--leading-underscore) **【推荐】** 变量命名时不要使用前置或者后置下划线。 eslint: [`no-underscore-dangle`](https://eslint.org/docs/rules/no-underscore-dangle.html)
|
|
3326
|
+
- [23.4](#naming--leading-underscore) <a id="naming--leading-underscore"></a> **【推荐】** 变量命名时不要使用前置或者后置下划线。 eslint: [`no-underscore-dangle`](https://eslint.org/docs/rules/no-underscore-dangle.html)
|
|
3327
3327
|
|
|
3328
3328
|
> 因为在 Javascript 里属性和方法没有私有成员一说。 虽然前置下划线通常表示这是私有成员,但实际上还是公开的。 你无法阻止外部调用这类方法。 并且,开发人员容易误以为修改这类函数不需要知会调用方或者不需要测试。 简而言之,如果你想定义私有成员,必须使其不可见。
|
|
3329
3329
|
|
|
@@ -3341,7 +3341,7 @@ stages:
|
|
|
3341
3341
|
firstNames.set(this, 'Panda');
|
|
3342
3342
|
```
|
|
3343
3343
|
|
|
3344
|
-
- [23.5](#naming--self-this) **【推荐】** 不要保存 `this` 的引用,请使用箭头函数或者 [函数#bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)。
|
|
3344
|
+
- [23.5](#naming--self-this) <a id="naming--self-this"></a> **【推荐】** 不要保存 `this` 的引用,请使用箭头函数或者 [函数#bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)。
|
|
3345
3345
|
|
|
3346
3346
|
```javascript
|
|
3347
3347
|
// bad
|
|
@@ -3368,7 +3368,7 @@ stages:
|
|
|
3368
3368
|
}
|
|
3369
3369
|
```
|
|
3370
3370
|
|
|
3371
|
-
- [23.6](#naming--filename-matches-export) **【可选】** 文件名应该和默认导出的名称保持一致(文件名建议使用 kebab-case,后缀为小写)。
|
|
3371
|
+
- [23.6](#naming--filename-matches-export) <a id="naming--filename-matches-export"></a> **【可选】** 文件名应该和默认导出的名称保持一致(文件名建议使用 kebab-case,后缀为小写)。
|
|
3372
3372
|
|
|
3373
3373
|
```javascript
|
|
3374
3374
|
// file 1 contents
|
|
@@ -3403,7 +3403,7 @@ stages:
|
|
|
3403
3403
|
// ^ supports both inside-directory.js and inside-directory/index.js
|
|
3404
3404
|
```
|
|
3405
3405
|
|
|
3406
|
-
- [23.7](#naming--camelCase-default-export) **【必须】** 导出默认函数时使用驼峰命名法,并且文件名应该和方法名相同。文件名建议使用 kebab-case,后缀为小写。
|
|
3406
|
+
- [23.7](#naming--camelCase-default-export) <a id="naming--camelCase-default-export"></a> **【必须】** 导出默认函数时使用驼峰命名法,并且文件名应该和方法名相同。文件名建议使用 kebab-case,后缀为小写。
|
|
3407
3407
|
|
|
3408
3408
|
```javascript
|
|
3409
3409
|
function makeStyleGuide() {
|
|
@@ -3413,7 +3413,7 @@ stages:
|
|
|
3413
3413
|
export default makeStyleGuide;
|
|
3414
3414
|
```
|
|
3415
3415
|
|
|
3416
|
-
- [23.8](#naming--PascalCase-singleton) **【必须】** 当导出构造器 / 类 / 单例 / 函数库 / 对象时应该使用帕斯卡命名法(首字母大写)。
|
|
3416
|
+
- [23.8](#naming--PascalCase-singleton) <a id="naming--PascalCase-singleton"></a> **【必须】** 当导出构造器 / 类 / 单例 / 函数库 / 对象时应该使用帕斯卡命名法(首字母大写)。
|
|
3417
3417
|
|
|
3418
3418
|
```javascript
|
|
3419
3419
|
const TencentStyleGuide = {
|
|
@@ -3424,7 +3424,7 @@ stages:
|
|
|
3424
3424
|
export default TencentStyleGuide;
|
|
3425
3425
|
```
|
|
3426
3426
|
|
|
3427
|
-
- [23.9](#naming--Acronyms-and-Initialisms) **【推荐】** 缩略词和缩写都必须是全部大写或者全部小写,可读性更好。
|
|
3427
|
+
- [23.9](#naming--Acronyms-and-Initialisms) <a id="naming--Acronyms-and-Initialisms"></a> **【推荐】** 缩略词和缩写都必须是全部大写或者全部小写,可读性更好。
|
|
3428
3428
|
|
|
3429
3429
|
```javascript
|
|
3430
3430
|
// bad
|
|
@@ -3457,7 +3457,7 @@ stages:
|
|
|
3457
3457
|
];
|
|
3458
3458
|
```
|
|
3459
3459
|
|
|
3460
|
-
- [23.10](#naming--uppercase) **【可选】** 对于export的常量,可以用全大写命名,但模块内部的常量名不需要全大写(用驼峰试命名可读性更好)。
|
|
3460
|
+
- [23.10](#naming--uppercase) <a id="naming--uppercase"></a> **【可选】** 对于export的常量,可以用全大写命名,但模块内部的常量名不需要全大写(用驼峰试命名可读性更好)。
|
|
3461
3461
|
|
|
3462
3462
|
> UPPERCASE_VARIABLES 全大写变量可以让开发者知道这是个常量。 但注意在常量对象内的属性名不需要全大写(如 `EXPORTED_OBJECT.key`)。
|
|
3463
3463
|
|
|
@@ -3496,7 +3496,7 @@ stages:
|
|
|
3496
3496
|
|
|
3497
3497
|
- [24.1] **【可选】** 如果没有特殊需要,类属性存取器其实是没有必要的。
|
|
3498
3498
|
|
|
3499
|
-
- [24.2](#accessors--no-getters-setters) **【可选】** 不要使用 JavaScript 的 getters/setters 方法,因为它们会导致意外的副作用,并且更加难以测试、维护和推敲。 相应的,如果你需要处理存取过程的时候可以使用函数 `getVal()` 和 `setVal('hello')` 实现。
|
|
3499
|
+
- [24.2](#accessors--no-getters-setters) <a id="accessors--no-getters-setters"></a> **【可选】** 不要使用 JavaScript 的 getters/setters 方法,因为它们会导致意外的副作用,并且更加难以测试、维护和推敲。 相应的,如果你需要处理存取过程的时候可以使用函数 `getVal()` 和 `setVal('hello')` 实现。
|
|
3500
3500
|
|
|
3501
3501
|
```javascript
|
|
3502
3502
|
// bad
|
|
@@ -3522,7 +3522,7 @@ stages:
|
|
|
3522
3522
|
}
|
|
3523
3523
|
```
|
|
3524
3524
|
|
|
3525
|
-
- [24.3](#accessors--boolean-prefix) **【推荐】** 如果属性/方法是一个 `boolean` 值,使用 `isVal()` 或者 `hasVal()`。
|
|
3525
|
+
- [24.3](#accessors--boolean-prefix) <a id="accessors--boolean-prefix"></a> **【推荐】** 如果属性/方法是一个 `boolean` 值,使用 `isVal()` 或者 `hasVal()`。
|
|
3526
3526
|
|
|
3527
3527
|
```javascript
|
|
3528
3528
|
// bad
|
|
@@ -3536,7 +3536,7 @@ stages:
|
|
|
3536
3536
|
}
|
|
3537
3537
|
```
|
|
3538
3538
|
|
|
3539
|
-
- [24.4](#accessors--consistent) **【可选】** 可以创建 `get()` 和 `set()` 方法,但是要保证一致性。
|
|
3539
|
+
- [24.4](#accessors--consistent) <a id="accessors--consistent"></a> **【可选】** 可以创建 `get()` 和 `set()` 方法,但是要保证一致性。
|
|
3540
3540
|
|
|
3541
3541
|
```javascript
|
|
3542
3542
|
class Jedi {
|
|
@@ -3557,7 +3557,7 @@ stages:
|
|
|
3557
3557
|
|
|
3558
3558
|
## 25. 事件
|
|
3559
3559
|
|
|
3560
|
-
- [25.1](#events--hash) **【推荐】** 无论原生 DOM 事件还是像 Backbone 事件这样的自定义事件,在给事件传递参数时,参数应该使用对象而不是单个值。这样未来如果参数有所变化,就不用修改事件回调函数本身。
|
|
3560
|
+
- [25.1](#events--hash) <a id="events--hash"></a> **【推荐】** 无论原生 DOM 事件还是像 Backbone 事件这样的自定义事件,在给事件传递参数时,参数应该使用对象而不是单个值。这样未来如果参数有所变化,就不用修改事件回调函数本身。
|
|
3561
3561
|
|
|
3562
3562
|
```javascript
|
|
3563
3563
|
// bad
|
|
@@ -3583,7 +3583,7 @@ stages:
|
|
|
3583
3583
|
|
|
3584
3584
|
## 26. 分号
|
|
3585
3585
|
|
|
3586
|
-
- [26.1](#semicolons--required) **【必须】** **要加分号** eslint: [`semi`](https://eslint.org/docs/rules/semi.html)
|
|
3586
|
+
- [26.1](#semicolons--required) <a id="semicolons--required"></a> **【必须】** **要加分号** eslint: [`semi`](https://eslint.org/docs/rules/semi.html)
|
|
3587
3587
|
|
|
3588
3588
|
> 原因? 当 JavaScript 解析器解析到没有分号的单行代码时,它会使用一个叫做 [自动分号插入算法(Automatic Semicolon Insertion)](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion) 来确定是否应该以换行符视为语句的结束,如果判断为语句结束,会在代码中断前插入一个分号到代码中。 但是,ASI 包含了一些奇怪的行为,如果 JavaScript 错误的解释了你的换行符,你的代码将会中断。 随着越来越多的新特性成为 JavaScript 的一部分,这些规则将变得更加复杂。明确地终止你的语句,并配置你的 linter 以捕获缺少分号的代码行,将有助于预防此类问题。
|
|
3589
3589
|
|
|
@@ -3632,7 +3632,7 @@ stages:
|
|
|
3632
3632
|
|
|
3633
3633
|
## 1. 类
|
|
3634
3634
|
|
|
3635
|
-
[1.1](#explicit-member-accessibility) **【可选】** 必须设置类的成员的可访问性。 eslint: [`@typescript-eslint/explicit-member-accessibility`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md)
|
|
3635
|
+
[1.1](#explicit-member-accessibility) <a id="explicit-member-accessibility"></a> **【可选】** 必须设置类的成员的可访问性。 eslint: [`@typescript-eslint/explicit-member-accessibility`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md)
|
|
3636
3636
|
>原因?
|
|
3637
3637
|
>1、将不需要公开的成员设为私有的,可以增强代码的可理解性,对文档输出也很友好
|
|
3638
3638
|
>2、将暴露出去的设置为public的,类和外界的联系一目了然
|
|
@@ -3674,7 +3674,7 @@ class Foo2 {
|
|
|
3674
3674
|
}
|
|
3675
3675
|
```
|
|
3676
3676
|
|
|
3677
|
-
[1.2](#member-ordering) **【必须】** 指定类成员的排序规则。 eslint: [`@typescript-eslint/member-ordering`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-ordering.md)
|
|
3677
|
+
[1.2](#member-ordering) <a id="member-ordering"></a> **【必须】** 指定类成员的排序规则。 eslint: [`@typescript-eslint/member-ordering`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-ordering.md)
|
|
3678
3678
|
|
|
3679
3679
|
>优先级:
|
|
3680
3680
|
|
|
@@ -3733,7 +3733,7 @@ class Foo2 {
|
|
|
3733
3733
|
|
|
3734
3734
|
```
|
|
3735
3735
|
|
|
3736
|
-
[1.3](#no-parameter-properties) **【可选】** 禁止给类的构造函数的参数添加修饰符。 eslint: [`@typescript-eslint/no-parameter-properties`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-parameter-properties.md)
|
|
3736
|
+
[1.3](#no-parameter-properties) <a id="no-parameter-properties"></a> **【可选】** 禁止给类的构造函数的参数添加修饰符。 eslint: [`@typescript-eslint/no-parameter-properties`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-parameter-properties.md)
|
|
3737
3737
|
>原因?强制所有属性都定义到类里面,比较统一
|
|
3738
3738
|
|
|
3739
3739
|
```typescript
|
|
@@ -3750,7 +3750,7 @@ class Foo2 {
|
|
|
3750
3750
|
|
|
3751
3751
|
## 2. 函数
|
|
3752
3752
|
|
|
3753
|
-
[2.1](#adjacent-overload-signatures) **【必须】** 重载的函数必须写在一起,增强可读性。 eslint: [`@typescript-eslint/adjacent-overload-signatures`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/adjacent-overload-signatures.md)
|
|
3753
|
+
[2.1](#adjacent-overload-signatures) <a id="adjacent-overload-signatures"></a> **【必须】** 重载的函数必须写在一起,增强可读性。 eslint: [`@typescript-eslint/adjacent-overload-signatures`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/adjacent-overload-signatures.md)
|
|
3754
3754
|
|
|
3755
3755
|
```typescript
|
|
3756
3756
|
// bad
|
|
@@ -3798,7 +3798,7 @@ interface IFoo2 {
|
|
|
3798
3798
|
}
|
|
3799
3799
|
```
|
|
3800
3800
|
|
|
3801
|
-
[2.2](#unified-signatures) **【必须】** 函数重载时,若能通过联合类型将两个函数的类型声明合为一个,则使用联合类型而不是两个函数声明。 eslint: [`@typescript-eslint/unified-signatures`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/unified-signatures.md)
|
|
3801
|
+
[2.2](#unified-signatures) <a id="unified-signatures"></a> **【必须】** 函数重载时,若能通过联合类型将两个函数的类型声明合为一个,则使用联合类型而不是两个函数声明。 eslint: [`@typescript-eslint/unified-signatures`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/unified-signatures.md)
|
|
3802
3802
|
|
|
3803
3803
|
```typescript
|
|
3804
3804
|
// bad
|
|
@@ -3816,7 +3816,7 @@ function foo2(x: number | string): boolean {
|
|
|
3816
3816
|
|
|
3817
3817
|
## 3. 类型
|
|
3818
3818
|
|
|
3819
|
-
[3.1](#consistent-type-assertions) **【必须】** 类型断言必须使用 as Type,禁止使用 \<Type>,禁止对对象字面量进行类型断言(断言成 any 是允许的)。 eslint: [`@typescript-eslint/consistent-type-assertions`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md)
|
|
3819
|
+
[3.1](#consistent-type-assertions) <a id="consistent-type-assertions"></a> **【必须】** 类型断言必须使用 as Type,禁止使用 \<Type>,禁止对对象字面量进行类型断言(断言成 any 是允许的)。 eslint: [`@typescript-eslint/consistent-type-assertions`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md)
|
|
3820
3820
|
>原因?避免Type被理解为 jsx
|
|
3821
3821
|
|
|
3822
3822
|
```typescript
|
|
@@ -3839,7 +3839,7 @@ const baz2 = {
|
|
|
3839
3839
|
} as any;
|
|
3840
3840
|
```
|
|
3841
3841
|
|
|
3842
|
-
[3.2](#no-inferrable-types) **【推荐】** 禁止给一个初始化时直接赋值为 number, string 的变量显式的声明类型, 可以简化代码。 eslint: [`@typescript-eslint/no-inferrable-types`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-inferrable-types.md)
|
|
3842
|
+
[3.2](#no-inferrable-types) <a id="no-inferrable-types"></a> **【推荐】** 禁止给一个初始化时直接赋值为 number, string 的变量显式的声明类型, 可以简化代码。 eslint: [`@typescript-eslint/no-inferrable-types`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-inferrable-types.md)
|
|
3843
3843
|
|
|
3844
3844
|
```typescript
|
|
3845
3845
|
// bad
|
|
@@ -3851,7 +3851,7 @@ const foo2 = 1;
|
|
|
3851
3851
|
const bar2 = '';
|
|
3852
3852
|
```
|
|
3853
3853
|
|
|
3854
|
-
[3.3](#typedef) **【必须】** interface 和 type 定义时必须声明成员的类型。 eslint: [`@typescript-eslint/typedef`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/typedef.md)
|
|
3854
|
+
[3.3](#typedef) <a id="typedef"></a> **【必须】** interface 和 type 定义时必须声明成员的类型。 eslint: [`@typescript-eslint/typedef`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/typedef.md)
|
|
3855
3855
|
|
|
3856
3856
|
```typescript
|
|
3857
3857
|
// bad
|
|
@@ -3867,7 +3867,7 @@ type Foo2 = {
|
|
|
3867
3867
|
};
|
|
3868
3868
|
```
|
|
3869
3869
|
|
|
3870
|
-
[3.4](#prefer-function-type) **【推荐】** 使用函数类型别名替代包含函数调用声明的接口。 eslint: [`@typescript-eslint/prefer-function-type`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-function-type.md)
|
|
3870
|
+
[3.4](#prefer-function-type) <a id="prefer-function-type"></a> **【推荐】** 使用函数类型别名替代包含函数调用声明的接口。 eslint: [`@typescript-eslint/prefer-function-type`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-function-type.md)
|
|
3871
3871
|
|
|
3872
3872
|
```typescript
|
|
3873
3873
|
// bad
|
|
@@ -3881,7 +3881,7 @@ type Foo2 = () => string;
|
|
|
3881
3881
|
|
|
3882
3882
|
## 4. 接口
|
|
3883
3883
|
|
|
3884
|
-
[4.1](#consistent-type-definitions) **【可选】** 优先使用 interface 而不是 type。 eslint: [`@typescript-eslint/consistent-type-definitions`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-definitions.md)
|
|
3884
|
+
[4.1](#consistent-type-definitions) <a id="consistent-type-definitions"></a> **【可选】** 优先使用 interface 而不是 type。 eslint: [`@typescript-eslint/consistent-type-definitions`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-definitions.md)
|
|
3885
3885
|
>需要被implement, extend 或者merge的建议定义为interface
|
|
3886
3886
|
|
|
3887
3887
|
```typescript
|
|
@@ -3897,7 +3897,7 @@ interface Foo2 {
|
|
|
3897
3897
|
|
|
3898
3898
|
```
|
|
3899
3899
|
|
|
3900
|
-
[4.2](#method-signature-style) **【可选】** 接口中的方法必须用属性的方式定义。 eslint: [`@typescript-eslint/method-signature-style`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/method-signature-style.md)
|
|
3900
|
+
[4.2](#method-signature-style) <a id="method-signature-style"></a> **【可选】** 接口中的方法必须用属性的方式定义。 eslint: [`@typescript-eslint/method-signature-style`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/method-signature-style.md)
|
|
3901
3901
|
>原因?配置了 strictFunctionTypes 之后,用属性的方式定义方法可以获得更严格的检查
|
|
3902
3902
|
|
|
3903
3903
|
```typescript
|
|
@@ -3912,7 +3912,7 @@ interface Foo1 {
|
|
|
3912
3912
|
}
|
|
3913
3913
|
```
|
|
3914
3914
|
|
|
3915
|
-
[4.3](#no-empty-interface) **【必须】** 禁止定义空的接口。 eslint: [`@typescript-eslint/no-empty-interface`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-empty-interface.md)
|
|
3915
|
+
[4.3](#no-empty-interface) <a id="no-empty-interface"></a> **【必须】** 禁止定义空的接口。 eslint: [`@typescript-eslint/no-empty-interface`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-empty-interface.md)
|
|
3916
3916
|
|
|
3917
3917
|
```typescript
|
|
3918
3918
|
// bad
|
|
@@ -3926,7 +3926,7 @@ interface Foo2 {
|
|
|
3926
3926
|
|
|
3927
3927
|
## 5. 命名空间
|
|
3928
3928
|
|
|
3929
|
-
[5.1](#no-namespace) **【必须】** 禁止使用 namespace 来定义命名空间。 eslint: [`@typescript-eslint/no-namespace`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-namespace.md)
|
|
3929
|
+
[5.1](#no-namespace) <a id="no-namespace"></a> **【必须】** 禁止使用 namespace 来定义命名空间。 eslint: [`@typescript-eslint/no-namespace`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-namespace.md)
|
|
3930
3930
|
>原因?使用 es6 引入模块,才是更标准的方式。
|
|
3931
3931
|
但是允许使用 declare namespace ... {} 来定义外部命名空间
|
|
3932
3932
|
|
|
@@ -3938,7 +3938,7 @@ namespace foo1 {}
|
|
|
3938
3938
|
declare namespace foo1 {}
|
|
3939
3939
|
```
|
|
3940
3940
|
|
|
3941
|
-
[5.2](#prefer-namespace-keyword) **【必须】** 禁止使用 module 来定义命名空间。 eslint: [`@typescript-eslint/prefer-namespace-keyword`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-namespace-keyword.md)
|
|
3941
|
+
[5.2](#prefer-namespace-keyword) <a id="prefer-namespace-keyword"></a> **【必须】** 禁止使用 module 来定义命名空间。 eslint: [`@typescript-eslint/prefer-namespace-keyword`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-namespace-keyword.md)
|
|
3942
3942
|
>原因?module 已成为 js 的关键字
|
|
3943
3943
|
|
|
3944
3944
|
```typescript
|
|
@@ -3951,7 +3951,7 @@ namespace Foo2 {}
|
|
|
3951
3951
|
|
|
3952
3952
|
## 6. 语法
|
|
3953
3953
|
|
|
3954
|
-
[6.1](#no-non-null-asserted-optional-chain) **【必须】** 禁止在 optional chaining 之后使用 non-null 断言(感叹号)。 eslint: [`@typescript-eslint/no-non-null-asserted-optional-chain`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-non-null-asserted-optional-chain.md)
|
|
3954
|
+
[6.1](#no-non-null-asserted-optional-chain) <a id="no-non-null-asserted-optional-chain"></a> **【必须】** 禁止在 optional chaining 之后使用 non-null 断言(感叹号)。 eslint: [`@typescript-eslint/no-non-null-asserted-optional-chain`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-non-null-asserted-optional-chain.md)
|
|
3955
3955
|
>原因?optional chaining 后面的属性一定是非空的
|
|
3956
3956
|
|
|
3957
3957
|
```typescript
|
|
@@ -3964,7 +3964,7 @@ let foo2: { bar: string } | undefined;
|
|
|
3964
3964
|
console.log(foo2?.bar);
|
|
3965
3965
|
```
|
|
3966
3966
|
|
|
3967
|
-
[6.2](#no-this-alias) **【必须】** 禁止将 this 赋值给其他变量,除非是解构赋值。 eslint: [`@typescript-eslint/no-this-alias`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-this-alias.md)
|
|
3967
|
+
[6.2](#no-this-alias) <a id="no-this-alias"></a> **【必须】** 禁止将 this 赋值给其他变量,除非是解构赋值。 eslint: [`@typescript-eslint/no-this-alias`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-this-alias.md)
|
|
3968
3968
|
|
|
3969
3969
|
```typescript
|
|
3970
3970
|
// bad
|
|
@@ -3984,7 +3984,7 @@ console.log(foo2?.bar);
|
|
|
3984
3984
|
}
|
|
3985
3985
|
```
|
|
3986
3986
|
|
|
3987
|
-
[6.3](#no-unused-expressions) **【必须】** 禁止无用的表达式。 eslint: [`@typescript-eslint/no-unused-expressions`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-expressions.md)
|
|
3987
|
+
[6.3](#no-unused-expressions) <a id="no-unused-expressions"></a> **【必须】** 禁止无用的表达式。 eslint: [`@typescript-eslint/no-unused-expressions`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-expressions.md)
|
|
3988
3988
|
|
|
3989
3989
|
```typescript
|
|
3990
3990
|
// bad
|
|
@@ -4011,7 +4011,7 @@ foo2`bar2`;
|
|
|
4011
4011
|
|
|
4012
4012
|
```
|
|
4013
4013
|
|
|
4014
|
-
[6.4](#prefer-optional-chain) **【必须】** 使用 optional chaining 替代 &&。 eslint: [`@typescript-eslint/prefer-optional-chain`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-optional-chain.md)
|
|
4014
|
+
[6.4](#prefer-optional-chain) <a id="prefer-optional-chain"></a> **【必须】** 使用 optional chaining 替代 &&。 eslint: [`@typescript-eslint/prefer-optional-chain`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-optional-chain.md)
|
|
4015
4015
|
>原因?简化代码
|
|
4016
4016
|
|
|
4017
4017
|
```typescript
|
|
@@ -4026,7 +4026,7 @@ console.log(foo2?.a?.b?.c);
|
|
|
4026
4026
|
|
|
4027
4027
|
## 7. 导入
|
|
4028
4028
|
|
|
4029
|
-
[7.1](#triple-slash-reference) **【必须】** 禁止使用三斜杠导入文件。 eslint: [`@typescript-eslint/triple-slash-reference`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/triple-slash-reference.md)
|
|
4029
|
+
[7.1](#triple-slash-reference) <a id="triple-slash-reference"></a> **【必须】** 禁止使用三斜杠导入文件。 eslint: [`@typescript-eslint/triple-slash-reference`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/triple-slash-reference.md)
|
|
4030
4030
|
|
|
4031
4031
|
```typescript
|
|
4032
4032
|
// bad
|
|
@@ -4036,7 +4036,7 @@ console.log(foo2?.a?.b?.c);
|
|
|
4036
4036
|
import Animal from './Animal';
|
|
4037
4037
|
```
|
|
4038
4038
|
|
|
4039
|
-
[7.2](#no-require-imports) **【必须】** 禁止使用 require。 eslint: [`@typescript-eslint/no-require-imports`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-require-imports.md)
|
|
4039
|
+
[7.2](#no-require-imports) <a id="no-require-imports"></a> **【必须】** 禁止使用 require。 eslint: [`@typescript-eslint/no-require-imports`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-require-imports.md)
|
|
4040
4040
|
>原因?统一使用 import 来引入模块,特殊情况使用单行注释允许 require 引入
|
|
4041
4041
|
|
|
4042
4042
|
```typescript
|
|
@@ -4049,7 +4049,7 @@ import Animal from './Animal';
|
|
|
4049
4049
|
|
|
4050
4050
|
## 8. 代码风格
|
|
4051
4051
|
|
|
4052
|
-
[8.1](#prefer-for-of) **【推荐】** 使用 for 循环遍历数组时,如果索引仅用于获取成员,则必须使用 for of 循环替代 for 循环。 eslint: [`@typescript-eslint/prefer-for-of`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-for-of.md)
|
|
4052
|
+
[8.1](#prefer-for-of) <a id="prefer-for-of"></a> **【推荐】** 使用 for 循环遍历数组时,如果索引仅用于获取成员,则必须使用 for of 循环替代 for 循环。 eslint: [`@typescript-eslint/prefer-for-of`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-for-of.md)
|
|
4053
4053
|
|
|
4054
4054
|
```typescript
|
|
4055
4055
|
// bad
|
|
@@ -4075,7 +4075,7 @@ for (let i = 0; i < arr2.length; i++) {
|
|
|
4075
4075
|
|
|
4076
4076
|
```
|
|
4077
4077
|
|
|
4078
|
-
[8.2](#type-annotation-spacing) **【必须】** 在类型注释周围需要一致的间距。 eslint: [`@typescript-eslint/type-annotation-spacing`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/type-annotation-spacing.md)
|
|
4078
|
+
[8.2](#type-annotation-spacing) <a id="type-annotation-spacing"></a> **【必须】** 在类型注释周围需要一致的间距。 eslint: [`@typescript-eslint/type-annotation-spacing`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/type-annotation-spacing.md)
|
|
4079
4079
|
|
|
4080
4080
|
```typescript
|
|
4081
4081
|
// bad
|
|
@@ -4117,7 +4117,7 @@ type Foo = () => {};
|
|
|
4117
4117
|
|
|
4118
4118
|
## 9. 命名方式
|
|
4119
4119
|
|
|
4120
|
-
[9.1](#naming-convention-class) **【推荐】** 变量和函数使用驼峰法进行命名。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4120
|
+
[9.1](#naming-convention-class) <a id="naming-convention-class"></a> **【推荐】** 变量和函数使用驼峰法进行命名。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4121
4121
|
|
|
4122
4122
|
> 对应 JS 规范中的 camelCase 规则
|
|
4123
4123
|
|
|
@@ -4133,7 +4133,7 @@ const fooRef = () = {};
|
|
|
4133
4133
|
function fooFunction() {};
|
|
4134
4134
|
```
|
|
4135
4135
|
|
|
4136
|
-
[9.2](#naming-convention-exported-varible) **【推荐】** 导出的常规变量使用全大写,下划线分割单词。但是对象、函数、类实例等使用 camelCase。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4136
|
+
[9.2](#naming-convention-exported-varible) <a id="naming-convention-exported-varible"></a> **【推荐】** 导出的常规变量使用全大写,下划线分割单词。但是对象、函数、类实例等使用 camelCase。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4137
4137
|
|
|
4138
4138
|
```typescript
|
|
4139
4139
|
// bad
|
|
@@ -4147,7 +4147,7 @@ export const BAR = 'abc';
|
|
|
4147
4147
|
export const instance = new Object();
|
|
4148
4148
|
```
|
|
4149
4149
|
|
|
4150
|
-
[9.3](#naming-convention-react-component) **【推荐】** React 组件使用 Pascal 写法。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4150
|
+
[9.3](#naming-convention-react-component) <a id="naming-convention-react-component"></a> **【推荐】** React 组件使用 Pascal 写法。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4151
4151
|
|
|
4152
4152
|
```typescript
|
|
4153
4153
|
// bad
|
|
@@ -4157,7 +4157,7 @@ const bar = <Foo />;
|
|
|
4157
4157
|
const Bar = <Foo />;
|
|
4158
4158
|
```
|
|
4159
4159
|
|
|
4160
|
-
[9.4](#class-type-naming-convention) **【推荐】** 类名和类型定义使用首字母大写。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4160
|
+
[9.4](#class-type-naming-convention) <a id="class-type-naming-convention"></a> **【推荐】** 类名和类型定义使用首字母大写。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4161
4161
|
|
|
4162
4162
|
```typescript
|
|
4163
4163
|
// bad
|
|
@@ -4177,7 +4177,7 @@ class Foo implements Foo {
|
|
|
4177
4177
|
}
|
|
4178
4178
|
```
|
|
4179
4179
|
|
|
4180
|
-
[9.5](#class-property-naming-convention) **【推荐】** 类成员使用驼峰法,并阻止使用下划线开头和结尾。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4180
|
+
[9.5](#class-property-naming-convention) <a id="class-property-naming-convention"></a> **【推荐】** 类成员使用驼峰法,并阻止使用下划线开头和结尾。 eslint: [`@typescript-eslint/naming-convention`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md)
|
|
4181
4181
|
|
|
4182
4182
|
```typescript
|
|
4183
4183
|
// bad
|
|
@@ -4209,7 +4209,7 @@ class Foo {
|
|
|
4209
4209
|
|
|
4210
4210
|
## 10. Promise 和 async await
|
|
4211
4211
|
|
|
4212
|
-
- [10.1](#promimse--no-misused-promises) **【必须】** 禁止对条件语句中 promise 的误用。 eslint: [`@typescript-eslint/no-misused-promises`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-misused-promises.md)
|
|
4212
|
+
- [10.1](#promimse--no-misused-promises) <a id="promimse--no-misused-promises"></a> **【必须】** 禁止对条件语句中 promise 的误用。 eslint: [`@typescript-eslint/no-misused-promises`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-misused-promises.md)
|
|
4213
4213
|
|
|
4214
4214
|
> 条件语句中的 promise 恒为真,需要用 await 才能获取异步调用后的返回值。
|
|
4215
4215
|
|