# no-setter-return
Disallow setters to return values.
Setters cannot return values. To be more precise, a setter that returns a value is not treated as an error, but we cannot use the returned value at all. Thus, if you write a setter that will return something, it is most likely either completely unnecessary or a possible error.
Note that return
without a value is allowed because it is considered a control flow statement.
This rule checks setters in:
- Object literals
- Class declarations and class expressions
- Property descriptors in
Object.create
,Object.defineProperty
,Object.defineProperties
, andReflect.defineProperty
# Incorrect code examples
let foo = {
set a(value) {
this.val = value;
// The setter always returns a value
return value;
}
};
class Foo {
set a(value) {
this.val = value;
// The setter always returns a value
return this.val;
}
}
const Bar = class {
static set a(value) {
if (value < 0) {
this.val = 0;
// The setter returns `0` if the value is negative
return 0;
}
this.val = value;
}
};
Object.defineProperty(foo, "bar", {
set(value) {
if (value < 0) {
// The setter returns `false` if the value is negative
return false;
}
this.val = value;
}
});
# Correct code examples
let foo = {
set a(value) {
this.val = value;
}
};
class Foo {
set a(value) {
this.val = value;
}
}
const Bar = class {
static set a(value) {
if (value < 0) {
this.val = 0;
// Returning without a value is allowed
return;
}
this.val = value;
}
};
Object.defineProperty(foo, "bar", {
set(value) {
if (value < 0) {
// Throwing an error is also allowed
throw new Error("Negative value is not allowed.");
}
this.val = value;
}
});
More incorrect examples
let foo = {
set bar(val) {
return 42;
}
};
let bar = {
set foo(val) {
if (bar) {
return 42;
}
}
};
let bar = {
set foo(val) {
switch (bar) {
case 5:
case 6:
if (bar) {
return 42;
}
}
}
};
let bar = {
set foo(val) {
if (bar) {
} else {
return 42;
}
}
};
class Foo {
set bar(val) {
return 42;
}
}
let Foo = class {
set bar(val) {
return 42;
}
};
Object.create(null, {
foo: {
set(val) {
return 42;
}
}
});
Object.defineProperty(foo, 'bar', {
set(val) {
return 42;
}
});
Object.defineProperties(foo, 'bar', {
set(val) {
return 42;
}
});
Reflect.defineProperties(foo, 'bar', {
set(val) {
return 42;
}
});
More correct examples
({ set foo(val) { return; } })
({ set foo(val) { if (val) { return; } } })
class A { set foo(val) { return; } }
(class { set foo(val) { if (val) { return; } else { return; } return; } })
class A { set foo(val) { try {} catch(e) { return; } } }
Object.defineProperty(foo, 'bar', { set(val) { return; } })