JSX中的ESLint额外括号

时间:2018-09-08 15:03:27

标签: reactjs jsx eslint

我注意到使用jsx文件中的逻辑AND(&&)将多余的括号添加到多行条件渲染中。例如,此代码来自React文档...

{unreadMessages.length > 0 &&
  <h2>
    You have {unreadMessages.length} unread messages.
  </h2>
}

...将进行如下修改:

{unreadMessages.length > 0 && (
  <h2>
    You have {unreadMessages.length} unread messages.
  </h2>
)}

Here is my ESLint config

"eslintConfig": {
  "root": true,
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "impliedStrict": true
    }
  },
  "env": {
    "browser": true,
    "node": true,
    "es6": true,
    "mocha": true
  },
  "plugins": [
    "react"
  ],
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "rules": {
    "computed-property-spacing": [
      "error"
    ],
    "indent": [
      "error",
      2
    ],
    "jsx-quotes": [
      "error"
    ],
    "key-spacing": [
      "error"
    ],
    "no-case-declarations": [
      "off"
    ],
    "no-console": [
      "off"
    ],
    "no-var": [
      "error"
    ],
    "object-curly-spacing": [
      "error",
      "always"
    ],
    "prefer-const": [
      "error"
    ],
    "quotes": [
      "error",
      "single",
      {
        "avoidEscape": true,
        "allowTemplateLiterals": true
      }
    ],
    "react/no-children-prop": "off",
    "react/prop-types": "off",
    "semi": [
      "error",
      "never"
    ]
  }
}

是我无意中造成了这个问题,还是有充分的理由呢?如果没有,我该如何预防?对disallow unnecessary parentheses来说似乎有些矫kill过正。

1 个答案:

答案 0 :(得分:1)

这似乎是由react/jsx-wrap-multilines引起的,可以通过将logical语法类型设置为"ignore"来防止。