UNPKG

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