1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function(context) {
|
13 | var options = {
|
14 | builtinGlobals: Boolean(context.options[0] && context.options[0].builtinGlobals)
|
15 | };
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function findVariablesInScope(scope) {
|
24 | scope.variables.forEach(function(variable) {
|
25 | var hasBuiltin = options.builtinGlobals && "writeable" in variable;
|
26 | var count = (hasBuiltin ? 1 : 0) + variable.identifiers.length;
|
27 |
|
28 | if (count >= 2) {
|
29 | variable.identifiers.sort(function(a, b) {
|
30 | return a.range[1] - b.range[1];
|
31 | });
|
32 |
|
33 | for (var i = (hasBuiltin ? 0 : 1), l = variable.identifiers.length; i < l; i++) {
|
34 | context.report(
|
35 | variable.identifiers[i],
|
36 | "'{{a}}' is already defined",
|
37 | {a: variable.name});
|
38 | }
|
39 | }
|
40 | });
|
41 |
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function checkForGlobal(node) {
|
51 | var scope = context.getScope(),
|
52 | parserOptions = context.parserOptions,
|
53 | ecmaFeatures = parserOptions.ecmaFeatures || {};
|
54 |
|
55 |
|
56 | if (ecmaFeatures.globalReturn || node.sourceType === "module") {
|
57 | findVariablesInScope(scope.childScopes[0]);
|
58 | } else {
|
59 | findVariablesInScope(scope);
|
60 | }
|
61 | }
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | function checkForBlock() {
|
69 | findVariablesInScope(context.getScope());
|
70 | }
|
71 |
|
72 | if (context.parserOptions.ecmaVersion >= 6) {
|
73 | return {
|
74 | "Program": checkForGlobal,
|
75 | "BlockStatement": checkForBlock,
|
76 | "SwitchStatement": checkForBlock
|
77 | };
|
78 | } else {
|
79 | return {
|
80 | "Program": checkForGlobal,
|
81 | "FunctionDeclaration": checkForBlock,
|
82 | "FunctionExpression": checkForBlock,
|
83 | "ArrowFunctionExpression": checkForBlock
|
84 | };
|
85 | }
|
86 | };
|
87 |
|
88 | module.exports.schema = [
|
89 | {
|
90 | "type": "object",
|
91 | "properties": {
|
92 | "builtinGlobals": {"type": "boolean"}
|
93 | },
|
94 | "additionalProperties": false
|
95 | }
|
96 | ];
|