no-multi-spaces
一行中出现多个空格,且不是用于缩进的,通常是错误。例如
if(foo === "bar") {}很难看出来,但在 foo 和 === 之间有两个空格。通常情况下,这种多个空格是不被推荐的,建议使用单个空格
if(foo === "bar") {}规则详情
此规则旨在禁止在逻辑表达式、条件表达式、声明、数组元素、对象属性、序列和函数参数周围出现多个空格。
此规则的错误代码示例
/*eslint @stylistic/js/no-multi-spaces: "error"*/
var a = 1;
if(foo === "bar") {}
a << b
var arr = [1, 2];
a ? b: c此规则的正确代码示例
/*eslint @stylistic/js/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"是默认情况下指定的唯一节点)指定要忽略的节点"includeTabs": false(默认值为true)将多个制表符或与制表符混合的空格视为多个空格
ignoreEOLComments
此规则的错误代码示例,使用 { "ignoreEOLComments": false }(默认)选项
/*eslint @stylistic/js/no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
var x = 5; // comment
var x = 5; /* multiline
* comment
*/此规则的正确代码示例,使用 { "ignoreEOLComments": false }(默认)选项
/*eslint @stylistic/js/no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
var x = 5; // comment
var x = 5; /* multiline
* comment
*/此规则的正确代码示例,使用 { "ignoreEOLComments": true } 选项
/*eslint @stylistic/js/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 的节点类型的最简单方法是使用 AST Explorer 和 espree 解析器。
默认情况下,只有 Property 节点类型被忽略,因为对于 key-spacing 规则,某些对齐选项需要在对象字面量的属性中使用多个空格。
默认 "exceptions": { "Property": true } 选项的正确代码示例
/*eslint @stylistic/js/no-multi-spaces: "error"*/
/*eslint key-spacing: ["error", { align: "value" }]*/
var obj = {
first: "first",
second: "second"
};"exceptions": { "Property": false } 选项的错误代码示例
/*eslint @stylistic/js/no-multi-spaces: ["error", { exceptions: { "Property": false } }]*/
/*eslint key-spacing: ["error", { align: "value" }]*/
var obj = {
first: "first",
second: "second"
};"exceptions": { "BinaryExpression": true } 选项的正确代码示例
/*eslint @stylistic/js/no-multi-spaces: ["error", { exceptions: { "BinaryExpression": true } }]*/
var a = 1 * 2;"exceptions": { "VariableDeclarator": true } 选项的正确代码示例
/*eslint @stylistic/js/no-multi-spaces: ["error", { exceptions: { "VariableDeclarator": true } }]*/
var someVar = 'foo';
var someOtherVar = 'barBaz';"exceptions": { "ImportDeclaration": true } 选项的正确代码示例
/*eslint @stylistic/js/no-multi-spaces: ["error", { exceptions: { "ImportDeclaration": true } }]*/
import mod from 'mod';
import someOtherMod from 'some-other-mod';includeTabs
对于此规则,将多个制表符 (\t) 或空格与制表符混合视为多个空格。此选项默认为 true。
使用 { "includeTabs": true } 选项时,此规则的错误代码示例
/*eslint @stylistic/js/no-multi-spaces: ["error", { "includeTabs": true }]*/
var a = 1 + 2;何时不使用它
如果您不想检查和禁止多个空格,则应关闭此规则。