Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

.eslintrc.js

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

Before saving:

before saving

After saving, Prettier automatically formats the code:

after saving, Prettier automatically formats the code.

I want to use Prettier and ESLint. Without using .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. How can I do this?

In VSCode User Settings, set "prettier.eslintIntegration": true

what about WebStorm?

like image 347
Дмитрий Шарапов Avatar asked Mar 15 '19 07:03

Дмитрий Шарапов


People also ask

How do you use Prettier with ESLint WebStorm?

You can use the Reformat with Prettier action (Opt+Shift+Cmd+P on macOS or Alt+Shift+Ctrl+P on Windows and Linux) to format the selected code, a file, or a whole directory. You can also configure WebStorm to run Prettier on save (Cmd+S/Ctrl+S) or use it as the default formatter (Opt+Cmd+L/Ctrl+Alt+L).

Can Prettier and ESLint work together?

Creating an ESLint and Prettier workflow setup. ESLint and Prettier files can conflict with each other because there are occasions when they end up checking the same rules which can lead to duplication. Fortunately, it is possible to get them both to work together.

How do you add Prettier to ESLint?

Open the terminal in your project root folder and install eslint as a dev dependency. We also need to enable the eslint and prettier extension for the VSCode. So visit the extensions section of VSCode (ctrl + shift + x) and search for Eslint and Prettier — Code formatter and install it.


1 Answers

I'll take a project created with the create-nuxt-app utility, where ESLint and Prettier are already properly configured for this example.

For reference only, here's what the config files look like. Open spoilers to see if needed.

.prettierrc

.prettierrc

.eslintrc.js

.eslintrc.js

.editorconfig

.editorconfig

.package.json

enter image description here

Next, follow these steps to set up your IDE:

  1. Re-import a project.

    To do it, you should remove the .idea directory from the project root. 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. You can check this by opening the Event Log.

    Event Log

  2. Open .prettierrc and in the dialog "Use code style based on Prettier for this project?" choose No.

    .prettierrc, choose No here

  3. Open Settings | Languages & Frameworks | JavaScript | Code Quality Tools | ESLint
    And make sure the Automatic ESLint configuration checkbox is enabled.

    ESLint configuration

  4. Then open Settings | Languages & Frameworks | JavaScript | Prettier and configure the Prettier package.

    Prettier configuration

    Now when you press Ctrl+Alt+Shift+P the file will be formatted according to Prittier settings.

    But this option does not suit us, since not all rules specified for ESLint will apply. We need to run eslint --fix instead, which will format the file according to Prettier settings.

    So let's assign the shortcut Ctrl+Alt+Shift+ P to run eslint --fix.

  5. Remove the Format with Prettier shortcut in Settings | Keymap.

    Delete format with Prettier shortcut

  6. Then reassign the shortcut to Fix ESLint problems.

    Fix ESLint problems shortcut setup

Now when you run Ctrl+Alt+Shift+P your file will be correctly formatted.

like image 156
innomerphey Avatar answered Oct 10 '22 02:10

innomerphey