index.js
5.44 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _core = require("@babel/core");
var _default = (0, _helperPluginUtils.declare)((api, options) => {
api.assertVersion(7);
const {
allowMutablePropsOnTags
} = options;
if (allowMutablePropsOnTags != null && !Array.isArray(allowMutablePropsOnTags)) {
throw new Error(".allowMutablePropsOnTags must be an array, null, or undefined.");
}
const HOISTED = new WeakMap();
function declares(node, scope) {
if (_core.types.isJSXIdentifier(node, {
name: "this"
}) || _core.types.isJSXIdentifier(node, {
name: "arguments"
}) || _core.types.isJSXIdentifier(node, {
name: "super"
}) || _core.types.isJSXIdentifier(node, {
name: "new"
})) {
const {
path
} = scope;
return path.isFunctionParent() && !path.isArrowFunctionExpression();
}
return scope.hasOwnBinding(node.name);
}
function isHoistingScope({
path
}) {
return path.isFunctionParent() || path.isLoop() || path.isProgram();
}
function getHoistingScope(scope) {
while (!isHoistingScope(scope)) scope = scope.parent;
return scope;
}
const targetScopeVisitor = {
ReferencedIdentifier(path, state) {
const {
node
} = path;
let {
scope
} = path;
while (scope !== state.jsxScope) {
if (declares(node, scope)) return;
scope = scope.parent;
}
while (scope) {
if (scope === state.targetScope) return;
if (declares(node, scope)) break;
scope = scope.parent;
}
state.targetScope = getHoistingScope(scope);
}
};
const immutabilityVisitor = {
enter(path, state) {
const stop = () => {
state.isImmutable = false;
path.stop();
};
const skip = () => {
path.skip();
};
if (path.isJSXClosingElement()) return skip();
if (path.isJSXIdentifier({
name: "ref"
}) && path.parentPath.isJSXAttribute({
name: path.node
})) {
return stop();
}
if (path.isJSXIdentifier() || path.isJSXMemberExpression() || path.isJSXNamespacedName() || path.isImmutable()) {
return;
}
if (path.isIdentifier()) {
const binding = path.scope.getBinding(path.node.name);
if (binding && binding.constant) return;
}
const {
mutablePropsAllowed
} = state;
if (mutablePropsAllowed && path.isFunction()) {
path.traverse(targetScopeVisitor, state);
return skip();
}
if (!path.isPure()) return stop();
const expressionResult = path.evaluate();
if (expressionResult.confident) {
const {
value
} = expressionResult;
if (mutablePropsAllowed || value === null || typeof value !== "object" && typeof value !== "function") {
return skip();
}
} else if (_core.types.isIdentifier(expressionResult.deopt)) {
return;
}
stop();
}
};
const hoistingVisitor = Object.assign({}, immutabilityVisitor, targetScopeVisitor);
return {
name: "transform-react-constant-elements",
visitor: {
JSXElement(path) {
var _jsxScope;
if (HOISTED.has(path.node)) return;
const name = path.node.openingElement.name;
let mutablePropsAllowed = false;
if (allowMutablePropsOnTags != null) {
let lastSegment = name;
while (_core.types.isJSXMemberExpression(lastSegment)) {
lastSegment = lastSegment.property;
}
const elementName = lastSegment.name;
mutablePropsAllowed = allowMutablePropsOnTags.includes(elementName);
}
let jsxScope;
let current = path;
while (!jsxScope && current.parentPath.isJSX()) {
current = current.parentPath;
jsxScope = HOISTED.get(current.node);
}
(_jsxScope = jsxScope) != null ? _jsxScope : jsxScope = path.scope;
HOISTED.set(path.node, jsxScope);
const visitorState = {
isImmutable: true,
mutablePropsAllowed,
jsxScope,
targetScope: path.scope.getProgramParent()
};
path.traverse(hoistingVisitor, visitorState);
if (!visitorState.isImmutable) return;
const {
targetScope
} = visitorState;
for (let currentScope = jsxScope;;) {
if (targetScope === currentScope) return;
if (isHoistingScope(currentScope)) break;
currentScope = currentScope.parent;
if (!currentScope) {
throw new Error("Internal @babel/plugin-transform-react-constant-elements error: " + "targetScope must be an ancestor of jsxScope. " + "This is a Babel bug, please report it.");
}
}
const id = path.scope.generateUidBasedOnNode(name);
targetScope.push({
id: _core.types.identifier(id)
});
HOISTED.set(path.node, targetScope);
let replacement = _core.template.expression.ast`
${_core.types.identifier(id)} || (${_core.types.identifier(id)} = ${path.node})
`;
if (path.parentPath.isJSXElement() || path.parentPath.isJSXAttribute()) {
replacement = _core.types.jsxExpressionContainer(replacement);
}
path.replaceWith(replacement);
}
}
};
});
exports.default = _default;
//# sourceMappingURL=index.js.map