diff --git a/package.json b/package.json index 265ea820..ac28610e 100644 --- a/package.json +++ b/package.json @@ -85,12 +85,12 @@ "globby": "14.0.0", "js-yaml": "4.1.0", "json-schema-to-typescript": "13.1.2", + "jsonc-parser": "3.2.0", "markdown-it-for-inline": "2.0.1", "markdown-it-sub": "2.0.0", "markdown-it-sup": "2.0.0", "markdownlint-rule-helpers": "0.24.0", "npm-run-all": "4.1.5", - "strip-json-comments": "5.0.1", "terser-webpack-plugin": "5.3.10", "toml": "3.0.0", "typescript": "5.3.3", diff --git a/test/markdownlint-test-repos-mdn-content.js b/test/markdownlint-test-repos-mdn-content.js index 1af6a5d6..57f6ce7d 100644 --- a/test/markdownlint-test-repos-mdn-content.js +++ b/test/markdownlint-test-repos-mdn-content.js @@ -9,6 +9,6 @@ const { lintTestRepo } = require("./markdownlint-test-repos"); test("https://github.com/mdn/content", (t) => { const rootDir = "./test-repos/mdn-content"; const globPatterns = [ join(rootDir, "**/*.md") ]; - const configPath = join(rootDir, ".markdownlint-cli2.jsonc"); + const configPath = join(rootDir, ".markdownlint.jsonc"); return lintTestRepo(t, globPatterns, configPath); }); diff --git a/test/markdownlint-test-repos.js b/test/markdownlint-test-repos.js index 436dfcb2..3a0831c5 100644 --- a/test/markdownlint-test-repos.js +++ b/test/markdownlint-test-repos.js @@ -3,6 +3,7 @@ "use strict"; const { join } = require("node:path").posix; +const jsoncParser = require("jsonc-parser"); const jsYaml = require("js-yaml"); const markdownlint = require("../lib/markdownlint"); @@ -17,9 +18,8 @@ const markdownlint = require("../lib/markdownlint"); async function lintTestRepo(t, globPatterns, configPath) { t.plan(1); const { globby } = await import("globby"); - const { "default": stripJsonComments } = await import("strip-json-comments"); const jsoncParse = (json) => { - const config = JSON.parse(stripJsonComments(json)); + const config = jsoncParser.parse(json, [], { "allowTrailingComma": true }); return config.config || config; }; const yamlParse = (yaml) => jsYaml.load(yaml); diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index bc30202e..9dbb3608 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -5,6 +5,7 @@ const fs = require("node:fs"); const path = require("node:path"); const Ajv = require("ajv"); +const jsoncParser = require("jsonc-parser"); const jsYaml = require("js-yaml"); const md = require("markdown-it")(); const pluginInline = require("markdown-it-for-inline"); @@ -997,9 +998,8 @@ test("validateConfigSchemaAppliesToUnknownProperties", (t) => { } }); -test("validateConfigExampleJson", async(t) => { +test("validateConfigExampleJson", (t) => { t.plan(4); - const { "default": stripJsonComments } = await import("strip-json-comments"); // Validate schema // @ts-ignore @@ -1018,7 +1018,7 @@ test("validateConfigExampleJson", async(t) => { path.join(__dirname, "../schema", fileJson), "utf8" ); - const jsonObject = JSON.parse(stripJsonComments(dataJson)); + const jsonObject = jsoncParser.parse(dataJson); const result = validateSchema(jsonObject); t.truthy( result, @@ -1218,7 +1218,6 @@ test("configParsersJSON", async(t) => { test("configParsersJSONC", async(t) => { t.plan(1); - const { "default": stripJsonComments } = await import("strip-json-comments"); const options = { "strings": { "content": [ @@ -1233,7 +1232,7 @@ test("configParsersJSONC", async(t) => { "" ].join("\n") }, - "configParsers": [ (content) => JSON.parse(stripJsonComments(content)) ] + "configParsers": [ jsoncParser.parse ] }; const actual = await markdownlint.promises.markdownlint(options); t.is(actual.toString(), "", "Unexpected results."); @@ -1262,7 +1261,6 @@ test("configParsersYAML", async(t) => { test("configParsersTOML", async(t) => { t.plan(1); - const { "default": stripJsonComments } = await import("strip-json-comments"); const options = { "strings": { "content": [ @@ -1276,7 +1274,7 @@ test("configParsersTOML", async(t) => { ].join("\n") }, "configParsers": [ - (content) => JSON.parse(stripJsonComments(content)), + jsoncParser.parse, require("toml").parse ] }; diff --git a/test/rules/validate-json.js b/test/rules/validate-json.js index 67767ca6..16a582b9 100644 --- a/test/rules/validate-json.js +++ b/test/rules/validate-json.js @@ -3,23 +3,26 @@ "use strict"; const { filterTokens } = require("markdownlint-rule-helpers"); +const { parse, printParseErrorCode } = require("jsonc-parser"); module.exports = { "names": [ "validate-json" ], "description": "Rule that validates JSON code", "tags": [ "test", "validate", "json" ], "asynchronous": true, - "function": async(params, onError) => { - const { "default": stripJsonComments } = - await import("strip-json-comments"); + "function": (params, onError) => { filterTokens(params, "fence", (fence) => { if (/jsonc?/i.test(fence.info)) { - try { - JSON.parse(stripJsonComments(fence.content)); - } catch (error) { + const errors = []; + parse(fence.content, errors); + if (errors.length > 0) { + const detail = errors.map( + (err) => `${printParseErrorCode(err.error)} (offset ${err.offset}, length ${err.length})` + ).join(", "); onError({ + // @ts-ignore "lineNumber": fence.lineNumber, - "detail": error.message + detail }); } } diff --git a/test/snapshots/markdownlint-test-repos-small.js.md b/test/snapshots/markdownlint-test-repos-small.js.md index 2b53d749..06573533 100644 --- a/test/snapshots/markdownlint-test-repos-small.js.md +++ b/test/snapshots/markdownlint-test-repos-small.js.md @@ -174,20 +174,7 @@ Generated by [AVA](https://avajs.dev). > Expected linting violations - `test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 361: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 362: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 363: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 364: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 365: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 366: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 367: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 376: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 377: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 378: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 379: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 380: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 381: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]␊ - test-repos/eslint-eslint/docs/src/extend/scope-manager-interface.md: 382: MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]` + '' ## https://github.com/mkdocs/mkdocs diff --git a/test/snapshots/markdownlint-test-repos-small.js.snap b/test/snapshots/markdownlint-test-repos-small.js.snap index ff65db30..4d28e8f0 100644 Binary files a/test/snapshots/markdownlint-test-repos-small.js.snap and b/test/snapshots/markdownlint-test-repos-small.js.snap differ