@@ -18,6 +18,7 @@ const messages = {
18
18
useCallback : 'Use callback in setState when referencing the previous state.' ,
19
19
} ;
20
20
21
+ /** @type { import('eslint').Rule.RuleModule } */
21
22
module . exports = {
22
23
meta : {
23
24
docs : {
@@ -72,10 +73,10 @@ module.exports = {
72
73
// Appends all the methods that are calling another
73
74
// method containing this.state to the methods array
74
75
methods . forEach ( ( method ) => {
75
- if ( node . callee . name === method . methodName ) {
76
+ if ( node . callee . type === 'Identifier' && node . callee . name === method . methodName ) {
76
77
let current = node . parent ;
77
78
while ( current . type !== 'Program' ) {
78
- if ( current . type === 'MethodDefinition' ) {
79
+ if ( current . type === 'MethodDefinition' && current . key . type === 'PrivateIdentifier' ) {
79
80
methods . push ( {
80
81
methodName : current . key . name ,
81
82
node : method . node ,
@@ -91,7 +92,7 @@ module.exports = {
91
92
// to further check if they contains this.state
92
93
let current = node . parent ;
93
94
while ( current . type !== 'Program' ) {
94
- if ( isFirstArgumentInSetStateCall ( current , node ) ) {
95
+ if ( isFirstArgumentInSetStateCall ( current , node ) && node . callee . type === 'Identifier' ) {
95
96
const methodName = node . callee . name ;
96
97
methods . forEach ( ( method ) => {
97
98
if ( method . methodName === methodName ) {
@@ -109,10 +110,12 @@ module.exports = {
109
110
110
111
MemberExpression ( node ) {
111
112
if (
112
- node . property . name === 'state'
113
+ node . property . type === 'Identifier'
114
+ && node . property . name === 'state'
113
115
&& node . object . type === 'ThisExpression'
114
116
&& isClassComponent ( node )
115
117
) {
118
+ /** @type {import("eslint").Rule.Node } */
116
119
let current = node ;
117
120
while ( current . type !== 'Program' ) {
118
121
// Reporting if this.state is directly within this.setState
@@ -124,13 +127,18 @@ module.exports = {
124
127
}
125
128
126
129
// Storing all functions and methods that contains this.state
127
- if ( current . type === 'MethodDefinition' ) {
130
+ if ( current . type === 'MethodDefinition' && current . key . type === 'Identifier' ) {
128
131
methods . push ( {
129
132
methodName : current . key . name ,
130
133
node,
131
134
} ) ;
132
135
break ;
133
- } else if ( current . type === 'FunctionExpression' && current . parent . key ) {
136
+ } else if (
137
+ current . type === 'FunctionExpression'
138
+ && ( current . parent . type === 'Property' || current . parent . type === 'MethodDefinition' )
139
+ && current . parent . key . type === 'Identifier'
140
+ && current . parent . key
141
+ ) {
134
142
methods . push ( {
135
143
methodName : current . parent . key . name ,
136
144
node,
@@ -139,7 +147,7 @@ module.exports = {
139
147
}
140
148
141
149
// Storing all variables containing this.state
142
- if ( current . type === 'VariableDeclarator' ) {
150
+ if ( current . type === 'VariableDeclarator' && current . id . type === 'Identifier' ) {
143
151
vars . push ( {
144
152
node,
145
153
scope : getScope ( context , node ) ,
@@ -155,13 +163,14 @@ module.exports = {
155
163
156
164
Identifier ( node ) {
157
165
// Checks if the identifier is a variable within an object
166
+ /** @type {import("eslint").Rule.Node } */
158
167
let current = node ;
159
168
while ( current . parent . type === 'BinaryExpression' ) {
160
169
current = current . parent ;
161
170
}
162
171
if (
163
- current . parent . value === current
164
- || current . parent . object === current
172
+ ( current . parent . type === 'Property' && current . parent . value === current )
173
+ || ( current . parent . type === 'MemberExpression' && current . parent . object === current )
165
174
) {
166
175
while ( current . type !== 'Program' ) {
167
176
if ( isFirstArgumentInSetStateCall ( current , node ) ) {
@@ -179,9 +188,9 @@ module.exports = {
179
188
} ,
180
189
181
190
ObjectPattern ( node ) {
182
- const isDerivedFromThis = node . parent . init && node . parent . init . type === 'ThisExpression' ;
191
+ const isDerivedFromThis = ( node . parent . type === 'ForStatement' || node . parent . type === 'VariableDeclarator' ) && node . parent . init && node . parent . init . type === 'ThisExpression' ;
183
192
node . properties . forEach ( ( property ) => {
184
- if ( property && property . key && property . key . name === 'state' && isDerivedFromThis ) {
193
+ if ( property . type === 'Property' && property . key . type === 'Identifier' && property . key . name === 'state' && isDerivedFromThis ) {
185
194
vars . push ( {
186
195
node : property . key ,
187
196
scope : getScope ( context , node ) ,
0 commit comments