Skip to content

Commit cab6943

Browse files
duncanbeeversljharb
authored andcommitted
[Fix] hook-use-state: Allow UPPERCASE setState setter prefixes
1 parent 73ad445 commit cab6943

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Fixed
9+
* [`hook-use-state`]: Allow UPPERCASE setState setter prefixes ([#3244][] @duncanbeevers)
10+
11+
[#3244]: https://github.com/yannickcr/eslint-plugin-react/pull/3244
12+
813
## [7.29.4] - 2022.03.13
914

1015
### Fixed

lib/rules/hook-use-state.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,31 @@ module.exports = {
6666
? setterVariable.name
6767
: undefined;
6868

69-
const expectedSetterVariableName = valueVariableName ? (
70-
`set${valueVariableName.charAt(0).toUpperCase()}${valueVariableName.slice(1)}`
71-
) : undefined;
69+
const caseCandidateMatch = valueVariableName ? valueVariableName.match(/(^[a-z]+)(.*)/) : undefined;
70+
const upperCaseCandidatePrefix = caseCandidateMatch ? caseCandidateMatch[1] : undefined;
71+
const caseCandidateSuffix = caseCandidateMatch ? caseCandidateMatch[2] : undefined;
72+
const expectedSetterVariableNames = upperCaseCandidatePrefix ? [
73+
`set${upperCaseCandidatePrefix.charAt(0).toUpperCase()}${upperCaseCandidatePrefix.slice(1)}${caseCandidateSuffix}`,
74+
`set${upperCaseCandidatePrefix.toUpperCase()}${caseCandidateSuffix}`,
75+
] : [];
7276

7377
const isSymmetricGetterSetterPair = valueVariable
7478
&& setterVariable
75-
&& setterVariableName === expectedSetterVariableName
79+
&& expectedSetterVariableNames.indexOf(setterVariableName) !== -1
7680
&& variableNodes.length === 2;
7781

7882
if (!isSymmetricGetterSetterPair) {
7983
const suggestions = [
8084
{
8185
desc: 'Destructure useState call into value + setter pair',
8286
fix: (fixer) => {
87+
if (expectedSetterVariableNames.length === 0) {
88+
return;
89+
}
90+
8391
const fix = fixer.replaceTextRange(
8492
node.parent.id.range,
85-
`[${valueVariableName}, ${expectedSetterVariableName}]`
93+
`[${valueVariableName}, ${expectedSetterVariableNames[0]}]`
8694
);
8795

8896
return fix;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ const tests = {
3535
}
3636
`,
3737
},
38+
{
39+
code: `
40+
import { useState } from 'react'
41+
function useRGB() {
42+
const [rgb, setRGB] = useState()
43+
return [rgb, setRGB]
44+
}
45+
`,
46+
},
47+
{
48+
code: `
49+
import { useState } from 'react'
50+
function useRGBValue() {
51+
const [rgbValue, setRGBValue] = useState()
52+
return [rgbValue, setRGBValue]
53+
}
54+
`,
55+
},
56+
{
57+
code: `
58+
import { useState } from 'react'
59+
function useCustomColorValue() {
60+
const [customColorValue, setCustomColorValue] = useState()
61+
return [customColorValue, setCustomColorValue]
62+
}
63+
`,
64+
},
3865
{
3966
code: `
4067
import { useState } from 'react'

0 commit comments

Comments
 (0)