no-useless-computed-key

禁止在对象和类中使用不必要的计算属性键

🔧 Fixable

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

没有必要将计算属性与字面一起使用,例如:

¥It’s unnecessary to use computed properties with literals such as:

var foo = {["a"]: "b"};

代码可以重写为:

¥The code can be rewritten as:

var foo = {"a": "b"};

规则详情

¥Rule Details

此规则不允许不必要地使用计算的属性键。

¥This rule disallows unnecessary usage of computed property keys.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-useless-computed-key: "error"*/

var a = { ['0']: 0 };
var a = { ['0+1,234']: 0 };
var a = { [0]: 0 };
var a = { ['x']: 0 };
var a = { ['x']() {} };

class Foo {
    ["foo"] = "bar";

    [0]() {}
    ['a']() {}
    get ['b']() {}
    set ['c'](value) {}

    static ["foo"] = "bar";

    static ['a']() {}
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-useless-computed-key: "error"*/

var c = { 'a': 0 };
var c = { 0: 0 };
var a = { x() {} };
var c = { a: 0 };
var c = { '0+1,234': 0 };

class Foo {
    "foo" = "bar";

    0() {}
    'a'() {}
    get 'b'() {}
    set 'c'(value) {}

    static "foo" = "bar";

    static 'a'() {}
}

此规则的附加正确代码示例:

¥Examples of additional correct code for this rule:

在线运行
/*eslint no-useless-computed-key: "error"*/

var 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 `[]`
}

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • enforceForClassMembers 设置为 false 会对类成员禁用此规则(默认 true)。

    ¥enforceForClassMembers set to false disables this rule for class members (Default true).

enforceForClassMembers

默认情况下,此规则还检查类声明和类表达式,因为 enforceForClassMembers 的默认值为 true

¥By default, this rule also checks class declarations and class expressions, as the default value for enforceForClassMembers is true.

enforceForClassMembers 设置为 false 时,该规则将允许在类字段、类方法、类 getter 和类 setter 中使用不必要的计算键。

¥When enforceForClassMembers is set to false, the rule will allow unnecessary computed keys inside of class fields, class methods, class getters, and class setters.

使用 { "enforceForClassMembers": false } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "enforceForClassMembers": false } option:

在线运行
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": false }]*/

const obj = {
    ["foo"]: "bar",
    [42]: "baz",

    ['a']() {},
    get ['b']() {},
    set ['c'](value) {}
};

使用 { "enforceForClassMembers": false } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "enforceForClassMembers": false } option:

在线运行
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": false }]*/

class SomeClass {
    ["foo"] = "bar";
    [42] = "baz";

    ['a']() {}
    get ['b']() {}
    set ['c'](value) {}

    static ["foo"] = "bar";
    static ['baz']() {}
}

何时不使用

¥When Not To Use It

如果你不想收到有关不必要的计算属性键的通知,你可以安全地禁用此规则。

¥If you don’t want to be notified about unnecessary computed property keys, you can safely disable this rule.

版本

此规则是在 ESLint v2.9.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号