From e2698a3124fedf07a90d4b1369bccb073864948c Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Mon, 6 Mar 2023 16:33:07 -0500 Subject: [PATCH] [rhir] Add printDeps for debugging reactive dependency trees --- .../DeriveMinimalDependencies.ts | 35 +++++++++++++++++++ .../PropagateScopeDependencies.ts | 9 +++++ 2 files changed, 44 insertions(+) diff --git a/compiler/forget/src/ReactiveScopes/DeriveMinimalDependencies.ts b/compiler/forget/src/ReactiveScopes/DeriveMinimalDependencies.ts index b7c42bf64a..fb075943f4 100644 --- a/compiler/forget/src/ReactiveScopes/DeriveMinimalDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/DeriveMinimalDependencies.ts @@ -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 { + const results: Array = []; + 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, V, U>( arr: Array, fn: (arg0: U) => T | undefined | null diff --git a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts index 2b82ce0db5..07bf316cf9 100644 --- a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -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