Vue項目中ESlint規範

開發項目中使用的eslint校驗規範,供參考:
module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint',
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  extends: ['plugin:vue/essential', 'eslint:recommended'],
  rules: {
    // 等級分爲三級: 0-不顯示;1-顯示警告warning; 2-顯示錯誤error

    ////////////////////
    ///// vue.規範 /////
    ///////////////////
    "vue/max-attributes-per-line": [1, {              //多個特性的元素應該分多行撰寫,每一個特性一行
      "singleline": 10,
      "multiline": {
        "max": 1,
        "allowFirstLine": false
      }
    }],
    "vue/singleline-html-element-content-newline": 0, // 在單行元素的內容先後須要換行符
    "vue/multiline-html-element-content-newline": 0,  // 在多行元素的內容以前和以後須要換行符
    "vue/name-property-casing": [1, "PascalCase"],    // JS/JSX中的組件名應該始終是帕斯卡命名法
    "vue/no-v-html": 0,
    "vue/prop-name-casing": [1, "camelCase"],         // 在聲明prop的時候,其命名應該始終使用駝峯命名
    "vue/require-v-for-key": 1,                       // 給v-for設置鍵值,與key結合使用,能夠高效的更新虛擬DOM
    "vue/no-use-v-if-with-v-for": [2, {
      "allowUsingIterationVar": false
    }],                                               // 不要把 v-if 和 v-for 用在同一個元素上——由於v-for 比 v-if 具備更高的優先級
    "vue/order-in-components": [0, {                  // 組件/實例的選項的順序
      "order": [
        "el",
        "name",
        "parent",
        "functional",
        ["delimiters", "comments"],
        ["components", "directives", "filters"],
        "extends",
        "mixins",
        "inheritAttrs",
        "model",
        ["props", "propsData"],
        "data",
        "computed",
        "watch",
        "LIFECYCLE_HOOKS",
        "methods",
        ["template", "render"],
        "renderError"
      ]
    }],

    ////////////////////
    ///// js.規範 /////
    ///////////////////
    'accessor-pairs': 2,              // 在對象中強制使用getter/setter
    'arrow-spacing': [2, {            // 在箭頭函數以前/以後須要空格
      'before': true,
      'after': true
    }],
    'block-spacing': [2, 'always'],   // 在打開塊以後和關閉塊以前,禁止或強制執行塊內部的空格
    'brace-style': [1, '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方法
    'curly': [2, 'multi-line'],       
    'dot-location': [1, 'property'],  // 在dot以前和以後強制換行
    'eol-last': 2,                    // 在文件末尾要求或禁止換行
    'eqeqeq': [0, "always", { "null": "ignore" }], // 是否使用全等 
    'generator-star-spacing': [2, {   // 在生成器函數中強制執行*周圍的間距
      'before': true,
      'after': true
    }],
    'handle-callback-err': [1, '^(err|error)$'], // 強制執行回調錯誤處理
    'indent': [2, 2, {                  // 強制執行一致的縮進
      'SwitchCase': 1
    }],
    'jsx-quotes': [2, 'prefer-single'], // 強制在JSX文件中一導致用單引號
    'key-spacing': [2, {                // 在對象屬性中強制鍵和值之間的一致間距
      'beforeColon': false,
      'afterColon': true
    }],
    'keyword-spacing': [2, {            // 關鍵字先後強制執行一致的間距
      'before': true,
      'after': true
    }],
    'new-cap': [2, {                    // 要求構造函數名稱以大寫字母開頭
      'newIsCap': true,
      'capIsNew': false
    }],
    'new-parens': 2,                    // 調用不帶參數的函數時須要括號
    'no-array-constructor': 2,          // 禁止陣列構建器
    'no-caller': 2,                     // 禁止使用來電者/被叫者
    'no-console': 'off',                // 不容許使用控制檯
    'no-class-assign': 2,               // 禁止修改類聲明的變量
    'no-cond-assign': 2,                // 在條件語句中禁止賦值運算符
    'no-const-assign': 2,               // 禁止修改使用const聲明的變量
    'no-control-regex': 0,              // 禁止正則表達式中的控制字符
    'no-delete-var': 2,                 // 禁止刪除變量
    'no-dupe-args': 2,                  // 在函數定義中禁止重複參數 
    'no-dupe-class-members': 2,         // 禁止在類成員中重複名稱
    'no-dupe-keys': 2,                  // 禁止對象重複聲明屬性
    'no-duplicate-case': 2,             // 規則禁止重複案例標籤
    'no-empty-character-class': 2,      // 禁止在正則表達式中使用空字符類
    'no-empty-pattern': 2,              // 不容許空的解構模式
    'no-eval': 2,                       // 禁止使用eval()
    'no-ex-assign': 2,                  // 禁止在catch子句中從新分配異常
    'no-extend-native': 2,              // 禁止擴展原生對象
    'no-extra-bind': 2,                 // 禁止沒必要要的功能綁定
    'no-extra-boolean-cast': 2,         // 禁止沒必要要的布爾類型轉換
    'no-extra-parens': [2, 'functions'], // 禁止沒必要要的括號
    'no-fallthrough': 2,                 // 禁止太多陳述描述
    'no-floating-decimal': 2,            // 禁止浮動小數
    'no-func-assign': 2,                 // 禁止從新分配函數聲明 
    'no-implied-eval': 2,                
    'no-inner-declarations': [2, 'functions'],  // 禁止嵌套塊中的變量或函數聲明
    'no-invalid-regexp': 2,              // 禁止在RegExp中使用無效的正則表達式字符串
    'no-irregular-whitespace': 2,        // 不容許不規則的空白
    'no-iterator': 2,                    // 禁止迭代器
    'no-label-var': 2,                   // 禁止變量名稱的標籤
    'no-labels': [2, {                   
      'allowLoop': false,
      'allowSwitch': false
    }],
    'no-lone-blocks': 2,                 // 禁止沒必要要的嵌套塊
    'no-mixed-spaces-and-tabs': 2,       // 禁止使用混合空格和製表符進行縮進
    'no-multi-spaces': 2,                // 禁止多個空格
    'no-multi-str': 2,                   // 禁止多行字符串
    'no-multiple-empty-lines': [2, {     // 禁止多個空行
      'max': 1
    }],
    'no-native-reassign': 2,               
    'no-negated-in-lhs': 2,
    'no-new-object': 2,
    'no-new-require': 2,
    'no-new-symbol': 2,
    'no-new-wrappers': 2,
    'no-obj-calls': 2,
    'no-octal': 2,
    'no-octal-escape': 2,
    'no-path-concat': 2,
    'no-proto': 2,
    'no-redeclare': 2,
    'no-regex-spaces': 2,
    'no-return-assign': [2, 'except-parens'],
    'no-self-assign': 2,
    'no-self-compare': 2,
    'no-sequences': 2,
    'no-shadow-restricted-names': 2,
    'no-spaced-func': 2,
    'no-sparse-arrays': 2,
    'no-this-before-super': 2,
    'no-throw-literal': 2,
    'no-trailing-spaces': 2,
    'no-undef': 0,
    'no-undef-init': 2,
    'no-unexpected-multiline': 2,
    'no-unmodified-loop-condition': 2,    // 禁止未修改的循環條件
    'no-unneeded-ternary': [2, {          // 當存在更簡單的替代方案時,不容許三元運算符
      'defaultAssignment': false
    }],
    'no-unreachable': 2,                  // 返回,拋出,繼續和中斷語句後禁止沒法訪問的代碼
    'no-unsafe-finally': 2,               // 禁止finally塊中的控制流語句
    'no-unused-vars': [1, {               // 禁止使用未聲明的變量
      '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,
    'one-var': [2, {
      'initialized': 'never'
    }],
    'operator-linebreak': [2, 'after', {  // 爲維護強制執行一致的換行方式
      'overrides': {
        '?': 'before',
        ':': 'before'
      }
    }],
    'padded-blocks': [2, 'never'],         // 在塊內要求或禁止填充
    'quotes': [2, 'single', {
      'avoidEscape': true,
      'allowTemplateLiterals': true
    }],
    'semi': [2, '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,
    'valid-typeof': 2,
    'wrap-iife': [2, 'any'],
    'yield-star-spacing': [2, 'both'],
    'yoda': [2, 'never'],
    'prefer-const': 1,
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
    'object-curly-spacing': [2, 'always', {
      objectsInObjects: false
    }],
    'array-bracket-spacing': [2, 'never']
  }
}
複製代碼
相關文章
相關標籤/搜索