import * as stackTrace from "stack-trace" ; import { basename as pathBasename , sep as pathSep } from "path" ; import { extensionRoot , debug } from "./constants" ; export function getStackTrace ( ex ) { let trace = "\t" ; for ( const frame of stackTrace . parse ( ex ) ) { let filename = frame . getFileName ( ) ; if ( filename ) { if ( ! debug && ! isExtensionFile ( filename ) ) { continue ; } filename = sanitizeFilename ( filename ) ; const lineno = frame . getLineNumber ( ) ; const colno = frame . getColumnNumber ( ) ; trace += `\n\tat ${ getCallSite ( frame ) } ${ filename }:${ lineno }:${ colno }` ; } else { trace += "\n\tat <anonymous>" ; } } return trace . trim ( ) . replace ( /\\/g , "/" ) ; } function isExtensionFile ( filename ) { const extensionPath = extensionRoot . path ; if ( ! extensionPath ) { return true ; } return filename . startsWith ( extensionPath ) ; } function sanitizeFilename ( filename ) { const extensionPath = extensionRoot . path ; if ( ! extensionPath ) { return "<hidden_no_extension_root>" ; } if ( filename . startsWith ( extensionPath ) ) { filename = `<autoDocstring>${ filename . substring ( extensionPath . length ) }` ; } else { filename = `<hidden>${ pathSep }${ pathBasename ( filename ) }` ; } return filename ; } function sanitizeName ( name ) { if ( name . indexOf ( "/" ) === - 1 && name . indexOf ( "\\" ) === - 1 ) { return name ; } else { return "<hidden>" ; } } function getCallSite ( frame ) { const parts = [ ] ; if ( typeof frame . getTypeName ( ) === "string" && frame . getTypeName ( ) . length > 0 ) { parts . push ( frame . getTypeName ( ) ) ; } if ( typeof frame . getMethodName ( ) === "string" && frame . getMethodName ( ) . length > 0 ) { parts . push ( frame . getMethodName ( ) ) ; } if ( typeof frame . getFunctionName ( ) === "string" && frame . getFunctionName ( ) . length > 0 ) { if ( parts . length !== 2 || parts . join ( "." ) !== frame . getFunctionName ( ) ) { parts . push ( frame . getFunctionName ( ) ) ; } } return parts . map ( sanitizeName ) . join ( "." ) ; }