1. 安装

      1. npm i eslint -D
    2. 在项目根目录中使用命令行初始化

      1. npx eslint --init
      2. #√ How would you like to use ESLint? · style
      3. #√ What type of modules does your project use? · esm
      4. #√ Which framework does your project use? · vue
      5. #√ Does your project use TypeScript? · No
      6. #√ Where does your code run? · node
      7. #√ How would you like to define a style for your project? · guide
      8. #√ Which style guide do you want to follow? · standard
      9. #√ What format do you want your config file to be in? · JavaScript
      10. #√ Would you like to install them now? · Yes
      11. #√ Which package manager do you want to use? · npm
    3. 在npm scripts中添加验证脚本

      1. {
      2. "lint": "eslint ./src/**/*.{js,jsx,vue} --fix"
      3. }
    4. 如果使用的是vue3,则需要在配置文件中修改extends下的plugin为vue/vue3-strongly-recommend,默认的essential是vue2的语法规则

    5. 打开IDE的ESLint检查与保存时自动修复
    6. 使用eslint-disable-next-line可以忽略下一行的语法检查
    7. 配置模板

      1. module.exports = {
      2. root: true,
      3. parserOptions: {
      4. parser: 'babel-eslint',
      5. sourceType: 'module'
      6. },
      7. env: {
      8. browser: true,
      9. node: true,
      10. es6: true,
      11. },
      12. extends: ['plugin:vue/recommended', 'eslint:recommended'],
      13. // add your custom rules here
      14. //it is base on https://github.com/vuejs/eslint-config-vue
      15. rules: {
      16. 'camelcase': [0, {
      17. 'properties': 'always'
      18. }], //强制驼峰法命名 - 关闭
      19. 'eol-last': 0, //文件以单一的换行符结束 - 打开
      20. 'eqeqeq': ["error", "always", {
      21. "null": "ignore"
      22. }], //必须使用全等 0关 1警告 2或者error错误
      23. 'no-const-assign': 2, //禁止修改const声明的变量 - 开启
      24. "no-empty": 0, //块语句中的内容不能为空 - 关闭
      25. 'no-spaced-func': 2, //函数调用时 函数名与()之间不能有空格 - 开启
      26. 'no-undef': 2, //不能有未定义的变量 - 打开
      27. 'no-unused-vars': [2, {
      28. 'vars': 'all',
      29. 'args': 'none'
      30. }], //不能有声明后未被使用的变量或参数 0关 1警告 2声明不使用就爆红
      31. "vue/html-self-closing": ['error', {
      32. "html": {
      33. "void": "never",
      34. "normal": "any",
      35. "component": "any"
      36. },
      37. "svg": "always",
      38. "math": "always"
      39. }], //html使用单闭合标签 -打开(配置:在html中never从不开启)
      40. 'space-before-function-paren': [0, 'never'], //函数定义时括号前面要不要有空格:设置为0就是关闭这个规则的检验 1就是如不符合规则就警告 2就是如不符合就报错 --至于保存代码时候 是否会生成空格 这个取决于你设置的代码风格(例如prettier)
      41. 'prefer-const': 0, //首选const -- 关闭(就可以用var let const)
      42. 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
      43. 'object-curly-spacing': [2, 'always', {
      44. objectsInObjects: false
      45. }],
      46. 'array-bracket-spacing': [2, 'never']
      47. }
      48. }