From 54f0f8490ce973c4687fd7c22f5bddf3ec791cf4 Mon Sep 17 00:00:00 2001 From: Alexandr Metreniuc Date: Tue, 4 Oct 2022 23:16:03 +0300 Subject: [PATCH] avoid confusion when setter is destructured accidentally --- lib/rules/hook-use-state.js | 6 +++--- tests/lib/rules/hook-use-state.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/rules/hook-use-state.js b/lib/rules/hook-use-state.js index 36559cf7b9..a9d3b7961c 100644 --- a/lib/rules/hook-use-state.js +++ b/lib/rules/hook-use-state.js @@ -75,9 +75,9 @@ module.exports = { const variableNodes = node.parent.id.elements; const valueVariable = variableNodes[0]; const setterVariable = variableNodes[1]; - const isValueDestructuring = isNodeDestructuring(valueVariable); + const isOnlyValueDestructuring = isNodeDestructuring(valueVariable) && !isNodeDestructuring(setterVariable); - if (allowDestructuredState && isValueDestructuring) { + if (allowDestructuredState && isOnlyValueDestructuring) { return; } @@ -168,7 +168,7 @@ module.exports = { }); } - if (isValueDestructuring) { + if (isOnlyValueDestructuring) { report( context, messages.useStateErrorMessageOrAddOption, diff --git a/tests/lib/rules/hook-use-state.js b/tests/lib/rules/hook-use-state.js index 6e2baa4bfa..1cadaf8a1a 100644 --- a/tests/lib/rules/hook-use-state.js +++ b/tests/lib/rules/hook-use-state.js @@ -538,6 +538,19 @@ const tests = { }, ], }, + { + code: ` + import { useState } from 'react'; + + const [{foo, bar, baz}, {setFooBarBaz}] = useState({foo: "bbb", bar: "aaa", baz: "qqq"}) + `, + options: [{ allowDestructuredState: true }], + errors: [ + { + message: 'useState call is not destructured into value + setter pair', + }, + ], + }, { code: ` import { useState } from 'react'