Vue3+ESLint9.0版本+Prettier 配置:解决格式化冲突与代码规范问题

项目场景:

vue3+eslint9.0+prettier配置

解决方案:

  1. 核心配置文件:eslint.config.jseslint.config.js文件
import { defineConfig, globalIgnores } from 'eslint/config'
import globals from 'globals'
import js from '@eslint/js'
import pluginVue from 'eslint-plugin-vue'
import pluginPrettier from 'eslint-plugin-prettier'
// import skipFormatting from '@vue/eslint-config-prettier/skip-formatting'

export default defineConfig([
  {
    name: 'app/files-to-lint',
    files: ['**/*.{js,mjs,jsx,vue}']
  },

  globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']),

  {
    languageOptions: {
      globals: {
        ...globals.browser
      }
    }
  },

  js.configs.recommended,
  ...pluginVue.configs['flat/essential'],

  // 添加 Prettier 插件和规则
  {
    plugins: { prettier: pluginPrettier },
    rules: {
      // 应用你的 Prettier 配置
      'prettier/prettier': [
        'warn',
        {
          singleQuote: true,
          semi: false,
          printWidth: 80,
          trailingComma: 'none',
          endOfLine: 'auto'
        },
        {
          // 确保 ESLint 不会检查 Prettier 已经处理的格式问题
          usePrettierrc: false,
          // 添加这一行来跳过格式化冲突检查
          skipFormatting: true // 注意:这里应该是布尔值,而不是数组
        }
      ],

      // 自定义 Vue 规则 规范
      'vue/multi-word-component-names': [
        'warn',
        {
          ignores: ['index']
        }
      ],
      'vue/no-setup-props-destructure': 'off',
      'no-undef': 'error'
    }
  }

  // 添加自定义 Vue 规则
  //  ...skipFormatting,
])

  1. VSCode 编辑器配置:setting.json
 "editor.codeActionsOnSave": {
    "source.fixAll": "always",
    "source.fixAll.eslint": "always", 
  },
  "editor.formatOnSave": false,

关键依赖安装:pnpm add --save-dev eslint-plugin-prettier

验证配置效果:pnpm exec eslint src --ext .js,.vue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值