Callstack for javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { sep } from 'path'; | |
function splitPath(filename, level: number) { | |
const paths = filename.split(sep); | |
return paths.splice(paths.length - level).join(sep); | |
} | |
export function caller(level: number) { | |
const oldStackTrace = Error.prepareStackTrace; | |
try { | |
// eslint-disable-next-line handle-callback-err | |
Error.prepareStackTrace = (err, structuredStackTrace) => | |
structuredStackTrace; | |
Error.captureStackTrace(this); | |
const callSite = this.stack[level]; | |
return ( | |
splitPath(callSite.getFileName(), 2) + ':' + callSite.getLineNumber() | |
); | |
} finally { | |
Error.prepareStackTrace = oldStackTrace; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment