From 3d9f87d02bb033ee30b353b5336e6685afab03c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?mac=C2=B7ufutx?= Date: Mon, 9 Jun 2025 17:10:41 +0800 Subject: [PATCH] 20250609 eslint V8 --- .eslintignore | 8 ++- .eslintrc.cjs | 111 +++++++++++++++++----------------- .husky/pre-commit | 1 - package.json | 14 +++-- src/App.vue | 3 + src/assets/vue.svg | 7 ++- src/components/HelloWorld.vue | 26 ++++---- src/style.css | 99 ++++++++++++++++-------------- tsconfig.app.json | 26 ++++++-- tsconfig.eslint.json | 4 ++ tsconfig.node.json | 12 +++- 11 files changed, 179 insertions(+), 132 deletions(-) create mode 100644 tsconfig.eslint.json diff --git a/.eslintignore b/.eslintignore index c7b44f5..c5eaaa7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,4 +4,10 @@ dist public *.d.ts .vscode -.husky \ No newline at end of file +.husky +# 忽略目录 +coverage/ + +# 忽略文件 +*.d.ts +*.log \ No newline at end of file diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 20c755f..7c27f6e 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,60 +1,59 @@ // .eslintrc.cjs +const path = require('path') + module.exports = { - root: true, - env: { - browser: true, - node: true, - es6: true, - }, - extends: [ - 'eslint:recommended', - 'plugin:vue/vue3-recommended', // Vue 3 推荐规则 - '@vue/eslint-config-typescript', // TypeScript 支持 - 'prettier', // 关闭与 Prettier 冲突的规则(需先安装 prettier) - 'plugin:prettier/recommended', // 将 Prettier 规则作为 ESLint 规则 + root: true, + env: { + browser: true, + node: true, + es6: true + }, + extends: [ + 'eslint:recommended', + 'plugin:vue/vue3-recommended', // 加载 Vue 3 推荐规则 + '@vue/eslint-config-typescript', // 加载 TypeScript 支持 + 'plugin:prettier/recommended' // 确保放在最后 + ], + parser: 'vue-eslint-parser', // 解析 Vue 模板 + parserOptions: { + parser: '@typescript-eslint/parser', // 解析 TypeScript 代码 + project: path.resolve(__dirname, 'tsconfig.app.json'), // 使用正确的 TSConfig + tsconfigRootDir: __dirname, + ecmaVersion: 'latest', + sourceType: 'module' + }, + plugins: ['vue', 'prettier', '@typescript-eslint'], // 显式注册所有插件 + rules: { + 'vue/no-undef-components': 'error', + // 针对非 Vue 文件禁用 Vue 规则 + // 'vue/no-unregistered-components': [ + // 'error', + // { ignorePatterns: ['^router$', '^store$'] } // 可选:忽略特定组件名称 + // ], + 'vue/multi-word-component-names': [ + 'error', + { + ignores: ['Test'] // 可选:忽略特定组件名 + } ], - parserOptions: { - ecmaVersion: 'latest', - sourceType: 'module', - ecmaFeatures: { - jsx: true, - }, - }, - rules: { - // 自定义规则(根据团队需求调整) - 'vue/multi-word-component-names': 'off', - 'prettier/prettier': 'error', // 将 Prettier 错误视为 ESLint 错误 - // 强制使用 === 而非 == - 'eqeqeq': 'error', - // 组件 props 必须有类型和默认值 - 'vue/require-default-prop': 'error', - 'vue/require-prop-types': 'error', - - // 限制每行最大长度 - 'max-len': ['warn', {code: 120}], - - // 禁止使用未定义的组件 - 'vue/no-unregistered-components': 'error', - - // 组件名必须为 PascalCase - 'vue/component-name-in-template-casing': ['error', 'PascalCase'], - - // 禁止使用 v-html(防止 XSS) - 'vue/no-v-html': 'warn', - // 自定义规则(根据团队需求调整) - 'vue/multi-word-component-names': 'off', // 允许单字组件名 - 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', - 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', - '@typescript-eslint/no-unused-vars': ['warn', {argsIgnorePattern: '^_'}], - }, - overrides: [ - // 针对测试文件的规则 - { - files: ['**/__tests__/*.{j,t}s?(x)', '**/tests/unit/**/*.spec.{j,t}s?(x)'], - env: { - jest: true, - }, - }, - ], -} \ No newline at end of file + // 其他自定义规则 + 'prettier/prettier': 'error', + '@typescript-eslint/no-unused-vars': 'warn', + 'max-len': 'off' + }, + overrides: [ + // { + // files: ['*.vue'], // 仅对 Vue 文件应用 Vue 规则 + // rules: { + // 'vue/no-unregistered-components': 'error' + // } + // }, + { + files: ['*.ts', '*.js'], // 非 Vue 文件不应用 Vue 规则 + rules: { + 'vue/no-unregistered-components': 'off' + } + } + ] +} diff --git a/.husky/pre-commit b/.husky/pre-commit index e1c12eb..d24fdfc 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npm test npx lint-staged diff --git a/package.json b/package.json index d63a614..6a58dcc 100644 --- a/package.json +++ b/package.json @@ -26,21 +26,23 @@ }, "devDependencies": { "@types/axios": "^0.9.36", - "@typescript-eslint/eslint-plugin": "^8.33.1", - "@typescript-eslint/parser": "^8.33.1", + "@typescript-eslint/eslint-plugin": "5.62.0", + "@typescript-eslint/parser": "5.62.0", "@vitejs/plugin-vue": "^4.2.3", - "@vue/eslint-config-typescript": "^14.5.0", + "@vue/eslint-config-typescript": "12.0.0", "@vue/tsconfig": "^0.7.0", "autoprefixer": "^10.4.14", - "eslint": "^9.28.0", - "eslint-plugin-vue": "^10.2.0", + "eslint": "8.57.1", + "eslint-config-prettier": "9.1.0", + "eslint-plugin-prettier": "^5.0.0", + "eslint-plugin-vue": "^9.0.0", "husky": "^8.0.0", "less": "^4.3.0", "less-loader": "^12.3.0", "lint-staged": "^16.1.0", "postcss": "^8.4.24", "prettier": "^3.5.3", - "typescript": "~5.8.3", + "typescript": "5.1.6", "vite": "^4.4.9", "vite-plugin-image-optimizer": "^1.1.8", "vue-tsc": "^2.2.8" diff --git a/src/App.vue b/src/App.vue index 58b0f21..87c6450 100644 --- a/src/App.vue +++ b/src/App.vue @@ -4,6 +4,7 @@ import HelloWorld from './components/HelloWorld.vue'