Skip to content

Commit

Permalink
JS-20 - Fix FP S6754 (hook-use-state): Add exception when using `us…
Browse files Browse the repository at this point in the history
…eState` for components
  • Loading branch information
Eric Morand committed Jan 7, 2025
1 parent c7f5058 commit 0007168
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
3 changes: 0 additions & 3 deletions its/ruling/src/test/expected/jsts/eigen/typescript-S6754.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
17,
18
],
"eigen:src/app/Scenes/Inbox/Components/Conversations/CTAPopUp.tsx": [
5
],
"eigen:src/app/Scenes/MyCollection/Screens/ArtworkForm/Components/Rarity.tsx": [
13
],
Expand Down
28 changes: 25 additions & 3 deletions packages/jsts/src/rules/S6754/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import type { Rule } from 'eslint';
import { generateMeta, interceptReportForReact } from '../helpers/index.js';
import { meta } from './meta.js';
import { Node } from 'estree';

export function decorate(rule: Rule.RuleModule): Rule.RuleModule {
return interceptReportForReact(
Expand All @@ -27,10 +28,31 @@ export function decorate(rule: Rule.RuleModule): Rule.RuleModule {
meta: generateMeta(meta as Rule.RuleMetaData, rule.meta),
},
(context, descriptor) => {
const { node } = descriptor as any;
if (node.type === 'ArrayPattern' && node.elements.length === 1) {
return;
const { node } = descriptor as {
node: Node;
};

if (node.type === 'ArrayPattern') {
if (node.elements.length === 1) {
return;
}

const [getter, setter] = node.elements;

if (getter?.type === 'Identifier' && setter?.type === 'Identifier') {
const getterName = getter.name;
const setterName = setter.name;
const setterPrefix = 'set';

if (
setterName.startsWith(setterPrefix) &&
setterName.substring(setterPrefix.length) === getterName
) {
return;
}
}
}

context.report(descriptor);
},
);
Expand Down
30 changes: 30 additions & 0 deletions packages/jsts/src/rules/S6754/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ ruleTester.run(
return [foo];
}`,
},
{
code: `
import { useState } from 'react';
function useFoo() {
const [foo, setFoo] = useState();
}`,
},
{
code: `
import { useState } from 'react';
function useFoo() {
const [Foo, setFoo] = useState();
}`,
},
],
invalid: [
{
Expand All @@ -44,6 +58,22 @@ ruleTester.run(
}`,
errors: 1,
},
{
code: `import { useState } from 'react';
function foo() {
[client.searchState, client.setSearchState] = useState(baseState);
}`,
errors: 1,
},
{
code: `
import { useState } from 'react';
function useFoo() {
const getterAndSetter = useState();
}`,
errors: 1,
},
],
},
);

0 comments on commit 0007168

Please sign in to comment.