How to make WebStorm format code according to eslint?

WebstormEslint

Webstorm Problem Overview


I've specified eslint configuration for my WebStorm project. But it does not seem to apply to code reformat feature. For example, it continues to format import { something } from 'somewhere' as import {something} from 'somewhere'.

Is there a way to make WebStorm to format code according to eslint configuration?

Webstorm Solutions


Solution 1 - Webstorm

(Described steps and screenshots are from IntelliJ IDEA 2017.2)

You can add a keyboard shortcut to action 'Fix ESLint Problem'. Ensure plugin 'JavaScript Support' is installed and enabled.

First got to Preferences | Language & Frameworks | JavaScript | Code Quality Tools | ESLint and enable it. You will need to define your 'Node interpreter', 'ESlint package' and optional 'Configuration file'.

enter image description here

Next go to Preferences | Keymap and search there for 'eslint'. Now you can add for example the shortcut 'control + shift + L'.

enter image description here

Solution 2 - Webstorm

I have just installed WebStorm 2017 and I do not know if this works for WS 2016. Instead of pressing Option + Command + L, to reformat your code, you could press Option + Enter when your cursor is close to an ESLint error. This will open up the context menu, then select the "ESLint: Fix current file" enter image description here

Solution 3 - Webstorm

If you are using a newer version of WebStorm like me, you can import the ESLint rules into WebStorm by right-clicking on the .eslintrc.js (or other ESLint configuration files) and then click the "Apply ESLint Code Style Rules" option as shown here.

Do note as the time of writing, you will also need to exclude the indentation of <script> tags in the "Settings" or "Preference" Menu. An awesome guy wrote a short write-up on how to do that.

Solution 4 - Webstorm

Update: the answer is obsolete. The best solution is described in https://stackoverflow.com/a/46099408/1057218

Unfortunately you cannot import eslint code style configuration (WEB-19350) but you can configure the code style manually.

See the settings: "File | Settings | Editor | Code Style | JavaScript"
For import braces: "Spaces" -> "ES6 import / export braces"

Solution 5 - Webstorm

Solved for me in 2021 :

Webstorm is using Prettier package and there is an option:

Preferences | Languages & Frameworks | JavaScript | Prettier -> On 'Reformat Code' action

Now Webstorm uses eslint rules to reformat the code on Reformat Code hotkey.

enter image description here

Solution 6 - Webstorm

A temporary fix for now that I've been using is to export my ESLint config to JSCS. It works pretty nicely with WebStorm and PHPStorm!

I used this package called Polyjuice, and here is the output from my eslint config.

Solution 7 - Webstorm

The best way is to automate it by using a Macro.

  1. Go to Edit | Macro | Start Macro Recording
  2. Go to File | Save All
  3. Press Strg + Shift + A
  4. Search for eslint and run it.
  5. Go to Edit | Macro | Stop Macro Recording
  6. Go to Edit | Macro | Edit Macros and remove everything expect the two Action:...
  7. Go to File | Settings | Keymap | Macros and add Strg + S on the Macro

    This works great and makes it a lot more easier to work :)

Solution 8 - Webstorm

For those still in trouble, I did all the steps of the answers above and it was not working at all.

For me the problem was the Node version I was using in my terminal (12.x) vs the .nvmrc and "engine" in the package.json of the project

enter image description here

enter image description here

After the two changes pointed above my eslint returned to work.

p.s: I'm assuming you have all the dependencies installed as well as a .eslintrc.* in the root of your project

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiberianGuyView Question on Stackoverflow
Solution 1 - WebstormphseView Answer on Stackoverflow
Solution 2 - WebstormOyvind HabberstadView Answer on Stackoverflow
Solution 3 - WebstormnixklaiView Answer on Stackoverflow
Solution 4 - WebstormanstarovoytView Answer on Stackoverflow
Solution 5 - WebstormuptoyouView Answer on Stackoverflow
Solution 6 - WebstormCameronView Answer on Stackoverflow
Solution 7 - WebstormLeon338View Answer on Stackoverflow
Solution 8 - WebstormlucianokrebsView Answer on Stackoverflow