Skip to content

Commit

Permalink
Update JSONC parsing to use jsonc-parser package's parse export in or…
Browse files Browse the repository at this point in the history
…der to handle trailing commas.
  • Loading branch information
DavidAnson committed Jan 20, 2024
1 parent e2747e9 commit 35f5ec7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const schemeFileSystemLike = new Set([
schemeVscodeTestWeb
]);
const configParsers = [
(content) => JSON.parse(require("jsonc-parser").stripComments(content)),
jsoncParse,
(content) => require("js-yaml").load(content)
];
const codeActionKindQuickFix = vscode.CodeActionKind.QuickFix;
Expand Down Expand Up @@ -121,6 +121,21 @@ function errorString (error) {
return `${error.name}: ${error.message}\n${error.stack}`;
}

// Parses JSONC text and returns an object
function jsoncParse (text) {
const { parse, printParseErrorCode } = require("jsonc-parser");
const errors = [];
const result = parse(text, errors, { "allowTrailingComma": true });
if (errors.length > 0) {
const aggregate = errors.map(
(error) => `${printParseErrorCode(error.error)} (offset ${error.offset}, length ${error.length})`
).join(", ");
throw new Error(`Unable to parse JSON(C) content, ${aggregate}`);
}
return result;
}


// Converts to a POSIX-style path
// eslint-disable-next-line id-length
function posixPath (p) {
Expand Down

0 comments on commit 35f5ec7

Please sign in to comment.