-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmicrotask.ts
37 lines (35 loc) · 1.1 KB
/
microtask.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
import type { TSESTree } from "@typescript-eslint/types"
import { ReferenceTracker } from "@eslint-community/eslint-utils"
import type { RuleContext } from "../../types"
type FunctionName = "setTimeout" | "setInterval" | "queueMicrotask"
/**
* Get usage of `setTimeout`, `setInterval`, `queueMicrotask`
*/
export function* extractTaskReferences(
context: RuleContext,
functionNames: FunctionName[] = [
"setTimeout",
"setInterval",
"queueMicrotask",
],
): Generator<{ node: TSESTree.CallExpression; name: string }, void> {
const referenceTracker = new ReferenceTracker(
context.getSourceCode().scopeManager.globalScope!,
)
for (const { node, path } of referenceTracker.iterateGlobalReferences({
setTimeout: {
[ReferenceTracker.CALL]: functionNames.includes("setTimeout"),
},
setInterval: {
[ReferenceTracker.CALL]: functionNames.includes("setInterval"),
},
queueMicrotask: {
[ReferenceTracker.CALL]: functionNames.includes("queueMicrotask"),
},
})) {
yield {
node: node as TSESTree.CallExpression,
name: path[path.length - 1],
}
}
}