my-code-style 1.9.1 → 1.9.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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## 变更日志
2
2
 
3
+ ## [1.9.3](https://github.com/myvn/codeStyle/compare/v1.9.2...v1.9.3) (2026-09-23)
4
+
5
+ ### 🐛 Bug Fixes | Bug 修复
6
+
7
+ - **init:** BUG-038 兼容段 files 补 vue/nvue——SFC 内显式 .ts import 不再报 error ([c2298af](https://github.com/myvn/codeStyle/commit/c2298af354592f4f9232d3571be5ff31075a81e5))
8
+ - **peer:** typescript 放宽至 ^5.0.0 || ^6.0.0——TS 6(Vite 8 模板默认)不再堵死装机(BUG-039) ([fa633f1](https://github.com/myvn/codeStyle/commit/fa633f1a0f65229609cd6a38f88d032d08124e50))
9
+
10
+ ## [1.9.2](https://github.com/myvn/codeStyle/compare/v1.9.1...v1.9.2) (2026-09-23)
11
+
12
+ ### 🐛 Bug Fixes | Bug 修复
13
+
14
+ - **init:** 兼容段 off→warn——带/不带 .ts 扩展名 import 双向兼容 ([a26cfb6](https://github.com/myvn/codeStyle/commit/a26cfb6884b96c466e45b935a606c217bd39069c))
15
+ - **init:** 显式 .ts 扩展名 import 自动兼容,fresh init 保持绿起点(BUG-037) ([4c63221](https://github.com/myvn/codeStyle/commit/4c632219817cf188d80963a45076817bcfd9d6ef))
16
+
3
17
  ## [1.9.1](https://github.com/myvn/codeStyle/compare/v1.9.0...v1.9.1) (2026-09-23)
4
18
 
5
19
  ### 🐛 Bug Fixes | Bug 修复
package/README.md CHANGED
@@ -26,7 +26,7 @@ npx my-code-style-init --dry-run
26
26
  npx my-code-style-init # 需保留旧文件时加 --backup
27
27
 
28
28
  # 3. 按 CLI 输出的缺失依赖清单安装 peerDependencies(会按包管理器生成命令,别漏 typescript 和 stylelint-order——后者是 recess-order 7 的 peer,Vue/SCSS 项目缺了它 stylelint 直接崩)
29
- pnpm add -D my-code-style eslint@^9.0.0 typescript@^5.0.0 prettier@^3.0.0 ... # 以 CLI 实际输出为准
29
+ pnpm add -D my-code-style eslint@^9.0.0 typescript@^6.0.0 prettier@^3.0.0 ... # 以 CLI 实际输出为准
30
30
 
31
31
  # 4. 初始化 hooks 并首次检查
32
32
  pnpm prepare # npm: npm run prepare
@@ -206,13 +206,25 @@ npx my-code-style-init [--dry-run] [--backup] [--merge] [--version|-v] [--help|-
206
206
 
207
207
  ## lint 覆盖范围须知
208
208
 
209
- `lint` 脚本只跑 **ESLint**;Stylelint 默认只在 pre-commit 的 lint-staged 里对**本次改动的文件**执行。CI 如果只跑 `pnpm lint`,CSS/SCSS/Vue 内嵌样式不会被检查。有 CSS 预处理器的项目,init 会额外生成全量样式检查入口:
209
+ | 入口 | 覆盖 | 典型用途 |
210
+ | ------------------------- | ----------------------------------------------- | ------------------ |
211
+ | `pnpm lint` | ESLint(JS/TS/Vue 脚本层) | 本地与 CI 基础门禁 |
212
+ | `pnpm lint:style` | Stylelint 全仓(Vue/HTML 内嵌 + css/scss/less) | CI 补齐样式检查 |
213
+ | `pnpm lint:all` | ESLint + Stylelint 全仓 | CI 一条命令全覆盖 |
214
+ | lint-staged(pre-commit) | 仅本次改动的文件(三件套自动修复) | 提交门禁 |
210
215
 
211
- ```bash
212
- pnpm lint:style # stylelint "**/*.{vue,nvue,html,css,scss,less}"
213
- ```
216
+ `lint` 只跑 **ESLint**;Stylelint 默认只在 pre-commit 对**本次改动的文件**执行——CI 只跑 `pnpm lint` 时 CSS/SCSS/Vue 内嵌样式不会被检查。有 CSS 预处理器的项目,init 会生成 `lint:style` 与 `lint:all`。
217
+
218
+ ### 显式扩展名 import 的兼容行为
219
+
220
+ `import-x/extensions` 默认要求相对导入**省略** `.ts/.tsx` 扩展名。Vite 等 bundler resolution 项目常写 `import x from "@/types/Foo.ts"`(合法但与该规则冲突)。
221
+
222
+ init 会**自动检测**:命中显式扩展名 import 时,生成的 `eslint.config.mjs` 注入兼容段——该规则**降级为 warn**,`pnpm lint` 退出码不受影响:
214
223
 
215
- 把 `lint:style` 加进 CI(或并进 `lint:all`)即可补齐覆盖面。
224
+ - **带 `.ts` 与不带 `.ts` 都可以写**,两种风格共存不报错;
225
+ - 兼容段同样覆盖 **Vue SFC(`.vue`/`.nvue`)**:`<script setup>` 里的显式扩展名 import 不会绕过兼容段报错(BUG-038);
226
+ - 不一致的写法仍会出 warning,作为渐进统一的风格信号;
227
+ - 团队统一后想恢复严格检查(error 级、只允许省略),删除 `eslint.config.mjs` 里的兼容段即可。
216
228
 
217
229
  ## 项目体检:my-code-style-doctor
218
230
 
@@ -252,6 +264,7 @@ npx my-code-style-doctor
252
264
  | `pnpm prepare` 输出 `.git can't be found`,hooks 不生效 | husky 必须在 git 仓库内执行 | 先 `git init`、再 `pnpm prepare`;验证 `git config core.hooksPath` 输出 `.husky/_` |
253
265
  | 安装依赖报 `ERR_PNPM_IGNORED_BUILDS`(如 `unrs-resolver`) | pnpm 10+ 默认拦截依赖的构建脚本 | 执行 `pnpm approve-builds` 后重新安装,不是初始化失败 |
254
266
  | `eslint` 声明 `>=8.0.0` 却生成了 Flat Config | 无上界范围 npm 实际会装 9,CLI 按 9 生成 | 确定留在 8 就把范围收紧到 `<9`(如 `~8.57.0`)后重跑 init |
267
+ | 首次接入存量/脚手架项目,`lint:style` 集中报几十个属性顺序错 | stylelint-order(recess-order)按既有排序规约检查,脚手架模板(如 Vite 的 `style.css`)不满足 | 一次性 `stylelint "**/*.{vue,nvue,html,css,scss,less}" --fix` 自动归位后再提交;此后保持绿色。属存量风格收敛,非本配置引入的错误 |
255
268
  | 重复 init 后,改过的 `.prettierrc.cjs` / hooks 被覆盖 | 除 ESLint 入口与已存在的 `.gitignore` 外,目标文件一律覆盖 | 先用 `--dry-run` 看清单、用 `--backup` 留档(备份含 `package.json`,目录会自动写入 `.gitignore`) |
256
269
  | 自定义 `lint-staged` 分组消失 | `lint-staged` 为整体替换,避免残留失效分组 | 从 `.my-code-style-backup/package.json` 恢复后手动合并 |
257
270
  | 提交时 lint 报错、提交失败 | 修不了的错误(语法错误、非法 CSS 属性)会阻止提交 | 按提示修复;紧急情况可用 `git commit --no-verify`,但请在 **CI 用 `pnpm lint` 兜底** |
@@ -268,12 +281,12 @@ npx my-code-style-doctor
268
281
  ## 测试套件
269
282
 
270
283
  ```bash
271
- npm run test:all # 全量运行 197 项:用例明细 + 分类统计 + 最慢文件定位
284
+ npm run test:all # 全量运行 200 项:用例明细 + 分类统计 + 最慢文件定位
272
285
  npm run diagnose # 换机器后先跑它:进程 / git / 文件系统 / hooks 各占多少
273
286
  npm run sweep # 给本机找最佳文件级并发(扫 integration 套件)
274
- npm test # 只跑基础 CLI 与配置矩阵(96 项)
287
+ npm test # 只跑基础 CLI 与配置矩阵(98 项)
275
288
  npm run test:integration:setup # 安装现代化隔离依赖运行环境
276
- npm run test:integration # Flat Config、真实 Husky 及提交链路测试(65 项)
289
+ npm run test:integration # Flat Config、真实 Husky 及提交链路测试(66 项)
277
290
  npm run test:legacy:setup # 安装 ESLint 8 隔离运行环境
278
291
  npm run test:legacy # ESLint 8.57.0 兼容性回归测试(27 项)
279
292
  npm run test:stylelint17:setup # 安装 Stylelint 17 隔离运行环境
@@ -286,47 +299,47 @@ npm run test:stylelint17 # Stylelint 17 生态兼容性回归(8 项)
286
299
  ```text
287
300
  my-code-style 测试套件
288
301
 
289
- ▶ [1/4] 基础 CLI 与配置矩阵 (共 96 项,5 文件 · 3 文件并发(自动:2 核 / 内存 4G))
302
+ ▶ [1/4] 基础 CLI 与配置矩阵 (共 98 项,5 文件 · 3 文件并发(自动:2 核 / 内存 4G))
290
303
  ✓ 运行器 TAP 解析:统计通过与失败、SKIP 与每用例耗时 · 7ms
291
304
  ✓ cz 交互提示为中文,且可选类型与 type-enum 一一对应 · 1ms
292
305
  ✓ peer 范围覆盖我们声明支持的上游大版本(校准防回退,BUG-027) · 0ms
293
306
  ✓ 一键安装命令对含 || 的版本范围加引号,可整段粘贴执行 · 40ms
294
307
  ✓ 初始化结尾指引把 cz 用法说清楚(先 git add + 指到 README 手册) · 80ms
295
308
  …
296
- ✓ 通过 96 · 3.6s · 最慢文件 init-matrix.test.cjs 3.4s
309
+ ✓ 通过 98 · 4.2s · 最慢文件 init-matrix.test.cjs 4.0s
297
310
 
298
- ▶ [2/4] 现代工具链与提交链 (共 65 项,24 文件 · 3 文件并发(自动:2 核 / 内存 4G))
311
+ ▶ [2/4] 现代工具链与提交链 (共 66 项,24 文件 · 3 文件并发(自动:2 核 / 内存 4G))
299
312
  ✓ 完整提交链:JS/TS/Vue/nvue/CSS/less 自动修复及二次复检 · 10.4s
300
313
  ✓ 发布防呆:HEAD 已有 v* tag 时拦截(此时 release 工具会静默抬版并写出空 CHANGELOG) · 609ms
301
314
  …
302
- ✓ 通过 65 · 39.5s · 最慢文件 commit-chain-scss.test.cjs 12.7s
315
+ ✓ 通过 66 · 43.8s · 最慢文件 commit-chain-both.test.cjs 11.9s
303
316
 
304
317
  ▶ [3/4] ESLint 8 兼容性 (共 27 项,1 文件 · 3 文件并发(自动:2 核 / 内存 4G))
305
318
  ✓ 独立运行 ESLint 8 支持下限而非 ESLint 9 · 1ms
306
319
  ✓ ESLint 8 base:实际 CLI 加载生成配置并接受正常文件 · 1.3s
307
320
  …
308
- ✓ 通过 27 · 16.3s
321
+ ✓ 通过 27 · 20.2s
309
322
 
310
323
  ▶ [4/4] Stylelint 17 兼容性 (共 9 项,1 文件 · 3 文件并发(自动:2 核 / 内存 4G))
311
324
  ✓ 隔离环境装的是 stylelint 17(不是 16) · 2ms
312
325
  ✓ 生成的 .stylelintrc.cjs 在 stylelint 17 下可加载并放过正常 SCSS · 700ms
313
326
  …
314
- ✓ 通过 9 · 7.4s
327
+ ✓ 通过 9 · 8.8s
315
328
 
316
329
  ──────────────────────────────────────────────────────
317
330
  套件 通过 失败 跳过 用时
318
331
  ──────────────────────────────────────────────────────
319
- 基础 CLI 与配置矩阵 96 0 - 3.6s
320
- 现代工具链与提交链 65 0 - 39.5s
321
- ESLint 8 兼容性 27 0 - 16.3s
322
- Stylelint 17 兼容性 9 0 - 7.4s
332
+ 基础 CLI 与配置矩阵 98 0 - 4.2s
333
+ 现代工具链与提交链 66 0 - 43.8s
334
+ ESLint 8 兼容性 27 0 - 20.2s
335
+ Stylelint 17 兼容性 9 0 - 8.8s
323
336
  ──────────────────────────────────────────────────────
324
- 合计 197 0 - 66.8s
337
+ 合计 200 0 - 77.1s
325
338
  ──────────────────────────────────────────────────────
326
339
 
327
340
  ⏱ 最慢文件:eslint8.test.cjs 18.0s · commit-chain-less.test.cjs 11.4s · commit-chain-both.test.cjs 11.3s
328
341
 
329
- ✅ 全部通过:197/197 项,用时 66.8s
342
+ ✅ 全部通过:200/200 项,用时 77.1s
330
343
  ```
331
344
 
332
345
  终端里每个套件下方还有一条实时进度条(`██████░░░░ 38/66 失败 0 4.0s`)。附加参数:
@@ -404,7 +417,7 @@ npm run test:all -- --profile # 看每个测试文件耗时,定位瓶颈
404
417
  | `npm run test:integration`(运行器调度,自动并发 3) | 30.6s(只看集成套件那一段) |
405
418
  | 串行套件 + 文件级并发 23(全部一波) | ✗ 3.8G 内存沙箱被 OOM 压垮(19 项 SIGKILL) |
406
419
 
407
- 同机复测(197 项,同一台 2 核沙箱连续跑;绝对秒数随机器负载波动,相对关系稳定):默认自动并发 3 = 66.8s、`npm run test:integration` = 39.5s(本轮实跑;串行与 parallel 口径为早前基线实测,相对关系稳定)。
420
+ 同机复测(200 项,同一台 2 核沙箱连续跑;绝对秒数随机器负载波动,相对关系稳定):默认自动并发 3 = 77.1s、`npm run test:integration` = 43.8s(本轮实跑;串行与 parallel 口径为早前基线实测,相对关系稳定)。
408
421
 
409
422
  (2 核沙箱里并行收益被 CPU 争抢吃掉大半,`--concurrency=1` 时单个文件只要 0.7–4.2s;
410
423
  16 核机器上集成套件那段 ≈ 最慢子文件,即"一次真实提交"的量级。)
package/bin/init CHANGED
@@ -555,7 +555,55 @@ module.exports = config
555
555
  `
556
556
  }
557
557
 
558
- function eslintFlatConfigContent(projectType) {
558
+ /**
559
+ * 检测项目源码是否存在显式 .ts/.tsx/.mts/.cts 扩展名 import(下游 1.9.1 实测:
560
+ * Vite 等 bundler resolution 项目大量使用 `import xxx from "@/types/Foo.ts"`,
561
+ * 全部合法,但默认的 import-x/extensions 强制省略扩展名会让 fresh init 直接
562
+ * 报数百个错误)。命中则生成的配置注入兼容段,fresh init 保持"绿起点"。
563
+ */
564
+ function detectExplicitTsImports() {
565
+ const roots = ["src", "app", "pages", "scripts"].filter((dir) =>
566
+ fs.existsSync(path.resolve(PROJECT_ROOT, dir)),
567
+ )
568
+ const scanRoots = roots.length > 0 ? roots : ["."]
569
+ const exts = new Set([".ts", ".tsx", ".mts", ".cts", ".vue", ".js", ".jsx", ".mjs", ".cjs"])
570
+ const pattern =
571
+ /(?:from\s+|import\s*\(\s*|require\s*\(\s*)["'][^"']+:?[\w@/.-]*\.(?:ts|tsx|mts|cts)["']/
572
+ let scanned = 0
573
+ const walk = (dir) => {
574
+ let entries
575
+ try {
576
+ entries = fs.readdirSync(dir, { withFileTypes: true })
577
+ } catch {
578
+ return false
579
+ }
580
+ for (const entry of entries) {
581
+ if (scanned > 2000) return false
582
+ const full = path.join(dir, entry.name)
583
+ if (entry.isDirectory()) {
584
+ if (
585
+ entry.name === "node_modules" ||
586
+ entry.name === "dist" ||
587
+ entry.name.startsWith(".")
588
+ )
589
+ continue
590
+ if (walk(full)) return true
591
+ } else if (exts.has(path.extname(entry.name))) {
592
+ scanned++
593
+ try {
594
+ if (pattern.test(fs.readFileSync(full, "utf8"))) return true
595
+ } catch {}
596
+ }
597
+ }
598
+ return false
599
+ }
600
+ for (const root of scanRoots) {
601
+ if (walk(path.resolve(PROJECT_ROOT, root))) return true
602
+ }
603
+ return false
604
+ }
605
+
606
+ function eslintFlatConfigContent(projectType, { compatExplicitTsImports = false } = {}) {
559
607
  let importPath = "my-code-style/eslint/flat"
560
608
  let varName = "baseConfig"
561
609
  if (projectType === "uniapp") {
@@ -566,11 +614,24 @@ function eslintFlatConfigContent(projectType) {
566
614
  varName = "vue3Config"
567
615
  }
568
616
 
617
+ const compatBlock = compatExplicitTsImports
618
+ ? `,
619
+ // 兼容存量代码:检测到项目存在显式 .ts/.tsx 扩展名 import(Vite 等 bundler
620
+ // resolution 下合法)。降级为 warn:带与不带扩展名都可写、不再报错阻断,
621
+ // 两种写法的"风格不一致"仍会以警告提示,团队可渐进统一;想恢复严格检查
622
+ // (error + 只允许省略)删除此段即可。
623
+ {
624
+ // 含 vue/nvue:SFC <script setup> 内的显式 .ts import 同样走兼容口径,
625
+ // 否则 .vue 文件会绕过此段仍按 error 报(BUG-038)
626
+ files: ["**/*.{ts,tsx,mts,cts,vue,nvue}"],
627
+ rules: { "import-x/extensions": "warn" },
628
+ }`
629
+ : ""
569
630
  return `// ESLint Flat Config — powered by my-code-style
570
631
  // https://www.npmjs.com/package/my-code-style
571
632
  import ${varName} from "${importPath}"
572
633
 
573
- export default [...${varName}]
634
+ export default [...${varName}${compatBlock}]
574
635
  `
575
636
  }
576
637
 
@@ -837,6 +898,12 @@ function main() {
837
898
  console.log(" 保留已有 ESLint 配置,不生成额外入口")
838
899
  } else if (useFlatConfig) {
839
900
  console.log(" 生成 Flat Config 入口 eslint.config.mjs")
901
+ const compatExplicitTsImports = detectExplicitTsImports()
902
+ if (compatExplicitTsImports) {
903
+ console.log(
904
+ " · 检测到显式 .ts 扩展名 import:已注入兼容段(import-x/extensions 降级为 warn,带/不带扩展名均可写;统一风格时删除该段)",
905
+ )
906
+ }
840
907
  files.push({
841
908
  path: "eslint.config.mjs",
842
909
  exists:
@@ -844,7 +911,7 @@ function main() {
844
911
  fileExists("eslint.config.js") ||
845
912
  fileExists("eslint.config.ts") ||
846
913
  fileExists("eslint.config.cjs"),
847
- content: eslintFlatConfigContent(projectType),
914
+ content: eslintFlatConfigContent(projectType, { compatExplicitTsImports }),
848
915
  })
849
916
  } else {
850
917
  console.log(" 生成传统格式入口 .eslintrc.cjs")
@@ -984,11 +1051,13 @@ function main() {
984
1051
  stepHeader(4, 6, "package.json")
985
1052
  if (dryRun) {
986
1053
  console.log(
987
- " · 将更新 scripts(lint / lint:fix / format / prepare / release / cz)与 lint-staged",
1054
+ " · 将更新 scripts(lint / lint:fix / lint:style / lint:all / format / prepare / release / cz)与 lint-staged",
988
1055
  )
989
1056
  return
990
1057
  }
991
- console.log(" ✓ scripts 已更新(lint / lint:fix / format / prepare / release / cz)")
1058
+ console.log(
1059
+ " ✓ scripts 已更新(lint / lint:fix / lint:style / lint:all / format / prepare / release / cz)",
1060
+ )
992
1061
  console.log(" ✓ lint-staged 已按检测到的 CSS 预处理器生成")
993
1062
  }
994
1063
 
@@ -1156,7 +1225,10 @@ function main() {
1156
1225
  // 配置里排除 node_modules/dist 等,glob 全仓是安全的
1157
1226
  ...(skipStylelint
1158
1227
  ? {}
1159
- : { "lint:style": 'stylelint "**/*.{vue,nvue,html,css,scss,less}"' }),
1228
+ : {
1229
+ "lint:style": 'stylelint "**/*.{vue,nvue,html,css,scss,less}"',
1230
+ "lint:all": `${lintCommand} && stylelint "**/*.{vue,nvue,html,css,scss,less}"`,
1231
+ }),
1160
1232
  format: "prettier --write .",
1161
1233
  prepare: "husky",
1162
1234
  release: "commit-and-tag-version",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "my-code-style",
3
- "version": "1.9.1",
3
+ "version": "1.9.3",
4
4
  "description": "Lint/Format/Git 配置工程化 npm 包(ESLint + Prettier + Stylelint + Commitlint + Husky + standard-version)",
5
5
  "type": "commonjs",
6
6
  "main": "src/eslint/uniapp.cjs",
@@ -101,7 +101,7 @@
101
101
  "stylelint-config-recommended-vue": "^2.0.0",
102
102
  "stylelint-order": "^6.0.0 || ^7.0.0 || ^8.0.0",
103
103
  "stylelint-prettier": "^5.0.0",
104
- "typescript": "^5.0.0",
104
+ "typescript": "^5.0.0 || ^6.0.0",
105
105
  "typescript-eslint": "^8.0.0",
106
106
  "vue-eslint-parser": "^10.3.0"
107
107
  },