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