Eslint
规则 | Rules

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选项忽略某些节点。

选项是一个期望属性名称为ESTree定义的AST节点类型的对象。确定节点类型exceptions的最简单方法是使用在线演示。

只有Property节点类型在默认情况下被忽略,因为对于键间距规则,一些对齐选项在对象文字的属性中需要多个空格。

"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';

何时不使用它

如果你不想检查和禁止多个空格,那么你应该关闭这个规则。

相关规则

  • key-spacing

  • space-infix-ops

  • space-in-brackets (deprecated)

  • space-in-parens

  • space-after-keywords

  • space-unary-ops

  • space-return-throw-case

版本

该规则在ESLint 0.9.0中引入。

资源