简体   繁体   English

在 React 项目中使用 Prettier 安装 ESlint 的正确方法

[英]Proper way of Installing ESlint with Prettier in a React Project

I've recently started using eslint and prettier in my projects, but I'm always not sure if I'm installing them correctly.我最近开始在我的项目中使用 eslint 和 prettier,但我总是不确定我是否正确安装了它们。 I've read several articles online and it seems each one does it differently.我在网上阅读了几篇文章,似乎每篇文章的做法都不一样。 I'm trying to use the Airbnb configuration.我正在尝试使用 Airbnb 配置。 I currently do not get any errors in a basic React app, but I just want to be sure it's the correct configuration.我目前在基本的 React 应用程序中没有遇到任何错误,但我只是想确保它是正确的配置。 What would be the best way to run eslint with prettier?用 prettier 运行 eslint 的最佳方法是什么?

Here are my files:这是我的文件:

.eslintrc.js .eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
    jest: true,
  },
  extends: [
    'plugin:react/recommended',
    'airbnb',
    'plugin:prettier/recommended',
  ],
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: ['react', 'prettier'],
  rules: {
    'prettier/prettier': 'error',
    'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx'] }],
  },
};

.prettierrc.js .prettierrc.js

module.exports = {
    trailingComma: "es5",
    tabWidth: 2,
    semi: true,
    singleQuote: true,
  };

in my package.json在我的 package.json

"devDependencies": {
    "eslint": "^7.25.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-react": "^7.23.2",
    "eslint-plugin-react-hooks": "^4.2.0",
    "prettier": "^2.2.1"
  }

There are a number of ways to set up prettier to work with eslint and it can be confusing.有很多方法可以设置 prettier 以使用 eslint,这可能会让人感到困惑。

To run prettier as an eslint rule, you would want to add a rule that allows eslint to run prettier.要将 prettier 作为 eslint 规则运行,您需要添加一个允许 eslint 运行 prettier 的规则。 You do that with the eslint-plugin-prettier plugin.你可以使用eslint-plugin-prettier做到这一点。

  "plugins": ["prettier"],          // Defines a rule that allows eslint to run prettier.
  "rules": {
    "prettier/prettier": "error"    // Turns on that rule.
  }

You would also want to turn off eslint rules that may conflct with prettier.您还想关闭可能与 prettier 冲突的 eslint 规则。 You do this with the eslint-config-prettier extension.您可以使用eslint-config-prettier扩展来执行此操作。 Note this should come after any other extensions in order to override the rules as appropriate.请注意,这应该出现在任何其他扩展之后,以便根据需要覆盖规则。

"extends": [
    /*other extensions*/, 
    "prettier"                      // Turns off conflicting eslint rules.  
]             

As per their documentation, it sounds the recommended extension that comes with the prettier plugin actually takes care of everything for you, so you should be able to get away with just:根据他们的文档,听起来更漂亮的插件附带的推荐扩展实际上会为您处理所有事情,因此您应该能够摆脱:

  extends: [
    // .. other extensions
    'plugin:prettier/recommended',
  ]

https://github.com/prettier/eslint-plugin-prettier https://github.com/prettier/eslint-plugin-prettier

Also, in case third-party plugins come with their own rules that may conflict with prettier, you used to have to add "prettier/that-plugin" - "prettier/react" in your case for instance - to the list of plugins in order to tell eslint to defer to prettier for these non-standard rules as relevant as well.此外,如果第三方插件有自己的规则可能与 prettier 冲突,您曾经必须将"prettier/that-plugin" - 例如在您的情况下为"prettier/react" - 添加到插件列表中为了告诉 eslint 对于这些非标准规则也同样相关。 But this no longer seems required.但这似乎不再需要。

I used this guide for setting up eslint with airbnb's style guide on a Yarn monorepo/workspace.我使用本指南在 Yarn monorepo/workspace 上使用 airbnb 的样式指南设置 eslint。

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

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