-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy patheslint-scope.ts
41 lines (37 loc) · 1.01 KB
/
eslint-scope.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
import * as escope from "eslint-scope"
import { lte } from "semver"
import path from "path"
import { createRequire } from "module"
type ESLintScope = typeof escope & {
version: string
}
let escopeCache: ESLintScope | null = null
/**
* Load the newest `eslint-scope` from the loaded ESLint or dependency.
*/
export function getEslintScope(): ESLintScope {
return escopeCache ?? (escopeCache = getNewest())
}
/**
* Load the newest `eslint-scope` from the dependency.
*/
function getNewest(): ESLintScope {
let newest = escope
const userEscope = getEslintScopeFromUser()
if (userEscope.version != null && lte(newest.version, userEscope.version)) {
newest = userEscope
}
return newest
}
/**
* Load `eslint-scope` from the user dir.
*/
function getEslintScopeFromUser(): ESLintScope {
try {
const cwd = process.cwd()
const relativeTo = path.join(cwd, "__placeholder__.js")
return createRequire(relativeTo)("eslint-scope")
} catch {
return escope
}
}