VSCode+Prettier+ESLint团队代码风格统一,保存自动根据ESLint格式化

VSCode+Prettier+ESLint团队代码风格统一,保存自动根据Eslint格式化
团队协作代码风格保持一致-----Prettier
代码格式校验保持一致----ESLint

代码规范除了编辑器检查以外,还有一些譬如变量名、样式名的规范,可以参考腾讯的代码规范或者百度的代码规范

腾讯代码规范
百度代码规范

1、先行安装如下四个插件

在这里插入图片描述

2、VScode编辑器设置
settings.json
{
    //主题和图标配置,根据自己情况配置
    "workbench.iconTheme": "simple-icons",
    "workbench.colorTheme": "Monokai Dimmed",
    //忽略搜索的文件夹
    "search.exclude": {
        "**/bower_components": true,
        "**/node_modules": false
    },
    //忽略工程打开的文件夹
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.DS_Store": true,
        "**/node_modules": true,
        "**/iOS": true
    },
    // 头部注释 ctrl+alt+i
    "fileheader.customMade": {
        "Description": "",
        "Autor": "Godfery",
        "Date": "Do not edit"
    },
    // 函数注释 ctrl+alt+t
    "fileheader.cursorMode": {
        "description": "",
        "param": "",
        "return": "",
        "author": "Godfery"
    },
    "fileheader.configObj": {
        "autoAdd": true,
        "autoAlready": true,
        "prohibitAutoAdd": ["json", "md"],
        "wideSame": false,
        "wideNum": 13
    },
    // editor部分-------------
    "editor.formatOnSave": true, // 保存自动格式化
    "editor.wordWrapColumn": 200,
    "editor.codeActionsOnSave": {
        // eslint 保存时自动修复 【可修复function关键字后不带空格】
        "source.fixAll.eslint": true
    },
    // eslint代码语法校验部分
    "eslint.alwaysShowStatus": true,
    "eslint.quiet": true,
    "eslint.validate": ["javascript", "javascriptreact", "vue", "html"],
    //对不同文件进行不同的格式化选择
    "[json]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[jsonc]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[vue]": {
        "editor.defaultFormatter": "octref.vetur"
    },
    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[html]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[css]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    // vetur 默认使用prettier格式化
    "vetur.format.defaultFormatter.js": "prettier",
    "vetur.format.defaultFormatter.html": "prettyhtml",
    "vetur.format.options.tabSize": 4,
    "vetur.format.defaultFormatterOptions": {
        "prettier": {
            "semi": false, // 结尾无分号
            "printWidth": 200, // 超过200个字符换行
            "singleQuote": true, // 使用单引号
            "trailingComma": "none", // 无尾随逗号
            "arrowParens": "avoid" // 箭头函数单个参数不加分号
        },
        "prettyhtml": {
            "printWidth": 200
        }
    },
    // prettier代码风格格式化部分
    "prettier.semi": false,
    "prettier.printWidth": 200,
    "prettier.trailingComma": "none",
    "prettier.singleQuote": true,
    "prettier.arrowParens": "avoid",
    "prettier.tabWidth": 4,
    "files.associations": {
        "*.cjson": "jsonc",
        "*.wxss": "css",
        "*.wxs": "javascript"
    }
}

项目目录结构

在这里插入图片描述

.eslintrc.js 设置
/*
 * @Description:eslint 规则配置
 * @Autor: Godfery
 * @Date: 2021-07-12 17:04:51
 */
// ESlint 检查配置
module.exports = {
    root: true,
    parserOptions: {
        parser: "babel-eslint",
        sourceType: "module"
    },
    env: {
        browser: true,
        node: true,
        es6: true
    },
    extends: ["eslint:recommended", "plugin:vue/essential"],
    // add your custom rules here
    // it is base on https://github.com/vuejs/eslint-config-vue
    rules: {
        "new-parens": 2,
        "no-array-constructor": 2,
        "no-caller": 2,
        "no-console": "off",
        "no-class-assign": 2,
        "no-cond-assign": 2,
        "no-const-assign": 2,
        "no-control-regex": 0,
        "no-delete-var": 2,
        "no-dupe-args": 2,
        "no-dupe-class-members": 2,
        "no-dupe-keys": 2,
        "no-duplicate-case": 2,
        "no-empty-character-class": 2,
        "no-empty-pattern": 2,
        "no-eval": 2,
        "no-ex-assign": 2,
        "no-extend-native": 2,
        "no-extra-bind": 2,
        "no-extra-boolean-cast": 2,
        "no-extra-parens": [2, "functions"],
        "no-fallthrough": 2,
        "no-floating-decimal": 2,
        "no-func-assign": 2,
        "no-implied-eval": 2,
        "no-inner-declarations": [2, "functions"],
        "no-invalid-regexp": 2,
        "no-irregular-whitespace": 2,
        "no-iterator": 2,
        "no-label-var": 2,
        "no-lone-blocks": 2,
        "no-mixed-spaces-and-tabs": 2,
        "no-multi-spaces": 2,
        "no-multi-str": 2,
        "no-native-reassign": 2,
        "no-negated-in-lhs": 2,
        "no-new-object": 2,
        "no-new-require": 2,
        "no-new-symbol": 2,
        "no-new-wrappers": 2,
        "no-obj-calls": 2,
        "no-octal": 2,
        "no-octal-escape": 2,
        "no-path-concat": 2,
        "no-proto": 2,
        "no-redeclare": 2,
        "no-regex-spaces": 2,
        "no-return-assign": [2, "except-parens"],
        "no-self-assign": 2,
        "no-self-compare": 2,
        "no-shadow-restricted-names": 2,
        "no-spaced-func": 2,
        "no-sparse-arrays": 2,
        "no-this-before-super": 2,
        "no-throw-literal": 2,
        "no-undef": 2,
        "no-undef-init": 2,
        "no-unexpected-multiline": 2,
        "no-unmodified-loop-condition": 2,
        "no-unreachable": 2,
        "no-unsafe-finally": 2,
        "no-useless-call": 2,
        "no-useless-computed-key": 2,
        "no-useless-constructor": 2,
        "no-useless-escape": 0,
        "no-whitespace-before-property": 2,
        "no-with": 2,
        "padded-blocks": [2, "never"],
        "space-before-blocks": [2, "always"],
        "space-in-parens": [2, "never"],
        "space-infix-ops": 2,
        "template-curly-spacing": [2, "never"],
        "use-isnan": 2,
        "valid-typeof": 2,
        "wrap-iife": [2, "any"],
        "yield-star-spacing": [2, "both"],
        "no-debugger": process.env.NODE_ENV === "production" ? 2 : 0
    }
};

.eslintignore 忽略eslint检查设置
# 忽略build目录下类型为js的文件的语法检查
build/*.js
# 忽略src/assets目录下文件的语法检查
src/assets
# 忽略public目录下文件的语法检查
public

.editorconfig 编辑器配置
root = true
# 匹配全部文件
[*]
# 设置字符集
charset = utf-8
# 缩进风格,可选space、tab
indent_style = space
# 缩进的空格数
indent_size = 4
# 结尾换行符,可选lf、cr、crlf
end_of_line = lf
# 在文件结尾插入新行
insert_final_newline = true
# 删除一行中的前后空格
trim_trailing_whitespace = true

# 匹配md结尾的文件
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: VSCode中有很多格式化代码的插件,其中比较常用的有以下几种: 1. Prettier:一个流行的代码格式化工具,支持多种语言,可以通过配置文件进行自定义格式化规则。 2. Beautify:支持多种语言的代码格式化插件,可以通过配置文件进行自定义格式化规则。 3. ESLint:一个JavaScript代码检查工具,也可以用于格式化代码。 4. TSLint:一个TypeScript代码检查工具,也可以用于格式化代码。 5. HTMLHint:一个HTML代码检查工具,也可以用于格式化代码。 这些插件都可以通过VSCode插件市场进行下载安装。 ### 回答2: VSCode(Visual Studio Code)是目前非常受欢迎的开源文本编辑器,它提供了丰富的功能和插件来满足开发者的需求。其中,格式化代码插件是VSCode中非常重要的插件之一。 VSCode格式化代码插件可以帮助开发自动调整代码的格式,使得代码更加可读、整洁。这个插件可以根据代码的语法规范,自动调整代码的缩进、换行、空格等细节,使代码的结构更加清晰、易于阅读,提高代码的可维护性。 使用VSCode格式化代码插件具有以下优势: 1. 提高开发效率:格式化代码插件可以节省开发者手动调整代码格式的时间,尤其是当项目中有多个人共同开发时,保持统一代码风格非常重要。 2. 保证代码品质:格式化代码插件可以遵循团队约定的代码编写规范,消除不一致的代码格式,确保代码的一致性和可读性,减少出错的机会。 3. 适应多种语言:VSCode格式化插件可以适应各种编程语言,如JavaScript、Python、Java等,使得开发者可以在不同的项目中使用相同的编辑器和插件。 4. 可定制性强:VSCode格式化代码插件通常都支持自定义配置,开发者可以根据自己团队代码规范进行调整。例如,可以设置缩进为Tab还是空格、缩进的数量、换行的风格等。 总结来说,VSCode格式化代码插件可以帮助开发者提高代码的可读性、减少错误发生的机会,提高团队协作效率。通过自动化调整代码格式,开发者可以更专注于业务逻辑的实现,提高开发效率和代码品质。 ### 回答3: VSCode是一个功能强大的代码编辑器,拥有丰富的插件生态系统。其中,格式化代码插件是开发者常用的插件之一。 VSCode提供了多种格式化代码的插件选择,如Prettier、Beautify等。这些插件可以根据开发者的配置,将代码自动格式化统一风格,大大提高了代码的可读性和维护性。 使用格式化代码插件,开发者可以快速美化代码,避免手动调整代码缩进、换行等繁琐的工作。使用插件进行代码格式化可以减少开发者的工作量,节省开发时间。 常见的代码格式化规范包括缩进、对齐、换行等。开发者可以根据自己的喜好和项目要求进行个性化的配置,使代码风格一致而且易于阅读。 除了自动格式化VSCode格式化代码插件还提供了代码审查和错误检测的功能。插件可以帮助开发者找出代码中的潜在问题,并提供修复建议。这大大提高了代码的质量和可靠性。 总之,VSCode格式化代码插件是一款方便实用的工具,能够快速美化代码、提高代码的可读性和维护性、提供代码审查和错误检测等功能。开发者可以根据个人喜好和项目要求进行配置,从而更高效地进行代码开发
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值