char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
e0f52fe
main
1import { 2Error , 3ErrorGetStack , 4ObjectDefineProperty , 5ObjectGetPrototypeOf , 6ReflectApply , 7String , 8StringPrototypeSlice , 9uncurryThis , 10} from "ext:core/primordials" ; 11import { mapSourceLocation } from "ext:core/source_map" ; 12const lastIndexOf = uncurryThis ( String . prototype . lastIndexOf ); 13 14ObjectDefineProperty ( Error , "prepareStackTrace" , { 15value :( _ , frames ) => frames , 16writable :true , 17configurable :true , 18}); 19const prototype = ObjectGetPrototypeOf ( ErrorGetStack ( new Error ())[ 0 ]); 20const fileName = uncurryThis ( prototype . getFileName ); 21const lineNumber = uncurryThis ( prototype . getLineNumber ); 22const columnNumber = uncurryThis ( prototype . getColumnNumber ); 23const frameToString = uncurryThis ( prototype . toString ); 24const errorToString = uncurryThis ( Error . prototype . toString ); 25 26delete Error . prepareStackTrace ; 27export function formatStackTrace ( error , frames ) { 28const custom = Error . prepareStackTrace ; 29if ( typeof custom === "function" ) { 30return ReflectApply ( custom , Error , [ error , frames ]); 31} 32let stack ; 33try { 34stack = errorToString ( error ); 35for ( let index = 0 ; index < frames . length ; index ++ ) { 36stack += `\n at ${ frameToString ( frames [ index ])} ` ; 37} 38} catch { 39// V8's reentrant fallback preserves the original message and guards hostile getters. 40stack = ErrorGetStack ( error ); 41} 42let end = stack . length ; 43for ( let index = frames . length - 1 ; index >= 0 ; index -- ) { 44const frame = frames [ index ]; 45const name = fileName ( frame ); 46const line = lineNumber ( frame ); 47const column = columnNumber ( frame ); 48const mapped = name && line && column && 49mapSourceLocation ( name , line , column ); 50if ( ! mapped ) continue ; 51let text ; 52try { 53text = `\n at ${ frameToString ( frame )} ` ; 54} catch { 55continue ; 56} 57const generated = ` ${ name } : ${ line } : ${ column } ` ; 58const frameAt = lastIndexOf ( stack , text , end ); 59const locationAt = lastIndexOf ( text , generated ); 60if ( frameAt === - 1 || locationAt === - 1 ) continue ; 61end = frameAt - 1 ; 62const at = frameAt + locationAt ; 63stack = StringPrototypeSlice ( stack , 0 , at ) + 64` ${ mapped [ 0 ]} : ${ mapped [ 1 ]} : ${ mapped [ 2 ]} ` + 65StringPrototypeSlice ( stack , at + generated . length ); 66} 67return stack ; 68}