// eslint.config.js
import eslint from '@eslint/js';
import tsParser from '@typescript-eslint/parser';
import vueParser from 'vue-eslint-parser';
import stylistic from '@stylistic/eslint-plugin';
import vuePlugin from 'eslint-plugin-vue';
import tsPlugin from '@typescript-eslint/eslint-plugin';
import globals from 'globals';
import importPlugin from 'eslint-plugin-import';
import eslintPluginPrettier from 'eslint-plugin-prettier';
export default [
// 基础 ESLint 规则
eslint.configs.recommended,
// Vue3 规则
{
files: ['**/*.vue'],
languageOptions: {
parser: vueParser,
parserOptions: {
parser: {
ts: tsParser, // 解析 Vue 文件中的 TypeScript
js: 'espree',
},
extraFileExtensions: ['.vue'],
ecmaVersion: 'latest',
sourceType: 'module',
},
},
plugins: {
vue: vuePlugin,
'@stylistic': stylistic,
},
rules: {
...vuePlugin.configs['vue3-recommended'].rules,
// 自定义 Vue 规则
'vue/no-v-html': 'off',
'vue/require-default-prop': 'off',
'vue/require-explicit-emits': 'off',
'vue/multi-word-component-names': 'off',
'vue/html-self-closing': ['error', { html: { void: 'always' } }],
},
},
// TypeScript 规则
{
files: ['**/*.ts', '**/*.tsx'],
languageOptions: {
parser: tsParser,
},
plugins: {
'@typescript-eslint': tsPlugin,
},
rules: {
...tsPlugin.configs.recommended.rules,
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
// '@typescript-eslint/consistent-type-imports': ['error', { disallowTypeAnnotations: false }],
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-this-alias': 'off',
},
},
// 通用 JavaScript/TypeScript 规则
{
files: ['**/*.js', '**/*.ts', '**/*.vue'],
plugins: {
'@stylistic': stylistic,
import: importPlugin,
prettier: eslintPluginPrettier,
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
// 'no-console': ['warn', { allow: ['error'] }],
'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
camelcase: ['error', { properties: 'never' }],
'no-undef': 'off',
'no-var': 'error',
'no-empty': ['error', { allowEmptyCatch: true }],
'no-void': 'error',
'prefer-const': ['warn', { destructuring: 'all', ignoreReadBeforeAssign: true }],
'prefer-template': 'error',
'object-shorthand': ['error', 'always', { ignoreConstructors: false, avoidQuotes: true }],
'block-scoped-var': 'error',
'no-constant-condition': ['error', { checkLoops: false }],
// 代码风格规则
// '@stylistic/indent': ['error', 2],
'@stylistic/quotes': ['error', 'single'],
// '@stylistic/semi': ['error', 'never'],
'@stylistic/comma-dangle': ['error', 'always-multiline'],
// import
'import/first': 'error',
'import/no-duplicates': 'error',
'import/order': [
'error',
{
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
pathGroups: [
{
pattern: 'vue',
group: 'external',
position: 'before',
},
{
pattern: '@vue/**',
group: 'external',
position: 'before',
},
{
pattern: 'ant-design-vue',
group: 'internal',
},
],
pathGroupsExcludedImportTypes: ['type'],
},
],
'import/no-unresolved': [
'error',
{
ignore: ['^@/'], // 忽略以'@/'开头的模块路径
},
],
// prettier
'prettier/prettier': 'error',
},
},
// 全局环境配置
{
languageOptions: {
globals: {
process: 'readable',
__dirname: 'readable',
require: 'readable',
module: 'readable',
...globals.browser,
...globals.es2021,
...globals.node,
},
ecmaVersion: 'latest',
},
ignores: [
'*.sh',
'node_modules',
'*.md',
'*.woff',
'*.ttf',
'.vscode',
'.idea',
'/public',
'/docs',
'.husky',
'.local',
'/bin',
'/src/mock',
'Dockerfile',
'commitlint.config.js',
'‌**/dist/**‌',
'‌**/dist-ssr/**‌',
'‌**/coverage/**‌'
],
},
];
vue3项目eslint@9.20.1 对应的eslint.config.js示例
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...