]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/accessor-pairs.md
25028d9ef73121718c7a0eb89faf55e6c752f469
[pve-eslint.git] / eslint / docs / rules / accessor-pairs.md
1 # Enforces getter/setter pairs in objects and classes (accessor-pairs)
2
3 It's a common mistake in JavaScript to create an object with just a setter for a property but never have a corresponding getter defined for it. Without a getter, you cannot read the property, so it ends up not being used.
4
5 Here are some examples:
6
7 ```js
8 // Bad
9 var o = {
10 set a(value) {
11 this.val = value;
12 }
13 };
14
15 // Good
16 var o = {
17 set a(value) {
18 this.val = value;
19 },
20 get a() {
21 return this.val;
22 }
23 };
24
25 ```
26
27 This rule warns if setters are defined without getters. Using an option `getWithoutSet`, it will warn if you have a getter without a setter also.
28
29 ## Rule Details
30
31 This rule enforces a style where it requires to have a getter for every property which has a setter defined.
32
33 By activating the option `getWithoutSet` it enforces the presence of a setter for every property which has a getter defined.
34
35 This rule always checks object literals and property descriptors. By default, it also checks class declarations and class expressions.
36
37 ## Options
38
39 * `setWithoutGet` set to `true` will warn for setters without getters (Default `true`).
40 * `getWithoutSet` set to `true` will warn for getters without setters (Default `false`).
41 * `enforceForClassMembers` set to `true` additionally applies this rule to class getters/setters (Default `true`). Set `enforceForClassMembers` to `false` if you want this rule to ignore class declarations and class expressions.
42
43 ### setWithoutGet
44
45 Examples of **incorrect** code for the default `{ "setWithoutGet": true }` option:
46
47 ```js
48 /*eslint accessor-pairs: "error"*/
49
50 var o = {
51 set a(value) {
52 this.val = value;
53 }
54 };
55
56 var o = {d: 1};
57 Object.defineProperty(o, 'c', {
58 set: function(value) {
59 this.val = value;
60 }
61 });
62 ```
63
64 Examples of **correct** code for the default `{ "setWithoutGet": true }` option:
65
66 ```js
67 /*eslint accessor-pairs: "error"*/
68
69 var o = {
70 set a(value) {
71 this.val = value;
72 },
73 get a() {
74 return this.val;
75 }
76 };
77
78 var o = {d: 1};
79 Object.defineProperty(o, 'c', {
80 set: function(value) {
81 this.val = value;
82 },
83 get: function() {
84 return this.val;
85 }
86 });
87
88 ```
89
90 ### getWithoutSet
91
92 Examples of **incorrect** code for the `{ "getWithoutSet": true }` option:
93
94 ```js
95 /*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/
96
97 var o = {
98 set a(value) {
99 this.val = value;
100 }
101 };
102
103 var o = {
104 get a() {
105 return this.val;
106 }
107 };
108
109 var o = {d: 1};
110 Object.defineProperty(o, 'c', {
111 set: function(value) {
112 this.val = value;
113 }
114 });
115
116 var o = {d: 1};
117 Object.defineProperty(o, 'c', {
118 get: function() {
119 return this.val;
120 }
121 });
122 ```
123
124 Examples of **correct** code for the `{ "getWithoutSet": true }` option:
125
126 ```js
127 /*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/
128 var o = {
129 set a(value) {
130 this.val = value;
131 },
132 get a() {
133 return this.val;
134 }
135 };
136
137 var o = {d: 1};
138 Object.defineProperty(o, 'c', {
139 set: function(value) {
140 this.val = value;
141 },
142 get: function() {
143 return this.val;
144 }
145 });
146
147 ```
148
149 ### enforceForClassMembers
150
151 When `enforceForClassMembers` is set to `true` (default):
152
153 * `"getWithoutSet": true` will also warn for getters without setters in classes.
154 * `"setWithoutGet": true` will also warn for setters without getters in classes.
155
156 Examples of **incorrect** code for `{ "getWithoutSet": true, "enforceForClassMembers": true }`:
157
158 ```js
159 /*eslint accessor-pairs: ["error", { "getWithoutSet": true, "enforceForClassMembers": true }]*/
160
161 class Foo {
162 get a() {
163 return this.val;
164 }
165 }
166
167 class Bar {
168 static get a() {
169 return this.val;
170 }
171 }
172
173 const Baz = class {
174 get a() {
175 return this.val;
176 }
177 static set a(value) {
178 this.val = value;
179 }
180 }
181 ```
182
183 Examples of **incorrect** code for `{ "setWithoutGet": true, "enforceForClassMembers": true }`:
184
185 ```js
186 /*eslint accessor-pairs: ["error", { "setWithoutGet": true, "enforceForClassMembers": true }]*/
187
188 class Foo {
189 set a(value) {
190 this.val = value;
191 }
192 }
193
194 const Bar = class {
195 static set a(value) {
196 this.val = value;
197 }
198 }
199 ```
200
201 When `enforceForClassMembers` is set to `false`, this rule ignores classes.
202
203 Examples of **correct** code for `{ "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false }`:
204
205 ```js
206 /*eslint accessor-pairs: ["error", {
207 "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false
208 }]*/
209
210 class Foo {
211 get a() {
212 return this.val;
213 }
214 }
215
216 class Bar {
217 static set a(value) {
218 this.val = value;
219 }
220 }
221
222 const Baz = class {
223 static get a() {
224 return this.val;
225 }
226 }
227
228 const Quux = class {
229 set a(value) {
230 this.val = value;
231 }
232 }
233 ```
234
235
236 ## Known Limitations
237
238 Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases
239 might not report a missing pair for a getter/setter that has a computed key, like in the following example:
240
241 ```js
242 /*eslint accessor-pairs: "error"*/
243
244 var a = 1;
245
246 // no warnings
247 var o = {
248 get [a++]() {
249 return this.val;
250 },
251 set [a++](value) {
252 this.val = value;
253 }
254 };
255 ```
256
257 Also, this rule does not disallow duplicate keys in object literals and class definitions, and in certain cases with duplicate keys
258 might not report a missing pair for a getter/setter, like in the following example:
259
260 ```js
261 /*eslint accessor-pairs: "error"*/
262
263 // no warnings
264 var o = {
265 get a() {
266 return this.val;
267 },
268 a: 1,
269 set a(value) {
270 this.val = value;
271 }
272 };
273 ```
274
275 The code above creates an object with just a setter for the property `"a"`.
276
277 See [no-dupe-keys](no-dupe-keys.md) if you also want to disallow duplicate keys in object literals.
278
279 See [no-dupe-class-members](no-dupe-class-members.md) if you also want to disallow duplicate names in class definitions.
280
281 ## When Not To Use It
282
283 You can turn this rule off if you are not concerned with the simultaneous presence of setters and getters on objects.
284
285 ## Further Reading
286
287 * [Object Setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set)
288 * [Object Getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)
289 * [Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects)