Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Jan 10, 2025
1 parent 7c796b1 commit 9c89ee7
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 38 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default [
{
"files": [
"**/*-formatter-*.js",
"resolve-module.cjs",
"webworker/*.cjs"
],
"languageOptions": {
Expand Down
6 changes: 2 additions & 4 deletions markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

// Imports
import fsNode from "node:fs";
import { createRequire } from "node:module";
const dynamicRequire = createRequire(import.meta.url);
import os from "node:os";
import pathDefault from "node:path";
const pathPosix = pathDefault.posix;
Expand All @@ -15,7 +13,7 @@ import { lint, extendConfig, readConfig } from "markdownlint/promise";
import { expandTildePath } from "markdownlint/helpers";
import appendToArray from "./append-to-array.mjs";
import mergeOptions from "./merge-options.mjs";
import resolveModule from "./resolve-module.mjs";
import resolveModule from "./resolve-module.cjs";
import parsers from "./parsers/parsers.mjs";
import jsoncParse from "./parsers/jsonc-parse.mjs";
import yamlParse from "./parsers/yaml-parse.mjs";
Expand Down Expand Up @@ -78,7 +76,7 @@ const importModule = async (dirOrDirs, id, noImport) => {
let moduleName = null;
try {
try {
moduleName = pathToFileURL(resolveModule(dynamicRequire, expandId, dirs));
moduleName = pathToFileURL(resolveModule(expandId, dirs));
} catch (error) {
errors.push(error);
moduleName =
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"parsers/jsonc-parse.mjs",
"parsers/yaml-parse.mjs",
"README.md",
"resolve-module.mjs",
"resolve-module.cjs",
"schema/markdownlint-cli2-config-schema.json",
"schema/markdownlint-config-schema.json",
"schema/ValidatingConfiguration.md"
Expand All @@ -77,7 +77,7 @@
"globby": "14.0.2",
"js-yaml": "4.1.0",
"jsonc-parser": "3.3.1",
"markdownlint": "0.37.3",
"markdownlint": "DavidAnson/markdownlint#vsc",
"markdownlint-cli2-formatter-default": "0.0.5",
"micromatch": "4.0.8"
},
Expand Down
10 changes: 6 additions & 4 deletions resolve-module.mjs → resolve-module.cjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// @ts-check

"use strict";

/**
* Wrapper for calling Node's require.resolve with additional paths.
* @param {object} require Node's require implementation (or equivalent).
* @param {string} request Module path to require.
* @param {string[]} paths Paths to resolve module location from.
* @returns {string} Resolved file name.
*/
const resolveModule = (require, request, paths) => {
const resolvePaths = require.resolve.paths ? require.resolve.paths("") : [];
const resolveModule = (request, paths) => {
// eslint-disable-next-line no-inline-comments
const resolvePaths = require.resolve.paths("") /* c8 ignore next */ || [];
const allPaths = [ ...paths, ...resolvePaths ];
return require.resolve(request, { "paths": allPaths });
};

export default resolveModule;
module.exports = resolveModule;
2 changes: 1 addition & 1 deletion test/markdownlint-cli2-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test("validateMarkdownlintConfigSchema", async (t) => {
const validateConfigSchema = ajv.compile(markdownlintConfigSchemaDefinition);
t.is(
markdownlintConfigSchemaDefinition.$id.replace(schemaIdVersionRe, "$<version>"),
packageJson.dependencies.markdownlint
"0.37.3"
);
t.is(
markdownlintConfigSchemaDefinition.$id,
Expand Down
31 changes: 4 additions & 27 deletions test/resolve-module-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import test from "ava";
import path from "node:path";
import { __dirname } from "./esm-helpers.mjs";
import resolve from "../resolve-module.mjs";
import resolve from "../resolve-module.cjs";

import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
Expand All @@ -12,15 +12,15 @@ test("built-in module", (t) => {
t.plan(1);
t.deepEqual(
require.resolve("node:fs"),
resolve(require, "node:fs", [ __dirname(import.meta) ])
resolve("node:fs", [ __dirname(import.meta) ])
);
});

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

Expand All @@ -29,7 +29,6 @@ test("relative (to __dirname(import.meta)) path to module", (t) => {
t.deepEqual(
require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolve(
require,
"./customRules/node_modules/markdownlint-rule-sample-commonjs",
[ __dirname(import.meta) ]
)
Expand All @@ -46,26 +45,6 @@ test("module in alternate node_modules", (t) => {
t.deepEqual(
require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolve(
require,
"markdownlint-rule-sample-commonjs",
[ path.join(__dirname(import.meta), "customRules") ]
)
);
});

test("module in alternate node_modules and no require.resolve.paths", (t) => {
t.plan(2);
// @ts-ignore
delete require.resolve.paths;
t.throws(
// @ts-ignore
() => require.resolve("markdownlint-rule-sample-commonjs"),
{ "code": "MODULE_NOT_FOUND" }
);
t.deepEqual(
require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolve(
require,
"markdownlint-rule-sample-commonjs",
[ path.join(__dirname(import.meta), "customRules") ]
)
Expand All @@ -80,20 +59,18 @@ test("module local, relative, and in alternate node_modules", (t) => {
];
t.deepEqual(
require.resolve("micromatch"),
resolve(require, "micromatch", dirs)
resolve("micromatch", dirs)
);
t.deepEqual(
require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolve(
require,
"./customRules/node_modules/markdownlint-rule-sample-commonjs",
dirs
)
);
t.deepEqual(
require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolve(
require,
"markdownlint-rule-sample-commonjs",
dirs
)
Expand Down

0 comments on commit 9c89ee7

Please sign in to comment.