简体   繁体   English

ESLint 和 Prettier 冲突,无法为代码块禁用 Prettier

[英]ESLint and Prettier Conflict, Unable to Disable Prettier for code block

Our project is using Prettier and ESLint.我们的项目使用 Prettier 和 ESLint。 Normally they work fine together, but we're running into an issue where the two are in conflict.通常它们可以很好地协同工作,但我们遇到了两者冲突的问题。 I don't know why, I can't figure out how to fix it, and I can't disable prettier for the line because I get errors.我不知道为什么,我不知道如何修复它,而且我无法禁用更漂亮的线路,因为我得到了错误。

Relevant parts of our settings我们设置的相关部分

// .prettierrc
  "printWidth": 80,
  "tabWidth": 2,
  "useTabs": false,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "es5",
  "arrowParens": "always"

// eslintrc.js
  extends: ['airbnb', 'eslint-config-prettier', 'prettier/react'],
  plugins: [
    'eslint-plugin-prettier',
    'redux-saga',
    'react',
    'react-hooks',
    'jsx-a11y',
  ],
  rules: {
    'prettier/prettier': ['error'],
    indent: [
      2,
      2,
      {
        SwitchCase: 1,
      },
    ],
    'max-len': 0,

And here is the code, with comments added to indicate the issues:这是代码,添加了注释以指示问题:

    const options =
      children === undefined
        ? items.map((item) => (
          // Prettier complains about the next four lines
          <option key={uuidv1()} value={item}>
            {item}
          </option>
        ))
        : children;

Prettier wants those lines intended another two spaces. Prettier 想要这些线条用于另外两个空格。

错误截图

ESLint likes them where they are. ESLint 喜欢他们在哪里。 I'm inclined to agree with ESLint.我倾向于同意 ESLint。 If I auto-format for Prettier, ESLint complains and wants it changed back.如果我为 Prettier 自动格式化,ESLint 会抱怨并希望它改回来。 I am using VSCode.我正在使用 VSCode。 We haven't had such a conflict anywhere else in our code base.我们的代码库中的其他任何地方都没有发生过这样的冲突。

I tried just disabling prettier for those lines, but the auto-disable option with eslint adds // eslint-disable-next-line prettier/prettier , which causes the app to error out with Definition for rule 'prettier/prettier' was not found .我尝试为这些行禁用更漂亮,但是使用 eslint 的自动禁用选项添加了// eslint-disable-next-line prettier/prettier ,这导致应用程序出错, Definition for rule 'prettier/prettier' was not found . Trying to add // prettier-ignore is impossible because of the JSX.由于 JSX,尝试添加// prettier-ignore是不可能的。

I don't understand why Prettier wants what ESLint says it wants.我不明白为什么 Prettier 想要 ESLint 所说的那样。 The only way we've been able to fix it is by removing 'prettier/prettier': ['error'], from our ESLint config entirely, which doesn't seem appropriate.我们能够修复它的唯一方法是从我们的 ESLint 配置中完全删除'prettier/prettier': ['error'],这似乎不合适。

Suggestions?建议?

Update 10/10/19 - Thanks to a suggestion by user chazsolo, I modified how the function was formatted to get this, which doesn't have any linting issues: 2019 年 10 月 10 日更新- 感谢用户 chazsolo 的建议,我修改了 function 的格式以得到这个,它没有任何掉毛问题:

const dropDownOptions =
  children ||
  items.map((item) => (
    <option key={uuidv1()} value={item.value || item}>
      {item.text || item}
    </option>
  ));

This is a viable workaround for this problem, but I'm leaving this question unanswered, as I still feel like my code is entirely valid and shouldn't get this conflict.对于这个问题,这是一个可行的解决方法,但我没有回答这个问题,因为我仍然觉得我的代码是完全有效的,不应该出现这种冲突。

The workaround also only works if I'm checking a value like children for being falsey.该解决方法也仅在我检查诸如children之类的值是否虚假时才有效。 We have another condition where it is a match, and I can't short-circuit it in the same way.我们还有一个匹配的条件,我不能以同样的方式短路它。 All of these problems involve the use of .map() .所有这些问题都涉及到.map()的使用。

// Can't short-circuit here
var === SOME_ENUM
  ? filteredItems.map((item) => (
      // some JSX
    ))
  : filteredItems.map((item) => (
      // some other JSX
    ));

I can move the condition inside the map function, but that leads to checking the condition every loop .我可以在 map function 中移动条件,但这会导致每个循环都检查条件。

This problem has cropped up a lot , and I'll probably be creating an issue in Prettier for it at this rate, because it's rather annoying to troubleshoot.这个问题已经出现了很多,我可能会以这种速度在 Prettier 中为它创建一个问题,因为解决问题很烦人。

My basic setup is working well for vue/ts in.eslintrc.json.我的基本设置适用于 vue/ts in.eslintrc.json。 You should add prettier in plugins你应该在插件中添加更漂亮

{
  "parser": "vue-eslint-parser",
  "parserOptions": { 
    "parser": "@typescript-eslint/parser" 
  },
  "plugins": ["@typescript-eslint", "prettier"],
  "rules": {
    "semi": ["error", "never"],
    "quotes": ["error", "single"],
    "prettier/prettier": "error"
  }
}

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

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