我刚刚意识到有比 更好的东西JSLint 我有点麻烦。我指的是 ESLint .
我正在为 设置它VS Code ,与官方 Plugin by Dirk Baeumer .
到目前为止,一切都运行良好,直到我遇到 indent
环境。
我正在为我的编辑器使用自定义的自动美化设置,它在保存时默认为 4 个空格/制表符缩进。
由于集成的代码约定类型,我从 ESLint 收到 2 个错误:[ESLint] Expected indentaion of 2 spaces but found 4. (indent)
而且,对于其他一些情况,我得到以下信息:[ESLint] Expected indendation of 6 spaces but found 8. (indent)
等等.. 10 但 12 , 12 但 14 ...... 你明白了。
How can I default ALL the indentations troughout my document to be 4 spaces?
我的当前
.eslintrc
设置:{
"extends": [
"eslint:recommended"
],
"root": true,
"rules": {
// '===' instead of '==' rule
"eqeqeq": [2, "allow-null"],
// ^^^ '===' instead of '==' rule
"indent": [2, 4],
"quotes": [2, "single"],
"semi": [2, "always"],
"no-console": 0
},
"env": {
"es6": false,
"browser": true,
"commonjs": true,
"node": false,
"jquery": true
}
}
我的
"indent": [2, 4],
没有按我期望的方式工作。示例代码供引用:
window.setTimeout(function () {
'use strict';
$(document).ready(function () {
var current_width = $(window).width();
if (current_width <= 481) {
$('.widget-form-list').addClass('supv');
$('.widget-form-item').addClass('supv-form-item');
}
})
// window resize
$(window).resize(function () {
var current_width = $(window).width()
if (current_width < 481) {
$('.widget-form-list').addClass('supv')
$('.widget-form-item').addClass('supv-form-item')
}
})
}, 1000)
最佳答案
ESLint 配置是级联的。这意味着您可以拥有多个 .eslintrc
不同子目录中的文件,它们将在 lint 时合并。您对 indent
的配置规则看起来是正确的,但根据您收到的错误消息,很可能 - 它被将默认缩进空格数设置为 2 的东西覆盖。
要弄清楚发生了什么,您可以在 ESLint 中使用两个不同的命令。首先,您可以使用 print-config 运行(从命令行)ESLint选项。它需要一个文件来进行 lint,它将输出 ESLint 将用于该文件的计算配置。验证在该配置中缩进设置为 [2, 4]。如果不是,您可以使用 debug 运行 ESLint标志,它应该打印出所有正在使用的配置文件的位置。这应该会告诉您哪个文件覆盖了您的默认配置。
关于javascript - ESLint > 自动美化和空格缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41959715/