【问题标题】:What's the difference between prettier-eslint, eslint-plugin-prettier and eslint-config-prettier?prettier-eslint、eslint-plugin-prettier 和 eslint-config-prettier 有什么区别?
【发布时间】:2017-11-25 04:42:59
【问题描述】:

我想同时使用 Prettier 和 ESLint,但是我一个接一个地使用就遇到了一些冲突。我看到有这三个包似乎允许它们串联使用:

  • prettier-eslint
  • eslint-plugin-prettier
  • eslint-config-prettier

但是,我不确定要使用哪个,因为这些包名称都包含 eslintprettier

我应该使用哪个?

【问题讨论】:

    标签: javascript ecmascript-6 eslint code-formatting prettier


    【解决方案1】:

    tl;dr:使用eslint-config-prettier,其余的可以忽略。

    ESLint 包含许多规则,与格式相关的规则可能会与 Prettier 冲突,例如arrow-parensspace-before-function-paren 等。因此将它们一起使用会导致一些问题。已经创建了以下工具来同时使用 ESLint 和 Prettier。

    prettier-eslint eslint-plugin-prettier eslint-config-prettier
    What it is A JavaScript module exporting a single function. An ESLint plugin. An ESLint configuration.
    What it does Runs the code (string) through prettier then eslint --fix. The output is also a string. Plugins usually contain implementations for additional rules that ESLint will check for. This plugin uses Prettier under the hood and will raise ESLint errors when your code differs from Prettier's expected output. This config turns off formatting-related rules that might conflict with Prettier, allowing you to use Prettier with other ESLint configs like eslint-config-airbnb.
    How to use it Either calling the function in your code or via prettier-eslint-cli if you prefer the command line. Add it to your .eslintrc. Add it to your .eslintrc.
    Is the final output Prettier compliant? Depends on your ESLint config Yes Yes
    Do you need to run prettier command separately? No No Yes
    Do you need to use anything else? No You may want to turn off conflicting rules using eslint-config-prettier. No

    有关详细信息,请参阅official Prettier docs

    推荐的做法是让 Prettier 处理非格式问题的格式和 ESLint,prettier-eslint 与该做法的方向不同,因此不再推荐 prettier-eslint。您可以同时使用eslint-plugin-prettiereslint-config-prettier

    【讨论】:

    • 我有一个 eslintrc 文件,其中有自定义规则。即 no-extra-semi 但是用 prettier 运行 eslint --fix 需要分号,我需要一个单独的规则文件来更漂亮吗?
    • 只是对 eslint 插件和配置之间的一般区别的评论,因为我觉得它是我所缺少的:插件定义了新的 eslint 规则,并且配置设置是否(以及如何)规则应该应用。
    • 有了eslint-config-prettier,我们为什么要跑得更漂亮? eslint --fix 不会以更漂亮的方式格式化代码吗?
    • 现在是 2019 年,这仍然是我找到的最好的解释,比官方的解释要好得多。您可能会补充说,不再推荐 prettier-eslint。而后两者现在可以一起工作了。
    • @YangshunTay 不过我很好奇,谁在推荐这种做法?
    猜你喜欢
    • 2022-01-09
    • 2020-12-25
    • 2018-09-18
    • 2021-05-06
    • 2021-11-30
    • 2021-09-29
    • 2018-08-22
    • 2019-09-07
    • 2023-03-26
    相关资源
    最近更新 更多