MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / renderParsedBody

Function renderParsedBody

packages/kernel/core/src/code-recovery.ts:87–120  ·  view source on GitHub ↗
(source: string)

Source from the content-addressed store, hash-verified

85};
86
87const renderParsedBody = (source: string): string => {
88 const program = parse(source, {
89 sourceType: "module",
90 allowAwaitOutsideFunction: true,
91 allowReturnOutsideFunction: true,
92 allowImportExportEverywhere: true,
93 plugins: ["typescript"],
94 }).program;
95
96 if (program.body.length !== 1) return source;
97
98 const [statement] = program.body;
99 if (!statement) return source;
100
101 switch (statement.type) {
102 case "ExpressionStatement": {
103 const expression = unwrapExpression(
104 statement.expression as { type: string; expression?: unknown },
105 ) as {
106 type?: string;
107 };
108 return expression?.type === "ArrowFunctionExpression" ||
109 expression?.type === "FunctionExpression"
110 ? wrapCallableBody(source)
111 : source;
112 }
113 case "FunctionDeclaration":
114 return statement.id?.name ? wrapNamedFunctionBody(source, statement.id.name) : source;
115 case "ExportDefaultDeclaration":
116 return renderExportDefaultBody(source, statement.declaration);
117 default:
118 return source;
119 }
120};
121
122const renderHeuristicBody = (source: string): string => {
123 const withoutDefaultExport = source.replace(/^export\s+default\s+/, "").trim();

Callers 1

recoverExecutionBodyFunction · 0.85

Calls 4

wrapNamedFunctionBodyFunction · 0.85
renderExportDefaultBodyFunction · 0.85
unwrapExpressionFunction · 0.70
wrapCallableBodyFunction · 0.70

Tested by

no test coverage detected