( ctx: StateContext, path: babel.NodePath<ValidFunction>, direct: boolean, )
| 85 | } |
| 86 | |
| 87 | function transformFunction( |
| 88 | ctx: StateContext, |
| 89 | path: babel.NodePath<ValidFunction>, |
| 90 | direct: boolean, |
| 91 | ) { |
| 92 | if (!direct) { |
| 93 | if (!isFunctionDirectiveValid(ctx, path)) { |
| 94 | return; |
| 95 | } |
| 96 | cleanFunctionDirectives(ctx, path); |
| 97 | } |
| 98 | // First, get root statement |
| 99 | const rootStatement = getRootStatementPath(path); |
| 100 | |
| 101 | // Create a unique ID for the function |
| 102 | const fnID = createID(ctx, getDescriptiveName(path, "anonymous")); |
| 103 | |
| 104 | if (ctx.mode === "server") { |
| 105 | // Create a "source" function on the root-level |
| 106 | const sourceReference = t.callExpression( |
| 107 | getImportIdentifier(ctx.imports, path, ctx.definitions.register), |
| 108 | [t.stringLiteral(fnID), path.node], |
| 109 | ); |
| 110 | |
| 111 | const sourceID = generateUniqueName(path, "serverFn"); |
| 112 | |
| 113 | rootStatement.insertBefore( |
| 114 | t.variableDeclaration("const", [t.variableDeclarator(sourceID, sourceReference)]), |
| 115 | ); |
| 116 | |
| 117 | // Clone the source function to replace the server function |
| 118 | path.replaceWith( |
| 119 | t.callExpression(getImportIdentifier(ctx.imports, path, ctx.definitions.clone), [sourceID]), |
| 120 | ); |
| 121 | } else { |
| 122 | // Otherwise, clone the function based on its ID |
| 123 | path.replaceWith( |
| 124 | t.callExpression(getImportIdentifier(ctx.imports, path, ctx.definitions.clone), [ |
| 125 | t.stringLiteral(fnID), |
| 126 | ]), |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | path.scope.crawl(); |
| 131 | } |
| 132 | |
| 133 | function traceBinding(path: babel.NodePath, name: string): Binding | undefined { |
| 134 | const current = path.scope.getBinding(name); |
no test coverage detected