@babel/traverse 7.1.0 → 7.23.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/cache.js +27 -7
- package/lib/cache.js.map +1 -0
- package/lib/context.js +22 -59
- package/lib/context.js.map +1 -0
- package/lib/hub.js +2 -6
- package/lib/hub.js.map +1 -0
- package/lib/index.js +42 -77
- package/lib/index.js.map +1 -0
- package/lib/path/ancestry.js +15 -62
- package/lib/path/ancestry.js.map +1 -0
- package/lib/path/comments.js +31 -24
- package/lib/path/comments.js.map +1 -0
- package/lib/path/context.js +68 -91
- package/lib/path/context.js.map +1 -0
- package/lib/path/conversion.js +275 -273
- package/lib/path/conversion.js.map +1 -0
- package/lib/path/evaluation.js +84 -149
- package/lib/path/evaluation.js.map +1 -0
- package/lib/path/family.js +189 -94
- package/lib/path/family.js.map +1 -0
- package/lib/path/index.js +106 -132
- package/lib/path/index.js.map +1 -0
- package/lib/path/inference/index.js +81 -64
- package/lib/path/inference/index.js.map +1 -0
- package/lib/path/inference/inferer-reference.js +22 -52
- package/lib/path/inference/inferer-reference.js.map +1 -0
- package/lib/path/inference/inferers.js +110 -125
- package/lib/path/inference/inferers.js.map +1 -0
- package/lib/path/inference/util.js +30 -0
- package/lib/path/inference/util.js.map +1 -0
- package/lib/path/introspection.js +182 -168
- package/lib/path/introspection.js.map +1 -0
- package/lib/path/lib/hoister.js +37 -54
- package/lib/path/lib/hoister.js.map +1 -0
- package/lib/path/lib/removal-hooks.js +4 -4
- package/lib/path/lib/removal-hooks.js.map +1 -0
- package/lib/path/lib/virtual-types-validator.js +161 -0
- package/lib/path/lib/virtual-types-validator.js.map +1 -0
- package/lib/path/lib/virtual-types.js +21 -189
- package/lib/path/lib/virtual-types.js.map +1 -0
- package/lib/path/modification.js +103 -98
- package/lib/path/modification.js.map +1 -0
- package/lib/path/removal.js +18 -23
- package/lib/path/removal.js.map +1 -0
- package/lib/path/replacement.js +91 -144
- package/lib/path/replacement.js.map +1 -0
- package/lib/scope/binding.js +28 -16
- package/lib/scope/binding.js.map +1 -0
- package/lib/scope/index.js +407 -414
- package/lib/scope/index.js.map +1 -0
- package/lib/scope/lib/renamer.js +45 -70
- package/lib/scope/lib/renamer.js.map +1 -0
- package/lib/traverse-node.js +29 -0
- package/lib/traverse-node.js.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/visitors.js +77 -113
- package/lib/visitors.js.map +1 -0
- package/package.json +28 -17
package/lib/path/removal.js
CHANGED
@@ -3,46 +3,39 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.
|
7
|
-
exports._removeFromScope = _removeFromScope;
|
6
|
+
exports._assertUnremoved = _assertUnremoved;
|
8
7
|
exports._callRemovalHooks = _callRemovalHooks;
|
9
|
-
exports._remove = _remove;
|
10
8
|
exports._markRemoved = _markRemoved;
|
11
|
-
exports.
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
exports._remove = _remove;
|
10
|
+
exports._removeFromScope = _removeFromScope;
|
11
|
+
exports.remove = remove;
|
12
|
+
var _removalHooks = require("./lib/removal-hooks.js");
|
13
|
+
var _cache = require("../cache.js");
|
14
|
+
var _index = require("./index.js");
|
15
15
|
function remove() {
|
16
|
+
var _this$opts;
|
16
17
|
this._assertUnremoved();
|
17
|
-
|
18
18
|
this.resync();
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
if (!((_this$opts = this.opts) != null && _this$opts.noScope)) {
|
20
|
+
this._removeFromScope();
|
21
|
+
}
|
22
22
|
if (this._callRemovalHooks()) {
|
23
23
|
this._markRemoved();
|
24
|
-
|
25
24
|
return;
|
26
25
|
}
|
27
|
-
|
28
26
|
this.shareCommentsWithSiblings();
|
29
|
-
|
30
27
|
this._remove();
|
31
|
-
|
32
28
|
this._markRemoved();
|
33
29
|
}
|
34
|
-
|
35
30
|
function _removeFromScope() {
|
36
31
|
const bindings = this.getBindingIdentifiers();
|
37
32
|
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
|
38
33
|
}
|
39
|
-
|
40
34
|
function _callRemovalHooks() {
|
41
35
|
for (const fn of _removalHooks.hooks) {
|
42
36
|
if (fn(this, this.parentPath)) return true;
|
43
37
|
}
|
44
38
|
}
|
45
|
-
|
46
39
|
function _remove() {
|
47
40
|
if (Array.isArray(this.container)) {
|
48
41
|
this.container.splice(this.key, 1);
|
@@ -51,15 +44,17 @@ function _remove() {
|
|
51
44
|
this._replaceWith(null);
|
52
45
|
}
|
53
46
|
}
|
54
|
-
|
55
47
|
function _markRemoved() {
|
56
|
-
this.
|
57
|
-
this.
|
48
|
+
this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
|
49
|
+
if (this.parent) {
|
50
|
+
(0, _cache.getCachedPaths)(this.hub, this.parent).delete(this.node);
|
51
|
+
}
|
58
52
|
this.node = null;
|
59
53
|
}
|
60
|
-
|
61
54
|
function _assertUnremoved() {
|
62
55
|
if (this.removed) {
|
63
56
|
throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
|
64
57
|
}
|
65
|
-
}
|
58
|
+
}
|
59
|
+
|
60
|
+
//# sourceMappingURL=removal.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_removalHooks","require","_cache","_index","remove","_this$opts","_assertUnremoved","resync","opts","noScope","_removeFromScope","_callRemovalHooks","_markRemoved","shareCommentsWithSiblings","_remove","bindings","getBindingIdentifiers","Object","keys","forEach","name","scope","removeBinding","fn","hooks","parentPath","Array","isArray","container","splice","key","updateSiblingKeys","_replaceWith","_traverseFlags","SHOULD_SKIP","REMOVED","parent","getCachedPaths","hub","delete","node","removed","buildCodeFrameError"],"sources":["../../src/path/removal.ts"],"sourcesContent":["// This file contains methods responsible for removing a node.\n\nimport { hooks } from \"./lib/removal-hooks.ts\";\nimport { getCachedPaths } from \"../cache.ts\";\nimport type NodePath from \"./index.ts\";\nimport { REMOVED, SHOULD_SKIP } from \"./index.ts\";\n\nexport function remove(this: NodePath) {\n this._assertUnremoved();\n\n this.resync();\n if (!this.opts?.noScope) {\n this._removeFromScope();\n }\n\n if (this._callRemovalHooks()) {\n this._markRemoved();\n return;\n }\n\n this.shareCommentsWithSiblings();\n this._remove();\n this._markRemoved();\n}\n\nexport function _removeFromScope(this: NodePath) {\n const bindings = this.getBindingIdentifiers();\n Object.keys(bindings).forEach(name => this.scope.removeBinding(name));\n}\n\nexport function _callRemovalHooks(this: NodePath) {\n for (const fn of hooks) {\n if (fn(this, this.parentPath)) return true;\n }\n}\n\nexport function _remove(this: NodePath) {\n if (Array.isArray(this.container)) {\n this.container.splice(this.key as number, 1);\n this.updateSiblingKeys(this.key as number, -1);\n } else {\n this._replaceWith(null);\n }\n}\n\nexport function _markRemoved(this: NodePath) {\n // this.shouldSkip = true; this.removed = true;\n this._traverseFlags |= SHOULD_SKIP | REMOVED;\n if (this.parent) {\n getCachedPaths(this.hub, this.parent).delete(this.node);\n }\n this.node = null;\n}\n\nexport function _assertUnremoved(this: NodePath) {\n if (this.removed) {\n throw this.buildCodeFrameError(\n \"NodePath has been removed so is read-only.\",\n );\n }\n}\n"],"mappings":";;;;;;;;;;;AAEA,IAAAA,aAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAEA,IAAAE,MAAA,GAAAF,OAAA;AAEO,SAASG,MAAMA,CAAA,EAAiB;EAAA,IAAAC,UAAA;EACrC,IAAI,CAACC,gBAAgB,CAAC,CAAC;EAEvB,IAAI,CAACC,MAAM,CAAC,CAAC;EACb,IAAI,GAAAF,UAAA,GAAC,IAAI,CAACG,IAAI,aAATH,UAAA,CAAWI,OAAO,GAAE;IACvB,IAAI,CAACC,gBAAgB,CAAC,CAAC;EACzB;EAEA,IAAI,IAAI,CAACC,iBAAiB,CAAC,CAAC,EAAE;IAC5B,IAAI,CAACC,YAAY,CAAC,CAAC;IACnB;EACF;EAEA,IAAI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4CACF,CAAC;EACH;AACF"}
|
package/lib/path/replacement.js
CHANGED
@@ -3,107 +3,67 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.replaceWithMultiple = replaceWithMultiple;
|
7
|
-
exports.replaceWithSourceString = replaceWithSourceString;
|
8
|
-
exports.replaceWith = replaceWith;
|
9
6
|
exports._replaceWith = _replaceWith;
|
10
7
|
exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
|
11
8
|
exports.replaceInline = replaceInline;
|
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
|
-
return data;
|
45
|
-
}
|
46
|
-
|
47
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
48
|
-
|
49
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
50
|
-
|
51
|
-
const hoistVariablesVisitor = {
|
52
|
-
Function(path) {
|
53
|
-
path.skip();
|
54
|
-
},
|
55
|
-
|
56
|
-
VariableDeclaration(path) {
|
57
|
-
if (path.node.kind !== "var") return;
|
58
|
-
const bindings = path.getBindingIdentifiers();
|
59
|
-
|
60
|
-
for (const key in bindings) {
|
61
|
-
path.scope.push({
|
62
|
-
id: bindings[key]
|
63
|
-
});
|
64
|
-
}
|
65
|
-
|
66
|
-
const exprs = [];
|
67
|
-
|
68
|
-
for (const declar of path.node.declarations) {
|
69
|
-
if (declar.init) {
|
70
|
-
exprs.push(t().expressionStatement(t().assignmentExpression("=", declar.id, declar.init)));
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
path.replaceWithMultiple(exprs);
|
75
|
-
}
|
76
|
-
|
77
|
-
};
|
78
|
-
|
9
|
+
exports.replaceWith = replaceWith;
|
10
|
+
exports.replaceWithMultiple = replaceWithMultiple;
|
11
|
+
exports.replaceWithSourceString = replaceWithSourceString;
|
12
|
+
var _codeFrame = require("@babel/code-frame");
|
13
|
+
var _index = require("../index.js");
|
14
|
+
var _index2 = require("./index.js");
|
15
|
+
var _cache = require("../cache.js");
|
16
|
+
var _parser = require("@babel/parser");
|
17
|
+
var _t = require("@babel/types");
|
18
|
+
var _helperHoistVariables = require("@babel/helper-hoist-variables");
|
19
|
+
const {
|
20
|
+
FUNCTION_TYPES,
|
21
|
+
arrowFunctionExpression,
|
22
|
+
assignmentExpression,
|
23
|
+
awaitExpression,
|
24
|
+
blockStatement,
|
25
|
+
callExpression,
|
26
|
+
cloneNode,
|
27
|
+
expressionStatement,
|
28
|
+
identifier,
|
29
|
+
inheritLeadingComments,
|
30
|
+
inheritTrailingComments,
|
31
|
+
inheritsComments,
|
32
|
+
isExpression,
|
33
|
+
isProgram,
|
34
|
+
isStatement,
|
35
|
+
removeComments,
|
36
|
+
returnStatement,
|
37
|
+
toSequenceExpression,
|
38
|
+
validate,
|
39
|
+
yieldExpression
|
40
|
+
} = _t;
|
79
41
|
function replaceWithMultiple(nodes) {
|
42
|
+
var _getCachedPaths;
|
80
43
|
this.resync();
|
81
44
|
nodes = this._verifyNodeList(nodes);
|
82
|
-
|
83
|
-
|
45
|
+
inheritLeadingComments(nodes[0], this.node);
|
46
|
+
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
47
|
+
(_getCachedPaths = (0, _cache.getCachedPaths)(this.hub, this.parent)) == null ? void 0 : _getCachedPaths.delete(this.node);
|
84
48
|
this.node = this.container[this.key] = null;
|
85
49
|
const paths = this.insertAfter(nodes);
|
86
|
-
|
87
50
|
if (this.node) {
|
88
51
|
this.requeue();
|
89
52
|
} else {
|
90
53
|
this.remove();
|
91
54
|
}
|
92
|
-
|
93
55
|
return paths;
|
94
56
|
}
|
95
|
-
|
96
57
|
function replaceWithSourceString(replacement) {
|
97
58
|
this.resync();
|
98
|
-
|
59
|
+
let ast;
|
99
60
|
try {
|
100
61
|
replacement = `(${replacement})`;
|
101
|
-
|
62
|
+
ast = (0, _parser.parse)(replacement);
|
102
63
|
} catch (err) {
|
103
64
|
const loc = err.loc;
|
104
|
-
|
105
65
|
if (loc) {
|
106
|
-
err.message += " - make sure this is an expression.\n" + (0, _codeFrame
|
66
|
+
err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
|
107
67
|
start: {
|
108
68
|
line: loc.line,
|
109
69
|
column: loc.column + 1
|
@@ -111,142 +71,127 @@ function replaceWithSourceString(replacement) {
|
|
111
71
|
});
|
112
72
|
err.code = "BABEL_REPLACE_SOURCE_ERROR";
|
113
73
|
}
|
114
|
-
|
115
74
|
throw err;
|
116
75
|
}
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
_index.default.removeProperties(replacement);
|
121
|
-
|
122
|
-
return this.replaceWith(replacement);
|
76
|
+
const expressionAST = ast.program.body[0].expression;
|
77
|
+
_index.default.removeProperties(expressionAST);
|
78
|
+
return this.replaceWith(expressionAST);
|
123
79
|
}
|
124
|
-
|
125
|
-
function replaceWith(replacement) {
|
80
|
+
function replaceWith(replacementPath) {
|
126
81
|
this.resync();
|
127
|
-
|
128
82
|
if (this.removed) {
|
129
83
|
throw new Error("You can't replace this node, we've already removed it");
|
130
84
|
}
|
131
|
-
|
132
|
-
if (replacement instanceof _index2.default) {
|
133
|
-
replacement = replacement.node;
|
134
|
-
}
|
135
|
-
|
85
|
+
let replacement = replacementPath instanceof _index2.default ? replacementPath.node : replacementPath;
|
136
86
|
if (!replacement) {
|
137
87
|
throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
|
138
88
|
}
|
139
|
-
|
140
89
|
if (this.node === replacement) {
|
141
90
|
return [this];
|
142
91
|
}
|
143
|
-
|
144
|
-
if (this.isProgram() && !t().isProgram(replacement)) {
|
92
|
+
if (this.isProgram() && !isProgram(replacement)) {
|
145
93
|
throw new Error("You can only replace a Program root node with another Program node");
|
146
94
|
}
|
147
|
-
|
148
95
|
if (Array.isArray(replacement)) {
|
149
96
|
throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
|
150
97
|
}
|
151
|
-
|
152
98
|
if (typeof replacement === "string") {
|
153
99
|
throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
|
154
100
|
}
|
155
|
-
|
156
101
|
let nodePath = "";
|
157
|
-
|
158
|
-
if (this.isNodeType("Statement") && t().isExpression(replacement)) {
|
102
|
+
if (this.isNodeType("Statement") && isExpression(replacement)) {
|
159
103
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
|
160
|
-
replacement =
|
104
|
+
replacement = expressionStatement(replacement);
|
161
105
|
nodePath = "expression";
|
162
106
|
}
|
163
107
|
}
|
164
|
-
|
165
|
-
if (this.isNodeType("Expression") && t().isStatement(replacement)) {
|
108
|
+
if (this.isNodeType("Expression") && isStatement(replacement)) {
|
166
109
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
|
167
110
|
return this.replaceExpressionWithStatements([replacement]);
|
168
111
|
}
|
169
112
|
}
|
170
|
-
|
171
113
|
const oldNode = this.node;
|
172
|
-
|
173
114
|
if (oldNode) {
|
174
|
-
|
175
|
-
|
115
|
+
inheritsComments(replacement, oldNode);
|
116
|
+
removeComments(oldNode);
|
176
117
|
}
|
177
|
-
|
178
118
|
this._replaceWith(replacement);
|
179
|
-
|
180
119
|
this.type = replacement.type;
|
181
120
|
this.setScope();
|
182
121
|
this.requeue();
|
183
122
|
return [nodePath ? this.get(nodePath) : this];
|
184
123
|
}
|
185
|
-
|
186
124
|
function _replaceWith(node) {
|
125
|
+
var _getCachedPaths2;
|
187
126
|
if (!this.container) {
|
188
127
|
throw new ReferenceError("Container is falsy");
|
189
128
|
}
|
190
|
-
|
191
129
|
if (this.inList) {
|
192
|
-
|
130
|
+
validate(this.parent, this.key, [node]);
|
193
131
|
} else {
|
194
|
-
|
132
|
+
validate(this.parent, this.key, node);
|
195
133
|
}
|
196
|
-
|
197
|
-
this.
|
134
|
+
this.debug(`Replace with ${node == null ? void 0 : node.type}`);
|
135
|
+
(_getCachedPaths2 = (0, _cache.getCachedPaths)(this.hub, this.parent)) == null ? void 0 : _getCachedPaths2.set(node, this).delete(this.node);
|
198
136
|
this.node = this.container[this.key] = node;
|
199
137
|
}
|
200
|
-
|
201
138
|
function replaceExpressionWithStatements(nodes) {
|
202
139
|
this.resync();
|
203
|
-
const
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
const
|
210
|
-
|
211
|
-
this.
|
140
|
+
const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
|
141
|
+
if (nodesAsSequenceExpression) {
|
142
|
+
return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
|
143
|
+
}
|
144
|
+
const functionParent = this.getFunctionParent();
|
145
|
+
const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
|
146
|
+
const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
|
147
|
+
const container = arrowFunctionExpression([], blockStatement(nodes));
|
148
|
+
this.replaceWith(callExpression(container, []));
|
149
|
+
const callee = this.get("callee");
|
150
|
+
(0, _helperHoistVariables.default)(callee.get("body"), id => {
|
151
|
+
this.scope.push({
|
152
|
+
id
|
153
|
+
});
|
154
|
+
}, "var");
|
212
155
|
const completionRecords = this.get("callee").getCompletionRecords();
|
213
|
-
|
214
156
|
for (const path of completionRecords) {
|
215
157
|
if (!path.isExpressionStatement()) continue;
|
216
158
|
const loop = path.findParent(path => path.isLoop());
|
217
|
-
|
218
159
|
if (loop) {
|
219
160
|
let uid = loop.getData("expressionReplacementReturnUid");
|
220
|
-
|
221
161
|
if (!uid) {
|
222
|
-
const callee = this.get("callee");
|
223
162
|
uid = callee.scope.generateDeclaredUidIdentifier("ret");
|
224
|
-
callee.get("body").pushContainer("body",
|
163
|
+
callee.get("body").pushContainer("body", returnStatement(cloneNode(uid)));
|
225
164
|
loop.setData("expressionReplacementReturnUid", uid);
|
226
165
|
} else {
|
227
|
-
uid =
|
166
|
+
uid = identifier(uid.name);
|
228
167
|
}
|
229
|
-
|
230
|
-
path.get("expression").replaceWith(t().assignmentExpression("=", t().cloneNode(uid), path.node.expression));
|
168
|
+
path.get("expression").replaceWith(assignmentExpression("=", cloneNode(uid), path.node.expression));
|
231
169
|
} else {
|
232
|
-
path.replaceWith(
|
170
|
+
path.replaceWith(returnStatement(path.node.expression));
|
233
171
|
}
|
234
172
|
}
|
235
|
-
|
236
|
-
const callee = this.get("callee");
|
237
173
|
callee.arrowFunctionToExpression();
|
238
|
-
|
174
|
+
const newCallee = callee;
|
175
|
+
const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
|
176
|
+
const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
|
177
|
+
if (needToAwaitFunction) {
|
178
|
+
newCallee.set("async", true);
|
179
|
+
if (!needToYieldFunction) {
|
180
|
+
this.replaceWith(awaitExpression(this.node));
|
181
|
+
}
|
182
|
+
}
|
183
|
+
if (needToYieldFunction) {
|
184
|
+
newCallee.set("generator", true);
|
185
|
+
this.replaceWith(yieldExpression(this.node, true));
|
186
|
+
}
|
187
|
+
return newCallee.get("body.body");
|
239
188
|
}
|
240
|
-
|
241
189
|
function replaceInline(nodes) {
|
242
190
|
this.resync();
|
243
|
-
|
244
191
|
if (Array.isArray(nodes)) {
|
245
192
|
if (Array.isArray(this.container)) {
|
246
193
|
nodes = this._verifyNodeList(nodes);
|
247
|
-
|
248
194
|
const paths = this._containerInsertAfter(nodes);
|
249
|
-
|
250
195
|
this.remove();
|
251
196
|
return paths;
|
252
197
|
} else {
|
@@ -255,4 +200,6 @@ function replaceInline(nodes) {
|
|
255
200
|
} else {
|
256
201
|
return this.replaceWith(nodes);
|
257
202
|
}
|
258
|
-
}
|
203
|
+
}
|
204
|
+
|
205
|
+
//# sourceMappingURL=replacement.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_codeFrame","require","_index","_index2","_cache","_parser","_t","_helperHoistVariables","FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","callExpression","cloneNode","expressionStatement","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isExpression","isProgram","isStatement","removeComments","returnStatement","toSequenceExpression","validate","yieldExpression","replaceWithMultiple","nodes","_getCachedPaths","resync","_verifyNodeList","node","length","getCachedPaths","hub","parent","delete","container","key","paths","insertAfter","requeue","remove","replaceWithSourceString","replacement","ast","parse","err","loc","message","codeFrameColumns","start","line","column","code","expressionAST","program","body","expression","traverse","removeProperties","replaceWith","replacementPath","removed","Error","NodePath","Array","isArray","nodePath","isNodeType","canHaveVariableDeclarationOrExpression","canSwapBetweenExpressionAndStatement","parentPath","isExportDefaultDeclaration","replaceExpressionWithStatements","oldNode","_replaceWith","type","setScope","get","_getCachedPaths2","ReferenceError","inList","debug","set","nodesAsSequenceExpression","scope","functionParent","getFunctionParent","isParentAsync","is","isParentGenerator","callee","hoistVariables","id","push","completionRecords","getCompletionRecords","path","isExpressionStatement","loop","findParent","isLoop","uid","getData","generateDeclaredUidIdentifier","pushContainer","setData","name","arrowFunctionToExpression","newCallee","needToAwaitFunction","hasType","needToYieldFunction","replaceInline","_containerInsertAfter"],"sources":["../../src/path/replacement.ts"],"sourcesContent":["// This file contains methods responsible for replacing a node with another.\n\nimport { codeFrameColumns } from \"@babel/code-frame\";\nimport traverse from \"../index.ts\";\nimport NodePath from \"./index.ts\";\nimport { getCachedPaths } from \"../cache.ts\";\nimport { parse } from \"@babel/parser\";\nimport {\n FUNCTION_TYPES,\n arrowFunctionExpression,\n assignmentExpression,\n awaitExpression,\n blockStatement,\n callExpression,\n cloneNode,\n expressionStatement,\n identifier,\n inheritLeadingComments,\n inheritTrailingComments,\n inheritsComments,\n isExpression,\n isProgram,\n isStatement,\n removeComments,\n returnStatement,\n toSequenceExpression,\n validate,\n yieldExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport hoistVariables from \"@babel/helper-hoist-variables\";\n\n/**\n * Replace a node with an array of multiple. This method performs the following steps:\n *\n * - Inherit the comments of first provided node with that of the current node.\n * - Insert the provided nodes after the current node.\n * - Remove the current node.\n */\n\nexport function replaceWithMultiple(\n this: NodePath,\n nodes: t.Node | t.Node[],\n): NodePath[] {\n this.resync();\n\n nodes = this._verifyNodeList(nodes);\n inheritLeadingComments(nodes[0], this.node);\n inheritTrailingComments(nodes[nodes.length - 1], this.node);\n getCachedPaths(this.hub, this.parent)?.delete(this.node);\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = null;\n const paths = this.insertAfter(nodes);\n\n if (this.node) {\n this.requeue();\n } else {\n this.remove();\n }\n return paths;\n}\n\n/**\n * Parse a string as an expression and replace the current node with the result.\n *\n * NOTE: This is typically not a good idea to use. Building source strings when\n * transforming ASTs is an antipattern and SHOULD NOT be encouraged. Even if it's\n * easier to use, your transforms will be extremely brittle.\n */\n\nexport function replaceWithSourceString(this: NodePath, replacement: string) {\n this.resync();\n let ast: t.File;\n\n try {\n replacement = `(${replacement})`;\n // @ts-expect-error todo: use babel-types ast typings in Babel parser\n ast = parse(replacement);\n } catch (err) {\n const loc = err.loc;\n if (loc) {\n err.message +=\n \" - make sure this is an expression.\\n\" +\n codeFrameColumns(replacement, {\n start: {\n line: loc.line,\n column: loc.column + 1,\n },\n });\n err.code = \"BABEL_REPLACE_SOURCE_ERROR\";\n }\n throw err;\n }\n\n const expressionAST = (ast.program.body[0] as t.ExpressionStatement)\n .expression;\n traverse.removeProperties(expressionAST);\n return this.replaceWith(expressionAST);\n}\n\n/**\n * Replace the current node with another.\n */\n\nexport function replaceWith<R extends t.Node>(\n this: NodePath,\n replacementPath: R | NodePath<R>,\n): [NodePath<R>] {\n this.resync();\n\n if (this.removed) {\n throw new Error(\"You can't replace this node, we've already removed it\");\n }\n\n let replacement: t.Node =\n replacementPath instanceof NodePath\n ? replacementPath.node\n : replacementPath;\n\n if (!replacement) {\n throw new Error(\n \"You passed `path.replaceWith()` a falsy node, use `path.remove()` instead\",\n );\n }\n\n if (this.node === replacement) {\n return [this as NodePath<R>];\n }\n\n if (this.isProgram() && !isProgram(replacement)) {\n throw new Error(\n \"You can only replace a Program root node with another Program node\",\n );\n }\n\n if (Array.isArray(replacement)) {\n throw new Error(\n \"Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`\",\n );\n }\n\n if (typeof replacement === \"string\") {\n throw new Error(\n \"Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`\",\n );\n }\n\n let nodePath = \"\";\n\n if (this.isNodeType(\"Statement\") && isExpression(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement) &&\n !this.parentPath.isExportDefaultDeclaration()\n ) {\n // replacing a statement with an expression so wrap it in an expression statement\n replacement = expressionStatement(replacement);\n nodePath = \"expression\";\n }\n }\n\n if (this.isNodeType(\"Expression\") && isStatement(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement)\n ) {\n // replacing an expression with a statement so let's explode it\n return this.replaceExpressionWithStatements([replacement]) as [\n NodePath<R>,\n ];\n }\n }\n\n const oldNode = this.node;\n if (oldNode) {\n inheritsComments(replacement, oldNode);\n removeComments(oldNode);\n }\n\n // replace the node\n this._replaceWith(replacement);\n this.type = replacement.type;\n\n // potentially create new scope\n this.setScope();\n\n // requeue for visiting\n this.requeue();\n\n return [\n nodePath ? (this.get(nodePath) as NodePath<R>) : (this as NodePath<R>),\n ];\n}\n\n/**\n * Description\n */\n\nexport function _replaceWith(this: NodePath, node: t.Node) {\n if (!this.container) {\n throw new ReferenceError(\"Container is falsy\");\n }\n\n if (this.inList) {\n // @ts-expect-error todo(flow->ts): check if validate accepts a numeric key\n validate(this.parent, this.key, [node]);\n } else {\n validate(this.parent, this.key as string, node);\n }\n\n this.debug(`Replace with ${node?.type}`);\n getCachedPaths(this.hub, this.parent)?.set(node, this).delete(this.node);\n\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = node;\n}\n\n/**\n * This method takes an array of statements nodes and then explodes it\n * into expressions. This method retains completion records which is\n * extremely important to retain original semantics.\n */\n\nexport function replaceExpressionWithStatements(\n this: NodePath,\n nodes: Array<t.Statement>,\n) {\n this.resync();\n\n const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);\n\n if (nodesAsSequenceExpression) {\n return this.replaceWith(nodesAsSequenceExpression)[0].get(\"expressions\");\n }\n\n const functionParent = this.getFunctionParent();\n const isParentAsync = functionParent?.is(\"async\");\n const isParentGenerator = functionParent?.is(\"generator\");\n\n const container = arrowFunctionExpression([], blockStatement(nodes));\n\n this.replaceWith(callExpression(container, []));\n // replaceWith changes the type of \"this\", but it isn't trackable by TS\n type ThisType = NodePath<\n t.CallExpression & {\n callee: t.ArrowFunctionExpression & { body: t.BlockStatement };\n }\n >;\n\n // hoist variable declaration in do block\n // `(do { var x = 1; x;})` -> `var x; (() => { x = 1; return x; })()`\n const callee = (this as ThisType).get(\"callee\");\n hoistVariables(\n callee.get(\"body\"),\n (id: t.Identifier) => {\n this.scope.push({ id });\n },\n \"var\",\n );\n\n // add implicit returns to all ending expression statements\n const completionRecords: Array<NodePath> = (this as ThisType)\n .get(\"callee\")\n .getCompletionRecords();\n for (const path of completionRecords) {\n if (!path.isExpressionStatement()) continue;\n\n const loop = path.findParent(path => path.isLoop());\n if (loop) {\n let uid = loop.getData(\"expressionReplacementReturnUid\");\n\n if (!uid) {\n uid = callee.scope.generateDeclaredUidIdentifier(\"ret\");\n callee\n .get(\"body\")\n .pushContainer(\"body\", returnStatement(cloneNode(uid)));\n loop.setData(\"expressionReplacementReturnUid\", uid);\n } else {\n uid = identifier(uid.name);\n }\n\n path\n .get(\"expression\")\n .replaceWith(\n assignmentExpression(\"=\", cloneNode(uid), path.node.expression),\n );\n } else {\n path.replaceWith(returnStatement(path.node.expression));\n }\n }\n\n // This is an IIFE, so we don't need to worry about the noNewArrows assumption\n callee.arrowFunctionToExpression();\n // Fixme: we can not `assert this is NodePath<t.FunctionExpression>` in `arrowFunctionToExpression`\n // because it is not a class method known at compile time.\n const newCallee = callee as unknown as NodePath<t.FunctionExpression>;\n\n // (() => await xxx)() -> await (async () => await xxx)();\n const needToAwaitFunction =\n isParentAsync &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"AwaitExpression\",\n FUNCTION_TYPES,\n );\n const needToYieldFunction =\n isParentGenerator &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"YieldExpression\",\n FUNCTION_TYPES,\n );\n if (needToAwaitFunction) {\n newCallee.set(\"async\", true);\n // yield* will await the generator return result\n if (!needToYieldFunction) {\n this.replaceWith(awaitExpression((this as ThisType).node));\n }\n }\n if (needToYieldFunction) {\n newCallee.set(\"generator\", true);\n this.replaceWith(yieldExpression((this as ThisType).node, true));\n }\n\n return newCallee.get(\"body.body\");\n}\n\nexport function replaceInline(this: NodePath, nodes: t.Node | Array<t.Node>) {\n this.resync();\n\n if (Array.isArray(nodes)) {\n if (Array.isArray(this.container)) {\n nodes = this._verifyNodeList(nodes);\n const paths = this._containerInsertAfter(nodes);\n this.remove();\n return paths;\n } else {\n return this.replaceWithMultiple(nodes);\n }\n } else {\n return this.replaceWith(nodes);\n }\n}\n"],"mappings":";;;;;;;;;;;AAEA,IAAAA,UAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AACA,IAAAK,EAAA,GAAAL,OAAA;AAuBA,IAAAM,qBAAA,GAAAN,OAAA;AAA2D;EAtBzDO,cAAc;EACdC,uBAAuB;EACvBC,oBAAoB;EACpBC,eAAe;EACfC,cAAc;EACdC,cAAc;EACdC,SAAS;EACTC,mBAAmB;EACnBC,UAAU;EACVC,sBAAsB;EACtBC,uBAAuB;EACvBC,gBAAgB;EAChBC,YAAY;EACZC,SAAS;EACTC,WAAW;EACXC,cAAc;EACdC,eAAe;EACfC,oBAAoB;EACpBC,QAAQ;EACRC;AAAe,IAAArB,EAAA;AAaV,SAASsB,mBAAmBA,CAEjCC,KAAwB,EACZ;EAAA,IAAAC,eAAA;EACZ,IAAI,CAACC,MAAM,CAAC,CAAC;EAEbF,KAAK,GAAG,IAAI,CAACG,eAAe,CAACH,KAAK,CAAC;EACnCZ,sBAAsB,CAACY,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAACI,IAAI,CAAC;EAC3Cf,uBAAuB,CAACW,KAAK,CAACA,KAAK,CAACK,MAAM,GAAG,CAAC,CAAC,EAAE,IAAI,CAACD,IAAI,CAAC;EAC3D,CAAAH,eAAA,OAAAK,qBAAc,EAAC,IAAI,CAACC,GAAG,EAAE,IAAI,CAACC,MAAM,CAAC,qBAArCP,eAAA,CAAuCQ,MAAM,CAAC,IAAI,CAACL,IAAI,CAAC;EACxD,IAAI,CAACA,IAAI,GAEP,IAAI,CAACM,SAAS,CAAC,IAAI,CAACC,GA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AACpD,WAAW,CAACpD,eAAe,CAAE,IAAI,CAAcsB,IAAI,CAAC,CAAC;IAC5D;EACF;EACA,IAAIkF,mBAAmB,EAAE;IACvBH,SAAS,CAAC1B,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC;IAChC,IAAI,CAACvB,WAAW,CAACpC,eAAe,CAAE,IAAI,CAAcM,IAAI,EAAE,IAAI,CAAC,CAAC;EAClE;EAEA,OAAO+E,SAAS,CAAC/B,GAAG,CAAC,WAAW,CAAC;AACnC;AAEO,SAASmC,aAAaA,CAAiBvF,KAA6B,EAAE;EAC3E,IAAI,CAACE,MAAM,CAAC,CAAC;EAEb,IAAIqC,KAAK,CAACC,OAAO,CAACxC,KAAK,CAAC,EAAE;IACxB,IAAIuC,KAAK,CAACC,OAAO,CAAC,IAAI,CAAC9B,SAAS,CAAC,EAAE;MACjCV,KAAK,GAAG,IAAI,CAACG,eAAe,CAACH,KAAK,CAAC;MACnC,MAAMY,KAAK,GAAG,IAAI,CAAC4E,qBAAqB,CAACxF,KAAK,CAAC;MAC/C,IAAI,CAACe,MAAM,CAAC,CAAC;MACb,OAAOH,KAAK;IACd,CAAC,MAAM;MACL,OAAO,IAAI,CAACb,mBAAmB,CAACC,KAAK,CAAC;IACxC;EACF,CAAC,MAAM;IACL,OAAO,IAAI,CAACkC,WAAW,CAAClC,KAAK,CAAC;EAChC;AACF"}
|
package/lib/scope/binding.js
CHANGED
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
class Binding {
|
9
8
|
constructor({
|
10
9
|
identifier,
|
@@ -12,60 +11,73 @@ class Binding {
|
|
12
11
|
path,
|
13
12
|
kind
|
14
13
|
}) {
|
15
|
-
this.identifier =
|
16
|
-
this.scope =
|
17
|
-
this.path =
|
18
|
-
this.kind =
|
14
|
+
this.identifier = void 0;
|
15
|
+
this.scope = void 0;
|
16
|
+
this.path = void 0;
|
17
|
+
this.kind = void 0;
|
19
18
|
this.constantViolations = [];
|
20
19
|
this.constant = true;
|
21
20
|
this.referencePaths = [];
|
22
21
|
this.referenced = false;
|
23
22
|
this.references = 0;
|
23
|
+
this.identifier = identifier;
|
24
|
+
this.scope = scope;
|
25
|
+
this.path = path;
|
26
|
+
this.kind = kind;
|
27
|
+
if ((kind === "var" || kind === "hoisted") && isDeclaredInLoop(path)) {
|
28
|
+
this.reassign(path);
|
29
|
+
}
|
24
30
|
this.clearValue();
|
25
31
|
}
|
26
|
-
|
27
32
|
deoptValue() {
|
28
33
|
this.clearValue();
|
29
34
|
this.hasDeoptedValue = true;
|
30
35
|
}
|
31
|
-
|
32
36
|
setValue(value) {
|
33
37
|
if (this.hasDeoptedValue) return;
|
34
38
|
this.hasValue = true;
|
35
39
|
this.value = value;
|
36
40
|
}
|
37
|
-
|
38
41
|
clearValue() {
|
39
42
|
this.hasDeoptedValue = false;
|
40
43
|
this.hasValue = false;
|
41
44
|
this.value = null;
|
42
45
|
}
|
43
|
-
|
44
46
|
reassign(path) {
|
45
47
|
this.constant = false;
|
46
|
-
|
47
48
|
if (this.constantViolations.indexOf(path) !== -1) {
|
48
49
|
return;
|
49
50
|
}
|
50
|
-
|
51
51
|
this.constantViolations.push(path);
|
52
52
|
}
|
53
|
-
|
54
53
|
reference(path) {
|
55
54
|
if (this.referencePaths.indexOf(path) !== -1) {
|
56
55
|
return;
|
57
56
|
}
|
58
|
-
|
59
57
|
this.referenced = true;
|
60
58
|
this.references++;
|
61
59
|
this.referencePaths.push(path);
|
62
60
|
}
|
63
|
-
|
64
61
|
dereference() {
|
65
62
|
this.references--;
|
66
63
|
this.referenced = !!this.references;
|
67
64
|
}
|
68
|
-
|
65
|
+
}
|
66
|
+
exports.default = Binding;
|
67
|
+
function isDeclaredInLoop(path) {
|
68
|
+
for (let {
|
69
|
+
parentPath,
|
70
|
+
key
|
71
|
+
} = path; parentPath; ({
|
72
|
+
parentPath,
|
73
|
+
key
|
74
|
+
} = parentPath)) {
|
75
|
+
if (parentPath.isFunctionParent()) return false;
|
76
|
+
if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
|
77
|
+
return true;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
return false;
|
69
81
|
}
|
70
82
|
|
71
|
-
|
83
|
+
//# sourceMappingURL=binding.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["Binding","constructor","identifier","scope","path","kind","constantViolations","constant","referencePaths","referenced","references","isDeclaredInLoop","reassign","clearValue","deoptValue","hasDeoptedValue","setValue","value","hasValue","indexOf","push","reference","dereference","exports","default","parentPath","key","isFunctionParent","isWhile","isForXStatement","isForStatement"],"sources":["../../src/scope/binding.ts"],"sourcesContent":["import type NodePath from \"../path/index.ts\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"./index.ts\";\n\nexport type BindingKind =\n | \"var\" /* var declarator */\n | \"let\" /* let declarator, class declaration id, catch clause parameters */\n | \"const\" /* const/using declarator */\n | \"module\" /* import specifiers */\n | \"hoisted\" /* function declaration id */\n | \"param\" /* function declaration parameters */\n | \"local\" /* function expression id, class expression id */\n | \"unknown\"; /* export specifiers */\n/**\n * This class is responsible for a binding inside of a scope.\n *\n * It tracks the following:\n *\n * * Node path.\n * * Amount of times referenced by other nodes.\n * * Paths to nodes that reassign or modify this binding.\n * * The kind of binding. (Is it a parameter, declaration etc)\n */\n\nexport default class Binding {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n\n constructor({\n identifier,\n scope,\n path,\n kind,\n }: {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n }) {\n this.identifier = identifier;\n this.scope = scope;\n this.path = path;\n this.kind = kind;\n\n if ((kind === \"var\" || kind === \"hoisted\") && isDeclaredInLoop(path)) {\n this.reassign(path);\n }\n\n this.clearValue();\n }\n\n constantViolations: Array<NodePath> = [];\n constant: boolean = true;\n\n referencePaths: Array<NodePath> = [];\n referenced: boolean = false;\n references: number = 0;\n\n declare hasDeoptedValue: boolean;\n declare hasValue: boolean;\n declare value: any;\n\n deoptValue() {\n this.clearValue();\n this.hasDeoptedValue = true;\n }\n\n setValue(value: any) {\n if (this.hasDeoptedValue) return;\n this.hasValue = true;\n this.value = value;\n }\n\n clearValue() {\n this.hasDeoptedValue = false;\n this.hasValue = false;\n this.value = null;\n }\n\n /**\n * Register a constant violation with the provided `path`.\n */\n\n reassign(path: NodePath) {\n this.constant = false;\n if (this.constantViolations.indexOf(path) !== -1) {\n return;\n }\n this.constantViolations.push(path);\n }\n\n /**\n * Increment the amount of references to this binding.\n */\n\n reference(path: NodePath) {\n if (this.referencePaths.indexOf(path) !== -1) {\n return;\n }\n this.referenced = true;\n this.references++;\n this.referencePaths.push(path);\n }\n\n /**\n * Decrement the amount of references to this binding.\n */\n\n dereference() {\n this.references--;\n this.referenced = !!this.references;\n }\n}\n\nfunction isDeclaredInLoop(path: NodePath) {\n for (\n let { parentPath, key } = path;\n parentPath;\n { parentPath, key } = parentPath\n ) {\n if (parentPath.isFunctionParent()) return false;\n if (\n parentPath.isWhile() ||\n parentPath.isForXStatement() ||\n (parentPath.isForStatement() && key === \"body\")\n ) {\n return true;\n }\n }\n return false;\n}\n"],"mappings":";;;;;;AAwBe,MAAMA,OAAO,CAAC;EAM3BC,WAAWA,CAAC;IACVC,UAAU;IACVC,KAAK;IACLC,IAAI;IACJC;EAMF,CAAC,EAAE;IAAA,KAfHH,UAAU;IAAA,KACVC,KAAK;IAAA,KACLC,IAAI;IAAA,KACJC,IAAI;IAAA,KAyBJC,kBAAkB,GAAoB,EAAE;IAAA,KACxCC,QAAQ,GAAY,IAAI;IAAA,KAExBC,cAAc,GAAoB,EAAE;IAAA,KACpCC,UAAU,GAAY,KAAK;IAAA,KAC3BC,UAAU,GAAW,CAAC;IAjBpB,IAAI,CAACR,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACC,KAAK,GAAGA,KAAK;IAClB,IA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