fix(static_reflector): resolve values of functions in the function context

This commit is contained in:
Tobias Bosch 2016-07-25 05:29:20 -07:00
parent 6f4e49ed53
commit d6b65db9a7
2 changed files with 20 additions and 12 deletions

View File

@ -279,16 +279,18 @@ export class StaticReflector implements ReflectorReader {
let callContext: {[name: string]: string}|undefined = undefined;
if (expression['__symbolic'] == 'call') {
let target = expression['expression'];
let functionSymbol: StaticSymbol;
let targetFunction: any;
if (target && target.__symbolic === 'reference') {
callContext = {name: target.name};
targetFunction = resolveReferenceValue(resolveReference(context, target));
functionSymbol = resolveReference(context, target);
targetFunction = resolveReferenceValue(functionSymbol);
}
if (targetFunction && targetFunction['__symbolic'] == 'function') {
if (calling.get(targetFunction)) {
if (calling.get(functionSymbol)) {
throw new Error('Recursion not supported');
}
calling.set(targetFunction, true);
calling.set(functionSymbol, true);
let value = targetFunction['value'];
if (value) {
// Determine the arguments
@ -302,13 +304,13 @@ export class StaticReflector implements ReflectorReader {
let result: any;
try {
scope = functionScope.done();
result = simplify(value);
result = simplifyInContext(functionSymbol, value, depth + 1);
} finally {
scope = oldScope;
}
return result;
}
calling.delete(targetFunction);
calling.delete(functionSymbol);
}
}

View File

@ -322,8 +322,8 @@ describe('StaticReflector', () => {
})).toEqual(['some-value']);
expect(simplify(new StaticSymbol('/tmp/src/function-reference.ts', ''), {
__symbolic: 'reference',
name: 'two'
})).toEqual(2);
name: 'three'
})).toEqual(3);
});
it('should error on direct recursive calls', () => {
@ -332,7 +332,7 @@ describe('StaticReflector', () => {
new StaticSymbol('/tmp/src/function-reference.ts', ''),
{__symbolic: 'reference', name: 'recursion'}))
.toThrow(new Error(
'Recursion not supported, resolving symbol recursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts'));
'Recursion not supported, resolving symbol recursive in /tmp/src/function-recursive.d.ts, resolving symbol recursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts'));
});
it('should error on indirect recursive calls', () => {
@ -341,7 +341,7 @@ describe('StaticReflector', () => {
new StaticSymbol('/tmp/src/function-reference.ts', ''),
{__symbolic: 'reference', name: 'indirectRecursion'}))
.toThrow(new Error(
'Recursion not supported, resolving symbol indirectRecursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts'));
'Recursion not supported, resolving symbol indirectRecursion2 in /tmp/src/function-recursive.d.ts, resolving symbol indirectRecursion1 in /tmp/src/function-recursive.d.ts, resolving symbol indirectRecursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts'));
});
it('should simplify a spread expression', () => {
@ -749,9 +749,15 @@ class MockReflectorHost implements StaticReflectorHost {
__symbolic: 'binop',
operator: '+',
left: {__symbolic: 'reference', name: 'a'},
right: {__symbolic: 'reference', name: 'b'}
right: {
__symbolic: 'binop',
operator: '+',
left: {__symbolic: 'reference', name: 'b'},
right: {__symbolic: 'reference', name: 'oneLiteral'}
}
}
}
},
oneLiteral: 1
}
},
'/tmp/src/function-reference.ts': {
@ -767,7 +773,7 @@ class MockReflectorHost implements StaticReflectorHost {
},
arguments: ['some-value']
},
two: {
three: {
__symbolic: 'call',
expression: {
__symbolic: 'reference',