From 8564517543489ca89ec1c419b112945048e0b76c Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Mon, 20 Jul 2026 06:53:59 +0100 Subject: [PATCH 1/2] fix: JSON parsing with trailing commas enabled in `readJsonFile`. Fixes #36. During parsing of JSON files, the parser will throw an `ValueExpected` error when it reaches a comma and unexpectedly encounters a closing bracket straight after. Trailing commas are disabled in the parser by default as it's not standard in JSON or JSONC. So it expects some kind of value after all commas. Luckily, we can enable trailing commas... - Fixed `ValueExpected` JSON parse error for trailing commas by enabling the `allowTrailingComma` parse option in `readJsonFile` util function. This is especially useful when parsing language config files, when the contributing extension has auto-formatting enabled for trailing commas. --- src/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 5d6fe71..8d19d40 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -38,7 +38,8 @@ export function readJsonFile(filepath: string, .toString() .replace(/^\uFEFF/, ""); // Remove BOM if present. - const jsonContents = jsonc.parse(fileContent, jsonErrors, {allowEmptyContent: true}) ?? {}; + // Parse the JSON content using jsonc-parser, allowing empty content and trailing commas. + const jsonContents = jsonc.parse(fileContent, jsonErrors, {allowEmptyContent: true, allowTrailingComma: true}) ?? {}; if (jsonErrors.length > 0) { const errorMessages = constructJsonParseErrorMsg(filepath, fileContent, jsonErrors); From 826a76e159bdf2b954af1c8eff5c4cb23d97c442 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Mon, 20 Jul 2026 07:07:58 +0100 Subject: [PATCH 2/2] refactor: JSON parse error message formatting. - Refactored the JSON parse error messages in `constructJsonParseErrorMsg` util function to include the original error name, as well as the formatted name, just to ensure the errors have a proper error code for debugging. --- src/utils.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 8d19d40..de2752b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -79,19 +79,18 @@ function constructJsonParseErrorMsg(filepath: string, fileContent: string, jsonE return jsonErrors .map((err, i) => { // Get the error name from the numeric error code. - // The name is PascalCased, so we need to format it by adding spaces + const errorName = jsonc.printParseErrorCode(err.error); + + // The error name is PascalCased, so we need to format it by adding spaces // before capital letters for readability. - const errorName = jsonc - .printParseErrorCode(err.error) - .replace(/([A-Z])/g, " $1") - .trim(); + const errorNameFormatted = errorName.replace(/([A-Z])/g, " $1").trim(); // Calculate line and column numbers from the error offset. const lineNumber = fileContent.substring(0, err.offset).split("\n").length; const columnNumber = err.offset - fileContent.lastIndexOf("\n", err.offset - 1); // Return the formatted error message. - return `\tError ${i + 1} - ${errorName} at "${filepath}:${lineNumber}:${columnNumber}"\n`; + return `\tError ${i + 1} [${errorName}] - ${errorNameFormatted} at "${filepath}:${lineNumber}:${columnNumber}"\n`; }) .join("\n"); }