(source: string)
| 85 | }; |
| 86 | |
| 87 | const 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 | |
| 122 | const renderHeuristicBody = (source: string): string => { |
| 123 | const withoutDefaultExport = source.replace(/^export\s+default\s+/, "").trim(); |
no test coverage detected