Skip to content

Commit 96f36d6

Browse files
dgp1130thePunderWoman
authored andcommitted
refactor(devtools): use ng.getDirectiveMetadata for component name (angular#60475)
This reads the component name from `ng.getDirectiveMetadata`. PR Close angular#60475
1 parent d639a8d commit 96f36d6

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

devtools/projects/ng-devtools-backend/src/lib/directive-forest/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("//devtools/tools:typescript.bzl", "ts_library", "ts_test_library")
21
load("//devtools/tools:defaults.bzl", "karma_web_test_suite")
2+
load("//devtools/tools:typescript.bzl", "ts_library", "ts_test_library")
33

44
package(default_visibility = ["//visibility:public"])
55

@@ -37,6 +37,7 @@ ts_test_library(
3737
":directive-forest",
3838
"//devtools/projects/ng-devtools-backend/src/lib:interfaces",
3939
"//devtools/projects/ng-devtools-backend/src/lib:utils",
40+
"//packages/core",
4041
"@npm//@types",
4142
],
4243
)

devtools/projects/ng-devtools-backend/src/lib/directive-forest/render-tree.spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,36 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {
10+
ɵDirectiveDebugMetadata as DirectiveDebugMetadata,
11+
ɵFramework as Framework,
12+
ɵFrameworkAgnosticGlobalUtils as FrameworkAgnosticGlobalUtils,
13+
} from '@angular/core';
914
import {RTreeStrategy} from './render-tree';
1015

1116
describe('render tree extraction', () => {
1217
let treeStrategy: RTreeStrategy;
1318
let directiveMap: Map<Node, any[]>;
1419
let componentMap: Map<Element, any>;
20+
let directiveMetadataMap: Map<any, DirectiveDebugMetadata>;
1521

1622
beforeEach(() => {
1723
treeStrategy = new RTreeStrategy();
1824
directiveMap = new Map();
1925
componentMap = new Map();
26+
directiveMetadataMap = new Map();
2027

2128
(window as any).ng = {
22-
getDirectiveMetadata(): void {},
29+
getDirectiveMetadata(dir: any): DirectiveDebugMetadata | null {
30+
return directiveMetadataMap.get(dir) ?? null;
31+
},
2332
getComponent(element: Element): any {
2433
return componentMap.get(element);
2534
},
2635
getDirectives(node: Node): any {
2736
return directiveMap.get(node) || [];
2837
},
29-
};
38+
} satisfies Partial<FrameworkAgnosticGlobalUtils>;
3039
});
3140

3241
afterEach(() => delete (window as any).ng);
@@ -106,4 +115,20 @@ describe('render tree extraction', () => {
106115
expect(rtree[0].children[0].children.length).toBe(0);
107116
expect(rtree[1].component?.instance).toBe(siblingComponent);
108117
});
118+
119+
it('should use component name from `ng.getDirectiveMetadata`', () => {
120+
const appNode = document.createElement('app');
121+
122+
const appComponent = {};
123+
componentMap.set(appNode, appComponent);
124+
directiveMetadataMap.set(appComponent, {
125+
framework: Framework.Angular,
126+
name: 'AppComponent',
127+
inputs: {},
128+
outputs: {},
129+
});
130+
131+
const rtree = treeStrategy.build(appNode);
132+
expect(rtree[0].component!.name).toBe('AppComponent');
133+
});
109134
});

devtools/projects/ng-devtools-backend/src/lib/directive-forest/render-tree.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ɵHydratedNode as HydrationNode} from '@angular/core';
9+
import type {
10+
ɵFrameworkAgnosticGlobalUtils as FrameworkAgnosticGlobalUtils,
11+
ɵHydratedNode as HydrationNode,
12+
} from '@angular/core';
1013
import {HydrationStatus} from 'protocol';
1114

1215
import {ComponentTreeNode} from '../interfaces';
@@ -16,8 +19,9 @@ import {isCustomElement} from '../utils';
1619
const extractViewTree = (
1720
domNode: Node | Element,
1821
result: ComponentTreeNode[],
19-
getComponent?: (element: Element) => {} | null,
20-
getDirectives?: (node: Node) => {}[],
22+
getComponent?: FrameworkAgnosticGlobalUtils['getComponent'],
23+
getDirectives?: FrameworkAgnosticGlobalUtils['getDirectives'],
24+
getDirectiveMetadata?: FrameworkAgnosticGlobalUtils['getDirectiveMetadata'],
2125
): ComponentTreeNode[] => {
2226
// Ignore DOM Node if it came from a different frame. Use instanceof Node to check this.
2327
if (!(domNode instanceof Node)) {
@@ -50,19 +54,25 @@ const extractViewTree = (
5054
componentTreeNode.component = {
5155
instance: component,
5256
isElement: isCustomElement(domNode),
53-
name: domNode.nodeName.toLowerCase(),
57+
name: getDirectiveMetadata?.(component)?.name ?? domNode.nodeName.toLowerCase(),
5458
};
5559
}
5660
if (component || componentTreeNode.directives.length) {
5761
result.push(componentTreeNode);
5862
}
5963
if (componentTreeNode.component || componentTreeNode.directives.length) {
6064
domNode.childNodes.forEach((node) =>
61-
extractViewTree(node, componentTreeNode.children, getComponent, getDirectives),
65+
extractViewTree(
66+
node,
67+
componentTreeNode.children,
68+
getComponent,
69+
getDirectives,
70+
getDirectiveMetadata,
71+
),
6272
);
6373
} else {
6474
domNode.childNodes.forEach((node) =>
65-
extractViewTree(node, result, getComponent, getDirectives),
75+
extractViewTree(node, result, getComponent, getDirectives, getDirectiveMetadata),
6676
);
6777
}
6878
return result;
@@ -98,8 +108,8 @@ export class RTreeStrategy {
98108
while (element.parentElement) {
99109
element = element.parentElement;
100110
}
101-
const getComponent = ngDebugClient().getComponent;
102-
const getDirectives = ngDebugClient().getDirectives;
103-
return extractViewTree(element, [], getComponent, getDirectives);
111+
112+
const ng = ngDebugClient();
113+
return extractViewTree(element, [], ng.getComponent, ng.getDirectives, ng.getDirectiveMetadata);
104114
}
105115
}

0 commit comments

Comments
 (0)