UNPKG

@visulima/error

Version:

Error with more than just a message, stacktrace parsing.

131 lines (126 loc) 4.93 kB
var __defProp$2 = Object.defineProperty; var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true }); const normalizeLF = /* @__PURE__ */ __name$2((code) => code.replaceAll(/\r\n|\r(?!\n)|\n/gu, "\n"), "normalizeLF"); 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; } }); var __defProp$1 = Object.defineProperty; var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true }); const getMarkerLines = /* @__PURE__ */ __name$1((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 }; }, "getMarkerLines"); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const CODE_FRAME_POINTER = process.platform === "win32" && !process.env?.WT_SESSION ? ">" : "❯"; const codeFrame = /* @__PURE__ */ __name((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: /* @__PURE__ */ __name((value) => value, "gutter"), marker: /* @__PURE__ */ __name((value) => value, "marker"), message: /* @__PURE__ */ __name((value) => value, "message"), ...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 + "\n" + frame; } return frame; }, "codeFrame"); export { CODE_FRAME_POINTER, codeFrame };