版本

accessor-pairs

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

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

这里有一些例子

// 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,它也会发出警告。

规则详情

此规则强制执行一种风格,要求每个定义了 setter 的属性都必须有一个 getter。

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

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

选项

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

setWithoutGet

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

在 Playground 中打开
/*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 } 选项的正确代码示例

在 Playground 中打开
/*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 } 选项的错误代码示例

在 Playground 中打开
/*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 } 选项的正确代码示例

在 Playground 中打开
/*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(默认)时

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

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

在 Playground 中打开
/*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 }错误代码示例

在 Playground 中打开
/*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 时,此规则会忽略类。

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

在 Playground 中打开
/*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;
    }
}

已知限制

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

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

const z = 1;

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

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

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

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

上面的代码创建了一个对象,其中仅为属性 "a" 设置了 setter。

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

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

何时不使用

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

版本

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

延伸阅读

资源

更改语言