MCPcopy Index your code
hub / github.com/solidjs/solid-start / transformFunction

Function transformFunction

packages/start/src/directives/plugin.ts:87–131  ·  view source on GitHub ↗
(
  ctx: StateContext,
  path: babel.NodePath<ValidFunction>,
  direct: boolean,
)

Source from the content-addressed store, hash-verified

85}
86
87function 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
133function traceBinding(path: babel.NodePath, name: string): Binding | undefined {
134 const current = path.scope.getBinding(name);

Callers 3

ArrowFunctionExpressionFunction · 0.85
FunctionExpressionFunction · 0.85

Calls 7

getRootStatementPathFunction · 0.90
getDescriptiveNameFunction · 0.90
getImportIdentifierFunction · 0.90
generateUniqueNameFunction · 0.90
isFunctionDirectiveValidFunction · 0.85
cleanFunctionDirectivesFunction · 0.85
createIDFunction · 0.85

Tested by

no test coverage detected