Skip to content

Commit e616d7f

Browse files
committed
Use original implementation
1 parent 872e394 commit e616d7f

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

lib/rules/no-unused-state.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function getInitialClassInfo() {
5454
// shadowing, assignments, etc. To keep things simple, we only
5555
// maintain one set of aliases per method and accept that it will
5656
// produce some false negatives.
57-
aliases: new Set()
57+
aliases: null
5858
};
5959
}
6060

@@ -86,6 +86,7 @@ module.exports = {
8686

8787
const isAliasedStateReference =
8888
node.type === 'Identifier' &&
89+
classInfo.aliases &&
8990
classInfo.aliases.has(node.name);
9091

9192
return isDirectStateReference || isAliasedStateReference;
@@ -124,7 +125,10 @@ module.exports = {
124125
for (const prop of node.properties) {
125126
if (prop.type === 'Property') {
126127
addUsedStateField(prop.key);
127-
} else if (prop.type === 'ExperimentalRestProperty') {
128+
} else if (
129+
prop.type === 'ExperimentalRestProperty' &&
130+
classInfo.aliases
131+
) {
128132
classInfo.aliases.add(getName(prop.argument));
129133
}
130134
}
@@ -135,14 +139,14 @@ module.exports = {
135139
function handleAssignment(left, right) {
136140
switch (left.type) {
137141
case 'Identifier':
138-
if (isStateReference(right)) {
142+
if (isStateReference(right) && classInfo.aliases) {
139143
classInfo.aliases.add(left.name);
140144
}
141145
break;
142146
case 'ObjectPattern':
143147
if (isStateReference(right)) {
144148
handleStateDestructuring(left);
145-
} else if (isThisExpression(right)) {
149+
} else if (isThisExpression(right) && classInfo.aliases) {
146150
for (const prop of left.properties) {
147151
if (prop.type === 'Property' && getName(prop.key) === 'state') {
148152
const name = getName(prop.value);
@@ -244,6 +248,18 @@ module.exports = {
244248
}
245249
},
246250

251+
'ClassProperty:exit'(node) {
252+
if (
253+
classInfo &&
254+
!node.static &&
255+
node.value &&
256+
node.value.type === 'ArrowFunctionExpression'
257+
) {
258+
// Forget our set of local aliases.
259+
classInfo.aliases = null;
260+
}
261+
},
262+
247263
MethodDefinition() {
248264
if (!classInfo) {
249265
return;
@@ -252,6 +268,14 @@ module.exports = {
252268
classInfo.aliases = new Set();
253269
},
254270

271+
'MethodDefinition:exit'() {
272+
if (!classInfo) {
273+
return;
274+
}
275+
// Forget our set of local aliases.
276+
classInfo.aliases = null;
277+
},
278+
255279
FunctionExpression(node) {
256280
if (!classInfo) {
257281
return;

0 commit comments

Comments
 (0)