]>
Commit | Line | Data |
---|---|---|
6f036462 TL |
1 | # disallow specified identifiers (id-denylist) |
2 | ||
3 | > "There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton | |
4 | ||
5 | 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. | |
6 | ||
7 | ## Rule Details | |
8 | ||
9 | This rule disallows specified identifiers in assignments and `function` definitions. | |
10 | ||
11 | This rule will catch disallowed identifiers that are: | |
12 | ||
13 | - variable declarations | |
14 | - function declarations | |
15 | - object properties assigned to during object creation | |
609c276f TL |
16 | - class fields |
17 | - class methods | |
6f036462 TL |
18 | |
19 | It will not catch disallowed identifiers that are: | |
20 | ||
21 | - function calls (so you can still use functions you do not have control over) | |
22 | - object properties (so you can still use objects you do not have control over) | |
23 | ||
24 | ## Options | |
25 | ||
26 | The rule takes one or more strings as options: the names of restricted identifiers. | |
27 | ||
28 | For example, to restrict the use of common generic identifiers: | |
29 | ||
30 | ```json | |
31 | { | |
32 | "id-denylist": ["error", "data", "err", "e", "cb", "callback"] | |
33 | } | |
34 | ``` | |
35 | ||
36 | Examples of **incorrect** code for this rule with sample `"data", "callback"` restricted identifiers: | |
37 | ||
38 | ```js | |
39 | /*eslint id-denylist: ["error", "data", "callback"] */ | |
40 | ||
41 | var data = {...}; | |
42 | ||
43 | function callback() { | |
44 | // ... | |
45 | } | |
46 | ||
47 | element.callback = function() { | |
48 | // ... | |
49 | }; | |
50 | ||
51 | var itemSet = { | |
52 | data: [...] | |
53 | }; | |
609c276f TL |
54 | |
55 | class Foo { | |
56 | data = []; | |
57 | } | |
58 | ||
59 | class Foo { | |
60 | #data = []; | |
61 | } | |
62 | ||
63 | class Foo { | |
64 | callback( {); | |
65 | } | |
66 | ||
67 | class Foo { | |
68 | #callback( {); | |
69 | } | |
6f036462 TL |
70 | ``` |
71 | ||
72 | Examples of **correct** code for this rule with sample `"data", "callback"` restricted identifiers: | |
73 | ||
74 | ```js | |
75 | /*eslint id-denylist: ["error", "data", "callback"] */ | |
76 | ||
77 | var encodingOptions = {...}; | |
78 | ||
79 | function processFileResult() { | |
80 | // ... | |
81 | } | |
82 | ||
83 | element.successHandler = function() { | |
84 | // ... | |
85 | }; | |
86 | ||
87 | var itemSet = { | |
88 | entities: [...] | |
89 | }; | |
90 | ||
91 | callback(); // all function calls are ignored | |
92 | ||
93 | foo.callback(); // all function calls are ignored | |
94 | ||
95 | foo.data; // all property names that are not assignments are ignored | |
609c276f TL |
96 | |
97 | class Foo { | |
98 | items = []; | |
99 | } | |
100 | ||
101 | class Foo { | |
102 | #items = []; | |
103 | } | |
104 | ||
105 | class Foo { | |
106 | method( {); | |
107 | } | |
108 | ||
109 | class Foo { | |
110 | #method( {); | |
111 | } | |
6f036462 TL |
112 | ``` |
113 | ||
114 | ## When Not To Use It | |
115 | ||
116 | You can turn this rule off if you do not want to restrict the use of certain identifiers. |