1 | "use strict";
|
2 | var __importStar = (this && this.__importStar) || function (mod) {
|
3 | if (mod && mod.__esModule) return mod;
|
4 | var result = {};
|
5 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
6 | result["default"] = mod;
|
7 | return result;
|
8 | };
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const babylon = require("babylon");
|
11 | const recast = __importStar(require("recast"));
|
12 | function wrapInAsyncFunction(code) {
|
13 | const codeInAsyncFunction = `(async () => {
|
14 | ${code}
|
15 | })()`;
|
16 | const ast = recast.parse(codeInAsyncFunction, { parser: babylon });
|
17 | const body = ast.program.body[0].expression.callee.body.body;
|
18 | if (body.length !== 0) {
|
19 | const last = body.pop();
|
20 | if (last.type === "ExpressionStatement") {
|
21 | body.push({
|
22 | type: "ReturnStatement",
|
23 | argument: last,
|
24 | });
|
25 | }
|
26 | else {
|
27 | body.push(last);
|
28 | }
|
29 | }
|
30 |
|
31 |
|
32 | ast.program.body[0].expression.callee.body.body = body.map((node) => {
|
33 | if (node.type === "VariableDeclaration") {
|
34 | return {
|
35 | type: "ExpressionStatement",
|
36 | expression: {
|
37 | type: "SequenceExpression",
|
38 | expressions: node.declarations.map((declaration) => ({
|
39 | type: "AssignmentExpression",
|
40 | operator: "=",
|
41 | left: declaration.id,
|
42 | right: declaration.init,
|
43 | })),
|
44 | },
|
45 | };
|
46 | }
|
47 | else {
|
48 | return node;
|
49 | }
|
50 | });
|
51 | return recast.print(ast).code;
|
52 | }
|
53 | exports.wrapInAsyncFunction = wrapInAsyncFunction;
|
54 |
|
\ | No newline at end of file |