Skip to content

Commit

Permalink
Fixing the core evaluator integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
travist committed May 22, 2024
1 parent 11a9aca commit da13e39
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions src/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/Wizard.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions src/WizardBuilder.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
5 changes: 1 addition & 4 deletions src/utils/Evaluator.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand Down Expand Up @@ -52,7 +52,4 @@ export class Evaluator extends CoreEvaluator {
}
return template;
}
static evaluate(func, args) {
return Array.isArray(args) ? func(...args) : func(args);
}
}
67 changes: 4 additions & 63 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit da13e39

Please sign in to comment.