From 539c18885c6764c268ad197218810b2afbbbf42c Mon Sep 17 00:00:00 2001 From: David Anson Date: Sun, 28 Jan 2024 21:50:48 +0000 Subject: [PATCH] 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). --- markdownlint.js | 14 +++++++++++--- test/config-files/jsonc/.markdownlint.jsonc | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/markdownlint.js b/markdownlint.js index 46b3cb8bb..67aa13654 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 2636cd7db..8c8f220dd 100644 --- a/test/config-files/jsonc/.markdownlint.jsonc +++ b/test/config-files/jsonc/.markdownlint.jsonc @@ -4,6 +4,6 @@ /* * Block comment */ - "punctuation": "$" - } + "punctuation": "$", + }, }