Vscode 中 ESLint+Prettier 冲突解决

3,744 阅读1分钟

为什么要使用他们呢?

ESLint 代码的校验

js作为一门灵活的弱类型语言,代码风格千奇百怪,一千个人写js就有一千种写法。虽然js没有官方推荐的代码规范,不过社区有些比较热门的代码规范,比如standardjs、airbnb。使用ESLint配合这些规范,能够检测出代码中的潜在问题,提高代码质量,但是并不能完全统一代码风格

Prettier 格式化代码

Prettier是一个能够完全统一你和同事代码风格的利器。顾名思义,Prettier直意过来就是更漂亮的代码,让代码拥有统一风格,提高代码的可读性。

然而在使用过程中我们发现他们直接会有冲突,本来符合ESlint规范的代码,在保存后自动格式化过不了Eslint的规则,很是难受。好好的配合却吵架了,只能说缺少润滑剂🙂

Eslint-config-prettier

我们看介绍,是不是眼前一亮,工具来了,eslint-config-prettier

Installation

Tip: First, you might be interested in installing [eslint-plugin-prettier]. Follow the instructions over there. This is optional, though.

Install eslint-config-prettier:

npm install --save-dev eslint-config-prettier

Then, add eslint-config-prettier to the "extends" array in your .eslintrc.* file. Make sure to put it last, so it gets the chance to override other configs.

{
  "extends": [
    "some-other-config-you-use",
    "prettier"
  ]
}