computed-property-spacing
在计算属性括号内强制执行一致的间距
此规则报告的一些问题可通过 --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
.
虽然格式化偏好是非常个人化的,但在以下情况下,许多风格指南要求或不允许计算属性之间存在空格:
¥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"
(默认)不允许计算属性括号内有空格¥
"never"
(default) disallows spaces inside computed property brackets -
"always"
需要计算属性括号内有一个或多个空格¥
"always"
requires one or more spaces inside computed property brackets
对象选项:
¥Object option:
-
"enforceForClassMembers": true
(默认)还将此规则应用于类成员。¥
"enforceForClassMembers": true
(default) additionally applies this rule to class members.
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 中引入。