解决vscode eslint与prettier冲突

解决vscode eslint与prettier冲突

起因

vscode 升级了 prettier 3.0+之后,eslint 与 prettier 配合使用的配置总是冲突,保存自动格式化时人都快搞没了!!

版本 :vscode v1.41.1 、vscode-prettier v3.18.0、 vscode-eslint v2.0.11

我的方案

  1. 按推荐删除原来的 prettier 设置,然后迁移到在 package.json 中
// package.json 添加prettier配置
"prettier": {
    "stylelintIntegration": true,
    "eslintIntegration": true
  },
  1. 工作区文件修改 xxx.code-workspace,
"settings": {
      // ...
    "editor.codeActionsOnSave": {
      // For ESLint and StyleLint
      "source.fixAll": true
    },
    "editor.formatOnSave": false,
    "eslint.enable": true,
    "eslint.options": {
      //指定vscode的eslint所处理的文件的后缀
      "extensions": [".js", ".ts", ".tsx"]
    },
    "eslint.validate": ["javascript", "typescript", "typescriptreact"],
      // ...
  }
  1. 解决 eslintrc 与 prettire 默认规则的冲突。比如我以前的 eslint 规则”semi”: [“error”, “never”] 结尾不需要分号,而 prettire 默认是分号结尾,所以在格式化是会冲突。解决方法是:要么修改 eslintrc, 要么修改 prettire 规则。我修改了 prettire 规则,修改 package.json :
"prettier": {
    "eslintIntegration": true,
    "stylelintIntegration": true,
    // 添加规则
    "singleQuote": true,
    "semi": false,
    "insertPragma": false,
    "trailingComma": "none",
    "arrowParens": "avoid"
  },

其他冲突规则也用类似方法处理,要么修改 eslintrc,要么修改 prettier 配置,但是如果为了少改动老代码,推荐修改 prettier 配置去适应老的 eslint 规则。 prettier 所有的配置: Prettier - JavaScript formatter - Visual Studio Marketplace


最后分享eslint配置

// .eslintrc.json
{
  "extends": [
    "react-app",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended"
  ],
  "parserOptions": {
    "ecmaVersion": 2017,
    "sourceType": "module",
    "impliedStrict": true,
    "ecmaFeatures": {
      "legacyDecorators": true,
      "experimentalObjectRestSpread": true,
      "jsx": true
    }
  },
  "parser": "@typescript-eslint/parser",
  "plugins": ["react-hooks", "prettier"],
  "rules": {
    "prettier/prettier": "error",
    "@typescript-eslint/indent": "off",
    "@typescript-eslint/no-var-requires": "off",
    "@typescript-eslint/member-delimiter-style": "off",
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/ban-ts-ignore": "warn",
    "@typescript-eslint/no-empty-interface": "off",
    "@typescript-eslint/camelcase": "warn",
    "@typescript-eslint/interface-name-prefix": "warn",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-empty-function": "off"
  }
}

package.json 中的 prettier属性配置:

// package.json

  "prettier": {
    "singleQuote": true,
    "semi": false,
    "stylelintIntegration": true,
    "eslintIntegration": true,
    "insertPragma": false,
    "trailingComma": "none",
    "arrowParens": "avoid"
  }

style规则 .stylelintrc.json

// .stylelintrc.json
{
  "extends": ["stylelint-prettier/recommended", "stylelint-config-standard"],
  "plugins": ["stylelint-prettier"],
  "rules": {
    "prettier/prettier": true,
    "unit-case": null,
    "no-descending-specificity": null,
    "block-no-empty": null,
    "no-empty-source": [true, { "severity": "warning" }],
    "declaration-colon-newline-after": null,
    "function-name-case": null,
    "indentation": null,
    "no-invalid-double-slash-comments": null
  }
}

编辑于 2021-03-19 11:59