/
index.js
42 lines (35 loc) 路 912 Bytes
/
index.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
export default function () {
let immutabilityVisitor = {
enter(path, state) {
let stop = () => {
state.isImmutable = false;
path.stop();
};
if (path.isJSXClosingElement()) {
path.skip();
return;
}
if (path.isJSXIdentifier({ name: "ref" }) && path.parentPath.isJSXAttribute({ name: path.node })) {
return stop();
}
if (path.isJSXIdentifier() || path.isIdentifier() || path.isJSXMemberExpression() || path.isPure()) {
return;
}
if (!path.isImmutable()) stop();
}
};
return {
visitor: {
JSXElement(path) {
if (path.node._hoisted) return;
let state = { isImmutable: true };
path.traverse(immutabilityVisitor, state);
if (state.isImmutable) {
path.hoist();
} else {
path.node._hoisted = true;
}
}
}
};
}