-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathespree.ts
58 lines (52 loc) · 1.76 KB
/
espree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Module from "module"
import path from "path"
import type { ESLintCustomParser } from "./resolve-parser"
const createRequire: (filename: string) => (modName: string) => any =
// Added in v12.2.0
Module.createRequire ||
// Added in v10.12.0, but deprecated in v12.2.0.
// @ts-expect-error -- old type
Module.createRequireFromPath ||
// Polyfill - This is not executed on the tests on node@>=10.
/* istanbul ignore next */
((modName) => {
const mod = new Module(modName)
mod.filename = modName
mod.paths = (Module as any)._nodeModulePaths(path.dirname(modName))
;(mod as any)._compile("module.exports = require;", modName)
return mod.exports
})
let espreeCache: ESLintCustomParser | null = null
/** Checks if given path is linter path */
function isLinterPath(p: string): boolean {
return (
// ESLint 6 and above
p.includes(
`eslint${path.sep}lib${path.sep}linter${path.sep}linter.js`,
) ||
// ESLint 5
p.includes(`eslint${path.sep}lib${path.sep}linter.js`)
)
}
/**
* Load `espree` from the loaded ESLint.
* If the loaded ESLint was not found, just returns `require("espree")`.
*/
export function getEspree(): ESLintCustomParser {
if (!espreeCache) {
// Lookup the loaded eslint
const linterPath = Object.keys(require.cache || {}).find(isLinterPath)
if (linterPath) {
try {
espreeCache = createRequire(linterPath)("espree")
} catch {
// ignore
}
}
if (!espreeCache) {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
espreeCache = require("espree")
}
}
return espreeCache!
}