source: frontend/node_modules/@babel/plugin-transform-explicit-resource-management/lib/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 6.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _helperPluginUtils = require("@babel/helper-plugin-utils");
8var _pluginTransformDestructuring = require("@babel/plugin-transform-destructuring");
9var _core = require("@babel/core");
10function isAnonymousFunctionDefinition(node) {
11 return _core.types.isArrowFunctionExpression(node) || (_core.types.isFunctionExpression(node) || _core.types.isClassExpression(node)) && !node.id;
12}
13function emitSetFunctionNameCall(state, expression, name) {
14 return _core.types.callExpression(state.addHelper("setFunctionName"), [expression, _core.types.stringLiteral(name)]);
15}
16var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
17 api.assertVersion("^7.23.9 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
18 const TOP_LEVEL_USING = new Map();
19 function isUsingDeclaration(node) {
20 if (!_core.types.isVariableDeclaration(node)) return false;
21 return node.kind === "using" || node.kind === "await using" || TOP_LEVEL_USING.has(node);
22 }
23 const transformUsingDeclarationsVisitor = {
24 ForOfStatement(path) {
25 const {
26 left
27 } = path.node;
28 if (!isUsingDeclaration(left)) return;
29 const {
30 id
31 } = left.declarations[0];
32 const tmpId = path.scope.generateUidIdentifierBasedOnNode(id);
33 left.declarations[0].id = tmpId;
34 left.kind = "const";
35 path.ensureBlock();
36 (0, _pluginTransformDestructuring.unshiftForXStatementBody)(path, [_core.types.variableDeclaration("using", [_core.types.variableDeclarator(id, _core.types.cloneNode(tmpId))])]);
37 },
38 "BlockStatement|StaticBlock"(path, state) {
39 let ctx = null;
40 let needsAwait = false;
41 const scope = path.scope;
42 for (const node of path.node.body) {
43 if (!isUsingDeclaration(node)) continue;
44 ctx != null ? ctx : ctx = scope.generateUidIdentifier("usingCtx");
45 const isAwaitUsing = node.kind === "await using" || TOP_LEVEL_USING.get(node) === 1;
46 needsAwait || (needsAwait = isAwaitUsing);
47 if (!TOP_LEVEL_USING.delete(node)) {
48 node.kind = "const";
49 }
50 for (const decl of node.declarations) {
51 const currentInit = decl.init;
52 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]);
53 }
54 }
55 if (!ctx) return;
56 const disposeCall = _core.types.callExpression(_core.types.memberExpression(_core.types.cloneNode(ctx), _core.types.identifier("d")), []);
57 const replacement = _core.template.statement.ast`
58 try {
59 var ${_core.types.cloneNode(ctx)} = ${state.addHelper("usingCtx")}();
60 ${path.node.body}
61 } catch (_) {
62 ${_core.types.cloneNode(ctx)}.e = _;
63 } finally {
64 ${needsAwait ? _core.types.awaitExpression(disposeCall) : disposeCall}
65 }
66 `;
67 _core.types.inherits(replacement, path.node);
68 const {
69 parentPath
70 } = path;
71 if (parentPath.isFunction() || parentPath.isTryStatement() || parentPath.isCatchClause()) {
72 path.replaceWith(_core.types.blockStatement([replacement]));
73 } else if (path.isStaticBlock()) {
74 path.node.body = [replacement];
75 } else {
76 path.replaceWith(replacement);
77 }
78 }
79 };
80 const transformUsingDeclarationsVisitorSkipFn = _core.traverse.visitors.merge([transformUsingDeclarationsVisitor, {
81 Function(path) {
82 path.skip();
83 }
84 }]);
85 return {
86 name: "transform-explicit-resource-management",
87 manipulateOptions: (_, p) => p.plugins.push("explicitResourceManagement"),
88 visitor: _core.traverse.visitors.merge([transformUsingDeclarationsVisitor, {
89 Program(path) {
90 TOP_LEVEL_USING.clear();
91 if (path.node.sourceType !== "module") return;
92 if (!path.node.body.some(isUsingDeclaration)) return;
93 const innerBlockBody = [];
94 for (const stmt of path.get("body")) {
95 if (stmt.isFunctionDeclaration() || stmt.isImportDeclaration()) {
96 continue;
97 }
98 let node = stmt.node;
99 let shouldRemove = true;
100 if (stmt.isExportDefaultDeclaration()) {
101 let {
102 declaration
103 } = stmt.node;
104 let varId;
105 if (_core.types.isClassDeclaration(declaration)) {
106 varId = declaration.id;
107 declaration.id = _core.types.cloneNode(varId);
108 declaration = _core.types.toExpression(declaration);
109 } else if (!_core.types.isExpression(declaration)) {
110 continue;
111 }
112 varId != null ? varId : varId = path.scope.generateUidIdentifier("_default");
113 innerBlockBody.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(varId, declaration)]));
114 stmt.replaceWith(_core.types.exportNamedDeclaration(null, [_core.types.exportSpecifier(_core.types.cloneNode(varId), _core.types.identifier("default"))]));
115 continue;
116 }
117 if (stmt.isExportNamedDeclaration()) {
118 node = stmt.node.declaration;
119 if (!node || _core.types.isFunction(node)) continue;
120 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)))));
121 shouldRemove = false;
122 } else if (stmt.isExportDeclaration()) {
123 continue;
124 }
125 if (_core.types.isClassDeclaration(node)) {
126 const {
127 id
128 } = node;
129 node.id = _core.types.cloneNode(id);
130 innerBlockBody.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(id, _core.types.toExpression(node))]));
131 } else if (_core.types.isVariableDeclaration(node)) {
132 if (node.kind === "using") {
133 TOP_LEVEL_USING.set(stmt.node, 0);
134 } else if (node.kind === "await using") {
135 TOP_LEVEL_USING.set(stmt.node, 1);
136 }
137 node.kind = "var";
138 innerBlockBody.push(node);
139 } else {
140 innerBlockBody.push(stmt.node);
141 }
142 if (shouldRemove) stmt.remove();
143 }
144 path.pushContainer("body", _core.types.blockStatement(innerBlockBody));
145 },
146 Function(path, state) {
147 if (path.node.async) {
148 path.traverse(transformUsingDeclarationsVisitorSkipFn, state);
149 }
150 }
151 }])
152 };
153});
154
155//# sourceMappingURL=index.js.map
Note: See TracBrowser for help on using the repository browser.