]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-useless-computed-key.md
first commit
[pve-eslint.git] / eslint / docs / rules / no-useless-computed-key.md
1 # Disallow unnecessary computed property keys in objects and classes (no-useless-computed-key)
2
3 It's unnecessary to use computed properties with literals such as:
4
5 ```js
6 var foo = {["a"]: "b"};
7 ```
8
9 The code can be rewritten as:
10
11 ```js
12 var foo = {"a": "b"};
13 ```
14
15 ## Rule Details
16
17 This rule disallows unnecessary usage of computed property keys.
18
19 Examples of **incorrect** code for this rule:
20
21 ```js
22 /*eslint no-useless-computed-key: "error"*/
23 /*eslint-env es6*/
24
25 var a = { ['0']: 0 };
26 var a = { ['0+1,234']: 0 };
27 var a = { [0]: 0 };
28 var a = { ['x']: 0 };
29 var a = { ['x']() {} };
30 ```
31
32 Examples of **correct** code for this rule:
33
34 ```js
35 /*eslint no-useless-computed-key: "error"*/
36
37 var c = { 'a': 0 };
38 var c = { 0: 0 };
39 var a = { x() {} };
40 var c = { a: 0 };
41 var c = { '0+1,234': 0 };
42 ```
43
44 ## Options
45
46 This rule has an object option:
47
48 * `enforceForClassMembers` set to `true` additionally applies this rule to class members (Default `false`).
49
50 ### enforceForClassMembers
51
52 By default, this rule does not check class declarations and class expressions,
53 as the default value for `enforceForClassMembers` is `false`.
54
55 When `enforceForClassMembers` is set to `true`, the rule will also disallow unnecessary computed
56 keys inside of class methods, getters and setters.
57
58 Examples of **incorrect** code for `{ "enforceForClassMembers": true }`:
59
60 ```js
61 /*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/
62
63 class Foo {
64 [0]() {}
65 ['a']() {}
66 get ['b']() {}
67 set ['c'](value) {}
68
69 static ['a']() {}
70 }
71 ```
72
73 ## When Not To Use It
74
75 If you don't want to be notified about unnecessary computed property keys, you can safely disable this rule.