diff --git a/markdownlint-cli2.mjs b/markdownlint-cli2.mjs index 46c3cb72..f3d046d9 100755 --- a/markdownlint-cli2.mjs +++ b/markdownlint-cli2.mjs @@ -1,6 +1,6 @@ // @ts-ignore -// Requires +// Imports import fsNode from "node:fs"; import { createRequire } from "node:module"; const dynamicRequire = createRequire(import.meta.url); @@ -15,7 +15,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 resolveAndRequire from "./resolve-and-require.mjs"; +import resolveModule from "./resolve-module.mjs"; import parsers from "./parsers/parsers.mjs"; import jsoncParse from "./parsers/jsonc-parse.mjs"; import yamlParse from "./parsers/yaml-parse.mjs"; @@ -65,73 +65,65 @@ const readConfigFile = (fs, dir, name, otherwise) => () => { ); }; -// Import or resolve/require a module ID with a custom directory in the path -const importOrRequireResolve = async (dirOrDirs, id, noRequire) => { - if (typeof id === "string") { - if (noRequire) { - return null; - } - const dirs = Array.isArray(dirOrDirs) ? dirOrDirs : [ dirOrDirs ]; - const expandId = expandTildePath(id, os); - const errors = []; - // Try to load via require(...) - try { - const isModule = /\.mjs$/iu.test(expandId); - if (!isModule) { - // Try not to use require for modules due to breaking change in Node 22.12: - // https://github.com/nodejs/node/releases/tag/v22.12.0 - return resolveAndRequire(dynamicRequire, expandId, dirs); - } - } catch (error) { - errors.push(error); - } - // Try to load via import(...) +// Import a module ID with a custom directory in the path +const importModule = async (dirOrDirs, id, noImport) => { + if (typeof id !== "string") { + return id; + } else if (noImport) { + return null; + } + const dirs = Array.isArray(dirOrDirs) ? dirOrDirs : [ dirOrDirs ]; + const expandId = expandTildePath(id, os); + const errors = []; + let moduleName = null; + try { try { - // eslint-disable-next-line n/no-unsupported-features/node-builtins - const isURL = !pathDefault.isAbsolute(expandId) && URL.canParse(expandId); - const urlString = ( - isURL ? new URL(expandId) : pathToFileURL(pathDefault.resolve(dirs[0], expandId)) - ).toString(); - // eslint-disable-next-line no-inline-comments - const module = await import(/* webpackIgnore: true */ urlString); - return module.default; + moduleName = pathToFileURL(resolveModule(dynamicRequire, expandId, dirs)); } catch (error) { errors.push(error); + moduleName = + // eslint-disable-next-line n/no-unsupported-features/node-builtins + (!pathDefault.isAbsolute(expandId) && URL.canParse(expandId)) + ? new URL(expandId) + : pathToFileURL(pathDefault.resolve(dirs[0], expandId)); } - // Give up + // eslint-disable-next-line no-inline-comments + const module = await import(/* webpackIgnore: true */ moduleName); + return module.default; + } catch (error) { + errors.push(error); throw new AggregateError( errors, - `Unable to require or import module '${id}'.` + `Unable to import module '${id}'.` ); } - return id; }; -// Import or require an array of modules by ID -const importOrRequireIds = (dirs, ids, noRequire) => ( +// Import an array of modules by ID +const importModuleIds = (dirs, ids, noImport) => ( Promise.all( ids.map( - (id) => importOrRequireResolve(dirs, id, noRequire) + (id) => importModule(dirs, id, noImport) ) ).then((results) => results.filter(Boolean)) ); -// Import or require an array of modules by ID (preserving parameters) -const importOrRequireIdsAndParams = (dirs, idsAndParams, noRequire) => ( +// Import an array of modules by ID (preserving parameters) +const importModuleIdsAndParams = (dirs, idsAndParams, noImport) => ( Promise.all( idsAndParams.map( - (idAndParams) => importOrRequireResolve(dirs, idAndParams[0], noRequire). + (idAndParams) => importModule(dirs, idAndParams[0], noImport). then((module) => module && [ module, ...idAndParams.slice(1) ]) ) ).then((results) => results.filter(Boolean)) ); -// Import or require a JavaScript file and return the exported object -const importOrRequireConfig = (fs, dir, name, noRequire, otherwise) => () => { +// Import a JavaScript file and return the exported object +const importConfig = (fs, dir, name, noImport, otherwise) => () => { const file = pathPosix.join(dir, name); return fs.promises.access(file). then( - () => importOrRequireResolve(dir, name, noRequire), + () => importModule(dir, name, noImport), otherwise ); }; @@ -151,7 +143,7 @@ const getExtendedConfig = (config, configPath, fs) => { }; // Read an options or config file in any format and return the object -const readOptionsOrConfig = async (configPath, fs, noRequire) => { +const readOptionsOrConfig = async (configPath, fs, noImport) => { const basename = pathPosix.basename(configPath); const dirname = pathPosix.dirname(configPath); let options = null; @@ -165,7 +157,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => { basename.endsWith(".markdownlint-cli2.cjs") || basename.endsWith(".markdownlint-cli2.mjs") ) { - options = await importOrRequireResolve(dirname, basename, noRequire); + options = await importModule(dirname, basename, noImport); } else if ( basename.endsWith(".markdownlint.jsonc") || basename.endsWith(".markdownlint.json") || @@ -177,7 +169,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => { basename.endsWith(".markdownlint.cjs") || basename.endsWith(".markdownlint.mjs") ) { - config = await importOrRequireResolve(dirname, basename, noRequire); + config = await importModule(dirname, basename, noImport); } else { throw new Error( "File name should be (or end with) one of the supported types " + @@ -288,7 +280,7 @@ const getAndProcessDirInfo = ( dirToDirInfo, dir, relativeDir, - noRequire, + noImport, allowPackageJson ) => { // Create dirInfo @@ -322,10 +314,10 @@ const getAndProcessDirInfo = ( () => fs.promises.readFile(file, utf8).then(yamlParse), () => fs.promises.access(captureFile(markdownlintCli2Cjs)). then( - () => importOrRequireResolve(dir, file, noRequire), + () => importModule(dir, file, noImport), () => fs.promises.access(captureFile(markdownlintCli2Mjs)). then( - () => importOrRequireResolve(dir, file, noRequire), + () => importModule(dir, file, noImport), () => (allowPackageJson ? fs.promises.access(captureFile(packageJson)) // eslint-disable-next-line prefer-promise-reject-errors @@ -379,16 +371,16 @@ const getAndProcessDirInfo = ( fs, dir, ".markdownlint.yml", - importOrRequireConfig( + importConfig( fs, dir, ".markdownlint.cjs", - noRequire, - importOrRequireConfig( + noImport, + importConfig( fs, dir, ".markdownlint.mjs", - noRequire, + noImport, noop ) ) @@ -417,7 +409,7 @@ const getBaseOptions = async ( options, fixDefault, noGlobs, - noRequire + noImport ) => { const tasks = []; const dirToDirInfo = {}; @@ -427,7 +419,7 @@ const getBaseOptions = async ( dirToDirInfo, baseDir, relativeDir, - noRequire, + noImport, true ); await Promise.all(tasks); @@ -468,7 +460,7 @@ const enumerateFiles = async ( dirToDirInfo, gitignore, ignoreFiles, - noRequire + noImport ) => { const tasks = []; /** @type {import("globby").Options} */ @@ -536,7 +528,7 @@ const enumerateFiles = async ( dirToDirInfo, dir, null, - noRequire, + noImport, false ); dirInfo.files.push(file); @@ -549,7 +541,7 @@ const enumerateParents = async ( fs, baseDir, dirToDirInfo, - noRequire + noImport ) => { const tasks = []; @@ -578,7 +570,7 @@ const enumerateParents = async ( dirToDirInfo, dir, null, - noRequire, + noImport, false ); lastDirInfo.parent = dirInfo; @@ -603,7 +595,7 @@ const createDirInfos = async ( optionsOverride, gitignore, ignoreFiles, - noRequire + noImport ) => { await enumerateFiles( fs, @@ -613,13 +605,13 @@ const createDirInfos = async ( dirToDirInfo, gitignore, ignoreFiles, - noRequire + noImport ); await enumerateParents( fs, baseDir, dirToDirInfo, - noRequire + noImport ); // Merge file lists with identical configuration @@ -650,10 +642,10 @@ const createDirInfos = async ( ); if (markdownlintOptions && markdownlintOptions.customRules) { tasks.push( - importOrRequireIds( + importModuleIds( [ effectiveDir, ...effectiveModulePaths ], markdownlintOptions.customRules, - noRequire + noImport ).then((customRules) => { // Expand nested arrays (for packages that export multiple rules) markdownlintOptions.customRules = customRules.flat(); @@ -662,10 +654,10 @@ const createDirInfos = async ( } if (markdownlintOptions && markdownlintOptions.markdownItPlugins) { tasks.push( - importOrRequireIdsAndParams( + importModuleIdsAndParams( [ effectiveDir, ...effectiveModulePaths ], markdownlintOptions.markdownItPlugins, - noRequire + noImport ).then((markdownItPlugins) => { markdownlintOptions.markdownItPlugins = markdownItPlugins; }) @@ -858,7 +850,7 @@ const outputSummary = async ( modulePaths, logMessage, logError, - noRequire + noImport ) => { const errorsPresent = (summary.length > 0); if (errorsPresent || outputFormatters) { @@ -871,7 +863,7 @@ const outputSummary = async ( const dir = relativeDir || baseDir; const dirs = [ dir, ...modulePaths ]; const formattersAndParams = outputFormatters - ? await importOrRequireIdsAndParams(dirs, outputFormatters, noRequire) + ? await importModuleIdsAndParams(dirs, outputFormatters, noImport) // eslint-disable-next-line no-inline-comments, unicorn/no-await-expression-member : [ [ (await import(/* webpackMode: "eager" */ "markdownlint-cli2-formatter-default")).default ] ]; await Promise.all(formattersAndParams.map((formatterAndParams) => { @@ -891,7 +883,7 @@ export const main = async (params) => { optionsDefault, optionsOverride, fileContents, - noRequire, + noImport, allowStdin } = params; let { @@ -948,7 +940,7 @@ export const main = async (params) => { const resolvedConfigPath = posixPath(pathDefault.resolve(baseDirSystem, configPath)); optionsArgv = - await readOptionsOrConfig(resolvedConfigPath, fs, noRequire); + await readOptionsOrConfig(resolvedConfigPath, fs, noImport); relativeDir = pathPosix.dirname(resolvedConfigPath); } // Process arguments and get base options @@ -961,7 +953,7 @@ export const main = async (params) => { optionsArgv || optionsDefault, fixDefault, noGlobs, - noRequire + noImport ); } finally { if (!baseOptions?.baseMarkdownlintOptions.noBanner) { @@ -1020,7 +1012,7 @@ export const main = async (params) => { optionsOverride, gitignore, ignoreFiles, - noRequire + noImport ); // Output linting status if (showProgress) { @@ -1058,7 +1050,7 @@ export const main = async (params) => { modulePaths, logMessage, logError, - noRequire + noImport ); // Return result return errorsPresent ? 1 : 0; diff --git a/package.json b/package.json index 700dfa10..b7e4cf7f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "playwright-test": "playwright test --config ./webworker/playwright.config.mjs", "playwright-test-docker": "docker run --rm --volume $PWD:/home/workdir --workdir /home/workdir --ipc=host mcr.microsoft.com/playwright:v1.49.1 npm run playwright-test", "schema": "cpy ./node_modules/markdownlint/schema/markdownlint-config-schema.json ./schema --flat", - "test": "ava --timeout=1m test/append-to-array-test.mjs test/fs-mock-test.mjs test/fs-virtual-test.mjs test/markdownlint-cli2-test.mjs test/markdownlint-cli2-test-exec.mjs test/markdownlint-cli2-test-exports.mjs test/markdownlint-cli2-test-fs.mjs test/markdownlint-cli2-test-main.mjs test/merge-options-test.mjs test/resolve-and-require-test.mjs", + "test": "ava --timeout=1m test/append-to-array-test.mjs test/fs-mock-test.mjs test/fs-virtual-test.mjs test/markdownlint-cli2-test.mjs test/markdownlint-cli2-test-exec.mjs test/markdownlint-cli2-test-exports.mjs test/markdownlint-cli2-test-fs.mjs test/markdownlint-cli2-test-main.mjs test/merge-options-test.mjs test/resolve-module-test.mjs", "test-cover": "c8 --100 npm test", "test-docker-hub-image": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2:v$VERSION davidanson/markdownlint-cli2:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:latest \"*.md\"", "test-docker-hub-image-rules": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2-rules:v$VERSION davidanson/markdownlint-cli2-rules:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:latest \"*.md\"", @@ -68,7 +68,7 @@ "parsers/jsonc-parse.mjs", "parsers/yaml-parse.mjs", "README.md", - "resolve-and-require.mjs", + "resolve-module.mjs", "schema/markdownlint-cli2-config-schema.json", "schema/markdownlint-config-schema.json", "schema/ValidatingConfiguration.md" diff --git a/resolve-and-require.mjs b/resolve-and-require.mjs deleted file mode 100644 index 8650ee2e..00000000 --- a/resolve-and-require.mjs +++ /dev/null @@ -1,17 +0,0 @@ -// @ts-check - -/** - * Wrapper for calling Node's require.resolve/require with an additional path. - * @param {object} req Node's require implementation (or equivalent). - * @param {string} id Package identifier to require. - * @param {string[]} dirs Directories to include when resolving paths. - * @returns {object} Exported module content. - */ -const resolveAndRequire = (req, id, dirs) => { - const resolvePaths = req.resolve.paths ? req.resolve.paths("") : []; - const paths = [ ...dirs, ...resolvePaths ]; - const resolved = req.resolve(id, { paths }); - return req(resolved); -}; - -export default resolveAndRequire; diff --git a/resolve-module.mjs b/resolve-module.mjs new file mode 100644 index 00000000..13c87ac5 --- /dev/null +++ b/resolve-module.mjs @@ -0,0 +1,16 @@ +// @ts-check + +/** + * 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 allPaths = [ ...paths, ...resolvePaths ]; + return require.resolve(request, { "paths": allPaths }); +}; + +export default resolveModule; diff --git a/test/customRules/dir/subdir/.markdownlint-cli2.jsonc b/test/customRules/dir/subdir/.markdownlint-cli2.jsonc index 66c3cb4b..5bb6868d 100644 --- a/test/customRules/dir/subdir/.markdownlint-cli2.jsonc +++ b/test/customRules/dir/subdir/.markdownlint-cli2.jsonc @@ -1,7 +1,7 @@ { "customRules": [ "../../rules/first-line.cjs", - "../../node_modules/markdownlint-rule-sample-commonjs", + "../../node_modules/markdownlint-rule-sample-commonjs/sample-rule.cjs", "../../node_modules/markdownlint-rule-sample-module/sample-rule.mjs", "markdownlint-rule-extended-ascii" ] diff --git a/test/customRules/dir2/.markdownlint-cli2.jsonc b/test/customRules/dir2/.markdownlint-cli2.jsonc index 8a668d71..9c7b3fec 100644 --- a/test/customRules/dir2/.markdownlint-cli2.jsonc +++ b/test/customRules/dir2/.markdownlint-cli2.jsonc @@ -1,6 +1,6 @@ { "customRules": [ "markdownlint-rule-sample-commonjs", - "../node_modules/markdownlint-rule-sample-module/sample-rule.mjs" + "markdownlint-rule-sample-module" ] } diff --git a/test/customRules/dir3/.markdownlint-cli2.jsonc b/test/customRules/dir3/.markdownlint-cli2.jsonc index e9a71c6c..a931ee9b 100644 --- a/test/customRules/dir3/.markdownlint-cli2.jsonc +++ b/test/customRules/dir3/.markdownlint-cli2.jsonc @@ -2,6 +2,6 @@ "customRules": [ "../rules/all-rules.cjs", "../node_modules/markdownlint-rule-sample-commonjs", - "../node_modules/markdownlint-rule-sample-module/sample-rule.mjs" + "../node_modules/markdownlint-rule-sample-module" ] } diff --git a/test/customRules/dir4/.markdownlint-cli2.mjs b/test/customRules/dir4/.markdownlint-cli2.mjs index f3d232e0..06e30e27 100644 --- a/test/customRules/dir4/.markdownlint-cli2.mjs +++ b/test/customRules/dir4/.markdownlint-cli2.mjs @@ -2,6 +2,10 @@ const options = { "customRules": [ + (new URL( + "../node_modules/markdownlint-rule-sample-commonjs/sample-rule.cjs", + import.meta.url + )).toString(), (new URL( "../node_modules/markdownlint-rule-sample-module/sample-rule.mjs", import.meta.url diff --git a/test/markdownlint-cli2-test-cases.mjs b/test/markdownlint-cli2-test-cases.mjs index ac654fac..41f1c50b 100644 --- a/test/markdownlint-cli2-test-cases.mjs +++ b/test/markdownlint-cli2-test-cases.mjs @@ -19,7 +19,7 @@ const testCases = ({ host, invoke, absolute, - includeNoRequire, + includeNoImport, includeEnv, includeScript, includeRequire, @@ -38,7 +38,7 @@ const testCases = ({ stderrRe, pre, post, - noRequire, + noImport, usesRequire } = options; const usesEnv = Boolean(env); @@ -46,7 +46,7 @@ const testCases = ({ // eslint-disable-next-line unicorn/no-array-callback-reference const usesAbsolute = args.some(path.isAbsolute); if ( - (noRequire && !includeNoRequire) || + (noImport && !includeNoImport) || (usesEnv && !includeEnv) || (usesRequire && !includeRequire) || (usesScript && !includeScript) || @@ -58,7 +58,7 @@ const testCases = ({ t.plan(3); const directory = path.join(__dirname(import.meta), cwd || name); return ((pre || noop)(name, shadow) || Promise.resolve()). - then(invoke(directory, args, noRequire, env, script)). + then(invoke(directory, args, noImport, env, script)). then((result) => Promise.all([ result, fs.readFile( @@ -359,7 +359,7 @@ const testCases = ({ "name": "markdownlint-cjs-invalid", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /Unable to require or import module '.*\.markdownlint\.cjs'/u, + "stderrRe": /Unable to import module '.*\.markdownlint\.cjs'/u, "usesRequire": true }); @@ -367,7 +367,7 @@ const testCases = ({ "name": "markdownlint-mjs-invalid", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /Unable to require or import module '.*\.markdownlint\.mjs'/u, + "stderrRe": /Unable to import module '.*\.markdownlint\.mjs'/u, "usesRequire": true }); @@ -491,7 +491,7 @@ const testCases = ({ "name": "markdownlint-cli2-cjs-invalid", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /'[^']*\.markdownlint-cli2\.cjs'.*Unable to require or import module '/u, + "stderrRe": /'[^']*\.markdownlint-cli2\.cjs'.*Unable to import module '/u, "usesRequire": true }); @@ -499,7 +499,7 @@ const testCases = ({ "name": "markdownlint-cli2-mjs-invalid", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /'[^']*\.markdownlint-cli2\.mjs'.*Unable to require or import module '/u, + "stderrRe": /'[^']*\.markdownlint-cli2\.mjs'.*Unable to import module '/u, "usesRequire": true }); @@ -679,7 +679,7 @@ const testCases = ({ } const unableToParseJsonc = "Unable to parse JSONC content"; - const unableToRequireOrImport = "Unable to require or import module"; + const unableToRequireOrImport = "Unable to import module"; const invalidConfigFiles = [ [ "invalid.markdownlint-cli2.jsonc", unableToParseJsonc ], [ "invalid.markdownlint-cli2.cjs", unableToRequireOrImport ], @@ -813,7 +813,7 @@ const testCases = ({ "name": "customRules-missing", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /Unable to require or import module 'missing-package'\./u, + "stderrRe": /Unable to import module 'missing-package'\./u, "usesRequire": true }); @@ -843,7 +843,7 @@ const testCases = ({ "name": "markdownItPlugins-missing", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /Unable to require or import module 'missing-package'\./u, + "stderrRe": /Unable to import module 'missing-package'\./u, "usesRequire": true }); @@ -929,7 +929,7 @@ const testCases = ({ "name": "outputFormatters-missing", "args": [ ".*" ], "exitCode": 2, - "stderrRe": /Unable to require or import module 'missing-package'\./u, + "stderrRe": /Unable to import module 'missing-package'\./u, "usesRequire": true }); @@ -990,7 +990,7 @@ const testCases = ({ "args": [ "**/*.md" ], "exitCode": 1, "cwd": "markdownlint-cjs", - "noRequire": true + "noImport": true }); testCase({ @@ -998,7 +998,7 @@ const testCases = ({ "args": [ "**/*.md" ], "exitCode": 1, "cwd": "markdownlint-mjs", - "noRequire": true + "noImport": true }); testCase({ @@ -1006,7 +1006,7 @@ const testCases = ({ "args": [ "**/*.md" ], "exitCode": 1, "cwd": "markdownlint-cli2-cjs", - "noRequire": true + "noImport": true }); testCase({ @@ -1014,7 +1014,7 @@ const testCases = ({ "args": [ "**/*.md" ], "exitCode": 1, "cwd": "markdownlint-cli2-mjs", - "noRequire": true + "noImport": true }); testCase({ @@ -1022,7 +1022,7 @@ const testCases = ({ "args": [ "**/*.md" ], "exitCode": 1, "cwd": "customRules", - "noRequire": true + "noImport": true }); testCase({ @@ -1030,7 +1030,7 @@ const testCases = ({ "args": [ "**/*.md" ], "exitCode": 1, "cwd": "markdownItPlugins", - "noRequire": true + "noImport": true }); if (sameFileSystem) { diff --git a/test/markdownlint-cli2-test-exec.mjs b/test/markdownlint-cli2-test-exec.mjs index 25749219..bff481b2 100644 --- a/test/markdownlint-cli2-test-exec.mjs +++ b/test/markdownlint-cli2-test-exec.mjs @@ -10,7 +10,7 @@ import { __dirname } from "./esm-helpers.mjs"; const absolute = (rootDir, file) => path.join(rootDir, file); const repositoryPath = (name) => path.join(__dirname(import.meta), "..", name); -const invoke = (directory, args, noRequire, env, script) => async () => { +const invoke = (directory, args, noImport, env, script) => async () => { await fs.access(directory); return spawn( "node", @@ -34,7 +34,7 @@ testCases({ "host": "exec", invoke, absolute, - "includeNoRequire": false, + "includeNoImport": false, "includeEnv": true, "includeScript": true, "includeRequire": true, diff --git a/test/markdownlint-cli2-test-fs.mjs b/test/markdownlint-cli2-test-fs.mjs index 8306a1c2..8ca84531 100644 --- a/test/markdownlint-cli2-test-fs.mjs +++ b/test/markdownlint-cli2-test-fs.mjs @@ -9,7 +9,7 @@ const mockDirectory = "/mock"; const linesEndingWithNewLine = (lines) => lines.map((line) => `${line}\n`).join(""); -const invoke = (directory, args, noRequire) => () => { +const invoke = (directory, args, noImport) => () => { const stdouts = []; const stderrs = []; return markdownlintCli2({ @@ -17,7 +17,7 @@ const invoke = (directory, args, noRequire) => () => { "argv": args, "logMessage": (msg) => stdouts.push(msg), "logError": (msg) => stderrs.push(msg), - noRequire, + noImport, "fs": new FsMock(directory) }). then( @@ -40,7 +40,7 @@ testCases({ "host": "fs", invoke, absolute, - "includeNoRequire": true, + "includeNoImport": true, "includeEnv": false, "includeScript": false, "includeRequire": false, diff --git a/test/markdownlint-cli2-test-main.mjs b/test/markdownlint-cli2-test-main.mjs index 067e3bec..2f17c551 100644 --- a/test/markdownlint-cli2-test-main.mjs +++ b/test/markdownlint-cli2-test-main.mjs @@ -7,7 +7,7 @@ import testCases from "./markdownlint-cli2-test-cases.mjs"; const linesEndingWithNewLine = (lines) => lines.map((line) => `${line}\n`).join(""); -const invoke = (directory, args, noRequire) => () => { +const invoke = (directory, args, noImport) => () => { const stdouts = []; const stderrs = []; return markdownlintCli2({ @@ -15,7 +15,7 @@ const invoke = (directory, args, noRequire) => () => { "argv": args, "logMessage": (msg) => stdouts.push(msg), "logError": (msg) => stderrs.push(msg), - noRequire + noImport }). then( (exitCode) => exitCode, @@ -37,7 +37,7 @@ testCases({ "host": "main", invoke, absolute, - "includeNoRequire": true, + "includeNoImport": true, "includeEnv": false, "includeScript": false, "includeRequire": true, diff --git a/test/markdownlint-cli2-test.mjs b/test/markdownlint-cli2-test.mjs index 5e56419e..2f8dd6a9 100644 --- a/test/markdownlint-cli2-test.mjs +++ b/test/markdownlint-cli2-test.mjs @@ -475,7 +475,7 @@ test("custom fs, extension scenario for untitled", (t) => { writeFile } }, - "noRequire": true + "noImport": true }). then((exitCode) => { t.is(exitCode, 1); @@ -570,7 +570,7 @@ test("custom fs, using node:fs", (t) => { }); }); -test("custom fs, using node:fs and noRequire=false", (t) => { +test("custom fs, using node:fs and noImport=false", (t) => { t.plan(2); return markdownlintCli2({ "directory": "test/markdownlint-cjs", @@ -579,14 +579,14 @@ test("custom fs, using node:fs and noRequire=false", (t) => { "outputFormatters": [ [ outputFormatterLengthIs(t, 11) ] ] }, "fs": nodeFs, - "noRequire": false + "noImport": false }). then((exitCode) => { t.is(exitCode, 1); }); }); -test("custom fs, using node:fs and noRequire=true", (t) => { +test("custom fs, using node:fs and noImport=true", (t) => { t.plan(2); return markdownlintCli2({ "directory": "test/markdownlint-cjs", @@ -595,7 +595,7 @@ test("custom fs, using node:fs and noRequire=true", (t) => { "outputFormatters": [ [ outputFormatterLengthIs(t, 14) ] ] }, "fs": nodeFs, - "noRequire": true + "noImport": true }). then((exitCode) => { t.is(exitCode, 1); @@ -611,7 +611,7 @@ test("custom fs, using fsMock", (t) => { "outputFormatters": [ [ outputFormatterLengthIs(t, 10) ] ] }, "fs": new FsMock(path.join(__dirname(import.meta), "markdownlint-cli2-jsonc")), - "noRequire": true + "noImport": true }). then((exitCode) => { t.is(exitCode, 1); @@ -627,7 +627,7 @@ test("custom fs, using fsMock simulating symbolic links", (t) => { "outputFormatters": [ [ outputFormatterLengthIs(t, 10) ] ] }, "fs": new FsMock(path.join(__dirname(import.meta), "markdownlint-cli2-jsonc"), true), - "noRequire": true + "noImport": true }). then((exitCode) => { t.is(exitCode, 1); diff --git a/test/outputFormatters-module/.markdownlint-cli2.jsonc b/test/outputFormatters-module/.markdownlint-cli2.jsonc index f64f929a..3769a370 100644 --- a/test/outputFormatters-module/.markdownlint-cli2.jsonc +++ b/test/outputFormatters-module/.markdownlint-cli2.jsonc @@ -1,6 +1,8 @@ { "outputFormatters": [ [ "output-formatter-sample-commonjs" ], + [ "output-formatter-sample-module" ], + [ "./node_modules/output-formatter-sample-commonjs/sample-formatter.cjs" ], [ "./node_modules/output-formatter-sample-module/sample-formatter.mjs" ] ] } diff --git a/test/resolve-and-require-test.mjs b/test/resolve-module-test.mjs similarity index 63% rename from test/resolve-and-require-test.mjs rename to test/resolve-module-test.mjs index 54049592..2db0f549 100644 --- a/test/resolve-and-require-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 resolveAndRequire from "../resolve-and-require.mjs"; +import resolve from "../resolve-module.mjs"; import { createRequire } from "node:module"; const require = createRequire(import.meta.url); @@ -11,24 +11,24 @@ const require = createRequire(import.meta.url); test("built-in module", (t) => { t.plan(1); t.deepEqual( - require("node:fs"), - resolveAndRequire(require, "fs", [ __dirname(import.meta) ]) + require.resolve("node:fs"), + resolve(require, "node:fs", [ __dirname(import.meta) ]) ); }); test("locally-installed module", (t) => { t.plan(1); t.deepEqual( - require("micromatch"), - resolveAndRequire(require, "micromatch", [ __dirname(import.meta) ]) + require.resolve("micromatch"), + resolve(require, "micromatch", [ __dirname(import.meta) ]) ); }); test("relative (to __dirname(import.meta)) path to module", (t) => { t.plan(1); t.deepEqual( - require("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolveAndRequire( + require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), + resolve( require, "./customRules/node_modules/markdownlint-rule-sample-commonjs", [ __dirname(import.meta) ] @@ -40,12 +40,12 @@ test("module in alternate node_modules", (t) => { t.plan(2); t.throws( // @ts-ignore - () => require("markdownlint-rule-sample-commonjs"), + () => require.resolve("markdownlint-rule-sample-commonjs"), { "code": "MODULE_NOT_FOUND" } ); t.deepEqual( - require("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolveAndRequire( + require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), + resolve( require, "markdownlint-rule-sample-commonjs", [ path.join(__dirname(import.meta), "customRules") ] @@ -59,12 +59,12 @@ test("module in alternate node_modules and no require.resolve.paths", (t) => { delete require.resolve.paths; t.throws( // @ts-ignore - () => require("markdownlint-rule-sample-commonjs"), + () => require.resolve("markdownlint-rule-sample-commonjs"), { "code": "MODULE_NOT_FOUND" } ); t.deepEqual( - require("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolveAndRequire( + require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), + resolve( require, "markdownlint-rule-sample-commonjs", [ path.join(__dirname(import.meta), "customRules") ] @@ -79,20 +79,20 @@ test("module local, relative, and in alternate node_modules", (t) => { path.join(__dirname(import.meta), "customRules") ]; t.deepEqual( - require("micromatch"), - resolveAndRequire(require, "micromatch", dirs) + require.resolve("micromatch"), + resolve(require, "micromatch", dirs) ); t.deepEqual( - require("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolveAndRequire( + require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), + resolve( require, "./customRules/node_modules/markdownlint-rule-sample-commonjs", dirs ) ); t.deepEqual( - require("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolveAndRequire( + require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), + resolve( require, "markdownlint-rule-sample-commonjs", dirs diff --git a/test/snapshots/markdownlint-cli2-test-exec.mjs.md b/test/snapshots/markdownlint-cli2-test-exec.mjs.md index 8df1378b..314fb669 100644 --- a/test/snapshots/markdownlint-cli2-test-exec.mjs.md +++ b/test/snapshots/markdownlint-cli2-test-exec.mjs.md @@ -2726,6 +2726,7 @@ Generated by [AVA](https://avajs.dev). dir3/hr.md:2 second-line Rule that reports an error for the second line␊ dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊ dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊ + dir4/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊ dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊ viewme.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊ viewme.md:1 first-line Rule that reports an error for the first line␊ @@ -2738,7 +2739,7 @@ Generated by [AVA](https://avajs.dev). stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ Finding: **/*.md␊ Linting: 9 file(s)␊ - Summary: 44 error(s)`, + Summary: 45 error(s)`, } ## customRules-pre-imported (exec) @@ -5705,6 +5706,16 @@ Generated by [AVA](https://avajs.dev). mjs: viewme.md 5 MD012/no-multiple-blanks␊ mjs: viewme.md 6 MD025/single-title/single-h1␊ mjs: viewme.md 12 MD019/no-multiple-space-atx␊ + mjs: viewme.md 14 MD047/single-trailing-newline␊ + cjs: viewme.md 3 MD009/no-trailing-spaces␊ + cjs: viewme.md 5 MD012/no-multiple-blanks␊ + cjs: viewme.md 6 MD025/single-title/single-h1␊ + cjs: viewme.md 12 MD019/no-multiple-space-atx␊ + cjs: viewme.md 14 MD047/single-trailing-newline␊ + mjs: viewme.md 3 MD009/no-trailing-spaces␊ + mjs: viewme.md 5 MD012/no-multiple-blanks␊ + mjs: viewme.md 6 MD025/single-title/single-h1␊ + mjs: viewme.md 12 MD019/no-multiple-space-atx␊ mjs: viewme.md 14 MD047/single-trailing-newline`, stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ Finding: **/*.md␊ diff --git a/test/snapshots/markdownlint-cli2-test-exec.mjs.snap b/test/snapshots/markdownlint-cli2-test-exec.mjs.snap index 3250a6be..b09248bc 100644 Binary files a/test/snapshots/markdownlint-cli2-test-exec.mjs.snap and b/test/snapshots/markdownlint-cli2-test-exec.mjs.snap differ diff --git a/test/snapshots/markdownlint-cli2-test-main.mjs.md b/test/snapshots/markdownlint-cli2-test-main.mjs.md index 31e4a3b6..b0d52be9 100644 --- a/test/snapshots/markdownlint-cli2-test-main.mjs.md +++ b/test/snapshots/markdownlint-cli2-test-main.mjs.md @@ -2919,6 +2919,7 @@ Generated by [AVA](https://avajs.dev). dir3/hr.md:2 second-line Rule that reports an error for the second line␊ dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊ dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊ + dir4/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊ dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊ viewme.md:1 every-n-lines Rule that reports an error every N lines [This rule threw an exception: getLineMetadata is not a function]␊ viewme.md:1 first-line Rule that reports an error for the first line␊ @@ -2932,7 +2933,7 @@ Generated by [AVA](https://avajs.dev). stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ Finding: **/*.md␊ Linting: 9 file(s)␊ - Summary: 44 error(s)␊ + Summary: 45 error(s)␊ `, } @@ -4951,6 +4952,16 @@ Generated by [AVA](https://avajs.dev). mjs: viewme.md 6 MD025/single-title/single-h1␊ mjs: viewme.md 12 MD019/no-multiple-space-atx␊ mjs: viewme.md 14 MD047/single-trailing-newline␊ + cjs: viewme.md 3 MD009/no-trailing-spaces␊ + cjs: viewme.md 5 MD012/no-multiple-blanks␊ + cjs: viewme.md 6 MD025/single-title/single-h1␊ + cjs: viewme.md 12 MD019/no-multiple-space-atx␊ + cjs: viewme.md 14 MD047/single-trailing-newline␊ + mjs: viewme.md 3 MD009/no-trailing-spaces␊ + mjs: viewme.md 5 MD012/no-multiple-blanks␊ + mjs: viewme.md 6 MD025/single-title/single-h1␊ + mjs: viewme.md 12 MD019/no-multiple-space-atx␊ + mjs: viewme.md 14 MD047/single-trailing-newline␊ `, stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ Finding: **/*.md␊ diff --git a/test/snapshots/markdownlint-cli2-test-main.mjs.snap b/test/snapshots/markdownlint-cli2-test-main.mjs.snap index 1821d01c..f78e8cf1 100644 Binary files a/test/snapshots/markdownlint-cli2-test-main.mjs.snap and b/test/snapshots/markdownlint-cli2-test-main.mjs.snap differ