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