diff --git a/packages/compiler/src/aot/static_reflector.ts b/packages/compiler/src/aot/static_reflector.ts index c60b6dbe24..9964474086 100644 --- a/packages/compiler/src/aot/static_reflector.ts +++ b/packages/compiler/src/aot/static_reflector.ts @@ -235,7 +235,9 @@ export class StaticReflector implements CompileReflector { const prop = (propData) .find(a => a['__symbolic'] == 'property' || a['__symbolic'] == 'method'); const decorators: any[] = []; - if (propMetadata![propName]) { + // hasOwnProperty() is used here to make sure we do not look up methods + // on `Object.prototype`. + if (propMetadata?.hasOwnProperty(propName)) { decorators.push(...propMetadata![propName]); } propMetadata![propName] = decorators; diff --git a/packages/compiler/test/aot/static_reflector_spec.ts b/packages/compiler/test/aot/static_reflector_spec.ts index d1ae5dd5a4..cf56450fbe 100644 --- a/packages/compiler/test/aot/static_reflector_spec.ts +++ b/packages/compiler/test/aot/static_reflector_spec.ts @@ -777,6 +777,35 @@ describe('StaticReflector', () => { .toEqual({}); }); + it('should not inherit methods from Object.prototype', () => { + const filePath = '/tmp/test.ts'; + init({ + ...DEFAULT_TEST_DATA, + [filePath]: ` + import {Component} from '@angular/core'; + + @Component({ + selector: 'test-component', + }) + export class TestComponent { + title = 'Hello World'; + + toString() { + return 'Test Component'; + } + } + `, + }); + const declaration = reflector.getStaticSymbol(filePath, 'TestComponent'); + expect(declaration.filePath).toBe(filePath); + expect(declaration.name).toBe('TestComponent'); + const propMetadata = reflector.propMetadata(declaration); + // 'toString' is a member of TestComponent so it should be part of the metadata. + expect(propMetadata.hasOwnProperty('toString')).toBe(true); + // There are no decorators on 'toString' so it should be an empty array. + expect(propMetadata['toString']).toEqual([]); + }); + it('should inherit lifecycle hooks', () => { initWithDecorator({ '/tmp/src/main.ts': `