美文网首页
Vue + Eslint + Standard + Husky

Vue + Eslint + Standard + Husky

作者: 孙洪雨 | 来源:发表于2021-04-20 16:43 被阅读0次

    package.json (配置依赖)

      "scripts": {
        "serve": "vue-cli-service serve",
        "build": "vue-cli-service build",
        "lint": "vue-cli-service lint"
      },
      "dependencies": {
        "core-js": "^3.6.5",
        "vue": "^2.6.11"
      },
     "devDependencies": {
        "@vue/cli-plugin-babel": "~4.5.0",
        "@vue/cli-plugin-eslint": "~4.5.0",
        "@vue/cli-service": "~4.5.0",
        "@vue/eslint-config-standard": "^5.1.2",
        "babel-eslint": "^10.1.0",
        "eslint": "^6.7.2",
        "eslint-plugin-import": "^2.20.2",
        "eslint-plugin-node": "^11.1.0",
        "eslint-plugin-promise": "^4.2.1",
        "eslint-plugin-standard": "^4.0.0",
        "eslint-plugin-vue": "^6.2.2",
        "lint-staged": "^9.5.0",
        "vue-template-compiler": "^2.6.11"
     },
      "gitHooks": {
        "pre-commit": "lint-staged"
      },
      "lint-staged": {
        "*.{js,jsx,vue}": [
          "vue-cli-service lint",
          "git add"
        ]
      }
    

    .eslintrc.js (检查规则)

    module.exports = {
      globals: {
        G2: true
      },
      root: true,
      parserOptions: {
        parser: 'babel-eslint'
      },
      env: {
        browser: true,
        node: true,
        es6: true,
      },
      extends: ['plugin:vue/essential','eslint-config-standard'],
    
      /*
       下面这些rules是用来设置从插件来的规范代码的规则,使用必须去掉前缀eslint-plugin-
        主要有如下的设置规则,可以设置字符串也可以设置数字,两者效果一致
        "off" -> 0 关闭规则
        "warn" -> 1 开启警告规则
        "error" -> 2 开启错误规则
      */
      // add your custom rules here
      //it is base on https://github.com/vuejs/eslint-config-vue
      rules: {
        "vue/max-attributes-per-line": [2, {
          "singleline": 10,
          "multiline": {
            "max": 1,
            "allowFirstLine": false
          }
        }],
        "vue/singleline-html-element-content-newline": "off",
        "vue/multiline-html-element-content-newline":"off",
        "vue/name-property-casing": ["error", "PascalCase"],
        "vue/no-v-html": "off",
        'accessor-pairs': 2,                                          // 在对象中使用getter/setter
        'arrow-spacing': [2, {                                        // => 的前/后括号
          'before': true,
          'after': true
        }],
        'block-spacing': [2, 'always'],                               // 块内部通知间隔样式
        'brace-style': [2, '1tbs', {                                  // 大括号风格
          'allowSingleLine': true
        }],
        'camelcase': [0, {                                            // 强制驼峰法命名
          'properties': 'always'
        }],
        'comma-dangle': [2, 'never'],                                 // 对象字面量项尾不能有逗号
        'comma-spacing': [2, {                                        // 逗号前后的空格
          'before': false,
          'after': true
        }],
        'comma-style': [2, 'last'],                                   // 逗号风格,换行时在行首还是行尾
        'constructor-super': 2,                                       // 非派生类不能调用super,派生类必须调用super
        'curly': [2, 'multi-line'],                                   // 必须使用 if(){} 中的{}         
        'dot-location': [2, 'property'],                              // 对象访问符的位置,换行的时候在行首还是行尾
        'eol-last': 2,                                                // 文件以单一的换行符结束
        'eqeqeq': ["error", "always", {"null": "ignore"}],            // 必须使用全等  
        'generator-star-spacing': [2, {                               // 生成器函数*的前后空格  
          'before': true,
          'after': true
        }],
        'handle-callback-err': [2, '^(err|error)$'],                  // nodejs 处理错误
        'indent': 'off',                                              // 缩进风格
        'jsx-quotes': [2, 'prefer-single'],                           // JSX 属性值可以包含字符串文字,禁止用单引号分隔
        'key-spacing': [0, {                                          // 对象字面量中冒号的前后空格
          'beforeColon': false,
          'afterColon': true
        }],
        'keyword-spacing': [2, {                                      // 禁止关键词使用的空格
          'before': true,
          'after': true
        }],
        'new-cap': [2, {                                              // 函数名首行大写必须使用new方式调用,首行小写必须用不带new方式调用
          'newIsCap': true,
          'capIsNew': false
        }],
        'new-parens': 2,                                              // new时必须加小括号
        'no-array-constructor': 2,                                    // 禁止使用数组构造器
        'no-caller': 2,                                               // 禁止使用arguments.caller或arguments.callee
        'no-console': 'off',                                          // 禁止使用console
        'no-class-assign': 2,                                         // 禁止给类赋值
        'no-cond-assign': 2,                                          // 禁止在条件表达式中使用赋值语句
        'no-const-assign': 2,                                         // 禁止修改const声明的变量
        'no-control-regex': 0,                                        // 禁止在正则表达式中使用控制字符 
        'no-delete-var': 2,                                           // 不能对var声明的变量使用delete操作符    
        'no-dupe-args': 2,                                            // 函数参数不能重复
        'no-dupe-class-members': 2,                                   // 禁止在类成员中有相同名称的声明
        'no-dupe-keys': 2,                                            // 在创建对象字面量时不允许键重复 {a:1,a:1}
        'no-duplicate-case': 2,                                       // switch中的case标签不能重复
        'no-empty-character-class': 2,                                // 正则表达式中的[]内容不能为空
        'no-empty-pattern': 2,                                        // 禁止解构结构对象和数组中的任何空模式
        'no-eval': 2,                                                 // 禁止使用eval
        'no-ex-assign': 2,                                            // 禁止给catch语句中的异常参数赋值
        'no-extend-native': 2,                                        // 禁止扩展native对象
        'no-extra-bind': 2,                                           // 禁止不必要的函数绑定 
        'no-extra-boolean-cast': 2,                                   // 禁止不必要的bool转换 
        'no-extra-parens': [2, 'functions'],                          // 禁止非必要的括号 
        'no-fallthrough': 2,                                          // 禁止switch穿透
        'no-floating-decimal': 2,                                     // 禁止省略浮点数中的0 .5 3. 
        'no-func-assign': 2,                                          // 禁止重复的函数声明
        'no-implied-eval': 2,                                         // 禁止使用隐式eval
        'no-inner-declarations': [2, 'functions'],                    // 禁止在块语句中使用声明(变量或函数) 
        'no-invalid-regexp': 2,                                       // 禁止无效的正则表达式
        'no-irregular-whitespace': 2,                                 // 不能有不规则的空格 
        'no-iterator': 2,                                             // 禁止使用__iterator__ 属性   
        'no-label-var': 2,                                            // label名不能与var声明的变量名相同
        'no-labels': [2, {                                            // 禁止标签声明 
          'allowLoop': false,
          'allowSwitch': false
        }],
        'no-lone-blocks': 2,                                          // 禁止不必要的嵌套块 
        'no-mixed-spaces-and-tabs': 2,                                // 禁止混用tab和空格 
        'no-multi-spaces': 1,                                         // 不允许多个空格
        'no-multi-str': 2,                                            // 字符串不能用\换行
        'no-multiple-empty-lines': [1, {                              // 空行最多不能超过3行
          'max': 3
        }],
        'no-native-reassign': 2,                                      // 不能重写native对象
        'no-negated-in-lhs': 2,                                       // in 操作符的左边不能有! 
        'no-new-object': 2,                                           // 禁止使用new Object() 
        'no-new-require': 2,                                          // 禁止使用new require
        'no-new-symbol': 2,                                           // 禁止Symbol与new操作员一起使用
        'no-new-wrappers': 2,                                         // 禁止使用new创建包装实例,new String new Boolean new Numbe
        'no-obj-calls': 2,                                            // 不能调用内置的全局对象,比如Math() JSON()
        'no-octal': 2,                                                // 禁止使用八进制数字   
        'no-octal-escape': 2,                                         // 禁止使用八进制转义序列
        'no-path-concat': 2,                                          // node中不能使用__dirname或__filename做路径拼接
        'no-proto': 2,                                                // 禁止使用__proto__属性
        'no-redeclare': 2,                                            // 禁止重复声明变量
        'no-regex-spaces': 2,                                         // 禁止在正则表达式字面量中使用多个空格 /foo bar/   
        'no-return-assign': [2, 'except-parens'],                     // return 语句中不能有赋值表达式
        'no-self-assign': 2,                                          // 禁止自身分配
        'no-self-compare': 1,                                         // 禁止自身比较
        'no-sequences': 2,                                            // 禁止使用逗号运算符
        'no-shadow-restricted-names': 2,                              // 严格模式中规定的限制标识符不能作为声明时的变量名使用 
        'no-spaced-func': 2,                                          // 函数调用时 函数名与()之间不能有空格
        'no-sparse-arrays': 2,                                        // 禁止稀疏数组, [1,,2]
        'no-this-before-super': 2,                                    // 在调用super()之前不能使用this或super     
        'no-throw-literal': 2,                                        // 禁止抛出字面量错误 throw "error";
        'no-trailing-spaces': 2,                                      // 一行结束后面不要有空格
        'no-undef': 2,                                                // 不能有未定义的变量   
        'no-undef-init': 2,                                           // 变量初始化时不能直接给它赋值为undefined
        'no-unexpected-multiline': 2,                                 // 避免多行表达式
        'no-unmodified-loop-condition': 2,                            // 禁止引用的变量是否在循环中被修改
        'no-unneeded-ternary': [2, {                                  // 禁止不必要的嵌套 var isYes = answer === 1 ? true : false;
          'defaultAssignment': false
        }],
        'no-unreachable': 2,                                          // 不能有无法执行的代码
        'no-unsafe-finally': 2,
        'no-unused-vars': [2, {                                       // 不能有声明后未被使用的变量或参数
          'vars': 'all',
          'args': 'none'
        }],
        'no-useless-call': 2,                                         // 禁止不必要的call和apply
        'no-useless-computed-key': 2,                                 // 禁止不必要地使用计算属性键
        'no-useless-constructor': 2,                                  // 禁止在不改变类的工作方式的情况下安全地移除的类构造函数
        'no-useless-escape': 0,
        'no-whitespace-before-property': 2,                           // 禁止对象的属性位于同一行上,不允许围绕点或在开头括号之前留出空白。
        'no-with': 2,                                                 // 禁用with
        'one-var': [2, {                                              // 连续声明
          'initialized': 'never'                                 
        }],
        'operator-linebreak': [2, 'after', {                          // 换行时运算符在行尾还是行首 
          'overrides': {
            '?': 'before',
            ':': 'before'
          }
        }],
        'padded-blocks': 0,                                           // 块语句内行首行尾是否要空行
        'quotes': [2, 'single', {                                     // 引号类型 `` "" ''
          'avoidEscape': true,
          'allowTemplateLiterals': true
        }],
        'semi': [2, 'never',{
          'beforeStatementContinuationChars': 'never'
        }],                                        
        'semi-spacing': [2, {                                         // 分号前后空格
          'before': false,
          'after': true
        }],
        'space-before-blocks': [2, 'always'],                         // 不以新行开始的块{前面要不要有空格       
        'space-before-function-paren': [2, 'never'],                  // 函数定义时括号前面要不要有空格
        'space-in-parens': [2, 'never'],                              // 小括号里面要不要有空格       
        'space-infix-ops': 2,                                         // 中缀操作符周围要不要有空格
        'space-unary-ops': [2, {                                      // 一元运算符的前/后要不要加空格
          'words': true,
          'nonwords': false
        }],
        'spaced-comment': [2, 'always', {                             // 注释风格要不要有空格什么的  
          'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
        }],
        'template-curly-spacing': [2, 'never'],                       // 保持模板文字内部空间的一致性,不允许大括号内的空格
        'use-isnan': 2,                                               // 禁止比较时使用NaN,只能用isNaN()
        'valid-typeof': 2,                                            // 必须使用合法的typeof的值     
        'wrap-iife': [2, 'any'],                                      // 立即执行函数表达式的小括号风格 
        'yield-star-spacing': [2, 'both'],                            // 强制执行*周围 yield*表达式的间距。    
        'yoda': [2, 'never'],                                         // 禁止尤达条件
        'prefer-const': 2,
        'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, // 禁止使用debugger
        'object-curly-spacing': [2, 'always', {                       // 大括号内是否允许不必要的空格
          objectsInObjects: false
        }],
        'array-bracket-spacing': [2, 'never'],                        // 是否允许非空数组里面有多余的空格
        'vue/html-self-closing': ['off',{
          'html': {
            'void': 'never',
            'normal': 'any',
            'component': 'any'
          },
          'svg': 'any',
          'math': 'any'
        }],
        "vue/html-closing-bracket-spacing": 0,
        "vue/attribute-hyphenation": "error"                          // 强行断字
      }
    }
    
    执行命令
    npm run lint
    

    VSCode插件

    • Eslint
    • Vetur
    • Prettier - Code formatter
    • VScode-Icons
    • Dracula Official

    Settings配置

    {
      "window.zoomLevel": 0.3,
      "workbench.editor.enablePreview": false, // 打开文件不覆盖
      "workbench.sideBar.location": "left",
      "workbench.colorTheme": "Dracula",
      "workbench.iconTheme": "vscode-icons",
      "files.autoSave": "afterDelay", // 打开自动保存
      "search.exclude": { // 搜索忽略内容
        "**/node_modules": true,
        "**/dist": true
      },
      "editor.formatOnType": false, // 在键入一行后是否自动化格式
      "editor.formatOnPaste": true, // 在粘贴时自动格式化
      "editor.minimap.enabled": false, // 关闭快速预览
      "editor.fontFamily": "Consolas, 'Courier New', monospace,'微软雅黑'",
      "editor.fontSize": 16,
      "editor.tabSize": 2,
      "editor.lineHeight": 24, // 设置文字行高
      "editor.lineNumbers": "on", // 开启行数提示
      "editor.quickSuggestions": { // 开启自动显示建议
        "other": true,
        "comments": true,
        "strings": true
      },
      // 自动修复
      "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true,
        "source.fixAll": true
      },
      "eslint.format.enable": true,
      // 配置 ESLint 检查的文件类型
      "eslint.validate": [
        "javascript",
        "vue",
        "html"
      ],
      // #去掉代码结尾的分号 
      "prettier.semi": false,
      // #使用带引号替代双引号 
      "prettier.singleQuote": true,
      "prettier.printWidth": 400, // 指定代码长度,超出换行
      "prettier.requireConfig": true, // 需要 prettier.requireConfig格式化
      "prettier.useEditorConfig": false,
      // 在函数名后面加上括号,类似这种形式 foo () {}
      "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
      "javascript.format.semicolons": "remove",
      "vetur.format.defaultFormatterOptions": {
        "prettier": {
          "singleQuote": true, // 用单引号
          "semi": false, // 不加分号
          "tabWidth": 2, // 会忽略vetur的tabSize配置
          "trailingComma": "none" // 禁止随时添加逗号
        },
        "js-beautify-html": {
          "wrap_line_length": 400, //多少字符换行
          "wrap_attributes": "auto",
          "end_with_newline": false
        }
      },
      "vetur.format.defaultFormatter.html": "prettyhtml",
      "vetur.format.defaultFormatter.css": "prettier",
      "vetur.format.defaultFormatter.postcss": "prettier",
      "vetur.format.defaultFormatter.scss": "prettier",
      "vetur.format.defaultFormatter.less": "prettier",
      "vetur.format.defaultFormatter.stylus": "stylus-supremacy",
      "vetur.format.defaultFormatter.js": "prettier",
      "vetur.format.defaultFormatter.ts": "prettier",
      "vetur.format.defaultFormatter.sass": "sass-formatter",
      "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
      },
      "[vue]": {
        "editor.defaultFormatter": "octref.vetur"
      },
      "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
      }
    }
    

    相关文章

      网友评论

          本文标题:Vue + Eslint + Standard + Husky

          本文链接:https://www.haomeiwen.com/subject/mvenlltx.html