]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-setter-return.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-setter-return.md
1 ---
2 title: no-setter-return
3 layout: doc
4 rule_type: problem
5 related_rules:
6 - getter-return
7 further_reading:
8 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
9 ---
10
11
12
13 Setters cannot return values.
14
15 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, since the returned value cannot be used.
16
17 ## Rule Details
18
19 This rule disallows returning values from setters and reports `return` statements in setter functions.
20
21 Only `return` without a value is allowed, as it's a control flow statement.
22
23 This rule checks setters in:
24
25 * Object literals.
26 * Class declarations and class expressions.
27 * Property descriptors in `Object.create`, `Object.defineProperty`, `Object.defineProperties`, and `Reflect.defineProperty` methods of the global objects.
28
29 Examples of **incorrect** code for this rule:
30
31 ::: incorrect
32
33 ```js
34 /*eslint no-setter-return: "error"*/
35
36 var foo = {
37 set a(value) {
38 this.val = value;
39 return value;
40 }
41 };
42
43 class Foo {
44 set a(value) {
45 this.val = value * 2;
46 return this.val;
47 }
48 }
49
50 const Bar = class {
51 static set a(value) {
52 if (value < 0) {
53 this.val = 0;
54 return 0;
55 }
56 this.val = value;
57 }
58 };
59
60 Object.defineProperty(foo, "bar", {
61 set(value) {
62 if (value < 0) {
63 return false;
64 }
65 this.val = value;
66 }
67 });
68 ```
69
70 :::
71
72 Examples of **correct** code for this rule:
73
74 ::: correct
75
76 ```js
77 /*eslint no-setter-return: "error"*/
78
79 var foo = {
80 set a(value) {
81 this.val = value;
82 }
83 };
84
85 class Foo {
86 set a(value) {
87 this.val = value * 2;
88 }
89 }
90
91 const Bar = class {
92 static set a(value) {
93 if (value < 0) {
94 this.val = 0;
95 return;
96 }
97 this.val = value;
98 }
99 };
100
101 Object.defineProperty(foo, "bar", {
102 set(value) {
103 if (value < 0) {
104 throw new Error("Negative value.");
105 }
106 this.val = value;
107 }
108 });
109 ```
110
111 :::