Skip to content

Latest commit

 

History

History
86 lines (63 loc) · 1.59 KB

getter-return.md

File metadata and controls

86 lines (63 loc) · 1.59 KB

Enforces that a return statement is present in property getters (getter-return)

The get syntax binds an object property to a function that will be called when that property is looked up. It was first introduced in ECMAScript 5:

    var p = {
        get name(){
            return "nicholas";
        }
    };

    Object.defineProperty(p, "age", {
        get: function (){
            return 17;
        }
    });

Note that every getter is expected to return a value.

Rule Details

This rule enforces that a return statement is present in property getters.

Examples of incorrect code for this rule:

/*eslint getter-return: "error"*/

p = {
    get name(){
        // no returns.
    }
};

Object.defineProperty(p, "age", {
    get: function (){
        // no returns.
    }
});

class P{
    get name(){
        // no returns.
    }
}

Examples of correct code for this rule:

/*eslint getter-return: "error"*/

p = {
    get name(){
        return "nicholas";
    }
};

Object.defineProperty(p, "age", {
    get: function (){
        return 18;
    }
});

class P{
    get name(){
        return "nicholas";
    }
}

Options

This rule has an object option:

  • "allowImplicit": false (default) disallows implicitly return a value.

When Not To Use It

If your project will not be using getter you do not need this rule.

Further Reading