@stryke/json
Version:
A package containing JSON parsing/stringify utilities used by Storm Software.
34 lines (32 loc) • 898 B
JavaScript
import { codeFrameColumns } from "./code-frames.mjs";
import { printParseErrorCode } from "jsonc-parser";
import { LinesAndColumns } from "lines-and-columns";
//#region src/utils/parse-error.ts
/**
* Nicely formats a JSON error with context
*
* @param input - JSON content as string
* @param parseError - jsonc ParseError
* @returns Formatted error message with context
*/
function formatParseError(input, parseError) {
const { error, offset, length } = parseError;
const result = new LinesAndColumns(input).locationForIndex(offset);
let line = result?.line ?? 0;
let column = result?.column ?? 0;
line++;
column++;
return `${printParseErrorCode(error)} in JSON at ${line}:${column}\n${codeFrameColumns(input, {
start: {
line,
column
},
end: {
line,
column: column + length
}
})}\n`;
}
//#endregion
export { formatParseError };
//# sourceMappingURL=parse-error.mjs.map