feat(compiler): Added support for conditional expressions. (#10366)
Expression evaluated by the static reflector can now supports conditional expressions. Closes: #10365
This commit is contained in:
@ -307,26 +307,29 @@ export class StaticReflector implements ReflectorReader {
|
||||
throw new Error('Recursion not supported');
|
||||
}
|
||||
calling.set(functionSymbol, true);
|
||||
let 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();
|
||||
for (let i = 0; i < parameters.length; i++) {
|
||||
functionScope.define(parameters[i], args[i]);
|
||||
try {
|
||||
let 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();
|
||||
for (let i = 0; i < parameters.length; i++) {
|
||||
functionScope.define(parameters[i], args[i]);
|
||||
}
|
||||
let oldScope = scope;
|
||||
let result: any;
|
||||
try {
|
||||
scope = functionScope.done();
|
||||
result = simplifyInContext(functionSymbol, value, depth + 1);
|
||||
} finally {
|
||||
scope = oldScope;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
let oldScope = scope;
|
||||
let result: any;
|
||||
try {
|
||||
scope = functionScope.done();
|
||||
result = simplifyInContext(functionSymbol, value, depth + 1);
|
||||
} finally {
|
||||
scope = oldScope;
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
calling.delete(functionSymbol);
|
||||
}
|
||||
calling.delete(functionSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
@ -417,6 +420,10 @@ export class StaticReflector implements ReflectorReader {
|
||||
return left % right;
|
||||
}
|
||||
return null;
|
||||
case 'if':
|
||||
let condition = simplify(expression['condition']);
|
||||
return condition ? simplify(expression['thenExpression']) :
|
||||
simplify(expression['elseExpression']);
|
||||
case 'pre':
|
||||
let operand = simplify(expression['operand']);
|
||||
if (shouldIgnore(operand)) return operand;
|
||||
|
@ -411,6 +411,16 @@ describe('StaticReflector', () => {
|
||||
expect(annotations.length).toBe(1);
|
||||
expect(annotations[0].providers).toEqual([{provider: 'a', useValue: 'Some string'}]);
|
||||
});
|
||||
|
||||
it('should be able to get the metadata for a class calling a method with a conditional expression',
|
||||
() => {
|
||||
const annotations = reflector.annotations(
|
||||
host.getStaticSymbol('/tmp/src/static-method-call.ts', 'MyCondComponent'));
|
||||
expect(annotations.length).toBe(1);
|
||||
expect(annotations[0].providers).toEqual([
|
||||
[{provider: 'a', useValue: '1'}], [{provider: 'a', useValue: '2'}]
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
class MockReflectorHost implements StaticReflectorHost {
|
||||
@ -960,6 +970,9 @@ class MockReflectorHost implements StaticReflectorHost {
|
||||
static with(data: any) {
|
||||
return { provider: 'a', useValue: data }
|
||||
}
|
||||
static condMethod(cond: boolean) {
|
||||
return [{ provider: 'a', useValue: cond ? '1' : '2'}];
|
||||
}
|
||||
}
|
||||
`,
|
||||
'/tmp/src/static-method-call.ts': `
|
||||
@ -970,6 +983,11 @@ class MockReflectorHost implements StaticReflectorHost {
|
||||
providers: MyModule.with(100)
|
||||
})
|
||||
export class MyComponent { }
|
||||
|
||||
@Component({
|
||||
providers: [MyModule.condMethod(true), MyModule.condMethod(false)]
|
||||
})
|
||||
export class MyCondComponent { }
|
||||
`,
|
||||
'/tmp/src/static-field.ts': `
|
||||
import {Injectable} from 'angular2/core';
|
||||
|
Reference in New Issue
Block a user