Skip to content

Commit ff93253

Browse files
committed
wip
1 parent ba955be commit ff93253

19 files changed

+59372
-88
lines changed

lib/markdownlint.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import nodeFs from "node:fs";
44
import { createRequire } from "node:module";
5-
import os from "node:os";
65
const dynamicRequire = createRequire(import.meta.url);
6+
import os from "node:os";
77
import path from "node:path";
88
import { promisify } from "node:util";
99
import { getMarkdownItTokens } from "./markdownit.cjs";

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "./lib/markdownlint.mjs",
77
"exports": {
88
".": "./lib/markdownlint.mjs",
9-
"./helpers": "./helpers/helpers.js",
9+
"./helpers": "./helpers/helpers.cjs",
1010
"./style/all": "./style/all.json",
1111
"./style/cirosantilli": "./style/cirosantilli.json",
1212
"./style/prettier": "./style/prettier.json",

test/esm-helpers.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
3+
import fs from "node:fs/promises";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
7+
// Shims import.meta.filename on Node 18
8+
// eslint-disable-next-line no-underscore-dangle
9+
export const __filename = (meta) => fileURLToPath(meta.url);
10+
11+
// Shims import.meta.dirname on Node 18
12+
// eslint-disable-next-line no-underscore-dangle
13+
export const __dirname = (meta) => path.dirname(__filename(meta));
14+
15+
// Avoids "ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time"
16+
export const importWithTypeJson = async (file) => (
17+
// @ts-ignore
18+
JSON.parse(await fs.readFile(path.resolve(__dirname(import.meta), file)))
19+
);

test/markdownlint-test-config.js renamed to test/markdownlint-test-config.mjs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// @ts-check
22

3-
"use strict";
3+
import { createRequire } from "node:module";
4+
const require = createRequire(import.meta.url);
5+
import os from "node:os";
6+
import path from "node:path";
7+
import test from "ava";
8+
import markdownlint from"../lib/markdownlint.mjs";
9+
import { __dirname } from "./esm-helpers.mjs";
410

5-
const os = require("node:os");
6-
const path = require("node:path");
7-
const test = require("ava").default;
8-
const markdownlint = require("../lib/markdownlint");
9-
10-
const sameFileSystem = (path.relative(os.homedir(), __dirname) !== __dirname);
11+
const sameFileSystem = (path.relative(os.homedir(), __dirname(import.meta)) !== __dirname(import.meta));
1112

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

2324
test("configAbsolute", (t) => new Promise((resolve) => {
2425
t.plan(2);
25-
markdownlint.readConfig(path.join(__dirname, "config", "config-child.json"),
26+
markdownlint.readConfig(path.join(__dirname(import.meta), "config", "config-child.json"),
2627
function callback(err, actual) {
2728
t.falsy(err);
2829
const expected = require("./config/config-child.json");
@@ -268,7 +269,7 @@ test("configSingleSync", (t) => {
268269
test("configAbsoluteSync", (t) => {
269270
t.plan(1);
270271
const actual = markdownlint.readConfigSync(
271-
path.join(__dirname, "config", "config-child.json"));
272+
path.join(__dirname(import.meta), "config", "config-child.json"));
272273
const expected = require("./config/config-child.json");
273274
t.deepEqual(actual, expected, "Config object not correct.");
274275
});

test/markdownlint-test-fixes.js renamed to test/markdownlint-test-fixes.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const test = require("ava").default;
6-
const markdownlint = require("../lib/markdownlint");
3+
import test from "ava";
4+
import markdownlint from "../lib/markdownlint.mjs";
75

86
test("applyFix", (t) => {
97
t.plan(4);

test/markdownlint-test-helpers.js renamed to test/markdownlint-test-helpers.mjs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const os = require("node:os");
6-
const path = require("node:path");
7-
const test = require("ava").default;
8-
const helpers = require("../helpers");
9-
const libMarkdownlint = require("../lib/markdownlint");
3+
import os from "node:os";
4+
import path from "node:path";
5+
import test from "ava";
6+
import helpers from "../helpers/helpers.cjs";
7+
import libMarkdownlint from "../lib/markdownlint.mjs";
108
const { markdownlint } = libMarkdownlint.promises;
11-
const { forEachInlineCodeSpan } = require("../lib/markdownit.cjs");
9+
import { forEachInlineCodeSpan } from "../lib/markdownit.cjs";
10+
import { getReferenceLinkImageData } from "../lib/cache.mjs";
1211

1312
test("clearHtmlCommentTextValid", (t) => {
1413
t.plan(1);
@@ -397,7 +396,6 @@ test("getReferenceLinkImageData().shortcuts", (t) => {
397396
"parser": "none",
398397
"function":
399398
() => {
400-
const { getReferenceLinkImageData } = require("../lib/cache");
401399
const { shortcuts } = getReferenceLinkImageData();
402400
t.is(shortcuts.size, 0, [ ...shortcuts.keys() ].join(", "));
403401
}

test/markdownlint-test-micromark.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import fs from "node:fs/promises";
44
import test from "ava";
5-
import { newLineRe } from "../helpers/helpers.js";
5+
import { newLineRe } from "../helpers/helpers.cjs";
66
import { filterByPredicate, filterByTypes } from "../helpers/micromark-helpers.cjs";
7-
import { getEvents, parse } from "../helpers/micromark-parse.cjs";
7+
import { getEvents, parse } from "../helpers/micromark-parse.mjs";
88

99
const testContent = new Promise((resolve, reject) => {
1010
fs

test/markdownlint-test-result-object.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const test = require("ava").default;
6-
const packageJson = require("../package.json");
7-
const markdownlint = require("../lib/markdownlint");
3+
import test from "ava";
4+
import markdownlint from "../lib/markdownlint.mjs";
5+
import { importWithTypeJson } from "./esm-helpers.mjs";
6+
const packageJson = await importWithTypeJson("../package.json");
87
const homepage = packageJson.homepage;
98
const version = packageJson.version;
109

test/markdownlint-test-scenarios.js renamed to test/markdownlint-test-scenarios.mjs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const fs = require("node:fs").promises;
6-
const path = require("node:path");
7-
const test = require("ava").default;
8-
const libMarkdownlint = require("../lib/markdownlint");
3+
import fs from "node:fs/promises";
4+
import path from "node:path";
5+
import test from "ava";
6+
import libMarkdownlint from "../lib/markdownlint.mjs";
97
const { applyFixes, promises } = libMarkdownlint;
108
const { markdownlint } = promises;
11-
const helpers = require("../helpers");
12-
const constants = require("../lib/constants");
9+
import helpers from "../helpers/helpers.cjs";
10+
import { fixableRuleNames } from "../lib/constants.mjs";
1311

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

@@ -75,7 +73,7 @@ function createTestForFile(file) {
7573
indices.push(error.lineNumber);
7674
}
7775
t.true(
78-
!error.fixInfo || constants.fixableRuleNames.includes(rule),
76+
!error.fixInfo || fixableRuleNames.includes(rule),
7977
`Fixable rule ${rule} is not tagged as such.`
8078
);
8179
}
@@ -104,8 +102,7 @@ function createTestForFile(file) {
104102
);
105103
}
106104

107-
const files = require("node:fs")
108-
.readdirSync("./test")
105+
const files = (await fs.readdir("./test"))
109106
.filter((file) => /\.md$/.test(file));
110107
for (const file of files) {
111108
// @ts-ignore

test/markdownlint-test.js renamed to test/markdownlint-test.mjs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const fs = require("node:fs");
6-
const path = require("node:path");
7-
const Ajv = require("ajv");
8-
const jsoncParser = require("jsonc-parser");
9-
const jsYaml = require("js-yaml");
10-
const md = require("markdown-it")();
11-
const pluginInline = require("markdown-it-for-inline");
12-
const pluginSub = require("markdown-it-sub");
13-
const pluginSup = require("markdown-it-sup");
14-
const test = require("ava").default;
15-
const { "exports": packageExports, homepage, name, version } = require("../package.json");
16-
const markdownlint = require("../lib/markdownlint");
17-
const constants = require("../lib/constants");
18-
const rules = require("../lib/rules");
19-
const customRules = require("./rules/rules.js");
20-
const configSchema = require("../schema/markdownlint-config-schema.json");
21-
const configSchemaStrict = require("../schema/markdownlint-config-schema-strict.json");
3+
import fs from "node:fs";
4+
import { createRequire } from "node:module";
5+
const require = createRequire(import.meta.url);
6+
import path from "node:path";
7+
import Ajv from "ajv";
8+
import jsoncParser from "jsonc-parser";
9+
import jsYaml from "js-yaml";
10+
import markdownIt from "markdown-it";
11+
import pluginInline from "markdown-it-for-inline";
12+
import pluginSub from "markdown-it-sub";
13+
import pluginSup from "markdown-it-sup";
14+
import test from "ava";
15+
import markdownlint from "../lib/markdownlint.mjs";
16+
import * as constants from "../lib/constants.mjs";
17+
import rules from "../lib/rules.mjs";
18+
import customRules from "./rules/rules.cjs";
19+
import { __dirname, importWithTypeJson } from "./esm-helpers.mjs";
20+
const packageJson = await importWithTypeJson("../package.json");
21+
const configSchema = await importWithTypeJson("../schema/markdownlint-config-schema.json");
22+
const configSchemaStrict = await importWithTypeJson("../schema/markdownlint-config-schema-strict.json");
2223

2324
const deprecatedRuleNames = new Set(constants.deprecatedRuleNames);
2425
const ajvOptions = {
@@ -82,7 +83,7 @@ test("projectFiles", (t) => {
8283
return import("globby")
8384
.then((module) => module.globby(projectFiles))
8485
.then((files) => {
85-
t.is(files.length, 61);
86+
t.is(files.length, 60);
8687
const options = {
8788
files,
8889
"config": require("../.markdownlint.json")
@@ -109,7 +110,7 @@ test("projectFilesExtendedAscii", (t) => {
109110
"!doc/md036.md"
110111
]))
111112
.then((files) => {
112-
t.is(files.length, 57);
113+
t.is(files.length, 56);
113114
const options = {
114115
files,
115116
"config": require("../.markdownlint.json"),
@@ -450,7 +451,7 @@ test("styleFiles", async(t) => {
450451
t.truthy(require(path.join("../style", file)), "Unable to load/parse.");
451452
const exportValue = `./style/${file}`;
452453
const exportKey = exportValue.replace(/\.json$/, "");
453-
t.is(packageExports[exportKey], exportValue);
454+
t.is(packageJson.exports[exportKey], exportValue);
454455
}
455456
});
456457

@@ -884,7 +885,7 @@ test("readme", async(t) => {
884885
let seenTags = false;
885886
let inTags = false;
886887
// @ts-ignore
887-
for (const token of md.parse(contents, {})) {
888+
for (const token of markdownIt().parse(contents, {})) {
888889
if (
889890
(token.type === "bullet_list_open") &&
890891
(token.level === 0)
@@ -1035,14 +1036,14 @@ test("validateConfigExampleJson", (t) => {
10351036
const validateSchema = ajv.compile(configSchema);
10361037
t.is(
10371038
configSchema.$id.replace(/^.*\/v(?<ver>\d+\.\d+\.\d+)\/.*$/u, "$<ver>"),
1038-
version
1039+
packageJson.version
10391040
);
10401041
t.is(configSchema.$id, configSchema.properties.$schema.default);
10411042

10421043
// Validate JSONC
10431044
const fileJson = ".markdownlint.jsonc";
10441045
const dataJson = fs.readFileSync(
1045-
path.join(__dirname, "../schema", fileJson),
1046+
path.join(__dirname(import.meta), "../schema", fileJson),
10461047
"utf8"
10471048
);
10481049
const jsonObject = jsoncParser.parse(dataJson);
@@ -1055,7 +1056,7 @@ test("validateConfigExampleJson", (t) => {
10551056
// Validate YAML
10561057
const fileYaml = ".markdownlint.yaml";
10571058
const dataYaml = fs.readFileSync(
1058-
path.join(__dirname, "../schema", fileYaml),
1059+
path.join(__dirname(import.meta), "../schema", fileYaml),
10591060
"utf8"
10601061
);
10611062
const yamlObject = jsYaml.load(dataYaml);
@@ -1074,7 +1075,7 @@ test("allBuiltInRulesHaveValidUrl", (t) => {
10741075
t.is(
10751076
// @ts-ignore
10761077
rule.information.href,
1077-
`${homepage}/blob/v${version}/doc/${ruleName}.md`
1078+
`${packageJson.homepage}/blob/v${packageJson.version}/doc/${ruleName}.md`
10781079
);
10791080
}
10801081
});
@@ -1087,12 +1088,12 @@ test("someCustomRulesHaveValidUrl", (t) => {
10871088
if (rule === customRules.anyBlockquote) {
10881089
t.is(
10891090
rule.information?.href,
1090-
`${homepage}/blob/main/test/rules/any-blockquote.js`
1091+
`${packageJson.homepage}/blob/main/test/rules/any-blockquote.js`
10911092
);
10921093
} else if (rule === customRules.lettersEX) {
10931094
t.is(
10941095
rule.information?.href,
1095-
`${homepage}/blob/main/test/rules/letters-E-X.js`
1096+
`${packageJson.homepage}/blob/main/test/rules/letters-E-X.js`
10961097
);
10971098
}
10981099
}
@@ -1351,21 +1352,21 @@ test("configParsersTOML", async(t) => {
13511352
test("getVersion", (t) => {
13521353
t.plan(1);
13531354
const actual = markdownlint.getVersion();
1354-
const expected = version;
1355+
const expected = packageJson.version;
13551356
t.is(actual, expected, "Version string not correct.");
13561357
});
13571358

13581359
test("constants", (t) => {
13591360
t.plan(2);
13601361
// @ts-ignore
1361-
t.is(constants.homepage, homepage);
1362+
t.is(constants.homepage, packageJson.homepage);
13621363
// @ts-ignore
1363-
t.is(constants.version, version);
1364+
t.is(constants.version, packageJson.version);
13641365
});
13651366

13661367
const exportMappings = new Map([
1367-
[ ".", "../lib/markdownlint.js" ],
1368-
[ "./helpers", "../helpers/helpers.js" ],
1368+
[ ".", "../lib/markdownlint.mjs" ],
1369+
[ "./helpers", "../helpers/helpers.cjs" ],
13691370
[ "./style/all", "../style/all.json" ],
13701371
[ "./style/cirosantilli", "../style/cirosantilli.json" ],
13711372
[ "./style/prettier", "../style/prettier.json" ],
@@ -1374,16 +1375,22 @@ const exportMappings = new Map([
13741375

13751376
test("exportMappings", (t) => {
13761377
t.deepEqual(
1377-
Object.keys(packageExports),
1378+
Object.keys(packageJson.exports),
13781379
[ ...exportMappings.keys() ]
13791380
);
13801381
});
13811382

1383+
// const commonJsRe = /\.js$/u;
1384+
const jsonRe = /\.json$/u;
1385+
const importOptionsJson = { "with": { "type": "json" } };
1386+
13821387
for (const [ exportName, exportPath ] of exportMappings) {
1383-
test(exportName, (t) => {
1384-
t.is(
1385-
require(exportName.replace(/^\./u, name)),
1386-
require(exportPath)
1387-
);
1388+
test(exportName, async (t) => {
1389+
// const commonJs = !commonJsRe.test(exportPath);
1390+
const json = jsonRe.test(exportPath);
1391+
const importOptions = json ? importOptionsJson : undefined;
1392+
const importExportName = await import(exportName.replace(/^\./u, packageJson.name), importOptions);
1393+
const importExportPath = await import(exportPath, importOptions);
1394+
t.is(importExportName, importExportPath);
13881395
});
13891396
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)