The --fix
option on the command line can automatically fix some of the problems reported by this rule.
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) };
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.
This rule has a string option:
"never"
(default) disallows spaces inside computed property brackets"always"
requires one or more spaces inside computed property bracketsExamples of incorrect code for this rule with the default "never"
option:
/*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:
/*eslint computed-property-spacing: ["error", "never"]*/ /*eslint-env es6*/ obj[foo] obj['foo'] var x = {[b]: a} obj[foo[bar]]
Examples of incorrect code for this rule with the "always"
option:
/*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:
/*eslint computed-property-spacing: ["error", "always"]*/ /*eslint-env es6*/ obj[ foo ] obj[ 'foo' ] var x = {[ b ]: a} obj[ foo[ bar ] ]
You can turn this rule off if you are not concerned with the consistency of computed properties.
This rule was introduced in ESLint 0.23.0.
© JS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/rules/computed-property-spacing