Skip to content

Commit

Permalink
[Fix] no-unused-state: false negative on using state of non-lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot authored and ljharb committed May 12, 2019
1 parent fce69eb commit eb90ce6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module.exports = {
parent &&
parent.type === 'MethodDefinition' && (
parent.static && parent.key.name === 'getDerivedStateFromProps' ||
classMethods.indexOf(parent.key.name !== -1)
classMethods.indexOf(parent.key.name) !== -1
) &&
parent.value.type === 'FunctionExpression' &&
parent.value.params[1] &&
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,26 @@ eslintTester.run('no-unused-state', rule, {
errors: getErrorMessages(['foo']),
parser: parsers.BABEL_ESLINT
},
{
code: `class UseStateParameterOfNonLifecycleTest extends Component {
constructor(props) {
super(props);
this.state = {
foo: 123,
};
}
nonLifecycle(someProps, someState) {
doStuff(someState.foo)
}
render() {
return (
<SomeComponent />
);
}
}`,
errors: getErrorMessages(['foo']),
parser: parsers.BABEL_ESLINT
},
{
code: `class MissingStateParameterTest extends Component {
constructor(props) {
Expand Down

0 comments on commit eb90ce6

Please sign in to comment.