vscode和eslint的配置極其繁瑣,為了不勞民傷財是目,本著救天下程序員于水深火熱之中的宏愿谤饭,地獄不空,誓不成佛懊纳,特施恩于爾等揉抵。(怎么就沒表情符號可用呢,這讓我如何嘚瑟嗤疯?看來只能說:你來打我撒T┙瘛)
eslint與vscode相輔相成,再也不怕代碼不規(guī)范了茂缚,任你怎么亂寫戏罢,保存代碼后可手動或是自動格式化代碼!
一阱佛、需要用到的vscode插件:
1帖汞、ESLint,Vetur(必須)
2凑术、以下插件就是自己的喜好羅翩蘸,有助于美化樣式和輔助提示(用了才知道,那個不痛淮逊,月月輕松4呤住)。
Auto Close Tag(自動標(biāo)簽閉合)泄鹏,
Auto Rename Tag(標(biāo)簽重命名)郎任,
Bracket Pair Colorizer 2(成對括號顏色標(biāo)記),
GitLens--Git supercharged(git代碼提交歷史信息)备籽,
Highlight Matching Tag(高亮匹配的成對標(biāo)簽)舶治,
HTML CSS Support(樣式屬性提示)
HTML Snippets(html標(biāo)簽提示)
Prettier - Code formatter(代碼美化工具分井,下載了vuter,就沒必要下載它了)
Path Intellisense(路徑補全提示)
vscode-icons(文件圖標(biāo)樣式)
二霉猛、好了尺锚,你也是夠了,上代碼惜浅!配置詳情如下瘫辩。
vscode > settings.json(文件 > 首選項 > 設(shè)置 > 在settings.json中編輯;編輯器左下方設(shè)置圖標(biāo)也可進入配置坛悉。)
{
"editor.tabSize": 4,
"editor.detectIndentation": false,
"files.autoSave": "onWindowChange",
"editor.fontSize": 16,
"editor.autoClosingBrackets": "always",
"editor.autoClosingQuotes": "beforeWhitespace",
"editor.codeActionsOnSave": null,
"editor.padding.bottom": 30,
"editor.padding.top": 30,
"editor.showFoldingControls": "mouseover",
"workbench.preferredLightColorTheme": "One Monokai",
"workbench.colorTheme": "One Monokai",
"workbench.preferredDarkColorTheme": "One Monokai",
"workbench.editor.highlightModifiedTabs": true,
"window.autoDetectHighContrast": false,
"explorer.sortOrder": "type",
"extensions.closeExtensionDetailsOnViewChange": true,
"terminal.integrated.cursorBlinking": true,
"terminal.integrated.copyOnSelection": true,
"terminal.integrated.cursorStyle": "line",
"terminal.integrated.cursorWidth": 2,
"breadcrumbs.enabled": true,
"eslint.format.enable": true,
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"eslint.probe": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"html",
"vue",
"markdown"
],
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"html",
"vue",
"markdown"
],
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[json]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[vue]": {
"editor.defaultFormatter": "octref.vetur"
},
"vetur.completion.autoImport": true,
"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",
"vetur.format.options.tabSize": 4,
"vetur.format.options.useTabs": false,
"vetur.format.defaultFormatterOptions": {
"prettier": {
"semi": false,
"useTabs": false,
"singleQuote": true,
"tabWidth": 4,
"vueIndentScriptAndStyle": true,
"bracketSpacing": true,
"arrowParens": "avoid",
"trailingComma": "none"
},
"prettyhtml": {
"wrapAttributes": false,
"usePrettier": true,
"tabWidth": 4,
"useTabs": false,
"printWidth": 200,
"singleQuote": false
},
"sass.format.debug": false,
"sass.format.deleteEmptyRows": true,
"sass.format.deleteWhitespace": true,
"sass.format.convert": true,
"sass.format.setPropertySpace": true
},
"prettier.useTabs": false,
"prettier.singleQuote": true,
"prettier.semi": false,
"prettier.tabWidth": 4,
"prettier.vueIndentScriptAndStyle": true,
"prettier.bracketSpacing": true,
"prettier.arrowParens": "avoid",
"prettier.trailingComma": "none"
}
三伐厌、ESLint配置如下(配置項及其之多,要想明白每一項的配置裸影,花點時間可是不少罢豕臁)
- .eslintrc.js,項目根目錄下新建此文件轩猩。
- 下載所需的npm包:eslint刃唐,eslint-friendly-formatter,eslint-loader界轩,eslint-plugin-html画饥,eslint-plugin-vue
- 說明:extends: ['eslint:recommended', 'plugin:vue/essential'],eslint:recommended表示eslint的默認(rèn)生效的規(guī)則,下面rules的規(guī)則大多不在默認(rèn)項中浊猾,或是對規(guī)則有自己的配置抖甘,畢竟是和vscode配合使用,最終要在保存的時候一鍵格式化的葫慎,有些eslint默認(rèn)配置與vuter格式化工具規(guī)定的不一樣衔彻,所以需要修改eslint的配置。
module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint',
sourceType: 'module',
allowImportExportEverywhere: true
},
env: {
browser: true,
node: true,
es6: true
},
extends: ['eslint:recommended', 'plugin:vue/essential'],
plugins: [
'html',
'vue'
],
'rules': {
'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'],
'curly': [2, 'multi-line'],
'dot-location': [2, 'property'],
'eol-last': [2, 'always'],
'eqeqeq': [2, 'always', {
'null': 'ignore'
}],
'generator-star-spacing': [2, {
'before': true,
'after': true
}],
'handle-callback-err': [2, '^(err|error)$'],
'indent': ['error', 4, {
'SwitchCase': 1,
'VariableDeclarator': 1,
'MemberExpression': 1
}],
'vue/script-indent': ['error', 4, {
'baseIndent': 0,
'switchCase': 1
}],
'jsx-quotes': [2, 'prefer-single'],
'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-class-assign': 2,
'no-eval': 2,
'no-extra-parens': [2, 'functions'],
'no-implied-eval': 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, 'smart-tabs'],
'no-multi-spaces': [2, {
'ignoreEOLComments': true
}],
'no-multi-str': 2,
'no-multiple-empty-lines': 2,
'no-new-require': 2,
'no-octal-escape': 2,
'no-path-concat': 2,
'no-proto': 2,
'no-return-assign': [2, 'except-parens'],
'no-self-compare': 2,
'no-sequences': 2,
'no-spaced-func': 2,
'no-throw-literal': 2,
'no-trailing-spaces': 2,
'no-undef-init': 2,
'no-unmodified-loop-condition': 2,
'no-unused-vars': [2, {
'vars': 'all',
'args': 'none'
}],
'no-useless-call': 2,
'no-useless-computed-key': 2,
'no-useless-constructor': 2,
'no-whitespace-before-property': 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'],
'wrap-iife': [2, 'any'],
'yoda': [2, 'never'],
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
'object-curly-spacing': [2, 'always', {
objectsInObjects: false
}],
'array-bracket-spacing': [2, 'never']
}
}