From da13e3923e6f13e43f398c7505ef739b48cb95e7 Mon Sep 17 00:00:00 2001 From: Travis Tidwell Date: Wed, 22 May 2024 14:20:42 -0500 Subject: [PATCH] Fixing the core evaluator integration. --- package.json | 2 +- src/Element.js | 7 ++-- src/Webform.unit.js | 2 -- src/Wizard.unit.js | 3 -- src/WizardBuilder.unit.js | 3 -- src/utils/Evaluator.js | 5 +-- src/utils/utils.js | 67 +++------------------------------------ test/harness.js | 3 +- yarn.lock | 8 ++--- 9 files changed, 16 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index 5c7c62430c..3a0542e4ae 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "dependencies": { "@formio/bootstrap": "^3.0.0-rc.24", "@formio/choices.js": "^10.2.1", - "@formio/core": "^2.1.0-dev.tt.10", + "@formio/core": "^2.1.0-dev.tt.12", "@formio/text-mask-addons": "^3.8.0-formio.2", "@formio/vanilla-text-mask": "^5.1.1-formio.1", "abortcontroller-polyfill": "^1.7.5", diff --git a/src/Element.js b/src/Element.js index e40f112cb6..f663ae9b44 100644 --- a/src/Element.js +++ b/src/Element.js @@ -594,11 +594,12 @@ export default class Element { * @param {string|Function|object} func - The function or string to evaluate. * @param {object} args - The arguments to pass to the evaluation. * @param {string} ret - The name of the variable within the evaluation context to return. - * @param {boolean} tokenize - Determines if it should replace all {{ }} token references with actual data. + * @param {boolean} interpolate - Determines if it should replace all {{ }} token references with actual data. + * @param {import('@formio/core').EvaluatorOptions} options - The options to pass to the evaluation. * @returns {*} - The result of the evaluation. */ - evaluate(func, args, ret, tokenize) { - return FormioUtils.evaluate(func, this.evalContext(args), ret, tokenize); + evaluate(func, args, ret, interpolate, options = {}) { + return FormioUtils.evaluate(func, this.evalContext(args), ret, interpolate, options); } /** diff --git a/src/Webform.unit.js b/src/Webform.unit.js index 53c9ae846b..5ee1c74fec 100644 --- a/src/Webform.unit.js +++ b/src/Webform.unit.js @@ -79,8 +79,6 @@ import formWithValidation from '../test/forms/formWithValidation'; import formWithNotAllowedTags from '../test/forms/formWithNotAllowedTags'; import formWithValidateWhenHidden from '../test/forms/formWithValidateWhenHidden'; const SpySanitize = sinon.spy(FormioUtils, 'sanitize'); -global.requestAnimationFrame = (cb) => cb(); -global.cancelAnimationFrame = () => {}; if (_.has(Formio, 'Components.setComponents')) { Formio.Components.setComponents(AllComponents); diff --git a/src/Wizard.unit.js b/src/Wizard.unit.js index 1a2a18ee0d..570a01564c 100644 --- a/src/Wizard.unit.js +++ b/src/Wizard.unit.js @@ -42,9 +42,6 @@ import formWithFormController from '../test/forms/formWithFormController'; import { fastCloneDeep } from './utils/utils'; import formsWithAllowOverride from '../test/forms/formsWithAllowOverrideComps'; -global.requestAnimationFrame = (cb) => cb(); -global.cancelAnimationFrame = () => {}; - // eslint-disable-next-line max-statements describe('Wizard tests', () => { it('Should recalculate values for components with "allow override" after wizard is canceled', function(done) { diff --git a/src/WizardBuilder.unit.js b/src/WizardBuilder.unit.js index 587ab3857d..4a4362a476 100644 --- a/src/WizardBuilder.unit.js +++ b/src/WizardBuilder.unit.js @@ -5,9 +5,6 @@ import simpleWizard from '../test/forms/simpleWizard'; import formWithFormController from '../test/forms/formWithFormController'; import { fastCloneDeep } from './utils/utils'; -global.requestAnimationFrame = (cb) => cb(); -global.cancelAnimationFrame = () => {}; - describe('WizardBuilder tests', function() { let formBuilderElement, formBuilder; after((done) => { diff --git a/src/utils/Evaluator.js b/src/utils/Evaluator.js index 8d00349747..c1098e8509 100644 --- a/src/utils/Evaluator.js +++ b/src/utils/Evaluator.js @@ -1,6 +1,6 @@ import _ from 'lodash'; import stringHash from 'string-hash'; -import { Evaluator as CoreEvaluator } from '@formio/core/utils'; +import { JSONLogicEvaluator as CoreEvaluator } from '@formio/core/utils'; export class Evaluator extends CoreEvaluator { static cache = {}; @@ -52,7 +52,4 @@ export class Evaluator extends CoreEvaluator { } return template; } - static evaluate(func, args) { - return Array.isArray(args) ? func(...args) : func(args); - } } diff --git a/src/utils/utils.js b/src/utils/utils.js index 134b7cf03d..1cc424c0b0 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -55,71 +55,12 @@ function setPathToComponentAndPerentSchema(component) { * @param {Function|string|object} func - The function to evaluate. * @param {*} args - A map of arguments to pass to the function. * @param {string} ret - The name of the "return" variable in the script. - * @param {boolean} tokenize - True if the script should be interpolated before being executed. + * @param {boolean} interpolate - True if the script should be interpolated before being executed. + * @param {import('@formio/core').EvaluatorOptions} options - The evaluator options. * @returns {*} - The result of the evaluation. */ -export function evaluate(func, args, ret, tokenize) { - let returnVal = null; - const component = args.component ? args.component : { key: 'unknown' }; - if (!args.form && args.instance) { - args.form = _.get(args.instance, 'root._form', {}); - } - - const componentKey = component.key; - - if (typeof func === 'string') { - if (ret) { - func += `;return ${ret}`; - } - - if (tokenize) { - // Replace all {{ }} references with actual data. - func = func.replace(/({{\s+(.*)\s+}})/, (match, $1, $2) => { - if ($2.indexOf('data.') === 0) { - return _.get(args.data, $2.replace('data.', '')); - } - else if ($2.indexOf('row.') === 0) { - return _.get(args.row, $2.replace('row.', '')); - } - - // Support legacy... - return _.get(args.data, $2); - }); - } - - try { - func = Evaluator.evaluator(func, args); - args = _.values(args); - } - catch (err) { - console.warn(`An error occured within the custom function for ${componentKey}`, err); - returnVal = null; - func = false; - } - } - - if (typeof func === 'function') { - try { - returnVal = Evaluator.evaluate(func, args); - } - catch (err) { - returnVal = null; - console.warn(`An error occured within custom function for ${componentKey}`, err); - } - } - else if (typeof func === 'object') { - try { - returnVal = jsonLogic.apply(func, args); - } - catch (err) { - returnVal = null; - console.warn(`An error occured within custom function for ${componentKey}`, err); - } - } - else if (func) { - console.warn(`Unknown function type for ${componentKey}`); - } - return returnVal; +export function evaluate(func, args, ret, interpolate, options = {}) { + return Evaluator.evaluate(func, args, ret, interpolate, undefined, options); } /** diff --git a/test/harness.js b/test/harness.js index a8f8f6e0af..cf4f3cc510 100644 --- a/test/harness.js +++ b/test/harness.js @@ -5,7 +5,8 @@ import { expect } from 'chai'; import FormBuilder from '../src/FormBuilder'; import AllComponents from '../src/components'; import Components from '../src/components/Components'; - +global.requestAnimationFrame = (cb) => cb(); +global.cancelAnimationFrame = () => {}; Components.setComponents(AllComponents); if (process) { diff --git a/yarn.lock b/yarn.lock index d3fdb9f69e..88c6a4d531 100644 --- a/yarn.lock +++ b/yarn.lock @@ -277,10 +277,10 @@ fuse.js "^6.6.2" redux "^4.2.0" -"@formio/core@^2.1.0-dev.tt.10": - version "2.1.0-dev.tt.10" - resolved "https://registry.npmjs.org/@formio/core/-/core-2.1.0-dev.tt.10.tgz#9518055acfc323db9042aeab983cba828a270899" - integrity sha512-nhveqoFtQMDxKw828jww3dbINyWv0JavUKxDSmWQpvnYKewp/t422XhrPqKbEdRHU3t3b10Au/jW33KLq1JmHw== +"@formio/core@^2.1.0-dev.tt.12": + version "2.1.0-dev.tt.12" + resolved "https://registry.npmjs.org/@formio/core/-/core-2.1.0-dev.tt.12.tgz#cb8dddd535e52c14ee60d5e34dc623de7fc9ac3f" + integrity sha512-9Kns1NDXZbVnQWUFB4qQP/NhNRbVrcvuEwQMp0liLO9fDCDabHRXM84VTBJFvpVxYr0b0be52NLojLJRdR4Ncw== dependencies: "@types/json-logic-js" "^2.0.7" browser-cookies "^1.2.0"