-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathmatching-state-variable.ts
More file actions
142 lines (131 loc) · 4.58 KB
/
matching-state-variable.ts
File metadata and controls
142 lines (131 loc) · 4.58 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
AST_NODE_TYPES,
ESLintUtils,
type TSESTree,
} from '@typescript-eslint/utils';
import { isIdentifier } from '@typescript-eslint/utils/ast-utils';
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-matching-state-variable"
export const RULE_NAME = 'matching-state-variable';
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description:
'Ensure recoil value and setter are named after their atom name',
recommended: 'recommended',
},
fixable: 'code',
schema: [],
messages: {
invalidVariableName:
"Invalid usage of {{ hookName }}: the variable should be named '{{ expectedName }}' but found '{{ actualName }}'.",
invalidSetterName:
"Invalid usage of {{ hookName }}: Expected setter '{{ expectedName }}' but found '{{ actualName }}'.",
},
},
defaultOptions: [],
create: (context) => {
return {
VariableDeclarator: (node: TSESTree.VariableDeclarator) => {
if (
node?.init?.type === AST_NODE_TYPES.CallExpression &&
isIdentifier(node.init.callee) &&
[
'useRecoilState',
'useRecoilScopedState',
'useRecoilFamilyState',
'useRecoilScopedFamilyState',
'useRecoilValue',
'useRecoilScopedValue',
].includes(node.init.callee.name)
) {
const stateNameBase = isIdentifier(node.init.arguments[0])
? node.init.arguments[0].name
: undefined;
if (!stateNameBase) {
return;
}
const expectedVariableNameBase = stateNameBase.replace(
/(State|FamilyState|Selector|ScopedState|ScopedFamilyState|ScopedSelector)$/,
'',
);
if (isIdentifier(node.id)) {
const actualVariableName = node.id.name;
if (actualVariableName !== expectedVariableNameBase) {
context.report({
node,
messageId: 'invalidVariableName',
data: {
actualName: actualVariableName,
expectedName: expectedVariableNameBase,
hookName: stateNameBase,
callee: node.init.callee.name,
},
fix: (fixer) => {
return fixer.replaceText(node.id, expectedVariableNameBase);
},
});
}
return;
}
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
const actualVariableName =
node.id.elements?.[0]?.type === AST_NODE_TYPES.Identifier
? node.id.elements[0].name
: undefined;
if (
actualVariableName &&
actualVariableName !== expectedVariableNameBase
) {
context.report({
node,
messageId: 'invalidVariableName',
data: {
actual: actualVariableName,
expected: expectedVariableNameBase,
callee: node.init.callee.name,
},
fix: (fixer) => {
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
return fixer.replaceText(
node.id.elements[0] as TSESTree.Node,
expectedVariableNameBase,
);
}
return null;
},
});
}
if (isIdentifier(node.id.elements[1])) {
const actualSetterName = node.id.elements[1].name;
const expectedSetterName = `set${expectedVariableNameBase
.charAt(0)
.toUpperCase()}${expectedVariableNameBase.slice(1)}`;
if (actualSetterName !== expectedSetterName) {
context.report({
node,
messageId: 'invalidSetterName',
data: {
hookName: stateNameBase,
actualName: actualSetterName,
expectedName: expectedSetterName,
},
fix: (fixer) => {
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
return fixer.replaceText(
node.id.elements[1]!,
expectedSetterName,
);
}
return null;
},
});
}
}
}
}
},
};
},
});