]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/id-denylist.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / id-denylist.md
1 ---
2 title: id-denylist
3 layout: doc
4 rule_type: suggestion
5 ---
6
7
8 > "There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
9
10 Generic names can lead to hard-to-decipher code. This rule allows you to specify a deny list of disallowed identifier names to avoid this practice.
11
12 ## Rule Details
13
14 This rule disallows specified identifiers in assignments and `function` definitions.
15
16 This rule will catch disallowed identifiers that are:
17
18 * variable declarations
19 * function declarations
20 * object properties assigned to during object creation
21 * class fields
22 * class methods
23
24 It will not catch disallowed identifiers that are:
25
26 * function calls (so you can still use functions you do not have control over)
27 * object properties (so you can still use objects you do not have control over)
28
29 ## Options
30
31 The rule takes one or more strings as options: the names of restricted identifiers.
32
33 For example, to restrict the use of common generic identifiers:
34
35 ```json
36 {
37 "id-denylist": ["error", "data", "err", "e", "cb", "callback"]
38 }
39 ```
40
41 **Note:** The first element of the array is for the rule severity (see [configuring rules](/docs/latest/user-guide/configuring/rules). The other elements in the array are the identifiers that you want to disallow.
42
43 Examples of **incorrect** code for this rule with sample `"data", "callback"` restricted identifiers:
44
45 ::: incorrect
46
47 ```js
48 /*eslint id-denylist: ["error", "data", "callback"] */
49
50 var data = {...};
51
52 function callback() {
53 // ...
54 }
55
56 element.callback = function() {
57 // ...
58 };
59
60 var itemSet = {
61 data: [...]
62 };
63
64 class Foo {
65 data = [];
66 }
67
68 class Foo {
69 #data = [];
70 }
71
72 class Foo {
73 callback( {);
74 }
75
76 class Foo {
77 #callback( {);
78 }
79 ```
80
81 :::
82
83 Examples of **correct** code for this rule with sample `"data", "callback"` restricted identifiers:
84
85 ::: correct
86
87 ```js
88 /*eslint id-denylist: ["error", "data", "callback"] */
89
90 var encodingOptions = {...};
91
92 function processFileResult() {
93 // ...
94 }
95
96 element.successHandler = function() {
97 // ...
98 };
99
100 var itemSet = {
101 entities: [...]
102 };
103
104 callback(); // all function calls are ignored
105
106 foo.callback(); // all function calls are ignored
107
108 foo.data; // all property names that are not assignments are ignored
109
110 class Foo {
111 items = [];
112 }
113
114 class Foo {
115 #items = [];
116 }
117
118 class Foo {
119 method( {);
120 }
121
122 class Foo {
123 #method( {);
124 }
125 ```
126
127 :::
128
129 ## When Not To Use It
130
131 You can turn this rule off if you do not want to restrict the use of certain identifiers.