-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.ts
73 lines (61 loc) · 1.36 KB
/
context.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
/*eslint no-unused-private-class-members: "error"*/
import type * as tsm from "ts-morph";
import { ts } from "ts-morph";
type ContextNode = {
getKind(): ts.SyntaxKind;
getText(): string;
};
export class Context {
parent?: Context;
node: ContextNode;
debug: boolean;
numParents = 0;
constructor(node: ContextNode, parent?: Context, debug: boolean = false) {
this.node = node;
this.parent = parent;
this.debug = !!(debug || (parent && parent.debug));
this.numParents = parent ? parent.numParents + 1 : 0;
}
toString(): string {
let pre = "";
if (this.parent) {
pre = this.parent.toString() + "\n";
}
pre += " ".repeat(this.numParents);
return pre + this.toStringWithoutParent();
}
toStringWithoutParent(): string {
switch (this.node.getKind()) {
case ts.SyntaxKind.SourceFile:
return "SourceFile";
}
return this.node.getText() + " [[" + ts.SyntaxKind[this.node.getKind()] + "]]";
}
log(...args: unknown[]) {
if (!this.debug) {
return;
}
console.log(this.toString(), ...args);
}
time(label: string) {
if (!this.debug) {
return;
}
console.time(label);
}
timeEnd(label: string) {
if (!this.debug) {
return;
}
console.timeEnd(label);
}
hasChecked(node: tsm.Node): boolean {
if (this.node === node) {
return true;
}
if (!this.parent) {
return false;
}
return this.parent.hasChecked(node);
}
}