useGetterReturn (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v1.0.0 - Diagnostic Category:
lint/suspicious/useGetterReturn - This rule is recommended, meaning it is enabled by default.
- This rule doesnβt have a fix.
- The default severity of this rule is error.
- Sources:
- Same as
getter-return
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "suspicious": { "useGetterReturn": "error" } } }}Description
Section titled βDescriptionβEnforce get methods to always return a value.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβclass Person { get firstName() {}}code-block.js:2:5 lint/suspicious/useGetterReturn ββββββββββββββββββββββββββββββββββββββββββββββββββ
β This getter should return a value.
1 β class Person {
> 2 β get firstName() {}
β ^^^^^^^^^^^^^^^^^^
3 β }
4 β
βΉ A getter without a return statement always evaluates to undefined, so reading the property does not give the value the code intends.
βΉ Add a return statement that returns the value of the property.
const obj = { get firstName() { return; }}code-block.js:3:9 lint/suspicious/useGetterReturn ββββββββββββββββββββββββββββββββββββββββββββββββββ
β This return should return a value because it is located in a getter.
1 β const obj = {
2 β get firstName() {
> 3 β return;
β ^^^^^^^
4 β }
5 β }
βΉ A return without a value makes the getter evaluate to undefined, so reading the property does not give the value the code intends.
βΉ Return the value of the property from this statement.
class Option { get value() { if (this.hasValue) { log(); } else { return null; } }}code-block.js:2:5 lint/suspicious/useGetterReturn ββββββββββββββββββββββββββββββββββββββββββββββββββ
β This getter should return a value.
1 β class Option {
> 2 β get value() {
β ^^^^^^^^^^^^^
> 3 β if (this.hasValue) {
> 4 β log();
> 5 β } else {
> 6 β return null;
> 7 β }
> 8 β }
β ^
9 β }
10 β
βΉ A getter without a return statement always evaluates to undefined, so reading the property does not give the value the code intends.
βΉ Add a return statement that returns the value of the property.
class Person { get firstName() { return this.fullname.split(" ")[0]; }}const obj = { get firstName() { return this.fullname.split(" ")[0]; }}Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.