From 31dd0a6c501e72a7fa0cb83be5d7e5de3e6452c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Fri, 20 Sep 2024 23:01:33 +0200 Subject: [PATCH] fix: don't require espree directly Require espree as if it was required from the ESLint module, so it works regardless of the node_modules tree. Fixes #271 --- src/index.js | 46 ++++++++++++++------------------ src/verifyWithFlatConfigPatch.js | 7 ++--- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/index.js b/src/index.js index b250196..1923f2c 100644 --- a/src/index.js +++ b/src/index.js @@ -29,34 +29,15 @@ const needles = [ iterateESLintModules(patch) -function getLinterFromModule(moduleExports) { - return moduleExports.Linter - ? moduleExports.Linter // ESLint 6+ - : moduleExports // ESLint 5- -} - -function getModuleFromCache(key) { - if (!needles.some((needle) => key.endsWith(needle))) return - - const module = require.cache[key] - if (!module || !module.exports) return - - const Linter = getLinterFromModule(module.exports) - if ( - typeof Linter === "function" && - typeof Linter.prototype.verify === "function" - ) { - return Linter - } -} - function iterateESLintModules(fn) { let found = false for (const key in require.cache) { - const Linter = getModuleFromCache(key) - if (Linter) { - fn(Linter) + if (!needles.some((needle) => key.endsWith(needle))) continue + + const module = require.cache[key] + if (module && module.exports) { + fn(module) found = true } } @@ -68,7 +49,8 @@ function iterateESLintModules(fn) { } } -function patch(Linter) { +function patch(module) { + const Linter = getLinterFromModule(module) // ignore if verify function is already been patched sometime before if (Linter[LINTER_ISPATCHED_PROPERTY_NAME] === true) { return @@ -84,5 +66,17 @@ function patch(Linter) { const verifyWithFlatConfig = Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors = - createVerifyWithFlatConfigPatch(verifyWithFlatConfig) + createVerifyWithFlatConfigPatch(module, verifyWithFlatConfig) +} + +function getLinterFromModule(module) { + const Linter = module.exports.Linter + ? module.exports.Linter // ESLint 6+ + : module.exports // ESLint 5- + if ( + typeof Linter === "function" && + typeof Linter.prototype.verify === "function" + ) { + return Linter + } } diff --git a/src/verifyWithFlatConfigPatch.js b/src/verifyWithFlatConfigPatch.js index 1dcd413..507810b 100644 --- a/src/verifyWithFlatConfigPatch.js +++ b/src/verifyWithFlatConfigPatch.js @@ -10,7 +10,7 @@ const PREPARE_PLUGIN_NAME = "__eslint-plugin-html-prepare" module.exports = { createVerifyWithFlatConfigPatch } -function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) { +function createVerifyWithFlatConfigPatch(eslintModule, verifyWithFlatConfig) { return function (textOrSourceCode, providedConfig, providedOptions) { const callOriginalVerify = () => verifyWithFlatConfig.call( @@ -33,6 +33,7 @@ function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) { let messages ;[messages, providedConfig] = verifyExternalHtmlPlugin( + eslintModule, providedConfig, callOriginalVerify ) @@ -144,7 +145,7 @@ function findExternalHtmlPluginName(config) { } } -function verifyExternalHtmlPlugin(config, callOriginalVerify) { +function verifyExternalHtmlPlugin(eslintModule, config, callOriginalVerify) { const htmlPluginName = findExternalHtmlPluginName(config) if (!htmlPluginName) { return [[], config] @@ -163,7 +164,7 @@ function verifyExternalHtmlPlugin(config, callOriginalVerify) { ...config, languageOptions: { ...config.languageOptions, - parser: require("espree"), + parser: eslintModule.require("espree"), }, rules, },