Skip to content

Commit 83ef690

Browse files
authored
fix: JSON parsing with trailing commas enabled (#37)
* 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. * 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.
1 parent 569d03b commit 83ef690

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function readJsonFile<T extends JsonValue = JsonObject>(filepath: string,
3838
.toString()
3939
.replace(/^\uFEFF/, ""); // Remove BOM if present.
4040

41-
const jsonContents = jsonc.parse(fileContent, jsonErrors, {allowEmptyContent: true}) ?? {};
41+
// Parse the JSON content using jsonc-parser, allowing empty content and trailing commas.
42+
const jsonContents = jsonc.parse(fileContent, jsonErrors, {allowEmptyContent: true, allowTrailingComma: true}) ?? {};
4243

4344
if (jsonErrors.length > 0) {
4445
const errorMessages = constructJsonParseErrorMsg(filepath, fileContent, jsonErrors);
@@ -78,19 +79,18 @@ function constructJsonParseErrorMsg(filepath: string, fileContent: string, jsonE
7879
return jsonErrors
7980
.map((err, i) => {
8081
// Get the error name from the numeric error code.
81-
// The name is PascalCased, so we need to format it by adding spaces
82+
const errorName = jsonc.printParseErrorCode(err.error);
83+
84+
// The error name is PascalCased, so we need to format it by adding spaces
8285
// before capital letters for readability.
83-
const errorName = jsonc
84-
.printParseErrorCode(err.error)
85-
.replace(/([A-Z])/g, " $1")
86-
.trim();
86+
const errorNameFormatted = errorName.replace(/([A-Z])/g, " $1").trim();
8787

8888
// Calculate line and column numbers from the error offset.
8989
const lineNumber = fileContent.substring(0, err.offset).split("\n").length;
9090
const columnNumber = err.offset - fileContent.lastIndexOf("\n", err.offset - 1);
9191

9292
// Return the formatted error message.
93-
return `\tError ${i + 1} - ${errorName} at "${filepath}:${lineNumber}:${columnNumber}"\n`;
93+
return `\tError ${i + 1} [${errorName}] - ${errorNameFormatted} at "${filepath}:${lineNumber}:${columnNumber}"\n`;
9494
})
9595
.join("\n");
9696
}

0 commit comments

Comments
 (0)