# getter-return

> Require getters to return a value

A getter that doesn't return gives `undefined` to any code that reads the property. Usually someone forgot the `return`, or one branch of the getter doesn't have one.

The rule checks `get` in classes and object literals, and the `get` function of a property descriptor passed to `Object.defineProperty`, `Reflect.defineProperty`, `Object.defineProperties` or `Object.create`.

This rule is on by default in JavaScript files. It's off in TypeScript files, where TypeScript reports the same mistake.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as an error. To turn it off:

```toml bunfig.toml
[lint.rules]
"getter-return" = "off" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"getter-return" = ["error", {
  allowImplicit = false,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
const user = {
  get fullName() {
    `${this.first} ${this.last}`;
  },
};
```

{/* bad */}
```js
class Cart {
  get total() {
    if (this.items.length > 0) {
      return sum(this.items);
    }
  }
}
```

{/* bad */}
```js
Object.defineProperty(user, "age", {
  get: function () {
    now() - this.born;
  },
});
```

Not reported:

{/* good */}
```js
const user = {
  get fullName() {
    return `${this.first} ${this.last}`;
  },
};

class Cart {
  get total() {
    if (this.items.length > 0) {
      return sum(this.items);
    }
    return 0;
  }
}
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allowImplicit` | `false` | Allow `return;` without a value. The getter still needs to end with a `return` on every path. |

{/* bad */}
```js
class Session {
  get token() {
    return;
  }
}
```

{/* good {"options": [{"allowImplicit": true}]} */}
```js
class Session {
  get token() {
    return;
  }
}
```
