[rhir] Add printDeps for debugging reactive dependency trees
Mofei Zhang committed
Mar 6, 2023 at 16:33 UTC
e2698a3124fedf07a90d4b1369bccb073864948c
2 files changed
+44
compiler/forget/src/ReactiveScopes/DeriveMinimalDependencies.ts
+35
@@ -1,5 +1,6 @@
1
import invariant from "invariant";
2
import { Identifier, IdentifierId, ReactiveScopeDependency } from "../HIR";
3
+import { printIdentifier } from "../HIR/PrintHIR";
4
import { assertExhaustive } from "../Utils/utils";
5
6
export type ReactiveScopeDependencyInfo = ReactiveScopeDependency & {
@@ -110,6 +111,23 @@ export class ReactiveScopeDependencyTree {
111
}
112
}
113
}
114
+
115
+ /**
116
+ * Prints dependency tree to string for debugging.
117
+ * @param includeAccesses
118
+ * @returns string representation of DependencyTree
119
+ */
120
+ printDeps(includeAccesses: boolean): string {
121
+ let res = [];
122
+
123
+ for (const [rootId, rootNode] of this.#roots.entries()) {
124
+ const rootResults = printSubtree(rootNode, includeAccesses).map(
125
+ (result) => `${printIdentifier(rootId)}.${result}`
126
+ );
127
+ res.push(rootResults);
128
+ }
129
+ return res.flat().join("\n");
130
+ }
131
}
132
133
/**
@@ -333,6 +351,23 @@ function addSubtreeIntersection(
351
}
352
}
353
354
+function printSubtree(
355
+ node: DependencyNode,
356
+ includeAccesses: boolean
357
+): Array<string> {
358
+ const results: Array<string> = [];
359
+ for (const [propertyName, propertyNode] of node.properties) {
360
+ if (includeAccesses || isDependency(propertyNode.accessType)) {
361
+ results.push(`${propertyName} (${propertyNode.accessType})`);
362
+ }
363
+ const propertyResults = printSubtree(propertyNode, includeAccesses);
364
+ results.push(
365
+ ...propertyResults.map((result) => `${propertyName}.${result}`)
366
+ );
367
+ }
368
+ return results;
369
+}
370
+
371
function mapNonNull<T extends NonNullable<V>, V, U>(
372
arr: Array<U>,
373
fn: (arg0: U) => T | undefined | null
compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts
+9
@@ -112,6 +112,15 @@ class Context {
112
return minScopeDependencies;
113
}
114
115
+ /**
116
+ * Prints dependency tree to string for debugging.
117
+ * @param includeAccesses
118
+ * @returns string representation of DependencyTree
119
+ */
120
+ printDeps(includeAccesses: boolean = false): string {
121
+ return this.#dependencies.printDeps(includeAccesses);
122
+ }
123
+
124
/**
125
* We track and return unconditional accesses / deps within this conditional.
126
* If an object property is always used (i.e. in every conditional path), we