computed-property-spacing
在计算属性括号内强制执行一致的间距
此规则报告的一些问题可通过 --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.
虽然格式化偏好非常个人化,但在以下情况下,许多风格指南要求或禁止在计算属性之间使用空格:
🌐 While formatting preferences are very personal, a number of style guides require or disallow spaces between computed properties in the following situations:
var obj = { prop: "value" };
var a = "prop";
var x = obj[a]; // computed property in object member expression
var a = "prop";
var obj = {
[a]: "value" // computed property key in object literal (ECMAScript 6)
};
var obj = { prop: "value" };
var a = "prop";
var { [a]: x } = obj; // computed property key in object destructuring pattern (ECMAScript 6)
规则详情
🌐 Rule Details
此规则强制计算属性括号内的间距一致。
🌐 This rule enforces consistent spacing inside computed property brackets.
它要么要求,要么不允许括号与其中的值之间有空格。
此规则不适用于与相邻值之间由换行符分隔的括号。
🌐 It either requires or disallows spaces between the brackets and the values inside of them. This rule does not apply to brackets that are separated from the adjacent value by a newline.
选项
🌐 Options
该规则有两个选项,一个字符串选项和一个对象选项。
🌐 This rule has two options, a string option and an object option.
字符串选项:
🌐 String option:
"never"(默认)不允许在计算属性括号内使用空格"always"在计算属性的括号内需要一个或多个空格
对象选项:
🌐 Object option:
"enforceForClassMembers": true(默认)还将此规则应用于类成员。
never
使用默认 "never" 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default "never" option:
/*eslint computed-property-spacing: ["error", "never"]*/
obj[foo ]
obj[ 'foo']
var x = {[ b ]: a}
obj[foo[ bar ]]
const { [ a ]: someProp } = obj;
({ [ b ]: anotherProp } = anotherObj);
使用默认 "never" 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default "never" option:
/*eslint computed-property-spacing: ["error", "never"]*/
obj[foo]
obj['foo']
var x = {[b]: a}
obj[foo[bar]]
const { [a]: someProp } = obj;
({ [b]: anotherProp } = anotherObj);
always
使用 "always" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always" option:
/*eslint computed-property-spacing: ["error", "always"]*/
obj[foo]
var x = {[b]: a}
obj[ foo]
obj['foo' ]
obj[foo[ bar ]]
var x = {[ b]: a}
const { [a]: someProp } = obj;
({ [b ]: anotherProp } = anotherObj);
使用 "always" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always" option:
/*eslint computed-property-spacing: ["error", "always"]*/
obj[ foo ]
obj[ 'foo' ]
var x = {[ b ]: a}
obj[ foo[ bar ] ]
const { [ a ]: someProp } = obj;
({ [ b ]: anotherProp } = anotherObj);
enforceForClassMembers
当 enforceForClassMembers 设置为 true(默认值)时,该规则还禁止/强制在类方法、getter 和 setter 的计算属性键内部使用空格。
🌐 With enforceForClassMembers set to true (default), the rule also disallows/enforces spaces inside of computed keys of class methods, getters and setters.
这个规则针对 "never" 和 { "enforceForClassMembers": true }(默认)的错误代码示例:
🌐 Examples of incorrect code for this rule with "never" and { "enforceForClassMembers": true } (default):
/*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
class Foo {
[a ]() {}
get [b ]() {}
set [b ](value) {}
}
const Bar = class {
[ a](){}
static [ b]() {}
static get [ c ]() {}
static set [ c ](value) {}
}
这个规则使用 "never" 和 { "enforceForClassMembers": true }(默认) 的正确代码示例:
🌐 Examples of correct code for this rule with "never" and { "enforceForClassMembers": true } (default):
/*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
class Foo {
[a]() {}
get [b]() {}
set [b](value) {}
}
const Bar = class {
[a](){}
static [b]() {}
static get [c]() {}
static set [c](value) {}
}
此规则中使用 "never" 和 { "enforceForClassMembers": false } 的正确代码示例:
🌐 Examples of correct code for this rule with "never" and { "enforceForClassMembers": false }:
/*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": false }]*/
class Foo {
[a ]() {}
get [b ]() {}
set [b ](value) {}
}
const Bar = class {
[ a](){}
static [ b]() {}
static get [ c ]() {}
static set [ c ](value) {}
}
何时不使用
🌐 When Not To Use It
如果你不关心计算属性的一致性,你可以关闭此规则。
🌐 You can turn this rule off if you are not concerned with the consistency of computed properties.
相关规则
版本
此规则是在 ESLint v0.23.0 中引入。