Skip to content

Commit 8564517

Browse files
committed
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.
1 parent 569d03b commit 8564517

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

src/utils.ts

Lines changed: 2 additions & 1 deletion
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);

0 commit comments

Comments
 (0)