8000 fix(core): properly handle the case where getSignalGraph is called on… · angular/angular@3683156 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3683156

Browse files
fix(core): properly handle the case where getSignalGraph is called on a componentless NodeInjector
Previously this would throw an error on the assertLView when we try to discover the templateLView. Now this properly returns null for the template consumer and continues discovering other effects on the injector.
1 parent 1e79d47 commit 3683156

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

packages/core/src/render3/util/signal_debug.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
SIGNAL_NODE,
2626
SignalNode,
2727
} from '../../../primitives/signals';
28+
import {isLView} from '../interfaces/type_checks';
2829

2930
export interface DebugSignalGraphNode {
3031
kind: string;
@@ -79,9 +80,10 @@ function getTemplateConsumer(injector: NodeInjector): ReactiveLViewConsumer | nu
7980
const lView = getNodeInjectorLView(injector)!;
8081
assertLView(lView);
8182
const templateLView = lView[tNode.index]!;
82-
assertLView(templateLView);
83-
84-
return templateLView[REACTIVE_TEMPLATE_CONSUMER];
83+
if (isLView(templateLView)) {
84+
return templateLView[REACTIVE_TEMPLATE_CONSUMER] ?? null;
85+
}
86+
return null;
8587
}
8688

8789
function getNodesAndEdgesFromSignalMap(signalMap: ReadonlyMap<ReactiveNode, ReactiveNode[]>): {

packages/core/test/acceptance/signal_debug_spec.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Component, computed, effect, inject, Injectable, signal} from '../../src/core';
9+
import {NodeInjector} from '../../../core/src/render3/di';
10+
import {getDirectives} from '../../../core/src/render3/util/discovery_utils';
11+
import {
12+
Component,
13+
Directive,
14+
computed,
15+
effect,
16+
inject,
17+
Injectable,
18+
signal,
19+
Injector,
20+
} from '../../src/core';
1021
import {
1122
getFrameworkDIDebugData,
1223
setupFrameworkInjectorProfiler,
@@ -302,4 +313,39 @@ describe('getSignalGraph', () => {
302313
producer: fourFiveSixNode,
303314
});
304315
}));
316+
317+
it('should return no nodes or edges for a NodeInjector that only has directives and no component', () => {
318+
@Directive({
319+
selector: '[myDirective]',
320+
})
321+
class MyDirective {
322+
injector = inject(Injector);
323+
}
324+
325+
@Component({
326+
selector: 'component-with-directive',
327+
template: `<div id="element-with-directive" myDirective></div>`,
328+
imports: [MyDirective],
329+
})
330+
class WithDirective {}
331+
332+
TestBed.configureTestingModule({imports: [WithDirective]});
333+
const fixture = TestBed.createComponent(WithDirective);
334+
fixture.detectChanges();
335+
336+
const element = fixture.nativeElement.querySelector('#element-with-directive');
337+
// get the directive instance
338+
const directiveInstances = getDirectives(element);
339+
expect(directiveInstances.length).toBe(1);
340+
const directiveInstance = directiveInstances[0];
341+
expect(directiveInstance).toBeInstanceOf(MyDirective);
342+
const injector = (directiveInstance as MyDirective).injector;
343+
expect(injector).toBeInstanceOf(NodeInjector);
344+
const signalGraph = getSignalGraph(injector);
345+
expect(signalGraph).toBeDefined();
346+
347+
const {nodes, edges} = signalGraph;
348+
expect(nodes.length).toBe(0);
349+
expect(edges.length).toBe(0);
350+
});
305351
});

0 commit comments

Comments
 (0)
0