This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-unused-variables.js
143 lines (119 loc) · 3.61 KB
/
disallow-unused-variables.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Disallows unused variables defined with var, let or const.
*
* Types: `Boolean`
*
* Values: `true`
*
* #### Example
*
* ```js
* "disallowUnusedVariables": true
* ```
*
* ##### Valid
*
* ```js
* var x=1;
*
* function getX() {
* return x;
* }
*
* ```
*
* ##### Invalid
*
* ```js
* var x=1;
*
* function getX() {
* return true;
* }
*
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value or should be removed'
);
},
getOptionName: function() {
return 'disallowUnusedVariables';
},
check: function(file, errors) {
var program = file.getProgram();
var variableList = [];
var nodesToCheck = [];
var unusedNodes = [];
function reportError(node) {
errors.add('Variable `' + node.name + '` is not used', node);
}
function isVariableGood(variable) {
var parentCheck = function(node) {
if (node.parentElement) {
if (node.parentElement.type === 'VariableDeclaration') {
var grandparentElement = node.parentElement.parentElement;
return grandparentElement.type !== 'ExportNamedDeclaration';
} else if (
node.parentElement.type === 'VariableDeclarator' ||
node.parentElement.type === 'ObjectProperty' ||
node.parentElement.isPattern
) {
return parentCheck(node.parentElement);
}
} else {
return false;
}
};
var useVariable = variable.getDefinitions().some(function checkVariableDefinition(definition) {
return parentCheck(definition.node);
});
return useVariable;
}
function getVariablesInAllScopes(scope) {
var variableList = [];
var iterateChildScopes = function(scope) {
scope.getVariables().forEach(function(variable) {
variableList.push(variable);
});
scope.childScopes.forEach(function(childScope) {
return iterateChildScopes(childScope);
});
};
iterateChildScopes(scope);
return variableList;
}
// Get all variables in all scopes.
variableList = getVariablesInAllScopes(file.getScopes().acquire(program));
// Check if variables are what we want to check..
variableList.reduce(function(acc, variable) {
if (isVariableGood(variable)) {
acc.push(variable);
}
return acc;
}, nodesToCheck);
// Check if variables are used.
nodesToCheck.reduce(function checkVariableReferences(acc, variable) {
if (variable.getReferences().length === 1) {
variable.getDefinitions().forEach(function addUnusedVariable(definition) {
acc.push(definition.node);
});
}
return acc;
}, unusedNodes);
unusedNodes.forEach(reportError);
},
_fix: function(file, error) {
var node = error.element;
while (node.type !== 'VariableDeclaration') {
node = node.parentElement;
}
node.parentElement.removeChild(node);
error.fixed = true;
}
};