@zjutjh/eslint-config 0.7.0 → 0.7.2

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 CHANGED
@@ -1,9 +1,6 @@
1
- # `@zjutjh/eslint-config`
1
+ # `@zjutjh/eslint-config` [![npm-version](https://img.shields.io/npm/v/%40zjutjh%2Feslint-config)](https://www.npmjs.com/package/@zjutjh/eslint-config)
2
2
 
3
- [![npm-version](https://img.shields.io/npm/v/%40zjutjh%2Feslint-config
4
- )](https://www.npmjs.com/package/@zjutjh/eslint-config)
5
-
6
- zjutjh 的 ESLint 配置,适用于 JS, TS, Vue3 项目。
3
+ zjutjh 的 ESLint 配置,适用于 JS, TS, Vue3 等项目。
7
4
 
8
5
  ## 使用方式
9
6
 
@@ -27,83 +24,118 @@ import zjutjh from "@zjutjh/eslint-config";
27
24
  export default zjutjh();
28
25
  ```
29
26
 
30
- ```ts
31
- // 添加定制化配置
32
- export default [
33
- ...await zjutjh(),
34
- {
35
- name: "local/ignores",
36
- ignores: [
37
- "dist"
38
- ]
39
- }
40
- ]
41
- ```
42
-
43
- > [!TIP]
44
- > 在项目中调整配置时,如果对最终计算出的的配置有疑问,可以使用 [@eslint/config-inspector](https://github.com/eslint/config-inspector) 来调试配置。
45
-
46
- ```ts
47
- // 覆盖原有配置
48
- export default [
49
- ...await zjutjh({
50
- overrides: {
51
- vue: {
52
- "vue/multi-word-component-names": "off"
53
- }
54
- }
55
- })
56
- ]
57
- ```
58
-
59
- > [!WARNING]
60
- > 不要在数组内添加新的配置来修改原有配置,请使用函数提供的 overrides 属性来修改。
61
- >
62
- > 在一个配置对象内,一条规则的生效需要声明出对应的插件。对于用户而言,在工厂函数 `zjutjh()` 之外声明规则,无法直接获知一条规则对应的插件是什么。
63
- > 所以函数开放了 `overrides` 入口来透传配置到内部的配置对象中。自定义的配置被带到合适插件的生效范围之下,并结合 JS 声明同名的对象属性来支持配置的覆盖。
64
-
65
27
  在 `package.json` 中添加如下命令。
66
28
 
67
29
  ```json
68
30
  {
69
31
  "scripts": {
70
- "lint": "eslint"
32
+ "lint": "eslint ."
71
33
  }
72
34
  }
73
35
  ```
74
36
 
75
- 在项目中运行即可。如果有提示按照依赖,请按照提示出的包名安装(应该安装到 `devDependencies`)。
37
+ 在项目根目录运行下面的命令,没有报错就算配置成功。
76
38
 
77
39
  ```sh
78
40
  $ npm run lint
79
41
  ```
80
42
 
43
+ > [!NOTE]
44
+ > 项目第一次接入时运行 `eslint .` 可能会有安装依赖的交互式命令,按照提示完成依赖安装即可。
45
+ >
46
+ > 在每次修改依赖配置后,最好也运行一遍 lint 命令,部分配置项可能需要安装额外的依赖。
47
+
48
+ 大部分场景到这里就能使用了,不需要额外的配置。如果你想自定义一些配置,请往下看。
49
+
50
+ ### 自定义配置
51
+
52
+ 基于现有能力修改配置
53
+
54
+ ```ts
55
+ export default zjutjh({
56
+ overrides: {
57
+ vue: {
58
+ "vue/multi-word-component-names": ["off"]
59
+ },
60
+ stylistic: {
61
+ "stylistic/quotes": ["error", "single"]
62
+ }
63
+ },
64
+ ignores: [
65
+ "**/build"
66
+ ]
67
+ })
68
+ ```
69
+
70
+ 扩展 eslint 配置
71
+
72
+ ```ts
73
+ export default zjutjh(
74
+ // 第一个参数是 zjutjh 专用的配置
75
+ {
76
+ overrides: {
77
+ stylistic: {
78
+ "stylistic/quotes": ["error", "single"]
79
+ }
80
+ },
81
+ ignores: [
82
+ "**/build"
83
+ ]
84
+ },
85
+ // 从第二个参数开始,可以任意传入多个 flat config
86
+ {
87
+ files: ["**/*.vue"],
88
+ rules: {
89
+ "vue/multi-word-component-names": ["off"]
90
+ }
91
+ },
92
+ {
93
+ // 不传入 files glob, 则对所有文件生效 */
94
+ // files: [/** any globs */],
95
+ plugins: {
96
+ // 使用 eslint 插件
97
+ },
98
+ rules: {
99
+ // 使用 eslint 规则
100
+ }
101
+ }
102
+ )
103
+ ```
104
+
105
+ > [!TIP]
106
+ > 在项目中调整配置时,如果对最终生效的配置有疑问,可以使用 [@eslint/config-inspector](https://github.com/eslint/config-inspector) 来调试配置。
107
+
81
108
  ## 代码格式化
82
109
 
83
110
  很多人在意代码的格式化,这里单独拿出一章讲。
84
111
 
85
- 支持使用 `@stylistic/eslint-plugin` (lint 工具对格式的检查) 或者传统的 formatter 工具
86
- (Prettier) 来对代码进行格式化。后者的具体配置放在 `options.prettier` stylistic 默认开启,
87
- 如果要使用 formatter,需要手动开启。
112
+ 内置两种格式化工具。`@stylistic/eslint-plugin` (lint 工具对格式的检查) 和传统的 formatter 工具
113
+ (Prettier) 。stylistic 默认开启,如果要使用 Prettier,需要手动开启。
88
114
 
89
115
  ```ts
90
116
  // 启用 prettier
91
- export default [
92
- ...(await zjutjh({
93
- prettier: {
94
- prettierSelfOptions: {
95
- // 自定义 prettier 的格式化风格配置
96
- },
97
- lang: {
98
- html: false // 关闭对一些文件的格式化,默认对支持的文件全部开启
99
- }
117
+ export default zjutjh({
118
+ prettier: true
119
+ }),
120
+ ```
121
+
122
+ 支持自定义 prettier 格式化选项,以及关闭对部分文件的格式化(默认对支持的文件全部开启)
123
+
124
+ ```ts
125
+ export default zjutjh({
126
+ prettier: {
127
+ prettierSelfOptions: {
128
+ // 自定义 prettier 的格式化风格配置
129
+ },
130
+ lang: {
131
+ html: false // 关闭对一些文件的格式化。默认对支持的文件全部开启
100
132
  }
101
- })),
102
- ];
133
+ }
134
+ })
103
135
  ```
104
136
 
105
137
  stylistic 只对 js(x) 和 ts(x) 进行格式化,而 prettier 还对其他文件,如 css,html 等的格式化。
106
- 如果你要格式化这些文件,需要配置编辑器来允许 eslint 校验这些类型的文件。
138
+ 如果你要**在编辑器中**自动格式化这些文件,需要配置编辑器来允许 eslint 校验这些类型的文件。
107
139
 
108
140
  ```jsonc
109
141
  // 可以参考仓库下的 .vscode/settings.json 给 vscode 配置
package/dist/index.d.ts CHANGED
@@ -2,25 +2,25 @@ import { Linter } from 'eslint';
2
2
  import { ParserOptions } from '@typescript-eslint/parser';
3
3
  import { Options } from 'prettier';
4
4
 
5
- type OverridesConfigs = {
5
+ interface OverridesConfigs {
6
6
  vue?: Linter.RulesRecord;
7
7
  ts?: Linter.RulesRecord;
8
8
  stylistic?: Linter.RulesRecord;
9
9
  react?: Linter.RulesRecord;
10
- };
11
- type OptionsConfig = {
10
+ }
11
+ interface OptionsConfig extends OptionsComponentExts {
12
12
  vue?: boolean;
13
- ts?: boolean | (OptionsOverrides & OptionsTypeScriptParserOptions);
13
+ ts?: boolean | OptionsTypeScriptParserOptions;
14
14
  taro?: boolean;
15
15
  jsx?: boolean;
16
16
  react?: boolean;
17
17
  prettier?: boolean | OptionsPrettier;
18
18
  ignores?: string[];
19
19
  overrides?: OverridesConfigs;
20
- };
21
- type OptionsOverrides = {
22
- overrides?: Linter.RulesRecord;
23
- };
20
+ }
21
+ interface OptionsComponentExts {
22
+ componentExts?: string[];
23
+ }
24
24
  interface OptionsTypeScriptParserOptions {
25
25
  parserOptions?: Partial<ParserOptions>;
26
26
  }
package/dist/index.js CHANGED
@@ -203,7 +203,12 @@ var prettierOptions = {
203
203
  singleAttributePerLine: false
204
204
  };
205
205
  async function prettier(options) {
206
- await ensurePackages(["eslint-plugin-prettier", "eslint-config-prettier", "prettier"]);
206
+ await ensurePackages([
207
+ "eslint-plugin-format",
208
+ "eslint-plugin-prettier",
209
+ "eslint-config-prettier",
210
+ "prettier"
211
+ ]);
207
212
  const [configPrettier, pluginFormat] = await Promise.all([
208
213
  interopDefault(import("eslint-plugin-prettier/recommended")),
209
214
  interopDefault(import("eslint-plugin-format"))
@@ -369,9 +374,11 @@ function stylistic(options) {
369
374
  // src/configs/typescript.ts
370
375
  async function typescript(options) {
371
376
  const {
377
+ componentExts = [],
372
378
  overrides,
373
379
  parserOptions
374
380
  } = options;
381
+ const files = [GLOB_TS, GLOB_TSX, ...componentExts.map((ext) => `**/*.${ext}`)];
375
382
  await ensurePackages([
376
383
  "@typescript-eslint/eslint-plugin",
377
384
  "@typescript-eslint/parser"
@@ -393,7 +400,7 @@ async function typescript(options) {
393
400
  },
394
401
  {
395
402
  name: "zjutjh/typescript/parser",
396
- files: [GLOB_TS, GLOB_TSX],
403
+ files,
397
404
  languageOptions: {
398
405
  parser: parserTs,
399
406
  parserOptions: {
@@ -410,6 +417,7 @@ async function typescript(options) {
410
417
  },
411
418
  {
412
419
  name: "zjutjh/typescript/rules",
420
+ files,
413
421
  rules: {
414
422
  ...pluginTs.configs.strict.rules,
415
423
  "@typescript-eslint/ban-ts-comment": ["error", { "ts-expect-error": "allow-with-description" }],
@@ -417,6 +425,7 @@ async function typescript(options) {
417
425
  "@typescript-eslint/no-non-null-assertion": "error",
418
426
  "@typescript-eslint/no-empty-function": "error",
419
427
  "@typescript-eslint/no-explicit-any": "off",
428
+ "@typescript-eslint/no-unnecessary-condition": "error",
420
429
  "@typescript-eslint/no-unused-expressions": ["error", {
421
430
  allowShortCircuit: true,
422
431
  allowTaggedTemplates: true,
@@ -480,6 +489,7 @@ async function vue(options) {
480
489
  // src/factory.ts
481
490
  async function zjutjh(options = {}, ...userConfigs) {
482
491
  const {
492
+ componentExts = [],
483
493
  vue: enableVue = isPackageExists2("vue"),
484
494
  ts: enableTs = isPackageExists2("typescript"),
485
495
  taro: enableTaro = isPackageExists2("@tarojs/taro"),
@@ -497,6 +507,9 @@ async function zjutjh(options = {}, ...userConfigs) {
497
507
  overrides: getOverrides(options, "stylistic")
498
508
  })
499
509
  );
510
+ if (enableVue) {
511
+ componentExts.push("vue");
512
+ }
500
513
  const typescriptOptions = resolveSubOptions(options, "ts");
501
514
  if (enableTs) {
502
515
  configs.push(
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zjutjh/eslint-config",
3
3
  "type": "module",
4
- "version": "0.7.0",
4
+ "version": "0.7.2",
5
5
  "license": "ISC",
6
6
  "author": "zjutjh",
7
7
  "description": "Eslint config used by zjutjh",