Skip to content

Commit

Permalink
Add trailing commas to JSONC test configuration, update use of jsonc-…
Browse files Browse the repository at this point in the history
…parser package to handle trailing commas and report all errors (reuses markdownlint-cli2 implementation).
  • Loading branch information
DavidAnson committed Jan 28, 2024
1 parent 7477055 commit 539c188
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ function posixPath(p) {
}

function jsoncParse(text) {
return JSON.parse(require('jsonc-parser').stripComments(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;
}

function jsYamlSafeLoad(text) {
function yamlParse(text) {
return require('js-yaml').load(text);
}

Expand All @@ -36,7 +44,7 @@ const exitCodes = {
};

const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
const configParsers = [jsoncParse, jsYamlSafeLoad];
const configParsers = [jsoncParse, yamlParse];
const fsOptions = {encoding: 'utf8'};
const processCwd = process.cwd();

Expand Down
4 changes: 2 additions & 2 deletions test/config-files/jsonc/.markdownlint.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
/*
* Block comment
*/
"punctuation": "$"
}
"punctuation": "$",
},
}

0 comments on commit 539c188

Please sign in to comment.