-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
hook-use-state.js
124 lines (106 loc) · 3.81 KB
/
hook-use-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @fileoverview Ensure symmetric naming of useState hook value and setter variables
* @author Duncan Beevers
*/
'use strict';
const docsUrl = require('../util/docsUrl');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const USE_STATE_ERROR_MESSAGE = 'useStateErrorMessage';
module.exports = {
meta: {
docs: {
description: 'Ensure symmetric naming of useState hook value and setter variables',
category: 'Best Practices',
recommended: false,
url: docsUrl('hook-use-state')
},
fixable: 'code',
messages: {
[USE_STATE_ERROR_MESSAGE]: 'setState call is not destructured into value + setter pair'
},
schema: [{
type: 'object',
additionalProperties: false
}]
},
create(context) {
let reactImportLocalName;
let reactUseStateLocalName;
return {
CallExpression(node) {
const isReactUseStateCall = (
reactImportLocalName
&& node.callee.type === 'MemberExpression'
&& node.callee.object.type === 'Identifier'
&& node.callee.object.name === reactImportLocalName
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'useState'
);
const isUseStateCall = (
reactUseStateLocalName
&& node.callee.type === 'Identifier'
&& node.callee.name === reactUseStateLocalName
);
// Ignore unless this is a useState() or React.useState() call.
if (!isReactUseStateCall && !isUseStateCall) {
return;
}
const isDestructuringDeclarator = (
node.parent.type === 'VariableDeclarator'
&& node.parent.id.type === 'ArrayPattern'
);
if (!isDestructuringDeclarator) {
context.report({node, messageId: USE_STATE_ERROR_MESSAGE});
return;
}
const variableNodes = node.parent.id.elements;
const valueVariable = variableNodes[0];
const setterVariable = variableNodes[1];
const valueVariableName = valueVariable
? valueVariable.name
: undefined;
const setterVariableName = setterVariable
? setterVariable.name
: undefined;
const expectedSetterVariableName = valueVariableName ? (
`set${
valueVariableName.charAt(0).toUpperCase()
}${valueVariableName.slice(1)}`
) : undefined;
if (
!valueVariable
|| !setterVariable
|| setterVariableName !== expectedSetterVariableName
|| variableNodes.length !== 2
) {
context.report({
node: node.parent.id,
messageId: USE_STATE_ERROR_MESSAGE,
fix: valueVariableName ? (fixer) => fixer.replaceTextRange(
[node.parent.id.range[0], node.parent.id.range[1]],
`[${valueVariableName}, ${expectedSetterVariableName}]`
) : undefined
});
}
},
ImportDeclaration(node) {
const isReactImported = node.source.type === 'Literal' && node.source.value === 'react';
const reactDefaultSpecifier = node.specifiers.find((specifier) => specifier.type === 'ImportDefaultSpecifier');
reactImportLocalName = reactDefaultSpecifier ? reactDefaultSpecifier.local.name : undefined;
const reactUseStateSpecifier = isReactImported
? node.specifiers.find(
(specifier) => (
specifier.type === 'ImportSpecifier'
&& specifier.imported.name === 'useState'
)
)
: undefined;
reactUseStateLocalName = reactUseStateSpecifier
? reactUseStateSpecifier.local.name
: undefined;
}
};
}
};