Skip to content

Commit 9630957

Browse files
jzabalaljharb
authored andcommitted
[Fix] no-unused-state: check for class expression
Fixes #2251
1 parent cadee91 commit 9630957

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

lib/rules/no-unused-state.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,28 @@ module.exports = {
219219
}
220220
}
221221

222+
function handleES6ComponentEnter(node) {
223+
if (utils.isES6Component(node)) {
224+
classInfo = getInitialClassInfo();
225+
}
226+
}
227+
228+
function handleES6ComponentExit() {
229+
if (!classInfo) {
230+
return;
231+
}
232+
reportUnusedFields();
233+
classInfo = null;
234+
}
235+
222236
return {
223-
ClassDeclaration(node) {
224-
if (utils.isES6Component(node)) {
225-
classInfo = getInitialClassInfo();
226-
}
227-
},
237+
ClassDeclaration: handleES6ComponentEnter,
238+
239+
'ClassDeclaration:exit': handleES6ComponentExit,
240+
241+
ClassExpression: handleES6ComponentEnter,
242+
243+
'ClassExpression:exit': handleES6ComponentExit,
228244

229245
ObjectExpression(node) {
230246
if (utils.isES5Component(node)) {
@@ -243,14 +259,6 @@ module.exports = {
243259
}
244260
},
245261

246-
'ClassDeclaration:exit'() {
247-
if (!classInfo) {
248-
return;
249-
}
250-
reportUnusedFields();
251-
classInfo = null;
252-
},
253-
254262
CallExpression(node) {
255263
if (!classInfo) {
256264
return;

tests/lib/rules/no-unused-state.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,16 @@ eslintTester.run('no-unused-state', rule, {
11931193
`,
11941194
parser: parsers.BABEL_ESLINT,
11951195
errors: getErrorMessages(['initial'])
1196+
}, {
1197+
code: `
1198+
wrap(class NotWorking extends React.Component {
1199+
state = {
1200+
dummy: null
1201+
};
1202+
});
1203+
`,
1204+
parser: parsers.BABEL_ESLINT,
1205+
errors: getErrorMessages(['dummy'])
11961206
}, {
11971207
code: `
11981208
class Foo extends Component {

0 commit comments

Comments
 (0)