Skip to content

Commit 31dd0a6

Browse files
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
1 parent 4bac8e0 commit 31dd0a6

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/index.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,15 @@ const needles = [
2929

3030
iterateESLintModules(patch)
3131

32-
function getLinterFromModule(moduleExports) {
33-
return moduleExports.Linter
34-
? moduleExports.Linter // ESLint 6+
35-
: moduleExports // ESLint 5-
36-
}
37-
38-
function getModuleFromCache(key) {
39-
if (!needles.some((needle) => key.endsWith(needle))) return
40-
41-
const module = require.cache[key]
42-
if (!module || !module.exports) return
43-
44-
const Linter = getLinterFromModule(module.exports)
45-
if (
46-
typeof Linter === "function" &&
47-
typeof Linter.prototype.verify === "function"
48-
) {
49-
return Linter
50-
}
51-
}
52-
5332
function iterateESLintModules(fn) {
5433
let found = false
5534

5635
for (const key in require.cache) {
57-
const Linter = getModuleFromCache(key)
58-
if (Linter) {
59-
fn(Linter)
36+
if (!needles.some((needle) => key.endsWith(needle))) continue
37+
38+
const module = require.cache[key]
39+
if (module && module.exports) {
40+
fn(module)
6041
found = true
6142
}
6243
}
@@ -68,7 +49,8 @@ function iterateESLintModules(fn) {
6849
}
6950
}
7051

71-
function patch(Linter) {
52+
function patch(module) {
53+
const Linter = getLinterFromModule(module)
7254
// ignore if verify function is already been patched sometime before
7355
if (Linter[LINTER_ISPATCHED_PROPERTY_NAME] === true) {
7456
return
@@ -84,5 +66,17 @@ function patch(Linter) {
8466
const verifyWithFlatConfig =
8567
Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors
8668
Linter.prototype._verifyWithFlatConfigArrayAndWithoutProcessors =
87-
createVerifyWithFlatConfigPatch(verifyWithFlatConfig)
69+
createVerifyWithFlatConfigPatch(module, verifyWithFlatConfig)
70+
}
71+
72+
function getLinterFromModule(module) {
73+
const Linter = module.exports.Linter
74+
? module.exports.Linter // ESLint 6+
75+
: module.exports // ESLint 5-
76+
if (
77+
typeof Linter === "function" &&
78+
typeof Linter.prototype.verify === "function"
79+
) {
80+
return Linter
81+
}
8882
}

src/verifyWithFlatConfigPatch.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const PREPARE_PLUGIN_NAME = "__eslint-plugin-html-prepare"
1010

1111
module.exports = { createVerifyWithFlatConfigPatch }
1212

13-
function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) {
13+
function createVerifyWithFlatConfigPatch(eslintModule, verifyWithFlatConfig) {
1414
return function (textOrSourceCode, providedConfig, providedOptions) {
1515
const callOriginalVerify = () =>
1616
verifyWithFlatConfig.call(
@@ -33,6 +33,7 @@ function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) {
3333

3434
let messages
3535
;[messages, providedConfig] = verifyExternalHtmlPlugin(
36+
eslintModule,
3637
providedConfig,
3738
callOriginalVerify
3839
)
@@ -144,7 +145,7 @@ function findExternalHtmlPluginName(config) {
144145
}
145146
}
146147

147-
function verifyExternalHtmlPlugin(config, callOriginalVerify) {
148+
function verifyExternalHtmlPlugin(eslintModule, config, callOriginalVerify) {
148149
const htmlPluginName = findExternalHtmlPluginName(config)
149150
if (!htmlPluginName) {
150151
return [[], config]
@@ -163,7 +164,7 @@ function verifyExternalHtmlPlugin(config, callOriginalVerify) {
163164
...config,
164165
languageOptions: {
165166
...config.languageOptions,
166-
parser: require("espree"),
167+
parser: eslintModule.require("espree"),
167168
},
168169
rules,
169170
},

0 commit comments

Comments
 (0)