Skip to content

Latest commit

 

History

History
175 lines (134 loc) · 3.59 KB

accessor-pairs.md

File metadata and controls

175 lines (134 loc) · 3.59 KB

Enforces getter/setter pairs in objects (accessor-pairs)

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

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

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

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

Options

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

setWithoutGet

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;
    }
});

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

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;
    }
});

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;
    }
});

Known Limitations

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

When Not To Use It

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

Further Reading