diff --git a/markdownlint.js b/markdownlint.js index 46b3cb8b..67aa1365 100755 --- a/markdownlint.js +++ b/markdownlint.js @@ -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); } @@ -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(); diff --git a/test/config-files/jsonc/.markdownlint.jsonc b/test/config-files/jsonc/.markdownlint.jsonc index 2636cd7d..8c8f220d 100644 --- a/test/config-files/jsonc/.markdownlint.jsonc +++ b/test/config-files/jsonc/.markdownlint.jsonc @@ -4,6 +4,6 @@ /* * Block comment */ - "punctuation": "$" - } + "punctuation": "$", + }, }