Spaces:
Sleeping
Sleeping
File size: 6,860 Bytes
af203d7 | 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 | "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _pluginTransformDestructuring = require("@babel/plugin-transform-destructuring");
var _core = require("@babel/core");
function isAnonymousFunctionDefinition(node) {
return _core.types.isArrowFunctionExpression(node) || (_core.types.isFunctionExpression(node) || _core.types.isClassExpression(node)) && !node.id;
}
function emitSetFunctionNameCall(state, expression, name) {
return _core.types.callExpression(state.addHelper("setFunctionName"), [expression, _core.types.stringLiteral(name)]);
}
var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
api.assertVersion("^7.23.9 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
const TOP_LEVEL_USING = new Map();
function isUsingDeclaration(node) {
if (!_core.types.isVariableDeclaration(node)) return false;
return node.kind === "using" || node.kind === "await using" || TOP_LEVEL_USING.has(node);
}
const transformUsingDeclarationsVisitor = {
ForOfStatement(path) {
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();
(0, _pluginTransformDestructuring.unshiftForXStatementBody)(path, [_core.types.variableDeclaration("using", [_core.types.variableDeclarator(id, _core.types.cloneNode(tmpId))])]);
},
"BlockStatement|StaticBlock"(path, state) {
let ctx = null;
let needsAwait = false;
const scope = path.scope;
for (const node of path.node.body) {
if (!isUsingDeclaration(node)) continue;
ctx != null ? ctx : ctx = scope.generateUidIdentifier("usingCtx");
const isAwaitUsing = node.kind === "await using" || TOP_LEVEL_USING.get(node) === 1;
needsAwait || (needsAwait = isAwaitUsing);
if (!TOP_LEVEL_USING.delete(node)) {
node.kind = "const";
}
for (const decl of node.declarations) {
const currentInit = decl.init;
decl.init = _core.types.callExpression(_core.types.memberExpression(_core.types.cloneNode(ctx), isAwaitUsing ? _core.types.identifier("a") : _core.types.identifier("u")), [isAnonymousFunctionDefinition(currentInit) && _core.types.isIdentifier(decl.id) ? emitSetFunctionNameCall(state, currentInit, decl.id.name) : currentInit]);
}
}
if (!ctx) return;
const disposeCall = _core.types.callExpression(_core.types.memberExpression(_core.types.cloneNode(ctx), _core.types.identifier("d")), []);
const replacement = _core.template.statement.ast`
try {
var ${_core.types.cloneNode(ctx)} = ${state.addHelper("usingCtx")}();
${path.node.body}
} catch (_) {
${_core.types.cloneNode(ctx)}.e = _;
} finally {
${needsAwait ? _core.types.awaitExpression(disposeCall) : disposeCall}
}
`;
_core.types.inherits(replacement, path.node);
const {
parentPath
} = path;
if (parentPath.isFunction() || parentPath.isTryStatement() || parentPath.isCatchClause()) {
path.replaceWith(_core.types.blockStatement([replacement]));
} else if (path.isStaticBlock()) {
path.node.body = [replacement];
} else {
path.replaceWith(replacement);
}
}
};
const transformUsingDeclarationsVisitorSkipFn = _core.traverse.visitors.merge([transformUsingDeclarationsVisitor, {
Function(path) {
path.skip();
}
}]);
return {
name: "transform-explicit-resource-management",
manipulateOptions: (_, p) => p.plugins.push("explicitResourceManagement"),
visitor: _core.traverse.visitors.merge([transformUsingDeclarationsVisitor, {
Program(path) {
TOP_LEVEL_USING.clear();
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.node;
let shouldRemove = true;
if (stmt.isExportDefaultDeclaration()) {
let {
declaration
} = stmt.node;
let varId;
if (_core.types.isClassDeclaration(declaration)) {
varId = declaration.id;
declaration.id = _core.types.cloneNode(varId);
declaration = _core.types.toExpression(declaration);
} else if (!_core.types.isExpression(declaration)) {
continue;
}
varId != null ? varId : varId = path.scope.generateUidIdentifier("_default");
innerBlockBody.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(varId, declaration)]));
stmt.replaceWith(_core.types.exportNamedDeclaration(null, [_core.types.exportSpecifier(_core.types.cloneNode(varId), _core.types.identifier("default"))]));
continue;
}
if (stmt.isExportNamedDeclaration()) {
node = stmt.node.declaration;
if (!node || _core.types.isFunction(node)) continue;
stmt.replaceWith(_core.types.exportNamedDeclaration(null, Object.keys(_core.types.getOuterBindingIdentifiers(node, false)).map(id => _core.types.exportSpecifier(_core.types.identifier(id), _core.types.identifier(id)))));
shouldRemove = false;
} else if (stmt.isExportDeclaration()) {
continue;
}
if (_core.types.isClassDeclaration(node)) {
const {
id
} = node;
node.id = _core.types.cloneNode(id);
innerBlockBody.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(id, _core.types.toExpression(node))]));
} else if (_core.types.isVariableDeclaration(node)) {
if (node.kind === "using") {
TOP_LEVEL_USING.set(stmt.node, 0);
} else if (node.kind === "await using") {
TOP_LEVEL_USING.set(stmt.node, 1);
}
node.kind = "var";
innerBlockBody.push(node);
} else {
innerBlockBody.push(stmt.node);
}
if (shouldRemove) stmt.remove();
}
path.pushContainer("body", _core.types.blockStatement(innerBlockBody));
},
Function(path, state) {
if (path.node.async) {
path.traverse(transformUsingDeclarationsVisitorSkipFn, state);
}
}
}])
};
});
//# sourceMappingURL=index.js.map
|