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:
parent
2abb414cfb
commit
fc496813e2
|
@ -90,6 +90,8 @@ export class StaticReflector {
|
||||||
annotations = (<any[]>classMetadata['decorators'])
|
annotations = (<any[]>classMetadata['decorators'])
|
||||||
.map(decorator => this.convertKnownDecorator(type.moduleId, decorator))
|
.map(decorator => this.convertKnownDecorator(type.moduleId, decorator))
|
||||||
.filter(decorator => isPresent(decorator));
|
.filter(decorator => isPresent(decorator));
|
||||||
|
} else {
|
||||||
|
annotations = [];
|
||||||
}
|
}
|
||||||
this.annotationCache.set(type, annotations);
|
this.annotationCache.set(type, annotations);
|
||||||
}
|
}
|
||||||
|
@ -101,6 +103,9 @@ export class StaticReflector {
|
||||||
if (!isPresent(propMetadata)) {
|
if (!isPresent(propMetadata)) {
|
||||||
let classMetadata = this.getTypeMetadata(type);
|
let classMetadata = this.getTypeMetadata(type);
|
||||||
propMetadata = this.getPropertyMetadata(type.moduleId, classMetadata['members']);
|
propMetadata = this.getPropertyMetadata(type.moduleId, classMetadata['members']);
|
||||||
|
if (!isPresent(propMetadata)) {
|
||||||
|
propMetadata = {};
|
||||||
|
}
|
||||||
this.propertyCache.set(type, propMetadata);
|
this.propertyCache.set(type, propMetadata);
|
||||||
}
|
}
|
||||||
return propMetadata;
|
return propMetadata;
|
||||||
|
@ -110,13 +115,21 @@ export class StaticReflector {
|
||||||
let parameters = this.parameterCache.get(type);
|
let parameters = this.parameterCache.get(type);
|
||||||
if (!isPresent(parameters)) {
|
if (!isPresent(parameters)) {
|
||||||
let classMetadata = this.getTypeMetadata(type);
|
let classMetadata = this.getTypeMetadata(type);
|
||||||
let ctorData = classMetadata['members']['__ctor__'];
|
if (isPresent(classMetadata)) {
|
||||||
|
let members = classMetadata['members'];
|
||||||
|
if (isPresent(members)) {
|
||||||
|
let ctorData = members['__ctor__'];
|
||||||
if (isPresent(ctorData)) {
|
if (isPresent(ctorData)) {
|
||||||
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] === 'constructor');
|
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] === 'constructor');
|
||||||
parameters = this.simplify(type.moduleId, ctor['parameters']);
|
parameters = this.simplify(type.moduleId, ctor['parameters']);
|
||||||
this.parameterCache.set(type, parameters);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (!isPresent(parameters)) {
|
||||||
|
parameters = [];
|
||||||
|
}
|
||||||
|
this.parameterCache.set(type, parameters);
|
||||||
|
}
|
||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +303,7 @@ export class StaticReflector {
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return null;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
|
|
@ -60,6 +60,15 @@ export function main() {
|
||||||
expect(annotation.selector).toEqual('my-hero-detail');
|
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', () => {
|
it('should get propMetadata for HeroDetailComponent', () => {
|
||||||
let host = new MockReflectorHost();
|
let host = new MockReflectorHost();
|
||||||
let reflector = new StaticReflector(host);
|
let reflector = new StaticReflector(host);
|
||||||
|
@ -70,6 +79,24 @@ export function main() {
|
||||||
expect(props['hero']).toBeTruthy();
|
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', () => {
|
it('should simplify primitive into itself', () => {
|
||||||
let host = new MockReflectorHost();
|
let host = new MockReflectorHost();
|
||||||
let reflector = new StaticReflector(host);
|
let reflector = new StaticReflector(host);
|
||||||
|
|
Loading…
Reference in New Issue