Replace strip-json-comments dev dependency with jsonc-parser to handle trailing comments in JSONC test input (for mdn/content), update test repo snapshots.

This commit is contained in:
David Anson 2024-01-21 20:14:18 -08:00
parent 555079da6d
commit 1769f70845
7 changed files with 20 additions and 32 deletions

View file

@ -85,12 +85,12 @@
"globby": "14.0.0", "globby": "14.0.0",
"js-yaml": "4.1.0", "js-yaml": "4.1.0",
"json-schema-to-typescript": "13.1.2", "json-schema-to-typescript": "13.1.2",
"jsonc-parser": "3.2.0",
"markdown-it-for-inline": "2.0.1", "markdown-it-for-inline": "2.0.1",
"markdown-it-sub": "2.0.0", "markdown-it-sub": "2.0.0",
"markdown-it-sup": "2.0.0", "markdown-it-sup": "2.0.0",
"markdownlint-rule-helpers": "0.24.0", "markdownlint-rule-helpers": "0.24.0",
"npm-run-all": "4.1.5", "npm-run-all": "4.1.5",
"strip-json-comments": "5.0.1",
"terser-webpack-plugin": "5.3.10", "terser-webpack-plugin": "5.3.10",
"toml": "3.0.0", "toml": "3.0.0",
"typescript": "5.3.3", "typescript": "5.3.3",

View file

@ -9,6 +9,6 @@ const { lintTestRepo } = require("./markdownlint-test-repos");
test("https://github.com/mdn/content", (t) => { test("https://github.com/mdn/content", (t) => {
const rootDir = "./test-repos/mdn-content"; const rootDir = "./test-repos/mdn-content";
const globPatterns = [ join(rootDir, "**/*.md") ]; const globPatterns = [ join(rootDir, "**/*.md") ];
const configPath = join(rootDir, ".markdownlint-cli2.jsonc"); const configPath = join(rootDir, ".markdownlint.jsonc");
return lintTestRepo(t, globPatterns, configPath); return lintTestRepo(t, globPatterns, configPath);
}); });

View file

@ -3,6 +3,7 @@
"use strict"; "use strict";
const { join } = require("node:path").posix; const { join } = require("node:path").posix;
const jsoncParser = require("jsonc-parser");
const jsYaml = require("js-yaml"); const jsYaml = require("js-yaml");
const markdownlint = require("../lib/markdownlint"); const markdownlint = require("../lib/markdownlint");
@ -17,9 +18,8 @@ const markdownlint = require("../lib/markdownlint");
async function lintTestRepo(t, globPatterns, configPath) { async function lintTestRepo(t, globPatterns, configPath) {
t.plan(1); t.plan(1);
const { globby } = await import("globby"); const { globby } = await import("globby");
const { "default": stripJsonComments } = await import("strip-json-comments");
const jsoncParse = (json) => { const jsoncParse = (json) => {
const config = JSON.parse(stripJsonComments(json)); const config = jsoncParser.parse(json, [], { "allowTrailingComma": true });
return config.config || config; return config.config || config;
}; };
const yamlParse = (yaml) => jsYaml.load(yaml); const yamlParse = (yaml) => jsYaml.load(yaml);

View file

@ -5,6 +5,7 @@
const fs = require("node:fs"); const fs = require("node:fs");
const path = require("node:path"); const path = require("node:path");
const Ajv = require("ajv"); const Ajv = require("ajv");
const jsoncParser = require("jsonc-parser");
const jsYaml = require("js-yaml"); const jsYaml = require("js-yaml");
const md = require("markdown-it")(); const md = require("markdown-it")();
const pluginInline = require("markdown-it-for-inline"); const pluginInline = require("markdown-it-for-inline");
@ -997,9 +998,8 @@ test("validateConfigSchemaAppliesToUnknownProperties", (t) => {
} }
}); });
test("validateConfigExampleJson", async(t) => { test("validateConfigExampleJson", (t) => {
t.plan(4); t.plan(4);
const { "default": stripJsonComments } = await import("strip-json-comments");
// Validate schema // Validate schema
// @ts-ignore // @ts-ignore
@ -1018,7 +1018,7 @@ test("validateConfigExampleJson", async(t) => {
path.join(__dirname, "../schema", fileJson), path.join(__dirname, "../schema", fileJson),
"utf8" "utf8"
); );
const jsonObject = JSON.parse(stripJsonComments(dataJson)); const jsonObject = jsoncParser.parse(dataJson);
const result = validateSchema(jsonObject); const result = validateSchema(jsonObject);
t.truthy( t.truthy(
result, result,
@ -1218,7 +1218,6 @@ test("configParsersJSON", async(t) => {
test("configParsersJSONC", async(t) => { test("configParsersJSONC", async(t) => {
t.plan(1); t.plan(1);
const { "default": stripJsonComments } = await import("strip-json-comments");
const options = { const options = {
"strings": { "strings": {
"content": [ "content": [
@ -1233,7 +1232,7 @@ test("configParsersJSONC", async(t) => {
"" ""
].join("\n") ].join("\n")
}, },
"configParsers": [ (content) => JSON.parse(stripJsonComments(content)) ] "configParsers": [ jsoncParser.parse ]
}; };
const actual = await markdownlint.promises.markdownlint(options); const actual = await markdownlint.promises.markdownlint(options);
t.is(actual.toString(), "", "Unexpected results."); t.is(actual.toString(), "", "Unexpected results.");
@ -1262,7 +1261,6 @@ test("configParsersYAML", async(t) => {
test("configParsersTOML", async(t) => { test("configParsersTOML", async(t) => {
t.plan(1); t.plan(1);
const { "default": stripJsonComments } = await import("strip-json-comments");
const options = { const options = {
"strings": { "strings": {
"content": [ "content": [
@ -1276,7 +1274,7 @@ test("configParsersTOML", async(t) => {
].join("\n") ].join("\n")
}, },
"configParsers": [ "configParsers": [
(content) => JSON.parse(stripJsonComments(content)), jsoncParser.parse,
require("toml").parse require("toml").parse
] ]
}; };

View file

@ -3,23 +3,26 @@
"use strict"; "use strict";
const { filterTokens } = require("markdownlint-rule-helpers"); const { filterTokens } = require("markdownlint-rule-helpers");
const { parse, printParseErrorCode } = require("jsonc-parser");
module.exports = { module.exports = {
"names": [ "validate-json" ], "names": [ "validate-json" ],
"description": "Rule that validates JSON code", "description": "Rule that validates JSON code",
"tags": [ "test", "validate", "json" ], "tags": [ "test", "validate", "json" ],
"asynchronous": true, "asynchronous": true,
"function": async(params, onError) => { "function": (params, onError) => {
const { "default": stripJsonComments } =
await import("strip-json-comments");
filterTokens(params, "fence", (fence) => { filterTokens(params, "fence", (fence) => {
if (/jsonc?/i.test(fence.info)) { if (/jsonc?/i.test(fence.info)) {
try { const errors = [];
JSON.parse(stripJsonComments(fence.content)); parse(fence.content, errors);
} catch (error) { if (errors.length > 0) {
const detail = errors.map(
(err) => `${printParseErrorCode(err.error)} (offset ${err.offset}, length ${err.length})`
).join(", ");
onError({ onError({
// @ts-ignore
"lineNumber": fence.lineNumber, "lineNumber": fence.lineNumber,
"detail": error.message detail
}); });
} }
} }

View file

@ -174,20 +174,7 @@ Generated by [AVA](https://avajs.dev).
> Expected linting violations > 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 ## https://github.com/mkdocs/mkdocs