Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 25, 2024
1 parent ba955be commit ff93253
Show file tree
Hide file tree
Showing 19 changed files with 59,372 additions and 88 deletions.
2 changes: 1 addition & 1 deletion lib/markdownlint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import nodeFs from "node:fs";
import { createRequire } from "node:module";
import os from "node:os";
const dynamicRequire = createRequire(import.meta.url);
import os from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { getMarkdownItTokens } from "./markdownit.cjs";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "./lib/markdownlint.mjs",
"exports": {
".": "./lib/markdownlint.mjs",
"./helpers": "./helpers/helpers.js",
"./helpers": "./helpers/helpers.cjs",
"./style/all": "./style/all.json",
"./style/cirosantilli": "./style/cirosantilli.json",
"./style/prettier": "./style/prettier.json",
Expand Down
19 changes: 19 additions & 0 deletions test/esm-helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @ts-check

import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

// Shims import.meta.filename on Node 18
// eslint-disable-next-line no-underscore-dangle
export const __filename = (meta) => fileURLToPath(meta.url);

// Shims import.meta.dirname on Node 18
// eslint-disable-next-line no-underscore-dangle
export const __dirname = (meta) => path.dirname(__filename(meta));

// Avoids "ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time"
export const importWithTypeJson = async (file) => (
// @ts-ignore
JSON.parse(await fs.readFile(path.resolve(__dirname(import.meta), file)))
);
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @ts-check

"use strict";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
import os from "node:os";
import path from "node:path";
import test from "ava";
import markdownlint from"../lib/markdownlint.mjs";
import { __dirname } from "./esm-helpers.mjs";

const os = require("node:os");
const path = require("node:path");
const test = require("ava").default;
const markdownlint = require("../lib/markdownlint");

const sameFileSystem = (path.relative(os.homedir(), __dirname) !== __dirname);
const sameFileSystem = (path.relative(os.homedir(), __dirname(import.meta)) !== __dirname(import.meta));

test("configSingle", (t) => new Promise((resolve) => {
t.plan(2);
Expand All @@ -22,7 +23,7 @@ test("configSingle", (t) => new Promise((resolve) => {

test("configAbsolute", (t) => new Promise((resolve) => {
t.plan(2);
markdownlint.readConfig(path.join(__dirname, "config", "config-child.json"),
markdownlint.readConfig(path.join(__dirname(import.meta), "config", "config-child.json"),
function callback(err, actual) {
t.falsy(err);
const expected = require("./config/config-child.json");
Expand Down Expand Up @@ -268,7 +269,7 @@ test("configSingleSync", (t) => {
test("configAbsoluteSync", (t) => {
t.plan(1);
const actual = markdownlint.readConfigSync(
path.join(__dirname, "config", "config-child.json"));
path.join(__dirname(import.meta), "config", "config-child.json"));
const expected = require("./config/config-child.json");
t.deepEqual(actual, expected, "Config object not correct.");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @ts-check

"use strict";

const test = require("ava").default;
const markdownlint = require("../lib/markdownlint");
import test from "ava";
import markdownlint from "../lib/markdownlint.mjs";

test("applyFix", (t) => {
t.plan(4);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// @ts-check

"use strict";

const os = require("node:os");
const path = require("node:path");
const test = require("ava").default;
const helpers = require("../helpers");
const libMarkdownlint = require("../lib/markdownlint");
import os from "node:os";
import path from "node:path";
import test from "ava";
import helpers from "../helpers/helpers.cjs";
import libMarkdownlint from "../lib/markdownlint.mjs";
const { markdownlint } = libMarkdownlint.promises;
const { forEachInlineCodeSpan } = require("../lib/markdownit.cjs");
import { forEachInlineCodeSpan } from "../lib/markdownit.cjs";
import { getReferenceLinkImageData } from "../lib/cache.mjs";

test("clearHtmlCommentTextValid", (t) => {
t.plan(1);
Expand Down Expand Up @@ -397,7 +396,6 @@ test("getReferenceLinkImageData().shortcuts", (t) => {
"parser": "none",
"function":
() => {
const { getReferenceLinkImageData } = require("../lib/cache");
const { shortcuts } = getReferenceLinkImageData();
t.is(shortcuts.size, 0, [ ...shortcuts.keys() ].join(", "));
}
Expand Down
4 changes: 2 additions & 2 deletions test/markdownlint-test-micromark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import fs from "node:fs/promises";
import test from "ava";
import { newLineRe } from "../helpers/helpers.js";
import { newLineRe } from "../helpers/helpers.cjs";
import { filterByPredicate, filterByTypes } from "../helpers/micromark-helpers.cjs";
import { getEvents, parse } from "../helpers/micromark-parse.cjs";
import { getEvents, parse } from "../helpers/micromark-parse.mjs";

const testContent = new Promise((resolve, reject) => {
fs
Expand Down
9 changes: 4 additions & 5 deletions test/markdownlint-test-result-object.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// @ts-check

"use strict";

const test = require("ava").default;
const packageJson = require("../package.json");
const markdownlint = require("../lib/markdownlint");
import test from "ava";
import markdownlint from "../lib/markdownlint.mjs";
import { importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson("../package.json");
const homepage = packageJson.homepage;
const version = packageJson.version;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// @ts-check

"use strict";

const fs = require("node:fs").promises;
const path = require("node:path");
const test = require("ava").default;
const libMarkdownlint = require("../lib/markdownlint");
import fs from "node:fs/promises";
import path from "node:path";
import test from "ava";
import libMarkdownlint from "../lib/markdownlint.mjs";
const { applyFixes, promises } = libMarkdownlint;
const { markdownlint } = promises;
const helpers = require("../helpers");
const constants = require("../lib/constants");
import helpers from "../helpers/helpers.cjs";
import { fixableRuleNames } from "../lib/constants.mjs";

const numericalSortCompareFn = (a, b) => a - b;

Expand Down Expand Up @@ -75,7 +73,7 @@ function createTestForFile(file) {
indices.push(error.lineNumber);
}
t.true(
!error.fixInfo || constants.fixableRuleNames.includes(rule),
!error.fixInfo || fixableRuleNames.includes(rule),
`Fixable rule ${rule} is not tagged as such.`
);
}
Expand Down Expand Up @@ -104,8 +102,7 @@ function createTestForFile(file) {
);
}

const files = require("node:fs")
.readdirSync("./test")
const files = (await fs.readdir("./test"))
.filter((file) => /\.md$/.test(file));
for (const file of files) {
// @ts-ignore
Expand Down
87 changes: 47 additions & 40 deletions test/markdownlint-test.js → test/markdownlint-test.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// @ts-check

"use strict";

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");
const pluginSub = require("markdown-it-sub");
const pluginSup = require("markdown-it-sup");
const test = require("ava").default;
const { "exports": packageExports, homepage, name, version } = require("../package.json");
const markdownlint = require("../lib/markdownlint");
const constants = require("../lib/constants");
const rules = require("../lib/rules");
const customRules = require("./rules/rules.js");
const configSchema = require("../schema/markdownlint-config-schema.json");
const configSchemaStrict = require("../schema/markdownlint-config-schema-strict.json");
import fs from "node:fs";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
import path from "node:path";
import Ajv from "ajv";
import jsoncParser from "jsonc-parser";
import jsYaml from "js-yaml";
import markdownIt from "markdown-it";
import pluginInline from "markdown-it-for-inline";
import pluginSub from "markdown-it-sub";
import pluginSup from "markdown-it-sup";
import test from "ava";
import markdownlint from "../lib/markdownlint.mjs";
import * as constants from "../lib/constants.mjs";
import rules from "../lib/rules.mjs";
import customRules from "./rules/rules.cjs";
import { __dirname, importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson("../package.json");
const configSchema = await importWithTypeJson("../schema/markdownlint-config-schema.json");
const configSchemaStrict = await importWithTypeJson("../schema/markdownlint-config-schema-strict.json");

const deprecatedRuleNames = new Set(constants.deprecatedRuleNames);
const ajvOptions = {
Expand Down Expand Up @@ -82,7 +83,7 @@ test("projectFiles", (t) => {
return import("globby")
.then((module) => module.globby(projectFiles))
.then((files) => {
t.is(files.length, 61);
t.is(files.length, 60);
const options = {
files,
"config": require("../.markdownlint.json")
Expand All @@ -109,7 +110,7 @@ test("projectFilesExtendedAscii", (t) => {
"!doc/md036.md"
]))
.then((files) => {
t.is(files.length, 57);
t.is(files.length, 56);
const options = {
files,
"config": require("../.markdownlint.json"),
Expand Down Expand Up @@ -450,7 +451,7 @@ test("styleFiles", async(t) => {
t.truthy(require(path.join("../style", file)), "Unable to load/parse.");
const exportValue = `./style/${file}`;
const exportKey = exportValue.replace(/\.json$/, "");
t.is(packageExports[exportKey], exportValue);
t.is(packageJson.exports[exportKey], exportValue);
}
});

Expand Down Expand Up @@ -884,7 +885,7 @@ test("readme", async(t) => {
let seenTags = false;
let inTags = false;
// @ts-ignore
for (const token of md.parse(contents, {})) {
for (const token of markdownIt().parse(contents, {})) {
if (
(token.type === "bullet_list_open") &&
(token.level === 0)
Expand Down Expand Up @@ -1035,14 +1036,14 @@ test("validateConfigExampleJson", (t) => {
const validateSchema = ajv.compile(configSchema);
t.is(
configSchema.$id.replace(/^.*\/v(?<ver>\d+\.\d+\.\d+)\/.*$/u, "$<ver>"),
version
packageJson.version
);
t.is(configSchema.$id, configSchema.properties.$schema.default);

// Validate JSONC
const fileJson = ".markdownlint.jsonc";
const dataJson = fs.readFileSync(
path.join(__dirname, "../schema", fileJson),
path.join(__dirname(import.meta), "../schema", fileJson),
"utf8"
);
const jsonObject = jsoncParser.parse(dataJson);
Expand All @@ -1055,7 +1056,7 @@ test("validateConfigExampleJson", (t) => {
// Validate YAML
const fileYaml = ".markdownlint.yaml";
const dataYaml = fs.readFileSync(
path.join(__dirname, "../schema", fileYaml),
path.join(__dirname(import.meta), "../schema", fileYaml),
"utf8"
);
const yamlObject = jsYaml.load(dataYaml);
Expand All @@ -1074,7 +1075,7 @@ test("allBuiltInRulesHaveValidUrl", (t) => {
t.is(
// @ts-ignore
rule.information.href,
`${homepage}/blob/v${version}/doc/${ruleName}.md`
`${packageJson.homepage}/blob/v${packageJson.version}/doc/${ruleName}.md`
);
}
});
Expand All @@ -1087,12 +1088,12 @@ test("someCustomRulesHaveValidUrl", (t) => {
if (rule === customRules.anyBlockquote) {
t.is(
rule.information?.href,
`${homepage}/blob/main/test/rules/any-blockquote.js`
`${packageJson.homepage}/blob/main/test/rules/any-blockquote.js`
);
} else if (rule === customRules.lettersEX) {
t.is(
rule.information?.href,
`${homepage}/blob/main/test/rules/letters-E-X.js`
`${packageJson.homepage}/blob/main/test/rules/letters-E-X.js`
);
}
}
Expand Down Expand Up @@ -1351,21 +1352,21 @@ test("configParsersTOML", async(t) => {
test("getVersion", (t) => {
t.plan(1);
const actual = markdownlint.getVersion();
const expected = version;
const expected = packageJson.version;
t.is(actual, expected, "Version string not correct.");
});

test("constants", (t) => {
t.plan(2);
// @ts-ignore
t.is(constants.homepage, homepage);
t.is(constants.homepage, packageJson.homepage);
// @ts-ignore
t.is(constants.version, version);
t.is(constants.version, packageJson.version);
});

const exportMappings = new Map([
[ ".", "../lib/markdownlint.js" ],
[ "./helpers", "../helpers/helpers.js" ],
[ ".", "../lib/markdownlint.mjs" ],
[ "./helpers", "../helpers/helpers.cjs" ],
[ "./style/all", "../style/all.json" ],
[ "./style/cirosantilli", "../style/cirosantilli.json" ],
[ "./style/prettier", "../style/prettier.json" ],
Expand All @@ -1374,16 +1375,22 @@ const exportMappings = new Map([

test("exportMappings", (t) => {
t.deepEqual(
Object.keys(packageExports),
Object.keys(packageJson.exports),
[ ...exportMappings.keys() ]
);
});

// const commonJsRe = /\.js$/u;
const jsonRe = /\.json$/u;
const importOptionsJson = { "with": { "type": "json" } };

for (const [ exportName, exportPath ] of exportMappings) {
test(exportName, (t) => {
t.is(
require(exportName.replace(/^\./u, name)),
require(exportPath)
);
test(exportName, async (t) => {
// const commonJs = !commonJsRe.test(exportPath);
const json = jsonRe.test(exportPath);
const importOptions = json ? importOptionsJson : undefined;
const importExportName = await import(exportName.replace(/^\./u, packageJson.name), importOptions);
const importExportPath = await import(exportPath, importOptions);
t.is(importExportName, importExportPath);
});
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ff93253

Please sign in to comment.