1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = {
|
13 | meta: {
|
14 | docs: {
|
15 | description: "disallow variable and `function` declarations in the global scope",
|
16 | category: "Best Practices",
|
17 | recommended: false,
|
18 | url: "https://eslint.org/docs/rules/no-implicit-globals"
|
19 | },
|
20 |
|
21 | schema: []
|
22 | },
|
23 |
|
24 | create(context) {
|
25 | return {
|
26 | Program() {
|
27 | const scope = context.getScope();
|
28 |
|
29 | scope.variables.forEach(variable => {
|
30 | if (variable.writeable) {
|
31 | return;
|
32 | }
|
33 |
|
34 | variable.defs.forEach(def => {
|
35 | if (def.type === "FunctionName" || (def.type === "Variable" && def.parent.kind === "var")) {
|
36 | context.report({ node: def.node, message: "Implicit global variable, assign as global property instead." });
|
37 | }
|
38 | });
|
39 | });
|
40 |
|
41 | scope.implicit.variables.forEach(variable => {
|
42 | const scopeVariable = scope.set.get(variable.name);
|
43 |
|
44 | if (scopeVariable && scopeVariable.writeable) {
|
45 | return;
|
46 | }
|
47 |
|
48 | variable.defs.forEach(def => {
|
49 | context.report({ node: def.node, message: "Implicit global variable, assign as global property instead." });
|
50 | });
|
51 | });
|
52 | }
|
53 | };
|
54 |
|
55 | }
|
56 | };
|