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 6, 2025
1 parent c7f5058 commit 7a25560
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
25 changes: 22 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,9 +28,27 @@ 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;
};

console.log(descriptor);

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;

if (setterName.startsWith('set') && setterName.substring(3) === getterName) {
return;
}
}
}
context.report(descriptor);
},
Expand Down
22 changes: 22 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,14 @@ ruleTester.run(
}`,
errors: 1,
},
{
code: `
import { useState } from 'react';
function useFoo() {
const getterAndSetter = useState();
}`,
errors: 1,
},
],
},
);

0 comments on commit 7a25560

Please sign in to comment.