close

getter-return

Configuration

PresetConfigured Value
✅ js.configs.recommended"error"
rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'getter-return': 'error',
    },
  },
]);

Rule Details

Enforces that property getters contain a return statement that returns a value. This applies to getter methods in object literals, class declarations, and property descriptors passed to Object.defineProperty, Object.defineProperties, Reflect.defineProperty, and Object.create.

Options

  • allowImplicit (default: false): When set to true, allows getters to implicitly return undefined by using return; without a value.

Examples of incorrect code for this rule:

var obj = {
  get name() {
    // no return
  },
};

class Foo {
  get bar() {
    // no return
  }
}

Object.defineProperty(obj, 'prop', {
  get: function () {
    // no return
  },
});

Examples of correct code for this rule:

var obj = {
  get name() {
    return 'foo';
  },
};

class Foo {
  get bar() {
    return this._bar;
  }
}

Object.defineProperty(obj, 'prop', {
  get: function () {
    return this._prop;
  },
});

// Throw statements are valid exit paths
class AbstractFoo {
  get value() {
    throw new Error('Not implemented');
  }
}

// All code paths must return or throw
class Bar {
  get value() {
    if (condition) {
      return this._value;
    } else {
      throw new Error('Invalid state');
    }
  }
}

Examples of correct code with { allowImplicit: true }:

var obj = {
  get name() {
    return;
  },
};

Original Documentation