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

时间:2017-05-23 09:57:47

标签: javascript eslint

如何正确配置规则以在带有大括号的代码块之前强制执行该规则,它只包含一个空格而不是多个空格或制表符?

到目前为止,我一直在使用:

/* 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');
}

1 个答案:

答案 0 :(得分:0)

您可以使用no-multi-spaces!将/* no-multi-spaces: "error" */添加到您的示例中,然后在demo中尝试:

/* 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');
}

这应该会给你你想要的错误:

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