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"Unnecessarily computed property [content-type] found., [0]: "first"Unnecessarily computed property [0] found. };
const { ["content-type"]: typeUnnecessarily computed property [content-type] found. } = headers;
class Cache {
["clear"]() {}Unnecessarily computed property [clear] found.
}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#
| Option | Default | What it does |
|---|---|---|
enforceForClassMembers | true | Also check class methods and fields. |
example.jswith
{ "enforceForClassMembers": false }No problemsclass Cache {
["clear"]() {}
}