no-useless-computed-key
eslint
Safe autofix
eslint.config.js
export default [
{
rules: {
"no-useless-computed-key": "error",
},
},
];Disallow unnecessary computed property keys in objects and classes
const a = { ['0']: 0 };
const b = { ['0+1,234']: 0 };
const c = { [0]: 0 };
const d = { ['x']: 0 };
const e = { ['x']() {} };
const { [0]: foo } = obj;
const { ['x']: bar } = obj;
class Foo {
["foo"] = "bar";
[0]() {}
['a']() {}
get ['b']() {}
set ['c'](value) {}
static ["foo"] = "bar";
static ['a']() {}
}const a = { 'a': 0 };
const b = { 0: 0 };
const c = { x() {} };
const d = { a: 0 };
const e = { '0+1,234': 0 };
const { 0: foo } = obj;
const { 'x': bar } = obj;
class Foo {
"foo" = "bar";
0() {}
'a'() {}
get 'b'() {}
set 'c'(value) {}
static "foo" = "bar";
static 'a'() {}
}const c = {
"__proto__": foo, // defines object's prototype
["__proto__"]: bar // defines a property named "__proto__"
};
class Foo {
["constructor"]; // instance field named "constructor"
"constructor"() {} // the constructor of this class
["constructor"]() {} // method named "constructor"
static ["constructor"]; // static field named "constructor"
static ["prototype"]; // runtime error, it would be a parsing error without `[]`
}Examples from the ESLint documentation.
no-useless-computed-key
eslint
export default [
{
rules: {
"no-useless-computed-key": "error",
},
},
];no-useless-computed-key
eslint plugin · style
{
"rules": {
"no-useless-computed-key": "error"
}
}complexity/useLiteralKeys
complexity · since 1.0.0
{
"linter": {
"rules": {
"complexity": {
"useLiteralKeys": "error"
}
}
}
}Disallow throwing literals as exceptions
Require or disallow Unicode byte order mark (BOM)
Disallow new operators with the Function object
Disallow new operators outside of assignments or comparisons
Disallow unnecessary calls to .bind()