-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.ts
213 lines (190 loc) 路 6.94 KB
/
index.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { declare } from "@babel/helper-plugin-utils";
import syntacExplicitResourceManagement from "@babel/plugin-syntax-explicit-resource-management";
import { types as t, template, traverse, type PluginPass } from "@babel/core";
import type { NodePath, Visitor } from "@babel/traverse";
export default declare(api => {
// TOOD: assert version 7.22.0
api.assertVersion(7);
const TOP_LEVEL_USING = new WeakSet();
const TOP_LEVEL_AWAIT_USING = new WeakSet();
function isUsingDeclaration(node: t.Node): node is t.VariableDeclaration {
if (!t.isVariableDeclaration(node)) return false;
return (
node.kind === "using" ||
node.kind === "await using" ||
TOP_LEVEL_USING.has(node) ||
TOP_LEVEL_AWAIT_USING.has(node)
);
}
const transformUsingDeclarationsVisitor: Visitor<PluginPass> = {
ForOfStatement(path: NodePath<t.ForOfStatement>) {
const { left } = path.node;
if (!isUsingDeclaration(left)) return;
const { id } = left.declarations[0];
const tmpId = path.scope.generateUidIdentifierBasedOnNode(id);
left.declarations[0].id = tmpId;
left.kind = "const";
path.ensureBlock();
path.node.body.body.unshift(
t.variableDeclaration("using", [
t.variableDeclarator(id, t.cloneNode(tmpId)),
]),
);
},
BlockStatement(path, state) {
let stackId: t.Identifier | null = null;
let needsAwait = false;
for (const node of path.node.body) {
if (!isUsingDeclaration(node)) continue;
stackId ??= path.scope.generateUidIdentifier("stack");
const isAwaitUsing =
node.kind === "await using" || TOP_LEVEL_AWAIT_USING.has(node);
needsAwait ||= isAwaitUsing;
if (
!TOP_LEVEL_AWAIT_USING.delete(node) &&
!TOP_LEVEL_USING.delete(node)
) {
node.kind = "const";
}
node.declarations.forEach(decl => {
const args = [t.cloneNode(stackId), decl.init];
if (isAwaitUsing) args.push(t.booleanLiteral(true));
decl.init = t.callExpression(state.addHelper("using"), args);
});
}
if (!stackId) return;
const errorId = path.scope.generateUidIdentifier("error");
const hasErrorId = path.scope.generateUidIdentifier("hasError");
let disposeCall: t.Expression = t.callExpression(
state.addHelper("dispose"),
[
t.cloneNode(stackId),
t.cloneNode(errorId),
t.cloneNode(hasErrorId),
// Pass SuppressedError so that it can be used with "pure"
// polyfills that do not compile the contents of runtime
// helpers.
template.expression.ast`
typeof SuppressedError !== "undefined" && SuppressedError
`,
],
);
if (needsAwait) disposeCall = t.awaitExpression(disposeCall);
const replacement = template.statement.ast`
try {
var ${stackId} = [];
${path.node.body}
} catch (_) {
var ${errorId} = _;
var ${hasErrorId} = true;
} finally {
${disposeCall}
}
`;
const { parentPath } = path;
if (
parentPath.isFunction() ||
parentPath.isTryStatement() ||
parentPath.isCatchClause() ||
parentPath.isStaticBlock()
) {
path.replaceWith(t.blockStatement([replacement]));
} else {
path.replaceWith(replacement);
}
},
};
return {
name: "proposal-explicit-resource-management",
inherits: syntacExplicitResourceManagement,
visitor: traverse.visitors.merge([
transformUsingDeclarationsVisitor,
{
// To transform top-level using declarations, we must wrap the
// module body in a block after hoisting all the exports and imports.
// This might cause some variables to be `undefined` rather than TDZ.
Program(path) {
if (path.node.sourceType !== "module") return;
if (!path.node.body.some(isUsingDeclaration)) return;
const innerBlockBody = [];
for (const stmt of path.get("body")) {
if (stmt.isFunctionDeclaration() || stmt.isImportDeclaration()) {
continue;
}
let { node } = stmt;
let shouldRemove = true;
if (stmt.isExportDefaultDeclaration()) {
let { declaration } = stmt.node;
let varId;
if (t.isClassDeclaration(declaration)) {
varId = declaration.id;
declaration.id = null;
declaration = t.toExpression(declaration);
} else if (!t.isExpression(declaration)) {
continue;
}
varId ??= path.scope.generateUidIdentifier("_default");
innerBlockBody.push(
t.variableDeclaration("var", [
t.variableDeclarator(varId, declaration),
]),
);
stmt.replaceWith(
t.exportNamedDeclaration(null, [
t.exportSpecifier(
t.cloneNode(varId),
t.identifier("default"),
),
]),
);
continue;
}
if (stmt.isExportNamedDeclaration()) {
node = stmt.node.declaration;
if (!node || t.isFunction(node)) continue;
stmt.replaceWith(
t.exportNamedDeclaration(
null,
Object.keys(t.getOuterBindingIdentifiers(node, false)).map(
id => t.exportSpecifier(t.identifier(id), t.identifier(id)),
),
),
);
shouldRemove = false;
} else if (stmt.isExportDeclaration()) {
continue;
}
if (t.isClassDeclaration(node)) {
const { id } = node;
node.id = null;
innerBlockBody.push(
t.variableDeclaration("var", [
t.variableDeclarator(id, t.toExpression(node)),
]),
);
} else if (t.isVariableDeclaration(node)) {
if (node.kind === "using") {
TOP_LEVEL_USING.add(stmt.node);
} else if (node.kind === "await using") {
TOP_LEVEL_AWAIT_USING.add(stmt.node);
}
node.kind = "var";
innerBlockBody.push(node);
} else {
innerBlockBody.push(stmt.node);
}
if (shouldRemove) stmt.remove();
}
path.pushContainer("body", t.blockStatement(innerBlockBody));
},
// We must transform `await using` in async functions before that
// async-to-generator will transform `await` expressions into `yield`
Function(path, state) {
if (path.node.async) {
path.traverse(transformUsingDeclarationsVisitor, state);
}
},
},
]),
};
});