From 3b5a4ddac09a6d8fa11a7ad3bdb7e2b30748eb0c Mon Sep 17 00:00:00 2001 From: Benjamin Parisel Date: Thu, 20 Jan 2022 14:59:23 +0100 Subject: [PATCH] defect(expressionJs): return undefined if variable not exist in model (#8) * Check if expression is invalid * check if dependencies is not exist in model and return undefined Covers [UID-630](https://bonitasoft.atlassian.net/browse/UID-630) --- src/resolvers/ExpressionResolver.ts | 22 +++++++++++++--------- tests/resolvers/ExpressionResolver.spec.ts | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/resolvers/ExpressionResolver.ts b/src/resolvers/ExpressionResolver.ts index c6889be..7322633 100644 --- a/src/resolvers/ExpressionResolver.ts +++ b/src/resolvers/ExpressionResolver.ts @@ -11,16 +11,20 @@ export class ExpressionResolver extends Resolver { resolve(): void { // use strict. Avoid pollution of the global object. - let expression = new Function( - '$data',//inject all data - 'uiTranslate',//inject translate function - '"use strict";' + this.content); + try { + let expression = new Function( + '$data',//inject all data + 'uiTranslate',//inject translate function + '"use strict";' + this.content); - this.model[this.name] = expression( - this.model, // all data - //TODO Implement Translate - (text: string) => text// translate function - ); + this.model[this.name] = expression( + this.model, // all data + //TODO Implement Translate + (text: string) => text// translate function + ); + }catch (e) { + console.error('This following error occur on expression resolution',e); + } } hasDependencies(): boolean { diff --git a/tests/resolvers/ExpressionResolver.spec.ts b/tests/resolvers/ExpressionResolver.spec.ts index 3473405..833322a 100644 --- a/tests/resolvers/ExpressionResolver.spec.ts +++ b/tests/resolvers/ExpressionResolver.spec.ts @@ -28,4 +28,22 @@ describe('expressionResolver', () => { expect(typeof model.myJsVar).to.equal("string"); expect(model.myJsVar).to.equal('return "Hello here"'); }); + + it('should return undefined when referenced variable is not exist in model', () => { + let expression = new ExpressionResolver(model, 'myJsVar', 'return $data.unknownVariable'); + + expression.resolve(); + + expect(expression.hasDependencies()).to.equal(true); + expect(model.myJsVar).to.equal(undefined); + }); + + it('should return undefined when variable used in expression is invalid', () => { + let expression = new ExpressionResolver(model, 'myJsVar', 'return $unknownVariable'); + + expression.resolve(); + + expect(expression.hasDependencies()).to.equal(false); + expect(model.myJsVar).to.equal(undefined); + }); }); \ No newline at end of file