node.js Eslint 预期缩进 1 个制表符,但发现 4 个空格错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43301014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:09:35  来源:igfitidea点击:

Eslint expected indentation of 1 tab but found 4 spaces error

node.jsconfigeslint

提问by JohnSnow

I am using VScode with latest version of Eslint. It is my first time using a linter.

我正在使用 VScode 和最新版本的 Eslint。这是我第一次使用 linter。

I keep getting this linting error when using a tab as indentation:

使用制表符作为缩进时,我不断收到此 linting 错误:

severity: 'Error' message: 'Expected indentation of 1 tab but found 4 spaces. (indent)' at: '4,5' source: 'eslint'

严重性:“错误”消息:“需要缩进 1 个制表符,但发现 4 个空格。(indent)' at: '4,5' 来源:'eslint'

Here is my config file

这是我的配置文件

{
"env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "node": true
},
"extends": "eslint:recommended",
"rules": {
    "indent": [
        "error",
        "tab"
    ],
    "linebreak-style": [
        "error",
        "unix"
    ],
    "quotes": [
        "error",
        "single"
    ],
    "semi": [
        "error",
        "always"
    ]
}
}

I don't understand why this error is being thrown as I indicated tabs for indentation. It is obviously calculating my 1 tab as 4 spaced but I don't understand why it is doing that when I am pressing tab for indentation.

我不明白为什么在我指示缩进的制表符时会抛出此错误。它显然是将我的 1 个制表符计算为 4 个间隔,但我不明白为什么当我按下制表符进行缩进时会这样做。

update:The reason is because in VScode using ctrl + shift + ito beautify code will actually use spaces and not tabs. That is the reason.

更新:原因是因为在 VScode 中使用ctrl + shift + i美化代码实际上会使用空格而不是制表符。这就是原因。

回答by Gary

In VSCODE, go to menu:

在 VSCODE 中,转到菜单:

File / Preferences / Settingsthen Text Editor(or just search for 'tab' in the search settings box)

File / Preferences / Settings然后Text Editor(或只是在搜索设置框中搜索“标签”)

Then remove the tick from the following settings:

然后从以下设置中删除勾号:

  • Insert Spaces
  • Detect Indentation
  • Insert Spaces
  • Detect Indentation

Also to auto format the document with the default VSCode keyboard shortcut use:

还要使用默认的 VSCode 键盘快捷键自动格式化文档,请使用:

Shift+Alt+f

Shift+ Alt+f

回答by Sergey Yarotskiy

Wee, that exactly what it says. You have in your config "indent": [ "error", "tab" ], So it expects tab as indent. But found in your file 4 spaces. Remove spaces and put tab in you file

Wee,这正是它所说的。您的配置中有“缩进”:[“错误”,“制表符”],因此它期望制表符为缩进。但是在你的文件中发现了4个空格。删除空格并将制表符放入您的文件中

回答by Reza Khodarahmi

I used VScode to solve this problem. All you have to do is hold the mouse over the part where there is an error. enter image description here

我用VScode来解决这个问题。您所要做的就是将鼠标悬停在出现错误的部分上。 在此处输入图片说明

and... enter image description here

和... 在此处输入图片说明