no-multi-spaces
不允许多个空格
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
连续出现的多个空格,如果不是用于缩进,通常是错误。例如:
🌐 Multiple spaces in a row that are not used for indentation are typically mistakes. For example:
if(foo === "bar") {}
很难看出来,但在 foo 和 === 之间有两个空格。像这样的多个空格通常不被赞同,倾向于使用单个空格:
🌐 It’s hard to tell, but there are two spaces between foo and ===. Multiple spaces such as this are generally frowned upon in favor of single spaces:
if(foo === "bar") {}
规则详情
🌐 Rule Details
该规则旨在禁止在逻辑表达式、条件表达式、声明、数组元素、对象属性、序列和函数参数周围出现多个空格。
🌐 This rule aims to disallow multiple whitespace around logical expressions, conditional expressions, declarations, array elements, object properties, sequences and function parameters.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-multi-spaces: "error"*/
var a = 1;
if(foo === "bar") {}
a << b
var arr = [1, 2];
a ? b: c
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-multi-spaces: "error"*/
var a = 1;
if(foo === "bar") {}
a << b
var arr = [1, 2];
a ? b: c
选项
🌐 Options
此规则的配置由具有以下属性的对象组成:
🌐 This rule’s configuration consists of an object with the following properties:
"ignoreEOLComments": true(默认值为false)会忽略出现在行尾的注释前的多个空格"exceptions": { "Property": true }(默认情况下只指定了"Property"节点)指定要忽略的节点
ignoreEOLComments
使用 { "ignoreEOLComments": false }(默认)选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "ignoreEOLComments": false } (default) option:
/*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
var x = 5; // comment
var x = 5; /* multiline
* comment
*/
此规则在使用 { "ignoreEOLComments": false }(默认)选项时的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreEOLComments": false } (default) option:
/*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
var x = 5; // comment
var x = 5; /* multiline
* comment
*/
使用 { "ignoreEOLComments": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreEOLComments": true } option:
/*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 选项用来忽略某些节点。
🌐 To avoid contradictions with other rules that require multiple spaces, this rule has an exceptions option to ignore certain nodes.
此选项是一个对象,期望属性名称为由 ESTree 定义的 AST 节点类型。确定 exceptions 节点类型的最简单方法是使用带有 espree 解析器的 AST Explorer。
🌐 This option is an object that expects property names to be AST node types as defined by ESTree. The easiest way to determine the node types for exceptions is to use AST Explorer with the espree parser.
默认情况下,只有 Property 节点类型会被忽略,因为对于 key-spacing 规则,一些对齐选项要求对象字面量的属性中有多个空格。
🌐 Only the Property node type is ignored by default, because for the key-spacing rule some alignment options require multiple spaces in properties of object literals.
默认 "exceptions": { "Property": true } 选项的正确代码示例:
🌐 Examples of correct code for the default "exceptions": { "Property": true } option:
/*eslint no-multi-spaces: "error"*/
/*eslint key-spacing: ["error", { align: "value" }]*/
var obj = {
first: "first",
second: "second"
};
针对 "exceptions": { "Property": false } 选项的错误代码示例:
🌐 Examples of incorrect code for the "exceptions": { "Property": false } option:
/*eslint no-multi-spaces: ["error", { exceptions: { "Property": false } }]*/
/*eslint key-spacing: ["error", { align: "value" }]*/
var obj = {
first: "first",
second: "second"
};
适用于 "exceptions": { "BinaryExpression": true } 选项的正确代码示例:
🌐 Examples of correct code for the "exceptions": { "BinaryExpression": true } option:
/*eslint no-multi-spaces: ["error", { exceptions: { "BinaryExpression": true } }]*/
var a = 1 * 2;
适用于 "exceptions": { "VariableDeclarator": true } 选项的正确代码示例:
🌐 Examples of correct code for the "exceptions": { "VariableDeclarator": true } option:
/*eslint no-multi-spaces: ["error", { exceptions: { "VariableDeclarator": true } }]*/
var someVar = 'foo';
var someOtherVar = 'barBaz';
适用于 "exceptions": { "ImportDeclaration": true } 选项的正确代码示例:
🌐 Examples of correct code for the "exceptions": { "ImportDeclaration": true } option:
/*eslint no-multi-spaces: ["error", { exceptions: { "ImportDeclaration": true } }]*/
import mod from 'mod';
import someOtherMod from 'some-other-mod';
何时不使用
🌐 When Not To Use It
如果你不想检查并禁止多个空格,则应关闭此规则。
🌐 If you don’t want to check and disallow multiple spaces, then you should turn this rule off.
相关规则
版本
此规则是在 ESLint v0.9.0 中引入。