feat(compiler): Support default parameters in static reflector (#10370)

Closes: #10369
This commit is contained in:
Chuck Jazdzewski
2016-07-29 09:10:45 -07:00
committed by GitHub
parent 0eca7abdd8
commit 763ca60f5b
5 changed files with 88 additions and 18 deletions

View File

@ -308,12 +308,17 @@ export class StaticReflector implements ReflectorReader {
}
calling.set(functionSymbol, true);
try {
let value = targetFunction['value'];
const value = targetFunction['value'];
if (value && (depth != 0 || value.__symbolic != 'error')) {
// Determine the arguments
let args = (expression['arguments'] || []).map((arg: any) => simplify(arg));
let parameters: string[] = targetFunction['parameters'];
let functionScope = BindingScope.build();
const args: any[] =
(expression['arguments'] || []).map((arg: any) => simplify(arg));
const parameters: string[] = targetFunction['parameters'];
const defaults: any[] = targetFunction.defaults;
if (defaults && defaults.length > args.length) {
args.push(...defaults.slice(args.length).map((value: any) => simplify(value)));
}
const functionScope = BindingScope.build();
for (let i = 0; i < parameters.length; i++) {
functionScope.define(parameters[i], args[i]);
}

View File

@ -421,6 +421,14 @@ describe('StaticReflector', () => {
[{provider: 'a', useValue: '1'}], [{provider: 'a', useValue: '2'}]
]);
});
it('should be able to get the metadata for a class calling a method with default parameters',
() => {
const annotations = reflector.annotations(
host.getStaticSymbol('/tmp/src/static-method-call.ts', 'MyDefaultsComponent'));
expect(annotations.length).toBe(1);
expect(annotations[0].providers).toEqual([['a', true, false]]);
});
});
class MockReflectorHost implements StaticReflectorHost {
@ -973,6 +981,9 @@ class MockReflectorHost implements StaticReflectorHost {
static condMethod(cond: boolean) {
return [{ provider: 'a', useValue: cond ? '1' : '2'}];
}
static defaultsMethod(a, b = true, c = false) {
return [a, b, c];
}
}
`,
'/tmp/src/static-method-call.ts': `
@ -988,6 +999,11 @@ class MockReflectorHost implements StaticReflectorHost {
providers: [MyModule.condMethod(true), MyModule.condMethod(false)]
})
export class MyCondComponent { }
@Component({
providers: [MyModule.defaultsMethod('a')]
})
export class MyDefaultsComponent { }
`,
'/tmp/src/static-field.ts': `
import {Injectable} from 'angular2/core';