Skip to content

Commit ca6295e

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
fix(core): handle different DI token types in Chrome DevTools integration (#61333)
This small refactor makes the DI events reporting code more resiliant with respect to finding names for different token types. PR Close #61333
1 parent 432bf4a commit ca6295e

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

packages/core/src/render3/debug/chrome_dev_tools_performance.ts

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

9-
import {InjectionToken} from '../../di';
109
import {isTypeProvider} from '../../di/provider_collection';
1110
import {assertDefined, assertEqual} from '../../util/assert';
1211
import {performanceMarkFeature} from '../../util/performance';
@@ -81,7 +80,6 @@ function measureEnd(
8180
`Profiling error: expected to see ${startEvent} event but got ${top[0]}`,
8281
);
8382

84-
// Expecting TypeScript error here as overloaded types are not supported yet in TS types
8583
console.timeStamp(
8684
entryName,
8785
'Event_' + top[0] + '_' + top[1],
@@ -216,13 +214,12 @@ function getComponentMeasureName(instance: {}) {
216214
}
217215

218216
function getProviderTokenMeasureName<T>(token: any) {
219-
if (token instanceof InjectionToken) {
220-
return token.toString();
221-
} else if (isTypeProvider(token)) {
217+
if (isTypeProvider(token)) {
222218
return token.name;
223-
} else {
219+
} else if (token.provide != null) {
224220
return getProviderTokenMeasureName(token.provide);
225221
}
222+
return token.toString();
226223
}
227224

228225
/**
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {enableProfiling} from '../../src/render3/debug/chrome_dev_tools_performance';
10+
import {Component, HostAttributeToken, inject, Inject} from '../../src/core';
11+
import {TestBed} from '../../testing';
12+
13+
describe('Chrome DevTools Performance integration', () => {
14+
describe('DI perf events', () => {
15+
it('should not crash when a HostAttributeToken is injected', () => {
16+
@Component({
17+
template: ``,
18+
})
19+
class MyCmp {
20+
attr = inject(new HostAttributeToken('someAttr'), {optional: true});
21+
}
22+
23+
spyOn(console, 'timeStamp');
24+
const stopProfiling = enableProfiling();
25+
26+
try {
27+
const fixture = TestBed.createComponent(MyCmp);
28+
fixture.detectChanges();
29+
30+
expect((console.timeStamp as any).calls.all().length).not.toBe(0);
31+
} finally {
32+
stopProfiling();
33+
}
34+
});
35+
36+
it('should not crash when a string provider is injected', () => {
37+
@Component({
38+
template: ``,
39+
providers: [
40+
{
41+
provide: 'foo',
42+
useValue: 'bar',
43+
},
44+
],
45+
})
46+
class MyCmp {
47+
constructor(@Inject('foo') foo: string) {}
48+
}
49+
50+
spyOn(console, 'timeStamp');
51+
const stopProfiling = enableProfiling();
52+
53+
try {
54+
const fixture = TestBed.createComponent(MyCmp);
55+
fixture.detectChanges();
56+
57+
expect((console.timeStamp as any).calls.all().length).not.toBe(0);
58+
} finally {
59+
stopProfiling();
60+
}
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)