Skip to content
Advertisement

How to configure eslint indent for WebStorm?

How to set "indent" in .eslintr.json to match the default used in WebStorm?

enter image description here

Everything I’ve tried so far, as per the official documentation can’t match it:

  • "indent": ["error", 2] – gives many Expected indentation of 2 spaces but found 4
  • "indent": ["error", 4] – gives many Expected indentation of 4 spaces but found 8
  • "indent": ["error", 8] – gives many Expected indentation of 8 spaces but found 4

My complete eslint configuration:

JavaScript

As I type the code, I always use Ctrl+Alt+L to auto-format the code, and the code formatting produced doesn’t comply with any eslint settings.


UPDATE

As was asked, a code example for "indent": ["error", 4]:

For this code: (formatted via Ctrl+Alt+L)

JavaScript

results in:

JavaScript

example 2

JavaScript

results in:

JavaScript

example 3

JavaScript

results in:

JavaScript

Advertisement

Answer

Switch-Case seems to be a special case for eslint regarding indentation. Per default the case clauses are not indented relative to the switch:

“SwitchCase” (default: 0) enforces indentation level for case clauses in switch statements

See here for an example: http://eslint.org/docs/rules/indent#switchcase

You need to set SwitchCase option to 1 like so:

JavaScript

So your complete eslint config will now look like this:

JavaScript

Regarding your 2nd example I think it is common to write it like this:

JavaScript

Both parenthesis are opened on the same line, so you close them on the same line. If you use auto format on that lines, they will not change.

The third example looks a bit tricky. I don’t know if you can get eslint and auto format on the same page for that one. I personally would prefer the eslint way, but I don’t know if you can tweak the auto format to do it like that.

Edit: You could write it like that:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement