vue项目--vscode代码美化(格式化)和eslint配置详情

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包:eslinteslint-friendly-formattereslint-loadereslint-plugin-htmleslint-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']

        }

    }

四、其实我的配置可以满足三种格式化,分别是 eslintvuterprettier-code formatter,这三种可以选择,在编辑器中右键选择‘文档格式化设置方式’,即可任意设置默认的格式化方式,但是,推荐选择vuter,因为要理解vscode的难处嘛。

五、 "editor.formatOnSave": true,可以设置是否在保存代码时自动格式化,看你自己罗,老项目不推荐自动格式化哦!

六、最后,配置完成后,尝试下:右键 > 格式化文档。实践才是检验真理的唯一标准。如果一切顺利,你将看到按照eslint规则格式化和按照vuter美化的代码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335