天天看点

vs code代码格式化配置

时间:2021.6.3

vscode使用eslint+vetur+Prettier - Code formatter格式化代码

//版本号,
vscode:v1.56.2
eslint:v2.1.20
vetur:v0.33.1
Prettier - Code formatter:v6.4.0
           

格式化配置

{
	//配置文件图标库
    "workbench.iconTheme": "vscode-great-icons",
    // vscode默认启用了根据文件类型自动设置tabsize的选项
    "editor.detectIndentation": false,
    // 重新设定tabsize
    "editor.tabSize": 4,
    // 110 列后换行
    "editor.wordWrapColumn": 110,
    // #每次保存的时候自动格式化
    "editor.formatOnSave": true,
    // #每次保存的时候将代码按eslint格式进行修复
    "editor.codeActionsOnSave": {
        "source.fixAll": true
    },
    //eslint 检测文件类型
    "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "html", "vue"],
    "prettier.printWidth": 110,
    "prettier.tabWidth": 4,
    //是否在每行末尾添加分号
    "prettier.semi": false,
    //如果为 true,则将多行jsx元素的 > 放在最后一行的末尾,而不是单独放在下一行
    "prettier.jsxBracketSameLine": true,
    //如果为 true,将使用单引号而不是双引号
    "prettier.singleQuote": true,
    //默认使用esbenp.prettier-vscode规则格式化文件
    "editor.defaultFormatter": "esbenp.prettier-vscode",
     //.vue文件使用vetur格式化,
     "[vue]": {
        "editor.defaultFormatter": "octref.vetur"
    },
    //vetur格式化采用js-beautify-html方式
    "vetur.format.defaultFormatter.html": "js-beautify-html"
    "vetur.format.options.tabSize": 4,
    //vetur格式化配置
    "vetur.format.defaultFormatterOptions": {
        "js-beautify-html": {
            "wrap_attributes": "force-aligned", //可以设置为“force-aligned/auto”,效果会不一样
            "wrap_line_length": 110,
            "end_with_newline": false,
            "singleQuote": true
        },
        "prettier": {
            "semi": false,
            "singleQuote": true
        }
    },
    //关闭快速查找
    // "search.followSymlinks": false,
    //关闭快速预览
    "editor.minimap.enabled": true,
    //取消自动更新
    "update.mode": "none",
    "git.enableSmartCommit": true,
}

           

新版vs code 1.41.1 vetur+eslint+prettier格式化配置

{
    "workbench.iconTheme": "vscode-great-icons",
    // vscode默认启用了根据文件类型自动设置tabsize的选项
    "editor.detectIndentation": false,
    // 重新设定tabsize
    "editor.tabSize": 4,
    // #每次保存的时候自动格式化 
    "editor.formatOnSave": true,
    // #每次保存的时候将代码按eslint格式进行修复
    "editor.codeActionsOnSave": {
        "source.fixAll": true
    },
    "eslint.validate": [ //开启对.vue文件中错误的检查
        "javascript",
        "javascriptreact",
        "html",
        "vue",
    ],
    "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
    "vetur.format.defaultFormatter.js": "prettier-eslint",
    "vetur.format.defaultFormatterOptions": {
        "prettyhtml": {
            "printWidth": 130, // No line exceeds 100 characters
            "wrapAttributes": false,
            "sortAttributes": true,
            "singleQuote": false // Prefer double quotes over single quotes
        },
        "prettier": {
            "semi": false,
            "singleQuote": true,
        }
    },
    "vetur.format.options.tabSize": 4,
    "eslint.alwaysShowStatus": true,
    //关闭快速查找
    "search.followSymlinks": false,
    //关闭快速预览
    "editor.minimap.enabled": false,
    //取消自动更新
    "update.mode": "none",
    "editor.quickSuggestions": {
        "strings": true
    },
    "explorer.confirmDelete": false,
    "prettier.tabWidth": 4,
    //是否在每行末尾添加分号
    "prettier.semi": false,
    //如果为 true,将使用单引号而不是双引号
    "prettier.singleQuote": true,
    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[vue]": {
        "editor.defaultFormatter": "octref.vetur"
    },
}
           

旧版vetur+eslint+prettier格式化配置

{
    // vscode默认启用了根据文件类型自动设置tabsize的选项
    "editor.detectIndentation": false,
    // 重新设定tabsize
    "editor.tabSize": 4,
    // #每次保存的时候自动格式化 
    "editor.formatOnSave": true,
    // #每次保存的时候将代码按eslint格式进行修复
    "eslint.autoFixOnSave": true,
    "eslint.validate": [ //开启对.vue文件中错误的检查
        "javascript",
        "javascriptreact",
        {
            "language": "html",
            "autoFix": true,
        },
        {
            "language": "vue",
            "autoFix": true
        }
    ],
    "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
    "vetur.format.defaultFormatter.js": "vscode-typescript",
    "vetur.format.defaultFormatter.html": "js-beautify-html",
    "vetur.format.defaultFormatterOptions": {
        "js-beautify-html": {
            "wrap_attributes": "force-aligned" //属性强制折行对齐
        },
    },
    //缩进设置为4
    "prettier.tabWidth": 4,
    //去掉代码结尾的分号指js
    "prettier.semi": false,
    "prettier.singleQuote": true,
    "eslint.alwaysShowStatus": true,
    //关闭快速查找
    "search.followSymlinks": false,
    //关闭快速预览
    "editor.minimap.enabled": false,
    //取消自动更新
    "update.mode": "none",
}