前言

eslint是一種代碼風格管理的工具,可以制定一些代碼編寫規(guī)范,在vue項目中經(jīng)常用到,本文就給大家分享了開發(fā)項目中使用的eslint校驗規(guī)范,供參考:
示例代碼
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.規(guī)范 /////
///////////////////
"vue/max-attributes-per-line": [1, { //多個特性的元素應該分多行撰寫,每個特性一行
"singleline": 10,
"multiline": {
"max": 1,
"allowFirstLine": false
}
}],
"vue/singleline-html-element-content-newline": 0, // 在單行元素的內(nèi)容前后需要換行符
"vue/multiline-html-element-content-newline": 0, // 在多行元素的內(nèi)容之前和之后需要換行符
"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結(jié)合使用,可以高效的更新虛擬DOM
"vue/no-use-v-if-with-v-for": [2, {
"allowUsingIterationVar": false
}], // 不要把 v-if 和 v-for 用在同一個元素上——因為v-for 比 v-if 具有更高的優(yōu)先級
"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.規(guī)范 /////
///////////////////
'accessor-pairs': 2, // 在對象中強制使用getter/setter
'arrow-spacing': [2, { // 在箭頭函數(shù)之前/之后需要空格
'before': true,
'after': true
}],
'block-spacing': [2, 'always'], // 在打開塊之后和關(guān)閉塊之前,禁止或強制執(zhí)行塊內(nèi)部的空格
'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, // 構(gòu)建方法中使用super方法
'curly': [2, 'multi-line'],
'dot-location': [1, 'property'], // 在dot之前和之后強制換行
'eol-last': 2, // 在文件末尾要求或禁止換行
'eqeqeq': [0, "always", { "null": "ignore" }], // 是否使用全等
'generator-star-spacing': [2, { // 在生成器函數(shù)中強制執(zhí)行*周圍的間距
'before': true,
'after': true
}],
'handle-callback-err': [1, '^(err|error)$'], // 強制執(zhí)行回調(diào)錯誤處理
'indent': [2, 2, { // 強制執(zhí)行一致的縮進
'SwitchCase': 1
}],
'jsx-quotes': [2, 'prefer-single'], // 強制在JSX文件中一致使用單引號
'key-spacing': [2, { // 在對象屬性中強制鍵和值之間的一致間距
'beforeColon': false,
'afterColon': true
}],
'keyword-spacing': [2, { // 關(guān)鍵字前后強制執(zhí)行一致的間距
'before': true,
'after': true
}],
'new-cap': [2, { // 要求構(gòu)造函數(shù)名稱以大寫字母開頭
'newIsCap': true,
'capIsNew': false
}],
'new-parens': 2, // 調(diào)用不帶參數(shù)的函數(shù)時需要括號
'no-array-constructor': 2, // 禁止陣列構(gòu)建器
'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, // 在函數(shù)定義中禁止重復參數(shù)
'no-dupe-class-members': 2, // 禁止在類成員中重復名稱
'no-dupe-keys': 2, // 禁止對象重復聲明屬性
'no-duplicate-case': 2, // 規(guī)則禁止重復案例標簽
'no-empty-character-class': 2, // 禁止在正則表達式中使用空字符類
'no-empty-pattern': 2, // 不允許空的解構(gòu)模式
'no-eval': 2, // 禁止使用eval()
'no-ex-assign': 2, // 禁止在catch子句中重新分配異常
'no-extend-native': 2, // 禁止擴展原生對象
'no-extra-bind': 2, // 禁止不必要的功能綁定
'no-extra-boolean-cast': 2, // 禁止不必要的布爾類型轉(zhuǎn)換
'no-extra-parens': [2, 'functions'], // 禁止不必要的括號
'no-fallthrough': 2, // 禁止太多陳述描述
'no-floating-decimal': 2, // 禁止浮動小數(shù)
'no-func-assign': 2, // 禁止重新分配函數(shù)聲明
'no-implied-eval': 2,
'no-inner-declarations': [2, 'functions'], // 禁止嵌套塊中的變量或函數(shù)聲明
'no-invalid-regexp': 2, // 禁止在RegExp中使用無效的正則表達式字符串
'no-irregular-whitespace': 2, // 不允許不規(guī)則的空白
'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, // 禁止未修改的循環(huán)條件
'no-unneeded-ternary': [2, { // 當存在更簡單的替代方案時,不允許三元運算符
'defaultAssignment': false
}],
'no-unreachable': 2, // 返回,拋出,繼續(xù)和中斷語句后禁止無法訪問的代碼
'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, // 禁止不必要的構(gòu)造方法
'no-useless-escape': 0, // 禁止不必要的轉(zhuǎn)義用法
'no-whitespace-before-property': 2, // 在屬性之前禁止空格
'no-with': 2,
'one-var': [2, {
'initialized': 'never'
}],
'operator-linebreak': [2, 'after', { // 為維護強制執(zhí)行一致的換行方式
'overrides': {
'?': 'before',
':': 'before'
}
}],
'padded-blocks': [2, 'never'], // 在塊內(nèi)要求或禁止填充
'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']
}
}另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章名稱:Vue項目中ESlint規(guī)范示例代碼-創(chuàng)新互聯(lián)
標題URL:http://chinadenli.net/article42/cedsec.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、定制網(wǎng)站、網(wǎng)站排名、網(wǎng)頁設計公司、網(wǎng)站建設、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容