no-useless-computed-key

Disallow brackets around a property key that is a plain string or number

fixable

{ ["name"]: value } is the same as { "name": value }. The brackets are for keys that are computed when the code runs. Around a string or a number they do nothing.

--fix removes the brackets.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-useless-computed-key" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-useless-computed-key" = ["error", {
  enforceForClassMembers = true,
}]

Examples#

example.js4 problems
const headers = { ["content-type"]: "text/plain", [0]: "first" };

const { ["content-type"]: type } = headers;

class Cache {
  ["clear"]() {}
}
example.jsNo problems
const headers = { "content-type": "text/plain", 0: "first", [name]: value };

const { "content-type": type } = headers;

class Cache {
  clear() {}
}

The brackets are allowed where they change the meaning. { "__proto__": base } sets the prototype and { ["__proto__"]: base } adds a normal property. In a class, ["constructor"]() {} is a normal method, and static prototype() {} without brackets is a syntax error.

example.jsNo problems
const settings = { ["__proto__"]: base };

class Cache {
  ["constructor"]() {}
  static ["prototype"]() {}
}

Options#

OptionDefaultWhat it does
enforceForClassMemberstrueAlso check class methods and fields.
example.jswith { "enforceForClassMembers": false }No problems
class Cache {
  ["clear"]() {}
}