gpt4 book ai didi

javascript - 如何为 WebStorm 配置 eslint 缩进?

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:18 25 4
gpt4 key购买 nike

如何在 .eslintr.json 中设置 "indent" 以匹配 WebStorm 中使用的默认值?

enter image description here

根据 the official documentation 到目前为止我已经尝试过的一切无法匹配:

  • "indent": ["error", 2] - 给出许多预期缩进 2 个空格但发现 4 个
  • "indent": ["error", 4] - 给出许多预期缩进 4 个空格但发现 8 个
  • "indent": ["error", 8] - 给出许多预期缩进 8 个空格但发现 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"
]
}
}

当我输入代码时,我总是使用 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;
}

结果:

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

例子2

obj.format('text', {
value: '${two}'
}
);

结果:

2:1   error  Expected indentation of 4 spaces but found 8
3:1 error Expected indentation of 0 spaces but found 4

示例 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);
});

结果:

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

最佳答案

Switch-Case 似乎是 eslint 关于缩进的特例。默认情况下,case 子句相对于 switch 不缩进:

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

示例见此处:http://eslint.org/docs/rules/indent#switchcase

您需要像这样将 SwitchCase 选项设置为 1:

"indent": [
"error",
4,
{"SwitchCase": 1}
]

所以你完整的 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"
]
}
}

关于你的第二个例子,我认为这样写是很常见的:

obj.format('text', {
value: '${two}'
});

两个括号都在同一行打开,因此您将它们关闭在同一行。如果您在该行上使用自动格式,它们将不会改变。

第三个例子看起来有点棘手。我不知道您是否可以在同一页面上获得 eslint 和自动格式。我个人更喜欢 eslint 方式,但我不知道您是否可以调整自动格式来做到这一点。

编辑:你可以这样写:

return begin()
.then(() => callback()
.then(data => {
success = true;
return commit();
}, reason => {
return rollback();
}),
function(reason) {
update(false, false, reason);
return $p.reject(reason);
});

关于javascript - 如何为 WebStorm 配置 eslint 缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com