forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferenceTracker.ts
98 lines (89 loc) · 3.77 KB
/
ReferenceTracker.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* eslint-disable @typescript-eslint/no-namespace */
import * as eslintUtils from '@eslint-community/eslint-utils';
import type * as TSESLint from '../../ts-eslint';
import type { TSESTree } from '../../ts-estree';
const ReferenceTrackerREAD: unique symbol = eslintUtils.ReferenceTracker.READ;
const ReferenceTrackerCALL: unique symbol = eslintUtils.ReferenceTracker.CALL;
const ReferenceTrackerCONSTRUCT: unique symbol =
eslintUtils.ReferenceTracker.CONSTRUCT;
const ReferenceTrackerESM: unique symbol = eslintUtils.ReferenceTracker.ESM;
interface ReferenceTracker {
/**
* Iterate the references that the given `traceMap` determined.
* This method starts to search from global variables.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iterateglobalreferences}
*/
iterateGlobalReferences<T>(
traceMap: ReferenceTracker.TraceMap<T>,
): IterableIterator<ReferenceTracker.FoundReference<T>>;
/**
* Iterate the references that the given `traceMap` determined.
* This method starts to search from `require()` expression.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iteratecjsreferences}
*/
iterateCjsReferences<T>(
traceMap: ReferenceTracker.TraceMap<T>,
): IterableIterator<ReferenceTracker.FoundReference<T>>;
/**
* Iterate the references that the given `traceMap` determined.
* This method starts to search from `import`/`export` declarations.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#tracker-iterateesmreferences}
*/
iterateEsmReferences<T>(
traceMap: ReferenceTracker.TraceMap<T>,
): IterableIterator<ReferenceTracker.FoundReference<T>>;
}
interface ReferenceTrackerStatic {
new (
globalScope: TSESLint.Scope.Scope,
options?: {
/**
* The mode which determines how the `tracker.iterateEsmReferences()` method scans CommonJS modules.
* If this is `"strict"`, the method binds CommonJS modules to the default export. Otherwise, the method binds
* CommonJS modules to both the default export and named exports. Optional. Default is `"strict"`.
*/
mode?: 'strict' | 'legacy';
/**
* The name list of Global Object. Optional. Default is `["global", "globalThis", "self", "window"]`.
*/
globalObjectNames?: readonly string[];
},
): ReferenceTracker;
readonly READ: typeof ReferenceTrackerREAD;
readonly CALL: typeof ReferenceTrackerCALL;
readonly CONSTRUCT: typeof ReferenceTrackerCONSTRUCT;
readonly ESM: typeof ReferenceTrackerESM;
}
namespace ReferenceTracker {
export type READ = ReferenceTrackerStatic['READ'];
export type CALL = ReferenceTrackerStatic['CALL'];
export type CONSTRUCT = ReferenceTrackerStatic['CONSTRUCT'];
export type ESM = ReferenceTrackerStatic['ESM'];
export type ReferenceType = READ | CALL | CONSTRUCT;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TraceMap<T = any> = Record<string, TraceMapElement<T>>;
export interface TraceMapElement<T> {
[ReferenceTrackerREAD]?: T;
[ReferenceTrackerCALL]?: T;
[ReferenceTrackerCONSTRUCT]?: T;
[ReferenceTrackerESM]?: true;
[key: string]: TraceMapElement<T>;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface FoundReference<T = any> {
node: TSESTree.Node;
path: readonly string[];
type: ReferenceType;
info: T;
}
}
/**
* The tracker for references. This provides reference tracking for global variables, CommonJS modules, and ES modules.
*
* @see {@link https://eslint-community.github.io/eslint-utils/api/scope-utils.html#referencetracker-class}
*/
const ReferenceTracker = eslintUtils.ReferenceTracker as ReferenceTrackerStatic;
export { ReferenceTracker };