Skip to content

Commit feb94a7

Browse files
authored
Speedup doc generation when running bun run dev (#204)
1 parent a872436 commit feb94a7

File tree

4 files changed

+832
-791
lines changed

4 files changed

+832
-791
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*eslint no-unused-private-class-members: "error"*/
2+
3+
import type * as tsm from "ts-morph";
4+
import { ts } from "ts-morph";
5+
6+
type ContextNode = {
7+
getKind(): ts.SyntaxKind;
8+
getText(): string;
9+
};
10+
11+
export class Context {
12+
parent?: Context;
13+
node: ContextNode;
14+
debug: boolean;
15+
numParents = 0;
16+
17+
constructor(node: ContextNode, parent?: Context, debug: boolean = false) {
18+
this.node = node;
19+
this.parent = parent;
20+
this.debug = !!(debug || (parent && parent.debug));
21+
this.numParents = parent ? parent.numParents + 1 : 0;
22+
}
23+
24+
toString(): string {
25+
let pre = "";
26+
if (this.parent) {
27+
pre = this.parent.toString() + "\n";
28+
}
29+
pre += " ".repeat(this.numParents);
30+
return pre + this.toStringWithoutParent();
31+
}
32+
33+
toStringWithoutParent(): string {
34+
switch (this.node.getKind()) {
35+
case ts.SyntaxKind.SourceFile:
36+
return "SourceFile";
37+
}
38+
39+
return this.node.getText() + " [[" + ts.SyntaxKind[this.node.getKind()] + "]]";
40+
}
41+
42+
log(...args: unknown[]) {
43+
if (!this.debug) {
44+
return;
45+
}
46+
console.log(this.toString(), ...args);
47+
}
48+
49+
time(label: string) {
50+
if (!this.debug) {
51+
return;
52+
}
53+
54+
console.time(label);
55+
}
56+
57+
timeEnd(label: string) {
58+
if (!this.debug) {
59+
return;
60+
}
61+
console.timeEnd(label);
62+
}
63+
64+
hasChecked(node: tsm.Node): boolean {
65+
if (this.node === node) {
66+
return true;
67+
}
68+
if (!this.parent) {
69+
return false;
70+
}
71+
return this.parent.hasChecked(node);
72+
}
73+
}

0 commit comments

Comments
 (0)