feat(compiler): Added support for references to static fields. (#10334)

Closes: #10332
This commit is contained in:
Chuck Jazdzewski
2016-07-27 19:26:59 -07:00
committed by GitHub
parent 422effdd18
commit b58e9ea775
4 changed files with 97 additions and 4 deletions

View File

@ -438,8 +438,17 @@ export class StaticReflector implements ReflectorReader {
return null;
case 'select':
let selectTarget = simplify(expression['expression']);
let member = simplify(expression['member']);
if (selectTarget && isPrimitive(member)) return selectTarget[member];
if (selectTarget instanceof StaticSymbol) {
// Access to a static instance variable
const declarationValue = resolveReferenceValue(selectTarget);
if (declarationValue && declarationValue.statics) {
selectTarget = declarationValue.statics;
} else {
return null;
}
}
const member = simplify(expression['member']);
if (selectTarget && isPrimitive(member)) return simplify(selectTarget[member]);
return null;
case 'reference':
if (!expression.module) {

View File

@ -404,6 +404,13 @@ describe('StaticReflector', () => {
expect(annotations.length).toBe(1);
expect(annotations[0].providers).toEqual({provider: 'a', useValue: 100});
});
it('should be able to get metadata for a class containing a static field reference', () => {
const annotations =
reflector.annotations(host.getStaticSymbol('/tmp/src/static-field-reference.ts', 'Foo'));
expect(annotations.length).toBe(1);
expect(annotations[0].providers).toEqual([{provider: 'a', useValue: 'Some string'}]);
});
});
class MockReflectorHost implements StaticReflectorHost {
@ -963,6 +970,23 @@ class MockReflectorHost implements StaticReflectorHost {
providers: MyModule.with(100)
})
export class MyComponent { }
`,
'/tmp/src/static-field.ts': `
import {Injectable} from 'angular2/core';
@Injectable()
export class MyModule {
static VALUE = 'Some string';
}
`,
'/tmp/src/static-field-reference.ts': `
import {Component} from 'angular2/src/core/metadata';
import {MyModule} from './static-field';
@Component({
providers: [ { provider: 'a', useValue: MyModule.VALUE } ]
})
export class Foo { }
`
};