no-multi-spaces
不允许多个空格
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
一行中的多个不用于缩进的空格通常是错误的。例如:
¥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
)忽略行尾注释前的多个空格¥
"ignoreEOLComments": true
(defaults tofalse
) ignores multiple spaces before comments that occur at the end of lines -
"exceptions": { "Property": true }
("Property"
是默认指定的唯一节点)指定要忽略的节点¥
"exceptions": { "Property": true }
("Property"
is the only node specified by default) specifies nodes to ignore
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
的节点类型的最简单方法是将 AST Explorer 与 espree 解析器一起使用。
¥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 中引入。