【问题标题】:How to configure Prettier to format Styled Components conditional rendering如何配置 Prettier 以格式化 Styled Components 条件渲染
【发布时间】:2020-07-20 14:14:27
【问题描述】:

我正在使用 Prettier、Eslint 和 Styled 组件 - 反引号样式声明。 到目前为止它可以工作,但是 Prettier 以 Eslint 不允许的方式格式化 Styled 组件的条件渲染,并且在格式化后 Eslint 开始抱怨并且构建失败。

让我通过代码告诉你问题。

Prettier 处理前的初始代码:

// styled components styles apply
const TextInputStyled = styled(TextInputThemed)`
  ${(props: StyledProps) => {
    const {
      theme: { theme10x },
      disabled,
      success,
      error,
    } = props;
    return `
      ${success && `
        border-color: ${theme10x.palette.common.success};
      `};

      ${error && `
        background-color: ${theme10x.palette.common.error};
      `};

      ${disabled && `
        background-color: ${theme10x.palette.background.disabled};
      `};
    `;
  }}

经过更漂亮的处理:

// styled components styles apply
const TextInputStyled = styled(TextInputThemed)`
  ${(props: StyledProps) => {
    const {
      theme: { theme10x },
      disabled,
      success,
      error,
    } = props;
    return `
      ${
        success &&
        `
        border-color: ${theme10x.palette.common.success};
      `
      };

      ${
        error &&
        `
        background-color: ${theme10x.palette.common.error};
      `
      };

      ${
        disabled &&
        `
        background-color: ${theme10x.palette.background.disabled};
      `
      };
    `;
  }}
`;

在那之后,Eslint 开始抱怨:

  133:1   error    Expected indentation of 2 spaces but found 8        indent
  133:19  error    '&&' should be placed at the beginning of the line  operator-linebreak
  137:1   error    Expected indentation of 0 spaces but found 6        indent
  140:1   error    Expected indentation of 2 spaces but found 8        indent
  140:17  error    '&&' should be placed at the beginning of the line  operator-linebreak
  144:1   error    Expected indentation of 0 spaces but found 6        indent
  147:1   error    Expected indentation of 2 spaces but found 8        indent
  147:20  error    '&&' should be placed at the beginning of the line  operator-linebreak
  151:1   error    Expected indentation of 0 spaces but found 6        indent

我不想更改 Eslint 规则,因为它们在“真实”用例中非常有用。 那么有什么方法可以正确解决这个问题吗? 感谢您的帮助!

更新:

我的 .eslintrc.json

{
  "extends": [
    "airbnb",
    "eslint:recommended",
    "plugin:jsx-a11y/recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "plugins": ["@typescript-eslint"],
  "parser": "@typescript-eslint/parser",
  "rules": {
    "@typescript-eslint/no-explicit-any": 0, 
    "react/static-property-placement": 0,

    "quotes": [2, "single"],
    "import/no-unresolved": 0,
    "comma-dangle": 0,
    "linebreak-style": 0,
    "react/state-in-constructor": 0,
    "no-underscore-dangle": 0,
    "react/jsx-props-no-spreading": 0,
    "semi": 1,
    "comma-dangle:": 0,
    "import/prefer-default-export": 0,
    "import/extensions": 0,
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [".jsx", ".tsx"]
      }
    ],
    "@typescript-eslint/explicit-function-return-type": 0
  }
}

我的 Prettier 配置(它是 package.json 的一部分)

  "prettier": {
    "trailingComma": "es5",
    "semi": true,
    "singleQuote": true
  }

我正在使用 lint:fix 链中的 husky 作为 git 钩子运行它

  "husky": {
    "hooks": {
      "pre-commit": "npm run lint:fix && pretty-quick --staged"
    }
  },

【问题讨论】:

  • 您的eslint 配置设置是什么?你是如何运行 Prettier - 作为脚本的一部分,或者作为例如的一部分自动运行VSCode?​​span>
  • 嗨杰瑞德。谢谢你的通知。请检查我的更新。
  • 更漂亮让styled-components更丑
  • 您最终找到了可行的解决方案吗?面临同样的问题。
  • 不幸的是没有,这有点烦人

标签: reactjs formatting eslint styled-components prettier


【解决方案1】:

您是否考虑过尝试eslint-config-prettier

来自 Prettier install 文档:

如果您使用 ESLint,请安装 eslint-config-prettier 以使 ESLint 和 Prettier 相互配合。它会关闭所有不必要或可能与 Prettier 冲突的 ESLint 规则。

查看source,它似乎会关闭 ESLint 抱怨的 indentoperator-linebreak 规则。


我看到你注意到了:

我不想更改 Eslint 规则,因为它们在“真实”用例中非常有用。

这两条规则真的有用吗?它们似乎属于“格式化规则”而不是“代码质量规则”的范畴,所以我想知道让 Prettier 做它的目的有什么害处。 (Reference)

在我看来,您的问题来自于使用 ESLint 和 Prettier,但让它们的职责重叠(eslint-config-prettier 可以解决)。

替代解决方案

如果您真的想换个方向并修改 Prettier 的行为,也许您可​​以忽略文件或使用内联 cmets:https://prettier.io/docs/en/ignore.html

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 2021-07-16
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2020-06-25
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多