简体   繁体   English

Prettier + Airbnb 的 ESLint 配置

[英]Prettier + Airbnb's ESLint config

Recently, I've started using Visual Studio Code for my editor and found the Prettier - JavaScript formatter.最近,我开始在我的编辑器中使用 Visual Studio Code,并找到了 Prettier - JavaScript 格式化程序。 I think it's a great plugin because it helps me keep my code looking nice.我认为这是一个很棒的插件,因为它可以帮助我保持代码的美观。

I set up Airbnb's ESLint config and have found that to be super helpful.我设置了 Airbnb 的 ESLint 配置,发现它非常有用。

Here's the catch.这是问题所在。 The Airbnb ESLint config I'm currently running doesn't play nice with Prettier.我目前运行的 Airbnb ESLint 配置与 Prettier 不兼容。 For example, for JavaScript string, Prettier is formatted to include double ticks and Airbnb's ESLint like single ticks.例如,对于 JavaScript 字符串,Prettier 被格式化为包含双刻度和 Airbnb 的 ESLint,就像单刻度一样。 When I format the code using Prettier, then Airbnb's ESLint doesn't agree.当我使用 Prettier 格式化代码时,Airbnb 的 ESLint 不同意。

I know Kent Dodds has done some work with ESLint configs, among others, example here.我知道 Kent Dodds 已经用 ESLint 配置做了一些工作,其中包括这里的例子。

But I can't seem to find a solution that lets me use the magic of Prettier to format my code to Airbnb's ESLint.但我似乎找不到一个解决方案,让我使用 Prettier 的魔力将我的代码格式化为 Airbnb 的 ESLint。

I think eslint-config-prettier was created just for this purpose: https://prettier.io/docs/en/eslint.html#turn-off-eslint-s-formatting-rules我认为eslint-config-prettier就是为此目的而创建的: https : //prettier.io/docs/en/eslint.html#turn-off-eslint-s-formatting-rules

Basically it turns off all rules that have to do with code styling because prettier will take care of it anyway.基本上它会关闭所有与代码样式有关的规则,因为prettier无论如何都会处理它。

So you just install this config along with any other desired eslint config (like eslint-config-airbnb ) and in your eslint configuration file you just add it to the extends field.因此,您只需将此配置与任何其他所需的 eslint 配置(如eslint-config-airbnb )一起安装,并在您的 eslint 配置文件中将其添加到extends字段中。 For example:例如:

{
  "extends": ["airbnb", "prettier"]
}

Here are a few ways to do it, in order of recommendation.这里有几种方法可以做到,按推荐顺序。 I would prefer the first approach as you won't ever have to bother with other rules that might conflict in future.我更喜欢第一种方法,因为您不必担心将来可能会发生冲突的其他规则。

i) Install eslint-config-prettier and extend from it in .eslintrc . i) 安装eslint-config-prettier并在.eslintrc扩展它。 Doing this turns off some of the formatting-related rules within ESLint that might conflict with Prettier:这样做会关闭 ESLint 中可能与 Prettier 冲突的一些与格式相关的规则:

{
  "extends": [
    "airbnb",
    "prettier"
  ]
}

ii) Adding "singleQuote": true to the .prettierrc config file: ii) 将"singleQuote": true添加到.prettierrc配置文件中:

{
  "singleQuote": true,
  ...
}

iii) Adding a --single-quote command line option when you invoke Prettier: iii) 在调用 Prettier 时添加--single-quote命令行选项:

$ prettier --single-quote ...

iv) Turn off ESLint's quotes rule within your .eslintrc config file (and potentially others that might conflict): iv) 在.eslintrc配置文件中关闭 ESLint 的quotes规则(以及其他可能冲突的文件):

{
  "rules": {
    "quotes": "off",
    ...
  }
}

A cleaner way is :更清洁的方法是

yarn add --dev eslint-plugin-prettier eslint-config-prettier

   // .eslintrc
   {
     "extends": ["airbnb", "plugin:prettier/recommended"]
   }

The plugin:prettier/recommended does three things : plugin:prettier/recommended 做了三件事

  • Enables eslint-plugin-prettier.启用 eslint-plugin-prettier。
  • Sets the prettier/prettier rule to "将更漂亮/更漂亮的规则设置为“
  • Extends the eslint-config-prettier configuration.扩展 eslint-config-prettier 配置。

And then if you are on react, you could add prettier/react too:然后,如果你正在反应,你也可以添加prettier/react

{
  "extends": [
    "airbnb",
    "plugin:prettier/recommended",
    "prettier/react"
  ]
}

So, you have your .eslintrc file, with the property "extends": "airbnb" Add another property, rules, and the rules that you will write in there will overwrite the ones inherited from airbnb所以,你有你的 .eslintrc 文件,属性为"extends": "airbnb"添加另一个属性、规则,你将在那里写入的规则将覆盖从 airbnb 继承的规则

"extends": "airbnb",
"rules": {
    "eqeqeq": 2,
    "comma-dangle": 1,
}

Now here I'm just overwriting two random rules, you will need to look for the one you need :)现在,我只是覆盖了两个随机规则,您需要寻找您需要的规则:)

Here is a little interactive CLI tool I built to setup ESLint with Prettier.这是我构建的一个小的交互式CLI 工具,用于使用 Prettier 设置 ESLint。 Just install it and run:只需安装并运行:

npx eslint-prettier-init

Which will resolve your issue.这将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM