Skip to content

useGetterReturn (JavaScript)

Language JavaScript (and super languages)
  • 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:
biome.json
{
"linter": {
"rules": {
"suspicious": {
"useGetterReturn": "error"
}
}
}
}

Enforce get methods to always return a value.

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