eslint.config.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // 引入vue模版的eslint
  2. import pluginVue from 'eslint-plugin-vue'
  3. import eslint from '@eslint/js'
  4. // ts-eslint解析器,使 eslint 可以解析 ts 语法
  5. import tseslint from 'typescript-eslint'
  6. // vue文件解析器
  7. import vueParser from 'vue-eslint-parser'
  8. import prettier from 'eslint-plugin-prettier'
  9. export default tseslint.config({
  10. // ignores: ['node_modules', 'prettier.config.cjs', 'dist*'],
  11. files: ['src/**/*.ts', 'src/**/*.tsx', 'src/**/*.vue'],
  12. // tseslint.config添加了extends扁平函数,直接用。否则是eslint9.0版本是没有extends的
  13. extends: [eslint.configs.recommended, ...tseslint.configs.recommended, ...pluginVue.configs['flat/essential']],
  14. plugins: {
  15. prettier
  16. },
  17. languageOptions: {
  18. parser: vueParser, // 使用vue解析器,这个可以识别vue文件
  19. parserOptions: {
  20. parser: tseslint.parser, // 在vue文件上使用ts解析器
  21. sourceType: 'module',
  22. ecmaVersion: 2020,
  23. jsxPragma: 'React',
  24. ecmaFeatures: {
  25. jsx: true
  26. }
  27. }
  28. },
  29. rules: {
  30. // eslint (http://eslint.cn/docs/rules)
  31. 'no-useless-escape': 0, // 关闭不必要的转义
  32. 'no-var': 'error', // 要求使用 let 或 const 而不是 var
  33. 'no-multiple-empty-lines': ['error', { max: 1 }], // 不允许多个空行
  34. 'prefer-const': 1, // 使用 let 关键字声明但在初始分配后从未重新分配的变量,要求使用 const
  35. 'no-use-before-define': 0, // 禁止在 函数/类/变量 定义之前使用它们
  36. 'prettier/prettier': [
  37. 'warn',
  38. {
  39. singleQuote: true,
  40. semi: false
  41. }
  42. ],
  43. // typeScript (https://typescript-eslint.io/rules)
  44. '@typescript-eslint/no-unused-vars': 0, // 禁止定义未使用的变量
  45. '@typescript-eslint/no-empty-function': 0, // 禁止空函数
  46. 'no-empty': 0, // 判据可以为空
  47. '@typescript-eslint/prefer-ts-expect-error': 'error', // 禁止使用 @ts-ignore
  48. '@typescript-eslint/ban-ts-comment': 'error', // 禁止 @ts-<directive> 使用注释或要求在指令后进行描述
  49. '@typescript-eslint/no-inferrable-types': 0, // 可以轻松推断的显式类型可能会增加不必要的冗长
  50. '@typescript-eslint/no-namespace': 0, // 禁止使用自定义 TypeScript 模块和命名空间
  51. '@typescript-eslint/no-explicit-any': 0, // 禁止使用 any 类型
  52. '@typescript-eslint/ban-types': 0, // 禁止使用特定类型
  53. '@typescript-eslint/no-var-requires': 0, // 允许使用 require() 函数导入模块
  54. '@typescript-eslint/no-non-null-assertion': 0, // 不允许使用后缀运算符的非空断言(!)
  55. '@typescript-eslint/no-unused-expressions': 0, // 方法可以不返回
  56. // vue (https://eslint.vuejs.org/rules)
  57. // 'vue/script-setup-uses-vars': 'error', // 防止<script setup>使用的变量<template>被标记为未使用,此规则仅在启用该 no-unused-vars 规则时有效
  58. 'vue/v-slot-style': 'error', // 强制执行 v-slot 指令样式
  59. 'vue/no-mutating-props': 'error', // 不允许改变组件 prop
  60. 'vue/custom-event-name-casing': 'error', // 为自定义事件名称强制使用特定大小写
  61. 'vue/attribute-hyphenation': 'error', // 对模板中的自定义组件强制执行属性命名样式:my-prop="prop"
  62. 'vue/attributes-order': 0, // vue api使用顺序,强制执行属性顺序
  63. 'vue/no-v-html': 0, // 禁止使用 v-html
  64. 'vue/require-default-prop': 0, // 此规则要求为每个 prop 为必填时,必须提供默认值
  65. 'vue/multi-word-component-names': 0, // 要求组件名称始终为 “-” 链接的单词
  66. 'vue/no-setup-props-destructure': 0, // 禁止解构 props 传递给 setup
  67. // singleline...单行元素的配置。如果元素没有属性或最后一个属性与左括号位于同一行,则它是单行元素。"never"(默认)...不允许在右括号前换行。"always"...要求在右括号前有一个换行符。
  68. // multiline...多行元素的配置。如果最后一个属性不在左括号的同一行上,则它是一个多行元素。"never"...不允许在右括号前换行。"always"(默认)...要求在右括号前有一个换行符。
  69. 'vue/html-closing-bracket-newline': [
  70. 'error',
  71. {
  72. singleline: 'never',
  73. multiline: 'never'
  74. }
  75. ]
  76. }
  77. })