简体   繁体   English

ESLint > 自动美化和空格缩进

[英]ESLint > Auto-beautify and space-indentation

I just realized that there is something better than JSLint and I am having a bit of a hassle with it.我刚刚意识到有比JSLint更好的东西,但我对它有点麻烦。 I am referring to ESLint .我指的是ESLint

I am setting it up for VS Code , with the official Plugin by Dirk Baeumer .我正在为VS Code设置它,使用Dirk Ba​​eumer的官方插件

So far, everything is running great, until I ran into the indent setting.到目前为止,一切都运行良好,直到我遇到indent设置。

I am using a customized auto-beautify setting for my editor and it defaults to 4 spaces/tabs indentation on save.我正在为我的编辑器使用自定义的自动美化设置,它在保存时默认为 4 个空格/制表符缩进。

I am getting 2 errors from ESLint due to integrated code convention types:由于集成的代码约定类型,我从 ESLint 收到 2 个错误:

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

and also, for some other case scenarios, I get the following:而且,对于其他一些情况,我得到以下信息:

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

and so on.. 10 but 12 , 12 but 14... you get the point.等等.. 10 但 12 , 12 但 14 ......你明白了。

How can I default ALL the indentations troughout my document to be 4 spaces?如何将文档中的所有缩进默认为 4 个空格?

My current .eslintrc settings:我当前的.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
}
}

My "indent": [2, 4], is not working the way I expect it to.我的"indent": [2, 4],没有按照我期望的方式工作。

Sample code for reference:参考示例代码:

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 configurations are cascading. ESLint 配置是级联的。 Meaning that you can have multiple .eslintrc files in different subdirectories and they will be merged at lint time.这意味着您可以在不同的子目录中拥有多个.eslintrc文件,它们将在 lint 时合并。 Your configuration for indent rule looks correct, but based on the error messages you are getting, chances are - it's being overridden by something that sets default number of indentation spaces to 2.您的indent规则配置看起来是正确的,但根据您收到的错误消息,很可能 - 它被将默认缩进空格数设置为 2 的某些内容覆盖。

To figure out what is going on, you can use two different commands in ESLint.要弄清楚发生了什么,您可以在 ESLint 中使用两个不同的命令。 First, you can run (from command line) ESLint with print-config option.首先,您可以使用print-config选项运行(从命令行)ESLint。 It requires a file to lint, and it will output computed configuration that ESLint will use for this file.它需要一个文件来 lint,并且它将输出 ESLint 将用于该文件的计算配置。 Verify that indent is set to [2, 4] in that configuration.验证在该配置中缩进设置为 [2, 4]。 If it's not, you can run ESLint with debug flag, which should print out locations of all config files that are being used.如果不是,您可以使用 调试标志运行 ESLint,它应该打印出所有正在使用的配置文件的位置。 That should show you which file overrides your default configuration.这应该会告诉你哪个文件覆盖了你的默认配置。

Problem fixed.问题已解决。

The issue was that while configuring the .eslintrc file using the terminal it somehow created another .eslintrc file within my */js/ directory by itself, which contains some default settings which override my */root/ file.问题在于,在使用终端配置.eslintrc文件时,它以某种方式在我的*/js/目录中自行创建了另一个.eslintrc文件,其中包含一些覆盖我的*/root/文件的默认设置。

If you are experiencing the same issue - search for a second .eslintrc file within your project folder.如果您遇到同样的问题 - 在您的项目文件夹中搜索第二个.eslintrc文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM