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 (檢查規(guī)則)
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是用來設置從插件來的規(guī)范代碼的規(guī)則壹将,使用必須去掉前綴eslint-plugin-
主要有如下的設置規(guī)則逐抑,可以設置字符串也可以設置數(shù)字暖途,兩者效果一致
"off" -> 0 關閉規(guī)則
"warn" -> 1 開啟警告規(guī)則
"error" -> 2 開啟錯誤規(guī)則
*/
// 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'], // 塊內(nèi)部通知間隔樣式
'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, // 非派生類不能調(diào)用super垦梆,派生類必須調(diào)用super
'curly': [2, 'multi-line'], // 必須使用 if(){} 中的{}
'dot-location': [2, 'property'], // 對象訪問符的位置画拾,換行的時候在行首還是行尾
'eol-last': 2, // 文件以單一的換行符結束
'eqeqeq': ["error", "always", {"null": "ignore"}], // 必須使用全等
'generator-star-spacing': [2, { // 生成器函數(shù)*的前后空格
'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, { // 函數(shù)名首行大寫必須使用new方式調(diào)用夺谁,首行小寫必須用不帶new方式調(diào)用
'newIsCap': true,
'capIsNew': false
}],
'new-parens': 2, // new時必須加小括號
'no-array-constructor': 2, // 禁止使用數(shù)組構造器
'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, // 函數(shù)參數(shù)不能重復
'no-dupe-class-members': 2, // 禁止在類成員中有相同名稱的聲明
'no-dupe-keys': 2, // 在創(chuàng)建對象字面量時不允許鍵重復 {a:1,a:1}
'no-duplicate-case': 2, // switch中的case標簽不能重復
'no-empty-character-class': 2, // 正則表達式中的[]內(nèi)容不能為空
'no-empty-pattern': 2, // 禁止解構結構對象和數(shù)組中的任何空模式
'no-eval': 2, // 禁止使用eval
'no-ex-assign': 2, // 禁止給catch語句中的異常參數(shù)賦值
'no-extend-native': 2, // 禁止擴展native對象
'no-extra-bind': 2, // 禁止不必要的函數(shù)綁定
'no-extra-boolean-cast': 2, // 禁止不必要的bool轉(zhuǎn)換
'no-extra-parens': [2, 'functions'], // 禁止非必要的括號
'no-fallthrough': 2, // 禁止switch穿透
'no-floating-decimal': 2, // 禁止省略浮點數(shù)中的0 .5 3.
'no-func-assign': 2, // 禁止重復的函數(shù)聲明
'no-implied-eval': 2, // 禁止使用隱式eval
'no-inner-declarations': [2, 'functions'], // 禁止在塊語句中使用聲明(變量或函數(shù))
'no-invalid-regexp': 2, // 禁止無效的正則表達式
'no-irregular-whitespace': 2, // 不能有不規(guī)則的空格
'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創(chuàng)建包裝實例廉赔,new String new Boolean new Numbe
'no-obj-calls': 2, // 不能調(diào)用內(nèi)置的全局對象,比如Math() JSON()
'no-octal': 2, // 禁止使用八進制數(shù)字
'no-octal-escape': 2, // 禁止使用八進制轉(zhuǎn)義序列
'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, // 嚴格模式中規(guī)定的限制標識符不能作為聲明時的變量名使用
'no-spaced-func': 2, // 函數(shù)調(diào)用時 函數(shù)名與()之間不能有空格
'no-sparse-arrays': 2, // 禁止稀疏數(shù)組匾鸥, [1,,2]
'no-this-before-super': 2, // 在調(diào)用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, // 禁止引用的變量是否在循環(huán)中被修改
'no-unneeded-ternary': [2, { // 禁止不必要的嵌套 var isYes = answer === 1 ? true : false;
'defaultAssignment': false
}],
'no-unreachable': 2, // 不能有無法執(zhí)行的代碼
'no-unsafe-finally': 2,
'no-unused-vars': [2, { // 不能有聲明后未被使用的變量或參數(shù)
'vars': 'all',
'args': 'none'
}],
'no-useless-call': 2, // 禁止不必要的call和apply
'no-useless-computed-key': 2, // 禁止不必要地使用計算屬性鍵
'no-useless-constructor': 2, // 禁止在不改變類的工作方式的情況下安全地移除的類構造函數(shù)
'no-useless-escape': 0,
'no-whitespace-before-property': 2, // 禁止對象的屬性位于同一行上蜡塌,不允許圍繞點或在開頭括號之前留出空白。
'no-with': 2, // 禁用with
'one-var': [2, { // 連續(xù)聲明
'initialized': 'never'
}],
'operator-linebreak': [2, 'after', { // 換行時運算符在行尾還是行首
'overrides': {
'?': 'before',
':': 'before'
}
}],
'padded-blocks': 0, // 塊語句內(nèi)行首行尾是否要空行
'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'], // 函數(shù)定義時括號前面要不要有空格
'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'], // 保持模板文字內(nèi)部空間的一致性勿负,不允許大括號內(nèi)的空格
'use-isnan': 2, // 禁止比較時使用NaN馏艾,只能用isNaN()
'valid-typeof': 2, // 必須使用合法的typeof的值
'wrap-iife': [2, 'any'], // 立即執(zhí)行函數(shù)表達式的小括號風格
'yield-star-spacing': [2, 'both'], // 強制執(zhí)行*周圍 yield*表達式的間距。
'yoda': [2, 'never'], // 禁止尤達條件
'prefer-const': 2,
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, // 禁止使用debugger
'object-curly-spacing': [2, 'always', { // 大括號內(nèi)是否允許不必要的空格
objectsInObjects: false
}],
'array-bracket-spacing': [2, 'never'], // 是否允許非空數(shù)組里面有多余的空格
'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" // 強行斷字
}
}
執(zhí)行命令
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": { // 搜索忽略內(nèi)容
"**/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", // 開啟行數(shù)提示
"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,
// 在函數(shù)名后面加上括號琅摩,類似這種形式 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"
}
}