Skip to content

Commit 11d533f

Browse files
committed
fixup! [New] Symmetric useState hook variable names
1 parent 908e285 commit 11d533f

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

lib/rules/hook-use-state.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ module.exports = {
3232
},
3333

3434
create(context) {
35-
let isReactImported = false;
36-
let reactUseStateLocal;
35+
let reactImportLocalName;
36+
let reactUseStateLocalName;
3737

3838
return {
3939
CallExpression(node) {
4040
const isReactUseStateCall = (
41-
isReactImported
41+
reactImportLocalName
4242
&& node.callee.type === 'MemberExpression'
4343
&& node.callee.object.type === 'Identifier'
44-
&& node.callee.object.name === 'React'
44+
&& node.callee.object.name === reactImportLocalName
4545
&& node.callee.property.type === 'Identifier'
4646
&& node.callee.property.name === 'useState'
4747
);
4848

4949
const isUseStateCall = (
50-
reactUseStateLocal
50+
reactUseStateLocalName
5151
&& node.callee.type === 'Identifier'
52-
&& node.callee.name === reactUseStateLocal
52+
&& node.callee.name === reactUseStateLocalName
5353
);
5454

5555
// Ignore unless this is a useState() or React.useState() call.
@@ -102,7 +102,10 @@ module.exports = {
102102
}
103103
},
104104
ImportDeclaration(node) {
105-
isReactImported = node.source.type === 'Literal' && node.source.value === 'react';
105+
const isReactImported = node.source.type === 'Literal' && node.source.value === 'react';
106+
const reactDefaultSpecifier = node.specifiers.find((specifier) => specifier.type === 'ImportDefaultSpecifier');
107+
reactImportLocalName = reactDefaultSpecifier ? reactDefaultSpecifier.local.name : undefined;
108+
106109
const reactUseStateSpecifier = isReactImported
107110
? node.specifiers.find(
108111
(specifier) => (
@@ -112,7 +115,7 @@ module.exports = {
112115
)
113116
: undefined;
114117

115-
reactUseStateLocal = reactUseStateSpecifier
118+
reactUseStateLocalName = reactUseStateSpecifier
116119
? reactUseStateSpecifier.local.name
117120
: undefined;
118121
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,22 @@ const tests = {
9191
}]
9292
},
9393
{
94-
code: `import { useState } from 'react';
95-
const result = useState()`,
94+
code: `import React from 'react';
95+
const result = React.useState()`,
96+
errors: [{
97+
message: 'setState call is not destructured into value + setter pair'
98+
}]
99+
},
100+
{
101+
code: `import ReactAlternative from 'react';
102+
ReactAlternative.useState()`,
96103
errors: [{
97104
message: 'setState call is not destructured into value + setter pair'
98105
}]
99106
},
100107
{
101108
code: `import { useState } from 'react';
102-
const result = React.useState()`,
109+
const result = useState()`,
103110
errors: [{
104111
message: 'setState call is not destructured into value + setter pair'
105112
}]

0 commit comments

Comments
 (0)