forked from adobe/react-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure-render.js
189 lines (169 loc) · 5.54 KB
/
pure-render.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
// Copied from https://github.com/facebook/react/pull/24506
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforces component render purity',
recommended: true,
url: 'https://beta.reactjs.org/learn/keeping-components-pure'
}
},
create(context) {
return {
MemberExpression(member) {
// Look for member expressions that look like refs (i.e. `ref.current`).
if (
member.object.type !== 'Identifier' ||
member.computed ||
member.property.type !== 'Identifier' ||
member.property.name !== 'current'
) {
return;
}
// Find the parent function of this node, as well as any if statement matching against the ref value
// (i.e. lazy init pattern shown in React docs).
let node = member;
let fn;
let conditional;
while (node) {
if (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
) {
fn = node;
break;
}
if (
node.type === 'IfStatement' &&
node.test.type === 'BinaryExpression' &&
(node.test.operator === '==' || node.test.operator === '===') &&
isMemberExpressionEqual(node.test.left, member)
) {
conditional = node.test;
}
node = node.parent;
}
if (!fn) {
return;
}
// Find the variable definition for the object.
const variable = getVariable(context.getScope(), member.object.name);
if (!variable) {
return;
}
// Find the initialization of the variable and see if it's a call to useRef.
const refDefinition = variable.defs.find(def => {
const init = def.node.init;
if (!init) {
return false;
}
return (
init.type === 'CallExpression' &&
((init.callee.type === 'Identifier' &&
init.callee.name === 'useRef') ||
(init.callee.type === 'MemberExpression' &&
init.callee.object.type === 'Identifier' &&
init.callee.object.name === 'React' &&
init.callee.property.type === 'Identifier' &&
init.callee.property.name === 'useRef')) &&
parentFunction(def.node) === fn
);
});
if (refDefinition) {
// If within an if statement, check if comparing with the initial value passed to useRef.
// This indicates the lazy init pattern, which is allowed.
if (conditional) {
const init = refDefinition.node.init.arguments[0] || {
type: 'Identifier',
name: 'undefined'
};
if (isLiteralEqual(conditional.operator, init, conditional.right)) {
return;
}
}
// Otherwise, report an error for either writing or reading to this ref based on parent expression.
context.report({
node: member,
message:
member.parent.type === 'AssignmentExpression' &&
member.parent.left === member
? 'Writing to refs during rendering is not allowed. Move this into a useEffect or useLayoutEffect. See https://beta.reactjs.org/apis/useref'
: 'Reading from refs during rendering is not allowed. See https://beta.reactjs.org/apis/useref'
});
}
}
};
}
};
function getVariable(scope, name) {
while (scope) {
const variable = scope.set.get(name);
if (variable) {
return variable;
}
scope = scope.upper;
}
}
function parentFunction(node) {
while (node) {
if (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
) {
return node;
}
node = node.parent;
}
}
function isMemberExpressionEqual(a, b) {
if (a === b) {
return true;
}
return (
a.type === 'MemberExpression' &&
b.type === 'MemberExpression' &&
a.object.type === 'Identifier' &&
b.object.type === 'Identifier' &&
a.object.name === b.object.name &&
a.property.type === 'Identifier' &&
b.property.type === 'Identifier' &&
a.property.name === b.property.name
);
}
function isLiteralEqual(operator, a, b) {
let aValue, bValue;
if (a.type === 'Identifier' && a.name === 'undefined') {
aValue = undefined;
} else if (a.type === 'Literal') {
aValue = a.value;
} else {
return;
}
if (b.type === 'Identifier' && b.name === 'undefined') {
bValue = undefined;
} else if (b.type === 'Literal') {
bValue = b.value;
} else {
return;
}
if (operator === '===') {
return aValue === bValue;
} else if (operator === '==') {
// eslint-disable-next-line
return aValue == bValue;
}
return false;
}