Lint Rules

noSetterReturn (since v11.0.0)

Disallow returning a value from a setter

While returning a value from a setter does not produce an error, the returned value is being ignored. Therefore, returning a value from a setter is either unnecessary or a possible error.

Only returning without a value is allowed, as it’s a control flow statement.

Examples

Invalid

class A {
    set foo(x) {
        return x;
    }
}
nursery/noSetterReturn.js:3:9 lint/nursery/noSetterReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The setter should not return a value.
  
    1 │ class A {
    2 │     set foo(x) {
  > 3 │         return x;
           ^^^^^^^^^
    4 │     }
    5 │ }
  
   The setter is here:
  
    1 │ class A {
  > 2 │     set foo(x) {
       ^^^^^^^^^^^^
  > 3 │         return x;
  > 4 │     }
       ^
    5 │ }
    6 │ 
  
   Returning a value from a setter is ignored.
  
const b = {
    set foo(x) {
        return x;
    },
};
nursery/noSetterReturn.js:3:9 lint/nursery/noSetterReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The setter should not return a value.
  
    1 │ const b = {
    2 │     set foo(x) {
  > 3 │         return x;
           ^^^^^^^^^
    4 │     },
    5 │ };
  
   The setter is here:
  
    1 │ const b = {
  > 2 │     set foo(x) {
       ^^^^^^^^^^^^
  > 3 │         return x;
  > 4 │     },
       ^
    5 │ };
    6 │ 
  
   Returning a value from a setter is ignored.
  
const c = {
    set foo(x) {
        if (x) {
            return x;
        }
    },
};
nursery/noSetterReturn.js:4:13 lint/nursery/noSetterReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The setter should not return a value.
  
    2 │     set foo(x) {
    3 │         if (x) {
  > 4 │             return x;
               ^^^^^^^^^
    5 │         }
    6 │     },
  
   The setter is here:
  
    1 │ const c = {
  > 2 │     set foo(x) {
       ^^^^^^^^^^^^
  > 3 │         if (x) {
  > 4 │             return x;
  > 5 │         }
  > 6 │     },
       ^
    7 │ };
    8 │ 
  
   Returning a value from a setter is ignored.
  

Valid

// early-return
class A {
    set foo(x) {
        if (x) {
            return;
        }
    }
}
// not a setter
class B {
  set(x) {
    return x;
  }
}