Skip to content

Commit af6ccb0

Browse files
committed
Fix no-unused-state to detect usage of this.state as an object (fixes #1572)
1 parent c7dd755 commit af6ccb0

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/rules/no-unused-state.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ module.exports = {
352352
}
353353
// Otherwise, record that we saw this property being accessed.
354354
addUsedStateField(node.property);
355+
// If we see a `this.state` access in a CallExpression, give up.
356+
} else if (isStateReference(node) && node.parent.type === 'CallExpression') {
357+
classInfo = null;
355358
}
356359
},
357360

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,18 @@ eslintTester.run('no-unused-state', rule, {
479479
}
480480
}`,
481481
parser: 'babel-eslint'
482+
},
483+
{
484+
code: `class ThisStateAsAnObject extends React.Component {
485+
state = {
486+
active: true
487+
};
488+
489+
render() {
490+
return <div className={classNames('overflowEdgeIndicator', className, this.state)} />;
491+
}
492+
}`,
493+
parser: 'babel-eslint'
482494
}
483495
],
484496

0 commit comments

Comments
 (0)