solid-code
Version:
JavaScript code quality front-end
38 lines (28 loc) • 1.03 kB
JavaScript
// getting caller code from
// http://stackoverflow.com/questions/13227489/how-can-one-get-the-file-path-of-the-caller-function-in-node-js
function getCaller() {
var stack = getStack()
// Remove superfluous function calls on stack
stack.shift()
stack.shift()
// Return caller's caller
return stack[1].receiver.id;
}
module.exports.caller = getCaller;
function getStack() {
// Save original Error.prepareStackTrace
var origPrepareStackTrace = Error.prepareStackTrace
// Override with function that just returns `stack`
Error.prepareStackTrace = function (_, stack) {
return stack
}
// Create a new `Error`, which automatically gets `stack`
var err = new Error()
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
var stack = err.stack
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace
// Remove superfluous function call on stack
stack.shift() // getStack --> Error
return stack
}