vscode和eslint的配置极其繁琐,为了不劳民伤财,本着救天下程序员于水深火热之中的宏愿,地狱不空,誓不成佛,特施恩于尔等。(怎么就没表情符号可用呢,这让我如何嘚瑟?看来只能说:你来打我撒!)
eslint与vscode相辅相成,再也不怕代码不规范了,任你怎么乱写,保存代码后可手动或是自动格式化代码!
一、需要用到的vscode插件:
1、ESLint,Vetur(必须)
2、以下插件就是自己的喜好罗,有助于美化样式和辅助提示(用了才知道,那个不痛,月月轻松!)。
Auto Close Tag(自动标签闭合),
Auto Rename Tag(标签重命名),
Bracket Pair Colorizer 2(成对括号颜色标记),
GitLens--Git supercharged(git代码提交历史信息),
Highlight Matching Tag(高亮匹配的成对标签),
HTML CSS Support(样式属性提示)
HTML Snippets(html标签提示)
Prettier - Code formatter(代码美化工具,下载了vuter,就没必要下载它了)
Path Intellisense(路径补全提示)
vscode-icons(文件图标样式)
二、好了,你也是够了,上代码!配置详情如下。
vscode > settings.json(文件 > 首选项 > 设置 > 在settings.json中编辑;编辑器左下方设置图标也可进入配置。)
{
"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的默认生效的规则,下面rules的规则大多不在默认项中,或是对规则有自己的配置,毕竟是和vscode配合使用,最终要在保存的时候一键格式化的,有些eslint默认配置与vuter格式化工具规定的不一样,所以需要修改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']
}
}