File tree Expand file tree Collapse file tree 3 files changed +45
-5
lines changed Expand file tree Collapse file tree 3 files changed +45
-5
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
5
5
6
6
## Unreleased
7
7
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
+
8
13
## [ 7.29.4] - 2022.03.13
9
14
10
15
### Fixed
Original file line number Diff line number Diff line change @@ -66,23 +66,31 @@ module.exports = {
66
66
? setterVariable . name
67
67
: undefined ;
68
68
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
+ ] : [ ] ;
72
76
73
77
const isSymmetricGetterSetterPair = valueVariable
74
78
&& setterVariable
75
- && setterVariableName === expectedSetterVariableName
79
+ && expectedSetterVariableNames . indexOf ( setterVariableName ) !== - 1
76
80
&& variableNodes . length === 2 ;
77
81
78
82
if ( ! isSymmetricGetterSetterPair ) {
79
83
const suggestions = [
80
84
{
81
85
desc : 'Destructure useState call into value + setter pair' ,
82
86
fix : ( fixer ) => {
87
+ if ( expectedSetterVariableNames . length === 0 ) {
88
+ return ;
89
+ }
90
+
83
91
const fix = fixer . replaceTextRange (
84
92
node . parent . id . range ,
85
- `[${ valueVariableName } , ${ expectedSetterVariableName } ]`
93
+ `[${ valueVariableName } , ${ expectedSetterVariableNames [ 0 ] } ]`
86
94
) ;
87
95
88
96
return fix ;
Original file line number Diff line number Diff line change @@ -35,6 +35,33 @@ const tests = {
35
35
}
36
36
` ,
37
37
} ,
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
+ } ,
38
65
{
39
66
code : `
40
67
import { useState } from 'react'
You can’t perform that action at this time.
0 commit comments