fix(core): return null when getDirectiveMetadata is called with null or undefined

Safely return null instead of throwing an unhandled TypeError when getDirectiveMetadata is called with a falsy directive or component instance.

(cherry picked from commit cea6896a0f)
This commit is contained in:
Edu
2026-09-02 00:19:21 +00:00
committed by Kristiyan Kostadinov
parent a4a8573a75
commit ec48c8305c
2 changed files with 8 additions and 0 deletions
@@ -304,6 +304,9 @@ export type DirectiveDebugMetadata =
export function getDirectiveMetadata(
directiveOrComponentInstance: any,
): AngularComponentDebugMetadata | AngularDirectiveDebugMetadata | null {
if (!directiveOrComponentInstance) {
return null;
}
const {constructor} = directiveOrComponentInstance;
if (!constructor) {
throw new Error('Unable to find the instance constructor');
@@ -405,6 +405,11 @@ describe('discovery utils', () => {
expect(metadata!.inputs).toEqual({a: 'b'});
expect(metadata!.outputs).toEqual({c: 'd'});
});
it('should return null when passed null or undefined', () => {
expect(getDirectiveMetadata(null)).toBeNull();
expect(getDirectiveMetadata(undefined)).toBeNull();
});
});
});