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",
"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",

View file

@ -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);
});

View file

@ -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);

View file

@ -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
]
};

View file

@ -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
});
}
}

View file

@ -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