Enforce return statements in getters
Using the recommended config from @eslint/js in a configuration file enables this rule
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:
const p = {
get name(){
return "nicholas";
}
};
Object.defineProperty(p, "age", {
get: function (){
return 17;
}
});
Note that every getter is expected to return a value.
This rule enforces that a return statement is present in property getters.
Examples of incorrect code for this rule:
/*eslint getter-return: "error"*/
const p = {
(){
// no returns.
}
};
Object.defineProperty(p, "age", {
(){
// no returns.
}
});
class P{
(){
// no returns.
}
}
Examples of correct code for this rule:
/*eslint getter-return: "error"*/
const p = {
get name(){
return "nicholas";
}
};
Object.defineProperty(p, "age", {
get: function (){
return 18;
}
});
class P{
get name(){
return "nicholas";
}
}
This rule has an object option:
"allowImplicit": false (default) disallows implicitly returning undefined with a return statement.Examples of correct code for the { "allowImplicit": true } option:
/*eslint getter-return: ["error", { allowImplicit: true }]*/
const p = {
get name(){
return; // return undefined implicitly.
}
};
If your project will not be using ES5 property getters you do not need this rule.
It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.
This rule was introduced in ESLint v4.2.0.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/getter-return