]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-restricted-globals.md
first commit
[pve-eslint.git] / eslint / docs / rules / no-restricted-globals.md
1 # Disallow specific global variables (no-restricted-globals)
2
3 Disallowing usage of specific global variables can be useful if you want to allow a set of global
4 variables by enabling an environment, but still want to disallow some of those.
5
6 For instance, early Internet Explorer versions exposed the current DOM event as a global variable
7 `event`, but using this variable has been considered as a bad practice for a long time. Restricting
8 this will make sure this variable isn't used in browser code.
9
10 ## Rule Details
11
12 This rule allows you to specify global variable names that you don't want to use in your application.
13
14 ## Options
15
16 This rule takes a list of strings, where each string is a global to be restricted:
17
18 ```json
19 {
20 "rules": {
21 "no-restricted-globals": ["error", "event", "fdescribe"]
22 }
23 }
24 ```
25
26 Alternatively, the rule also accepts objects, where the global name and an optional custom message are specified:
27
28 ```json
29 {
30 "rules": {
31 "no-restricted-globals": [
32 "error",
33 {
34 "name": "event",
35 "message": "Use local parameter instead."
36 },
37 {
38 "name": "fdescribe",
39 "message": "Do not commit fdescribe. Use describe instead."
40 }
41 ]
42 }
43 }
44 ```
45
46 Examples of **incorrect** code for sample `"event", "fdescribe"` global variable names:
47
48 ```js
49 /*global event, fdescribe*/
50 /*eslint no-restricted-globals: ["error", "event", "fdescribe"]*/
51
52 function onClick() {
53 console.log(event);
54 }
55
56 fdescribe("foo", function() {
57 });
58 ```
59
60 Examples of **correct** code for a sample `"event"` global variable name:
61
62 ```js
63 /*global event*/
64 /*eslint no-restricted-globals: ["error", "event"]*/
65
66 import event from "event-module";
67 ```
68
69 ```js
70 /*global event*/
71 /*eslint no-restricted-globals: ["error", "event"]*/
72
73 var event = 1;
74 ```
75
76 Examples of **incorrect** code for a sample `"event"` global variable name, along with a custom error message:
77
78 ```js
79 /*global event*/
80 /* eslint no-restricted-globals: ["error", { name: "event", message: "Use local parameter instead." }] */
81
82 function onClick() {
83 console.log(event); // Unexpected global variable 'event'. Use local parameter instead.
84 }
85 ```
86
87 ## Related Rules
88
89 * [no-restricted-properties](no-restricted-properties.md)
90 * [no-restricted-syntax](no-restricted-syntax.md)