forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-access-state-in-setstate.js
142 lines (130 loc) · 4.3 KB
/
no-access-state-in-setstate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @fileoverview Prevent usage of this.state within setState
* @author Rolf Erik Lekang, Jørgen Aaberg
*/
'use strict';
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Reports when this.state is accessed within setState',
category: 'Possible Errors',
recommended: false
}
},
create: function(context) {
function isSetStateCall(node) {
return node.type === 'CallExpression' &&
node.callee.property &&
node.callee.property.name === 'setState' &&
node.callee.object.type === 'ThisExpression';
}
// The methods array contains all methods or functions that are using this.state
// or that are calling another method or function using this.state
const methods = [];
// The vars array contains all variables that contains this.state
const vars = [];
return {
CallExpression(node) {
// Appends all the methods that are calling another
// method containg this.state to the methods array
methods.map(method => {
if (node.callee.name === method.methodName) {
let current = node.parent;
while (current.type !== 'Program') {
if (current.type === 'MethodDefinition') {
methods.push({
methodName: current.key.name,
node: method.node
});
break;
}
current = current.parent;
}
}
});
// Finding all CallExpressions that is inside a setState
// to further check if they contains this.state
let current = node.parent;
while (current.type !== 'Program') {
if (isSetStateCall(current)) {
const methodName = node.callee.name;
methods.map(method => {
if (method.methodName === methodName) {
context.report(
method.node,
'Use callback in setState when referencing the previous state.'
);
}
});
break;
}
current = current.parent;
}
},
MemberExpression(node) {
if (
node.property.name === 'state' &&
node.object.type === 'ThisExpression'
) {
let current = node;
while (current.type !== 'Program') {
// Reporting if this.state is directly within this.setState
if (isSetStateCall(current)) {
context.report(
node,
'Use callback in setState when referencing the previous state.'
);
break;
}
// Storing all functions and methods that contains this.state
if (current.type === 'MethodDefinition') {
methods.push({
methodName: current.key.name,
node: node
});
break;
} else if (current.type === 'FunctionExpression') {
methods.push({
methodName: current.parent.key.name,
node: node
});
break;
}
// Storing all variables containg this.state
if (current.type === 'VariableDeclarator') {
vars.push({
node: node,
scope: context.getScope()
});
break;
}
current = current.parent;
}
}
},
Identifier(node) {
// Checks if the identifier is a variable within an object
let current = node;
while (current.parent.type === 'BinaryExpression') {
current = current.parent;
}
if (current.parent.value === current) {
while (current.type !== 'Program') {
if (isSetStateCall(current)) {
vars
.filter(v => v.scope === context.getScope())
.map(v => context.report(
v.node,
'Use callback in setState when referencing the previous state.'
));
}
current = current.parent;
}
}
}
};
}
};