Skip to content

Commit 19ec70e

Browse files
committed
avoid confusion when setter is destructured accidentally
1 parent 7a2cffe commit 19ec70e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/rules/hook-use-state.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ module.exports = {
7575
const variableNodes = node.parent.id.elements;
7676
const valueVariable = variableNodes[0];
7777
const setterVariable = variableNodes[1];
78-
const isValueDestructuring = isNodeDestructuring(valueVariable);
78+
const isOnlyValueDestructuring = isNodeDestructuring(valueVariable) && !isNodeDestructuring(setterVariable);
7979

80-
if (allowDestructuredState && isValueDestructuring) {
80+
if (allowDestructuredState && isOnlyValueDestructuring) {
8181
return;
8282
}
8383

@@ -168,7 +168,7 @@ module.exports = {
168168
});
169169
}
170170

171-
if (isValueDestructuring) {
171+
if (isOnlyValueDestructuring) {
172172
report(
173173
context,
174174
messages.useStateErrorMessageOrAddOption,

tests/lib/rules/hook-use-state.js

+13
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,19 @@ const tests = {
538538
},
539539
],
540540
},
541+
{
542+
code: `
543+
import { useState } from 'react';
544+
545+
const [{foo, bar, baz}, {setFooBarBaz}] = useState({foo: "bbb", bar: "aaa", baz: "qqq"})
546+
`,
547+
options: [{ allowDestructuredState: true }],
548+
errors: [
549+
{
550+
message: 'useState call is not destructured into value + setter pair',
551+
},
552+
],
553+
},
541554
{
542555
code: `
543556
import { useState } from 'react'

0 commit comments

Comments
 (0)