]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/prefer-reflect.md
ea2ee7d416d938ed5e491be0f08b87f2502f5261
[pve-eslint.git] / eslint / docs / src / rules / prefer-reflect.md
1 ---
2 title: prefer-reflect
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-useless-call
7 - prefer-spread
8 - no-delete-var
9 ---
10
11
12 This rule was **deprecated** in ESLint v3.9.0 and will not be replaced. The original intent of this rule now seems misguided as we have come to understand that `Reflect` methods are not actually intended to replace the `Object` counterparts the rule suggests, but rather exist as low-level primitives to be used with proxies in order to replicate the default behavior of various previously existing functionality.
13
14 The ES6 Reflect API comes with a handful of methods which somewhat deprecate methods on old constructors:
15
16 * [`Reflect.apply`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.apply) effectively deprecates [`Function.prototype.apply`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.apply) and [`Function.prototype.call`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.call)
17 * [`Reflect.deleteProperty`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.deleteproperty) effectively deprecates the [`delete` keyword](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-delete-operator-runtime-semantics-evaluation)
18 * [`Reflect.getOwnPropertyDescriptor`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.getownpropertydescriptor) effectively deprecates [`Object.getOwnPropertyDescriptor`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.getownpropertydescriptor)
19 * [`Reflect.getPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.getprototypeof) effectively deprecates [`Object.getPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.getprototypeof)
20 * [`Reflect.setPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.setprototypeof) effectively deprecates [`Object.setPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.setprototypeof)
21 * [`Reflect.preventExtensions`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.preventextensions) effectively deprecates [`Object.preventExtensions`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.preventextensions)
22
23 The prefer-reflect rule will flag usage of any older method, suggesting to instead use the newer Reflect version.
24
25 ## Rule Details
26
27 ## Options
28
29 ### Exceptions
30
31 ```js
32 "prefer-reflect": [<enabled>, { "exceptions": [<...exceptions>] }]
33 ```
34
35 The `exceptions` option allows you to pass an array of methods names you'd like to continue to use in the old style.
36
37 For example if you wish to use all Reflect methods, except for `Function.prototype.apply` then your config would look like `prefer-reflect: [2, { "exceptions": ["apply"] }]`.
38
39 If you want to use Reflect methods, but keep using the `delete` keyword, then your config would look like `prefer-reflect: [2, { "exceptions": ["delete"] }]`.
40
41 These can be combined as much as you like. To make all methods exceptions (thereby rendering this rule useless), use `prefer-reflect: [2, { "exceptions": ["apply", "call", "defineProperty", "getOwnPropertyDescriptor", "getPrototypeOf", "setPrototypeOf", "isExtensible", "getOwnPropertyNames", "preventExtensions", "delete"] }]`
42
43 ### Reflect.apply
44
45 Deprecates `Function.prototype.apply()` and `Function.prototype.call()`
46
47 Examples of **incorrect** code for this rule when used without exceptions:
48
49 ::: incorrect
50
51 ```js
52 /*eslint prefer-reflect: "error"*/
53
54 myFunction.apply(undefined, args);
55 myFunction.apply(null, args);
56 obj.myMethod.apply(obj, args);
57 obj.myMethod.apply(other, args);
58
59 myFunction.call(undefined, arg);
60 myFunction.call(null, arg);
61 obj.myMethod.call(obj, arg);
62 obj.myMethod.call(other, arg);
63 ```
64
65 :::
66
67 Examples of **correct** code for this rule when used without exceptions:
68
69 ::: correct
70
71 ```js
72 /*eslint prefer-reflect: "error"*/
73
74 Reflect.apply(myFunction, undefined, args);
75 Reflect.apply(myFunction, null, args);
76 Reflect.apply(obj.myMethod, obj, args);
77 Reflect.apply(obj.myMethod, other, args);
78 Reflect.apply(myFunction, undefined, [arg]);
79 Reflect.apply(myFunction, null, [arg]);
80 Reflect.apply(obj.myMethod, obj, [arg]);
81 Reflect.apply(obj.myMethod, other, [arg]);
82 ```
83
84 :::
85
86 Examples of **correct** code for this rule with the `{ "exceptions": ["apply"] }` option:
87
88 ::: correct
89
90 ```js
91 /*eslint prefer-reflect: ["error", { "exceptions": ["apply"] }]*/
92
93 // in addition to Reflect.apply(...):
94 myFunction.apply(undefined, args);
95 myFunction.apply(null, args);
96 obj.myMethod.apply(obj, args);
97 obj.myMethod.apply(other, args);
98 ```
99
100 :::
101
102 Examples of **correct** code for this rule with the `{ "exceptions": ["call"] }` option:
103
104 ::: correct
105
106 ```js
107 /*eslint prefer-reflect: ["error", { "exceptions": ["call"] }]*/
108
109 // in addition to Reflect.apply(...):
110 myFunction.call(undefined, arg);
111 myFunction.call(null, arg);
112 obj.myMethod.call(obj, arg);
113 obj.myMethod.call(other, arg);
114 ```
115
116 :::
117
118 ### Reflect.defineProperty
119
120 Deprecates `Object.defineProperty()`
121
122 Examples of **incorrect** code for this rule when used without exceptions:
123
124 ::: incorrect
125
126 ```js
127 /*eslint prefer-reflect: "error"*/
128
129 Object.defineProperty({}, 'foo', {value: 1})
130 ```
131
132 :::
133
134 Examples of **correct** code for this rule when used without exceptions:
135
136 ::: correct
137
138 ```js
139 /*eslint prefer-reflect: "error"*/
140
141 Reflect.defineProperty({}, 'foo', {value: 1})
142 ```
143
144 :::
145
146 Examples of **correct** code for this rule with the `{ "exceptions": ["defineProperty"] }` option:
147
148 ::: correct
149
150 ```js
151 /*eslint prefer-reflect: ["error", { "exceptions": ["defineProperty"] }]*/
152
153 Object.defineProperty({}, 'foo', {value: 1})
154 Reflect.defineProperty({}, 'foo', {value: 1})
155 ```
156
157 :::
158
159 ### Reflect.getOwnPropertyDescriptor
160
161 Deprecates `Object.getOwnPropertyDescriptor()`
162
163 Examples of **incorrect** code for this rule when used without exceptions:
164
165 ::: incorrect
166
167 ```js
168 /*eslint prefer-reflect: "error"*/
169
170 Object.getOwnPropertyDescriptor({}, 'foo')
171 ```
172
173 :::
174
175 Examples of **correct** code for this rule when used without exceptions:
176
177 ::: correct
178
179 ```js
180 /*eslint prefer-reflect: "error"*/
181
182 Reflect.getOwnPropertyDescriptor({}, 'foo')
183 ```
184
185 :::
186
187 Examples of **correct** code for this rule with the `{ "exceptions": ["getOwnPropertyDescriptor"] }` option:
188
189 ::: correct
190
191 ```js
192 /*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyDescriptor"] }]*/
193
194 Object.getOwnPropertyDescriptor({}, 'foo')
195 Reflect.getOwnPropertyDescriptor({}, 'foo')
196 ```
197
198 :::
199
200 ### Reflect.getPrototypeOf
201
202 Deprecates `Object.getPrototypeOf()`
203
204 Examples of **incorrect** code for this rule when used without exceptions:
205
206 ::: incorrect
207
208 ```js
209 /*eslint prefer-reflect: "error"*/
210
211 Object.getPrototypeOf({}, 'foo')
212 ```
213
214 :::
215
216 Examples of **correct** code for this rule when used without exceptions:
217
218 ::: correct
219
220 ```js
221 /*eslint prefer-reflect: "error"*/
222
223 Reflect.getPrototypeOf({}, 'foo')
224 ```
225
226 :::
227
228 Examples of **correct** code for this rule with the `{ "exceptions": ["getPrototypeOf"] }` option:
229
230 ::: correct
231
232 ```js
233 /*eslint prefer-reflect: ["error", { "exceptions": ["getPrototypeOf"] }]*/
234
235 Object.getPrototypeOf({}, 'foo')
236 Reflect.getPrototypeOf({}, 'foo')
237 ```
238
239 :::
240
241 ### Reflect.setPrototypeOf
242
243 Deprecates `Object.setPrototypeOf()`
244
245 Examples of **incorrect** code for this rule when used without exceptions:
246
247 ::: incorrect
248
249 ```js
250 /*eslint prefer-reflect: "error"*/
251
252 Object.setPrototypeOf({}, Object.prototype)
253 ```
254
255 :::
256
257 Examples of **correct** code for this rule when used without exceptions:
258
259 ::: correct
260
261 ```js
262 /*eslint prefer-reflect: "error"*/
263
264 Reflect.setPrototypeOf({}, Object.prototype)
265 ```
266
267 :::
268
269 Examples of **correct** code for this rule with the `{ "exceptions": ["setPrototypeOf"] }` option:
270
271 ::: correct
272
273 ```js
274 /*eslint prefer-reflect: ["error", { "exceptions": ["setPrototypeOf"] }]*/
275
276 Object.setPrototypeOf({}, Object.prototype)
277 Reflect.setPrototypeOf({}, Object.prototype)
278 ```
279
280 :::
281
282 ### Reflect.isExtensible
283
284 Deprecates `Object.isExtensible`
285
286 Examples of **incorrect** code for this rule when used without exceptions:
287
288 ::: incorrect
289
290 ```js
291 /*eslint prefer-reflect: "error"*/
292
293 Object.isExtensible({})
294 ```
295
296 :::
297
298 Examples of **correct** code for this rule when used without exceptions:
299
300 ::: correct
301
302 ```js
303 /*eslint prefer-reflect: "error"*/
304
305 Reflect.isExtensible({})
306 ```
307
308 :::
309
310 Examples of **correct** code for this rule with the `{ "exceptions": ["isExtensible"] }` option:
311
312 ::: correct
313
314 ```js
315 /*eslint prefer-reflect: ["error", { "exceptions": ["isExtensible"] }]*/
316
317 Object.isExtensible({})
318 Reflect.isExtensible({})
319 ```
320
321 :::
322
323 ### Reflect.getOwnPropertyNames
324
325 Deprecates `Object.getOwnPropertyNames()`
326
327 Examples of **incorrect** code for this rule when used without exceptions:
328
329 ::: incorrect
330
331 ```js
332 /*eslint prefer-reflect: "error"*/
333
334 Object.getOwnPropertyNames({})
335 ```
336
337 :::
338
339 Examples of **correct** code for this rule when used without exceptions:
340
341 ::: correct
342
343 ```js
344 /*eslint prefer-reflect: "error"*/
345
346 Reflect.getOwnPropertyNames({})
347 ```
348
349 :::
350
351 Examples of **correct** code for this rule with the `{ "exceptions": ["getOwnPropertyNames"] }` option:
352
353 ::: correct
354
355 ```js
356 /*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyNames"] }]*/
357
358 Object.getOwnPropertyNames({})
359 Reflect.getOwnPropertyNames({})
360 ```
361
362 :::
363
364 ### Reflect.preventExtensions
365
366 Deprecates `Object.preventExtensions()`
367
368 Examples of **incorrect** code for this rule when used without exceptions:
369
370 ::: incorrect
371
372 ```js
373 /*eslint prefer-reflect: "error"*/
374
375 Object.preventExtensions({})
376 ```
377
378 :::
379
380 Examples of **correct** code for this rule when used without exceptions:
381
382 ::: correct
383
384 ```js
385 /*eslint prefer-reflect: "error"*/
386
387 Reflect.preventExtensions({})
388 ```
389
390 :::
391
392 Examples of **correct** code for this rule with the `{ "exceptions": ["preventExtensions"] }` option:
393
394 ::: correct
395
396 ```js
397 /*eslint prefer-reflect: ["error", { "exceptions": ["preventExtensions"] }]*/
398
399 Object.preventExtensions({})
400 Reflect.preventExtensions({})
401 ```
402
403 :::
404
405 ### Reflect.deleteProperty
406
407 Deprecates the `delete` keyword
408
409 Examples of **incorrect** code for this rule when used without exceptions:
410
411 ::: incorrect
412
413 ```js
414 /*eslint prefer-reflect: "error"*/
415
416 delete foo.bar; // deleting object property
417 ```
418
419 :::
420
421 Examples of **correct** code for this rule when used without exceptions:
422
423 ::: correct
424
425 ```js
426 /*eslint prefer-reflect: "error"*/
427
428 delete bar; // deleting variable
429 Reflect.deleteProperty(foo, 'bar');
430 ```
431
432 :::
433
434 Note: For a rule preventing deletion of variables, see [no-delete-var instead](no-delete-var)
435
436 Examples of **correct** code for this rule with the `{ "exceptions": ["delete"] }` option:
437
438 ::: correct
439
440 ```js
441 /*eslint prefer-reflect: ["error", { "exceptions": ["delete"] }]*/
442
443 delete bar
444 delete foo.bar
445 Reflect.deleteProperty(foo, 'bar');
446 ```
447
448 :::
449
450 ## When Not To Use It
451
452 This rule should not be used in ES3/5 environments.
453
454 In ES2015 (ES6) or later, if you don't want to be notified about places where Reflect could be used, you can safely disable this rule.