简体   繁体   English

当在eslint中启用space-before-blocks时,仅强制执行一个空间而不是几个空间

[英]Enforce only one space isntead of several when space-before-blocks is enabled in eslint

How can properly configure a rule to enforce that before a code block with braces it only contains one single space instead several spaces or tabs? 如何正确配置规则以强制执行在带有大括号的代码块之前它只包含一个空格而不是几个空格或制表符?

So far I have been using: 到目前为止我一直在使用:

/* globals isCorrect, aValue callback */
/*eslint space-before-blocks: "error"*/

if (isCorrect)  { // That should be incorrect, more than one space
    callback('correct');
} else if(aValue == 5) { // That should be correct, only one space
    callback('aValue');
} else      { // That is also incorrect, tabs!
    callback('incorrect');
}

You can use no-multi-spaces for that! 你可以使用no-multi-spaces Add /* no-multi-spaces: "error" */ to your example and try it in the demo : 添加/* no-multi-spaces: "error" */到您的示例并在演示中尝试:

/* globals isCorrect, aValue callback */
/* eslint space-before-blocks: "error" */
/* eslint no-multi-spaces: "error" */

if (isCorrect)  { // That should be incorrect, more than one space
    callback('correct');
} else if(aValue == 5) { // That should be correct, only one space
    callback('aValue');
} else      { // That is also incorrect, tabs!
    callback('incorrect');
}

That should give you the errors you want: 这应该会给你你想要的错误:

5:17 - Multiple spaces found before '{'. (no-multi-spaces)
9:13 - Multiple spaces found before '{'. (no-multi-spaces)

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

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