简体   繁体   English

JSX的ESlint + Prettier

[英]ESlint + Prettier for JSX

I am frustrated about the right config of prettier to achieve normal style for jsx tags. 我对更漂亮的配置以实现jsx标签的正常样式感到沮丧。
I want this : 我要这个 :

const template = (
  <div>
    <h1> Hello world!</h1> 
    <p> This is some info</p>
  </div>
);

and I got this: 我得到了这个:

const template = (
  <div>
    <h1> Hello world! </h1> <p> This is some info </p>{' '}
  </div>
);

My .eslintrc is: 我的.eslintrc是:

{
    "parser": "babel-eslint",
    "extends": [
        "airbnb",
        "prettier",
        "prettier/react"
    ],
    "env": {
        "browser": true
    },
    "plugins": [
        "react",
        "jsx-a11y",
        "import",
        "prettier"
    ],
    "rules": {
        "no-console": "off",
        "linebreak-style": "off",
        "react/jsx-filename-extension": [
            1,
            {
                "extensions": [".js", ".jsx"]
            }
        ],
        "react/react-in-jsx-scope": 0,
        "prettier/prettier": [
            "error",
            {
                "trailingComma": "es5",
                "singleQuote": true,
                "printWidth": 120
            }
        ]
    }
}

I've been searching about this problem and I couldn't figure out what to do. 我一直在寻找这个问题,我不知道该怎么办。
And If anyone has a good .eslintrc file please post it. 并且,如果有人拥有一个良好的.eslintrc文件,请将其发布。

I can't find anything wrong with your configuration. 我找不到您的配置有任何问题。 There are a few options but I don't really know your setup. 有几个选项,但我真的不知道您的设置。

I think there might be a configuration of Prettier in your IDE. 我认为您的IDE中可能存在Prettier的配置。 So for example, if you use VSCode and have the Prettier extension installed you can override some of these settings. 因此,例如,如果您使用VSCode并安装了Prettier扩展,则可以覆盖其中的一些设置。

Second thing is that I think the {' '} is because of a trailing space in your example. 第二件事是我认为{''}是由于您的示例中有尾随空格。 There really is no way for me to check or reproduce. 我真的没有办法检查或复制。 I'd suggest installing an addon which removes trailing spaces. 我建议安装一个可删除尾随空格的插件。 I use Trailing Spaces for VSCode. 我将尾随空格用于VSCode。

The last thing you can check is by installing eslint-config-react-app and doing something like this: 您可以检查的最后一件事是通过安装eslint-config-react-app并执行以下操作:

"eslintConfig": {
    "extends": "react-app"
},

My rootnode is of course from my package.json and should be different for your . eslintrc 我的rootnode当然来自我的package.json ,应该与您的有所不同. eslintrc . eslintrc but extending should be the same. . eslintrc扩展应该相同。

These are just a few things I can think of, hope they help and hope you get your linting solved, those issues also bug me a lot! 这些只是我能想到的几件事,希望它们能为您提供帮助,并希望您的棉绒得到解决,这些问题也让我很烦恼!

我遇到了同样的问题,可以通过删除Beatify扩展名来解决。

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

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