How to set "indent"
in .eslintr.json
to match the default used in WebStorm?
Everything I've tried so far, as per the official documentation can't match it:
"indent": ["error", 2]
- gives manyExpected indentation of 2 spaces but found 4
"indent": ["error", 4]
- gives manyExpected indentation of 4 spaces but found 8
"indent": ["error", 8]
- gives manyExpected indentation of 8 spaces but found 4
My complete eslint configuration:
{
"env": {
"es6": true,
"node": true,
"jasmine": true
},
"extends": "eslint:recommended",
"parserOptions": {
},
"rules": {
"no-else-return": "error",
"no-multi-spaces": "error",
"no-whitespace-before-property": "error",
"camelcase": "error",
"new-cap": "error",
"no-console": "error",
"comma-dangle": "error",
"no-var": "error",
"indent": ["error", 4],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
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)
const a = 123;
switch (a) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
break;
}
results in:
3:1 error Expected indentation of 0 spaces but found 4
4:1 error Expected indentation of 4 spaces but found 8
5:1 error Expected indentation of 0 spaces but found 4
6:1 error Expected indentation of 4 spaces but found 8
7:1 error Expected indentation of 0 spaces but found 4
8:1 error Expected indentation of 4 spaces but found 8
9:1 error Expected indentation of 0 spaces but found 4
10:1 error Expected indentation of 4 spaces but found 8
example 2
obj.format('text', {
value: '${two}'
}
);
results in:
2:1 error Expected indentation of 4 spaces but found 8
3:1 error Expected indentation of 0 spaces but found 4
example 3
return begin()
.then(() => {
return callback()
.then(data => {
success = true;
return commit();
}, reason => {
return rollback();
})
},
function (reason) {
update(false, false, reason);
return $p.reject(reason);
});
results in:
3:1 error Expected indentation of 8 spaces but found 12
4:1 error Expected indentation of 12 spaces but found 16
5:1 error Expected indentation of 16 spaces but found 20
6:1 error Expected indentation of 16 spaces but found 20
7:1 error Expected indentation of 12 spaces but found 16
8:1 error Expected indentation of 16 spaces but found 20
9:1 error Expected indentation of 12 spaces but found 16
10:1 error Expected indentation of 4 spaces but found 8
11:1 error Expected indentation of 4 spaces but found 8
12:1 error Expected indentation of 8 spaces but found 12
13:1 error Expected indentation of 8 spaces but found 12
14:1 error Expected indentation of 4 spaces but found 8
How to set "indent"
in .eslintr.json
to match the default used in WebStorm?
Everything I've tried so far, as per the official documentation can't match it:
"indent": ["error", 2]
- gives manyExpected indentation of 2 spaces but found 4
"indent": ["error", 4]
- gives manyExpected indentation of 4 spaces but found 8
"indent": ["error", 8]
- gives manyExpected indentation of 8 spaces but found 4
My complete eslint configuration:
{
"env": {
"es6": true,
"node": true,
"jasmine": true
},
"extends": "eslint:recommended",
"parserOptions": {
},
"rules": {
"no-else-return": "error",
"no-multi-spaces": "error",
"no-whitespace-before-property": "error",
"camelcase": "error",
"new-cap": "error",
"no-console": "error",
"comma-dangle": "error",
"no-var": "error",
"indent": ["error", 4],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
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)
const a = 123;
switch (a) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
break;
}
results in:
3:1 error Expected indentation of 0 spaces but found 4
4:1 error Expected indentation of 4 spaces but found 8
5:1 error Expected indentation of 0 spaces but found 4
6:1 error Expected indentation of 4 spaces but found 8
7:1 error Expected indentation of 0 spaces but found 4
8:1 error Expected indentation of 4 spaces but found 8
9:1 error Expected indentation of 0 spaces but found 4
10:1 error Expected indentation of 4 spaces but found 8
example 2
obj.format('text', {
value: '${two}'
}
);
results in:
2:1 error Expected indentation of 4 spaces but found 8
3:1 error Expected indentation of 0 spaces but found 4
example 3
return begin()
.then(() => {
return callback()
.then(data => {
success = true;
return commit();
}, reason => {
return rollback();
})
},
function (reason) {
update(false, false, reason);
return $p.reject(reason);
});
results in:
3:1 error Expected indentation of 8 spaces but found 12
4:1 error Expected indentation of 12 spaces but found 16
5:1 error Expected indentation of 16 spaces but found 20
6:1 error Expected indentation of 16 spaces but found 20
7:1 error Expected indentation of 12 spaces but found 16
8:1 error Expected indentation of 16 spaces but found 20
9:1 error Expected indentation of 12 spaces but found 16
10:1 error Expected indentation of 4 spaces but found 8
11:1 error Expected indentation of 4 spaces but found 8
12:1 error Expected indentation of 8 spaces but found 12
13:1 error Expected indentation of 8 spaces but found 12
14:1 error Expected indentation of 4 spaces but found 8
Share
Improve this question
edited Jun 27, 2017 at 13:20
vitaly-t
asked Jun 24, 2017 at 14:58
vitaly-tvitaly-t
25.9k17 gold badges127 silver badges150 bronze badges
8
- So you are getting eslint errors? When exactly? – ush189 Commented Jun 26, 2017 at 9:50
- @ush189 all over the place, that's the thing, no matter what I've tried, WebStorm seems to have a mind of its own about code formatting. – vitaly-t Commented Jun 26, 2017 at 10:40
- Your settings look fine. Could you post your eslint config? – ush189 Commented Jun 26, 2017 at 11:03
- @ush189 I have update my question for that ;) – vitaly-t Commented Jun 26, 2017 at 12:31
- I have the same webstorm settings and I just copied your eslint config and everything seem to work fine. So I think you also need to show us a screenshot of your code with the error message ;) – ush189 Commented Jun 26, 2017 at 13:07
3 Answers
Reset to default 11Switch-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:
"indent": [
"error",
4,
{"SwitchCase": 1}
]
So your complete eslint config will now look like this:
{
"env": {
"es6": true,
"node": true,
"jasmine": true
},
"extends": "eslint:recommended",
"parserOptions": {
},
"rules": {
"no-else-return": "error",
"no-multi-spaces": "error",
"no-whitespace-before-property": "error",
"camelcase": "error",
"new-cap": "error",
"no-console": "error",
"comma-dangle": "error",
"no-var": "error",
"indent": ["error", 4, {"SwitchCase": 1}],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
Regarding your 2nd example I think it is common to write it like this:
obj.format('text', {
value: '${two}'
});
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:
return begin()
.then(() => callback()
.then(data => {
success = true;
return commit();
}, reason => {
return rollback();
}),
function(reason) {
update(false, false, reason);
return $p.reject(reason);
});
I have fixed it by added the indent configuration into the file .eslintrc.js
module.exports = {
"rules": {
"indent": ["error", 4]
}
};
Seems you are getting the error due to WebStorm
formatting.
Solution
In webstorm
:
File
->Setting
- Go to
Editor
->Code Style
->HTML
- In
Do not indent children of
, add tagscript
. - Then format the source code again.
Then the error should be gone.
Screen shot: