Skip to content

Commit 38bd88c

Browse files
author
Will Binns-Smith
committed
Re-modernize and cleanup with node 4-compatible features
1 parent a611a27 commit 38bd88c

File tree

2 files changed

+412
-494
lines changed

2 files changed

+412
-494
lines changed

lib/rules/no-unused-state.js

+46-46
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
var Components = require('../util/Components');
12+
const Components = require('../util/Components');
1313

1414
// Descend through all wrapping TypeCastExpressions and return the expression
1515
// that was cast.
@@ -40,10 +40,10 @@ function isThisExpression(node) {
4040
function getInitialClassInfo() {
4141
return {
4242
// Set of nodes where state fields were defined.
43-
stateFields: [],
43+
stateFields: new Set(),
4444

4545
// Set of names of state fields that we've seen used.
46-
usedStateFields: [],
46+
usedStateFields: new Set(),
4747

4848
// Names of local variables that may be pointing to this.state. To
4949
// track this properly, we would need to keep track of all locals,
@@ -69,57 +69,57 @@ module.exports = {
6969
// not yet encountered any use of this.state which we have chosen not to
7070
// analyze. If we encounter any such usage (like this.state being spread as
7171
// JSX attributes), then this is again set to null.
72-
var classInfo = null;
72+
let classInfo = null;
7373

7474
// Returns true if the given node is possibly a reference to `this.state`.
7575
function isStateReference(node) {
7676
node = uncast(node);
7777

78-
var isDirectStateReference =
78+
const isDirectStateReference =
7979
node.type === 'MemberExpression' &&
8080
isThisExpression(node.object) &&
8181
node.property.name === 'state';
8282

83-
var isAliasedStateReference =
83+
const isAliasedStateReference =
8484
node.type === 'Identifier' &&
8585
classInfo.aliases &&
86-
classInfo.aliases.indexOf(node.name) >= 0;
86+
classInfo.aliases.has(node.name);
8787

8888
return isDirectStateReference || isAliasedStateReference;
8989
}
9090

9191
// Takes an ObjectExpression node and adds all named Property nodes to the
9292
// current set of state fields.
9393
function addStateFields(node) {
94-
node.properties.forEach(function(prop) {
94+
for (const prop of node.properties) {
9595
if (prop.type === 'Property' && getName(prop.key) !== null) {
96-
classInfo.stateFields.push(prop);
96+
classInfo.stateFields.add(prop);
9797
}
98-
});
98+
}
9999
}
100100

101101
// Adds the name of the given node as a used state field if the node is an
102102
// Identifier or a Literal. Other node types are ignored.
103103
function addUsedStateField(node) {
104-
var name = getName(node);
104+
const name = getName(node);
105105
if (name) {
106-
classInfo.usedStateFields.push(name);
106+
classInfo.usedStateFields.add(name);
107107
}
108108
}
109109

110110
// Records used state fields and new aliases for an ObjectPattern which
111111
// destructures `this.state`.
112112
function handleStateDestructuring(node) {
113-
node.properties.forEach(function(prop) {
113+
for (const prop of node.properties) {
114114
if (prop.type === 'Property') {
115115
addUsedStateField(prop.key);
116116
} else if (
117117
prop.type === 'ExperimentalRestProperty' &&
118118
classInfo.aliases
119119
) {
120-
classInfo.aliases.push(getName(prop.argument));
120+
classInfo.aliases.add(getName(prop.argument));
121121
}
122-
});
122+
}
123123
}
124124

125125
// Used to record used state fields and new aliases for both
@@ -128,54 +128,54 @@ module.exports = {
128128
switch (left.type) {
129129
case 'Identifier':
130130
if (isStateReference(right) && classInfo.aliases) {
131-
classInfo.aliases.push(left.name);
131+
classInfo.aliases.add(left.name);
132132
}
133133
break;
134134
case 'ObjectPattern':
135135
if (isStateReference(right)) {
136136
handleStateDestructuring(left);
137137
} else if (isThisExpression(right) && classInfo.aliases) {
138-
left.properties.forEach(function(prop) {
138+
for (const prop of left.properties) {
139139
if (prop.type === 'Property' && getName(prop.key) === 'state') {
140-
var name = getName(prop.value);
140+
const name = getName(prop.value);
141141
if (name) {
142-
classInfo.aliases.push(name);
142+
classInfo.aliases.add(name);
143143
} else if (prop.value.type === 'ObjectPattern') {
144144
handleStateDestructuring(prop.value);
145145
}
146146
}
147-
});
147+
}
148148
}
149149
break;
150150
default:
151-
// pass
151+
// pass
152152
}
153153
}
154154

155155
function reportUnusedFields() {
156156
// Report all unused state fields.
157-
classInfo.stateFields.forEach(function(node) {
158-
var name = getName(node.key);
159-
if (classInfo.usedStateFields.indexOf(name) < 0) {
160-
context.report(node, 'Unused state field: \'' + name + '\'');
157+
for (const node of classInfo.stateFields) {
158+
const name = getName(node.key);
159+
if (!classInfo.usedStateFields.has(name)) {
160+
context.report(node, `Unused state field: '${name}'`);
161161
}
162-
});
162+
}
163163
}
164164

165165
return {
166-
ClassDeclaration: function(node) {
166+
ClassDeclaration(node) {
167167
if (utils.isES6Component(node)) {
168168
classInfo = getInitialClassInfo();
169169
}
170170
},
171171

172-
ObjectExpression: function(node) {
172+
ObjectExpression(node) {
173173
if (utils.isES5Component(node)) {
174174
classInfo = getInitialClassInfo();
175175
}
176176
},
177177

178-
'ObjectExpression:exit': function(node) {
178+
'ObjectExpression:exit'(node) {
179179
if (!classInfo) {
180180
return;
181181
}
@@ -186,15 +186,15 @@ module.exports = {
186186
}
187187
},
188188

189-
'ClassDeclaration:exit': function() {
189+
'ClassDeclaration:exit'() {
190190
if (!classInfo) {
191191
return;
192192
}
193193
reportUnusedFields();
194194
classInfo = null;
195195
},
196196

197-
CallExpression: function(node) {
197+
CallExpression(node) {
198198
if (!classInfo) {
199199
return;
200200
}
@@ -211,7 +211,7 @@ module.exports = {
211211
}
212212
},
213213

214-
ClassProperty: function(node) {
214+
ClassProperty(node) {
215215
if (!classInfo) {
216216
return;
217217
}
@@ -227,35 +227,35 @@ module.exports = {
227227
}
228228
},
229229

230-
MethodDefinition: function() {
230+
MethodDefinition() {
231231
if (!classInfo) {
232232
return;
233233
}
234234
// Create a new set for this.state aliases local to this method.
235-
classInfo.aliases = [];
235+
classInfo.aliases = new Set();
236236
},
237237

238-
'MethodDefinition:exit': function() {
238+
'MethodDefinition:exit'() {
239239
if (!classInfo) {
240240
return;
241241
}
242242
// Forget our set of local aliases.
243243
classInfo.aliases = null;
244244
},
245245

246-
FunctionExpression: function(node) {
246+
FunctionExpression(node) {
247247
if (!classInfo) {
248248
return;
249249
}
250250

251-
var parent = node.parent;
251+
const parent = node.parent;
252252
if (!utils.isES5Component(parent.parent)) {
253253
return;
254254
}
255255

256256
if (parent.key.name === 'getInitialState') {
257-
var body = node.body.body;
258-
var lastBodyNode = body[body.length - 1];
257+
const body = node.body.body;
258+
const lastBodyNode = body[body.length - 1];
259259

260260
if (
261261
lastBodyNode.type === 'ReturnStatement' &&
@@ -265,11 +265,11 @@ module.exports = {
265265
}
266266
} else {
267267
// Create a new set for this.state aliases local to this method.
268-
classInfo.aliases = [];
268+
classInfo.aliases = new Set();
269269
}
270270
},
271271

272-
AssignmentExpression: function(node) {
272+
AssignmentExpression(node) {
273273
if (!classInfo) {
274274
return;
275275
}
@@ -281,7 +281,7 @@ module.exports = {
281281
node.right.type === 'ObjectExpression'
282282
) {
283283
// Find the nearest function expression containing this assignment.
284-
var fn = node;
284+
let fn = node;
285285
while (fn.type !== 'FunctionExpression' && fn.parent) {
286286
fn = fn.parent;
287287
}
@@ -300,14 +300,14 @@ module.exports = {
300300
}
301301
},
302302

303-
VariableDeclarator: function(node) {
303+
VariableDeclarator(node) {
304304
if (!classInfo || !node.init) {
305305
return;
306306
}
307307
handleAssignment(node.id, node.init);
308308
},
309309

310-
MemberExpression: function(node) {
310+
MemberExpression(node) {
311311
if (!classInfo) {
312312
return;
313313
}
@@ -322,13 +322,13 @@ module.exports = {
322322
}
323323
},
324324

325-
JSXSpreadAttribute: function(node) {
325+
JSXSpreadAttribute(node) {
326326
if (classInfo && isStateReference(node.argument)) {
327327
classInfo = null;
328328
}
329329
},
330330

331-
ExperimentalSpreadProperty: function(node) {
331+
ExperimentalSpreadProperty(node) {
332332
if (classInfo && isStateReference(node.argument)) {
333333
classInfo = null;
334334
}

0 commit comments

Comments
 (0)