From 4ae1dab5e159f355564b3680b7efdb121e4b477e Mon Sep 17 00:00:00 2001 From: peakchen90 <1558267774@qq.com> Date: Mon, 7 Oct 2019 14:21:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20useState=20hook=20x-model=20=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=BB=91=E5=AE=9A=E5=88=B0state=E7=9A=84?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rules/no-unused-vars.js | 22 ++++++++++++++++------ tests/lib/rules/no-unused-vars.js | 8 ++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/rules/no-unused-vars.js b/lib/rules/no-unused-vars.js index 4b4c111..0d06a5b 100644 --- a/lib/rules/no-unused-vars.js +++ b/lib/rules/no-unused-vars.js @@ -19,12 +19,22 @@ module.exports = { function checkUseData(stateName, setStateName) { const references = context.getScope().references; - const valid = references.find(ref => ( - ref.identifier.name === stateName - && ref.identifier.parent.type === 'JSXExpressionContainer' - && ref.identifier.parent.parent.type === 'JSXAttribute' - && ref.identifier.parent.parent.name.name === `${prefix}-model` - )); + const valid = references.find(({ identifier }) => { + if (identifier.name !== stateName) { + return false; + } + + let parent = identifier.parent; + while (parent && parent.type === 'MemberExpression') { + parent = parent.parent; + } + + return parent + && parent.type === 'JSXExpressionContainer' + && parent.parent.type === 'JSXAttribute' + && parent.parent.name.name === `${prefix}-model`; + }); + if (valid) { context.markVariableAsUsed(setStateName); } diff --git a/tests/lib/rules/no-unused-vars.js b/tests/lib/rules/no-unused-vars.js index cca7777..48148d9 100644 --- a/tests/lib/rules/no-unused-vars.js +++ b/tests/lib/rules/no-unused-vars.js @@ -8,6 +8,14 @@ new RuleTester({ } }).run('no-unused-vars', rule, { valid: [ + { + code: ` + export default () => { + const [data, setData] = useState(0); + return + } + ` + }, { code: ` export default () => {