mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
Kristiyan Kostadinov
parent
a4a8573a75
commit
ec48c8305c
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user