fix(7877): StaticReflector returns empty results instead of undefined.

Reflector always returns either an empty object or an empty list if no
metadata is recorded for the class. StaticReflector matches this
behavior.

Closes #7986
This commit is contained in:
Chuck Jazdzewski 2016-04-08 15:39:21 -07:00 committed by Robert Messerle
parent 2abb414cfb
commit fc496813e2
2 changed files with 46 additions and 6 deletions

View File

@ -90,6 +90,8 @@ export class StaticReflector {
annotations = (<any[]>classMetadata['decorators'])
.map(decorator => this.convertKnownDecorator(type.moduleId, decorator))
.filter(decorator => isPresent(decorator));
} else {
annotations = [];
}
this.annotationCache.set(type, annotations);
}
@ -101,6 +103,9 @@ export class StaticReflector {
if (!isPresent(propMetadata)) {
let classMetadata = this.getTypeMetadata(type);
propMetadata = this.getPropertyMetadata(type.moduleId, classMetadata['members']);
if (!isPresent(propMetadata)) {
propMetadata = {};
}
this.propertyCache.set(type, propMetadata);
}
return propMetadata;
@ -110,12 +115,20 @@ export class StaticReflector {
let parameters = this.parameterCache.get(type);
if (!isPresent(parameters)) {
let classMetadata = this.getTypeMetadata(type);
let ctorData = classMetadata['members']['__ctor__'];
if (isPresent(ctorData)) {
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] === 'constructor');
parameters = this.simplify(type.moduleId, ctor['parameters']);
this.parameterCache.set(type, parameters);
if (isPresent(classMetadata)) {
let members = classMetadata['members'];
if (isPresent(members)) {
let ctorData = members['__ctor__'];
if (isPresent(ctorData)) {
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] === 'constructor');
parameters = this.simplify(type.moduleId, ctor['parameters']);
}
}
}
if (!isPresent(parameters)) {
parameters = [];
}
this.parameterCache.set(type, parameters);
}
return parameters;
}
@ -290,7 +303,7 @@ export class StaticReflector {
});
return result;
}
return null;
return {};
}
// clang-format off

View File

@ -60,6 +60,15 @@ export function main() {
expect(annotation.selector).toEqual('my-hero-detail');
});
it('should get and empty annotation list for an unknown class', () => {
let host = new MockReflectorHost();
let reflector = new StaticReflector(host);
let UnknownClass = reflector.getStaticType('./app/app.component', 'UnknownClass');
let annotations = reflector.annotations(UnknownClass);
expect(annotations).toEqual([]);
});
it('should get propMetadata for HeroDetailComponent', () => {
let host = new MockReflectorHost();
let reflector = new StaticReflector(host);
@ -70,6 +79,24 @@ export function main() {
expect(props['hero']).toBeTruthy();
});
it('should get an empty object from propMetadata for an unknown class', () => {
let host = new MockReflectorHost();
let reflector = new StaticReflector(host);
let UnknownClass = reflector.getStaticType('./app/app.component', 'UnknownClass');
let properties = reflector.propMetadata(UnknownClass);
expect(properties).toEqual({});
});
it('should get empty parameters list for an unknown class ', () => {
let host = new MockReflectorHost();
let reflector = new StaticReflector(host);
let UnknownClass = reflector.getStaticType('./app/app.component', 'UnknownClass');
let parameters = reflector.parameters(UnknownClass);
expect(parameters).toEqual([]);
});
it('should simplify primitive into itself', () => {
let host = new MockReflectorHost();
let reflector = new StaticReflector(host);