Skip to content

Commit

Permalink
fix: don't require espree directly
Browse files Browse the repository at this point in the history
Require espree as if it was required from the ESLint module, so it works
regardless of the node_modules tree.

Fixes #271
  • Loading branch information
BenoitZugmeyer committed Sep 20, 2024
1 parent 4bac8e0 commit 31dd0a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
46 changes: 20 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
Expand All @@ -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
}
}
7 changes: 4 additions & 3 deletions src/verifyWithFlatConfigPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -33,6 +33,7 @@ function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) {

let messages
;[messages, providedConfig] = verifyExternalHtmlPlugin(
eslintModule,
providedConfig,
callOriginalVerify
)
Expand Down Expand Up @@ -144,7 +145,7 @@ function findExternalHtmlPluginName(config) {
}
}

function verifyExternalHtmlPlugin(config, callOriginalVerify) {
function verifyExternalHtmlPlugin(eslintModule, config, callOriginalVerify) {
const htmlPluginName = findExternalHtmlPluginName(config)
if (!htmlPluginName) {
return [[], config]
Expand All @@ -163,7 +164,7 @@ function verifyExternalHtmlPlugin(config, callOriginalVerify) {
...config,
languageOptions: {
...config.languageOptions,
parser: require("espree"),
parser: eslintModule.require("espree"),
},
rules,
},
Expand Down

0 comments on commit 31dd0a6

Please sign in to comment.