computed-property-spacing

在计算的属性括号内强制执行一致的间距

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

虽然格式化偏好是非常个人化的,但在以下情况下,许多样式指南要求或不允许计算属性之间存在空格:

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

var obj = { prop: "value" };
var a = "prop";
var { [a]: x } = obj; // computed property key in object destructuring pattern (ECMAScript 6)

规则详情

此规则强制计算属性括号内的间距一致。

它要求或不允许括号和其中的值之间有空格。此规则不适用于通过换行符与相邻值分隔的括号。

选项

EhErn4ugUQ85sgZAv6FNf/q7lO6mF01zp+iJLYKPz5Y7tgihppjqTfbe5p7vd5XbdFpsSkontvr3QOW7BQW3u3KPdQtJiqHKQZCv/2HCQh8=

KUeUdYgJ23xXvLdmzR+dd2x+6oaBmrm4b4HxP5rSCUQ=

    vTzUVDuHbAIcGJ9LpWmqox/Sc0VWX0D0yuHbJl29ASETyY2cTkwyjEvIJfZdXlPEDencCsgOJSP1cv+DBa8Iq/J6ZyzBQrrZ01WARiy006ztCeS39xx9QG2NpET2RnbRIJaxDFEB7X5QbDebhu2FKJ86Z01AomEg5eVQQTUmV6cVfq8Iy0QS85ZNS/KrMYU4y1xEXSwjixMT1GBcc5KXiRrDPAJGldIzZyQzd7a8eE4=

aun0wInWbMVczwU/w0Y7dg==

    zV+gMNxpVeSmhLroYc/+YY2aYpeYb07BqcFzcSCU72RIRcea8pzcfO/swZ7h2EnNATRG/ug6f/s6IW0iiJyrx5pvNLKdMBw5FoyeruS16B/MqqBWeuaZ14El2v8er3OGjtcwx7+v9iytnHZvwENNMw==

never

bm6gilSW2VXZz6rf3tI2AQUFpbv6ZKDqQB1kXR4nGSCh0w+bWTNszdSlqQL1eI4bnjcC6rHMKmd+FVDhBeAuDVUC10CSisYpoRwpMa8vtBI=

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

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

const { [ a ]: someProp } = obj;
({ [ b ]: anotherProp } = anotherObj);

bm6gilSW2VXZz6rf3tI2AQUFpbv6ZKDqQB1kXR4nGSCh0w+bWTNszdSlqQL1eI4bUycTVzRlfadpKTILZr1M72bPbf65DVW3SQ04VlAT4MY=

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

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

const { [a]: someProp } = obj;
({ [b]: anotherProp } = anotherObj);

always

0CQI/n5xTgnggRkjNbRwe3aJ0iPBiw+WGzycevxIrVZ8rukyzrIgwffELLoEXB7ufRi/jijyEt6YHqEZEH2sVP7q6cpeLmYMBPHoX5d9sUM=

/*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}
const { [a]: someProp } = obj;
({ [b ]: anotherProp } = anotherObj);

0CQI/n5xTgnggRkjNbRwe3aJ0iPBiw+WGzycevxIrVZJsWjwAZSA2HIWm4IX+ClLqrJ+0ULZnL6Bq44SYlKQwZvHYScU22meJ53mh07Q1eE=

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

obj[ foo ]
obj[ 'foo' ]
var x = {[ b ]: a}
obj[ foo[ bar ] ]
const { [ a ]: someProp } = obj;
({ [ b ]: anotherProp } = anotherObj);

enforceForClassMembers

mNQL3QmYlBS2tJor3RCTjuTbXYwW5/S7hBaOQWBFWzZaUlYRG4bTT+nwzbmRxk2cWjii275DR5ku9vaRviylLElY3QZ/qx7T8HxFe0x/+jZL/VC2PSePPtBYXpjQFmXOvbcl3KMJtMbuQXrYWR8qjMafqtdPj5GI8y6OAZcbsi2Ryz4CTz3K1ya6sJtNoeqEacpvDfHomLcw0AWYNlvs+xA0DqCxiHK92UC71NS04iI=

gNC8k1GhM1l+ifT0oB5hn57FAGInLAB6vu2OtNRPUJmghwDncoSPsAhxtCmJVW7AkGVFnxJ81cQaOqftTHYQXPhLwkBaBKwhY+xHnHg+Vhon0+ojQ6EBgUmUYjckLt1PPS55gIahkpVQbxoOm5VtbX38KbbU/V+vQTpDMCE8OV0=

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

class Foo {
  [a ]() {}
  get [b ]() {}
  set [b ](value) {}
}

const Bar = class {
  [ a](){}
  static [ b]() {}
  static get [ c ]() {}
  static set [ c ](value) {}
}

i8fBrbWlOixNpv1HSm4U+DE+L0a6Av/Z9r+46Pow6L1Gi7wqGx3tLB53D/k0vK2Oa6tSBKVEffFn/CvWiTwDzE1zBAXioFyN2s0Ba8eYybiJaf2T/Bu7RTiFNaupHUTC9j0boiZGPILAZu3PBvTMiTf1xOosev6gwL/Az6bOfmU=

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

class Foo {
  [a]() {}
  get [b]() {}
  set [b](value) {}
}

const Bar = class {
  [a](){}
  static [b]() {}
  static get [c]() {}
  static set [c](value) {}
}

i8fBrbWlOixNpv1HSm4U+JHkcyHZXv/5F9Nee4yHLmcpdyqqRsEZpZA+U2Alahkl3IW30IhKNx7NFzeahmD5QKW/u5aFC+y60bWqQ6IKQWzvdNspptWmkpW6ysFl2sFCRFNG3AfQ5rUYS49F8zNvQxW13NVygKPLI3Wx+hdGBuc=

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

class Foo {
  [a ]() {}
  get [b ]() {}
  set [b ](value) {}
}

const Bar = class {
  [ a](){}
  static [ b]() {}
  static get [ c ]() {}
  static set [ c ](value) {}
}

何时不使用

pIZyFV8d5bDsMgFuUuQLwd9DWWzRpBfRHVftgPF0h6rip2UdfsSURIkkvnvxGvdixpVTCsvlh84+JJYc3IGG9neQoxpm8f2ExtCofLVZA+4=