UNPKG

@visulima/error

Version:

Error with more than just a message, stacktrace parsing.

126 lines (121 loc) 4.29 kB
const normalizeLF = (code) => code.replaceAll(/\r\n|\r(?!\n)|\n/gu, "\n"); const _process = globalThis.process || /* @__PURE__ */ Object.create(null); const processShims = { versions: {} }; const process = new Proxy(_process, { get(target, property) { if (property in target) { return target[property]; } if (property in processShims) { return processShims[property]; } return void 0; } }); const getMarkerLines = (loc, source, linesAbove, linesBelow) => { const startLoc = { column: 0, // @ts-expect-error Can be overwritten line: -1, ...loc.start }; const endLoc = { ...startLoc, ...loc.end }; const startLine = startLoc.line; const startColumn = startLoc.column; const endLine = endLoc.line; const endColumn = endLoc.column; let start = Math.max(startLine - (linesAbove + 1), 0); let end = Math.min(source.length, endLine + linesBelow); if (startLine === -1) { start = 0; } if (endLine === -1) { end = source.length; } const lineDiff = endLine - startLine; const markerLines = {}; if (lineDiff) { for (let index = 0; index <= lineDiff; index++) { const lineNumber = index + startLine; if (!startColumn) { markerLines[lineNumber] = true; } else if (index === 0) { const sourceLength = source[lineNumber - 1]?.length; markerLines[lineNumber] = [startColumn, (sourceLength ?? 0) - startColumn + 1]; } else if (index === lineDiff) { markerLines[lineNumber] = [0, endColumn]; } else { const sourceLength = source[lineNumber - index]?.length; markerLines[lineNumber] = [0, sourceLength]; } } } else if (startColumn === endColumn) { markerLines[startLine] = startColumn ? [startColumn, 0] : true; } else { markerLines[startLine] = [startColumn, (endColumn ?? 0) - (startColumn ?? 0)]; } return { end, markerLines, start }; }; const CODE_FRAME_POINTER = process.platform === "win32" && !process.env?.WT_SESSION ? ">" : "❯"; const codeFrame = (source, loc, options) => { const config = { // grab 2 lines before, and 3 lines after focused line linesAbove: 2, linesBelow: 3, prefix: "", showGutter: true, tabWidth: 4, ...options, color: { gutter: (value) => value, marker: (value) => value, message: (value) => value, ...options?.color } }; const hasColumns = loc.start && typeof loc.start.column === "number"; let lines = normalizeLF(source).split("\n"); if (typeof config?.tabWidth === "number") { lines = lines.map((ln) => ln.replaceAll(" ", " ".repeat(config.tabWidth))); } const { end, markerLines, start } = getMarkerLines(loc, lines, config.linesAbove, config.linesBelow); const numberMaxWidth = String(end).length; const { gutter: colorizeGutter, marker: colorizeMarker, message: colorizeMessage } = config.color; let frame = lines.slice(start, end).map((line, index) => { const number = start + 1 + index; const hasMarker = markerLines[number]; const paddedNumber = ` ${number}`.slice(-numberMaxWidth); const lastMarkerLine = !markerLines[number + 1]; const gutter = ` ${paddedNumber}${config.showGutter ? " |" : ""}`; if (hasMarker) { let markerLine = ""; if (Array.isArray(hasMarker)) { const markerSpacing = line.replaceAll(/[^\t]/g, " ").slice(0, Math.max(hasMarker[0] - 1, 0)); const numberOfMarkers = hasMarker[1] || 1; markerLine = [ "\n ", config.prefix + colorizeGutter(gutter.replaceAll(/\d/g, " ")), " ", markerSpacing, colorizeMarker("^").repeat(numberOfMarkers) ].join(""); if (lastMarkerLine && config.message) { markerLine += ` ${colorizeMessage(config.message)}`; } } return [config.prefix + colorizeMarker(CODE_FRAME_POINTER), colorizeGutter(gutter), line.length > 0 ? ` ${line}` : "", markerLine].join(""); } return `${config.prefix} ${colorizeGutter(gutter)}${line.length > 0 ? ` ${line}` : ""}`; }).join("\n"); if (config.message && !hasColumns) { frame = `${config.prefix + " ".repeat(numberMaxWidth + 1) + config.message} ${frame}`; } return frame; }; export { CODE_FRAME_POINTER, codeFrame };