From ae93ba1140b62b1b01182fe709bd2a4875701495 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 23 Apr 2019 17:49:31 +0200 Subject: [PATCH] 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 --- .../src/ngtsc/partial_evaluator/src/interpreter.ts | 2 +- .../ngtsc/partial_evaluator/test/evaluator_spec.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts b/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts index 3d5698c333..4d2300f30f 100644 --- a/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts +++ b/packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts @@ -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; diff --git a/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts b/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts index 7a48703160..30c5b608fe 100644 --- a/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts +++ b/packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts @@ -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');