Index

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
const o = {
    set a(value) {
        this.val = value;
    }
};


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

该规则会在定义了 setter 但没有 getter 时发出警告。使用选项 getWithoutSet,如果有 getter 而没有 setter,它也会发出警告。

🌐 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)。
  • getWithoutSet 设置为 true 将会对没有 setter 的 getter 发出警告(默认 false)。
  • enforceForClassMembers 设置为 true 会额外将此规则应用于类的 getter/setter(默认 true)。如果你希望此规则忽略类声明和类表达式,请将 enforceForClassMembers 设置为 false
  • enforceForTSTypes:设置为 true 还会将此规则应用于 TypeScript 类型定义(默认 false)。

setWithoutGet

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

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

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

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


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

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

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

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

const s = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

const t = {d: 1};
Object.defineProperty(t, '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 }]*/

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

const v = {
    get a() {
        return this.val;
    }
};

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

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

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

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

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

const z = {d: 1};
Object.defineProperty(z, '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 发出警告。
  • "setWithoutGet": true 也会对类中没有 getter 的 setter 发出警告。

{ “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;
    }
}

enforceForTSTypes

enforceForTSTypes 设置为 true 时:

🌐 When enforceForTSTypes is set to true:

  • "getWithoutSet": true 还会警告 TypeScript 类型中没有 setter 的 getter。
  • "setWithoutGet": true 还会警告 TypeScript 类型中没有 getter 的 setter。

{ “getWithoutSet”: true, “enforceForTSTypes”: true } 的错误代码示例:

🌐 Examples of incorrect code for { "getWithoutSet": true, "enforceForTSTypes": true }:

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

interface I {
    get a(): string
}

type T = {
    get a(): number
}

{ “setWithoutGet”: true, “enforceForTSTypes”: true } 的错误代码示例:

🌐 Examples of incorrect code for { "setWithoutGet": true, "enforceForTSTypes": true }:

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

interface I {
    set a(value: unknown): void
}

type T = {
    set a(value: unknown): void
}

enforceForTSTypes 设置为 false 时,此规则会忽略 TypeScript 类型。

🌐 When enforceForTSTypes is set to false, this rule ignores TypeScript types.

{ “getWithoutSet”: true, “setWithoutGet”: true, “enforceForTSTypes”: false } 的正确代码示例:

🌐 Examples of correct code for { "getWithoutSet": true, "setWithoutGet": true, "enforceForTSTypes": false }:

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

interface I {
    get a(): string
}

type T = {
    set a(value: unknown): void
}

已知限制

🌐 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"*/

const z = 1;

// no warnings
const a = {
    get [z++]() {
        return this.val;
    },
    set [z++](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
const b = {
    get a() {
        return this.val;
    },
    a: 1,
    set a(value) {
        this.val = value;
    }
};

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

🌐 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 中引入。

进阶读物

资源