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

Skip to content

Commit 80a6cb5

Browse files
committed
更新 eslint 章节
1 parent c063ba9 commit 80a6cb5

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

.eslintrc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
parser: 'typescript-eslint-parser',
3+
plugins: [
4+
'typescript'
5+
],
6+
rules: {
7+
'spaced-comment': 'off',
8+
// 'react/no-unescaped-entities': 'off',
9+
// 'react/self-closing-comp': 'off',
10+
// 'react/void-dom-elements-no-children': 'off',
11+
'react/jsx-indent': 'off',
12+
// 'react/jsx-indent-props': 'off',
13+
// 'react/jsx-max-props-per-line': 'off'
14+
// 还不支持 properties https://github.com/yannickcr/eslint-plugin-react/issues/1342
15+
'react/sort-comp': 'off',
16+
'typescript/no-unused-vars': 'error'
17+
}
18+
};

ecosystem/eslint.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,77 @@ ESLint 也会对文件进行语法解析,它可以对一些代码风格进行
1717
```ts
1818
let myName = 'Tom';
1919

20-
comsole.log(`My name is ${myName}`);
2120
console.log(`My name is ${myNane}`);
2221
console.log(`My name is ${myName.toStrng()}`);
23-
console.log(`My name is ${ myName }`)
22+
console.log(`My name is ${myName}`)
2423

2524
// tsc 报错信息:
2625
//
27-
// index.ts(3,1): error TS2552: Cannot find name 'comsole'. Did you mean 'Console'?
28-
// index.ts(4,27): error TS2552: Cannot find name 'myNane'. Did you mean 'myName'?
29-
// index.ts(5,34): error TS2551: Property 'toStrng' does not exist on type 'string'. Did you mean 'toString'?
26+
// index.ts(3,27): error TS2552: Cannot find name 'myNane'. Did you mean 'myName'?
27+
// index.ts(4,34): error TS2551: Property 'toStrng' does not exist on type 'string'. Did you mean 'toString'?
3028
//
3129
//
3230
// eslint 报错信息:
3331
//
3432
// index.ts
35-
// 3:1 error 'comsole' is not defined no-undef
36-
// 4:27 error 'myNane' is not defined no-undef
37-
// 6:25 error Unexpected space(s) after '${' template-curly-spacing
38-
// 6:35 error Unexpected space(s) before '}' template-curly-spacing
39-
// 6:38 error Missing semicolon semi
33+
// 3:27 error 'myNane' is not defined no-undef
34+
// 5:38 error Missing semicolon semi
4035
//
41-
//5 problems (5 errors, 0 warnings)
42-
// 3 errors, 0 warnings potentially fixable with the `--fix` option.
36+
//2 problems (2 errors, 0 warnings)
37+
// 1 errors, 0 warnings potentially fixable with the `--fix` option.
4338
```
4439

4540
| 存在的问题 | `tsc` 是否报错 | `eslint` 是否报错 |
4641
| --------- | ------------- | ---------------- |
47-
| `console` 被勿写成了 `comsole` |||
4842
| `myName` 被勿写成了 `myNane` |||
4943
| `toString` 被勿写成了 `toStrng` | ✅️ ||
50-
| 模版字符串中 `${}` 内前后多出了两个空格 |||
5144
| 少了一个分号 |||
5245

53-
上例中,由于 `eslint` 无法识别 `myName` 存在哪些方法,所以对于拼写错误的 `toString` 没有检查出来。后面两个错误是代码风格,不影响编译,故 `tsc` 没有检查出来。而未定义的变量两者都能检查出来。
46+
上例中,由于 `eslint` 无法识别 `myName` 存在哪些方法,所以对于拼写错误的 `toString` 没有检查出来。而代码风格的错误不影响编译,故 `tsc` 没有检查出来。
47+
48+
未定义的变量两者都能检查出来。
5449

5550
值得注意的是,`tsc` 不仅检查出来了代码问题,还非常智能的给出了修改建议。
5651

57-
下面 TypeScirpt 作为一个静态代码检查工具,与 ESLint 的关系图:
52+
下面是 TypeScirpt 作为一个静态代码检查工具,与 ESLint 的关系图:
5853

5954
![TypeScript 和 ESLint 的关系](../assets/typescript-eslint.png)
6055

61-
上图中,静态代码检查包括了很多种,其中 TypeScirpt 与 ESLint 有重叠的部分,也有各自独立的部分,虽然发现代码错误比统一的代码风格更重要,但是当一个项目越来越庞大,开发人员也越来越多的时候,代码风格的约束还是必不可少的。
56+
上图中,TypeScript 与 ESLint 有重叠的部分,也有各自独立的部分,虽然发现代码错误比统一的代码风格更重要,但是当一个项目越来越庞大,开发人员也越来越多的时候,代码风格的约束还是必不可少的。
6257

6358
下面我们就来一步一步给我们的 TypeScript 项目添加 ESLint 检查。
6459

6560
## 安装
6661

67-
首先,我们需要安装 ESLint:
62+
ESLint 可以安装在当前项目中或全局环境下,因为代码检查是项目的重要组成部分,所以我们一般会将它安装在当前项目中。可以运行下面的脚本来安装
6863

6964
```bash
70-
npm install -g eslint
65+
npm install eslint --save-dev
7166
```
7267

73-
以上命令会在全局环境下安装 `eslint` 命令,安装完成之后,我们就可以在任何地方执行 `eslint` 命令了。
74-
7568
由于 ESLint 默认使用 [Espree](https://github.com/eslint/espree) 进行语法解析,无法识别 TypeScript 的一些语法,故我们需要安装 `typescript-eslint-parser`,替代掉默认的解析器:
7669

7770
```bash
78-
npm install -g typescript-eslint-parser
71+
npm install typescript-eslint-parser --save-dev
72+
```
73+
74+
由于 `typescript-eslint-parser` 对一部分 ESLint 规则支持性不好,故我们需要安装 `eslint-plugin-typescript`,弥补一些支持性不好的规则。
75+
76+
```bash
77+
npm install eslint-plugin-typescript --save-dev
7978
```
8079

8180
## 创建配置文件
8281

83-
如果没有创建配置文件,那么
82+
ESLint 需要一个配置文件来决定对哪些规则进行检查,配置文件的名称一般是 `.eslintrc.js``.eslintrc.json`
83+
84+
当运行 ESLint 的时候检查一个文件的时候,它会首先尝试读取该文件的目录下的配置文件,然后再一级一级往上查找,将所找到的配置合并起来,作为当前被检查文件的配置。
85+
86+
我们在项目的根目录下创建一个 `.eslintrc.js`,内容如下:
87+
88+
```js
89+
90+
```
8491

8592
- 使用 ESLint 加上
8693

0 commit comments

Comments
 (0)