@@ -59,57 +59,57 @@ module.exports = {
59
59
// not yet encountered any use of this.state which we have chosen not to
60
60
// analyze. If we encounter any such usage (like this.state being spread as
61
61
// JSX attributes), then this is again set to null.
62
- let classInfo = null ;
62
+ var classInfo = null ;
63
63
64
64
// Returns true if the given node is possibly a reference to `this.state`.
65
65
function isStateReference ( node ) {
66
66
node = uncast ( node ) ;
67
67
68
- const isDirectStateReference =
68
+ var isDirectStateReference =
69
69
node . type === 'MemberExpression' &&
70
70
isThisExpression ( node . object ) &&
71
71
node . property . name === 'state' ;
72
72
73
- const isAliasedStateReference =
73
+ var isAliasedStateReference =
74
74
node . type === 'Identifier' &&
75
75
classInfo . aliases &&
76
- classInfo . aliases . has ( node . name ) ;
76
+ classInfo . aliases . indexOf ( node . name ) >= 0 ;
77
77
78
78
return isDirectStateReference || isAliasedStateReference ;
79
79
}
80
80
81
81
// Takes an ObjectExpression node and adds all named Property nodes to the
82
82
// current set of state fields.
83
83
function addStateFields ( node ) {
84
- for ( let prop of node . properties ) {
84
+ node . properties . forEach ( function ( prop ) {
85
85
if ( prop . type === 'Property' && getName ( prop . key ) !== null ) {
86
- classInfo . stateFields . add ( prop ) ;
86
+ classInfo . stateFields . push ( prop ) ;
87
87
}
88
- }
88
+ } ) ;
89
89
}
90
90
91
91
// Adds the name of the given node as a used state field if the node is an
92
92
// Identifier or a Literal. Other node types are ignored.
93
93
function addUsedStateField ( node ) {
94
- let name = getName ( node ) ;
94
+ var name = getName ( node ) ;
95
95
if ( name ) {
96
- classInfo . usedStateFields . add ( name ) ;
96
+ classInfo . usedStateFields . push ( name ) ;
97
97
}
98
98
}
99
99
100
100
// Records used state fields and new aliases for an ObjectPattern which
101
101
// destructures `this.state`.
102
102
function handleStateDestructuring ( node ) {
103
- for ( let prop of node . properties ) {
103
+ node . properties . forEach ( function ( prop ) {
104
104
if ( prop . type === 'Property' ) {
105
105
addUsedStateField ( prop . key ) ;
106
106
} else if (
107
107
prop . type === 'ExperimentalRestProperty' &&
108
108
classInfo . aliases
109
109
) {
110
- classInfo . aliases . add ( getName ( prop . argument ) ) ;
110
+ classInfo . aliases . push ( getName ( prop . argument ) ) ;
111
111
}
112
- }
112
+ } ) ;
113
113
}
114
114
115
115
// Used to record used state fields and new aliases for both
@@ -118,23 +118,23 @@ module.exports = {
118
118
switch ( left . type ) {
119
119
case 'Identifier' :
120
120
if ( isStateReference ( right ) && classInfo . aliases ) {
121
- classInfo . aliases . add ( left . name ) ;
121
+ classInfo . aliases . push ( left . name ) ;
122
122
}
123
123
break ;
124
124
case 'ObjectPattern' :
125
125
if ( isStateReference ( right ) ) {
126
126
handleStateDestructuring ( left ) ;
127
127
} else if ( isThisExpression ( right ) && classInfo . aliases ) {
128
- for ( let prop of left . properties ) {
128
+ left . properties . forEach ( function ( prop ) {
129
129
if ( prop . type === 'Property' && getName ( prop . key ) === 'state' ) {
130
- let name = getName ( prop . value ) ;
130
+ var name = getName ( prop . value ) ;
131
131
if ( name ) {
132
- classInfo . aliases . add ( name ) ;
132
+ classInfo . aliases . push ( name ) ;
133
133
} else if ( prop . value . type === 'ObjectPattern' ) {
134
134
handleStateDestructuring ( prop . value ) ;
135
135
}
136
136
}
137
- }
137
+ } ) ;
138
138
}
139
139
break ;
140
140
default :
@@ -143,19 +143,19 @@ module.exports = {
143
143
}
144
144
145
145
return {
146
- ClassDeclaration ( node ) {
146
+ ClassDeclaration : function ( node ) {
147
147
// Simple heuristic for determining whether we're in a React component.
148
- const isReactComponent = node . body . body . some (
149
- child => isMethodDefinitionWithName ( child , 'render' )
150
- ) ;
148
+ var isReactComponent = node . body . body . some ( function ( child ) {
149
+ return isMethodDefinitionWithName ( child , 'render' ) ;
150
+ } ) ;
151
151
152
152
if ( isReactComponent ) {
153
153
classInfo = {
154
154
// Set of nodes where state fields were defined.
155
- stateFields : new Set ( ) ,
155
+ stateFields : [ ] ,
156
156
157
157
// Set of names of state fields that we've seen used.
158
- usedStateFields : new Set ( ) ,
158
+ usedStateFields : [ ] ,
159
159
160
160
// Names of local variables that may be pointing to this.state. To
161
161
// track this properly, we would need to keep track of all locals,
@@ -167,21 +167,21 @@ module.exports = {
167
167
}
168
168
} ,
169
169
170
- 'ClassDeclaration:exit' ( ) {
170
+ 'ClassDeclaration:exit' : function ( ) {
171
171
if ( ! classInfo ) {
172
172
return ;
173
173
}
174
174
// Report all unused state fields.
175
- for ( let node of classInfo . stateFields ) {
176
- let name = getName ( node . key ) ;
177
- if ( ! classInfo . usedStateFields . has ( name ) ) {
178
- context . report ( node , ` Unused state field: ' ${ name } '` ) ;
175
+ classInfo . stateFields . forEach ( function ( node ) {
176
+ var name = getName ( node . key ) ;
177
+ if ( classInfo . usedStateFields . indexOf ( name ) < 0 ) {
178
+ context . report ( node , ' Unused state field: \'' + name + '\'' ) ;
179
179
}
180
- }
180
+ } ) ;
181
181
classInfo = null ;
182
182
} ,
183
183
184
- CallExpression ( node ) {
184
+ CallExpression : function ( node ) {
185
185
if ( ! classInfo ) {
186
186
return ;
187
187
}
@@ -198,7 +198,7 @@ module.exports = {
198
198
}
199
199
} ,
200
200
201
- ClassProperty ( node ) {
201
+ ClassProperty : function ( node ) {
202
202
if ( ! classInfo ) {
203
203
return ;
204
204
}
@@ -214,23 +214,23 @@ module.exports = {
214
214
}
215
215
} ,
216
216
217
- MethodDefinition ( ) {
217
+ MethodDefinition : function ( ) {
218
218
if ( ! classInfo ) {
219
219
return ;
220
220
}
221
221
// Create a new set for this.state aliases local to this method.
222
- classInfo . aliases = new Set ( ) ;
222
+ classInfo . aliases = [ ] ;
223
223
} ,
224
224
225
- 'MethodDefinition:exit' ( ) {
225
+ 'MethodDefinition:exit' : function ( ) {
226
226
if ( ! classInfo ) {
227
227
return ;
228
228
}
229
229
// Forget our set of local aliases.
230
230
classInfo . aliases = null ;
231
231
} ,
232
232
233
- AssignmentExpression ( node ) {
233
+ AssignmentExpression : function ( node ) {
234
234
if ( ! classInfo ) {
235
235
return ;
236
236
}
@@ -242,7 +242,7 @@ module.exports = {
242
242
node . right . type === 'ObjectExpression'
243
243
) {
244
244
// Find the nearest function expression containing this assignment.
245
- let fn = node ;
245
+ var fn = node ;
246
246
while ( fn . type !== 'FunctionExpression' && fn . parent ) {
247
247
fn = fn . parent ;
248
248
}
@@ -261,14 +261,14 @@ module.exports = {
261
261
}
262
262
} ,
263
263
264
- VariableDeclarator ( node ) {
264
+ VariableDeclarator : function ( node ) {
265
265
if ( ! classInfo || ! node . init ) {
266
266
return ;
267
267
}
268
268
handleAssignment ( node . id , node . init ) ;
269
269
} ,
270
270
271
- MemberExpression ( node ) {
271
+ MemberExpression : function ( node ) {
272
272
if ( ! classInfo ) {
273
273
return ;
274
274
}
@@ -283,13 +283,13 @@ module.exports = {
283
283
}
284
284
} ,
285
285
286
- JSXSpreadAttribute ( node ) {
286
+ JSXSpreadAttribute : function ( node ) {
287
287
if ( classInfo && isStateReference ( node . argument ) ) {
288
288
classInfo = null ;
289
289
}
290
290
} ,
291
291
292
- ExperimentalSpreadProperty ( node ) {
292
+ ExperimentalSpreadProperty : function ( node ) {
293
293
if ( classInfo && isStateReference ( node . argument ) ) {
294
294
classInfo = null ;
295
295
}
0 commit comments