no-multi-spaces
禁止多个空格
此规则报告的一些问题可以通过 --fix
命令行 选项自动修复
一行中未用于缩进的多个空格通常是错误。例如
if(foo === "bar") {}
很难分辨,但在 foo
和 ===
之间有两个空格。通常不赞成使用这样的多个空格,而赞成使用单个空格
if(foo === "bar") {}
规则详情
此规则旨在禁止在逻辑表达式、条件表达式、声明、数组元素、对象属性、序列和函数参数周围使用多个空格。
此规则的错误代码示例
/*eslint no-multi-spaces: "error"*/
var a =1;
if(foo=== "bar") {}
a << b
var arr = [1,2];
a ? b: c
此规则的正确代码示例
/*eslint no-multi-spaces: "error"*/
var a = 1;
if(foo === "bar") {}
a << b
var arr = [1, 2];
a ? b: c
选项
此规则的配置由具有以下属性的对象组成
"ignoreEOLComments": true
(默认为false
)忽略行尾注释前的多个空格"exceptions": { "Property": true }
("Property"
是默认情况下唯一指定的节点)指定要忽略的节点
ignoreEOLComments
对于 { "ignoreEOLComments": false }
(默认)选项,此规则的错误代码示例
/*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
var x = 5;// comment
var x = 5;/* multiline
* comment
*/
对于 { "ignoreEOLComments": false }
(默认)选项,此规则的正确代码示例
/*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
var x = 5; // comment
var x = 5; /* multiline
* comment
*/
对于 { "ignoreEOLComments": true }
选项,此规则的正确代码示例
/*eslint no-multi-spaces: ["error", { ignoreEOLComments: true }]*/
var x = 5; // comment
var x = 5; // comment
var x = 5; /* multiline
* comment
*/
var x = 5; /* multiline
* comment
*/
exceptions
为了避免与其他需要多个空格的规则发生冲突,此规则具有 exceptions
选项来忽略某些节点。
此选项是一个对象,期望属性名称为 ESTree 定义的 AST 节点类型。确定 exceptions
的节点类型的最简单方法是使用带有 espree 解析器的 AST Explorer。
默认情况下仅忽略 Property
节点类型,因为对于 key-spacing 规则,某些对齐选项需要在对象字面量的属性中使用多个空格。
对于默认的 "exceptions": { "Property": true }
选项,此规则的正确代码示例
/*eslint no-multi-spaces: "error"*/
/*eslint key-spacing: ["error", { align: "value" }]*/
var obj = {
first: "first",
second: "second"
};
对于 "exceptions": { "Property": false }
选项,此规则的错误代码示例
/*eslint no-multi-spaces: ["error", { exceptions: { "Property": false } }]*/
/*eslint key-spacing: ["error", { align: "value" }]*/
var obj = {
first:"first",
second: "second"
};
对于 "exceptions": { "BinaryExpression": true }
选项,此规则的正确代码示例
/*eslint no-multi-spaces: ["error", { exceptions: { "BinaryExpression": true } }]*/
var a = 1 * 2;
对于 "exceptions": { "VariableDeclarator": true }
选项,此规则的正确代码示例
/*eslint no-multi-spaces: ["error", { exceptions: { "VariableDeclarator": true } }]*/
var someVar = 'foo';
var someOtherVar = 'barBaz';
对于 "exceptions": { "ImportDeclaration": true }
选项,此规则的正确代码示例
/*eslint no-multi-spaces: ["error", { exceptions: { "ImportDeclaration": true } }]*/
import mod from 'mod';
import someOtherMod from 'some-other-mod';
何时不使用
如果您不想检查和禁止多个空格,则应关闭此规则。
相关规则
版本
此规则在 ESLint v0.9.0 中引入。