vit*_*y-t 4 javascript webstorm eslint
如何设置"indent"在.eslintr.json以匹配WebStorm使用的默认?
到目前为止,根据官方文档,我尝试过的所有内容都无法与之匹配:
"indent": ["error", 2] -给很多 Expected indentation of 2 spaces but found 4"indent": ["error", 4] -给很多 Expected indentation of 4 spaces but found 8"indent": ["error", 8] -给很多 Expected indentation of 8 spaces but found 4我完整的eslint配置:
{
"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"
]
}
}
Run Code Online (Sandbox Code Playgroud)
键入代码时,我总是使用Ctrl+Alt+L自动格式化代码,并且产生的代码格式不符合任何eslint设置。
更新
根据要求,提供了一个代码示例"indent": ["error", 4]:
对于此代码:(通过Ctrl + Alt + L格式化)
const a = 123;
switch (a) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
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
Run Code Online (Sandbox Code Playgroud)
例子2
obj.format('text', {
value: '${two}'
}
);
Run Code Online (Sandbox Code Playgroud)
结果是:
2:1 error Expected indentation of 4 spaces but found 8
3:1 error Expected indentation of 0 spaces but found 4
Run Code Online (Sandbox Code Playgroud)
例子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);
});
Run Code Online (Sandbox Code Playgroud)
结果是:
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
Run Code Online (Sandbox Code Playgroud)
Switch-Case似乎是eslint关于缩进的一种特殊情况。默认情况下,case子句不相对于缩进switch:
“ SwitchCase”(默认值:0)对switch语句中的case子句强制执行缩进级别
参见此处的示例:http : //eslint.org/docs/rules/indent#switchcase
您需要将SwitchCaseoption 设置为1,如下所示:
"indent": [
"error",
4,
{"SwitchCase": 1}
]
Run Code Online (Sandbox Code Playgroud)
因此,您的完整eslint配置现在将如下所示:
{
"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"
]
}
}
Run Code Online (Sandbox Code Playgroud)
关于您的第二个示例,我认为这样写是很常见的:
obj.format('text', {
value: '${two}'
});
Run Code Online (Sandbox Code Playgroud)
两个括号都在同一行上打开,因此您在同一行上将它们关闭。如果您在那几行上使用自动格式,则它们不会更改。
第三个示例看起来有些棘手。我不知道您是否可以在同一页面上获得eslint和自动格式设置。我个人更喜欢使用eslint方式,但是我不知道您是否可以调整auto格式来做到这一点。
编辑:您可以这样写:
return begin()
.then(() => callback()
.then(data => {
success = true;
return commit();
}, reason => {
return rollback();
}),
function(reason) {
update(false, false, reason);
return $p.reject(reason);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3593 次 |
| 最近记录: |