execution-engine
Version:
A TypeScript library for tracing and visualizing code execution workflows.
54 lines (53 loc) • 2.16 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractFunctionParamNames = extractFunctionParamNames;
exports.extractFunctionMetadata = extractFunctionMetadata;
exports.extractClassMethodMetadata = extractClassMethodMetadata;
exports.attachFunctionMetadata = attachFunctionMetadata;
const isAsync_1 = require("./isAsync");
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
function extractFunctionParamNames(fn) {
const source = fn.toString();
const paramMatch = source.match(/\(([^)]*)\)/);
const rawParams = paramMatch ? paramMatch[1] : '';
return rawParams
.split(',')
.map((param) => param.trim())
.filter((param) => param);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
function extractFunctionMetadata(fn) {
return {
name: fn.name || 'anonymous',
parameters: extractFunctionParamNames(fn),
isAsync: (0, isAsync_1.isAsync)(fn),
isBound: fn.name.startsWith('bound ')
// source,
};
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
function extractClassMethodMetadata(className, methodName, fn) {
const functionMetadata = extractFunctionMetadata(fn);
return {
class: className,
method: methodName,
methodSignature: `${className}.${methodName?.toString()}(${functionMetadata.parameters.join(',')})`,
...functionMetadata
};
}
/**
* Wraps a function and attaches method metadata, or returns the value as-is.
* This is useful in method decorators, where the function needs to be aware of method-specific metadata
* that would otherwise be inaccessible in a plain function.
*
* @returns The original value or a function with attached metadata.
*/
function attachFunctionMetadata(paramOrFunction, thisMethodMetadata) {
return typeof paramOrFunction === 'function'
? (
// eslint-disable-next-line unused-imports/no-unused-vars
({ metadata, ...rest }) => {
return paramOrFunction.bind(this)({ ...rest, metadata: thisMethodMetadata });
})
: paramOrFunction;
}
;