@zjutjh/eslint-config 1.0.0 → 2.0.0-beta.1

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,4 +1,8 @@
1
- # `@zjutjh/eslint-config` [![npm-version](https://img.shields.io/npm/v/%40zjutjh%2Feslint-config)](https://www.npmjs.com/package/@zjutjh/eslint-config)
1
+ # `@zjutjh/eslint-config`
2
+
3
+ [![Open on npmx.dev](https://npmx.dev/api/registry/badge/license/@zjutjh/eslint-config?labelColor=000000&color=000000)](https://npmx.dev/package/@zjutjh/eslint-config)
4
+ [![Open on npmx.dev](https://npmx.dev/api/registry/badge/version/@zjutjh/eslint-config?labelColor=000000&color=000000)](https://npmx.dev/package/@zjutjh/eslint-config)
5
+ [![Open on npmx.dev](https://npmx.dev/api/registry/badge/size/@zjutjh/eslint-config?labelColor=000000&color=000000)](https://npmx.dev/package/@zjutjh/eslint-config)
2
6
 
3
7
  zjutjh 的 ESLint 配置,适用于 JS, TS, Vue3 等项目。
4
8
 
@@ -100,7 +104,10 @@ export default zjutjh({
100
104
  },
101
105
  ignores: [
102
106
  "**/build"
103
- ]
107
+ ],
108
+ // 默认关闭。设为 true 自动读取项目根目录下的 .gitignore(不存在则跳过)
109
+ // 也可传入 string 指定其他 gitignore 文件路径
110
+ // gitignore: true
104
111
  })
105
112
  ```
106
113
 
@@ -147,22 +154,23 @@ export default zjutjh(
147
154
  很多人在意代码的格式化,这里单独拿出一章讲。
148
155
 
149
156
  内置两种格式化工具。`@stylistic/eslint-plugin` (lint 工具对格式的检查) 和传统的 formatter 工具
150
- (Prettier) 。stylistic 默认开启,如果要使用 Prettier,需要手动开启。
157
+ ([oxfmt](https://oxc.rs/docs/guide/usage/formatter))。stylistic 默认开启,如果要使用 oxfmt,需要手动开启。
151
158
 
152
159
  ```ts
153
- // 启用 prettier
160
+ // 启用 oxfmt
154
161
  export default zjutjh({
155
- prettier: true
162
+ oxfmt: true
156
163
  }),
157
164
  ```
158
165
 
159
- 支持自定义 prettier 格式化选项,以及关闭对部分文件的格式化(默认对支持的文件全部开启)
166
+ 支持自定义 oxfmt 格式化选项,以及关闭对部分文件的格式化(默认对支持的文件全部开启)
160
167
 
161
168
  ```ts
162
169
  export default zjutjh({
163
- prettier: {
164
- prettierSelfOptions: {
165
- // 自定义 prettier 的格式化风格配置
170
+ oxfmt: {
171
+ oxfmtSelfOptions: {
172
+ // 自定义 oxfmt 的格式化风格配置
173
+ // 参考 https://oxc.rs/docs/guide/usage/formatter
166
174
  },
167
175
  lang: {
168
176
  html: false // 关闭对一些文件的格式化。默认对支持的文件全部开启
@@ -171,7 +179,7 @@ export default zjutjh({
171
179
  })
172
180
  ```
173
181
 
174
- stylistic 只对 js(x) 和 ts(x) 进行格式化,而 prettier 还对其他文件,如 css,html 等的格式化。
182
+ stylistic 只对 js(x) 和 ts(x) 进行格式化,而 oxfmt 还对其他文件,如 vue,css,html,json 等的格式化。
175
183
  如果你要**在编辑器中**自动格式化这些文件,需要配置编辑器来允许 eslint 校验这些类型的文件。
176
184
 
177
185
  ```jsonc
@@ -188,8 +196,8 @@ stylistic 只对 js(x) 和 ts(x) 进行格式化,而 prettier 还对其他文
188
196
  ```
189
197
 
190
198
  > [!WARNING]
191
- > 我们使用 eslint 调用 prettier 可执行文件来进行代码格式化,所以你的编辑器不需要安装 prettier 插件,有
192
- > eslint 插件就行。prettier 的格式化配置声明在配置源码内部,如果启用了 prettier 插件,他读取不到内部的配置,
199
+ > 我们通过 eslint 调用 oxfmt 来进行代码格式化,所以你的编辑器不需要安装额外的格式化插件,有
200
+ > eslint 插件就行。格式化配置声明在配置源码内部,如果启用了 prettier 插件,他读取不到内部的配置,
193
201
  > 会按照默认的配置来格式化,这会导致代码风格不一致。
194
202
 
195
203
  ## 开发指南
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Linter } from "eslint";
2
2
  import { ParserOptions } from "@typescript-eslint/parser";
3
- import { Options } from "prettier";
3
+ import { FormatOptions } from "oxfmt";
4
4
 
5
5
  //#region src/types.d.ts
6
6
  interface OverridesConfigs {
@@ -15,8 +15,9 @@ interface OptionsConfig extends OptionsComponentExts {
15
15
  taro?: boolean;
16
16
  jsx?: boolean;
17
17
  react?: boolean;
18
- prettier?: boolean | OptionsPrettier;
18
+ oxfmt?: boolean | OptionsOxfmt;
19
19
  ignores?: string[];
20
+ gitignore?: boolean | string;
20
21
  overrides?: OverridesConfigs;
21
22
  }
22
23
  interface OptionsComponentExts {
@@ -25,9 +26,10 @@ interface OptionsComponentExts {
25
26
  interface OptionsTypeScriptParserOptions {
26
27
  parserOptions?: Partial<ParserOptions>;
27
28
  }
28
- interface OptionsPrettier {
29
- prettierSelfOptions?: Options;
30
- /** 对哪些文件启用 prettier,默认全部启用 */
29
+ interface OptionsOxfmt {
30
+ /** 传递给 oxfmt format() API 的选项,参考 https://oxc.rs/docs/guide/usage/formatter */
31
+ oxfmtSelfOptions?: FormatOptions;
32
+ /** 对哪些文件启用 oxfmt,默认全部启用 */
31
33
  lang?: {
32
34
  /** js, ts, vue 文件 */es: boolean; /** css, less, scss 文件 */
33
35
  css: boolean;
package/dist/index.mjs CHANGED
@@ -1,4 +1,7 @@
1
1
  import { isPackageExists } from "local-pkg";
2
+ import { existsSync } from "node:fs";
3
+ import { isAbsolute, resolve } from "node:path";
4
+ import { includeIgnoreFile } from "@eslint/compat";
2
5
  import importXPlugin, { flatConfigs } from "eslint-plugin-import-x";
3
6
  import simpleImportSortPlugin from "eslint-plugin-simple-import-sort";
4
7
  import eslintJS from "@eslint/js";
@@ -57,10 +60,18 @@ const GLOBS_EXCLUDES = [
57
60
  //#endregion
58
61
  //#region src/configs/ignores.ts
59
62
  function ignores(options) {
60
- return [{
63
+ const { gitignore = false } = options ?? {};
64
+ const configs = [{
61
65
  name: "zjutjh/ignores",
62
66
  ignores: [...GLOBS_EXCLUDES, ...options?.userIgnores ?? []]
63
67
  }];
68
+ if (gitignore !== false) {
69
+ const path = gitignore === true ? ".gitignore" : gitignore;
70
+ const absolute = isAbsolute(path) ? path : resolve(process.cwd(), path);
71
+ if (existsSync(absolute)) configs.push(includeIgnoreFile(absolute, "zjutjh/gitignore"));
72
+ else if (gitignore !== true) throw new Error(`gitignore file not found: ${absolute}`);
73
+ }
74
+ return configs;
64
75
  }
65
76
  //#endregion
66
77
  //#region src/configs/imports.ts
@@ -170,11 +181,11 @@ function getOverrides(options, key) {
170
181
  return { ...options.overrides?.[key] };
171
182
  }
172
183
  //#endregion
173
- //#region src/configs/prettier.ts
184
+ //#region src/configs/oxfmt.ts
174
185
  /**
175
- * @see https://prettier.io/docs/options
186
+ * @see https://oxc.rs/docs/guide/usage/formatter
176
187
  */
177
- const prettierOptions = {
188
+ const oxfmtOptions = {
178
189
  printWidth: 100,
179
190
  tabWidth: 2,
180
191
  useTabs: false,
@@ -186,42 +197,23 @@ const prettierOptions = {
186
197
  bracketSpacing: true,
187
198
  bracketSameLine: false,
188
199
  arrowParens: "always",
189
- requirePragma: false,
190
- insertPragma: false,
191
- proseWrap: "preserve",
192
200
  htmlWhitespaceSensitivity: "css",
193
201
  vueIndentScriptAndStyle: false,
194
202
  endOfLine: "lf",
195
203
  embeddedLanguageFormatting: "auto",
196
204
  singleAttributePerLine: false
197
205
  };
198
- async function prettier(options) {
199
- await ensurePackages([
200
- "eslint-plugin-format",
201
- "eslint-plugin-prettier",
202
- "eslint-config-prettier",
203
- "prettier"
204
- ]);
205
- const [configPrettier, pluginFormat] = await Promise.all([interopDefault(import("eslint-plugin-prettier/recommended")), interopDefault(import("eslint-plugin-format"))]);
206
+ async function oxfmt(options) {
207
+ await ensurePackages(["eslint-plugin-format", "oxfmt"]);
208
+ const pluginFormat = await interopDefault(import("eslint-plugin-format"));
206
209
  const { es: enableESFormat = true, html: enableHTMLFormat = true, css: enableCSSFormat = true, json: enableJSONFormat = true } = options?.lang ?? {};
207
- const mergedPrettierOptions = {
208
- ...prettierOptions,
209
- ...options?.prettierSelfOptions
210
+ const mergedOptions = {
211
+ ...oxfmtOptions,
212
+ ...options?.oxfmtSelfOptions
210
213
  };
211
214
  return [
212
215
  enableESFormat ? {
213
- name: "zjutjh/prettier/setup",
214
- files: [
215
- GLOB_VUE,
216
- GLOB_TS,
217
- GLOB_JS,
218
- GLOB_TSX,
219
- GLOB_JSX
220
- ],
221
- ...configPrettier
222
- } : {},
223
- enableESFormat ? {
224
- name: "zjutjh/prettier/es",
216
+ name: "zjutjh/oxfmt/es",
225
217
  files: [
226
218
  GLOB_VUE,
227
219
  GLOB_TS,
@@ -229,10 +221,11 @@ async function prettier(options) {
229
221
  GLOB_TSX,
230
222
  GLOB_JSX
231
223
  ],
232
- rules: { "prettier/prettier": ["error", mergedPrettierOptions] }
224
+ plugins: { format: pluginFormat },
225
+ rules: { "format/oxfmt": ["error", mergedOptions] }
233
226
  } : {},
234
227
  enableCSSFormat ? {
235
- name: "zjutjh/prettier/css",
228
+ name: "zjutjh/oxfmt/css",
236
229
  files: [
237
230
  GLOB_CSS,
238
231
  GLOB_LESS,
@@ -240,23 +233,17 @@ async function prettier(options) {
240
233
  ],
241
234
  languageOptions: { parser: pluginFormat.parserPlain },
242
235
  plugins: { format: pluginFormat },
243
- rules: { "format/prettier": ["error", {
244
- parser: "css",
245
- mergedPrettierOptions
246
- }] }
236
+ rules: { "format/oxfmt": ["error", mergedOptions] }
247
237
  } : {},
248
238
  enableHTMLFormat ? {
249
- name: "zjutjh/prettier/html",
239
+ name: "zjutjh/oxfmt/html",
250
240
  files: [GLOB_HTML],
251
241
  languageOptions: { parser: pluginFormat.parserPlain },
252
242
  plugins: { format: pluginFormat },
253
- rules: { "format/prettier": ["error", {
254
- parser: "html",
255
- mergedPrettierOptions
256
- }] }
243
+ rules: { "format/oxfmt": ["error", mergedOptions] }
257
244
  } : {},
258
245
  enableJSONFormat ? {
259
- name: "zjutjh/prettier/json",
246
+ name: "zjutjh/oxfmt/json",
260
247
  files: [
261
248
  GLOB_JSON,
262
249
  GLOB_JSON5,
@@ -264,10 +251,7 @@ async function prettier(options) {
264
251
  ],
265
252
  languageOptions: { parser: pluginFormat.parserPlain },
266
253
  plugins: { format: pluginFormat },
267
- rules: { "format/prettier": ["error", {
268
- parser: "json",
269
- mergedPrettierOptions
270
- }] }
254
+ rules: { "format/oxfmt": ["error", mergedOptions] }
271
255
  } : {}
272
256
  ];
273
257
  }
@@ -462,9 +446,12 @@ async function vue(options) {
462
446
  //#endregion
463
447
  //#region src/factory.ts
464
448
  async function zjutjh(options = {}, ...userConfigs) {
465
- const { componentExts = [], vue: enableVue = isPackageExists("vue"), ts: enableTs = isPackageExists("typescript"), taro: enableTaro = isPackageExists("@tarojs/taro"), jsx: enableJSX = isPackageExists("react"), react: enableReact = isPackageExists("react"), ignores: userIgnores, prettier: enablePrettier = false } = options;
449
+ const { componentExts = [], vue: enableVue = isPackageExists("vue"), ts: enableTs = isPackageExists("typescript"), taro: enableTaro = isPackageExists("@tarojs/taro"), jsx: enableJSX = isPackageExists("react"), react: enableReact = isPackageExists("react"), ignores: userIgnores, gitignore: enableGitignore = false, oxfmt: enableOxfmt = false } = options;
466
450
  const configs = [];
467
- configs.push(ignores({ userIgnores }), javascript(), imports(), stylistic({ overrides: getOverrides(options, "stylistic") }), misc());
451
+ configs.push(ignores({
452
+ userIgnores,
453
+ gitignore: enableGitignore
454
+ }), javascript(), imports(), stylistic({ overrides: getOverrides(options, "stylistic") }), misc());
468
455
  if (enableVue) componentExts.push("vue");
469
456
  const typescriptOptions = resolveSubOptions(options, "ts");
470
457
  if (enableTs) configs.push(await typescript({
@@ -479,8 +466,8 @@ async function zjutjh(options = {}, ...userConfigs) {
479
466
  }));
480
467
  if (enableJSX) configs.push(jsx());
481
468
  if (enableReact) configs.push(await react({ overrides: getOverrides(options, "react") }));
482
- const codeStyleOptions = resolveSubOptions(options, "prettier");
483
- if (enablePrettier) configs.push(await prettier(codeStyleOptions));
469
+ const oxfmtOptions = resolveSubOptions(options, "oxfmt");
470
+ if (enableOxfmt) configs.push(await oxfmt(oxfmtOptions));
484
471
  return configs.flat(1).concat(userConfigs);
485
472
  }
486
473
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zjutjh/eslint-config",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "2.0.0-beta.1",
5
5
  "license": "ISC",
6
6
  "author": "zjutjh",
7
7
  "description": "ESLint config used by zjutjh",
@@ -32,13 +32,11 @@
32
32
  "@typescript-eslint/eslint-plugin": "^8.58.2",
33
33
  "@typescript-eslint/parser": "^8.58.2",
34
34
  "eslint": "^10.2.1",
35
- "eslint-config-prettier": "^10.1.8",
36
35
  "eslint-plugin-format": "^2.0.1",
37
- "eslint-plugin-prettier": "^5.5.5",
38
36
  "eslint-plugin-react-hooks": "^7.1.1",
39
37
  "eslint-plugin-react-refresh": "^0.5.2",
40
38
  "eslint-plugin-vue": "^10.8.0",
41
- "prettier": "^3.8.3",
39
+ "oxfmt": "^0.35.0",
42
40
  "vue-eslint-parser": "^10.4.0"
43
41
  },
44
42
  "peerDependenciesMeta": {
@@ -51,15 +49,9 @@
51
49
  "@typescript-eslint/parser": {
52
50
  "optional": true
53
51
  },
54
- "eslint-config-prettier": {
55
- "optional": true
56
- },
57
52
  "eslint-plugin-format": {
58
53
  "optional": true
59
54
  },
60
- "eslint-plugin-prettier": {
61
- "optional": true
62
- },
63
55
  "eslint-plugin-react-hooks": {
64
56
  "optional": true
65
57
  },
@@ -69,7 +61,7 @@
69
61
  "eslint-plugin-vue": {
70
62
  "optional": true
71
63
  },
72
- "prettier": {
64
+ "oxfmt": {
73
65
  "optional": true
74
66
  },
75
67
  "vue-eslint-parser": {
@@ -79,6 +71,7 @@
79
71
  "dependencies": {
80
72
  "@antfu/install-pkg": "^1.1.0",
81
73
  "@clack/prompts": "^1.2.0",
74
+ "@eslint/compat": "^2.0.5",
82
75
  "@eslint/js": "^10.0.1",
83
76
  "@stylistic/eslint-plugin": "^5.10.0",
84
77
  "eslint-plugin-import-x": "^4.16.2",
@@ -97,14 +90,12 @@
97
90
  "bumpp": "^11.0.1",
98
91
  "cspell": "^10.0.0",
99
92
  "eslint": "^10.2.1",
100
- "eslint-config-prettier": "^10.1.8",
101
93
  "eslint-plugin-format": "^2.0.1",
102
- "eslint-plugin-prettier": "^5.5.5",
103
94
  "eslint-plugin-react-hooks": "^7.1.1",
104
95
  "eslint-plugin-react-refresh": "^0.5.2",
105
96
  "eslint-plugin-vue": "^10.8.0",
106
97
  "lint-staged": "^16.4.0",
107
- "prettier": "^3.8.3",
98
+ "oxfmt": "^0.35.0",
108
99
  "simple-git-hooks": "^2.13.1",
109
100
  "tsdown": "^0.21.9",
110
101
  "typescript": "^6.0.3",