Skip to content

Commit 539c188

Browse files
committed
Add trailing commas to JSONC test configuration, update use of jsonc-parser package to handle trailing commas and report all errors (reuses markdownlint-cli2 implementation).
1 parent 7477055 commit 539c188

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

markdownlint.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ function posixPath(p) {
2121
}
2222

2323
function jsoncParse(text) {
24-
return JSON.parse(require('jsonc-parser').stripComments(text));
24+
const {parse, printParseErrorCode} = require('jsonc-parser');
25+
const errors = [];
26+
const result = parse(text, errors, {allowTrailingComma: true});
27+
if (errors.length > 0) {
28+
const aggregate = errors.map(error => `${printParseErrorCode(error.error)} (offset ${error.offset}, length ${error.length})`).join(', ');
29+
throw new Error(`Unable to parse JSON(C) content, ${aggregate}`);
30+
}
31+
32+
return result;
2533
}
2634

27-
function jsYamlSafeLoad(text) {
35+
function yamlParse(text) {
2836
return require('js-yaml').load(text);
2937
}
3038

@@ -36,7 +44,7 @@ const exitCodes = {
3644
};
3745

3846
const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
39-
const configParsers = [jsoncParse, jsYamlSafeLoad];
47+
const configParsers = [jsoncParse, yamlParse];
4048
const fsOptions = {encoding: 'utf8'};
4149
const processCwd = process.cwd();
4250

test/config-files/jsonc/.markdownlint.jsonc

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
/*
55
* Block comment
66
*/
7-
"punctuation": "$"
8-
}
7+
"punctuation": "$",
8+
},
99
}

0 commit comments

Comments
 (0)