accessor-pairs

在对象和类中强制执行 getter 和 setter 对

在 JavaScript 中,一个常见的错误是只使用属性的 setter 来创建对象,但从来没有为它定义相应的 getter。如果没有 getter,你将无法读取该属性,因此它最终不会被使用。

¥It’s a common mistake in JavaScript to create an object with just a setter for a property but never have a corresponding getter defined for it. Without a getter, you cannot read the property, so it ends up not being used.

这里有些例子:

¥Here are some examples:

// Bad
var o = {
    set a(value) {
        this.val = value;
    }
};


// Good
var o = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

如果在没有 getter 的情况下定义了 setter,则此规则会触发警告。使用选项 getWithoutSet,如果你有一个没有 setter 的 getter,它会触发警告。

¥This rule warns if setters are defined without getters. Using an option getWithoutSet, it will warn if you have a getter without a setter also.

规则详情

¥Rule Details

此规则强制执行一种样式,它要求每个定义了 setter 的属性都有一个 getter。

¥This rule enforces a style where it requires to have a getter for every property which has a setter defined.

通过激活选项 getWithoutSet,它强制每个定义了 getter 的属性都存在一个 setter。

¥By activating the option getWithoutSet it enforces the presence of a setter for every property which has a getter defined.

此规则始终检查对象字面和属性描述符。默认情况下,它还检查类声明和类表达式。

¥This rule always checks object literals and property descriptors. By default, it also checks class declarations and class expressions.

选项

¥Options

  • setWithoutGet 设置为 true 将对没有 getter 的 setter 触发警告(默认 true)。

    ¥setWithoutGet set to true will warn for setters without getters (Default true).

  • getWithoutSet 设置为 true 将警告没有设置器的获取器(默认 false)。

    ¥getWithoutSet set to true will warn for getters without setters (Default false).

  • enforceForClassMembers 设置为 true 还会将此规则应用于类 getter/setter(默认 true)。如果你希望此规则忽略类声明和类表达式,请将 enforceForClassMembers 设置为 false

    ¥enforceForClassMembers set to true additionally applies this rule to class getters/setters (Default true). Set enforceForClassMembers to false if you want this rule to ignore class declarations and class expressions.

setWithoutGet

默认 { "setWithoutGet": true } 选项的错误代码示例:

¥Examples of incorrect code for the default { "setWithoutGet": true } option:

在线运行
/*eslint accessor-pairs: "error"*/

var o = {
    set a(value) {
        this.val = value;
    }
};


var o = {d: 1};
Object.defineProperty(o, 'c', {
    set: function(value) {
        this.val = value;
    }
});

默认 { "setWithoutGet": true } 选项的正确代码示例:

¥Examples of correct code for the default { "setWithoutGet": true } option:

在线运行
/*eslint accessor-pairs: "error"*/

var o = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

var o = {d: 1};
Object.defineProperty(o, 'c', {
    set: function(value) {
        this.val = value;
    },
    get: function() {
        return this.val;
    }
});

getWithoutSet

{ "getWithoutSet": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "getWithoutSet": true } option:

在线运行
/*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/

var o = {
    set a(value) {
        this.val = value;
    }
};

var o = {
    get a() {
        return this.val;
    }
};

var o = {d: 1};
Object.defineProperty(o, 'c', {
    set: function(value) {
        this.val = value;
    }
});

var o = {d: 1};
Object.defineProperty(o, 'c', {
    get: function() {
        return this.val;
    }
});

{ "getWithoutSet": true } 选项的正确代码示例:

¥Examples of correct code for the { "getWithoutSet": true } option:

在线运行
/*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/
var o = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

var o = {d: 1};
Object.defineProperty(o, 'c', {
    set: function(value) {
        this.val = value;
    },
    get: function() {
        return this.val;
    }
});

enforceForClassMembers

enforceForClassMembers 设置为 true(默认)时:

¥When enforceForClassMembers is set to true (default):

  • "getWithoutSet": true 还会对类中没有 setter 的 getter 触发警告。

    ¥"getWithoutSet": true will also warn for getters without setters in classes.

  • "setWithoutGet": true 还会对类中没有 getter 的 setter 触发警告。

    ¥"setWithoutGet": true will also warn for setters without getters in classes.

{ "getWithoutSet": true, "enforceForClassMembers": true } 的错误代码示例:

¥Examples of incorrect code for { "getWithoutSet": true, "enforceForClassMembers": true }:

在线运行
/*eslint accessor-pairs: ["error", { "getWithoutSet": true, "enforceForClassMembers": true }]*/

class Foo {
    get a() {
        return this.val;
    }
}

class Bar {
    static get a() {
        return this.val;
    }
}

const Baz = class {
    get a() {
        return this.val;
    }
    static set a(value) {
        this.val = value;
    }
}

{ "setWithoutGet": true, "enforceForClassMembers": true } 的错误代码示例:

¥Examples of incorrect code for { "setWithoutGet": true, "enforceForClassMembers": true }:

在线运行
/*eslint accessor-pairs: ["error", { "setWithoutGet": true, "enforceForClassMembers": true }]*/

class Foo {
    set a(value) {
        this.val = value;
    }
}

const Bar = class {
    static set a(value) {
        this.val = value;
    }
}

enforceForClassMembers 设置为 false 时,此规则将忽略类。

¥When enforceForClassMembers is set to false, this rule ignores classes.

{ "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false } 的正确代码示例:

¥Examples of correct code for { "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false }:

在线运行
/*eslint accessor-pairs: ["error", {
    "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false
}]*/

class Foo {
    get a() {
        return this.val;
    }
}

class Bar {
    static set a(value) {
        this.val = value;
    }
}

const Baz = class {
    static get a() {
        return this.val;
    }
}

const Quux = class {
    set a(value) {
        this.val = value;
    }
}

已知限制

¥Known Limitations

由于静态分析的限制,此规则不考虑可能的副作用,并且在某些情况下可能不会报告具有计算键的 getter/setter 的缺失对,如下例所示:

¥Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases might not report a missing pair for a getter/setter that has a computed key, like in the following example:

/*eslint accessor-pairs: "error"*/

var a = 1;

// no warnings
var o = {
    get [a++]() {
        return this.val;
    },
    set [a++](value) {
        this.val = value;
    }
};

此外,此规则不允许对象字面量和类定义中的重复键,并且在某些情况下,重复键可能不会报告 getter/setter 的缺失对,如下例所示:

¥Also, this rule does not disallow duplicate keys in object literals and class definitions, and in certain cases with duplicate keys might not report a missing pair for a getter/setter, like in the following example:

/*eslint accessor-pairs: "error"*/

// no warnings
var o = {
    get a() {
        return this.val;
    },
    a: 1,
    set a(value) {
        this.val = value;
    }
};

上面的代码创建了一个只有属性 "a" 的 setter 的对象。

¥The code above creates an object with just a setter for the property "a".

如果你还想禁止对象字面量中的重复键,请参阅 no-dupe-keys

¥See no-dupe-keys if you also want to disallow duplicate keys in object literals.

如果你还想在类定义中禁止重复名称,请参阅 no-dupe-class-members

¥See no-dupe-class-members if you also want to disallow duplicate names in class definitions.

何时不使用

¥When Not To Use It

如果你不关心对象上同时存在 setter 和 getter,则可以关闭此规则。

¥You can turn this rule off if you are not concerned with the simultaneous presence of setters and getters on objects.

版本

此规则是在 ESLint v0.22.0 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号