[rhir] Add printDeps for debugging reactive dependency trees

This commit is contained in:
Mofei Zhang
2023-03-06 16:33:07 -05:00
parent e68ea941e6
commit e2698a3124
2 changed files with 44 additions and 0 deletions
@@ -1,5 +1,6 @@
import invariant from "invariant";
import { Identifier, IdentifierId, ReactiveScopeDependency } from "../HIR";
import { printIdentifier } from "../HIR/PrintHIR";
import { assertExhaustive } from "../Utils/utils";
export type ReactiveScopeDependencyInfo = ReactiveScopeDependency & {
@@ -110,6 +111,23 @@ export class ReactiveScopeDependencyTree {
}
}
}
/**
* Prints dependency tree to string for debugging.
* @param includeAccesses
* @returns string representation of DependencyTree
*/
printDeps(includeAccesses: boolean): string {
let res = [];
for (const [rootId, rootNode] of this.#roots.entries()) {
const rootResults = printSubtree(rootNode, includeAccesses).map(
(result) => `${printIdentifier(rootId)}.${result}`
);
res.push(rootResults);
}
return res.flat().join("\n");
}
}
/**
@@ -333,6 +351,23 @@ function addSubtreeIntersection(
}
}
function printSubtree(
node: DependencyNode,
includeAccesses: boolean
): Array<string> {
const results: Array<string> = [];
for (const [propertyName, propertyNode] of node.properties) {
if (includeAccesses || isDependency(propertyNode.accessType)) {
results.push(`${propertyName} (${propertyNode.accessType})`);
}
const propertyResults = printSubtree(propertyNode, includeAccesses);
results.push(
...propertyResults.map((result) => `${propertyName}.${result}`)
);
}
return results;
}
function mapNonNull<T extends NonNullable<V>, V, U>(
arr: Array<U>,
fn: (arg0: U) => T | undefined | null
@@ -112,6 +112,15 @@ class Context {
return minScopeDependencies;
}
/**
* Prints dependency tree to string for debugging.
* @param includeAccesses
* @returns string representation of DependencyTree
*/
printDeps(includeAccesses: boolean = false): string {
return this.#dependencies.printDeps(includeAccesses);
}
/**
* We track and return unconditional accesses / deps within this conditional.
* If an object property is always used (i.e. in every conditional path), we