简体   繁体   English

vscode 和 eslint 上的错误缩进<text> | JSX | 反应本机</text>

[英]vscode and eslint wrong indentation on <Text> | JSX | React Native

The original file looks like this:原始文件如下所示: 保存前

Then once I save, it changes to this:然后一旦我保存,它就会变成这样:

在此处输入图像描述

My development dependencies:我的开发依赖:

"devDependencies": {
"@babel/preset-flow": "^7.0.0-beta.56",
"babel-eslint": "^8.2.6",
"babel-jest": "23.4.2",
"babel-preset-react-native": "^5",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^17.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.10.0",
"flow-bin": "^0.78.0",
"jest": "23.4.2",
"prettier-eslint": "^8.8.2",
"react-test-renderer": "16.4.1"},

Any ideas why is that happening?任何想法为什么会这样?

Thanks for looking into it:)感谢您调查它:)

I think it's because of whitespace config (eg no-irregular-whitespace ?). 我认为这是由于空白配置(例如, no-irregular-whitespace吗?)。 I'd say the safest way would be having them as one-liner (and wrapped in curly brackets): 我想说,最安全的方法是将它们作为单线包装(并用大括号括起来):

<Text>{'Drawer'}</Text>

I did not have eslint-plugin-prettier and eslint-config-prettier installed, then I did the following: 我没有安装eslint-plugin-prettier和eslint-config-prettier,然后执行以下操作:

  1. Add "prettier" to "plugins" section in ".eslintrc" 在“ .eslintrc”的“插件”部分添加“漂亮”
  2. Add "'eslint-config-prettier", "'eslint-config-prettier/react" and "'eslint-config-prettier/flow" to "extends" section in ".eslintrc" 将“'eslint-config-prettier”,“'eslint-config-prettier / react”和“'eslint-config-prettier / flow”添加到“ .eslintrc”的“扩展”部分。
  3. Add "'prettier/prettier': ['error', { trailingComma: 'es5', singleQuote: true }]" to the "rules" section in ".eslintrc" 在“ .eslintrc”的“规则”部分中添加“'更漂亮/更漂亮':['错误',{TrailingComma:'es5',singleQuote:true}]”

And then removed "prettier-eslint" package so prettier can handle the formatting completely. 然后删除“ prettier-eslint”包,以便更漂亮可以完全处理格式。 "prettier-eslint" runs prettier then eslint so eslint can change the formatting made by prettier. 先运行“ prettier-eslint”,再运行eslint,以便eslint可以更改prettier制作的格式。

I also disable prettier extension on VScode and added prettier as suggested on eslint-config-prettier https://www.npmjs.com/package/eslint-plugin-prettier 我还禁用了VScode上的漂亮扩展,并按照eslint-config-prettier https://www.npmjs.com/package/eslint-plugin-prettier上的建议添加了漂亮

And also config my .eslintrc as following: 并配置我的.eslintrc如下:

{
"extends": [
"airbnb",
"prettier",
"prettier/react"
],
"parser": "babel-eslint",
"parserOptions":{
    "ecmaVersion": 8,
    "ecmaFeatures": {
        "experimentalObjectRestSpread": true,
        "impliedStrict": true,
        "classes": true
    }
},
"env": {
    "browser": true,
    "node": true,
    "jquery": true
},
"plugins": ["react", "prettier"],
"rules": {
    "react/jsx-filename-extension": [
    2,
    {
        "extensions": [".js", ".jsx"]
    }
    ],
    "react/forbid-prop-types": [0],
    "react/require-default-props": [0],
    "global-require": [0],
    "no-underscore-dangle": [0],
    "react/destructuring-assignment": 0,
    "ignoreClassFields": true,
    "no-unused-vars": 1,
    "react/prop-types": 1,
    "camelcase": 1,
    "max-len": 1,
    "prettier/prettier": ["error", { "trailingComma": "es5", "singleQuote": true }]
}

} }

Hope that helps someone out there :) 希望可以帮助某人:)

I solved this by adding:我通过添加解决了这个问题:

"react/jsx-indent": [
    "error",
    4
]

to my.eslintrc.json到我的.eslintrc.json

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

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