Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 18, 2024
1 parent 24a6926 commit b368049
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 62 deletions.
5 changes: 1 addition & 4 deletions append-to-array.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check

"use strict";

const sliceSize = 1000;

/**
Expand All @@ -21,5 +19,4 @@ const appendToArray = (destination, source) => {
}
};

appendToArray.sliceSize = sliceSize;
module.exports = appendToArray;
export { appendToArray as default, sliceSize };
5 changes: 2 additions & 3 deletions export-markdownlint-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-check

"use strict";

module.exports = require("markdownlint/helpers");
import * as helpers from "markdownlint/helpers";
export default helpers;
5 changes: 2 additions & 3 deletions export-markdownlint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-check

"use strict";

module.exports = require("markdownlint");
import * as markdownlint from "markdownlint";
export default markdownlint;
8 changes: 3 additions & 5 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

// @ts-check

"use strict";

// @ts-ignore
// eslint-disable-next-line camelcase, no-inline-comments, no-undef
const dynamicRequire = (typeof __non_webpack_require__ === "undefined") ? require : /* c8 ignore next */ __non_webpack_require__;
Expand All @@ -24,9 +22,9 @@ const {
"extendConfig": markdownlintExtendConfig,
"readConfig": markdownlintReadConfig
} = markdownlintPromises;
const appendToArray = require("./append-to-array");
const mergeOptions = require("./merge-options");
const resolveAndRequire = require("./resolve-and-require");
import appendToArray from "./append-to-array.js";
import mergeOptions from "./merge-options.js";
import resolveAndRequire from "./resolve-and-require.js";

// Variables
const packageName = "markdownlint-cli2";
Expand Down
4 changes: 1 addition & 3 deletions merge-options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check

"use strict";

/**
* Merges two options objects by combining config and replacing properties.
* @param {object} first First options object.
Expand All @@ -24,4 +22,4 @@ const mergeOptions = (first, second) => {
return merged;
};

module.exports = mergeOptions;
export default mergeOptions;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://dlaa.me/"
},
"license": "MIT",
"type": "commonjs",
"type": "module",
"main": "./markdownlint-cli2.js",
"exports": {
".": "./markdownlint-cli2.js",
Expand Down
6 changes: 2 additions & 4 deletions parsers/jsonc-parse.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check

"use strict";

const { parse, printParseErrorCode } = require("jsonc-parser");
import { parse, printParseErrorCode } from "jsonc-parser";

/**
* Parses a JSONC string, returning the corresponding object.
Expand All @@ -21,4 +19,4 @@ const jsoncParse = (text) => {
return result;
};

module.exports = jsoncParse;
export default jsoncParse;
8 changes: 3 additions & 5 deletions parsers/parsers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @ts-check

"use strict";

const jsoncParse = require("./jsonc-parse");
const yamlParse = require("./yaml-parse");
import jsoncParse from "./jsonc-parse.js";
import yamlParse from "./yaml-parse";

/**
* Array of parser objects ordered by priority.
Expand All @@ -13,4 +11,4 @@ const parsers = [
yamlParse
];

module.exports = parsers;
export default parsers;
6 changes: 2 additions & 4 deletions parsers/yaml-parse.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check

"use strict";

const yaml = require("js-yaml");
import yaml from "js-yaml";

/**
* Parses a YAML string, returning the corresponding object.
Expand All @@ -11,4 +9,4 @@ const yaml = require("js-yaml");
*/
const yamlParse = (text) => yaml.load(text);

module.exports = yamlParse;
export default yamlParse;
4 changes: 1 addition & 3 deletions resolve-and-require.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check

"use strict";

/**
* Wrapper for calling Node's require.resolve/require with an additional path.
* @param {object} req Node's require implementation (or equivalent).
Expand All @@ -16,4 +14,4 @@ const resolveAndRequire = (req, id, dirs) => {
return req(resolved);
};

module.exports = resolveAndRequire;
export default resolveAndRequire;
7 changes: 2 additions & 5 deletions test/append-to-array-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// @ts-check

"use strict";

const test = require("ava").default;
const appendToArray = require("../append-to-array");
const { sliceSize } = appendToArray;
import test from "ava";
import appendToArray, { sliceSize } from "../append-to-array.js";

const makeArray = (minimum, maximum) => {
const length = maximum - minimum + 1;
Expand Down
13 changes: 7 additions & 6 deletions test/markdownlint-cli2-test-exports.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-check

"use strict";
import test from "ava";
import packageJson from "../package.json" with { type: "json" };

const test = require("ava").default;
const packageJson = require("../package.json");
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

const exportMappings = new Map([
[ ".", ".." ],
Expand All @@ -22,10 +23,10 @@ test("exportMappings", (t) => {
});

for (const [ exportName, exportPath ] of exportMappings) {
test(exportName, (t) => {
test(exportName, async (t) => {
t.is(
require(exportName.replace(/^\./u, packageJson.name)),
require(exportPath)
await import(exportName.replace(/^\./u, packageJson.name)),
await import(exportPath)
);
});
}
6 changes: 2 additions & 4 deletions test/merge-options-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @ts-check

"use strict";

const test = require("ava").default;
const mergeOptions = require("../merge-options");
import test from "ava";
import mergeOptions from "../merge-options.js";

test("null/null", (t) => {
t.plan(1);
Expand Down
25 changes: 13 additions & 12 deletions test/resolve-and-require-test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
// @ts-check

"use strict";
import test from "ava";
import path from "node:path";
import resolveAndRequire from "../resolve-and-require.js";

const test = require("ava").default;
const path = require("node:path");
const resolveAndRequire = require("../resolve-and-require");
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

/* eslint-disable n/no-missing-require */

test("built-in module", (t) => {
t.plan(1);
t.deepEqual(
require("node:fs"),
resolveAndRequire(require, "fs", [ __dirname ])
resolveAndRequire(require, "fs", [ import.meta.dirname ])
);
});

test("locally-installed module", (t) => {
t.plan(1);
t.deepEqual(
require("markdownlint"),
resolveAndRequire(require, "markdownlint", [ __dirname ])
resolveAndRequire(require, "markdownlint", [ import.meta.dirname ])
);
});

test("relative (to __dirname) path to module", (t) => {
test("relative (to import.meta.dirname) path to module", (t) => {
t.plan(1);
t.deepEqual(
require("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolveAndRequire(
require,
"./customRules/node_modules/markdownlint-rule-sample-commonjs",
[ __dirname ]
[ import.meta.dirname ]
)
);
});
Expand All @@ -48,7 +49,7 @@ test("module in alternate node_modules", (t) => {
resolveAndRequire(
require,
"markdownlint-rule-sample-commonjs",
[ path.join(__dirname, "customRules") ]
[ path.join(import.meta.dirname, "customRules") ]
)
);
});
Expand All @@ -67,16 +68,16 @@ test("module in alternate node_modules and no require.resolve.paths", (t) => {
resolveAndRequire(
require,
"markdownlint-rule-sample-commonjs",
[ path.join(__dirname, "customRules") ]
[ path.join(import.meta.dirname, "customRules") ]
)
);
});

test("module local, relative, and in alternate node_modules", (t) => {
t.plan(3);
const dirs = [
__dirname,
path.join(__dirname, "customRules")
import.meta.dirname,
path.join(import.meta.dirname, "customRules")
];
t.deepEqual(
require("markdownlint"),
Expand Down

0 comments on commit b368049

Please sign in to comment.