1 | /**
|
---|
2 | * @fileoverview Prevent usage of this.state within setState
|
---|
3 | * @author Rolf Erik Lekang, Jørgen Aaberg
|
---|
4 | */
|
---|
5 |
|
---|
6 | 'use strict';
|
---|
7 |
|
---|
8 | const docsUrl = require('../util/docsUrl');
|
---|
9 | const astUtil = require('../util/ast');
|
---|
10 | const componentUtil = require('../util/componentUtil');
|
---|
11 | const report = require('../util/report');
|
---|
12 | const getScope = require('../util/eslint').getScope;
|
---|
13 |
|
---|
14 | // ------------------------------------------------------------------------------
|
---|
15 | // Rule Definition
|
---|
16 | // ------------------------------------------------------------------------------
|
---|
17 |
|
---|
18 | const messages = {
|
---|
19 | useCallback: 'Use callback in setState when referencing the previous state.',
|
---|
20 | };
|
---|
21 |
|
---|
22 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
23 | module.exports = {
|
---|
24 | meta: {
|
---|
25 | docs: {
|
---|
26 | description: 'Disallow when this.state is accessed within setState',
|
---|
27 | category: 'Possible Errors',
|
---|
28 | recommended: false,
|
---|
29 | url: docsUrl('no-access-state-in-setstate'),
|
---|
30 | },
|
---|
31 |
|
---|
32 | messages,
|
---|
33 | },
|
---|
34 |
|
---|
35 | create(context) {
|
---|
36 | function isSetStateCall(node) {
|
---|
37 | return astUtil.isCallExpression(node)
|
---|
38 | && node.callee.property
|
---|
39 | && node.callee.property.name === 'setState'
|
---|
40 | && node.callee.object.type === 'ThisExpression';
|
---|
41 | }
|
---|
42 |
|
---|
43 | function isFirstArgumentInSetStateCall(current, node) {
|
---|
44 | if (!isSetStateCall(current)) {
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 | while (node && node.parent !== current) {
|
---|
48 | node = node.parent;
|
---|
49 | }
|
---|
50 | return current.arguments[0] === node;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * @param {ASTNode} node
|
---|
55 | * @returns {boolean}
|
---|
56 | */
|
---|
57 | function isClassComponent(node) {
|
---|
58 | return !!(
|
---|
59 | componentUtil.getParentES6Component(context, node)
|
---|
60 | || componentUtil.getParentES5Component(context, node)
|
---|
61 | );
|
---|
62 | }
|
---|
63 |
|
---|
64 | // The methods array contains all methods or functions that are using this.state
|
---|
65 | // or that are calling another method or function using this.state
|
---|
66 | const methods = [];
|
---|
67 | // The vars array contains all variables that contains this.state
|
---|
68 | const vars = [];
|
---|
69 | return {
|
---|
70 | CallExpression(node) {
|
---|
71 | if (!isClassComponent(node)) {
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | // Appends all the methods that are calling another
|
---|
75 | // method containing this.state to the methods array
|
---|
76 | methods.forEach((method) => {
|
---|
77 | if ('name' in node.callee && node.callee.name === method.methodName) {
|
---|
78 | let current = node.parent;
|
---|
79 | while (current.type !== 'Program') {
|
---|
80 | if (current.type === 'MethodDefinition') {
|
---|
81 | methods.push({
|
---|
82 | methodName: 'name' in current.key ? current.key.name : undefined,
|
---|
83 | node: method.node,
|
---|
84 | });
|
---|
85 | break;
|
---|
86 | }
|
---|
87 | current = current.parent;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | });
|
---|
91 |
|
---|
92 | // Finding all CallExpressions that is inside a setState
|
---|
93 | // to further check if they contains this.state
|
---|
94 | let current = node.parent;
|
---|
95 | while (current.type !== 'Program') {
|
---|
96 | if (isFirstArgumentInSetStateCall(current, node)) {
|
---|
97 | const methodName = 'name' in node.callee ? node.callee.name : undefined;
|
---|
98 | methods.forEach((method) => {
|
---|
99 | if (method.methodName === methodName) {
|
---|
100 | report(context, messages.useCallback, 'useCallback', {
|
---|
101 | node: method.node,
|
---|
102 | });
|
---|
103 | }
|
---|
104 | });
|
---|
105 |
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | current = current.parent;
|
---|
109 | }
|
---|
110 | },
|
---|
111 |
|
---|
112 | MemberExpression(node) {
|
---|
113 | if (
|
---|
114 | 'name' in node.property
|
---|
115 | && node.property.name === 'state'
|
---|
116 | && node.object.type === 'ThisExpression'
|
---|
117 | && isClassComponent(node)
|
---|
118 | ) {
|
---|
119 | /** @type {import("eslint").Rule.Node} */
|
---|
120 | let current = node;
|
---|
121 | while (current.type !== 'Program') {
|
---|
122 | // Reporting if this.state is directly within this.setState
|
---|
123 | if (isFirstArgumentInSetStateCall(current, node)) {
|
---|
124 | report(context, messages.useCallback, 'useCallback', {
|
---|
125 | node,
|
---|
126 | });
|
---|
127 | break;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Storing all functions and methods that contains this.state
|
---|
131 | if (current.type === 'MethodDefinition') {
|
---|
132 | methods.push({
|
---|
133 | methodName: 'name' in current.key ? current.key.name : undefined,
|
---|
134 | node,
|
---|
135 | });
|
---|
136 | break;
|
---|
137 | } else if (
|
---|
138 | current.type === 'FunctionExpression'
|
---|
139 | && 'key' in current.parent
|
---|
140 | && current.parent.key
|
---|
141 | ) {
|
---|
142 | methods.push({
|
---|
143 | methodName: 'name' in current.parent.key ? current.parent.key.name : undefined,
|
---|
144 | node,
|
---|
145 | });
|
---|
146 | break;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Storing all variables containing this.state
|
---|
150 | if (current.type === 'VariableDeclarator') {
|
---|
151 | vars.push({
|
---|
152 | node,
|
---|
153 | scope: getScope(context, node),
|
---|
154 | variableName: 'name' in current.id ? current.id.name : undefined,
|
---|
155 | });
|
---|
156 | break;
|
---|
157 | }
|
---|
158 |
|
---|
159 | current = current.parent;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | },
|
---|
163 |
|
---|
164 | Identifier(node) {
|
---|
165 | // Checks if the identifier is a variable within an object
|
---|
166 | /** @type {import("eslint").Rule.Node} */
|
---|
167 | let current = node;
|
---|
168 | while (current.parent.type === 'BinaryExpression') {
|
---|
169 | current = current.parent;
|
---|
170 | }
|
---|
171 | if (
|
---|
172 | ('value' in current.parent && current.parent.value === current)
|
---|
173 | || ('object' in current.parent && current.parent.object === current)
|
---|
174 | ) {
|
---|
175 | while (current.type !== 'Program') {
|
---|
176 | if (isFirstArgumentInSetStateCall(current, node)) {
|
---|
177 | vars
|
---|
178 | .filter((v) => v.scope === getScope(context, node) && v.variableName === node.name)
|
---|
179 | .forEach((v) => {
|
---|
180 | report(context, messages.useCallback, 'useCallback', {
|
---|
181 | node: v.node,
|
---|
182 | });
|
---|
183 | });
|
---|
184 | }
|
---|
185 | current = current.parent;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | },
|
---|
189 |
|
---|
190 | ObjectPattern(node) {
|
---|
191 | const isDerivedFromThis = 'init' in node.parent && node.parent.init && node.parent.init.type === 'ThisExpression';
|
---|
192 | node.properties.forEach((property) => {
|
---|
193 | if (
|
---|
194 | property
|
---|
195 | && 'key' in property
|
---|
196 | && property.key
|
---|
197 | && 'name' in property.key
|
---|
198 | && property.key.name === 'state'
|
---|
199 | && isDerivedFromThis
|
---|
200 | ) {
|
---|
201 | vars.push({
|
---|
202 | node: property.key,
|
---|
203 | scope: getScope(context, node),
|
---|
204 | variableName: property.key.name,
|
---|
205 | });
|
---|
206 | }
|
---|
207 | });
|
---|
208 | },
|
---|
209 | };
|
---|
210 | },
|
---|
211 | };
|
---|