fix(ivy): don't throw error when evaluating function with more than one statement (#30061)

Resolves functions with more than one statement to unknown dynamic values, rather than throwing an error.

PR Close #30061
This commit is contained in:
Kristiyan Kostadinov 2019-04-23 17:49:31 +02:00 committed by Andrew Kushnir
parent 2e21997016
commit ae93ba1140
2 changed files with 12 additions and 1 deletions

View File

@ -421,7 +421,7 @@ export class StaticInterpreter {
const body = fn.body;
if (body.length !== 1 || !ts.isReturnStatement(body[0])) {
throw new Error('Function body must have a single return statement only.');
return DynamicValue.fromUnknown(node);
}
const ret = body[0] as ts.ReturnStatement;

View File

@ -356,6 +356,17 @@ describe('ngtsc metadata', () => {
expect(id.text).toEqual('Target');
});
it('should resolve functions with more than one statement to an unknown value', () => {
const value = evaluate(`function foo(bar) { const b = bar; return b; }`, 'foo("test")');
if (!(value instanceof DynamicValue)) {
return fail(`Should have resolved to a DynamicValue`);
}
expect(value.isFromUnknown()).toBe(true);
expect((value.node as ts.CallExpression).expression.getText()).toBe('foo');
});
describe('(visited file tracking)', () => {
it('should track each time a source file is visited', () => {
const visitedFilesSpy = jasmine.createSpy('visitedFilesCb');