| 18 | |
| 19 | // frame has the form "at myMethod (location/to/my/file.js:10:2)" |
| 20 | function getCodeFrame(frame) { |
| 21 | const locationStart = frame.indexOf('(') + 1 |
| 22 | const locationEnd = frame.indexOf(')') |
| 23 | const frameLocation = frame.slice(locationStart, locationEnd) |
| 24 | |
| 25 | const frameLocationElements = frameLocation.split(':') |
| 26 | const [filename, line, column] = [ |
| 27 | frameLocationElements[0], |
| 28 | parseInt(frameLocationElements[1], 10), |
| 29 | parseInt(frameLocationElements[2], 10), |
| 30 | ] |
| 31 | |
| 32 | let rawFileContents = '' |
| 33 | try { |
| 34 | rawFileContents = readFileSync(filename, 'utf-8') |
| 35 | } catch { |
| 36 | return '' |
| 37 | } |
| 38 | |
| 39 | const codeFrame = codeFrameColumns( |
| 40 | rawFileContents, |
| 41 | { |
| 42 | start: {line, column}, |
| 43 | }, |
| 44 | { |
| 45 | highlightCode: true, |
| 46 | linesBelow: 0, |
| 47 | }, |
| 48 | ) |
| 49 | return `${picocolors.dim(frameLocation)}\n${codeFrame}\n` |
| 50 | } |
| 51 | |
| 52 | function getUserCodeFrame() { |
| 53 | // If we couldn't load dependencies, we can't generate the user trace |