Disallow or enforce spaces inside of computed properties (computed-property-spacing)

禁止或强制在计算属性中使用空格 (computed-property-spacing)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

While formatting preferences are very personal, a number of style guides require or disallow spaces between computed properties in the following situations:

虽然代码风格纯属个人偏好,一些代码风格规范要求或禁止在以下情况的对计算属性内使用空格:

/*eslint-env es6*/

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)
};

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 a string option:

该规则有一个字符串选项:

never

Examples of incorrect code for this rule with the default "never" option:

默认选项 "never"错误 代码示例:

/*eslint computed-property-spacing: ["error", "never"]*/
/*eslint-env es6*/

obj[foo ]
obj[ 'foo']
var x = {[ b ]: a}
obj[foo[ bar ]]

Examples of correct code for this rule with the default "never" option:

默认选项 "never"正确 代码示例:

/*eslint computed-property-spacing: ["error", "never"]*/
/*eslint-env es6*/

obj[foo]
obj['foo']
var x = {[b]: a}
obj[foo[bar]]

always

Examples of incorrect code for this rule with the "always" option:

选项 "always"错误 代码示例:

/*eslint computed-property-spacing: ["error", "always"]*/
/*eslint-env es6*/

obj[foo]
var x = {[b]: a}
obj[ foo]
obj['foo' ]
obj[foo[ bar ]]
var x = {[ b]: a}

Examples of correct code for this rule with the "always" option:

选项 "always"正确 代码示例:

/*eslint computed-property-spacing: ["error", "always"]*/
/*eslint-env es6*/

obj[ foo ]
obj[ 'foo' ]
var x = {[ b ]: a}
obj[ foo[ bar ] ]

When Not To Use It

You can turn this rule off if you are not concerned with the consistency of computed properties.

如果你并不关注计算属性的一致性的话,关闭此规则即可。

Version

This rule was introduced in ESLint 0.23.0.

该规则在 ESLint 0.23.0 被引入。

Resources


+