Analyzes stackframes and source maps to get a function's call site as AST node *at runtime*.
npm install ts-call-site:exclamation: THIS MODULE RELIES ON SOURCE MAPS TO WORK :exclamation:
ts
// Enable source maps, see source-map-support's documentation for alternatives
import 'source-map-support/register'
import { getCallSite } from 'ts-call-site'
function myAmazingCaller() {
myAmazingFunction();
}
function myAmazingFunction() {
const caller = getCallSite();
// Now you can do whatever you want with your CallExpression
console.log(caller.getText()); // 'myAmazingFunction()'
}
`
The getCallSite method also supports an options object:
$3
An Error object containing the stack representing the call to be analyzed.
This stack will generally look like this:
`
[0] at myAmazingFunction (a:b)
[1] at myAmazingCaller (x:y)
`
The second element down this stack (index 1) is treated as 'the caller', and the CallExpression at that position is returned.
If none is provided, a new Error object is created for you.
The extra getCallSite stackframe is taken into account.
$3
A Project instance or ProjectOptions from ts-morph. Used to setup source analysis.
Re-using a Project instance among multiple getCallSite calls will almost certainly result in a big performance boost.
If none is provided, a new Project is created based on the tsconfig` file nearest to the calling file.