简体   繁体   English

通过 WebStorm 中的 ESLint 规则进行更漂亮的集成

[英]Prettier integration by ESLint rules in WebStorm

I created the rules in ESLint, how can I make Prettier format the code according to the rules specified in ESLint.我在 ESLint 中创建了规则,如何根据 ESLint 中指定的规则使 Prettier 格式化代码?

.eslintrc.js .eslintrc.js

  module.exports = {
   rules: {
      quotes: ["error", "single"]
   }
  };

Before saving:保存前:

保存前

After saving, Prettier automatically formats the code:保存后,Prettier 会自动格式化代码:

保存后,Prettier 会自动格式化代码。

I want to use Prettier and ESLint.我想使用 Prettier 和 ESLint。 Without using .prettierrc.js -> singleQuote: true, .不使用.prettierrc.js -> singleQuote: true, . in order for Prettier to format the file according to the ESLint rules, before saving, Prettier takes the rules specified in eslintrc.js and formats the code.为了让 Prettier 按照 ESLint 规则格式化文件,在保存之前,Prettier 会按照 eslintrc.js 中指定的规则对代码进行格式化。 How can I do this?我怎样才能做到这一点?

In VSCode User Settings, set "prettier.eslintIntegration": true在 VSCode 用户设置中,设置"prettier.eslintIntegration": true prettier.eslintIntegration "prettier.eslintIntegration": true

what about WebStorm?网络风暴呢?

I'll take a project created with the create-nuxt-app utility, where ESLint and Prettier are already properly configured for this example.我要与创建的项目创建,nuxt-应用程序,其中ESLint更漂亮已经正确配置的这个例子。

For reference only, here's what the config files look like.仅供参考,这里是配置文件的样子。 Open spoilers to see if needed.打开剧透看看是否需要。

.prettierrc .prettierrc

.prettierrc

.eslintrc.js .eslintrc.js

.eslintrc.js

.editorconfig .editorconfig

.editorconfig

.package.json .package.json

在此处输入图片说明

Next, follow these steps to set up your IDE:接下来,按照以下步骤设置您的 IDE:

  1. Re-import a project.重新导入项目。

    To do it, you should remove the .idea directory from the project root.为此,您应该从项目根目录中删除.idea目录。 Then import the project again.然后再次导入项目。 Note that you will lose all project related settings.请注意,您将丢失所有与项目相关的设置。 But this step is necessary because the IDE configures the Code Style and Editor settings during import.但是这一步是必须的,因为 IDE 在导入的时候会配置Code StyleEditor 设置 You can check this by opening the Event Log.您可以通过打开事件日志来检查这一点。

    事件记录日志

  2. Open .prettierrc and in the dialog "Use code style based on Prettier for this project?"打开.prettierrc并在对话框中“对这个项目使用基于.prettierrc代码样式?” choose No .选择

    .prettierrc,在这里选择否

  3. Open Settings |打开设置 | Languages & Frameworks |语言和框架 | JavaScript | JavaScript | Code Quality Tools |代码质量工具 | ESLint ESLint
    And make sure the Automatic ESLint configuration checkbox is enabled.并确保启用自动 ESLint 配置复选框。

    ESLint 配置

  4. Then open Settings |然后打开设置| Languages & Frameworks |语言和框架 | JavaScript | JavaScript | Prettier and configure the Prettier package . Prettier并配置Prettier 包

    更漂亮的配置

    Now when you press Ctrl + Alt + Shift + P the file will be formatted according to Prittier settings.现在,当您按Ctrl + Alt + Shift + P 时,文件将根据Prittier设置进行格式化。

    But this option does not suit us, since not all rules specified for ESLint will apply.但是这个选项并不适合我们,因为并非所有为ESLint指定的规则都适用。 We need to run eslint --fix instead, which will format the file according to Prettier settings.我们需要运行eslint --fix ,它将根据Prettier设置格式化文件。

    So let's assign the shortcut Ctrl + Alt + Shift + P to run eslint --fix .所以让我们分配快捷键Ctrl + Alt + Shift + P来运行eslint --fix

  5. Remove the Format with Prettier shortcut in Settings |“设置”中删除“使用更漂亮格式”快捷方式| Keymap .键盘映射

    使用 Prettier 快捷方式删除格式

  6. Then reassign the shortcut to Fix ESLint problems .然后将快捷方式重新分配到Fix ESLint questions

    修复 ESLint 问题快捷方式设置

Now when you run Ctrl + Alt + Shift + P your file will be correctly formatted.现在,当您运行Ctrl + Alt + Shift + P 时,您的文件将被正确格式化。

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

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