Skip to content

Commit

Permalink
[Fix] no-unused-state: check for class expression
Browse files Browse the repository at this point in the history
Fixes #2251
  • Loading branch information
jzabala authored and ljharb committed Jul 13, 2020
1 parent cadee91 commit 9630957
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -243,14 +259,6 @@ module.exports = {
}
},

'ClassDeclaration:exit'() {
if (!classInfo) {
return;
}
reportUnusedFields();
classInfo = null;
},

CallExpression(node) {
if (!classInfo) {
return;
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9630957

Please sign in to comment.