From 1750e088d9d494f692a524f9b14060662db8512f Mon Sep 17 00:00:00 2001 From: Mohammed Essehemy Date: Thu, 11 Oct 2018 11:03:42 +0200 Subject: [PATCH] check used state when using ternary assignment --- lib/rules/no-unused-state.js | 5 +++++ tests/lib/rules/no-unused-state.js | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/rules/no-unused-state.js b/lib/rules/no-unused-state.js index d3c307db4e..d62dd96f2a 100644 --- a/lib/rules/no-unused-state.js +++ b/lib/rules/no-unused-state.js @@ -170,6 +170,11 @@ module.exports = { // Used to record used state fields and new aliases for both // AssignmentExpressions and VariableDeclarators. function handleAssignment(left, right) { + if (right.type === 'ConditionalExpression') { + handleAssignment(left, right.consequent); + handleAssignment(left, right.alternate); + return; + } switch (left.type) { case 'Identifier': if (isStateReference(right) && classInfo.aliases) { diff --git a/tests/lib/rules/no-unused-state.js b/tests/lib/rules/no-unused-state.js index 8261ae708c..d72cb4f721 100644 --- a/tests/lib/rules/no-unused-state.js +++ b/tests/lib/rules/no-unused-state.js @@ -257,6 +257,15 @@ eslintTester.run('no-unused-state', rule, { return ; } }`, + `class ShorthandDestructuringWithTernaryOperatorTest extends React.Component { + constructor() { + this.state = { foo: 0 }; + } + render() { + const {foo} = true ? this.state : {}; + return ; + } + }`, `class AliasDeclarationTest extends React.Component { constructor() { this.state = { foo: 0 };