From 9c89ee7676f91dc8273133efcd4648d24211775b Mon Sep 17 00:00:00 2001 From: David Anson Date: Thu, 9 Jan 2025 20:45:12 -0800 Subject: [PATCH] wip --- eslint.config.mjs | 1 + markdownlint-cli2.mjs | 6 ++--- package.json | 4 +-- resolve-module.mjs => resolve-module.cjs | 10 +++++--- test/markdownlint-cli2-test.mjs | 2 +- test/resolve-module-test.mjs | 31 +++--------------------- 6 files changed, 16 insertions(+), 38 deletions(-) rename resolve-module.mjs => resolve-module.cjs (60%) diff --git a/eslint.config.mjs b/eslint.config.mjs index da8ca72..ab87978 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -65,6 +65,7 @@ export default [ { "files": [ "**/*-formatter-*.js", + "resolve-module.cjs", "webworker/*.cjs" ], "languageOptions": { diff --git a/markdownlint-cli2.mjs b/markdownlint-cli2.mjs index 6e287be..43b6a52 100755 --- a/markdownlint-cli2.mjs +++ b/markdownlint-cli2.mjs @@ -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; @@ -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"; @@ -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 = diff --git a/package.json b/package.json index b90935c..930dc45 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" }, diff --git a/resolve-module.mjs b/resolve-module.cjs similarity index 60% rename from resolve-module.mjs rename to resolve-module.cjs index 13c87ac..a0d9413 100644 --- a/resolve-module.mjs +++ b/resolve-module.cjs @@ -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; diff --git a/test/markdownlint-cli2-test.mjs b/test/markdownlint-cli2-test.mjs index 2f8dd6a..65ccb32 100644 --- a/test/markdownlint-cli2-test.mjs +++ b/test/markdownlint-cli2-test.mjs @@ -79,7 +79,7 @@ test("validateMarkdownlintConfigSchema", async (t) => { const validateConfigSchema = ajv.compile(markdownlintConfigSchemaDefinition); t.is( markdownlintConfigSchemaDefinition.$id.replace(schemaIdVersionRe, "$"), - packageJson.dependencies.markdownlint + "0.37.3" ); t.is( markdownlintConfigSchemaDefinition.$id, diff --git a/test/resolve-module-test.mjs b/test/resolve-module-test.mjs index 2db0f54..f871d9a 100644 --- a/test/resolve-module-test.mjs +++ b/test/resolve-module-test.mjs @@ -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); @@ -12,7 +12,7 @@ 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) ]) ); }); @@ -20,7 +20,7 @@ test("locally-installed module", (t) => { t.plan(1); t.deepEqual( require.resolve("micromatch"), - resolve(require, "micromatch", [ __dirname(import.meta) ]) + resolve("micromatch", [ __dirname(import.meta) ]) ); }); @@ -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) ] ) @@ -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") ] ) @@ -80,12 +59,11 @@ 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 ) @@ -93,7 +71,6 @@ test("module local, relative, and in alternate node_modules", (t) => { t.deepEqual( require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), resolve( - require, "markdownlint-rule-sample-commonjs", dirs )