From 96309574acac595a90eb2fcb25382032796857cb Mon Sep 17 00:00:00 2001 From: Johnny Zabala Date: Sun, 12 Jul 2020 21:52:17 -0400 Subject: [PATCH] [Fix] `no-unused-state`: check for class expression Fixes #2251 --- lib/rules/no-unused-state.js | 34 ++++++++++++++++++------------ tests/lib/rules/no-unused-state.js | 10 +++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/rules/no-unused-state.js b/lib/rules/no-unused-state.js index 2695f3661b..bee19d1b78 100644 --- a/lib/rules/no-unused-state.js +++ b/lib/rules/no-unused-state.js @@ -219,12 +219,28 @@ module.exports = { } } + function handleES6ComponentEnter(node) { + if (utils.isES6Component(node)) { + classInfo = getInitialClassInfo(); + } + } + + function handleES6ComponentExit() { + if (!classInfo) { + return; + } + reportUnusedFields(); + classInfo = null; + } + return { - ClassDeclaration(node) { - if (utils.isES6Component(node)) { - classInfo = getInitialClassInfo(); - } - }, + ClassDeclaration: handleES6ComponentEnter, + + 'ClassDeclaration:exit': handleES6ComponentExit, + + ClassExpression: handleES6ComponentEnter, + + 'ClassExpression:exit': handleES6ComponentExit, ObjectExpression(node) { if (utils.isES5Component(node)) { @@ -243,14 +259,6 @@ module.exports = { } }, - 'ClassDeclaration:exit'() { - if (!classInfo) { - return; - } - reportUnusedFields(); - classInfo = null; - }, - CallExpression(node) { if (!classInfo) { return; diff --git a/tests/lib/rules/no-unused-state.js b/tests/lib/rules/no-unused-state.js index eada128aa9..8007b0c02c 100644 --- a/tests/lib/rules/no-unused-state.js +++ b/tests/lib/rules/no-unused-state.js @@ -1193,6 +1193,16 @@ eslintTester.run('no-unused-state', rule, { `, parser: parsers.BABEL_ESLINT, errors: getErrorMessages(['initial']) + }, { + code: ` + wrap(class NotWorking extends React.Component { + state = { + dummy: null + }; + }); + `, + parser: parsers.BABEL_ESLINT, + errors: getErrorMessages(['dummy']) }, { code: ` class Foo extends Component {