@visulima/fs
Version:
Human friendly file system utilities for Node.js
232 lines (227 loc) • 7.52 kB
JavaScript
import JSONError from './JSONError-BkHRnInH.js';
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;
};
const binarySearch = (element, array) => {
let m = 0;
let n = array.length - 2;
while (m < n) {
const key = m + (n - m >> 1);
if (element < array[key]) {
n = key - 1;
} else if (element >= array[key + 1]) {
m = key + 1;
} else {
m = key;
break;
}
}
return m;
};
const getLineStartIndexes = (string_) => (
// eslint-disable-next-line unicorn/no-array-reduce
string_.split(/\n|\r(?!\n)/).reduce(
(accumulator, current) => {
accumulator.push(accumulator.at(-1) + current.length + 1);
return accumulator;
},
[0]
)
);
const indexToLineColumn = (input, index, options) => {
if ((!Array.isArray(input) && typeof input !== "string" || (typeof input === "string" || Array.isArray(input)) && input.length === 0)) {
return { column: 0, line: 0 };
}
if ((typeof index !== "number" || typeof input === "string" && index >= input.length || Array.isArray(input) && index + 1 >= input.at(-1))) {
return { column: 0, line: 0 };
}
if (typeof input === "string") {
const startIndexesOfEachLine = getLineStartIndexes(input);
const line2 = binarySearch(index, startIndexesOfEachLine);
return {
column: index - startIndexesOfEachLine[line2] + 1,
line: line2 + 1
};
}
const line = binarySearch(index, input);
return {
column: index - input[line] + 1,
line: line + 1
};
};
const getCodePoint = (character) => String.raw`\u{${character.codePointAt(0).toString(16)}}`;
const generateCodeFrame = (source, location, options) => codeFrame(
source,
{ start: location },
{
tabWidth: false,
...options
}
);
const getErrorLocation = (source, message) => {
const match = /in JSON at position (?<index>\d+)(?: \(line (?<line>\d+) column (?<column>\d+)\))?$/.exec(message);
if (!match) {
return void 0;
}
let { column, index, line } = match.groups;
if (line && column) {
return { column: Number(column), line: Number(line) };
}
index = Number(index);
if (index === source.length) {
index = source.length - 1;
}
return indexToLineColumn(source, index);
};
const addCodePointToUnexpectedToken = (message) => message.replace(
// The token is always quoted after Node.js 20, but we handle both cases for compatibility
// eslint-disable-next-line regexp/no-potentially-useless-backreference
/(?<=^Unexpected token )(?<quote>')?(.)\k<quote>/,
(_, _quote, token) => `"${token}"(${getCodePoint(token)})`
);
function parseJson(string, reviver, fileName, options) {
if (typeof reviver === "string") {
if (typeof fileName === "object") {
options = fileName;
}
fileName = reviver;
reviver = void 0;
}
let message;
try {
return JSON.parse(string, reviver);
} catch (error) {
message = error.message;
}
let location;
if (string) {
location = getErrorLocation(string, message);
message = addCodePointToUnexpectedToken(message);
} else {
message += " while parsing empty string";
}
const jsonError = new JSONError(message);
jsonError.fileName = fileName;
if (location) {
jsonError.codeFrame = generateCodeFrame(string, location, options);
}
throw jsonError;
}
export { parseJson as default };