Skip to content

Commit eb90ce6

Browse files
chiawendtljharb
authored andcommitted
[Fix] no-unused-state: false negative on using state of non-lifecycle
1 parent fce69eb commit eb90ce6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/rules/no-unused-state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = {
101101
parent &&
102102
parent.type === 'MethodDefinition' && (
103103
parent.static && parent.key.name === 'getDerivedStateFromProps' ||
104-
classMethods.indexOf(parent.key.name !== -1)
104+
classMethods.indexOf(parent.key.name) !== -1
105105
) &&
106106
parent.value.type === 'FunctionExpression' &&
107107
parent.value.params[1] &&

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,26 @@ eslintTester.run('no-unused-state', rule, {
10591059
errors: getErrorMessages(['foo']),
10601060
parser: parsers.BABEL_ESLINT
10611061
},
1062+
{
1063+
code: `class UseStateParameterOfNonLifecycleTest extends Component {
1064+
constructor(props) {
1065+
super(props);
1066+
this.state = {
1067+
foo: 123,
1068+
};
1069+
}
1070+
nonLifecycle(someProps, someState) {
1071+
doStuff(someState.foo)
1072+
}
1073+
render() {
1074+
return (
1075+
<SomeComponent />
1076+
);
1077+
}
1078+
}`,
1079+
errors: getErrorMessages(['foo']),
1080+
parser: parsers.BABEL_ESLINT
1081+
},
10621082
{
10631083
code: `class MissingStateParameterTest extends Component {
10641084
constructor(props) {

0 commit comments

Comments
 (0)