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