Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ea5280

Browse files
committed
完成 lint.md
1 parent 8cb0b47 commit 0ea5280

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

assets/typescript-eslint-tslint.png

28.3 KB
Loading

engineering/lint.md

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ console.log(`My name is ${myName}`)
6565

6666
而代码风格的错误不影响编译,故少了一个分号的错误 `tsc` 没有检查出来。
6767

68-
对于未定义的变量 `myNane``tsc` 可以检测出来。由于用到 `tslint` 的地方肯定会接入 `tsc` 编译,所以 `tslint` 就没必要检测这个错误了。`eslint` 需要能够独立于某个编译环境,所以能检测出此类错误,但是对于 TypeScript 代码,这其实是一种冗余的检测了。
68+
对于未定义的变量 `myNane``tsc` 可以检测出来。由于用到 `tslint` 的地方肯定会接入 `tsc` 编译,所以 `tslint` 就没必要检测这个错误了。`eslint` 需要能够独立于某个编译环境,所以能检测出此类错误,而对于 TypeScript 代码,这其实是一种冗余的检测了。
6969

70-
其实不止 `tsc``eslint` 之间有冗余的检测,`tsc``tslint` 之间也有一些冗余的检测,但是大部分都是因为早期的 `tsc` 还没能做到检测此类错误。
70+
事实上,不止 `tsc``eslint` 之间有冗余的检测,`tsc``tslint` 之间也有一些冗余的检测,但是大部分都是因为早期的 `tsc` 还没能做到检测此类错误。
7171

72-
比如 TSLint 中的 `typeof-compare` 要求 `typeof` 表达式比较的对象必须是 `'undefined'`, `'object'`, `'boolean'`, `'number'`, `'string'`, `'function'``'symbol'` 之一。而 TypeScript 2.2 之后,编译器就已经自带了这个功能。
72+
举个例子,TSLint 中的 `typeof-compare` 要求 `typeof` 表达式比较的对象必须是 `'undefined'`, `'object'`, `'boolean'`, `'number'`, `'string'`, `'function'``'symbol'` 之一。而 TypeScript 2.2 之后,编译器就已经自带了这个功能。
7373

7474
下图表示了 `tsc`, `eslint``tslint` 能覆盖的检查:
7575

76-
@TODO 图片待补充
76+
![TypeScript vs ESLint vs TSLint](../assets/typescript-eslint-tslint.png)
7777

7878
上图中,`tsc`, `eslint``tslint` 之间互相都有重叠的部分,也有各自独立的部分。
7979

@@ -250,7 +250,7 @@ if (tom.age == 25) {
250250

251251
需要注意的是,我们使用的是 `./node_modules/.bin/eslint`,而不是全局的 `eslint` 脚本,这是因为代码检查是项目的重要组成部分,所以我们一般会将它安装在当前项目中。
252252

253-
可是每次执行这么长一段脚本颇有不便,我们可以在 `package.json` 中添加一个 `script` 来简化这个步骤:
253+
可是每次执行这么长一段脚本颇有不便,我们可以通过在 `package.json` 中添加一个 `script` 来创建一个 npm script 来简化这个步骤:
254254

255255
```json
256256
{
@@ -298,7 +298,7 @@ VSCode 中的 ESLint 插件默认是不会检查 `.ts` 后缀的,需要在「
298298

299299
![VSCode ESLint 错误信息](../assets/vscode-eslint-error.png)
300300

301-
### 使用已完善的配置
301+
### 使用 AlloyTeam 的 ESLint 配置
302302

303303
ESLint 原生的规则和 `eslint-plugin-typescript` 的规则太多了,而且原生的规则有一些在 TypeScript 中支持的不好,需要禁用掉。
304304

@@ -379,7 +379,83 @@ npm install --save-dev eslint-plugin-react
379379

380380
## 在 TypeScirpt 中使用 TSLint
381381

382-
@TODO 待补充
382+
TSLint 的使用比较简单,参考[官网的步骤](https://palantir.github.io/tslint/)安装到本地即可:
383+
384+
```bash
385+
npm install --save-dev tslint
386+
```
387+
388+
创建配置文件 `tslint.json`
389+
390+
```json
391+
{
392+
"rules": {
393+
// 必须使用 === 或 !==,禁止使用 == 或 !=,与 null 比较时除外
394+
"triple-equals": [
395+
true,
396+
"allow-null-check"
397+
]
398+
},
399+
"linterOptions": {
400+
"exclude": [
401+
"**/node_modules/**"
402+
]
403+
}
404+
}
405+
```
406+
407+
`package.json` 添加 `tslint` 脚本
408+
409+
```json
410+
{
411+
"scripts": {
412+
"tslint": "tslint --project . src/**/*.ts src/**/*.tsx",
413+
}
414+
}
415+
```
416+
417+
其中 `--project .` 会要求 `tslint` 使用当前目录的 `tsconfig.json` 配置来获取类型信息,很多规则需要类型信息才能生效。
418+
419+
此时执行 `npm run tslint` 即可检查整个项目。
420+
421+
### 在 VSCode 中集成 TSLint 检查
422+
423+
在 VSCode 中安装 `tslint` 插件即可,安装好之后,默认是开启的状态。
424+
425+
### 使用 AlloyTeam 的 TSLint 配置
426+
427+
AlloyTeam 为 TSLint 也打造了一套配置 [tslint-config-alloy](https://github.com/AlloyTeam/tslint-config-alloy)
428+
429+
```bash
430+
npm install --save-dev tslint-config-alloy
431+
```
432+
433+
安装之后修改 `tsconfig.json` 即可
434+
435+
```json
436+
{
437+
"extends": "tslint-config-alloy",
438+
"rules": {
439+
// 这里填入你的项目需要的个性化配置,比如:
440+
//
441+
// 一个缩进必须用两个空格替代
442+
// "indent": [
443+
// true,
444+
// "spaces",
445+
// 2
446+
// ]
447+
},
448+
"linterOptions": {
449+
"exclude": [
450+
"**/node_modules/**"
451+
]
452+
}
453+
}
454+
```
455+
456+
### 使用 TSLint 检查 tsx 文件
457+
458+
TSLint 默认支持对 tsx 文件的检查,不需要做额外配置。
383459

384460
## Troubleshootings
385461

@@ -421,6 +497,12 @@ npm install --save-dev eslint-plugin-react
421497
}
422498
```
423499

500+
### 为什么有的错误 TSLint 可以检查出来,vscode 里的 TSLint 却检查不出来?
501+
502+
因为 TSLint 依赖 `tsconfig.json` 获得了类型信息,而 [vscode 里的 TSLint 暂不支持获取类型信息](https://github.com/Microsoft/vscode-tslint/tree/master/tslint#the-tslint-no-unused-variable-rule-doesnt-report-warnings-any-more),所以 `no-unused-variable` 就失效了。
503+
504+
不仅 `no-unused-variables` 失效了,[TSLint rules](https://palantir.github.io/tslint/rules/) 里面所有标有 `Requires Type Info` 的规则都失效了。
505+
424506
[TSLint]: https://palantir.github.io/tslint/
425507
[ESLint]: https://eslint.org/
426508
[`typescript-eslint-parser`]: https://github.com/eslint/typescript-eslint-parser

0 commit comments

Comments
 (0)