Skip to content

Commit

Permalink
avoid confusion when setter is destructured accidentally
Browse files Browse the repository at this point in the history
  • Loading branch information
metreniuk committed Oct 9, 2022
1 parent 9e743a2 commit 54f0f84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/rules/hook-use-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ module.exports = {
});
}

if (isValueDestructuring) {
if (isOnlyValueDestructuring) {
report(
context,
messages.useStateErrorMessageOrAddOption,
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/hook-use-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 54f0f84

Please sign in to comment.