ESLint > 自动美化和 space-缩进

ESLint > Auto-beautify and space-indentation

我刚刚意识到还有比JSLint and I am having a bit of a hassle with it. I am referring to ESLint更好的东西。

我正在设置 VS Code, with the official Plugin by Dirk Baeumer

到目前为止,一切都运行很好,直到我运行进入indent设置。

我正在为我的编辑器使用自定义的自动美化设置,保存时默认为 4 spaces/tabs 缩进。

由于集成代码约定类型,我从 ESLint 收到 2 个错误:

[ESLint] Expected indentaion of 2 spaces but found 4. (indent)

此外,对于其他一些情况,我得到以下信息:

[ESLint] Expected indendation of 6 spaces but found 8. (indent)

等等.. 10 but 12 , 12 but 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 option. It requires a file to lint, and it will output computed configuration that ESLint will use for this file. Verify that indent is set to [2, 4] in that configuration. If it's not, you can run ESLint with debug 标志的 ESLint,它应该打印出所有正在使用的配置文件的位置。那应该会告诉你哪个文件覆盖了你的默认配置。

问题已解决。

问题是,在使用终端配置 .eslintrc 文件时,它以某种方式在我的 */js/ 目录中自行创建了另一个 .eslintrc 文件,其中包含一些覆盖我的默认设置*/root/ 文件。

If you are experiencing the same issue - search for a second .eslintrc file within your project folder.